blob: b9a6a5713b8bb673317c005025bd570d155f3eac [file] [log] [blame]
Steve Narofff8ecff22008-05-01 22:18:59 +00001//===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner0cb78032009-02-24 22:27:37 +000010// This file implements semantic analysis for initializers. The main entry
11// point is Sema::CheckInitList(), but all of the work is performed
12// within the InitListChecker class.
13//
Steve Narofff8ecff22008-05-01 22:18:59 +000014//===----------------------------------------------------------------------===//
15
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/Designator.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Initialization.h"
18#include "clang/Sema/Lookup.h"
John McCall83024632010-08-25 22:03:47 +000019#include "clang/Sema/SemaInternal.h"
Tanya Lattner5029d562010-03-07 04:17:15 +000020#include "clang/Lex/Preprocessor.h"
Steve Narofff8ecff22008-05-01 22:18:59 +000021#include "clang/AST/ASTContext.h"
John McCallde6836a2010-08-24 07:21:54 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson98cee2f2009-05-27 16:10:08 +000023#include "clang/AST/ExprCXX.h"
Chris Lattnerd8b741c82009-02-24 23:10:27 +000024#include "clang/AST/ExprObjC.h"
Douglas Gregor1b303932009-12-22 15:35:07 +000025#include "clang/AST/TypeLoc.h"
Douglas Gregor3e1e5272009-12-09 23:02:17 +000026#include "llvm/Support/ErrorHandling.h"
Douglas Gregor85df8d82009-01-29 00:45:39 +000027#include <map>
Douglas Gregore4a0bb72009-01-22 00:58:24 +000028using namespace clang;
Steve Narofff8ecff22008-05-01 22:18:59 +000029
Chris Lattner0cb78032009-02-24 22:27:37 +000030//===----------------------------------------------------------------------===//
31// Sema Initialization Checking
32//===----------------------------------------------------------------------===//
33
Chris Lattnerd8b741c82009-02-24 23:10:27 +000034static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
Chris Lattnera9196812009-02-26 23:26:43 +000035 const ArrayType *AT = Context.getAsArrayType(DeclType);
36 if (!AT) return 0;
37
Eli Friedman893abe42009-05-29 18:22:49 +000038 if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
39 return 0;
40
Chris Lattnera9196812009-02-26 23:26:43 +000041 // See if this is a string literal or @encode.
42 Init = Init->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattnera9196812009-02-26 23:26:43 +000044 // Handle @encode, which is a narrow string.
45 if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
46 return Init;
47
48 // Otherwise we can only handle string literals.
49 StringLiteral *SL = dyn_cast<StringLiteral>(Init);
Chris Lattner012b3392009-02-26 23:42:47 +000050 if (SL == 0) return 0;
Eli Friedman42a84652009-05-31 10:54:53 +000051
52 QualType ElemTy = Context.getCanonicalType(AT->getElementType());
Chris Lattnera9196812009-02-26 23:26:43 +000053 // char array can be initialized with a narrow string.
54 // Only allow char x[] = "foo"; not char x[] = L"foo";
55 if (!SL->isWide())
Eli Friedman42a84652009-05-31 10:54:53 +000056 return ElemTy->isCharType() ? Init : 0;
Chris Lattnera9196812009-02-26 23:26:43 +000057
Eli Friedman42a84652009-05-31 10:54:53 +000058 // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
59 // correction from DR343): "An array with element type compatible with a
60 // qualified or unqualified version of wchar_t may be initialized by a wide
61 // string literal, optionally enclosed in braces."
62 if (Context.typesAreCompatible(Context.getWCharType(),
63 ElemTy.getUnqualifiedType()))
Chris Lattnera9196812009-02-26 23:26:43 +000064 return Init;
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner0cb78032009-02-24 22:27:37 +000066 return 0;
67}
68
Chris Lattnerd8b741c82009-02-24 23:10:27 +000069static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
70 // Get the length of the string as parsed.
71 uint64_t StrLength =
72 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
73
Mike Stump11289f42009-09-09 15:08:12 +000074
Chris Lattnerd8b741c82009-02-24 23:10:27 +000075 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattner0cb78032009-02-24 22:27:37 +000076 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Mike Stump11289f42009-09-09 15:08:12 +000077 // C99 6.7.8p14. We have an array of character type with unknown size
Chris Lattner0cb78032009-02-24 22:27:37 +000078 // being initialized to a string literal.
79 llvm::APSInt ConstVal(32);
Chris Lattner94e6c4b2009-02-24 23:01:39 +000080 ConstVal = StrLength;
Chris Lattner0cb78032009-02-24 22:27:37 +000081 // Return a new array type (C99 6.7.8p22).
John McCallc5b82252009-10-16 00:14:28 +000082 DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
83 ConstVal,
84 ArrayType::Normal, 0);
Chris Lattner94e6c4b2009-02-24 23:01:39 +000085 return;
Chris Lattner0cb78032009-02-24 22:27:37 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Eli Friedman893abe42009-05-29 18:22:49 +000088 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
Mike Stump11289f42009-09-09 15:08:12 +000089
Eli Friedman893abe42009-05-29 18:22:49 +000090 // C99 6.7.8p14. We have an array of character type with known size. However,
91 // the size may be smaller or larger than the string we are initializing.
92 // FIXME: Avoid truncation for 64-bit length strings.
93 if (StrLength-1 > CAT->getSize().getZExtValue())
94 S.Diag(Str->getSourceRange().getBegin(),
95 diag::warn_initializer_string_for_char_array_too_long)
96 << Str->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +000097
Eli Friedman893abe42009-05-29 18:22:49 +000098 // Set the type to the actual size that we are initializing. If we have
99 // something like:
100 // char x[1] = "foo";
101 // then this will set the string literal's type to char[1].
102 Str->setType(DeclT);
Chris Lattner0cb78032009-02-24 22:27:37 +0000103}
104
Chris Lattner0cb78032009-02-24 22:27:37 +0000105//===----------------------------------------------------------------------===//
106// Semantic checking for initializer lists.
107//===----------------------------------------------------------------------===//
108
Douglas Gregorcde232f2009-01-29 01:05:33 +0000109/// @brief Semantic checking for initializer lists.
110///
111/// The InitListChecker class contains a set of routines that each
112/// handle the initialization of a certain kind of entity, e.g.,
113/// arrays, vectors, struct/union types, scalars, etc. The
114/// InitListChecker itself performs a recursive walk of the subobject
115/// structure of the type to be initialized, while stepping through
116/// the initializer list one element at a time. The IList and Index
117/// parameters to each of the Check* routines contain the active
118/// (syntactic) initializer list and the index into that initializer
119/// list that represents the current initializer. Each routine is
120/// responsible for moving that Index forward as it consumes elements.
121///
122/// Each Check* routine also has a StructuredList/StructuredIndex
Abramo Bagnara92141d22011-01-27 19:55:10 +0000123/// arguments, which contains the current "structured" (semantic)
Douglas Gregorcde232f2009-01-29 01:05:33 +0000124/// initializer list and the index into that initializer list where we
125/// are copying initializers as we map them over to the semantic
126/// list. Once we have completed our recursive walk of the subobject
127/// structure, we will have constructed a full semantic initializer
128/// list.
129///
130/// C99 designators cause changes in the initializer list traversal,
131/// because they make the initialization "jump" into a specific
132/// subobject and then continue the initialization from that
133/// point. CheckDesignatedInitializer() recursively steps into the
134/// designated subobject and manages backing out the recursion to
135/// initialize the subobjects after the one designated.
Chris Lattner9ececce2009-02-24 22:48:58 +0000136namespace {
Douglas Gregor85df8d82009-01-29 00:45:39 +0000137class InitListChecker {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000138 Sema &SemaRef;
Douglas Gregor85df8d82009-01-29 00:45:39 +0000139 bool hadError;
140 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
141 InitListExpr *FullyStructuredList;
Mike Stump11289f42009-09-09 15:08:12 +0000142
Anders Carlsson6cabf312010-01-23 23:23:01 +0000143 void CheckImplicitInitList(const InitializedEntity &Entity,
Anders Carlssondbb25a32010-01-23 20:47:59 +0000144 InitListExpr *ParentIList, QualType T,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000145 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000146 unsigned &StructuredIndex,
147 bool TopLevelObject = false);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000148 void CheckExplicitInitList(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000149 InitListExpr *IList, QualType &T,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000150 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000151 unsigned &StructuredIndex,
152 bool TopLevelObject = false);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000153 void CheckListElementTypes(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000154 InitListExpr *IList, QualType &DeclType,
Mike Stump11289f42009-09-09 15:08:12 +0000155 bool SubobjectIsDesignatorContext,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000156 unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000157 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000158 unsigned &StructuredIndex,
159 bool TopLevelObject = false);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000160 void CheckSubElementType(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000161 InitListExpr *IList, QualType ElemType,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000162 unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000163 InitListExpr *StructuredList,
164 unsigned &StructuredIndex);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000165 void CheckScalarType(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000166 InitListExpr *IList, QualType DeclType,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000167 unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000168 InitListExpr *StructuredList,
169 unsigned &StructuredIndex);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000170 void CheckReferenceType(const InitializedEntity &Entity,
171 InitListExpr *IList, QualType DeclType,
Douglas Gregord14247a2009-01-30 22:09:00 +0000172 unsigned &Index,
173 InitListExpr *StructuredList,
174 unsigned &StructuredIndex);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000175 void CheckVectorType(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000176 InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000177 InitListExpr *StructuredList,
178 unsigned &StructuredIndex);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000179 void CheckStructUnionTypes(const InitializedEntity &Entity,
Anders Carlsson73eb7cd2010-01-23 20:20:40 +0000180 InitListExpr *IList, QualType DeclType,
Mike Stump11289f42009-09-09 15:08:12 +0000181 RecordDecl::field_iterator Field,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000182 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000183 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000184 unsigned &StructuredIndex,
185 bool TopLevelObject = false);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000186 void CheckArrayType(const InitializedEntity &Entity,
Anders Carlsson0cf999b2010-01-23 20:13:41 +0000187 InitListExpr *IList, QualType &DeclType,
Mike Stump11289f42009-09-09 15:08:12 +0000188 llvm::APSInt elementIndex,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000189 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000190 InitListExpr *StructuredList,
191 unsigned &StructuredIndex);
Anders Carlsson6cabf312010-01-23 23:23:01 +0000192 bool CheckDesignatedInitializer(const InitializedEntity &Entity,
Anders Carlsson3fa93b72010-01-23 22:49:02 +0000193 InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregora5324162009-04-15 04:56:10 +0000194 unsigned DesigIdx,
Mike Stump11289f42009-09-09 15:08:12 +0000195 QualType &CurrentObjectType,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000196 RecordDecl::field_iterator *NextField,
197 llvm::APSInt *NextElementIndex,
198 unsigned &Index,
199 InitListExpr *StructuredList,
200 unsigned &StructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000201 bool FinishSubobjectInit,
202 bool TopLevelObject);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000203 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
204 QualType CurrentObjectType,
205 InitListExpr *StructuredList,
206 unsigned StructuredIndex,
207 SourceRange InitRange);
Douglas Gregorcde232f2009-01-29 01:05:33 +0000208 void UpdateStructuredListElement(InitListExpr *StructuredList,
209 unsigned &StructuredIndex,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000210 Expr *expr);
211 int numArrayElements(QualType DeclType);
212 int numStructUnionElements(QualType DeclType);
Douglas Gregord14247a2009-01-30 22:09:00 +0000213
Douglas Gregor2bb07652009-12-22 00:05:34 +0000214 void FillInValueInitForField(unsigned Init, FieldDecl *Field,
215 const InitializedEntity &ParentEntity,
216 InitListExpr *ILE, bool &RequiresSecondPass);
Douglas Gregor723796a2009-12-16 06:35:08 +0000217 void FillInValueInitializations(const InitializedEntity &Entity,
218 InitListExpr *ILE, bool &RequiresSecondPass);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000219public:
Douglas Gregor723796a2009-12-16 06:35:08 +0000220 InitListChecker(Sema &S, const InitializedEntity &Entity,
221 InitListExpr *IL, QualType &T);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000222 bool HadError() { return hadError; }
223
224 // @brief Retrieves the fully-structured initializer list used for
225 // semantic analysis and code generation.
226 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
227};
Chris Lattner9ececce2009-02-24 22:48:58 +0000228} // end anonymous namespace
Chris Lattnerd9ae05b2009-01-29 05:10:57 +0000229
Douglas Gregor2bb07652009-12-22 00:05:34 +0000230void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
231 const InitializedEntity &ParentEntity,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000232 InitListExpr *ILE,
Douglas Gregor2bb07652009-12-22 00:05:34 +0000233 bool &RequiresSecondPass) {
234 SourceLocation Loc = ILE->getSourceRange().getBegin();
235 unsigned NumInits = ILE->getNumInits();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000236 InitializedEntity MemberEntity
Douglas Gregor2bb07652009-12-22 00:05:34 +0000237 = InitializedEntity::InitializeMember(Field, &ParentEntity);
238 if (Init >= NumInits || !ILE->getInit(Init)) {
239 // FIXME: We probably don't need to handle references
240 // specially here, since value-initialization of references is
241 // handled in InitializationSequence.
242 if (Field->getType()->isReferenceType()) {
243 // C++ [dcl.init.aggr]p9:
244 // If an incomplete or empty initializer-list leaves a
245 // member of reference type uninitialized, the program is
246 // ill-formed.
247 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
248 << Field->getType()
249 << ILE->getSyntacticForm()->getSourceRange();
250 SemaRef.Diag(Field->getLocation(),
251 diag::note_uninit_reference_member);
252 hadError = true;
253 return;
254 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000255
Douglas Gregor2bb07652009-12-22 00:05:34 +0000256 InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
257 true);
258 InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
259 if (!InitSeq) {
260 InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
261 hadError = true;
262 return;
263 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000264
John McCalldadc5752010-08-24 06:29:42 +0000265 ExprResult MemberInit
John McCallfaf5fb42010-08-26 23:41:50 +0000266 = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg());
Douglas Gregor2bb07652009-12-22 00:05:34 +0000267 if (MemberInit.isInvalid()) {
268 hadError = true;
269 return;
270 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000271
Douglas Gregor2bb07652009-12-22 00:05:34 +0000272 if (hadError) {
273 // Do nothing
274 } else if (Init < NumInits) {
275 ILE->setInit(Init, MemberInit.takeAs<Expr>());
276 } else if (InitSeq.getKind()
277 == InitializationSequence::ConstructorInitialization) {
278 // Value-initialization requires a constructor call, so
279 // extend the initializer list to include the constructor
280 // call and make a note that we'll need to take another pass
281 // through the initializer list.
Ted Kremenekac034612010-04-13 23:39:13 +0000282 ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>());
Douglas Gregor2bb07652009-12-22 00:05:34 +0000283 RequiresSecondPass = true;
284 }
285 } else if (InitListExpr *InnerILE
286 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000287 FillInValueInitializations(MemberEntity, InnerILE,
288 RequiresSecondPass);
Douglas Gregor2bb07652009-12-22 00:05:34 +0000289}
290
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000291/// Recursively replaces NULL values within the given initializer list
292/// with expressions that perform value-initialization of the
293/// appropriate type.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000294void
Douglas Gregor723796a2009-12-16 06:35:08 +0000295InitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
296 InitListExpr *ILE,
297 bool &RequiresSecondPass) {
Mike Stump11289f42009-09-09 15:08:12 +0000298 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregord14247a2009-01-30 22:09:00 +0000299 "Should not have void type");
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000300 SourceLocation Loc = ILE->getSourceRange().getBegin();
301 if (ILE->getSyntacticForm())
302 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump11289f42009-09-09 15:08:12 +0000303
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000304 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregor2bb07652009-12-22 00:05:34 +0000305 if (RType->getDecl()->isUnion() &&
306 ILE->getInitializedFieldInUnion())
307 FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
308 Entity, ILE, RequiresSecondPass);
309 else {
310 unsigned Init = 0;
311 for (RecordDecl::field_iterator
312 Field = RType->getDecl()->field_begin(),
313 FieldEnd = RType->getDecl()->field_end();
314 Field != FieldEnd; ++Field) {
315 if (Field->isUnnamedBitfield())
316 continue;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000317
Douglas Gregor2bb07652009-12-22 00:05:34 +0000318 if (hadError)
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000319 return;
Douglas Gregor2bb07652009-12-22 00:05:34 +0000320
321 FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
322 if (hadError)
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000323 return;
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000324
Douglas Gregor2bb07652009-12-22 00:05:34 +0000325 ++Init;
Douglas Gregor723796a2009-12-16 06:35:08 +0000326
Douglas Gregor2bb07652009-12-22 00:05:34 +0000327 // Only look at the first initialization of a union.
328 if (RType->getDecl()->isUnion())
329 break;
330 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000331 }
332
333 return;
Mike Stump11289f42009-09-09 15:08:12 +0000334 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000335
336 QualType ElementType;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Douglas Gregor723796a2009-12-16 06:35:08 +0000338 InitializedEntity ElementEntity = Entity;
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000339 unsigned NumInits = ILE->getNumInits();
340 unsigned NumElements = NumInits;
Chris Lattnerb0912a52009-02-24 22:50:46 +0000341 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000342 ElementType = AType->getElementType();
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000343 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
344 NumElements = CAType->getSize().getZExtValue();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000345 ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
Douglas Gregor723796a2009-12-16 06:35:08 +0000346 0, Entity);
John McCall9dd450b2009-09-21 23:43:11 +0000347 } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000348 ElementType = VType->getElementType();
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000349 NumElements = VType->getNumElements();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000350 ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
Douglas Gregor723796a2009-12-16 06:35:08 +0000351 0, Entity);
Mike Stump11289f42009-09-09 15:08:12 +0000352 } else
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000353 ElementType = ILE->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000354
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000355
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000356 for (unsigned Init = 0; Init != NumElements; ++Init) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000357 if (hadError)
358 return;
359
Anders Carlssoned8d80d2010-01-23 04:34:47 +0000360 if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
361 ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
Douglas Gregor723796a2009-12-16 06:35:08 +0000362 ElementEntity.setElementIndex(Init);
363
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000364 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregor723796a2009-12-16 06:35:08 +0000365 InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
366 true);
367 InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
368 if (!InitSeq) {
369 InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000370 hadError = true;
371 return;
372 }
373
John McCalldadc5752010-08-24 06:29:42 +0000374 ExprResult ElementInit
John McCallfaf5fb42010-08-26 23:41:50 +0000375 = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg());
Douglas Gregor723796a2009-12-16 06:35:08 +0000376 if (ElementInit.isInvalid()) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000377 hadError = true;
Douglas Gregor723796a2009-12-16 06:35:08 +0000378 return;
379 }
380
381 if (hadError) {
382 // Do nothing
383 } else if (Init < NumInits) {
384 ILE->setInit(Init, ElementInit.takeAs<Expr>());
385 } else if (InitSeq.getKind()
386 == InitializationSequence::ConstructorInitialization) {
387 // Value-initialization requires a constructor call, so
388 // extend the initializer list to include the constructor
389 // call and make a note that we'll need to take another pass
390 // through the initializer list.
Ted Kremenekac034612010-04-13 23:39:13 +0000391 ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>());
Douglas Gregor723796a2009-12-16 06:35:08 +0000392 RequiresSecondPass = true;
393 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000394 } else if (InitListExpr *InnerILE
Douglas Gregor723796a2009-12-16 06:35:08 +0000395 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
396 FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000397 }
398}
399
Chris Lattnerd9ae05b2009-01-29 05:10:57 +0000400
Douglas Gregor723796a2009-12-16 06:35:08 +0000401InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
402 InitListExpr *IL, QualType &T)
Chris Lattnerb0912a52009-02-24 22:50:46 +0000403 : SemaRef(S) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000404 hadError = false;
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000405
Eli Friedman23a9e312008-05-19 19:16:24 +0000406 unsigned newIndex = 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000407 unsigned newStructuredIndex = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000408 FullyStructuredList
Douglas Gregor5741efb2009-03-01 17:12:46 +0000409 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000410 CheckExplicitInitList(Entity, IL, T, newIndex,
Anders Carlssond0849252010-01-23 19:55:29 +0000411 FullyStructuredList, newStructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000412 /*TopLevelObject=*/true);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000413
Douglas Gregor723796a2009-12-16 06:35:08 +0000414 if (!hadError) {
415 bool RequiresSecondPass = false;
416 FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000417 if (RequiresSecondPass && !hadError)
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000418 FillInValueInitializations(Entity, FullyStructuredList,
Douglas Gregor723796a2009-12-16 06:35:08 +0000419 RequiresSecondPass);
420 }
Steve Narofff8ecff22008-05-01 22:18:59 +0000421}
422
423int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman85f54972008-05-25 13:22:35 +0000424 // FIXME: use a proper constant
425 int maxElements = 0x7FFFFFFF;
Chris Lattner7adf0762008-08-04 07:31:14 +0000426 if (const ConstantArrayType *CAT =
Chris Lattnerb0912a52009-02-24 22:50:46 +0000427 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000428 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
429 }
430 return maxElements;
431}
432
433int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000434 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000435 int InitializableMembers = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000436 for (RecordDecl::field_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000437 Field = structDecl->field_begin(),
438 FieldEnd = structDecl->field_end();
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000439 Field != FieldEnd; ++Field) {
440 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
441 ++InitializableMembers;
442 }
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +0000443 if (structDecl->isUnion())
Eli Friedman0e56c822008-05-25 14:03:31 +0000444 return std::min(InitializableMembers, 1);
445 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Narofff8ecff22008-05-01 22:18:59 +0000446}
447
Anders Carlsson6cabf312010-01-23 23:23:01 +0000448void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
Anders Carlssondbb25a32010-01-23 20:47:59 +0000449 InitListExpr *ParentIList,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000450 QualType T, unsigned &Index,
451 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000452 unsigned &StructuredIndex,
453 bool TopLevelObject) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000454 int maxElements = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000455
Steve Narofff8ecff22008-05-01 22:18:59 +0000456 if (T->isArrayType())
457 maxElements = numArrayElements(T);
Douglas Gregor8385a062010-04-26 21:31:17 +0000458 else if (T->isRecordType())
Steve Narofff8ecff22008-05-01 22:18:59 +0000459 maxElements = numStructUnionElements(T);
Eli Friedman23a9e312008-05-19 19:16:24 +0000460 else if (T->isVectorType())
John McCall9dd450b2009-09-21 23:43:11 +0000461 maxElements = T->getAs<VectorType>()->getNumElements();
Steve Narofff8ecff22008-05-01 22:18:59 +0000462 else
463 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman23a9e312008-05-19 19:16:24 +0000464
Eli Friedmane0f832b2008-05-25 13:49:22 +0000465 if (maxElements == 0) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000466 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedmane0f832b2008-05-25 13:49:22 +0000467 diag::err_implicit_empty_initializer);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000468 ++Index;
Eli Friedmane0f832b2008-05-25 13:49:22 +0000469 hadError = true;
470 return;
471 }
472
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000473 // Build a structured initializer list corresponding to this subobject.
474 InitListExpr *StructuredSubobjectInitList
Mike Stump11289f42009-09-09 15:08:12 +0000475 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
476 StructuredIndex,
Douglas Gregor5741efb2009-03-01 17:12:46 +0000477 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
478 ParentIList->getSourceRange().getEnd()));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000479 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedman23a9e312008-05-19 19:16:24 +0000480
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000481 // Check the element types and build the structural subobject.
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000482 unsigned StartIndex = Index;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000483 CheckListElementTypes(Entity, ParentIList, T,
Anders Carlssondbb25a32010-01-23 20:47:59 +0000484 /*SubobjectIsDesignatorContext=*/false, Index,
Mike Stump11289f42009-09-09 15:08:12 +0000485 StructuredSubobjectInitList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000486 StructuredSubobjectInitIndex,
487 TopLevelObject);
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000488 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregor07d8e3a2009-03-20 00:32:56 +0000489 StructuredSubobjectInitList->setType(T);
490
Douglas Gregor5741efb2009-03-01 17:12:46 +0000491 // Update the structured sub-object initializer so that it's ending
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000492 // range corresponds with the end of the last initializer it used.
493 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump11289f42009-09-09 15:08:12 +0000494 SourceLocation EndLoc
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000495 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
496 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
497 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000498
Tanya Lattner5029d562010-03-07 04:17:15 +0000499 // Warn about missing braces.
500 if (T->isArrayType() || T->isRecordType()) {
Tanya Lattner5cbff482010-03-07 04:40:06 +0000501 SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
502 diag::warn_missing_braces)
Tanya Lattner5029d562010-03-07 04:17:15 +0000503 << StructuredSubobjectInitList->getSourceRange()
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000504 << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(),
Douglas Gregora771f462010-03-31 17:46:05 +0000505 "{")
506 << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken(
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000507 StructuredSubobjectInitList->getLocEnd()),
Douglas Gregora771f462010-03-31 17:46:05 +0000508 "}");
Tanya Lattner5029d562010-03-07 04:17:15 +0000509 }
Steve Narofff8ecff22008-05-01 22:18:59 +0000510}
511
Anders Carlsson6cabf312010-01-23 23:23:01 +0000512void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000513 InitListExpr *IList, QualType &T,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000514 unsigned &Index,
515 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000516 unsigned &StructuredIndex,
517 bool TopLevelObject) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000518 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000519 SyntacticToSemantic[IList] = StructuredList;
520 StructuredList->setSyntacticForm(IList);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000521 CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true,
Anders Carlssond0849252010-01-23 19:55:29 +0000522 Index, StructuredList, StructuredIndex, TopLevelObject);
Douglas Gregora8a089b2010-07-13 18:40:04 +0000523 QualType ExprTy = T.getNonLValueExprType(SemaRef.Context);
524 IList->setType(ExprTy);
525 StructuredList->setType(ExprTy);
Eli Friedman85f54972008-05-25 13:22:35 +0000526 if (hadError)
527 return;
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000528
Eli Friedman85f54972008-05-25 13:22:35 +0000529 if (Index < IList->getNumInits()) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000530 // We have leftover initializers
Eli Friedmanbd327452009-05-29 20:20:05 +0000531 if (StructuredIndex == 1 &&
532 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000533 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmanbd327452009-05-29 20:20:05 +0000534 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000535 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmanbd327452009-05-29 20:20:05 +0000536 hadError = true;
537 }
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000538 // Special-case
Chris Lattnerb0912a52009-02-24 22:50:46 +0000539 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerf490e152008-11-19 05:27:50 +0000540 << IList->getInit(Index)->getSourceRange();
Eli Friedmand0e48ea2008-05-20 05:25:56 +0000541 } else if (!T->isIncompleteType()) {
Douglas Gregord42a0fb2009-01-30 22:26:29 +0000542 // Don't complain for incomplete types, since we'll get an error
543 // elsewhere
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000544 QualType CurrentObjectType = StructuredList->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000545 int initKind =
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000546 CurrentObjectType->isArrayType()? 0 :
547 CurrentObjectType->isVectorType()? 1 :
548 CurrentObjectType->isScalarType()? 2 :
549 CurrentObjectType->isUnionType()? 3 :
550 4;
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000551
552 unsigned DK = diag::warn_excess_initializers;
Eli Friedmanbd327452009-05-29 20:20:05 +0000553 if (SemaRef.getLangOptions().CPlusPlus) {
554 DK = diag::err_excess_initializers;
555 hadError = true;
556 }
Nate Begeman425038c2009-07-07 21:53:06 +0000557 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
558 DK = diag::err_excess_initializers;
559 hadError = true;
560 }
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000561
Chris Lattnerb0912a52009-02-24 22:50:46 +0000562 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000563 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000564 }
565 }
Eli Friedman6fcdec22008-05-19 20:20:43 +0000566
Eli Friedman0b4af8f2009-05-16 11:45:48 +0000567 if (T->isScalarType() && !TopLevelObject)
Chris Lattnerb0912a52009-02-24 22:50:46 +0000568 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregor170512f2009-04-01 23:51:29 +0000569 << IList->getSourceRange()
Douglas Gregora771f462010-03-31 17:46:05 +0000570 << FixItHint::CreateRemoval(IList->getLocStart())
571 << FixItHint::CreateRemoval(IList->getLocEnd());
Steve Narofff8ecff22008-05-01 22:18:59 +0000572}
573
Anders Carlsson6cabf312010-01-23 23:23:01 +0000574void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000575 InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +0000576 QualType &DeclType,
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000577 bool SubobjectIsDesignatorContext,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000578 unsigned &Index,
579 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000580 unsigned &StructuredIndex,
581 bool TopLevelObject) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000582 if (DeclType->isScalarType()) {
Anders Carlssond0849252010-01-23 19:55:29 +0000583 CheckScalarType(Entity, IList, DeclType, Index,
584 StructuredList, StructuredIndex);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000585 } else if (DeclType->isVectorType()) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000586 CheckVectorType(Entity, IList, DeclType, Index,
Anders Carlssond0849252010-01-23 19:55:29 +0000587 StructuredList, StructuredIndex);
Douglas Gregorddb24852009-01-30 17:31:00 +0000588 } else if (DeclType->isAggregateType()) {
589 if (DeclType->isRecordType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000590 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Anders Carlsson73eb7cd2010-01-23 20:20:40 +0000591 CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000592 SubobjectIsDesignatorContext, Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000593 StructuredList, StructuredIndex,
594 TopLevelObject);
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000595 } else if (DeclType->isArrayType()) {
Douglas Gregor033d1252009-01-23 16:54:12 +0000596 llvm::APSInt Zero(
Chris Lattnerb0912a52009-02-24 22:50:46 +0000597 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregor033d1252009-01-23 16:54:12 +0000598 false);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000599 CheckArrayType(Entity, IList, DeclType, Zero,
Anders Carlsson0cf999b2010-01-23 20:13:41 +0000600 SubobjectIsDesignatorContext, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000601 StructuredList, StructuredIndex);
Mike Stump12b8ce12009-08-04 21:02:39 +0000602 } else
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000603 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroffeaf58532008-08-10 16:05:48 +0000604 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
605 // This type is invalid, issue a diagnostic.
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000606 ++Index;
Chris Lattnerb0912a52009-02-24 22:50:46 +0000607 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000608 << DeclType;
Eli Friedmand0e48ea2008-05-20 05:25:56 +0000609 hadError = true;
Douglas Gregord14247a2009-01-30 22:09:00 +0000610 } else if (DeclType->isRecordType()) {
611 // C++ [dcl.init]p14:
612 // [...] If the class is an aggregate (8.5.1), and the initializer
613 // is a brace-enclosed list, see 8.5.1.
614 //
615 // Note: 8.5.1 is handled below; here, we diagnose the case where
616 // we have an initializer list and a destination type that is not
617 // an aggregate.
618 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattnerb0912a52009-02-24 22:50:46 +0000619 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord14247a2009-01-30 22:09:00 +0000620 << DeclType << IList->getSourceRange();
621 hadError = true;
622 } else if (DeclType->isReferenceType()) {
Anders Carlsson6cabf312010-01-23 23:23:01 +0000623 CheckReferenceType(Entity, IList, DeclType, Index,
624 StructuredList, StructuredIndex);
John McCall8b07ec22010-05-15 11:32:37 +0000625 } else if (DeclType->isObjCObjectType()) {
Douglas Gregor50ec46d2010-05-03 18:24:37 +0000626 SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
627 << DeclType;
628 hadError = true;
Steve Narofff8ecff22008-05-01 22:18:59 +0000629 } else {
Douglas Gregor50ec46d2010-05-03 18:24:37 +0000630 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
631 << DeclType;
632 hadError = true;
Steve Narofff8ecff22008-05-01 22:18:59 +0000633 }
634}
635
Anders Carlsson6cabf312010-01-23 23:23:01 +0000636void InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000637 InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +0000638 QualType ElemType,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000639 unsigned &Index,
640 InitListExpr *StructuredList,
641 unsigned &StructuredIndex) {
Douglas Gregorf6d27522009-01-29 00:39:20 +0000642 Expr *expr = IList->getInit(Index);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000643 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
644 unsigned newIndex = 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000645 unsigned newStructuredIndex = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000646 InitListExpr *newStructuredList
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000647 = getStructuredSubobjectInit(IList, Index, ElemType,
648 StructuredList, StructuredIndex,
649 SubInitList->getSourceRange());
Anders Carlssond0849252010-01-23 19:55:29 +0000650 CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000651 newStructuredList, newStructuredIndex);
652 ++StructuredIndex;
653 ++Index;
Chris Lattnerd8b741c82009-02-24 23:10:27 +0000654 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
655 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattneredbf3ba2009-02-24 22:41:04 +0000656 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000657 ++Index;
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000658 } else if (ElemType->isScalarType()) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000659 CheckScalarType(Entity, IList, ElemType, Index,
Anders Carlssond0849252010-01-23 19:55:29 +0000660 StructuredList, StructuredIndex);
Douglas Gregord14247a2009-01-30 22:09:00 +0000661 } else if (ElemType->isReferenceType()) {
Anders Carlsson6cabf312010-01-23 23:23:01 +0000662 CheckReferenceType(Entity, IList, ElemType, Index,
663 StructuredList, StructuredIndex);
Eli Friedman23a9e312008-05-19 19:16:24 +0000664 } else {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000665 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000666 // C++ [dcl.init.aggr]p12:
667 // All implicit type conversions (clause 4) are considered when
668 // initializing the aggregate member with an ini- tializer from
669 // an initializer-list. If the initializer can initialize a
670 // member, the member is initialized. [...]
Anders Carlsson03068aa2009-08-27 17:18:13 +0000671
Anders Carlsson0bd52402010-01-24 00:19:41 +0000672 // FIXME: Better EqualLoc?
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000673 InitializationKind Kind =
Anders Carlsson0bd52402010-01-24 00:19:41 +0000674 InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation());
675 InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000676
Anders Carlsson0bd52402010-01-24 00:19:41 +0000677 if (Seq) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000678 ExprResult Result =
John McCallfaf5fb42010-08-26 23:41:50 +0000679 Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1));
Anders Carlsson0bd52402010-01-24 00:19:41 +0000680 if (Result.isInvalid())
Douglas Gregord14247a2009-01-30 22:09:00 +0000681 hadError = true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000682
683 UpdateStructuredListElement(StructuredList, StructuredIndex,
Anders Carlsson0bd52402010-01-24 00:19:41 +0000684 Result.takeAs<Expr>());
Douglas Gregord14247a2009-01-30 22:09:00 +0000685 ++Index;
686 return;
687 }
688
689 // Fall through for subaggregate initialization
690 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000691 // C99 6.7.8p13:
Douglas Gregord14247a2009-01-30 22:09:00 +0000692 //
693 // The initializer for a structure or union object that has
694 // automatic storage duration shall be either an initializer
695 // list as described below, or a single expression that has
696 // compatible structure or union type. In the latter case, the
697 // initial value of the object, including unnamed members, is
698 // that of the expression.
Eli Friedman9782caa2009-06-13 10:38:46 +0000699 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
Argyrios Kyrtzidis0d349f92011-02-01 00:52:10 +0000700 SemaRef.CheckSingleAssignmentConstraints(ElemType, expr)
701 == Sema::Compatible) {
John McCall211e6992010-12-04 09:03:57 +0000702 SemaRef.DefaultFunctionArrayLvalueConversion(expr);
Douglas Gregord14247a2009-01-30 22:09:00 +0000703 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
704 ++Index;
705 return;
706 }
707
708 // Fall through for subaggregate initialization
709 }
710
711 // C++ [dcl.init.aggr]p12:
Mike Stump11289f42009-09-09 15:08:12 +0000712 //
Douglas Gregord14247a2009-01-30 22:09:00 +0000713 // [...] Otherwise, if the member is itself a non-empty
714 // subaggregate, brace elision is assumed and the initializer is
715 // considered for the initialization of the first member of
716 // the subaggregate.
717 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
Anders Carlssondbb25a32010-01-23 20:47:59 +0000718 CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
Douglas Gregord14247a2009-01-30 22:09:00 +0000719 StructuredIndex);
720 ++StructuredIndex;
721 } else {
722 // We cannot initialize this element, so let
723 // PerformCopyInitialization produce the appropriate diagnostic.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000724 SemaRef.PerformCopyInitialization(Entity, SourceLocation(),
Anders Carlssona18f0fb2010-01-30 01:56:32 +0000725 SemaRef.Owned(expr));
Douglas Gregord14247a2009-01-30 22:09:00 +0000726 hadError = true;
727 ++Index;
728 ++StructuredIndex;
729 }
730 }
Eli Friedman23a9e312008-05-19 19:16:24 +0000731}
732
Anders Carlsson6cabf312010-01-23 23:23:01 +0000733void InitListChecker::CheckScalarType(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000734 InitListExpr *IList, QualType DeclType,
Douglas Gregorf6d27522009-01-29 00:39:20 +0000735 unsigned &Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000736 InitListExpr *StructuredList,
737 unsigned &StructuredIndex) {
John McCall643169b2010-11-11 00:46:36 +0000738 if (Index >= IList->getNumInits()) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000739 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerf490e152008-11-19 05:27:50 +0000740 << IList->getSourceRange();
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000741 hadError = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000742 ++Index;
743 ++StructuredIndex;
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000744 return;
Steve Narofff8ecff22008-05-01 22:18:59 +0000745 }
John McCall643169b2010-11-11 00:46:36 +0000746
747 Expr *expr = IList->getInit(Index);
748 if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) {
749 SemaRef.Diag(SubIList->getLocStart(),
750 diag::warn_many_braces_around_scalar_init)
751 << SubIList->getSourceRange();
752
753 CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList,
754 StructuredIndex);
755 return;
756 } else if (isa<DesignatedInitExpr>(expr)) {
757 SemaRef.Diag(expr->getSourceRange().getBegin(),
758 diag::err_designator_for_scalar_init)
759 << DeclType << expr->getSourceRange();
760 hadError = true;
761 ++Index;
762 ++StructuredIndex;
763 return;
764 }
765
766 ExprResult Result =
767 SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
768 SemaRef.Owned(expr));
769
770 Expr *ResultExpr = 0;
771
772 if (Result.isInvalid())
773 hadError = true; // types weren't compatible.
774 else {
775 ResultExpr = Result.takeAs<Expr>();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000776
John McCall643169b2010-11-11 00:46:36 +0000777 if (ResultExpr != expr) {
778 // The type was promoted, update initializer list.
779 IList->setInit(Index, ResultExpr);
780 }
781 }
782 if (hadError)
783 ++StructuredIndex;
784 else
785 UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
786 ++Index;
Steve Narofff8ecff22008-05-01 22:18:59 +0000787}
788
Anders Carlsson6cabf312010-01-23 23:23:01 +0000789void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
790 InitListExpr *IList, QualType DeclType,
Douglas Gregord14247a2009-01-30 22:09:00 +0000791 unsigned &Index,
792 InitListExpr *StructuredList,
793 unsigned &StructuredIndex) {
794 if (Index < IList->getNumInits()) {
795 Expr *expr = IList->getInit(Index);
796 if (isa<InitListExpr>(expr)) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000797 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord14247a2009-01-30 22:09:00 +0000798 << DeclType << IList->getSourceRange();
799 hadError = true;
800 ++Index;
801 ++StructuredIndex;
802 return;
Mike Stump11289f42009-09-09 15:08:12 +0000803 }
Douglas Gregord14247a2009-01-30 22:09:00 +0000804
John McCalldadc5752010-08-24 06:29:42 +0000805 ExprResult Result =
Anders Carlssona91be642010-01-29 02:47:33 +0000806 SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
807 SemaRef.Owned(expr));
808
809 if (Result.isInvalid())
Douglas Gregord14247a2009-01-30 22:09:00 +0000810 hadError = true;
Anders Carlssona91be642010-01-29 02:47:33 +0000811
812 expr = Result.takeAs<Expr>();
813 IList->setInit(Index, expr);
814
Douglas Gregord14247a2009-01-30 22:09:00 +0000815 if (hadError)
816 ++StructuredIndex;
817 else
818 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
819 ++Index;
820 } else {
Mike Stump87c57ac2009-05-16 07:39:55 +0000821 // FIXME: It would be wonderful if we could point at the actual member. In
822 // general, it would be useful to pass location information down the stack,
823 // so that we know the location (or decl) of the "current object" being
824 // initialized.
Mike Stump11289f42009-09-09 15:08:12 +0000825 SemaRef.Diag(IList->getLocStart(),
Douglas Gregord14247a2009-01-30 22:09:00 +0000826 diag::err_init_reference_member_uninitialized)
827 << DeclType
828 << IList->getSourceRange();
829 hadError = true;
830 ++Index;
831 ++StructuredIndex;
832 return;
833 }
834}
835
Anders Carlsson6cabf312010-01-23 23:23:01 +0000836void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
Anders Carlssond0849252010-01-23 19:55:29 +0000837 InitListExpr *IList, QualType DeclType,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000838 unsigned &Index,
839 InitListExpr *StructuredList,
840 unsigned &StructuredIndex) {
John McCall6a16b2f2010-10-30 00:11:39 +0000841 if (Index >= IList->getNumInits())
842 return;
Mike Stump11289f42009-09-09 15:08:12 +0000843
John McCall6a16b2f2010-10-30 00:11:39 +0000844 const VectorType *VT = DeclType->getAs<VectorType>();
845 unsigned maxElements = VT->getNumElements();
846 unsigned numEltsInit = 0;
847 QualType elementType = VT->getElementType();
Anders Carlssond0849252010-01-23 19:55:29 +0000848
John McCall6a16b2f2010-10-30 00:11:39 +0000849 if (!SemaRef.getLangOptions().OpenCL) {
850 // If the initializing element is a vector, try to copy-initialize
851 // instead of breaking it apart (which is doomed to failure anyway).
852 Expr *Init = IList->getInit(Index);
853 if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) {
854 ExprResult Result =
855 SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(),
856 SemaRef.Owned(Init));
857
858 Expr *ResultExpr = 0;
859 if (Result.isInvalid())
860 hadError = true; // types weren't compatible.
861 else {
862 ResultExpr = Result.takeAs<Expr>();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000863
John McCall6a16b2f2010-10-30 00:11:39 +0000864 if (ResultExpr != Init) {
865 // The type was promoted, update initializer list.
866 IList->setInit(Index, ResultExpr);
Nate Begeman5ec4b312009-08-10 23:49:36 +0000867 }
868 }
John McCall6a16b2f2010-10-30 00:11:39 +0000869 if (hadError)
870 ++StructuredIndex;
871 else
872 UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
873 ++Index;
874 return;
Steve Narofff8ecff22008-05-01 22:18:59 +0000875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
John McCall6a16b2f2010-10-30 00:11:39 +0000877 InitializedEntity ElementEntity =
878 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000879
John McCall6a16b2f2010-10-30 00:11:39 +0000880 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
881 // Don't attempt to go past the end of the init list
882 if (Index >= IList->getNumInits())
883 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000884
John McCall6a16b2f2010-10-30 00:11:39 +0000885 ElementEntity.setElementIndex(Index);
886 CheckSubElementType(ElementEntity, IList, elementType, Index,
887 StructuredList, StructuredIndex);
888 }
889 return;
Steve Narofff8ecff22008-05-01 22:18:59 +0000890 }
John McCall6a16b2f2010-10-30 00:11:39 +0000891
892 InitializedEntity ElementEntity =
893 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000894
John McCall6a16b2f2010-10-30 00:11:39 +0000895 // OpenCL initializers allows vectors to be constructed from vectors.
896 for (unsigned i = 0; i < maxElements; ++i) {
897 // Don't attempt to go past the end of the init list
898 if (Index >= IList->getNumInits())
899 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000900
John McCall6a16b2f2010-10-30 00:11:39 +0000901 ElementEntity.setElementIndex(Index);
902
903 QualType IType = IList->getInit(Index)->getType();
904 if (!IType->isVectorType()) {
905 CheckSubElementType(ElementEntity, IList, elementType, Index,
906 StructuredList, StructuredIndex);
907 ++numEltsInit;
908 } else {
909 QualType VecType;
910 const VectorType *IVT = IType->getAs<VectorType>();
911 unsigned numIElts = IVT->getNumElements();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000912
John McCall6a16b2f2010-10-30 00:11:39 +0000913 if (IType->isExtVectorType())
914 VecType = SemaRef.Context.getExtVectorType(elementType, numIElts);
915 else
916 VecType = SemaRef.Context.getVectorType(elementType, numIElts,
Bob Wilsonaeb56442010-11-10 21:56:12 +0000917 IVT->getVectorKind());
John McCall6a16b2f2010-10-30 00:11:39 +0000918 CheckSubElementType(ElementEntity, IList, VecType, Index,
919 StructuredList, StructuredIndex);
920 numEltsInit += numIElts;
921 }
922 }
923
924 // OpenCL requires all elements to be initialized.
925 if (numEltsInit != maxElements)
926 if (SemaRef.getLangOptions().OpenCL)
927 SemaRef.Diag(IList->getSourceRange().getBegin(),
928 diag::err_vector_incorrect_num_initializers)
929 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Narofff8ecff22008-05-01 22:18:59 +0000930}
931
Anders Carlsson6cabf312010-01-23 23:23:01 +0000932void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
Anders Carlsson0cf999b2010-01-23 20:13:41 +0000933 InitListExpr *IList, QualType &DeclType,
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000934 llvm::APSInt elementIndex,
Mike Stump11289f42009-09-09 15:08:12 +0000935 bool SubobjectIsDesignatorContext,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000936 unsigned &Index,
937 InitListExpr *StructuredList,
938 unsigned &StructuredIndex) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000939 // Check for the special-case of initializing an array with a string.
940 if (Index < IList->getNumInits()) {
Chris Lattnerd8b741c82009-02-24 23:10:27 +0000941 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
942 SemaRef.Context)) {
943 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000944 // We place the string literal directly into the resulting
945 // initializer list. This is the only place where the structure
946 // of the structured initializer list doesn't match exactly,
947 // because doing so would involve allocating one character
948 // constant for each string.
Chris Lattneredbf3ba2009-02-24 22:41:04 +0000949 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattnerb0912a52009-02-24 22:50:46 +0000950 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Narofff8ecff22008-05-01 22:18:59 +0000951 ++Index;
Steve Narofff8ecff22008-05-01 22:18:59 +0000952 return;
953 }
954 }
Chris Lattner7adf0762008-08-04 07:31:14 +0000955 if (const VariableArrayType *VAT =
Chris Lattnerb0912a52009-02-24 22:50:46 +0000956 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman85f54972008-05-25 13:22:35 +0000957 // Check for VLAs; in standard C it would be possible to check this
958 // earlier, but I don't know where clang accepts VLAs (gcc accepts
959 // them in all sorts of strange places).
Chris Lattnerb0912a52009-02-24 22:50:46 +0000960 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerf490e152008-11-19 05:27:50 +0000961 diag::err_variable_object_no_init)
962 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman85f54972008-05-25 13:22:35 +0000963 hadError = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000964 ++Index;
965 ++StructuredIndex;
Eli Friedman85f54972008-05-25 13:22:35 +0000966 return;
967 }
968
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000969 // We might know the maximum number of elements in advance.
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000970 llvm::APSInt maxElements(elementIndex.getBitWidth(),
971 elementIndex.isUnsigned());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000972 bool maxElementsKnown = false;
973 if (const ConstantArrayType *CAT =
Chris Lattnerb0912a52009-02-24 22:50:46 +0000974 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000975 maxElements = CAT->getSize();
Jay Foad6d4db0c2010-12-07 08:25:34 +0000976 elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregor583cf0a2009-01-23 18:58:42 +0000977 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000978 maxElementsKnown = true;
979 }
980
Chris Lattnerb0912a52009-02-24 22:50:46 +0000981 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattner7adf0762008-08-04 07:31:14 +0000982 ->getElementType();
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000983 while (Index < IList->getNumInits()) {
984 Expr *Init = IList->getInit(Index);
985 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000986 // If we're not the subobject that matches up with the '{' for
987 // the designator, we shouldn't be handling the
988 // designator. Return immediately.
989 if (!SubobjectIsDesignatorContext)
990 return;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000991
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000992 // Handle this designated initializer. elementIndex will be
993 // updated to be the next array element we'll initialize.
Anders Carlsson3fa93b72010-01-23 22:49:02 +0000994 if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000995 DeclType, 0, &elementIndex, Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000996 StructuredList, StructuredIndex, true,
997 false)) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000998 hadError = true;
999 continue;
1000 }
1001
Douglas Gregor033d1252009-01-23 16:54:12 +00001002 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +00001003 maxElements = maxElements.extend(elementIndex.getBitWidth());
Douglas Gregor033d1252009-01-23 16:54:12 +00001004 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +00001005 elementIndex = elementIndex.extend(maxElements.getBitWidth());
Douglas Gregor583cf0a2009-01-23 18:58:42 +00001006 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor033d1252009-01-23 16:54:12 +00001007
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001008 // If the array is of incomplete type, keep track of the number of
1009 // elements in the initializer.
1010 if (!maxElementsKnown && elementIndex > maxElements)
1011 maxElements = elementIndex;
1012
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001013 continue;
1014 }
1015
1016 // If we know the maximum number of elements, and we've already
1017 // hit it, stop consuming elements in the initializer list.
1018 if (maxElementsKnown && elementIndex == maxElements)
Steve Narofff8ecff22008-05-01 22:18:59 +00001019 break;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001020
Anders Carlsson6cabf312010-01-23 23:23:01 +00001021 InitializedEntity ElementEntity =
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001022 InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex,
Anders Carlsson6cabf312010-01-23 23:23:01 +00001023 Entity);
1024 // Check this element.
1025 CheckSubElementType(ElementEntity, IList, elementType, Index,
1026 StructuredList, StructuredIndex);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001027 ++elementIndex;
1028
1029 // If the array is of incomplete type, keep track of the number of
1030 // elements in the initializer.
1031 if (!maxElementsKnown && elementIndex > maxElements)
1032 maxElements = elementIndex;
Steve Narofff8ecff22008-05-01 22:18:59 +00001033 }
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001034 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Narofff8ecff22008-05-01 22:18:59 +00001035 // If this is an incomplete array type, the actual type needs to
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +00001036 // be calculated here.
Douglas Gregor583cf0a2009-01-23 18:58:42 +00001037 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001038 if (maxElements == Zero) {
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +00001039 // Sizing an array implicitly to zero is not allowed by ISO C,
1040 // but is supported by GNU.
Chris Lattnerb0912a52009-02-24 22:50:46 +00001041 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +00001042 diag::ext_typecheck_zero_array_size);
Steve Narofff8ecff22008-05-01 22:18:59 +00001043 }
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +00001044
Mike Stump11289f42009-09-09 15:08:12 +00001045 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +00001046 ArrayType::Normal, 0);
Steve Narofff8ecff22008-05-01 22:18:59 +00001047 }
1048}
1049
Anders Carlsson6cabf312010-01-23 23:23:01 +00001050void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
Anders Carlsson73eb7cd2010-01-23 20:20:40 +00001051 InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +00001052 QualType DeclType,
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001053 RecordDecl::field_iterator Field,
Mike Stump11289f42009-09-09 15:08:12 +00001054 bool SubobjectIsDesignatorContext,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001055 unsigned &Index,
1056 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001057 unsigned &StructuredIndex,
1058 bool TopLevelObject) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001059 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001060
Eli Friedman23a9e312008-05-19 19:16:24 +00001061 // If the record is invalid, some of it's members are invalid. To avoid
1062 // confusion, we forgo checking the intializer for the entire record.
1063 if (structDecl->isInvalidDecl()) {
1064 hadError = true;
1065 return;
Mike Stump11289f42009-09-09 15:08:12 +00001066 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001067
1068 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1069 // Value-initialize the first named member of the union.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001070 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001071 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor0202cb42009-01-29 17:44:32 +00001072 Field != FieldEnd; ++Field) {
1073 if (Field->getDeclName()) {
1074 StructuredList->setInitializedFieldInUnion(*Field);
1075 break;
1076 }
1077 }
1078 return;
1079 }
1080
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001081 // If structDecl is a forward declaration, this loop won't do
1082 // anything except look at designated initializers; That's okay,
1083 // because an error should get printed out elsewhere. It might be
1084 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001085 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001086 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregora9add4e2009-02-12 19:00:39 +00001087 bool InitializedSomething = false;
John McCalle40b58e2010-03-11 19:32:38 +00001088 bool CheckForMissingFields = true;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001089 while (Index < IList->getNumInits()) {
1090 Expr *Init = IList->getInit(Index);
1091
1092 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001093 // If we're not the subobject that matches up with the '{' for
1094 // the designator, we shouldn't be handling the
1095 // designator. Return immediately.
1096 if (!SubobjectIsDesignatorContext)
1097 return;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001098
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001099 // Handle this designated initializer. Field will be updated to
1100 // the next field that we'll be initializing.
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001101 if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001102 DeclType, &Field, 0, Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001103 StructuredList, StructuredIndex,
1104 true, TopLevelObject))
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001105 hadError = true;
1106
Douglas Gregora9add4e2009-02-12 19:00:39 +00001107 InitializedSomething = true;
John McCalle40b58e2010-03-11 19:32:38 +00001108
1109 // Disable check for missing fields when designators are used.
1110 // This matches gcc behaviour.
1111 CheckForMissingFields = false;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001112 continue;
1113 }
1114
1115 if (Field == FieldEnd) {
1116 // We've run out of fields. We're done.
1117 break;
1118 }
1119
Douglas Gregora9add4e2009-02-12 19:00:39 +00001120 // We've already initialized a member of a union. We're done.
1121 if (InitializedSomething && DeclType->isUnionType())
1122 break;
1123
Douglas Gregor91f84212008-12-11 16:49:14 +00001124 // If we've hit the flexible array member at the end, we're done.
1125 if (Field->getType()->isIncompleteArrayType())
1126 break;
1127
Douglas Gregor51695702009-01-29 16:53:55 +00001128 if (Field->isUnnamedBitfield()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001129 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001130 ++Field;
Eli Friedman23a9e312008-05-19 19:16:24 +00001131 continue;
Steve Narofff8ecff22008-05-01 22:18:59 +00001132 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001133
Anders Carlsson6cabf312010-01-23 23:23:01 +00001134 InitializedEntity MemberEntity =
1135 InitializedEntity::InitializeMember(*Field, &Entity);
1136 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1137 StructuredList, StructuredIndex);
Douglas Gregora9add4e2009-02-12 19:00:39 +00001138 InitializedSomething = true;
Douglas Gregor51695702009-01-29 16:53:55 +00001139
1140 if (DeclType->isUnionType()) {
1141 // Initialize the first field within the union.
1142 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor51695702009-01-29 16:53:55 +00001143 }
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001144
1145 ++Field;
Steve Narofff8ecff22008-05-01 22:18:59 +00001146 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001147
John McCalle40b58e2010-03-11 19:32:38 +00001148 // Emit warnings for missing struct field initializers.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001149 if (InitializedSomething && CheckForMissingFields && Field != FieldEnd &&
John McCalle40b58e2010-03-11 19:32:38 +00001150 !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) {
1151 // It is possible we have one or more unnamed bitfields remaining.
1152 // Find first (if any) named field and emit warning.
1153 for (RecordDecl::field_iterator it = Field, end = RD->field_end();
1154 it != end; ++it) {
1155 if (!it->isUnnamedBitfield()) {
1156 SemaRef.Diag(IList->getSourceRange().getEnd(),
1157 diag::warn_missing_field_initializers) << it->getName();
1158 break;
1159 }
1160 }
1161 }
1162
Mike Stump11289f42009-09-09 15:08:12 +00001163 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001164 Index >= IList->getNumInits())
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001165 return;
1166
1167 // Handle GNU flexible array initializers.
Mike Stump11289f42009-09-09 15:08:12 +00001168 if (!TopLevelObject &&
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001169 (!isa<InitListExpr>(IList->getInit(Index)) ||
1170 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001171 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001172 diag::err_flexible_array_init_nonempty)
1173 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001174 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001175 << *Field;
1176 hadError = true;
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001177 ++Index;
1178 return;
1179 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001180 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001181 diag::ext_flexible_array_init)
1182 << IList->getInit(Index)->getSourceRange().getBegin();
1183 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1184 << *Field;
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001185 }
1186
Anders Carlsson6cabf312010-01-23 23:23:01 +00001187 InitializedEntity MemberEntity =
1188 InitializedEntity::InitializeMember(*Field, &Entity);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001189
Anders Carlsson6cabf312010-01-23 23:23:01 +00001190 if (isa<InitListExpr>(IList->getInit(Index)))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001191 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
Anders Carlsson6cabf312010-01-23 23:23:01 +00001192 StructuredList, StructuredIndex);
1193 else
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001194 CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
Anders Carlssondbb25a32010-01-23 20:47:59 +00001195 StructuredList, StructuredIndex);
Steve Narofff8ecff22008-05-01 22:18:59 +00001196}
Steve Narofff8ecff22008-05-01 22:18:59 +00001197
Douglas Gregord5846a12009-04-15 06:41:24 +00001198/// \brief Expand a field designator that refers to a member of an
1199/// anonymous struct or union into a series of field designators that
1200/// refers to the field within the appropriate subobject.
1201///
Douglas Gregord5846a12009-04-15 06:41:24 +00001202static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump11289f42009-09-09 15:08:12 +00001203 DesignatedInitExpr *DIE,
1204 unsigned DesigIdx,
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001205 IndirectFieldDecl *IndirectField) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001206 typedef DesignatedInitExpr::Designator Designator;
1207
Douglas Gregord5846a12009-04-15 06:41:24 +00001208 // Build the replacement designators.
1209 llvm::SmallVector<Designator, 4> Replacements;
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001210 for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(),
1211 PE = IndirectField->chain_end(); PI != PE; ++PI) {
1212 if (PI + 1 == PE)
Mike Stump11289f42009-09-09 15:08:12 +00001213 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregord5846a12009-04-15 06:41:24 +00001214 DIE->getDesignator(DesigIdx)->getDotLoc(),
1215 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1216 else
1217 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1218 SourceLocation()));
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001219 assert(isa<FieldDecl>(*PI));
1220 Replacements.back().setField(cast<FieldDecl>(*PI));
Douglas Gregord5846a12009-04-15 06:41:24 +00001221 }
1222
1223 // Expand the current designator into the set of replacement
1224 // designators, so we have a full subobject path down to where the
1225 // member of the anonymous struct/union is actually stored.
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001226 DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
Douglas Gregord5846a12009-04-15 06:41:24 +00001227 &Replacements[0] + Replacements.size());
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001228}
Mike Stump11289f42009-09-09 15:08:12 +00001229
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001230/// \brief Given an implicit anonymous field, search the IndirectField that
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001231/// corresponds to FieldName.
1232static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField,
1233 IdentifierInfo *FieldName) {
1234 assert(AnonField->isAnonymousStructOrUnion());
1235 Decl *NextDecl = AnonField->getNextDeclInContext();
1236 while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) {
1237 if (FieldName && FieldName == IF->getAnonField()->getIdentifier())
1238 return IF;
1239 NextDecl = NextDecl->getNextDeclInContext();
Douglas Gregord5846a12009-04-15 06:41:24 +00001240 }
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001241 return 0;
Douglas Gregord5846a12009-04-15 06:41:24 +00001242}
1243
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001244/// @brief Check the well-formedness of a C99 designated initializer.
1245///
1246/// Determines whether the designated initializer @p DIE, which
1247/// resides at the given @p Index within the initializer list @p
1248/// IList, is well-formed for a current object of type @p DeclType
1249/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump11289f42009-09-09 15:08:12 +00001250/// within the current subobject is returned in either
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001251/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001252///
1253/// @param IList The initializer list in which this designated
1254/// initializer occurs.
1255///
Douglas Gregora5324162009-04-15 04:56:10 +00001256/// @param DIE The designated initializer expression.
1257///
1258/// @param DesigIdx The index of the current designator.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001259///
1260/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1261/// into which the designation in @p DIE should refer.
1262///
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001263/// @param NextField If non-NULL and the first designator in @p DIE is
1264/// a field, this will be set to the field declaration corresponding
1265/// to the field named by the designator.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001266///
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001267/// @param NextElementIndex If non-NULL and the first designator in @p
1268/// DIE is an array designator or GNU array-range designator, this
1269/// will be set to the last index initialized by this designator.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001270///
1271/// @param Index Index into @p IList where the designated initializer
1272/// @p DIE occurs.
1273///
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001274/// @param StructuredList The initializer list expression that
1275/// describes all of the subobject initializers in the order they'll
1276/// actually be initialized.
1277///
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001278/// @returns true if there was an error, false otherwise.
Mike Stump11289f42009-09-09 15:08:12 +00001279bool
Anders Carlsson6cabf312010-01-23 23:23:01 +00001280InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001281 InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +00001282 DesignatedInitExpr *DIE,
Douglas Gregora5324162009-04-15 04:56:10 +00001283 unsigned DesigIdx,
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001284 QualType &CurrentObjectType,
1285 RecordDecl::field_iterator *NextField,
1286 llvm::APSInt *NextElementIndex,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001287 unsigned &Index,
1288 InitListExpr *StructuredList,
Douglas Gregor17bd0942009-01-28 23:36:17 +00001289 unsigned &StructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001290 bool FinishSubobjectInit,
1291 bool TopLevelObject) {
Douglas Gregora5324162009-04-15 04:56:10 +00001292 if (DesigIdx == DIE->size()) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001293 // Check the actual initialization for the designated object type.
1294 bool prevHadError = hadError;
Douglas Gregorf6d27522009-01-29 00:39:20 +00001295
1296 // Temporarily remove the designator expression from the
1297 // initializer list that the child calls see, so that we don't try
1298 // to re-process the designator.
1299 unsigned OldIndex = Index;
1300 IList->setInit(OldIndex, DIE->getInit());
1301
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001302 CheckSubElementType(Entity, IList, CurrentObjectType, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001303 StructuredList, StructuredIndex);
Douglas Gregorf6d27522009-01-29 00:39:20 +00001304
1305 // Restore the designated initializer expression in the syntactic
1306 // form of the initializer list.
1307 if (IList->getInit(OldIndex) != DIE->getInit())
1308 DIE->setInit(IList->getInit(OldIndex));
1309 IList->setInit(OldIndex, DIE);
1310
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001311 return hadError && !prevHadError;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001312 }
1313
Douglas Gregora5324162009-04-15 04:56:10 +00001314 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump11289f42009-09-09 15:08:12 +00001315 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001316 "Need a non-designated initializer list to start from");
1317
Douglas Gregora5324162009-04-15 04:56:10 +00001318 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001319 // Determine the structural initializer list that corresponds to the
1320 // current subobject.
1321 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump11289f42009-09-09 15:08:12 +00001322 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregor5741efb2009-03-01 17:12:46 +00001323 StructuredList, StructuredIndex,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001324 SourceRange(D->getStartLocation(),
1325 DIE->getSourceRange().getEnd()));
1326 assert(StructuredList && "Expected a structured initializer list");
1327
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001328 if (D->isFieldDesignator()) {
1329 // C99 6.7.8p7:
1330 //
1331 // If a designator has the form
1332 //
1333 // . identifier
1334 //
1335 // then the current object (defined below) shall have
1336 // structure or union type and the identifier shall be the
Mike Stump11289f42009-09-09 15:08:12 +00001337 // name of a member of that type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001338 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001339 if (!RT) {
1340 SourceLocation Loc = D->getDotLoc();
1341 if (Loc.isInvalid())
1342 Loc = D->getFieldLoc();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001343 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1344 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001345 ++Index;
1346 return true;
1347 }
1348
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001349 // Note: we perform a linear search of the fields here, despite
1350 // the fact that we have a faster lookup method, because we always
1351 // need to compute the field's index.
Douglas Gregord5846a12009-04-15 06:41:24 +00001352 FieldDecl *KnownField = D->getField();
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001353 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001354 unsigned FieldIndex = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001355 RecordDecl::field_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001356 Field = RT->getDecl()->field_begin(),
1357 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001358 for (; Field != FieldEnd; ++Field) {
1359 if (Field->isUnnamedBitfield())
1360 continue;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001361
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001362 // If we find a field representing an anonymous field, look in the
1363 // IndirectFieldDecl that follow for the designated initializer.
1364 if (!KnownField && Field->isAnonymousStructOrUnion()) {
1365 if (IndirectFieldDecl *IF =
1366 FindIndirectFieldDesignator(*Field, FieldName)) {
1367 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF);
1368 D = DIE->getDesignator(DesigIdx);
1369 break;
1370 }
1371 }
Douglas Gregor559c9fb2010-10-08 20:44:28 +00001372 if (KnownField && KnownField == *Field)
1373 break;
1374 if (FieldName && FieldName == Field->getIdentifier())
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001375 break;
1376
1377 ++FieldIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001378 }
1379
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001380 if (Field == FieldEnd) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001381 // There was no normal field in the struct with the designated
1382 // name. Perform another lookup for this name, which may find
1383 // something that we can't designate (e.g., a member function),
1384 // may find nothing, or may find a member of an anonymous
Mike Stump11289f42009-09-09 15:08:12 +00001385 // struct/union.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001386 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001387 FieldDecl *ReplacementField = 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001388 if (Lookup.first == Lookup.second) {
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001389 // Name lookup didn't find anything. Determine whether this
1390 // was a typo for another field name.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001391 LookupResult R(SemaRef, FieldName, D->getFieldLoc(),
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001392 Sema::LookupMemberName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +00001393 if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl(), false,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001394 Sema::CTC_NoKeywords) &&
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001395 (ReplacementField = R.getAsSingle<FieldDecl>()) &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001396 ReplacementField->getDeclContext()->getRedeclContext()
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001397 ->Equals(RT->getDecl())) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001398 SemaRef.Diag(D->getFieldLoc(),
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001399 diag::err_field_designator_unknown_suggest)
1400 << FieldName << CurrentObjectType << R.getLookupName()
Douglas Gregora771f462010-03-31 17:46:05 +00001401 << FixItHint::CreateReplacement(D->getFieldLoc(),
1402 R.getLookupName().getAsString());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001403 SemaRef.Diag(ReplacementField->getLocation(),
Douglas Gregor6da83622010-01-07 00:17:44 +00001404 diag::note_previous_decl)
1405 << ReplacementField->getDeclName();
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001406 } else {
1407 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1408 << FieldName << CurrentObjectType;
1409 ++Index;
1410 return true;
1411 }
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001412 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001413
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001414 if (!ReplacementField) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001415 // Name lookup found something, but it wasn't a field.
Chris Lattnerb0912a52009-02-24 22:50:46 +00001416 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001417 << FieldName;
Mike Stump11289f42009-09-09 15:08:12 +00001418 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001419 diag::note_field_designator_found);
Eli Friedman8d25b092009-04-16 17:49:48 +00001420 ++Index;
1421 return true;
Douglas Gregord5846a12009-04-15 06:41:24 +00001422 }
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001423
Francois Pichetf3e5b4e2010-12-22 03:46:10 +00001424 if (!KnownField) {
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001425 // The replacement field comes from typo correction; find it
1426 // in the list of fields.
1427 FieldIndex = 0;
1428 Field = RT->getDecl()->field_begin();
1429 for (; Field != FieldEnd; ++Field) {
1430 if (Field->isUnnamedBitfield())
1431 continue;
1432
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001433 if (ReplacementField == *Field ||
Douglas Gregor4e0299b2010-01-01 00:03:05 +00001434 Field->getIdentifier() == ReplacementField->getIdentifier())
1435 break;
1436
1437 ++FieldIndex;
1438 }
1439 }
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001440 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001441
1442 // All of the fields of a union are located at the same place in
1443 // the initializer list.
Douglas Gregor51695702009-01-29 16:53:55 +00001444 if (RT->getDecl()->isUnion()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001445 FieldIndex = 0;
Douglas Gregor51695702009-01-29 16:53:55 +00001446 StructuredList->setInitializedFieldInUnion(*Field);
1447 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001448
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001449 // Update the designator with the field declaration.
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001450 D->setField(*Field);
Mike Stump11289f42009-09-09 15:08:12 +00001451
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001452 // Make sure that our non-designated initializer list has space
1453 // for a subobject corresponding to this field.
1454 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattnerb0912a52009-02-24 22:50:46 +00001455 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001456
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001457 // This designator names a flexible array member.
1458 if (Field->getType()->isIncompleteArrayType()) {
1459 bool Invalid = false;
Douglas Gregora5324162009-04-15 04:56:10 +00001460 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001461 // We can't designate an object within the flexible array
1462 // member (because GCC doesn't allow it).
Mike Stump11289f42009-09-09 15:08:12 +00001463 DesignatedInitExpr::Designator *NextD
Douglas Gregora5324162009-04-15 04:56:10 +00001464 = DIE->getDesignator(DesigIdx + 1);
Mike Stump11289f42009-09-09 15:08:12 +00001465 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001466 diag::err_designator_into_flexible_array_member)
Mike Stump11289f42009-09-09 15:08:12 +00001467 << SourceRange(NextD->getStartLocation(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001468 DIE->getSourceRange().getEnd());
Chris Lattnerb0912a52009-02-24 22:50:46 +00001469 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001470 << *Field;
1471 Invalid = true;
1472 }
1473
Chris Lattner001b29c2010-10-10 17:49:49 +00001474 if (!hadError && !isa<InitListExpr>(DIE->getInit()) &&
1475 !isa<StringLiteral>(DIE->getInit())) {
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001476 // The initializer is not an initializer list.
Chris Lattnerb0912a52009-02-24 22:50:46 +00001477 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001478 diag::err_flexible_array_init_needs_braces)
1479 << DIE->getInit()->getSourceRange();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001480 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001481 << *Field;
1482 Invalid = true;
1483 }
1484
1485 // Handle GNU flexible array initializers.
Mike Stump11289f42009-09-09 15:08:12 +00001486 if (!Invalid && !TopLevelObject &&
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001487 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump11289f42009-09-09 15:08:12 +00001488 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001489 diag::err_flexible_array_init_nonempty)
1490 << DIE->getSourceRange().getBegin();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001491 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001492 << *Field;
1493 Invalid = true;
1494 }
1495
1496 if (Invalid) {
1497 ++Index;
1498 return true;
1499 }
1500
1501 // Initialize the array.
1502 bool prevHadError = hadError;
1503 unsigned newStructuredIndex = FieldIndex;
1504 unsigned OldIndex = Index;
1505 IList->setInit(Index, DIE->getInit());
Anders Carlsson6cabf312010-01-23 23:23:01 +00001506
1507 InitializedEntity MemberEntity =
1508 InitializedEntity::InitializeMember(*Field, &Entity);
1509 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001510 StructuredList, newStructuredIndex);
Anders Carlsson6cabf312010-01-23 23:23:01 +00001511
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001512 IList->setInit(OldIndex, DIE);
1513 if (hadError && !prevHadError) {
1514 ++Field;
1515 ++FieldIndex;
1516 if (NextField)
1517 *NextField = Field;
1518 StructuredIndex = FieldIndex;
1519 return true;
1520 }
1521 } else {
1522 // Recurse to check later designated subobjects.
1523 QualType FieldType = (*Field)->getType();
1524 unsigned newStructuredIndex = FieldIndex;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001525
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001526 InitializedEntity MemberEntity =
Anders Carlsson6cabf312010-01-23 23:23:01 +00001527 InitializedEntity::InitializeMember(*Field, &Entity);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001528 if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1,
1529 FieldType, 0, 0, Index,
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001530 StructuredList, newStructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001531 true, false))
1532 return true;
1533 }
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001534
1535 // Find the position of the next field to be initialized in this
1536 // subobject.
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001537 ++Field;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001538 ++FieldIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001539
1540 // If this the first designator, our caller will continue checking
1541 // the rest of this struct/class/union subobject.
1542 if (IsFirstDesignator) {
1543 if (NextField)
1544 *NextField = Field;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001545 StructuredIndex = FieldIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001546 return false;
1547 }
1548
Douglas Gregor17bd0942009-01-28 23:36:17 +00001549 if (!FinishSubobjectInit)
1550 return false;
1551
Douglas Gregord5846a12009-04-15 06:41:24 +00001552 // We've already initialized something in the union; we're done.
1553 if (RT->getDecl()->isUnion())
1554 return hadError;
1555
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001556 // Check the remaining fields within this class/struct/union subobject.
1557 bool prevHadError = hadError;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001558
Anders Carlsson6cabf312010-01-23 23:23:01 +00001559 CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001560 StructuredList, FieldIndex);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001561 return hadError && !prevHadError;
1562 }
1563
1564 // C99 6.7.8p6:
1565 //
1566 // If a designator has the form
1567 //
1568 // [ constant-expression ]
1569 //
1570 // then the current object (defined below) shall have array
1571 // type and the expression shall be an integer constant
1572 // expression. If the array is of unknown size, any
1573 // nonnegative value is valid.
1574 //
1575 // Additionally, cope with the GNU extension that permits
1576 // designators of the form
1577 //
1578 // [ constant-expression ... constant-expression ]
Chris Lattnerb0912a52009-02-24 22:50:46 +00001579 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001580 if (!AT) {
Chris Lattnerb0912a52009-02-24 22:50:46 +00001581 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001582 << CurrentObjectType;
1583 ++Index;
1584 return true;
1585 }
1586
1587 Expr *IndexExpr = 0;
Douglas Gregor17bd0942009-01-28 23:36:17 +00001588 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1589 if (D->isArrayDesignator()) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001590 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001591 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor17bd0942009-01-28 23:36:17 +00001592 DesignatedEndIndex = DesignatedStartIndex;
1593 } else {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001594 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor17bd0942009-01-28 23:36:17 +00001595
Mike Stump11289f42009-09-09 15:08:12 +00001596 DesignatedStartIndex =
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001597 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump11289f42009-09-09 15:08:12 +00001598 DesignatedEndIndex =
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001599 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001600 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor17bd0942009-01-28 23:36:17 +00001601
Chris Lattnerb0ed51d2011-02-19 22:28:58 +00001602 // Codegen can't handle evaluating array range designators that have side
1603 // effects, because we replicate the AST value for each initialized element.
1604 // As such, set the sawArrayRangeDesignator() bit if we initialize multiple
1605 // elements with something that has a side effect, so codegen can emit an
1606 // "error unsupported" error instead of miscompiling the app.
1607 if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&&
1608 DIE->getInit()->HasSideEffects(SemaRef.Context))
Douglas Gregorbf7207a2009-01-29 19:42:23 +00001609 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001610 }
1611
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001612 if (isa<ConstantArrayType>(AT)) {
1613 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Jay Foad6d4db0c2010-12-07 08:25:34 +00001614 DesignatedStartIndex
1615 = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
Douglas Gregor17bd0942009-01-28 23:36:17 +00001616 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
Jay Foad6d4db0c2010-12-07 08:25:34 +00001617 DesignatedEndIndex
1618 = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
Douglas Gregor17bd0942009-01-28 23:36:17 +00001619 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1620 if (DesignatedEndIndex >= MaxElements) {
Chris Lattnerb0912a52009-02-24 22:50:46 +00001621 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001622 diag::err_array_designator_too_large)
Douglas Gregor17bd0942009-01-28 23:36:17 +00001623 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001624 << IndexExpr->getSourceRange();
1625 ++Index;
1626 return true;
1627 }
Douglas Gregor17bd0942009-01-28 23:36:17 +00001628 } else {
1629 // Make sure the bit-widths and signedness match.
1630 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +00001631 DesignatedEndIndex
1632 = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001633 else if (DesignatedStartIndex.getBitWidth() <
1634 DesignatedEndIndex.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +00001635 DesignatedStartIndex
1636 = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
Douglas Gregor17bd0942009-01-28 23:36:17 +00001637 DesignatedStartIndex.setIsUnsigned(true);
1638 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001639 }
Mike Stump11289f42009-09-09 15:08:12 +00001640
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001641 // Make sure that our non-designated initializer list has space
1642 // for a subobject corresponding to this array element.
Douglas Gregor17bd0942009-01-28 23:36:17 +00001643 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump11289f42009-09-09 15:08:12 +00001644 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor17bd0942009-01-28 23:36:17 +00001645 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001646
Douglas Gregor17bd0942009-01-28 23:36:17 +00001647 // Repeatedly perform subobject initializations in the range
1648 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001649
Douglas Gregor17bd0942009-01-28 23:36:17 +00001650 // Move to the next designator
1651 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1652 unsigned OldIndex = Index;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001653
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001654 InitializedEntity ElementEntity =
Anders Carlsson6cabf312010-01-23 23:23:01 +00001655 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001656
Douglas Gregor17bd0942009-01-28 23:36:17 +00001657 while (DesignatedStartIndex <= DesignatedEndIndex) {
1658 // Recurse to check later designated subobjects.
1659 QualType ElementType = AT->getElementType();
1660 Index = OldIndex;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001661
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001662 ElementEntity.setElementIndex(ElementIndex);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001663 if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1,
1664 ElementType, 0, 0, Index,
Anders Carlsson3fa93b72010-01-23 22:49:02 +00001665 StructuredList, ElementIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001666 (DesignatedStartIndex == DesignatedEndIndex),
1667 false))
Douglas Gregor17bd0942009-01-28 23:36:17 +00001668 return true;
1669
1670 // Move to the next index in the array that we'll be initializing.
1671 ++DesignatedStartIndex;
1672 ElementIndex = DesignatedStartIndex.getZExtValue();
1673 }
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001674
1675 // If this the first designator, our caller will continue checking
1676 // the rest of this array subobject.
1677 if (IsFirstDesignator) {
1678 if (NextElementIndex)
Douglas Gregor17bd0942009-01-28 23:36:17 +00001679 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001680 StructuredIndex = ElementIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001681 return false;
1682 }
Mike Stump11289f42009-09-09 15:08:12 +00001683
Douglas Gregor17bd0942009-01-28 23:36:17 +00001684 if (!FinishSubobjectInit)
1685 return false;
1686
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001687 // Check the remaining elements within this array subobject.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001688 bool prevHadError = hadError;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001689 CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex,
Anders Carlsson0cf999b2010-01-23 20:13:41 +00001690 /*SubobjectIsDesignatorContext=*/false, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001691 StructuredList, ElementIndex);
Mike Stump11289f42009-09-09 15:08:12 +00001692 return hadError && !prevHadError;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001693}
1694
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001695// Get the structured initializer list for a subobject of type
1696// @p CurrentObjectType.
1697InitListExpr *
1698InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1699 QualType CurrentObjectType,
1700 InitListExpr *StructuredList,
1701 unsigned StructuredIndex,
1702 SourceRange InitRange) {
1703 Expr *ExistingInit = 0;
1704 if (!StructuredList)
1705 ExistingInit = SyntacticToSemantic[IList];
1706 else if (StructuredIndex < StructuredList->getNumInits())
1707 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump11289f42009-09-09 15:08:12 +00001708
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001709 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1710 return Result;
1711
1712 if (ExistingInit) {
1713 // We are creating an initializer list that initializes the
1714 // subobjects of the current object, but there was already an
1715 // initialization that completely initialized the current
1716 // subobject, e.g., by a compound literal:
Mike Stump11289f42009-09-09 15:08:12 +00001717 //
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001718 // struct X { int a, b; };
1719 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump11289f42009-09-09 15:08:12 +00001720 //
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001721 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1722 // designated initializer re-initializes the whole
1723 // subobject [0], overwriting previous initializers.
Mike Stump11289f42009-09-09 15:08:12 +00001724 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregor5741efb2009-03-01 17:12:46 +00001725 diag::warn_subobject_initializer_overrides)
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001726 << InitRange;
Mike Stump11289f42009-09-09 15:08:12 +00001727 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001728 diag::note_previous_initializer)
Douglas Gregore6af7a02009-01-28 23:43:32 +00001729 << /*FIXME:has side effects=*/0
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001730 << ExistingInit->getSourceRange();
1731 }
1732
Mike Stump11289f42009-09-09 15:08:12 +00001733 InitListExpr *Result
Ted Kremenekac034612010-04-13 23:39:13 +00001734 = new (SemaRef.Context) InitListExpr(SemaRef.Context,
1735 InitRange.getBegin(), 0, 0,
Ted Kremenek013041e2010-02-19 01:50:18 +00001736 InitRange.getEnd());
Douglas Gregor5741efb2009-03-01 17:12:46 +00001737
Douglas Gregora8a089b2010-07-13 18:40:04 +00001738 Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001739
Douglas Gregor6d00c992009-03-20 23:58:33 +00001740 // Pre-allocate storage for the structured initializer list.
1741 unsigned NumElements = 0;
Douglas Gregor221c9a52009-03-21 18:13:52 +00001742 unsigned NumInits = 0;
1743 if (!StructuredList)
1744 NumInits = IList->getNumInits();
1745 else if (Index < IList->getNumInits()) {
1746 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1747 NumInits = SubList->getNumInits();
1748 }
1749
Mike Stump11289f42009-09-09 15:08:12 +00001750 if (const ArrayType *AType
Douglas Gregor6d00c992009-03-20 23:58:33 +00001751 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1752 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1753 NumElements = CAType->getSize().getZExtValue();
1754 // Simple heuristic so that we don't allocate a very large
1755 // initializer with many empty entries at the end.
Douglas Gregor221c9a52009-03-21 18:13:52 +00001756 if (NumInits && NumElements > NumInits)
Douglas Gregor6d00c992009-03-20 23:58:33 +00001757 NumElements = 0;
1758 }
John McCall9dd450b2009-09-21 23:43:11 +00001759 } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
Douglas Gregor6d00c992009-03-20 23:58:33 +00001760 NumElements = VType->getNumElements();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001761 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregor6d00c992009-03-20 23:58:33 +00001762 RecordDecl *RDecl = RType->getDecl();
1763 if (RDecl->isUnion())
1764 NumElements = 1;
1765 else
Mike Stump11289f42009-09-09 15:08:12 +00001766 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001767 RDecl->field_end());
Douglas Gregor6d00c992009-03-20 23:58:33 +00001768 }
1769
Douglas Gregor221c9a52009-03-21 18:13:52 +00001770 if (NumElements < NumInits)
Douglas Gregor6d00c992009-03-20 23:58:33 +00001771 NumElements = IList->getNumInits();
1772
Ted Kremenekac034612010-04-13 23:39:13 +00001773 Result->reserveInits(SemaRef.Context, NumElements);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001774
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001775 // Link this new initializer list into the structured initializer
1776 // lists.
1777 if (StructuredList)
Ted Kremenekac034612010-04-13 23:39:13 +00001778 StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001779 else {
1780 Result->setSyntacticForm(IList);
1781 SyntacticToSemantic[IList] = Result;
1782 }
1783
1784 return Result;
1785}
1786
1787/// Update the initializer at index @p StructuredIndex within the
1788/// structured initializer list to the value @p expr.
1789void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1790 unsigned &StructuredIndex,
1791 Expr *expr) {
1792 // No structured initializer list to update
1793 if (!StructuredList)
1794 return;
1795
Ted Kremenekac034612010-04-13 23:39:13 +00001796 if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
1797 StructuredIndex, expr)) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001798 // This initializer overwrites a previous initializer. Warn.
Mike Stump11289f42009-09-09 15:08:12 +00001799 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001800 diag::warn_initializer_overrides)
1801 << expr->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001802 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001803 diag::note_previous_initializer)
Douglas Gregore6af7a02009-01-28 23:43:32 +00001804 << /*FIXME:has side effects=*/0
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001805 << PrevInit->getSourceRange();
1806 }
Mike Stump11289f42009-09-09 15:08:12 +00001807
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001808 ++StructuredIndex;
1809}
1810
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001811/// Check that the given Index expression is a valid array designator
1812/// value. This is essentailly just a wrapper around
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001813/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001814/// and produces a reasonable diagnostic if there is a
1815/// failure. Returns true if there was an error, false otherwise. If
1816/// everything went okay, Value will receive the value of the constant
1817/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00001818static bool
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001819CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001820 SourceLocation Loc = Index->getSourceRange().getBegin();
1821
1822 // Make sure this is an integer constant expression.
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001823 if (S.VerifyIntegerConstantExpression(Index, &Value))
1824 return true;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001825
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001826 if (Value.isSigned() && Value.isNegative())
1827 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001828 << Value.toString(10) << Index->getSourceRange();
1829
Douglas Gregor51650d32009-01-23 21:04:18 +00001830 Value.setIsUnsigned(true);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001831 return false;
1832}
1833
John McCalldadc5752010-08-24 06:29:42 +00001834ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
Nick Lewycky9331ed82010-11-20 01:29:55 +00001835 SourceLocation Loc,
1836 bool GNUSyntax,
1837 ExprResult Init) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001838 typedef DesignatedInitExpr::Designator ASTDesignator;
1839
1840 bool Invalid = false;
1841 llvm::SmallVector<ASTDesignator, 32> Designators;
1842 llvm::SmallVector<Expr *, 32> InitExpressions;
1843
1844 // Build designators and check array designator expressions.
1845 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1846 const Designator &D = Desig.getDesignator(Idx);
1847 switch (D.getKind()) {
1848 case Designator::FieldDesignator:
Mike Stump11289f42009-09-09 15:08:12 +00001849 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001850 D.getFieldLoc()));
1851 break;
1852
1853 case Designator::ArrayDesignator: {
1854 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1855 llvm::APSInt IndexValue;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001856 if (!Index->isTypeDependent() &&
1857 !Index->isValueDependent() &&
1858 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001859 Invalid = true;
1860 else {
1861 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump11289f42009-09-09 15:08:12 +00001862 D.getLBracketLoc(),
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001863 D.getRBracketLoc()));
1864 InitExpressions.push_back(Index);
1865 }
1866 break;
1867 }
1868
1869 case Designator::ArrayRangeDesignator: {
1870 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1871 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1872 llvm::APSInt StartValue;
1873 llvm::APSInt EndValue;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001874 bool StartDependent = StartIndex->isTypeDependent() ||
1875 StartIndex->isValueDependent();
1876 bool EndDependent = EndIndex->isTypeDependent() ||
1877 EndIndex->isValueDependent();
1878 if ((!StartDependent &&
1879 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1880 (!EndDependent &&
1881 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001882 Invalid = true;
Douglas Gregor7a95b082009-01-23 22:22:29 +00001883 else {
1884 // Make sure we're comparing values with the same bit width.
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001885 if (StartDependent || EndDependent) {
1886 // Nothing to compute.
1887 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +00001888 EndValue = EndValue.extend(StartValue.getBitWidth());
Douglas Gregor7a95b082009-01-23 22:22:29 +00001889 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +00001890 StartValue = StartValue.extend(EndValue.getBitWidth());
Douglas Gregor7a95b082009-01-23 22:22:29 +00001891
Douglas Gregor0f9d4002009-05-21 23:30:39 +00001892 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregor7a95b082009-01-23 22:22:29 +00001893 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump11289f42009-09-09 15:08:12 +00001894 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregor7a95b082009-01-23 22:22:29 +00001895 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1896 Invalid = true;
1897 } else {
1898 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump11289f42009-09-09 15:08:12 +00001899 D.getLBracketLoc(),
Douglas Gregor7a95b082009-01-23 22:22:29 +00001900 D.getEllipsisLoc(),
1901 D.getRBracketLoc()));
1902 InitExpressions.push_back(StartIndex);
1903 InitExpressions.push_back(EndIndex);
1904 }
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001905 }
1906 break;
1907 }
1908 }
1909 }
1910
1911 if (Invalid || Init.isInvalid())
1912 return ExprError();
1913
1914 // Clear out the expressions within the designation.
1915 Desig.ClearExprs(*this);
1916
1917 DesignatedInitExpr *DIE
Jay Foad7d0479f2009-05-21 09:52:38 +00001918 = DesignatedInitExpr::Create(Context,
1919 Designators.data(), Designators.size(),
1920 InitExpressions.data(), InitExpressions.size(),
Anders Carlssonb781bcd2009-05-01 19:49:17 +00001921 Loc, GNUSyntax, Init.takeAs<Expr>());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001922
Douglas Gregorc124e592011-01-16 16:13:16 +00001923 if (getLangOptions().CPlusPlus)
1924 Diag(DIE->getLocStart(), diag::ext_designated_init)
1925 << DIE->getSourceRange();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001926
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001927 return Owned(DIE);
1928}
Douglas Gregor85df8d82009-01-29 00:45:39 +00001929
Douglas Gregor723796a2009-12-16 06:35:08 +00001930bool Sema::CheckInitList(const InitializedEntity &Entity,
1931 InitListExpr *&InitList, QualType &DeclType) {
1932 InitListChecker CheckInitList(*this, Entity, InitList, DeclType);
Douglas Gregor85df8d82009-01-29 00:45:39 +00001933 if (!CheckInitList.HadError())
1934 InitList = CheckInitList.getFullyStructuredList();
1935
1936 return CheckInitList.HadError();
1937}
Douglas Gregora5c9e1a2009-02-02 17:43:21 +00001938
Douglas Gregor3e1e5272009-12-09 23:02:17 +00001939//===----------------------------------------------------------------------===//
1940// Initialization entity
1941//===----------------------------------------------------------------------===//
1942
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001943InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
Douglas Gregor723796a2009-12-16 06:35:08 +00001944 const InitializedEntity &Parent)
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001945 : Parent(&Parent), Index(Index)
Douglas Gregor723796a2009-12-16 06:35:08 +00001946{
Anders Carlssoned8d80d2010-01-23 04:34:47 +00001947 if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
1948 Kind = EK_ArrayElement;
Douglas Gregor1b303932009-12-22 15:35:07 +00001949 Type = AT->getElementType();
Anders Carlssoned8d80d2010-01-23 04:34:47 +00001950 } else {
1951 Kind = EK_VectorElement;
Douglas Gregor1b303932009-12-22 15:35:07 +00001952 Type = Parent.getType()->getAs<VectorType>()->getElementType();
Anders Carlssoned8d80d2010-01-23 04:34:47 +00001953 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00001954}
1955
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001956InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
Anders Carlsson43c64af2010-04-21 19:52:01 +00001957 CXXBaseSpecifier *Base,
1958 bool IsInheritedVirtualBase)
Douglas Gregor3e1e5272009-12-09 23:02:17 +00001959{
1960 InitializedEntity Result;
1961 Result.Kind = EK_Base;
Anders Carlsson43c64af2010-04-21 19:52:01 +00001962 Result.Base = reinterpret_cast<uintptr_t>(Base);
1963 if (IsInheritedVirtualBase)
1964 Result.Base |= 0x01;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001965
Douglas Gregor1b303932009-12-22 15:35:07 +00001966 Result.Type = Base->getType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00001967 return Result;
1968}
1969
Douglas Gregor85dabae2009-12-16 01:38:02 +00001970DeclarationName InitializedEntity::getName() const {
1971 switch (getKind()) {
Douglas Gregor85dabae2009-12-16 01:38:02 +00001972 case EK_Parameter:
Douglas Gregorbbeb5c32009-12-22 16:09:06 +00001973 if (!VariableOrMember)
1974 return DeclarationName();
1975 // Fall through
1976
1977 case EK_Variable:
Douglas Gregor85dabae2009-12-16 01:38:02 +00001978 case EK_Member:
1979 return VariableOrMember->getDeclName();
1980
1981 case EK_Result:
1982 case EK_Exception:
Douglas Gregore1314a62009-12-18 05:02:21 +00001983 case EK_New:
Douglas Gregor85dabae2009-12-16 01:38:02 +00001984 case EK_Temporary:
1985 case EK_Base:
Anders Carlssoned8d80d2010-01-23 04:34:47 +00001986 case EK_ArrayElement:
1987 case EK_VectorElement:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00001988 case EK_BlockElement:
Douglas Gregor85dabae2009-12-16 01:38:02 +00001989 return DeclarationName();
1990 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001991
Douglas Gregor85dabae2009-12-16 01:38:02 +00001992 // Silence GCC warning
1993 return DeclarationName();
1994}
1995
Douglas Gregora4b592a2009-12-19 03:01:41 +00001996DeclaratorDecl *InitializedEntity::getDecl() const {
1997 switch (getKind()) {
1998 case EK_Variable:
1999 case EK_Parameter:
2000 case EK_Member:
2001 return VariableOrMember;
2002
2003 case EK_Result:
2004 case EK_Exception:
2005 case EK_New:
2006 case EK_Temporary:
2007 case EK_Base:
Anders Carlssoned8d80d2010-01-23 04:34:47 +00002008 case EK_ArrayElement:
2009 case EK_VectorElement:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00002010 case EK_BlockElement:
Douglas Gregora4b592a2009-12-19 03:01:41 +00002011 return 0;
2012 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002013
Douglas Gregora4b592a2009-12-19 03:01:41 +00002014 // Silence GCC warning
2015 return 0;
2016}
2017
Douglas Gregor222cf0e2010-05-15 00:13:29 +00002018bool InitializedEntity::allowsNRVO() const {
2019 switch (getKind()) {
2020 case EK_Result:
2021 case EK_Exception:
2022 return LocAndNRVO.NRVO;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002023
Douglas Gregor222cf0e2010-05-15 00:13:29 +00002024 case EK_Variable:
2025 case EK_Parameter:
2026 case EK_Member:
2027 case EK_New:
2028 case EK_Temporary:
2029 case EK_Base:
2030 case EK_ArrayElement:
2031 case EK_VectorElement:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00002032 case EK_BlockElement:
Douglas Gregor222cf0e2010-05-15 00:13:29 +00002033 break;
2034 }
2035
2036 return false;
2037}
2038
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002039//===----------------------------------------------------------------------===//
2040// Initialization sequence
2041//===----------------------------------------------------------------------===//
2042
2043void InitializationSequence::Step::Destroy() {
2044 switch (Kind) {
2045 case SK_ResolveAddressOfOverloadedFunction:
2046 case SK_CastDerivedToBaseRValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002047 case SK_CastDerivedToBaseXValue:
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002048 case SK_CastDerivedToBaseLValue:
2049 case SK_BindReference:
2050 case SK_BindReferenceToTemporary:
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00002051 case SK_ExtraneousCopyToTemporary:
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002052 case SK_UserConversion:
2053 case SK_QualificationConversionRValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002054 case SK_QualificationConversionXValue:
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002055 case SK_QualificationConversionLValue:
Douglas Gregor51e77d52009-12-10 17:56:55 +00002056 case SK_ListInitialization:
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002057 case SK_ConstructorInitialization:
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002058 case SK_ZeroInitialization:
Douglas Gregore1314a62009-12-18 05:02:21 +00002059 case SK_CAssignment:
Eli Friedman78275202009-12-19 08:11:05 +00002060 case SK_StringInit:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002061 case SK_ObjCObjectConversion:
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002062 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002063
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002064 case SK_ConversionSequence:
2065 delete ICS;
2066 }
2067}
2068
Douglas Gregor838fcc32010-03-26 20:14:36 +00002069bool InitializationSequence::isDirectReferenceBinding() const {
2070 return getKind() == ReferenceBinding && Steps.back().Kind == SK_BindReference;
2071}
2072
2073bool InitializationSequence::isAmbiguous() const {
2074 if (getKind() != FailedSequence)
2075 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002076
Douglas Gregor838fcc32010-03-26 20:14:36 +00002077 switch (getFailureKind()) {
2078 case FK_TooManyInitsForReference:
2079 case FK_ArrayNeedsInitList:
2080 case FK_ArrayNeedsInitListOrStringLiteral:
2081 case FK_AddressOfOverloadFailed: // FIXME: Could do better
2082 case FK_NonConstLValueReferenceBindingToTemporary:
2083 case FK_NonConstLValueReferenceBindingToUnrelated:
2084 case FK_RValueReferenceBindingToLValue:
2085 case FK_ReferenceInitDropsQualifiers:
2086 case FK_ReferenceInitFailed:
2087 case FK_ConversionFailed:
2088 case FK_TooManyInitsForScalar:
2089 case FK_ReferenceBindingToInitList:
2090 case FK_InitListBadDestinationType:
2091 case FK_DefaultInitOfConst:
Douglas Gregor3f4f03a2010-05-20 22:12:02 +00002092 case FK_Incomplete:
Douglas Gregor838fcc32010-03-26 20:14:36 +00002093 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002094
Douglas Gregor838fcc32010-03-26 20:14:36 +00002095 case FK_ReferenceInitOverloadFailed:
2096 case FK_UserConversionOverloadFailed:
2097 case FK_ConstructorOverloadFailed:
2098 return FailedOverloadResult == OR_Ambiguous;
2099 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002100
Douglas Gregor838fcc32010-03-26 20:14:36 +00002101 return false;
2102}
2103
Douglas Gregorb33eed02010-04-16 22:09:46 +00002104bool InitializationSequence::isConstructorInitialization() const {
2105 return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization;
2106}
2107
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002108void InitializationSequence::AddAddressOverloadResolutionStep(
John McCall16df1e52010-03-30 21:47:33 +00002109 FunctionDecl *Function,
2110 DeclAccessPair Found) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002111 Step S;
2112 S.Kind = SK_ResolveAddressOfOverloadedFunction;
2113 S.Type = Function->getType();
John McCalla0296f72010-03-19 07:35:19 +00002114 S.Function.Function = Function;
John McCall16df1e52010-03-30 21:47:33 +00002115 S.Function.FoundDecl = Found;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002116 Steps.push_back(S);
2117}
2118
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002119void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
John McCall2536c6d2010-08-25 10:28:54 +00002120 ExprValueKind VK) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002121 Step S;
John McCall2536c6d2010-08-25 10:28:54 +00002122 switch (VK) {
2123 case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break;
2124 case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break;
2125 case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002126 default: llvm_unreachable("No such category");
2127 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002128 S.Type = BaseType;
2129 Steps.push_back(S);
2130}
2131
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002132void InitializationSequence::AddReferenceBindingStep(QualType T,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002133 bool BindingTemporary) {
2134 Step S;
2135 S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
2136 S.Type = T;
2137 Steps.push_back(S);
2138}
2139
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00002140void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) {
2141 Step S;
2142 S.Kind = SK_ExtraneousCopyToTemporary;
2143 S.Type = T;
2144 Steps.push_back(S);
2145}
2146
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002147void InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
John McCalla0296f72010-03-19 07:35:19 +00002148 DeclAccessPair FoundDecl,
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002149 QualType T) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002150 Step S;
2151 S.Kind = SK_UserConversion;
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002152 S.Type = T;
John McCalla0296f72010-03-19 07:35:19 +00002153 S.Function.Function = Function;
2154 S.Function.FoundDecl = FoundDecl;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002155 Steps.push_back(S);
2156}
2157
2158void InitializationSequence::AddQualificationConversionStep(QualType Ty,
John McCall2536c6d2010-08-25 10:28:54 +00002159 ExprValueKind VK) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002160 Step S;
John McCall7a1da892010-08-26 16:36:35 +00002161 S.Kind = SK_QualificationConversionRValue; // work around a gcc warning
John McCall2536c6d2010-08-25 10:28:54 +00002162 switch (VK) {
2163 case VK_RValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002164 S.Kind = SK_QualificationConversionRValue;
2165 break;
John McCall2536c6d2010-08-25 10:28:54 +00002166 case VK_XValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002167 S.Kind = SK_QualificationConversionXValue;
2168 break;
John McCall2536c6d2010-08-25 10:28:54 +00002169 case VK_LValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002170 S.Kind = SK_QualificationConversionLValue;
2171 break;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002172 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002173 S.Type = Ty;
2174 Steps.push_back(S);
2175}
2176
2177void InitializationSequence::AddConversionSequenceStep(
2178 const ImplicitConversionSequence &ICS,
2179 QualType T) {
2180 Step S;
2181 S.Kind = SK_ConversionSequence;
2182 S.Type = T;
2183 S.ICS = new ImplicitConversionSequence(ICS);
2184 Steps.push_back(S);
2185}
2186
Douglas Gregor51e77d52009-12-10 17:56:55 +00002187void InitializationSequence::AddListInitializationStep(QualType T) {
2188 Step S;
2189 S.Kind = SK_ListInitialization;
2190 S.Type = T;
2191 Steps.push_back(S);
2192}
2193
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002194void
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002195InitializationSequence::AddConstructorInitializationStep(
2196 CXXConstructorDecl *Constructor,
John McCall760af172010-02-01 03:16:54 +00002197 AccessSpecifier Access,
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002198 QualType T) {
2199 Step S;
2200 S.Kind = SK_ConstructorInitialization;
2201 S.Type = T;
John McCalla0296f72010-03-19 07:35:19 +00002202 S.Function.Function = Constructor;
2203 S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002204 Steps.push_back(S);
2205}
2206
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002207void InitializationSequence::AddZeroInitializationStep(QualType T) {
2208 Step S;
2209 S.Kind = SK_ZeroInitialization;
2210 S.Type = T;
2211 Steps.push_back(S);
2212}
2213
Douglas Gregore1314a62009-12-18 05:02:21 +00002214void InitializationSequence::AddCAssignmentStep(QualType T) {
2215 Step S;
2216 S.Kind = SK_CAssignment;
2217 S.Type = T;
2218 Steps.push_back(S);
2219}
2220
Eli Friedman78275202009-12-19 08:11:05 +00002221void InitializationSequence::AddStringInitStep(QualType T) {
2222 Step S;
2223 S.Kind = SK_StringInit;
2224 S.Type = T;
2225 Steps.push_back(S);
2226}
2227
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002228void InitializationSequence::AddObjCObjectConversionStep(QualType T) {
2229 Step S;
2230 S.Kind = SK_ObjCObjectConversion;
2231 S.Type = T;
2232 Steps.push_back(S);
2233}
2234
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002235void InitializationSequence::SetOverloadFailure(FailureKind Failure,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002236 OverloadingResult Result) {
2237 SequenceKind = FailedSequence;
2238 this->Failure = Failure;
2239 this->FailedOverloadResult = Result;
2240}
2241
2242//===----------------------------------------------------------------------===//
2243// Attempt initialization
2244//===----------------------------------------------------------------------===//
2245
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002246/// \brief Attempt list initialization (C++0x [dcl.init.list])
2247static void TryListInitialization(Sema &S,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002248 const InitializedEntity &Entity,
2249 const InitializationKind &Kind,
2250 InitListExpr *InitList,
2251 InitializationSequence &Sequence) {
Douglas Gregor51e77d52009-12-10 17:56:55 +00002252 // FIXME: We only perform rudimentary checking of list
2253 // initializations at this point, then assume that any list
2254 // initialization of an array, aggregate, or scalar will be
Sebastian Redld559a542010-06-30 16:41:54 +00002255 // well-formed. When we actually "perform" list initialization, we'll
Douglas Gregor51e77d52009-12-10 17:56:55 +00002256 // do all of the necessary checking. C++0x initializer lists will
2257 // force us to perform more checking here.
2258 Sequence.setSequenceKind(InitializationSequence::ListInitialization);
2259
Douglas Gregor1b303932009-12-22 15:35:07 +00002260 QualType DestType = Entity.getType();
Douglas Gregor51e77d52009-12-10 17:56:55 +00002261
2262 // C++ [dcl.init]p13:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002263 // If T is a scalar type, then a declaration of the form
Douglas Gregor51e77d52009-12-10 17:56:55 +00002264 //
2265 // T x = { a };
2266 //
2267 // is equivalent to
2268 //
2269 // T x = a;
2270 if (DestType->isScalarType()) {
2271 if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) {
2272 Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
2273 return;
2274 }
2275
2276 // Assume scalar initialization from a single value works.
2277 } else if (DestType->isAggregateType()) {
2278 // Assume aggregate initialization works.
2279 } else if (DestType->isVectorType()) {
2280 // Assume vector initialization works.
2281 } else if (DestType->isReferenceType()) {
2282 // FIXME: C++0x defines behavior for this.
2283 Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
2284 return;
2285 } else if (DestType->isRecordType()) {
2286 // FIXME: C++0x defines behavior for this
2287 Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType);
2288 }
2289
2290 // Add a general "list initialization" step.
2291 Sequence.AddListInitializationStep(DestType);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002292}
2293
2294/// \brief Try a reference initialization that involves calling a conversion
2295/// function.
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002296static OverloadingResult TryRefInitWithConversionFunction(Sema &S,
2297 const InitializedEntity &Entity,
2298 const InitializationKind &Kind,
2299 Expr *Initializer,
2300 bool AllowRValues,
2301 InitializationSequence &Sequence) {
Douglas Gregor1b303932009-12-22 15:35:07 +00002302 QualType DestType = Entity.getType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002303 QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
2304 QualType T1 = cv1T1.getUnqualifiedType();
2305 QualType cv2T2 = Initializer->getType();
2306 QualType T2 = cv2T2.getUnqualifiedType();
2307
2308 bool DerivedToBase;
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002309 bool ObjCConversion;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002310 assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002311 T1, T2, DerivedToBase,
2312 ObjCConversion) &&
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002313 "Must have incompatible references when binding via conversion");
Chandler Carruth8abbc652009-12-13 01:37:04 +00002314 (void)DerivedToBase;
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002315 (void)ObjCConversion;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002316
2317 // Build the candidate set directly in the initialization sequence
2318 // structure, so that it will persist if we fail.
2319 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2320 CandidateSet.clear();
2321
2322 // Determine whether we are allowed to call explicit constructors or
2323 // explicit conversion operators.
2324 bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002325
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002326 const RecordType *T1RecordType = 0;
Douglas Gregor496e8b342010-05-07 19:42:26 +00002327 if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) &&
2328 !S.RequireCompleteType(Kind.getLocation(), T1, 0)) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002329 // The type we're converting to is a class type. Enumerate its constructors
2330 // to see if there is a suitable conversion.
2331 CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
John McCall3696dcb2010-08-17 07:23:57 +00002332
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002333 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregor52b72822010-07-02 23:12:18 +00002334 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002335 Con != ConEnd; ++Con) {
John McCalla0296f72010-03-19 07:35:19 +00002336 NamedDecl *D = *Con;
2337 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
2338
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002339 // Find the constructor (which may be a template).
2340 CXXConstructorDecl *Constructor = 0;
John McCalla0296f72010-03-19 07:35:19 +00002341 FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002342 if (ConstructorTmpl)
2343 Constructor = cast<CXXConstructorDecl>(
2344 ConstructorTmpl->getTemplatedDecl());
2345 else
John McCalla0296f72010-03-19 07:35:19 +00002346 Constructor = cast<CXXConstructorDecl>(D);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002347
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002348 if (!Constructor->isInvalidDecl() &&
2349 Constructor->isConvertingConstructor(AllowExplicit)) {
2350 if (ConstructorTmpl)
John McCalla0296f72010-03-19 07:35:19 +00002351 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
John McCallb89836b2010-01-26 01:37:31 +00002352 /*ExplicitArgs*/ 0,
Argyrios Kyrtzidisdfbdfbb2010-10-05 03:05:30 +00002353 &Initializer, 1, CandidateSet,
2354 /*SuppressUserConversions=*/true);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002355 else
John McCalla0296f72010-03-19 07:35:19 +00002356 S.AddOverloadCandidate(Constructor, FoundDecl,
Argyrios Kyrtzidisdfbdfbb2010-10-05 03:05:30 +00002357 &Initializer, 1, CandidateSet,
2358 /*SuppressUserConversions=*/true);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002359 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002360 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002361 }
John McCall3696dcb2010-08-17 07:23:57 +00002362 if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl())
2363 return OR_No_Viable_Function;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002364
Douglas Gregor496e8b342010-05-07 19:42:26 +00002365 const RecordType *T2RecordType = 0;
2366 if ((T2RecordType = T2->getAs<RecordType>()) &&
2367 !S.RequireCompleteType(Kind.getLocation(), T2, 0)) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002368 // The type we're converting from is a class type, enumerate its conversion
2369 // functions.
2370 CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
2371
John McCallad371252010-01-20 00:46:10 +00002372 const UnresolvedSetImpl *Conversions
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002373 = T2RecordDecl->getVisibleConversionFunctions();
John McCallad371252010-01-20 00:46:10 +00002374 for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
2375 E = Conversions->end(); I != E; ++I) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002376 NamedDecl *D = *I;
2377 CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
2378 if (isa<UsingShadowDecl>(D))
2379 D = cast<UsingShadowDecl>(D)->getTargetDecl();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002380
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002381 FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2382 CXXConversionDecl *Conv;
2383 if (ConvTemplate)
2384 Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
2385 else
Sebastian Redld92badf2010-06-30 18:13:39 +00002386 Conv = cast<CXXConversionDecl>(D);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002387
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002388 // If the conversion function doesn't return a reference type,
2389 // it can't be considered for this conversion unless we're allowed to
2390 // consider rvalues.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002391 // FIXME: Do we need to make sure that we only consider conversion
2392 // candidates with reference-compatible results? That might be needed to
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002393 // break recursion.
2394 if ((AllowExplicit || !Conv->isExplicit()) &&
2395 (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
2396 if (ConvTemplate)
John McCalla0296f72010-03-19 07:35:19 +00002397 S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
John McCallb89836b2010-01-26 01:37:31 +00002398 ActingDC, Initializer,
Douglas Gregord412fe52011-01-21 00:27:08 +00002399 DestType, CandidateSet);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002400 else
John McCalla0296f72010-03-19 07:35:19 +00002401 S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
Douglas Gregord412fe52011-01-21 00:27:08 +00002402 Initializer, DestType, CandidateSet);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002403 }
2404 }
2405 }
John McCall3696dcb2010-08-17 07:23:57 +00002406 if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl())
2407 return OR_No_Viable_Function;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002408
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002409 SourceLocation DeclLoc = Initializer->getLocStart();
2410
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002411 // Perform overload resolution. If it fails, return the failed result.
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002412 OverloadCandidateSet::iterator Best;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002413 if (OverloadingResult Result
Douglas Gregord5b730c92010-09-12 08:07:23 +00002414 = CandidateSet.BestViableFunction(S, DeclLoc, Best, true))
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002415 return Result;
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002416
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002417 FunctionDecl *Function = Best->Function;
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002418
2419 // Compute the returned type of the conversion.
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002420 if (isa<CXXConversionDecl>(Function))
2421 T2 = Function->getResultType();
2422 else
2423 T2 = cv1T1;
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002424
2425 // Add the user-defined conversion step.
John McCalla0296f72010-03-19 07:35:19 +00002426 Sequence.AddUserConversionStep(Function, Best->FoundDecl,
Douglas Gregora8a089b2010-07-13 18:40:04 +00002427 T2.getNonLValueExprType(S.Context));
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002428
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002429 // Determine whether we need to perform derived-to-base or
Eli Friedmanad6c2e52009-12-11 02:42:07 +00002430 // cv-qualification adjustments.
John McCall2536c6d2010-08-25 10:28:54 +00002431 ExprValueKind VK = VK_RValue;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002432 if (T2->isLValueReferenceType())
John McCall2536c6d2010-08-25 10:28:54 +00002433 VK = VK_LValue;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002434 else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>())
John McCall2536c6d2010-08-25 10:28:54 +00002435 VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00002436
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002437 bool NewDerivedToBase = false;
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002438 bool NewObjCConversion = false;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002439 Sema::ReferenceCompareResult NewRefRelationship
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002440 = S.CompareReferenceRelationship(DeclLoc, T1,
Douglas Gregora8a089b2010-07-13 18:40:04 +00002441 T2.getNonLValueExprType(S.Context),
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002442 NewDerivedToBase, NewObjCConversion);
Douglas Gregor1ce52ca2010-03-07 23:17:44 +00002443 if (NewRefRelationship == Sema::Ref_Incompatible) {
2444 // If the type we've converted to is not reference-related to the
2445 // type we're looking for, then there is another conversion step
2446 // we need to perform to produce a temporary of the right type
2447 // that we'll be binding to.
2448 ImplicitConversionSequence ICS;
2449 ICS.setStandard();
2450 ICS.Standard = Best->FinalConversion;
2451 T2 = ICS.Standard.getToType(2);
2452 Sequence.AddConversionSequenceStep(ICS, T2);
2453 } else if (NewDerivedToBase)
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002454 Sequence.AddDerivedToBaseCastStep(
2455 S.Context.getQualifiedType(T1,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002456 T2.getNonReferenceType().getQualifiers()),
John McCall2536c6d2010-08-25 10:28:54 +00002457 VK);
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002458 else if (NewObjCConversion)
2459 Sequence.AddObjCObjectConversionStep(
2460 S.Context.getQualifiedType(T1,
2461 T2.getNonReferenceType().getQualifiers()));
2462
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002463 if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
John McCall2536c6d2010-08-25 10:28:54 +00002464 Sequence.AddQualificationConversionStep(cv1T1, VK);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002465
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002466 Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
2467 return OR_Success;
2468}
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002469
2470/// \brief Attempt reference initialization (C++0x [dcl.init.ref])
2471static void TryReferenceInitialization(Sema &S,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002472 const InitializedEntity &Entity,
2473 const InitializationKind &Kind,
2474 Expr *Initializer,
2475 InitializationSequence &Sequence) {
2476 Sequence.setSequenceKind(InitializationSequence::ReferenceBinding);
Sebastian Redld92badf2010-06-30 18:13:39 +00002477
Douglas Gregor1b303932009-12-22 15:35:07 +00002478 QualType DestType = Entity.getType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002479 QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
Chandler Carruth04bdce62010-01-12 20:32:25 +00002480 Qualifiers T1Quals;
2481 QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002482 QualType cv2T2 = Initializer->getType();
Chandler Carruth04bdce62010-01-12 20:32:25 +00002483 Qualifiers T2Quals;
2484 QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002485 SourceLocation DeclLoc = Initializer->getLocStart();
Sebastian Redld92badf2010-06-30 18:13:39 +00002486
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002487 // If the initializer is the address of an overloaded function, try
2488 // to resolve the overloaded function. If all goes well, T2 is the
2489 // type of the resulting function.
2490 if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) {
John McCall16df1e52010-03-30 21:47:33 +00002491 DeclAccessPair Found;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002492 if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer,
Douglas Gregorbcd62532010-11-08 15:20:28 +00002493 T1,
2494 false,
2495 Found)) {
2496 Sequence.AddAddressOverloadResolutionStep(Fn, Found);
2497 cv2T2 = Fn->getType();
2498 T2 = cv2T2.getUnqualifiedType();
2499 } else if (!T1->isRecordType()) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002500 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
2501 return;
2502 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002503 }
Sebastian Redld92badf2010-06-30 18:13:39 +00002504
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002505 // Compute some basic properties of the types and the initializer.
2506 bool isLValueRef = DestType->isLValueReferenceType();
2507 bool isRValueRef = !isLValueRef;
2508 bool DerivedToBase = false;
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002509 bool ObjCConversion = false;
Sebastian Redld92badf2010-06-30 18:13:39 +00002510 Expr::Classification InitCategory = Initializer->Classify(S.Context);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002511 Sema::ReferenceCompareResult RefRelationship
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002512 = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase,
2513 ObjCConversion);
Sebastian Redld92badf2010-06-30 18:13:39 +00002514
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002515 // C++0x [dcl.init.ref]p5:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002516 // A reference to type "cv1 T1" is initialized by an expression of type
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002517 // "cv2 T2" as follows:
2518 //
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002519 // - If the reference is an lvalue reference and the initializer
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002520 // expression
Sebastian Redld92badf2010-06-30 18:13:39 +00002521 // Note the analogous bullet points for rvlaue refs to functions. Because
2522 // there are no function rvalues in C++, rvalue refs to functions are treated
2523 // like lvalue refs.
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002524 OverloadingResult ConvOvlResult = OR_Success;
Sebastian Redld92badf2010-06-30 18:13:39 +00002525 bool T1Function = T1->isFunctionType();
2526 if (isLValueRef || T1Function) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002527 if (InitCategory.isLValue() &&
Douglas Gregor58281352011-01-27 00:58:17 +00002528 (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002529 (Kind.isCStyleOrFunctionalCast() &&
Douglas Gregor58281352011-01-27 00:58:17 +00002530 RefRelationship == Sema::Ref_Related))) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002531 // - is an lvalue (but is not a bit-field), and "cv1 T1" is
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002532 // reference-compatible with "cv2 T2," or
2533 //
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002534 // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002535 // bit-field when we're determining whether the reference initialization
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002536 // can occur. However, we do pay attention to whether it is a bit-field
2537 // to decide whether we're actually binding to a temporary created from
2538 // the bit-field.
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002539 if (DerivedToBase)
2540 Sequence.AddDerivedToBaseCastStep(
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002541 S.Context.getQualifiedType(T1, T2Quals),
John McCall2536c6d2010-08-25 10:28:54 +00002542 VK_LValue);
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00002543 else if (ObjCConversion)
2544 Sequence.AddObjCObjectConversionStep(
2545 S.Context.getQualifiedType(T1, T2Quals));
2546
Chandler Carruth04bdce62010-01-12 20:32:25 +00002547 if (T1Quals != T2Quals)
John McCall2536c6d2010-08-25 10:28:54 +00002548 Sequence.AddQualificationConversionStep(cv1T1, VK_LValue);
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002549 bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() &&
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002550 (Initializer->getBitField() || Initializer->refersToVectorElement());
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002551 Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002552 return;
2553 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002554
2555 // - has a class type (i.e., T2 is a class type), where T1 is not
2556 // reference-related to T2, and can be implicitly converted to an
2557 // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
2558 // with "cv3 T3" (this conversion is selected by enumerating the
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002559 // applicable conversion functions (13.3.1.6) and choosing the best
2560 // one through overload resolution (13.3)),
Sebastian Redld92badf2010-06-30 18:13:39 +00002561 // If we have an rvalue ref to function type here, the rhs must be
2562 // an rvalue.
2563 if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
2564 (isLValueRef || InitCategory.isRValue())) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002565 ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002566 Initializer,
Sebastian Redld92badf2010-06-30 18:13:39 +00002567 /*AllowRValues=*/isRValueRef,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002568 Sequence);
2569 if (ConvOvlResult == OR_Success)
2570 return;
John McCall0d1da222010-01-12 00:44:57 +00002571 if (ConvOvlResult != OR_No_Viable_Function) {
2572 Sequence.SetOverloadFailure(
2573 InitializationSequence::FK_ReferenceInitOverloadFailed,
2574 ConvOvlResult);
2575 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002576 }
2577 }
Sebastian Redld92badf2010-06-30 18:13:39 +00002578
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002579 // - Otherwise, the reference shall be an lvalue reference to a
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002580 // non-volatile const type (i.e., cv1 shall be const), or the reference
Douglas Gregor7a2a1162011-01-20 16:08:06 +00002581 // shall be an rvalue reference.
Douglas Gregor24f2e8e2011-01-21 00:52:42 +00002582 if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) {
Douglas Gregorbcd62532010-11-08 15:20:28 +00002583 if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
2584 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
2585 else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002586 Sequence.SetOverloadFailure(
2587 InitializationSequence::FK_ReferenceInitOverloadFailed,
2588 ConvOvlResult);
Douglas Gregor24f2e8e2011-01-21 00:52:42 +00002589 else
Sebastian Redld92badf2010-06-30 18:13:39 +00002590 Sequence.SetFailed(InitCategory.isLValue()
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002591 ? (RefRelationship == Sema::Ref_Related
2592 ? InitializationSequence::FK_ReferenceInitDropsQualifiers
2593 : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
2594 : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
Sebastian Redld92badf2010-06-30 18:13:39 +00002595
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002596 return;
2597 }
Sebastian Redld92badf2010-06-30 18:13:39 +00002598
Douglas Gregor92e460e2011-01-20 16:44:54 +00002599 // - If the initializer expression
2600 // - is an xvalue, class prvalue, array prvalue, or function lvalue and
2601 // "cv1 T1" is reference-compatible with "cv2 T2"
2602 // Note: functions are handled below.
2603 if (!T1Function &&
Douglas Gregor58281352011-01-27 00:58:17 +00002604 (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002605 (Kind.isCStyleOrFunctionalCast() &&
Douglas Gregor58281352011-01-27 00:58:17 +00002606 RefRelationship == Sema::Ref_Related)) &&
Douglas Gregor92e460e2011-01-20 16:44:54 +00002607 (InitCategory.isXValue() ||
2608 (InitCategory.isPRValue() && T2->isRecordType()) ||
2609 (InitCategory.isPRValue() && T2->isArrayType()))) {
2610 ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue;
2611 if (InitCategory.isPRValue() && T2->isRecordType()) {
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00002612 // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the
2613 // compiler the freedom to perform a copy here or bind to the
2614 // object, while C++0x requires that we bind directly to the
2615 // object. Hence, we always bind to the object without making an
2616 // extra copy. However, in C++03 requires that we check for the
2617 // presence of a suitable copy constructor:
2618 //
2619 // The constructor that would be used to make the copy shall
2620 // be callable whether or not the copy is actually done.
Francois Pichet687aaf02010-12-31 10:43:42 +00002621 if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft)
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00002622 Sequence.AddExtraneousCopyToTemporary(cv2T2);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002623 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002624
Douglas Gregor92e460e2011-01-20 16:44:54 +00002625 if (DerivedToBase)
2626 Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals),
2627 ValueKind);
2628 else if (ObjCConversion)
2629 Sequence.AddObjCObjectConversionStep(
2630 S.Context.getQualifiedType(T1, T2Quals));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002631
Douglas Gregor92e460e2011-01-20 16:44:54 +00002632 if (T1Quals != T2Quals)
2633 Sequence.AddQualificationConversionStep(cv1T1, ValueKind);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002634 Sequence.AddReferenceBindingStep(cv1T1,
Douglas Gregor92e460e2011-01-20 16:44:54 +00002635 /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType()));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002636 return;
Douglas Gregor92e460e2011-01-20 16:44:54 +00002637 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002638
2639 // - has a class type (i.e., T2 is a class type), where T1 is not
2640 // reference-related to T2, and can be implicitly converted to an
Douglas Gregor92e460e2011-01-20 16:44:54 +00002641 // xvalue, class prvalue, or function lvalue of type "cv3 T3",
2642 // where "cv1 T1" is reference-compatible with "cv3 T3",
Douglas Gregor92e460e2011-01-20 16:44:54 +00002643 if (T2->isRecordType()) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002644 if (RefRelationship == Sema::Ref_Incompatible) {
2645 ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
2646 Kind, Initializer,
2647 /*AllowRValues=*/true,
2648 Sequence);
2649 if (ConvOvlResult)
2650 Sequence.SetOverloadFailure(
2651 InitializationSequence::FK_ReferenceInitOverloadFailed,
2652 ConvOvlResult);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002653
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002654 return;
2655 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002656
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002657 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2658 return;
2659 }
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00002660
2661 // - Otherwise, a temporary of type "cv1 T1" is created and initialized
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002662 // from the initializer expression using the rules for a non-reference
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002663 // copy initialization (8.5). The reference is then bound to the
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002664 // temporary. [...]
John McCallec6f4e92010-06-04 02:29:22 +00002665
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002666 // Determine whether we are allowed to call explicit constructors or
2667 // explicit conversion operators.
2668 bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct);
John McCallec6f4e92010-06-04 02:29:22 +00002669
2670 InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
2671
2672 if (S.TryImplicitConversion(Sequence, TempEntity, Initializer,
2673 /*SuppressUserConversions*/ false,
2674 AllowExplicit,
Douglas Gregor58281352011-01-27 00:58:17 +00002675 /*FIXME:InOverloadResolution=*/false,
2676 /*CStyle=*/Kind.isCStyleOrFunctionalCast())) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002677 // FIXME: Use the conversion function set stored in ICS to turn
2678 // this into an overloading ambiguity diagnostic. However, we need
2679 // to keep that set as an OverloadCandidateSet rather than as some
2680 // other kind of set.
Douglas Gregore1314a62009-12-18 05:02:21 +00002681 if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
2682 Sequence.SetOverloadFailure(
2683 InitializationSequence::FK_ReferenceInitOverloadFailed,
2684 ConvOvlResult);
Douglas Gregorbcd62532010-11-08 15:20:28 +00002685 else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
2686 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
Douglas Gregore1314a62009-12-18 05:02:21 +00002687 else
2688 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002689 return;
2690 }
2691
2692 // [...] If T1 is reference-related to T2, cv1 must be the
2693 // same cv-qualification as, or greater cv-qualification
2694 // than, cv2; otherwise, the program is ill-formed.
Chandler Carruth04bdce62010-01-12 20:32:25 +00002695 unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
2696 unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002697 if (RefRelationship == Sema::Ref_Related &&
Chandler Carruth04bdce62010-01-12 20:32:25 +00002698 (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002699 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2700 return;
2701 }
2702
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002703 // [...] If T1 is reference-related to T2 and the reference is an rvalue
Douglas Gregor24f2e8e2011-01-21 00:52:42 +00002704 // reference, the initializer expression shall not be an lvalue.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002705 if (RefRelationship >= Sema::Ref_Related && !isLValueRef &&
Douglas Gregor24f2e8e2011-01-21 00:52:42 +00002706 InitCategory.isLValue()) {
2707 Sequence.SetFailed(
2708 InitializationSequence::FK_RValueReferenceBindingToLValue);
2709 return;
2710 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002711
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002712 Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
2713 return;
2714}
2715
2716/// \brief Attempt character array initialization from a string literal
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002717/// (C++ [dcl.init.string], C99 6.7.8).
2718static void TryStringLiteralInitialization(Sema &S,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002719 const InitializedEntity &Entity,
2720 const InitializationKind &Kind,
2721 Expr *Initializer,
2722 InitializationSequence &Sequence) {
Eli Friedman78275202009-12-19 08:11:05 +00002723 Sequence.setSequenceKind(InitializationSequence::StringInit);
Douglas Gregor1b303932009-12-22 15:35:07 +00002724 Sequence.AddStringInitStep(Entity.getType());
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002725}
2726
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002727/// \brief Attempt initialization by constructor (C++ [dcl.init]), which
2728/// enumerates the constructors of the initialized entity and performs overload
2729/// resolution to select the best.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002730static void TryConstructorInitialization(Sema &S,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002731 const InitializedEntity &Entity,
2732 const InitializationKind &Kind,
2733 Expr **Args, unsigned NumArgs,
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002734 QualType DestType,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002735 InitializationSequence &Sequence) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002736 Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002737
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002738 // Build the candidate set directly in the initialization sequence
2739 // structure, so that it will persist if we fail.
2740 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2741 CandidateSet.clear();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002742
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002743 // Determine whether we are allowed to call explicit constructors or
2744 // explicit conversion operators.
2745 bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct ||
2746 Kind.getKind() == InitializationKind::IK_Value ||
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002747 Kind.getKind() == InitializationKind::IK_Default);
Douglas Gregord9848152010-04-26 14:36:57 +00002748
2749 // The type we're constructing needs to be complete.
2750 if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Douglas Gregor3f4f03a2010-05-20 22:12:02 +00002751 Sequence.SetFailed(InitializationSequence::FK_Incomplete);
Douglas Gregord9848152010-04-26 14:36:57 +00002752 return;
2753 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002754
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002755 // The type we're converting to is a class type. Enumerate its constructors
2756 // to see if one is suitable.
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002757 const RecordType *DestRecordType = DestType->getAs<RecordType>();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002758 assert(DestRecordType && "Constructor initialization requires record type");
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002759 CXXRecordDecl *DestRecordDecl
2760 = cast<CXXRecordDecl>(DestRecordType->getDecl());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002761
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002762 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregor52b72822010-07-02 23:12:18 +00002763 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002764 Con != ConEnd; ++Con) {
John McCalla0296f72010-03-19 07:35:19 +00002765 NamedDecl *D = *Con;
2766 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
Douglas Gregorc779e992010-04-24 20:54:38 +00002767 bool SuppressUserConversions = false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002768
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002769 // Find the constructor (which may be a template).
2770 CXXConstructorDecl *Constructor = 0;
John McCalla0296f72010-03-19 07:35:19 +00002771 FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002772 if (ConstructorTmpl)
2773 Constructor = cast<CXXConstructorDecl>(
2774 ConstructorTmpl->getTemplatedDecl());
Douglas Gregorc779e992010-04-24 20:54:38 +00002775 else {
John McCalla0296f72010-03-19 07:35:19 +00002776 Constructor = cast<CXXConstructorDecl>(D);
Douglas Gregorc779e992010-04-24 20:54:38 +00002777
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002778 // If we're performing copy initialization using a copy constructor, we
Douglas Gregorc779e992010-04-24 20:54:38 +00002779 // suppress user-defined conversions on the arguments.
2780 // FIXME: Move constructors?
2781 if (Kind.getKind() == InitializationKind::IK_Copy &&
2782 Constructor->isCopyConstructor())
2783 SuppressUserConversions = true;
2784 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002785
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002786 if (!Constructor->isInvalidDecl() &&
Douglas Gregor85dabae2009-12-16 01:38:02 +00002787 (AllowExplicit || !Constructor->isExplicit())) {
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002788 if (ConstructorTmpl)
John McCalla0296f72010-03-19 07:35:19 +00002789 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
John McCallb89836b2010-01-26 01:37:31 +00002790 /*ExplicitArgs*/ 0,
Douglas Gregorc779e992010-04-24 20:54:38 +00002791 Args, NumArgs, CandidateSet,
2792 SuppressUserConversions);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002793 else
John McCalla0296f72010-03-19 07:35:19 +00002794 S.AddOverloadCandidate(Constructor, FoundDecl,
Douglas Gregorc779e992010-04-24 20:54:38 +00002795 Args, NumArgs, CandidateSet,
2796 SuppressUserConversions);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002797 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002798 }
2799
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002800 SourceLocation DeclLoc = Kind.getLocation();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002801
2802 // Perform overload resolution. If it fails, return the failed result.
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002803 OverloadCandidateSet::iterator Best;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002804 if (OverloadingResult Result
John McCall5c32be02010-08-24 20:38:10 +00002805 = CandidateSet.BestViableFunction(S, DeclLoc, Best)) {
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002806 Sequence.SetOverloadFailure(
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002807 InitializationSequence::FK_ConstructorOverloadFailed,
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002808 Result);
2809 return;
2810 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002811
2812 // C++0x [dcl.init]p6:
2813 // If a program calls for the default initialization of an object
2814 // of a const-qualified type T, T shall be a class type with a
2815 // user-provided default constructor.
2816 if (Kind.getKind() == InitializationKind::IK_Default &&
2817 Entity.getType().isConstQualified() &&
2818 cast<CXXConstructorDecl>(Best->Function)->isImplicit()) {
2819 Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2820 return;
2821 }
2822
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00002823 // Add the constructor initialization step. Any cv-qualification conversion is
2824 // subsumed by the initialization.
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002825 Sequence.AddConstructorInitializationStep(
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002826 cast<CXXConstructorDecl>(Best->Function),
John McCalla0296f72010-03-19 07:35:19 +00002827 Best->FoundDecl.getAccess(),
Douglas Gregore1314a62009-12-18 05:02:21 +00002828 DestType);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002829}
2830
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002831/// \brief Attempt value initialization (C++ [dcl.init]p7).
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002832static void TryValueInitialization(Sema &S,
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002833 const InitializedEntity &Entity,
2834 const InitializationKind &Kind,
2835 InitializationSequence &Sequence) {
2836 // C++ [dcl.init]p5:
2837 //
2838 // To value-initialize an object of type T means:
Douglas Gregor1b303932009-12-22 15:35:07 +00002839 QualType T = Entity.getType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002840
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002841 // -- if T is an array type, then each element is value-initialized;
2842 while (const ArrayType *AT = S.Context.getAsArrayType(T))
2843 T = AT->getElementType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002844
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002845 if (const RecordType *RT = T->getAs<RecordType>()) {
2846 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2847 // -- if T is a class type (clause 9) with a user-declared
2848 // constructor (12.1), then the default constructor for T is
2849 // called (and the initialization is ill-formed if T has no
2850 // accessible default constructor);
2851 //
2852 // FIXME: we really want to refer to a single subobject of the array,
2853 // but Entity doesn't have a way to capture that (yet).
2854 if (ClassDecl->hasUserDeclaredConstructor())
2855 return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002856
Douglas Gregor4f4b1862009-12-16 18:50:27 +00002857 // -- if T is a (possibly cv-qualified) non-union class type
2858 // without a user-provided constructor, then the object is
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00002859 // zero-initialized and, if T's implicitly-declared default
Douglas Gregor4f4b1862009-12-16 18:50:27 +00002860 // constructor is non-trivial, that constructor is called.
Abramo Bagnara6150c882010-05-11 21:36:43 +00002861 if ((ClassDecl->getTagKind() == TTK_Class ||
Douglas Gregor747eb782010-07-08 06:14:04 +00002862 ClassDecl->getTagKind() == TTK_Struct)) {
Douglas Gregor1b303932009-12-22 15:35:07 +00002863 Sequence.AddZeroInitializationStep(Entity.getType());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002864 return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
Douglas Gregor4f4b1862009-12-16 18:50:27 +00002865 }
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002866 }
2867 }
2868
Douglas Gregor1b303932009-12-22 15:35:07 +00002869 Sequence.AddZeroInitializationStep(Entity.getType());
Douglas Gregor7dc42e52009-12-15 00:01:57 +00002870 Sequence.setSequenceKind(InitializationSequence::ZeroInitialization);
2871}
2872
Douglas Gregor85dabae2009-12-16 01:38:02 +00002873/// \brief Attempt default initialization (C++ [dcl.init]p6).
2874static void TryDefaultInitialization(Sema &S,
2875 const InitializedEntity &Entity,
2876 const InitializationKind &Kind,
2877 InitializationSequence &Sequence) {
2878 assert(Kind.getKind() == InitializationKind::IK_Default);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002879
Douglas Gregor85dabae2009-12-16 01:38:02 +00002880 // C++ [dcl.init]p6:
2881 // To default-initialize an object of type T means:
2882 // - if T is an array type, each element is default-initialized;
Douglas Gregor1b303932009-12-22 15:35:07 +00002883 QualType DestType = Entity.getType();
Douglas Gregor85dabae2009-12-16 01:38:02 +00002884 while (const ArrayType *Array = S.Context.getAsArrayType(DestType))
2885 DestType = Array->getElementType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002886
Douglas Gregor85dabae2009-12-16 01:38:02 +00002887 // - if T is a (possibly cv-qualified) class type (Clause 9), the default
2888 // constructor for T is called (and the initialization is ill-formed if
2889 // T has no accessible default constructor);
Douglas Gregore6565622010-02-09 07:26:29 +00002890 if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) {
Chandler Carruthc9262402010-08-23 07:55:51 +00002891 TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence);
2892 return;
Douglas Gregor85dabae2009-12-16 01:38:02 +00002893 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002894
Douglas Gregor85dabae2009-12-16 01:38:02 +00002895 // - otherwise, no initialization is performed.
2896 Sequence.setSequenceKind(InitializationSequence::NoInitialization);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002897
Douglas Gregor85dabae2009-12-16 01:38:02 +00002898 // If a program calls for the default initialization of an object of
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002899 // a const-qualified type T, T shall be a class type with a user-provided
Douglas Gregor85dabae2009-12-16 01:38:02 +00002900 // default constructor.
Douglas Gregore6565622010-02-09 07:26:29 +00002901 if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus)
Douglas Gregor85dabae2009-12-16 01:38:02 +00002902 Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2903}
2904
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002905/// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
2906/// which enumerates all conversion functions and performs overload resolution
2907/// to select the best.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002908static void TryUserDefinedConversion(Sema &S,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00002909 const InitializedEntity &Entity,
2910 const InitializationKind &Kind,
2911 Expr *Initializer,
2912 InitializationSequence &Sequence) {
Douglas Gregor540c3b02009-12-14 17:27:33 +00002913 Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002914
Douglas Gregor1b303932009-12-22 15:35:07 +00002915 QualType DestType = Entity.getType();
Douglas Gregor540c3b02009-12-14 17:27:33 +00002916 assert(!DestType->isReferenceType() && "References are handled elsewhere");
2917 QualType SourceType = Initializer->getType();
2918 assert((DestType->isRecordType() || SourceType->isRecordType()) &&
2919 "Must have a class type to perform a user-defined conversion");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002920
Douglas Gregor540c3b02009-12-14 17:27:33 +00002921 // Build the candidate set directly in the initialization sequence
2922 // structure, so that it will persist if we fail.
2923 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2924 CandidateSet.clear();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002925
Douglas Gregor540c3b02009-12-14 17:27:33 +00002926 // Determine whether we are allowed to call explicit constructors or
2927 // explicit conversion operators.
2928 bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002929
Douglas Gregor540c3b02009-12-14 17:27:33 +00002930 if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
2931 // The type we're converting to is a class type. Enumerate its constructors
2932 // to see if there is a suitable conversion.
2933 CXXRecordDecl *DestRecordDecl
2934 = cast<CXXRecordDecl>(DestRecordType->getDecl());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002935
Douglas Gregord9848152010-04-26 14:36:57 +00002936 // Try to complete the type we're converting to.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002937 if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Douglas Gregord9848152010-04-26 14:36:57 +00002938 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregor52b72822010-07-02 23:12:18 +00002939 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
Douglas Gregord9848152010-04-26 14:36:57 +00002940 Con != ConEnd; ++Con) {
2941 NamedDecl *D = *Con;
2942 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002943
Douglas Gregord9848152010-04-26 14:36:57 +00002944 // Find the constructor (which may be a template).
2945 CXXConstructorDecl *Constructor = 0;
2946 FunctionTemplateDecl *ConstructorTmpl
2947 = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor540c3b02009-12-14 17:27:33 +00002948 if (ConstructorTmpl)
Douglas Gregord9848152010-04-26 14:36:57 +00002949 Constructor = cast<CXXConstructorDecl>(
2950 ConstructorTmpl->getTemplatedDecl());
Douglas Gregor7c426592010-07-01 03:43:00 +00002951 else
Douglas Gregord9848152010-04-26 14:36:57 +00002952 Constructor = cast<CXXConstructorDecl>(D);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002953
Douglas Gregord9848152010-04-26 14:36:57 +00002954 if (!Constructor->isInvalidDecl() &&
2955 Constructor->isConvertingConstructor(AllowExplicit)) {
2956 if (ConstructorTmpl)
2957 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
2958 /*ExplicitArgs*/ 0,
2959 &Initializer, 1, CandidateSet,
Douglas Gregor7c426592010-07-01 03:43:00 +00002960 /*SuppressUserConversions=*/true);
Douglas Gregord9848152010-04-26 14:36:57 +00002961 else
2962 S.AddOverloadCandidate(Constructor, FoundDecl,
2963 &Initializer, 1, CandidateSet,
Douglas Gregor7c426592010-07-01 03:43:00 +00002964 /*SuppressUserConversions=*/true);
Douglas Gregord9848152010-04-26 14:36:57 +00002965 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002966 }
Douglas Gregord9848152010-04-26 14:36:57 +00002967 }
Douglas Gregor540c3b02009-12-14 17:27:33 +00002968 }
Eli Friedman78275202009-12-19 08:11:05 +00002969
2970 SourceLocation DeclLoc = Initializer->getLocStart();
2971
Douglas Gregor540c3b02009-12-14 17:27:33 +00002972 if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
2973 // The type we're converting from is a class type, enumerate its conversion
2974 // functions.
Eli Friedman78275202009-12-19 08:11:05 +00002975
Eli Friedman4afe9a32009-12-20 22:12:03 +00002976 // We can only enumerate the conversion functions for a complete type; if
2977 // the type isn't complete, simply skip this step.
2978 if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
2979 CXXRecordDecl *SourceRecordDecl
2980 = cast<CXXRecordDecl>(SourceRecordType->getDecl());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002981
John McCallad371252010-01-20 00:46:10 +00002982 const UnresolvedSetImpl *Conversions
Eli Friedman4afe9a32009-12-20 22:12:03 +00002983 = SourceRecordDecl->getVisibleConversionFunctions();
John McCallad371252010-01-20 00:46:10 +00002984 for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002985 E = Conversions->end();
Eli Friedman4afe9a32009-12-20 22:12:03 +00002986 I != E; ++I) {
2987 NamedDecl *D = *I;
2988 CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
2989 if (isa<UsingShadowDecl>(D))
2990 D = cast<UsingShadowDecl>(D)->getTargetDecl();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002991
Eli Friedman4afe9a32009-12-20 22:12:03 +00002992 FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2993 CXXConversionDecl *Conv;
Douglas Gregor540c3b02009-12-14 17:27:33 +00002994 if (ConvTemplate)
Eli Friedman4afe9a32009-12-20 22:12:03 +00002995 Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
Douglas Gregor540c3b02009-12-14 17:27:33 +00002996 else
John McCallda4458e2010-03-31 01:36:47 +00002997 Conv = cast<CXXConversionDecl>(D);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002998
Eli Friedman4afe9a32009-12-20 22:12:03 +00002999 if (AllowExplicit || !Conv->isExplicit()) {
3000 if (ConvTemplate)
John McCalla0296f72010-03-19 07:35:19 +00003001 S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
John McCallb89836b2010-01-26 01:37:31 +00003002 ActingDC, Initializer, DestType,
Eli Friedman4afe9a32009-12-20 22:12:03 +00003003 CandidateSet);
3004 else
John McCalla0296f72010-03-19 07:35:19 +00003005 S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
John McCallb89836b2010-01-26 01:37:31 +00003006 Initializer, DestType, CandidateSet);
Eli Friedman4afe9a32009-12-20 22:12:03 +00003007 }
Douglas Gregor540c3b02009-12-14 17:27:33 +00003008 }
3009 }
3010 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003011
3012 // Perform overload resolution. If it fails, return the failed result.
Douglas Gregor540c3b02009-12-14 17:27:33 +00003013 OverloadCandidateSet::iterator Best;
John McCall0d1da222010-01-12 00:44:57 +00003014 if (OverloadingResult Result
Douglas Gregord5b730c92010-09-12 08:07:23 +00003015 = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) {
Douglas Gregor540c3b02009-12-14 17:27:33 +00003016 Sequence.SetOverloadFailure(
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003017 InitializationSequence::FK_UserConversionOverloadFailed,
Douglas Gregor540c3b02009-12-14 17:27:33 +00003018 Result);
3019 return;
3020 }
John McCall0d1da222010-01-12 00:44:57 +00003021
Douglas Gregor540c3b02009-12-14 17:27:33 +00003022 FunctionDecl *Function = Best->Function;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003023
Douglas Gregor540c3b02009-12-14 17:27:33 +00003024 if (isa<CXXConstructorDecl>(Function)) {
3025 // Add the user-defined conversion step. Any cv-qualification conversion is
3026 // subsumed by the initialization.
John McCalla0296f72010-03-19 07:35:19 +00003027 Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType);
Douglas Gregor540c3b02009-12-14 17:27:33 +00003028 return;
3029 }
3030
3031 // Add the user-defined conversion step that calls the conversion function.
Douglas Gregor603d81b2010-07-13 08:18:22 +00003032 QualType ConvType = Function->getCallResultType();
Douglas Gregor5ab11652010-04-17 22:01:05 +00003033 if (ConvType->getAs<RecordType>()) {
3034 // If we're converting to a class type, there may be an copy if
3035 // the resulting temporary object (possible to create an object of
3036 // a base class type). That copy is not a separate conversion, so
3037 // we just make a note of the actual destination type (possibly a
3038 // base class of the type returned by the conversion function) and
3039 // let the user-defined conversion step handle the conversion.
3040 Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType);
3041 return;
3042 }
Douglas Gregor540c3b02009-12-14 17:27:33 +00003043
Douglas Gregor5ab11652010-04-17 22:01:05 +00003044 Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003045
Douglas Gregor5ab11652010-04-17 22:01:05 +00003046 // If the conversion following the call to the conversion function
3047 // is interesting, add it as a separate step.
Douglas Gregor540c3b02009-12-14 17:27:33 +00003048 if (Best->FinalConversion.First || Best->FinalConversion.Second ||
3049 Best->FinalConversion.Third) {
3050 ImplicitConversionSequence ICS;
John McCall0d1da222010-01-12 00:44:57 +00003051 ICS.setStandard();
Douglas Gregor540c3b02009-12-14 17:27:33 +00003052 ICS.Standard = Best->FinalConversion;
3053 Sequence.AddConversionSequenceStep(ICS, DestType);
3054 }
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003055}
3056
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003057InitializationSequence::InitializationSequence(Sema &S,
3058 const InitializedEntity &Entity,
3059 const InitializationKind &Kind,
3060 Expr **Args,
John McCallbc077cf2010-02-08 23:07:23 +00003061 unsigned NumArgs)
3062 : FailedCandidateSet(Kind.getLocation()) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003063 ASTContext &Context = S.Context;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003064
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003065 // C++0x [dcl.init]p16:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003066 // The semantics of initializers are as follows. The destination type is
3067 // the type of the object or reference being initialized and the source
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003068 // type is the type of the initializer expression. The source type is not
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003069 // defined when the initializer is a braced-init-list or when it is a
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003070 // parenthesized list of expressions.
Douglas Gregor1b303932009-12-22 15:35:07 +00003071 QualType DestType = Entity.getType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003072
3073 if (DestType->isDependentType() ||
3074 Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
3075 SequenceKind = DependentSequence;
3076 return;
3077 }
3078
John McCalled75c092010-12-07 22:54:16 +00003079 for (unsigned I = 0; I != NumArgs; ++I)
3080 if (Args[I]->getObjectKind() == OK_ObjCProperty)
3081 S.ConvertPropertyForRValue(Args[I]);
3082
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003083 QualType SourceType;
3084 Expr *Initializer = 0;
Douglas Gregor85dabae2009-12-16 01:38:02 +00003085 if (NumArgs == 1) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003086 Initializer = Args[0];
3087 if (!isa<InitListExpr>(Initializer))
3088 SourceType = Initializer->getType();
3089 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003090
3091 // - If the initializer is a braced-init-list, the object is
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003092 // list-initialized (8.5.4).
3093 if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
3094 TryListInitialization(S, Entity, Kind, InitList, *this);
Douglas Gregor51e77d52009-12-10 17:56:55 +00003095 return;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003096 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003097
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003098 // - If the destination type is a reference type, see 8.5.3.
3099 if (DestType->isReferenceType()) {
3100 // C++0x [dcl.init.ref]p1:
3101 // A variable declared to be a T& or T&&, that is, "reference to type T"
3102 // (8.3.2), shall be initialized by an object, or function, of type T or
3103 // by an object that can be converted into a T.
3104 // (Therefore, multiple arguments are not permitted.)
3105 if (NumArgs != 1)
3106 SetFailed(FK_TooManyInitsForReference);
3107 else
3108 TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
3109 return;
3110 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003111
3112 // - If the destination type is an array of characters, an array of
3113 // char16_t, an array of char32_t, or an array of wchar_t, and the
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003114 // initializer is a string literal, see 8.5.2.
3115 if (Initializer && IsStringInit(Initializer, DestType, Context)) {
3116 TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
3117 return;
3118 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003119
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003120 // - If the initializer is (), the object is value-initialized.
Douglas Gregor85dabae2009-12-16 01:38:02 +00003121 if (Kind.getKind() == InitializationKind::IK_Value ||
3122 (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003123 TryValueInitialization(S, Entity, Kind, *this);
3124 return;
3125 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003126
Douglas Gregor85dabae2009-12-16 01:38:02 +00003127 // Handle default initialization.
Nick Lewycky9331ed82010-11-20 01:29:55 +00003128 if (Kind.getKind() == InitializationKind::IK_Default) {
Douglas Gregor85dabae2009-12-16 01:38:02 +00003129 TryDefaultInitialization(S, Entity, Kind, *this);
3130 return;
3131 }
Douglas Gregore1314a62009-12-18 05:02:21 +00003132
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003133 // - Otherwise, if the destination type is an array, the program is
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003134 // ill-formed.
3135 if (const ArrayType *AT = Context.getAsArrayType(DestType)) {
3136 if (AT->getElementType()->isAnyCharacterType())
3137 SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
3138 else
3139 SetFailed(FK_ArrayNeedsInitList);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003140
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003141 return;
3142 }
Eli Friedman78275202009-12-19 08:11:05 +00003143
3144 // Handle initialization in C
3145 if (!S.getLangOptions().CPlusPlus) {
3146 setSequenceKind(CAssignment);
3147 AddCAssignmentStep(DestType);
3148 return;
3149 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003150
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003151 // - If the destination type is a (possibly cv-qualified) class type:
3152 if (DestType->isRecordType()) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003153 // - If the initialization is direct-initialization, or if it is
3154 // copy-initialization where the cv-unqualified version of the
3155 // source type is the same class as, or a derived class of, the
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003156 // class of the destination, constructors are considered. [...]
3157 if (Kind.getKind() == InitializationKind::IK_Direct ||
3158 (Kind.getKind() == InitializationKind::IK_Copy &&
3159 (Context.hasSameUnqualifiedType(SourceType, DestType) ||
3160 S.IsDerivedFrom(SourceType, DestType))))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003161 TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
Douglas Gregor1b303932009-12-22 15:35:07 +00003162 Entity.getType(), *this);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003163 // - Otherwise (i.e., for the remaining copy-initialization cases),
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003164 // user-defined conversion sequences that can convert from the source
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003165 // type to the destination type or (when a conversion function is
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003166 // used) to a derived class thereof are enumerated as described in
3167 // 13.3.1.4, and the best one is chosen through overload resolution
3168 // (13.3).
3169 else
3170 TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
3171 return;
3172 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003173
Douglas Gregor85dabae2009-12-16 01:38:02 +00003174 if (NumArgs > 1) {
3175 SetFailed(FK_TooManyInitsForScalar);
3176 return;
3177 }
3178 assert(NumArgs == 1 && "Zero-argument case handled above");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003179
3180 // - Otherwise, if the source type is a (possibly cv-qualified) class
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003181 // type, conversion functions are considered.
Douglas Gregor85dabae2009-12-16 01:38:02 +00003182 if (!SourceType.isNull() && SourceType->isRecordType()) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003183 TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
3184 return;
3185 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003186
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003187 // - Otherwise, the initial value of the object being initialized is the
Douglas Gregor540c3b02009-12-14 17:27:33 +00003188 // (possibly converted) value of the initializer expression. Standard
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003189 // conversions (Clause 4) will be used, if necessary, to convert the
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003190 // initializer expression to the cv-unqualified version of the
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003191 // destination type; no user-defined conversions are considered.
John McCallec6f4e92010-06-04 02:29:22 +00003192 if (S.TryImplicitConversion(*this, Entity, Initializer,
3193 /*SuppressUserConversions*/ true,
3194 /*AllowExplicitConversions*/ false,
Douglas Gregor58281352011-01-27 00:58:17 +00003195 /*InOverloadResolution*/ false,
3196 /*CStyle=*/Kind.isCStyleOrFunctionalCast()))
Douglas Gregore81f58e2010-11-08 03:40:48 +00003197 {
Douglas Gregorb491ed32011-02-19 21:32:49 +00003198 DeclAccessPair dap;
3199 if (Initializer->getType() == Context.OverloadTy &&
3200 !S.ResolveAddressOfOverloadedFunction(Initializer
3201 , DestType, false, dap))
Douglas Gregore81f58e2010-11-08 03:40:48 +00003202 SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
3203 else
3204 SetFailed(InitializationSequence::FK_ConversionFailed);
3205 }
John McCallec6f4e92010-06-04 02:29:22 +00003206 else
3207 setSequenceKind(StandardConversion);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003208}
3209
3210InitializationSequence::~InitializationSequence() {
3211 for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(),
3212 StepEnd = Steps.end();
3213 Step != StepEnd; ++Step)
3214 Step->Destroy();
3215}
3216
3217//===----------------------------------------------------------------------===//
3218// Perform initialization
3219//===----------------------------------------------------------------------===//
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003220static Sema::AssignmentAction
Douglas Gregore1314a62009-12-18 05:02:21 +00003221getAssignmentAction(const InitializedEntity &Entity) {
3222 switch(Entity.getKind()) {
3223 case InitializedEntity::EK_Variable:
3224 case InitializedEntity::EK_New:
Douglas Gregor6dd3a6a2010-12-02 21:47:04 +00003225 case InitializedEntity::EK_Exception:
3226 case InitializedEntity::EK_Base:
Douglas Gregore1314a62009-12-18 05:02:21 +00003227 return Sema::AA_Initializing;
3228
3229 case InitializedEntity::EK_Parameter:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003230 if (Entity.getDecl() &&
Douglas Gregor6b7f12c2010-04-21 23:24:10 +00003231 isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
3232 return Sema::AA_Sending;
3233
Douglas Gregore1314a62009-12-18 05:02:21 +00003234 return Sema::AA_Passing;
3235
3236 case InitializedEntity::EK_Result:
3237 return Sema::AA_Returning;
3238
Douglas Gregore1314a62009-12-18 05:02:21 +00003239 case InitializedEntity::EK_Temporary:
3240 // FIXME: Can we tell apart casting vs. converting?
3241 return Sema::AA_Casting;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003242
Douglas Gregore1314a62009-12-18 05:02:21 +00003243 case InitializedEntity::EK_Member:
Anders Carlssoned8d80d2010-01-23 04:34:47 +00003244 case InitializedEntity::EK_ArrayElement:
3245 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00003246 case InitializedEntity::EK_BlockElement:
Douglas Gregore1314a62009-12-18 05:02:21 +00003247 return Sema::AA_Initializing;
3248 }
3249
3250 return Sema::AA_Converting;
3251}
3252
Douglas Gregor95562572010-04-24 23:45:46 +00003253/// \brief Whether we should binding a created object as a temporary when
3254/// initializing the given entity.
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003255static bool shouldBindAsTemporary(const InitializedEntity &Entity) {
Douglas Gregore1314a62009-12-18 05:02:21 +00003256 switch (Entity.getKind()) {
Anders Carlsson0bd52402010-01-24 00:19:41 +00003257 case InitializedEntity::EK_ArrayElement:
3258 case InitializedEntity::EK_Member:
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003259 case InitializedEntity::EK_Result:
Douglas Gregore1314a62009-12-18 05:02:21 +00003260 case InitializedEntity::EK_New:
3261 case InitializedEntity::EK_Variable:
3262 case InitializedEntity::EK_Base:
Anders Carlssoned8d80d2010-01-23 04:34:47 +00003263 case InitializedEntity::EK_VectorElement:
Anders Carlssonfcd764a2010-02-06 23:23:06 +00003264 case InitializedEntity::EK_Exception:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00003265 case InitializedEntity::EK_BlockElement:
Douglas Gregore1314a62009-12-18 05:02:21 +00003266 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003267
Douglas Gregore1314a62009-12-18 05:02:21 +00003268 case InitializedEntity::EK_Parameter:
3269 case InitializedEntity::EK_Temporary:
3270 return true;
3271 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003272
Douglas Gregore1314a62009-12-18 05:02:21 +00003273 llvm_unreachable("missed an InitializedEntity kind?");
3274}
3275
Douglas Gregor95562572010-04-24 23:45:46 +00003276/// \brief Whether the given entity, when initialized with an object
3277/// created for that initialization, requires destruction.
3278static bool shouldDestroyTemporary(const InitializedEntity &Entity) {
3279 switch (Entity.getKind()) {
3280 case InitializedEntity::EK_Member:
3281 case InitializedEntity::EK_Result:
3282 case InitializedEntity::EK_New:
3283 case InitializedEntity::EK_Base:
3284 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00003285 case InitializedEntity::EK_BlockElement:
Douglas Gregor95562572010-04-24 23:45:46 +00003286 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003287
Douglas Gregor95562572010-04-24 23:45:46 +00003288 case InitializedEntity::EK_Variable:
3289 case InitializedEntity::EK_Parameter:
3290 case InitializedEntity::EK_Temporary:
3291 case InitializedEntity::EK_ArrayElement:
3292 case InitializedEntity::EK_Exception:
3293 return true;
3294 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003295
3296 llvm_unreachable("missed an InitializedEntity kind?");
Douglas Gregor95562572010-04-24 23:45:46 +00003297}
3298
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003299/// \brief Make a (potentially elidable) temporary copy of the object
3300/// provided by the given initializer by calling the appropriate copy
3301/// constructor.
3302///
3303/// \param S The Sema object used for type-checking.
3304///
Abramo Bagnara92141d22011-01-27 19:55:10 +00003305/// \param T The type of the temporary object, which must either be
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003306/// the type of the initializer expression or a superclass thereof.
3307///
3308/// \param Enter The entity being initialized.
3309///
3310/// \param CurInit The initializer expression.
3311///
3312/// \param IsExtraneousCopy Whether this is an "extraneous" copy that
3313/// is permitted in C++03 (but not C++0x) when binding a reference to
3314/// an rvalue.
3315///
3316/// \returns An expression that copies the initializer expression into
3317/// a temporary object, or an error expression if a copy could not be
3318/// created.
John McCalldadc5752010-08-24 06:29:42 +00003319static ExprResult CopyObject(Sema &S,
Douglas Gregord5b730c92010-09-12 08:07:23 +00003320 QualType T,
3321 const InitializedEntity &Entity,
3322 ExprResult CurInit,
3323 bool IsExtraneousCopy) {
Douglas Gregor5ab11652010-04-17 22:01:05 +00003324 // Determine which class type we're copying to.
Anders Carlsson0bd52402010-01-24 00:19:41 +00003325 Expr *CurInitExpr = (Expr *)CurInit.get();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003326 CXXRecordDecl *Class = 0;
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003327 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003328 Class = cast<CXXRecordDecl>(Record->getDecl());
3329 if (!Class)
3330 return move(CurInit);
3331
Douglas Gregor5d369002011-01-21 18:05:27 +00003332 // C++0x [class.copy]p32:
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003333 // When certain criteria are met, an implementation is allowed to
3334 // omit the copy/move construction of a class object, even if the
3335 // copy/move constructor and/or destructor for the object have
3336 // side effects. [...]
3337 // - when a temporary class object that has not been bound to a
3338 // reference (12.2) would be copied/moved to a class object
3339 // with the same cv-unqualified type, the copy/move operation
3340 // can be omitted by constructing the temporary object
3341 // directly into the target of the omitted copy/move
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003342 //
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003343 // Note that the other three bullets are handled elsewhere. Copy
Douglas Gregor222cf0e2010-05-15 00:13:29 +00003344 // elision for return statements and throw expressions are handled as part
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003345 // of constructor initialization, while copy elision for exception handlers
Douglas Gregor222cf0e2010-05-15 00:13:29 +00003346 // is handled by the run-time.
John McCall7a626f62010-09-15 10:14:12 +00003347 bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class);
Douglas Gregore1314a62009-12-18 05:02:21 +00003348 SourceLocation Loc;
Douglas Gregore1314a62009-12-18 05:02:21 +00003349 switch (Entity.getKind()) {
3350 case InitializedEntity::EK_Result:
Douglas Gregore1314a62009-12-18 05:02:21 +00003351 Loc = Entity.getReturnLoc();
3352 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003353
Douglas Gregore1314a62009-12-18 05:02:21 +00003354 case InitializedEntity::EK_Exception:
Douglas Gregore1314a62009-12-18 05:02:21 +00003355 Loc = Entity.getThrowLoc();
3356 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003357
Douglas Gregore1314a62009-12-18 05:02:21 +00003358 case InitializedEntity::EK_Variable:
Douglas Gregora4b592a2009-12-19 03:01:41 +00003359 Loc = Entity.getDecl()->getLocation();
3360 break;
3361
Anders Carlsson0bd52402010-01-24 00:19:41 +00003362 case InitializedEntity::EK_ArrayElement:
3363 case InitializedEntity::EK_Member:
Douglas Gregore1314a62009-12-18 05:02:21 +00003364 case InitializedEntity::EK_Parameter:
Douglas Gregore1314a62009-12-18 05:02:21 +00003365 case InitializedEntity::EK_Temporary:
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003366 case InitializedEntity::EK_New:
Douglas Gregore1314a62009-12-18 05:02:21 +00003367 case InitializedEntity::EK_Base:
Anders Carlssoned8d80d2010-01-23 04:34:47 +00003368 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian28ed9272010-06-07 16:14:00 +00003369 case InitializedEntity::EK_BlockElement:
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003370 Loc = CurInitExpr->getLocStart();
3371 break;
Douglas Gregore1314a62009-12-18 05:02:21 +00003372 }
Douglas Gregord5c231e2010-04-24 21:09:25 +00003373
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003374 // Make sure that the type we are copying is complete.
Douglas Gregord5c231e2010-04-24 21:09:25 +00003375 if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete)))
3376 return move(CurInit);
3377
Douglas Gregorf282a762011-01-21 19:38:21 +00003378 // Perform overload resolution using the class's copy/move constructors.
Douglas Gregore1314a62009-12-18 05:02:21 +00003379 DeclContext::lookup_iterator Con, ConEnd;
John McCallbc077cf2010-02-08 23:07:23 +00003380 OverloadCandidateSet CandidateSet(Loc);
Douglas Gregor52b72822010-07-02 23:12:18 +00003381 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class);
Douglas Gregore1314a62009-12-18 05:02:21 +00003382 Con != ConEnd; ++Con) {
Douglas Gregorf282a762011-01-21 19:38:21 +00003383 // Only consider copy/move constructors and constructor templates. Per
Douglas Gregorcbd07102010-11-12 03:34:06 +00003384 // C++0x [dcl.init]p16, second bullet to class types, this
3385 // initialization is direct-initialization.
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003386 CXXConstructorDecl *Constructor = 0;
3387
3388 if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) {
Douglas Gregorf282a762011-01-21 19:38:21 +00003389 // Handle copy/moveconstructors, only.
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003390 if (!Constructor || Constructor->isInvalidDecl() ||
Douglas Gregorf282a762011-01-21 19:38:21 +00003391 !Constructor->isCopyOrMoveConstructor() ||
Douglas Gregorcbd07102010-11-12 03:34:06 +00003392 !Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003393 continue;
3394
3395 DeclAccessPair FoundDecl
3396 = DeclAccessPair::make(Constructor, Constructor->getAccess());
3397 S.AddOverloadCandidate(Constructor, FoundDecl,
3398 &CurInitExpr, 1, CandidateSet);
3399 continue;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003400 }
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003401
3402 // Handle constructor templates.
3403 FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con);
3404 if (ConstructorTmpl->isInvalidDecl())
Douglas Gregore1314a62009-12-18 05:02:21 +00003405 continue;
John McCalla0296f72010-03-19 07:35:19 +00003406
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003407 Constructor = cast<CXXConstructorDecl>(
3408 ConstructorTmpl->getTemplatedDecl());
Douglas Gregorcbd07102010-11-12 03:34:06 +00003409 if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003410 continue;
3411
3412 // FIXME: Do we need to limit this to copy-constructor-like
3413 // candidates?
John McCalla0296f72010-03-19 07:35:19 +00003414 DeclAccessPair FoundDecl
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00003415 = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess());
3416 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0,
3417 &CurInitExpr, 1, CandidateSet, true);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003418 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003419
Douglas Gregore1314a62009-12-18 05:02:21 +00003420 OverloadCandidateSet::iterator Best;
John McCall5c32be02010-08-24 20:38:10 +00003421 switch (CandidateSet.BestViableFunction(S, Loc, Best)) {
Douglas Gregore1314a62009-12-18 05:02:21 +00003422 case OR_Success:
3423 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003424
Douglas Gregore1314a62009-12-18 05:02:21 +00003425 case OR_No_Viable_Function:
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00003426 S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext()
3427 ? diag::ext_rvalue_to_reference_temp_copy_no_viable
3428 : diag::err_temp_copy_no_viable)
Douglas Gregora4b592a2009-12-19 03:01:41 +00003429 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregore1314a62009-12-18 05:02:21 +00003430 << CurInitExpr->getSourceRange();
John McCall5c32be02010-08-24 20:38:10 +00003431 CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1);
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00003432 if (!IsExtraneousCopy || S.isSFINAEContext())
John McCallfaf5fb42010-08-26 23:41:50 +00003433 return ExprError();
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00003434 return move(CurInit);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003435
Douglas Gregore1314a62009-12-18 05:02:21 +00003436 case OR_Ambiguous:
3437 S.Diag(Loc, diag::err_temp_copy_ambiguous)
Douglas Gregora4b592a2009-12-19 03:01:41 +00003438 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregore1314a62009-12-18 05:02:21 +00003439 << CurInitExpr->getSourceRange();
John McCall5c32be02010-08-24 20:38:10 +00003440 CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1);
John McCallfaf5fb42010-08-26 23:41:50 +00003441 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003442
Douglas Gregore1314a62009-12-18 05:02:21 +00003443 case OR_Deleted:
3444 S.Diag(Loc, diag::err_temp_copy_deleted)
Douglas Gregora4b592a2009-12-19 03:01:41 +00003445 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregore1314a62009-12-18 05:02:21 +00003446 << CurInitExpr->getSourceRange();
3447 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
3448 << Best->Function->isDeleted();
John McCallfaf5fb42010-08-26 23:41:50 +00003449 return ExprError();
Douglas Gregore1314a62009-12-18 05:02:21 +00003450 }
3451
Douglas Gregor5ab11652010-04-17 22:01:05 +00003452 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function);
John McCall37ad5512010-08-23 06:44:23 +00003453 ASTOwningVector<Expr*> ConstructorArgs(S);
Douglas Gregor5ab11652010-04-17 22:01:05 +00003454 CurInit.release(); // Ownership transferred into MultiExprArg, below.
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003455
Anders Carlssona01874b2010-04-21 18:47:17 +00003456 S.CheckConstructorAccess(Loc, Constructor, Entity,
Jeffrey Yasskincaa710d2010-06-07 15:58:05 +00003457 Best->FoundDecl.getAccess(), IsExtraneousCopy);
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003458
3459 if (IsExtraneousCopy) {
3460 // If this is a totally extraneous copy for C++03 reference
3461 // binding purposes, just return the original initialization
Douglas Gregor30b52772010-04-18 07:57:34 +00003462 // expression. We don't generate an (elided) copy operation here
3463 // because doing so would require us to pass down a flag to avoid
3464 // infinite recursion, where each step adds another extraneous,
3465 // elidable copy.
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003466
Douglas Gregor30b52772010-04-18 07:57:34 +00003467 // Instantiate the default arguments of any extra parameters in
3468 // the selected copy constructor, as if we were going to create a
3469 // proper call to the copy constructor.
3470 for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) {
3471 ParmVarDecl *Parm = Constructor->getParamDecl(I);
3472 if (S.RequireCompleteType(Loc, Parm->getType(),
3473 S.PDiag(diag::err_call_incomplete_argument)))
3474 break;
3475
3476 // Build the default argument expression; we don't actually care
3477 // if this succeeds or not, because this routine will complain
3478 // if there was a problem.
3479 S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm);
3480 }
3481
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003482 return S.Owned(CurInitExpr);
3483 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003484
Douglas Gregor5ab11652010-04-17 22:01:05 +00003485 // Determine the arguments required to actually perform the
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003486 // constructor call (we might have derived-to-base conversions, or
3487 // the copy constructor may have default arguments).
John McCallfaf5fb42010-08-26 23:41:50 +00003488 if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1),
Douglas Gregor5ab11652010-04-17 22:01:05 +00003489 Loc, ConstructorArgs))
John McCallfaf5fb42010-08-26 23:41:50 +00003490 return ExprError();
Douglas Gregor5ab11652010-04-17 22:01:05 +00003491
Douglas Gregord0ace022010-04-25 00:55:24 +00003492 // Actually perform the constructor call.
3493 CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable,
John McCallbfd822c2010-08-24 07:32:53 +00003494 move_arg(ConstructorArgs),
3495 /*ZeroInit*/ false,
Chandler Carruth01718152010-10-25 08:47:36 +00003496 CXXConstructExpr::CK_Complete,
3497 SourceRange());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003498
Douglas Gregord0ace022010-04-25 00:55:24 +00003499 // If we're supposed to bind temporaries, do so.
3500 if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity))
3501 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
3502 return move(CurInit);
Douglas Gregore1314a62009-12-18 05:02:21 +00003503}
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003504
Douglas Gregor4f4946a2010-04-22 00:20:18 +00003505void InitializationSequence::PrintInitLocationNote(Sema &S,
3506 const InitializedEntity &Entity) {
3507 if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) {
3508 if (Entity.getDecl()->getLocation().isInvalid())
3509 return;
3510
3511 if (Entity.getDecl()->getDeclName())
3512 S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here)
3513 << Entity.getDecl()->getDeclName();
3514 else
3515 S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here);
3516 }
3517}
3518
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003519ExprResult
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003520InitializationSequence::Perform(Sema &S,
3521 const InitializedEntity &Entity,
3522 const InitializationKind &Kind,
John McCallfaf5fb42010-08-26 23:41:50 +00003523 MultiExprArg Args,
Douglas Gregor51e77d52009-12-10 17:56:55 +00003524 QualType *ResultType) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003525 if (SequenceKind == FailedSequence) {
3526 unsigned NumArgs = Args.size();
3527 Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs);
John McCallfaf5fb42010-08-26 23:41:50 +00003528 return ExprError();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003529 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003530
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003531 if (SequenceKind == DependentSequence) {
Douglas Gregor51e77d52009-12-10 17:56:55 +00003532 // If the declaration is a non-dependent, incomplete array type
3533 // that has an initializer, then its type will be completed once
3534 // the initializer is instantiated.
Douglas Gregor1b303932009-12-22 15:35:07 +00003535 if (ResultType && !Entity.getType()->isDependentType() &&
Douglas Gregor51e77d52009-12-10 17:56:55 +00003536 Args.size() == 1) {
Douglas Gregor1b303932009-12-22 15:35:07 +00003537 QualType DeclType = Entity.getType();
Douglas Gregor51e77d52009-12-10 17:56:55 +00003538 if (const IncompleteArrayType *ArrayT
3539 = S.Context.getAsIncompleteArrayType(DeclType)) {
3540 // FIXME: We don't currently have the ability to accurately
3541 // compute the length of an initializer list without
3542 // performing full type-checking of the initializer list
3543 // (since we have to determine where braces are implicitly
3544 // introduced and such). So, we fall back to making the array
3545 // type a dependently-sized array type with no specified
3546 // bound.
3547 if (isa<InitListExpr>((Expr *)Args.get()[0])) {
3548 SourceRange Brackets;
Douglas Gregor1b303932009-12-22 15:35:07 +00003549
Douglas Gregor51e77d52009-12-10 17:56:55 +00003550 // Scavange the location of the brackets from the entity, if we can.
Douglas Gregor1b303932009-12-22 15:35:07 +00003551 if (DeclaratorDecl *DD = Entity.getDecl()) {
3552 if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) {
3553 TypeLoc TL = TInfo->getTypeLoc();
3554 if (IncompleteArrayTypeLoc *ArrayLoc
3555 = dyn_cast<IncompleteArrayTypeLoc>(&TL))
3556 Brackets = ArrayLoc->getBracketsRange();
3557 }
Douglas Gregor51e77d52009-12-10 17:56:55 +00003558 }
3559
3560 *ResultType
3561 = S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
3562 /*NumElts=*/0,
3563 ArrayT->getSizeModifier(),
3564 ArrayT->getIndexTypeCVRQualifiers(),
3565 Brackets);
3566 }
3567
3568 }
3569 }
3570
Eli Friedmana553d4a2009-12-22 02:35:53 +00003571 if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast())
John McCalldadc5752010-08-24 06:29:42 +00003572 return ExprResult(Args.release()[0]);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003573
Douglas Gregor0ab7af62010-02-05 07:56:11 +00003574 if (Args.size() == 0)
3575 return S.Owned((Expr *)0);
3576
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003577 unsigned NumArgs = Args.size();
3578 return S.Owned(new (S.Context) ParenListExpr(S.Context,
3579 SourceLocation(),
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003580 (Expr **)Args.release(),
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003581 NumArgs,
3582 SourceLocation()));
3583 }
3584
Douglas Gregor85dabae2009-12-16 01:38:02 +00003585 if (SequenceKind == NoInitialization)
3586 return S.Owned((Expr *)0);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003587
Douglas Gregor1b303932009-12-22 15:35:07 +00003588 QualType DestType = Entity.getType().getNonReferenceType();
3589 // FIXME: Ugly hack around the fact that Entity.getType() is not
Eli Friedman463e5232009-12-22 02:10:53 +00003590 // the same as Entity.getDecl()->getType() in cases involving type merging,
3591 // and we want latter when it makes sense.
Douglas Gregor51e77d52009-12-10 17:56:55 +00003592 if (ResultType)
Eli Friedman463e5232009-12-22 02:10:53 +00003593 *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() :
Douglas Gregor1b303932009-12-22 15:35:07 +00003594 Entity.getType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003595
John McCalldadc5752010-08-24 06:29:42 +00003596 ExprResult CurInit = S.Owned((Expr *)0);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003597
Douglas Gregor85dabae2009-12-16 01:38:02 +00003598 assert(!Steps.empty() && "Cannot have an empty initialization sequence");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003599
3600 // For initialization steps that start with a single initializer,
Douglas Gregor85dabae2009-12-16 01:38:02 +00003601 // grab the only argument out the Args and place it into the "current"
3602 // initializer.
3603 switch (Steps.front().Kind) {
Douglas Gregore1314a62009-12-18 05:02:21 +00003604 case SK_ResolveAddressOfOverloadedFunction:
3605 case SK_CastDerivedToBaseRValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003606 case SK_CastDerivedToBaseXValue:
Douglas Gregore1314a62009-12-18 05:02:21 +00003607 case SK_CastDerivedToBaseLValue:
3608 case SK_BindReference:
3609 case SK_BindReferenceToTemporary:
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003610 case SK_ExtraneousCopyToTemporary:
Douglas Gregore1314a62009-12-18 05:02:21 +00003611 case SK_UserConversion:
3612 case SK_QualificationConversionLValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003613 case SK_QualificationConversionXValue:
Douglas Gregore1314a62009-12-18 05:02:21 +00003614 case SK_QualificationConversionRValue:
3615 case SK_ConversionSequence:
3616 case SK_ListInitialization:
3617 case SK_CAssignment:
Eli Friedman78275202009-12-19 08:11:05 +00003618 case SK_StringInit:
John McCall34376a62010-12-04 03:47:34 +00003619 case SK_ObjCObjectConversion: {
Douglas Gregore1314a62009-12-18 05:02:21 +00003620 assert(Args.size() == 1);
John McCall34376a62010-12-04 03:47:34 +00003621 Expr *CurInitExpr = Args.get()[0];
3622 if (!CurInitExpr) return ExprError();
3623
3624 // Read from a property when initializing something with it.
3625 if (CurInitExpr->getObjectKind() == OK_ObjCProperty)
3626 S.ConvertPropertyForRValue(CurInitExpr);
3627
3628 CurInit = ExprResult(CurInitExpr);
Douglas Gregore1314a62009-12-18 05:02:21 +00003629 break;
John McCall34376a62010-12-04 03:47:34 +00003630 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003631
Douglas Gregore1314a62009-12-18 05:02:21 +00003632 case SK_ConstructorInitialization:
3633 case SK_ZeroInitialization:
3634 break;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003635 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003636
3637 // Walk through the computed steps for the initialization sequence,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003638 // performing the specified conversions along the way.
Douglas Gregor4f4b1862009-12-16 18:50:27 +00003639 bool ConstructorInitRequiresZeroInit = false;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003640 for (step_iterator Step = step_begin(), StepEnd = step_end();
3641 Step != StepEnd; ++Step) {
3642 if (CurInit.isInvalid())
John McCallfaf5fb42010-08-26 23:41:50 +00003643 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003644
John McCall34376a62010-12-04 03:47:34 +00003645 Expr *CurInitExpr = CurInit.get();
Douglas Gregor85dabae2009-12-16 01:38:02 +00003646 QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003647
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003648 switch (Step->Kind) {
3649 case SK_ResolveAddressOfOverloadedFunction:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003650 // Overload resolution determined which function invoke; update the
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003651 // initializer to reflect that choice.
John McCall16df1e52010-03-30 21:47:33 +00003652 S.CheckAddressOfMemberAccess(CurInitExpr, Step->Function.FoundDecl);
John McCall4fa0d5f2010-05-06 18:15:07 +00003653 S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation());
John McCall760af172010-02-01 03:16:54 +00003654 CurInit = S.FixOverloadedFunctionReference(move(CurInit),
John McCall16df1e52010-03-30 21:47:33 +00003655 Step->Function.FoundDecl,
John McCalla0296f72010-03-19 07:35:19 +00003656 Step->Function.Function);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003657 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003658
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003659 case SK_CastDerivedToBaseRValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003660 case SK_CastDerivedToBaseXValue:
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003661 case SK_CastDerivedToBaseLValue: {
3662 // We have a derived-to-base cast that produces either an rvalue or an
3663 // lvalue. Perform that cast.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003664
John McCallcf142162010-08-07 06:22:56 +00003665 CXXCastPath BasePath;
Anders Carlssona70cff62010-04-24 19:06:50 +00003666
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003667 // Casts to inaccessible base classes are allowed with C-style casts.
3668 bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
3669 if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
3670 CurInitExpr->getLocStart(),
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003671 CurInitExpr->getSourceRange(),
Anders Carlssona70cff62010-04-24 19:06:50 +00003672 &BasePath, IgnoreBaseAccess))
John McCallfaf5fb42010-08-26 23:41:50 +00003673 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003674
Douglas Gregor88d292c2010-05-13 16:44:06 +00003675 if (S.BasePathInvolvesVirtualBase(BasePath)) {
3676 QualType T = SourceType;
3677 if (const PointerType *Pointer = T->getAs<PointerType>())
3678 T = Pointer->getPointeeType();
3679 if (const RecordType *RecordTy = T->getAs<RecordType>())
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003680 S.MarkVTableUsed(CurInitExpr->getLocStart(),
Douglas Gregor88d292c2010-05-13 16:44:06 +00003681 cast<CXXRecordDecl>(RecordTy->getDecl()));
3682 }
3683
John McCall2536c6d2010-08-25 10:28:54 +00003684 ExprValueKind VK =
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003685 Step->Kind == SK_CastDerivedToBaseLValue ?
John McCall2536c6d2010-08-25 10:28:54 +00003686 VK_LValue :
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003687 (Step->Kind == SK_CastDerivedToBaseXValue ?
John McCall2536c6d2010-08-25 10:28:54 +00003688 VK_XValue :
3689 VK_RValue);
John McCallcf142162010-08-07 06:22:56 +00003690 CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
3691 Step->Type,
John McCalle3027922010-08-25 11:45:40 +00003692 CK_DerivedToBase,
John McCall2536c6d2010-08-25 10:28:54 +00003693 CurInit.get(),
3694 &BasePath, VK));
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003695 break;
3696 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003697
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003698 case SK_BindReference:
3699 if (FieldDecl *BitField = CurInitExpr->getBitField()) {
3700 // References cannot bind to bit fields (C++ [dcl.init.ref]p5).
3701 S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
Douglas Gregor1b303932009-12-22 15:35:07 +00003702 << Entity.getType().isVolatileQualified()
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003703 << BitField->getDeclName()
3704 << CurInitExpr->getSourceRange();
3705 S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
John McCallfaf5fb42010-08-26 23:41:50 +00003706 return ExprError();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003707 }
Anders Carlssona91be642010-01-29 02:47:33 +00003708
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003709 if (CurInitExpr->refersToVectorElement()) {
John McCallc17ae442010-02-02 19:02:38 +00003710 // References cannot bind to vector elements.
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003711 S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element)
3712 << Entity.getType().isVolatileQualified()
3713 << CurInitExpr->getSourceRange();
Douglas Gregor4f4946a2010-04-22 00:20:18 +00003714 PrintInitLocationNote(S, Entity);
John McCallfaf5fb42010-08-26 23:41:50 +00003715 return ExprError();
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003716 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003717
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003718 // Reference binding does not have any corresponding ASTs.
3719
3720 // Check exception specifications
3721 if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
John McCallfaf5fb42010-08-26 23:41:50 +00003722 return ExprError();
Anders Carlssonab0ddb52010-01-31 18:34:51 +00003723
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003724 break;
Anders Carlssonab0ddb52010-01-31 18:34:51 +00003725
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003726 case SK_BindReferenceToTemporary:
Anders Carlsson3b227bd2010-02-03 16:38:03 +00003727 // Reference binding does not have any corresponding ASTs.
3728
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003729 // Check exception specifications
3730 if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
John McCallfaf5fb42010-08-26 23:41:50 +00003731 return ExprError();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003732
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003733 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003734
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003735 case SK_ExtraneousCopyToTemporary:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003736 CurInit = CopyObject(S, Step->Type, Entity, move(CurInit),
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003737 /*IsExtraneousCopy=*/true);
3738 break;
3739
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003740 case SK_UserConversion: {
3741 // We have a user-defined conversion that invokes either a constructor
3742 // or a conversion function.
John McCall8cb679e2010-11-15 09:13:47 +00003743 CastKind CastKind;
Douglas Gregore1314a62009-12-18 05:02:21 +00003744 bool IsCopy = false;
John McCalla0296f72010-03-19 07:35:19 +00003745 FunctionDecl *Fn = Step->Function.Function;
3746 DeclAccessPair FoundFn = Step->Function.FoundDecl;
Douglas Gregor95562572010-04-24 23:45:46 +00003747 bool CreatedObject = false;
Douglas Gregor031296e2010-03-25 00:20:38 +00003748 bool IsLvalue = false;
John McCall760af172010-02-01 03:16:54 +00003749 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003750 // Build a call to the selected constructor.
John McCall37ad5512010-08-23 06:44:23 +00003751 ASTOwningVector<Expr*> ConstructorArgs(S);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003752 SourceLocation Loc = CurInitExpr->getLocStart();
3753 CurInit.release(); // Ownership transferred into MultiExprArg, below.
John McCall760af172010-02-01 03:16:54 +00003754
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003755 // Determine the arguments required to actually perform the constructor
3756 // call.
3757 if (S.CompleteConstructorCall(Constructor,
John McCallfaf5fb42010-08-26 23:41:50 +00003758 MultiExprArg(&CurInitExpr, 1),
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003759 Loc, ConstructorArgs))
John McCallfaf5fb42010-08-26 23:41:50 +00003760 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003761
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003762 // Build the an expression that constructs a temporary.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003763 CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
John McCallbfd822c2010-08-24 07:32:53 +00003764 move_arg(ConstructorArgs),
3765 /*ZeroInit*/ false,
Chandler Carruth01718152010-10-25 08:47:36 +00003766 CXXConstructExpr::CK_Complete,
3767 SourceRange());
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003768 if (CurInit.isInvalid())
John McCallfaf5fb42010-08-26 23:41:50 +00003769 return ExprError();
John McCall760af172010-02-01 03:16:54 +00003770
Anders Carlssona01874b2010-04-21 18:47:17 +00003771 S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity,
John McCalla0296f72010-03-19 07:35:19 +00003772 FoundFn.getAccess());
John McCall4fa0d5f2010-05-06 18:15:07 +00003773 S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003774
John McCalle3027922010-08-25 11:45:40 +00003775 CastKind = CK_ConstructorConversion;
Douglas Gregore1314a62009-12-18 05:02:21 +00003776 QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
3777 if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
3778 S.IsDerivedFrom(SourceType, Class))
3779 IsCopy = true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003780
Douglas Gregor95562572010-04-24 23:45:46 +00003781 CreatedObject = true;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003782 } else {
3783 // Build a call to the conversion function.
John McCall760af172010-02-01 03:16:54 +00003784 CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn);
Douglas Gregor031296e2010-03-25 00:20:38 +00003785 IsLvalue = Conversion->getResultType()->isLValueReferenceType();
John McCall1064d7e2010-03-16 05:22:47 +00003786 S.CheckMemberOperatorAccess(Kind.getLocation(), CurInitExpr, 0,
John McCalla0296f72010-03-19 07:35:19 +00003787 FoundFn);
John McCall4fa0d5f2010-05-06 18:15:07 +00003788 S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003789
3790 // FIXME: Should we move this initialization into a separate
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003791 // derived-to-base conversion? I believe the answer is "no", because
3792 // we don't want to turn off access control here for c-style casts.
Douglas Gregorcc3f3252010-03-03 23:55:11 +00003793 if (S.PerformObjectArgumentInitialization(CurInitExpr, /*Qualifier=*/0,
John McCall16df1e52010-03-30 21:47:33 +00003794 FoundFn, Conversion))
John McCallfaf5fb42010-08-26 23:41:50 +00003795 return ExprError();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003796
3797 // Do a little dance to make sure that CurInit has the proper
3798 // pointer.
3799 CurInit.release();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003800
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003801 // Build the actual call to the conversion function.
Douglas Gregor668443e2011-01-20 00:18:04 +00003802 CurInit = S.BuildCXXMemberCallExpr(CurInitExpr, FoundFn, Conversion);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003803 if (CurInit.isInvalid() || !CurInit.get())
John McCallfaf5fb42010-08-26 23:41:50 +00003804 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003805
John McCalle3027922010-08-25 11:45:40 +00003806 CastKind = CK_UserDefinedConversion;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003807
Douglas Gregor95562572010-04-24 23:45:46 +00003808 CreatedObject = Conversion->getResultType()->isRecordType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003809 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003810
3811 bool RequiresCopy = !IsCopy &&
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003812 getKind() != InitializationSequence::ReferenceBinding;
3813 if (RequiresCopy || shouldBindAsTemporary(Entity))
Douglas Gregore1314a62009-12-18 05:02:21 +00003814 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
Douglas Gregor95562572010-04-24 23:45:46 +00003815 else if (CreatedObject && shouldDestroyTemporary(Entity)) {
3816 CurInitExpr = static_cast<Expr *>(CurInit.get());
3817 QualType T = CurInitExpr->getType();
3818 if (const RecordType *Record = T->getAs<RecordType>()) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003819 CXXDestructorDecl *Destructor
Douglas Gregore71edda2010-07-01 22:47:18 +00003820 = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl()));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003821 S.CheckDestructorAccess(CurInitExpr->getLocStart(), Destructor,
Douglas Gregor95562572010-04-24 23:45:46 +00003822 S.PDiag(diag::err_access_dtor_temp) << T);
3823 S.MarkDeclarationReferenced(CurInitExpr->getLocStart(), Destructor);
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00003824 S.DiagnoseUseOfDecl(Destructor, CurInitExpr->getLocStart());
Douglas Gregor95562572010-04-24 23:45:46 +00003825 }
3826 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003827
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003828 CurInitExpr = CurInit.takeAs<Expr>();
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003829 // FIXME: xvalues
John McCallcf142162010-08-07 06:22:56 +00003830 CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
3831 CurInitExpr->getType(),
3832 CastKind, CurInitExpr, 0,
John McCall2536c6d2010-08-25 10:28:54 +00003833 IsLvalue ? VK_LValue : VK_RValue));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003834
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003835 if (RequiresCopy)
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00003836 CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity,
3837 move(CurInit), /*IsExtraneousCopy=*/false);
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003838
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003839 break;
3840 }
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003841
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003842 case SK_QualificationConversionLValue:
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003843 case SK_QualificationConversionXValue:
3844 case SK_QualificationConversionRValue: {
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003845 // Perform a qualification conversion; these can never go wrong.
John McCall2536c6d2010-08-25 10:28:54 +00003846 ExprValueKind VK =
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003847 Step->Kind == SK_QualificationConversionLValue ?
John McCall2536c6d2010-08-25 10:28:54 +00003848 VK_LValue :
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003849 (Step->Kind == SK_QualificationConversionXValue ?
John McCall2536c6d2010-08-25 10:28:54 +00003850 VK_XValue :
3851 VK_RValue);
John McCalle3027922010-08-25 11:45:40 +00003852 S.ImpCastExprToType(CurInitExpr, Step->Type, CK_NoOp, VK);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003853 CurInit.release();
3854 CurInit = S.Owned(CurInitExpr);
3855 break;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00003856 }
3857
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00003858 case SK_ConversionSequence: {
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00003859 if (S.PerformImplicitConversion(CurInitExpr, Step->Type, *Step->ICS,
Douglas Gregor6dd3a6a2010-12-02 21:47:04 +00003860 getAssignmentAction(Entity),
Douglas Gregor58281352011-01-27 00:58:17 +00003861 Kind.isCStyleOrFunctionalCast()))
John McCallfaf5fb42010-08-26 23:41:50 +00003862 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003863
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003864 CurInit.release();
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00003865 CurInit = S.Owned(CurInitExpr);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00003866 break;
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00003867 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003868
Douglas Gregor51e77d52009-12-10 17:56:55 +00003869 case SK_ListInitialization: {
3870 InitListExpr *InitList = cast<InitListExpr>(CurInitExpr);
3871 QualType Ty = Step->Type;
Douglas Gregor723796a2009-12-16 06:35:08 +00003872 if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty))
John McCallfaf5fb42010-08-26 23:41:50 +00003873 return ExprError();
Douglas Gregor51e77d52009-12-10 17:56:55 +00003874
3875 CurInit.release();
3876 CurInit = S.Owned(InitList);
3877 break;
3878 }
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003879
3880 case SK_ConstructorInitialization: {
Douglas Gregorb33eed02010-04-16 22:09:46 +00003881 unsigned NumArgs = Args.size();
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003882 CXXConstructorDecl *Constructor
John McCalla0296f72010-03-19 07:35:19 +00003883 = cast<CXXConstructorDecl>(Step->Function.Function);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003884
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003885 // Build a call to the selected constructor.
John McCall37ad5512010-08-23 06:44:23 +00003886 ASTOwningVector<Expr*> ConstructorArgs(S);
Fariborz Jahanianda2da9c2010-07-21 18:40:47 +00003887 SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid())
3888 ? Kind.getEqualLoc()
3889 : Kind.getLocation();
Chandler Carruthc9262402010-08-23 07:55:51 +00003890
3891 if (Kind.getKind() == InitializationKind::IK_Default) {
3892 // Force even a trivial, implicit default constructor to be
3893 // semantically checked. We do this explicitly because we don't build
3894 // the definition for completely trivial constructors.
3895 CXXRecordDecl *ClassDecl = Constructor->getParent();
3896 assert(ClassDecl && "No parent class for constructor.");
3897 if (Constructor->isImplicit() && Constructor->isDefaultConstructor() &&
3898 ClassDecl->hasTrivialConstructor() && !Constructor->isUsed(false))
3899 S.DefineImplicitDefaultConstructor(Loc, Constructor);
3900 }
3901
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003902 // Determine the arguments required to actually perform the constructor
3903 // call.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003904 if (S.CompleteConstructorCall(Constructor, move(Args),
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003905 Loc, ConstructorArgs))
John McCallfaf5fb42010-08-26 23:41:50 +00003906 return ExprError();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003907
3908
Douglas Gregor9bc6b7f2010-03-02 17:18:33 +00003909 if (Entity.getKind() == InitializedEntity::EK_Temporary &&
Douglas Gregorb33eed02010-04-16 22:09:46 +00003910 NumArgs != 1 && // FIXME: Hack to work around cast weirdness
Douglas Gregor9bc6b7f2010-03-02 17:18:33 +00003911 (Kind.getKind() == InitializationKind::IK_Direct ||
3912 Kind.getKind() == InitializationKind::IK_Value)) {
3913 // An explicitly-constructed temporary, e.g., X(1, 2).
3914 unsigned NumExprs = ConstructorArgs.size();
3915 Expr **Exprs = (Expr **)ConstructorArgs.take();
Fariborz Jahanian3fd2a552010-07-21 18:31:47 +00003916 S.MarkDeclarationReferenced(Loc, Constructor);
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00003917 S.DiagnoseUseOfDecl(Constructor, Loc);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003918
Douglas Gregor2b88c112010-09-08 00:15:04 +00003919 TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
3920 if (!TSInfo)
3921 TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003922
Douglas Gregor9bc6b7f2010-03-02 17:18:33 +00003923 CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context,
3924 Constructor,
Douglas Gregor2b88c112010-09-08 00:15:04 +00003925 TSInfo,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003926 Exprs,
Douglas Gregor9bc6b7f2010-03-02 17:18:33 +00003927 NumExprs,
Chandler Carruth01718152010-10-25 08:47:36 +00003928 Kind.getParenRange(),
Douglas Gregor199db362010-04-27 20:36:09 +00003929 ConstructorInitRequiresZeroInit));
Anders Carlssonbcc066b2010-05-02 22:54:08 +00003930 } else {
3931 CXXConstructExpr::ConstructionKind ConstructKind =
3932 CXXConstructExpr::CK_Complete;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003933
Anders Carlssonbcc066b2010-05-02 22:54:08 +00003934 if (Entity.getKind() == InitializedEntity::EK_Base) {
3935 ConstructKind = Entity.getBaseSpecifier()->isVirtual() ?
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003936 CXXConstructExpr::CK_VirtualBase :
Anders Carlssonbcc066b2010-05-02 22:54:08 +00003937 CXXConstructExpr::CK_NonVirtualBase;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003938 }
3939
Chandler Carruth01718152010-10-25 08:47:36 +00003940 // Only get the parenthesis range if it is a direct construction.
3941 SourceRange parenRange =
3942 Kind.getKind() == InitializationKind::IK_Direct ?
3943 Kind.getParenRange() : SourceRange();
3944
Douglas Gregor222cf0e2010-05-15 00:13:29 +00003945 // If the entity allows NRVO, mark the construction as elidable
3946 // unconditionally.
3947 if (Entity.allowsNRVO())
3948 CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
3949 Constructor, /*Elidable=*/true,
3950 move_arg(ConstructorArgs),
3951 ConstructorInitRequiresZeroInit,
Chandler Carruth01718152010-10-25 08:47:36 +00003952 ConstructKind,
3953 parenRange);
Douglas Gregor222cf0e2010-05-15 00:13:29 +00003954 else
3955 CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003956 Constructor,
Douglas Gregor222cf0e2010-05-15 00:13:29 +00003957 move_arg(ConstructorArgs),
3958 ConstructorInitRequiresZeroInit,
Chandler Carruth01718152010-10-25 08:47:36 +00003959 ConstructKind,
3960 parenRange);
Anders Carlssonbcc066b2010-05-02 22:54:08 +00003961 }
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003962 if (CurInit.isInvalid())
John McCallfaf5fb42010-08-26 23:41:50 +00003963 return ExprError();
John McCall760af172010-02-01 03:16:54 +00003964
3965 // Only check access if all of that succeeded.
Anders Carlssona01874b2010-04-21 18:47:17 +00003966 S.CheckConstructorAccess(Loc, Constructor, Entity,
John McCalla0296f72010-03-19 07:35:19 +00003967 Step->Function.FoundDecl.getAccess());
John McCall4fa0d5f2010-05-06 18:15:07 +00003968 S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003969
Douglas Gregor45cf7e32010-04-02 18:24:57 +00003970 if (shouldBindAsTemporary(Entity))
Douglas Gregore1314a62009-12-18 05:02:21 +00003971 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003972
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00003973 break;
3974 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003975
Douglas Gregor7dc42e52009-12-15 00:01:57 +00003976 case SK_ZeroInitialization: {
Douglas Gregor4f4b1862009-12-16 18:50:27 +00003977 step_iterator NextStep = Step;
3978 ++NextStep;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003979 if (NextStep != StepEnd &&
Douglas Gregor4f4b1862009-12-16 18:50:27 +00003980 NextStep->Kind == SK_ConstructorInitialization) {
3981 // The need for zero-initialization is recorded directly into
3982 // the call to the object's constructor within the next step.
3983 ConstructorInitRequiresZeroInit = true;
3984 } else if (Kind.getKind() == InitializationKind::IK_Value &&
3985 S.getLangOptions().CPlusPlus &&
3986 !Kind.isImplicitValueInit()) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00003987 TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
3988 if (!TSInfo)
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003989 TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type,
Douglas Gregor2b88c112010-09-08 00:15:04 +00003990 Kind.getRange().getBegin());
3991
3992 CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr(
3993 TSInfo->getType().getNonLValueExprType(S.Context),
3994 TSInfo,
Douglas Gregor7dc42e52009-12-15 00:01:57 +00003995 Kind.getRange().getEnd()));
Douglas Gregor4f4b1862009-12-16 18:50:27 +00003996 } else {
Douglas Gregor7dc42e52009-12-15 00:01:57 +00003997 CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type));
Douglas Gregor4f4b1862009-12-16 18:50:27 +00003998 }
Douglas Gregor7dc42e52009-12-15 00:01:57 +00003999 break;
4000 }
Douglas Gregore1314a62009-12-18 05:02:21 +00004001
4002 case SK_CAssignment: {
4003 QualType SourceType = CurInitExpr->getType();
4004 Sema::AssignConvertType ConvTy =
4005 S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr);
Douglas Gregor96596c92009-12-22 07:24:36 +00004006
4007 // If this is a call, allow conversion to a transparent union.
4008 if (ConvTy != Sema::Compatible &&
4009 Entity.getKind() == InitializedEntity::EK_Parameter &&
4010 S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr)
4011 == Sema::Compatible)
4012 ConvTy = Sema::Compatible;
4013
Douglas Gregor4f4946a2010-04-22 00:20:18 +00004014 bool Complained;
Douglas Gregore1314a62009-12-18 05:02:21 +00004015 if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
4016 Step->Type, SourceType,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004017 CurInitExpr,
Douglas Gregor4f4946a2010-04-22 00:20:18 +00004018 getAssignmentAction(Entity),
4019 &Complained)) {
4020 PrintInitLocationNote(S, Entity);
John McCallfaf5fb42010-08-26 23:41:50 +00004021 return ExprError();
Douglas Gregor4f4946a2010-04-22 00:20:18 +00004022 } else if (Complained)
4023 PrintInitLocationNote(S, Entity);
Douglas Gregore1314a62009-12-18 05:02:21 +00004024
4025 CurInit.release();
4026 CurInit = S.Owned(CurInitExpr);
4027 break;
4028 }
Eli Friedman78275202009-12-19 08:11:05 +00004029
4030 case SK_StringInit: {
4031 QualType Ty = Step->Type;
4032 CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, S);
4033 break;
4034 }
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004035
4036 case SK_ObjCObjectConversion:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004037 S.ImpCastExprToType(CurInitExpr, Step->Type,
John McCalle3027922010-08-25 11:45:40 +00004038 CK_ObjCObjectLValueCast,
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004039 S.CastCategory(CurInitExpr));
4040 CurInit.release();
4041 CurInit = S.Owned(CurInitExpr);
4042 break;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004043 }
4044 }
John McCall1f425642010-11-11 03:21:53 +00004045
4046 // Diagnose non-fatal problems with the completed initialization.
4047 if (Entity.getKind() == InitializedEntity::EK_Member &&
4048 cast<FieldDecl>(Entity.getDecl())->isBitField())
4049 S.CheckBitFieldInitialization(Kind.getLocation(),
4050 cast<FieldDecl>(Entity.getDecl()),
4051 CurInit.get());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004052
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004053 return move(CurInit);
4054}
4055
4056//===----------------------------------------------------------------------===//
4057// Diagnose initialization failures
4058//===----------------------------------------------------------------------===//
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004059bool InitializationSequence::Diagnose(Sema &S,
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004060 const InitializedEntity &Entity,
4061 const InitializationKind &Kind,
4062 Expr **Args, unsigned NumArgs) {
4063 if (SequenceKind != FailedSequence)
4064 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004065
Douglas Gregor1b303932009-12-22 15:35:07 +00004066 QualType DestType = Entity.getType();
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004067 switch (Failure) {
4068 case FK_TooManyInitsForReference:
Douglas Gregor7ae2d772010-01-31 09:12:51 +00004069 // FIXME: Customize for the initialized entity?
4070 if (NumArgs == 0)
4071 S.Diag(Kind.getLocation(), diag::err_reference_without_init)
4072 << DestType.getNonReferenceType();
4073 else // FIXME: diagnostic below could be better!
4074 S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
4075 << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004076 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004077
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004078 case FK_ArrayNeedsInitList:
4079 case FK_ArrayNeedsInitListOrStringLiteral:
4080 S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list)
4081 << (Failure == FK_ArrayNeedsInitListOrStringLiteral);
4082 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004083
John McCall16df1e52010-03-30 21:47:33 +00004084 case FK_AddressOfOverloadFailed: {
4085 DeclAccessPair Found;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004086 S.ResolveAddressOfOverloadedFunction(Args[0],
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004087 DestType.getNonReferenceType(),
John McCall16df1e52010-03-30 21:47:33 +00004088 true,
4089 Found);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004090 break;
John McCall16df1e52010-03-30 21:47:33 +00004091 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004092
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004093 case FK_ReferenceInitOverloadFailed:
Douglas Gregor540c3b02009-12-14 17:27:33 +00004094 case FK_UserConversionOverloadFailed:
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004095 switch (FailedOverloadResult) {
4096 case OR_Ambiguous:
Douglas Gregore1314a62009-12-18 05:02:21 +00004097 if (Failure == FK_UserConversionOverloadFailed)
4098 S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
4099 << Args[0]->getType() << DestType
4100 << Args[0]->getSourceRange();
4101 else
4102 S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
4103 << DestType << Args[0]->getType()
4104 << Args[0]->getSourceRange();
4105
John McCall5c32be02010-08-24 20:38:10 +00004106 FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004107 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004108
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004109 case OR_No_Viable_Function:
4110 S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
4111 << Args[0]->getType() << DestType.getNonReferenceType()
4112 << Args[0]->getSourceRange();
John McCall5c32be02010-08-24 20:38:10 +00004113 FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004114 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004115
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004116 case OR_Deleted: {
4117 S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
4118 << Args[0]->getType() << DestType.getNonReferenceType()
4119 << Args[0]->getSourceRange();
4120 OverloadCandidateSet::iterator Best;
John McCall5c32be02010-08-24 20:38:10 +00004121 OverloadingResult Ovl
Douglas Gregord5b730c92010-09-12 08:07:23 +00004122 = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best,
4123 true);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004124 if (Ovl == OR_Deleted) {
4125 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
4126 << Best->Function->isDeleted();
4127 } else {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004128 llvm_unreachable("Inconsistent overload resolution?");
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004129 }
4130 break;
4131 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004132
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004133 case OR_Success:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004134 llvm_unreachable("Conversion did not fail!");
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004135 break;
4136 }
4137 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004138
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004139 case FK_NonConstLValueReferenceBindingToTemporary:
4140 case FK_NonConstLValueReferenceBindingToUnrelated:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004141 S.Diag(Kind.getLocation(),
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004142 Failure == FK_NonConstLValueReferenceBindingToTemporary
4143 ? diag::err_lvalue_reference_bind_to_temporary
4144 : diag::err_lvalue_reference_bind_to_unrelated)
Douglas Gregord1e08642010-01-29 19:39:15 +00004145 << DestType.getNonReferenceType().isVolatileQualified()
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004146 << DestType.getNonReferenceType()
4147 << Args[0]->getType()
4148 << Args[0]->getSourceRange();
4149 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004150
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004151 case FK_RValueReferenceBindingToLValue:
4152 S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
Douglas Gregorbed28f72011-01-21 01:04:33 +00004153 << DestType.getNonReferenceType() << Args[0]->getType()
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004154 << Args[0]->getSourceRange();
4155 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004156
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004157 case FK_ReferenceInitDropsQualifiers:
4158 S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
4159 << DestType.getNonReferenceType()
4160 << Args[0]->getType()
4161 << Args[0]->getSourceRange();
4162 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004163
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004164 case FK_ReferenceInitFailed:
4165 S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
4166 << DestType.getNonReferenceType()
John McCall086a4642010-11-24 05:12:34 +00004167 << Args[0]->isLValue()
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004168 << Args[0]->getType()
4169 << Args[0]->getSourceRange();
4170 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004171
Douglas Gregorb491ed32011-02-19 21:32:49 +00004172 case FK_ConversionFailed: {
4173 QualType FromType = Args[0]->getType();
Douglas Gregore1314a62009-12-18 05:02:21 +00004174 S.Diag(Kind.getLocation(), diag::err_init_conversion_failed)
4175 << (int)Entity.getKind()
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004176 << DestType
John McCall086a4642010-11-24 05:12:34 +00004177 << Args[0]->isLValue()
Douglas Gregorb491ed32011-02-19 21:32:49 +00004178 << FromType
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004179 << Args[0]->getSourceRange();
Douglas Gregor51e77d52009-12-10 17:56:55 +00004180 break;
Douglas Gregorb491ed32011-02-19 21:32:49 +00004181 }
Douglas Gregor51e77d52009-12-10 17:56:55 +00004182 case FK_TooManyInitsForScalar: {
Douglas Gregor85dabae2009-12-16 01:38:02 +00004183 SourceRange R;
4184
4185 if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
Douglas Gregor8ec51732010-09-08 21:40:08 +00004186 R = SourceRange(InitList->getInit(0)->getLocEnd(),
Douglas Gregor85dabae2009-12-16 01:38:02 +00004187 InitList->getLocEnd());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004188 else
Douglas Gregor8ec51732010-09-08 21:40:08 +00004189 R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd());
Douglas Gregor51e77d52009-12-10 17:56:55 +00004190
Douglas Gregor8ec51732010-09-08 21:40:08 +00004191 R.setBegin(S.PP.getLocForEndOfToken(R.getBegin()));
4192 if (Kind.isCStyleOrFunctionalCast())
4193 S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg)
4194 << R;
4195 else
4196 S.Diag(Kind.getLocation(), diag::err_excess_initializers)
4197 << /*scalar=*/2 << R;
Douglas Gregor51e77d52009-12-10 17:56:55 +00004198 break;
4199 }
4200
4201 case FK_ReferenceBindingToInitList:
4202 S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
4203 << DestType.getNonReferenceType() << Args[0]->getSourceRange();
4204 break;
4205
4206 case FK_InitListBadDestinationType:
4207 S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
4208 << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
4209 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004210
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004211 case FK_ConstructorOverloadFailed: {
4212 SourceRange ArgsRange;
4213 if (NumArgs)
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004214 ArgsRange = SourceRange(Args[0]->getLocStart(),
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004215 Args[NumArgs - 1]->getLocEnd());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004216
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004217 // FIXME: Using "DestType" for the entity we're printing is probably
4218 // bad.
4219 switch (FailedOverloadResult) {
4220 case OR_Ambiguous:
4221 S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
4222 << DestType << ArgsRange;
John McCall5c32be02010-08-24 20:38:10 +00004223 FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates,
4224 Args, NumArgs);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004225 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004226
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004227 case OR_No_Viable_Function:
Douglas Gregor7ae2d772010-01-31 09:12:51 +00004228 if (Kind.getKind() == InitializationKind::IK_Default &&
4229 (Entity.getKind() == InitializedEntity::EK_Base ||
4230 Entity.getKind() == InitializedEntity::EK_Member) &&
4231 isa<CXXConstructorDecl>(S.CurContext)) {
4232 // This is implicit default initialization of a member or
4233 // base within a constructor. If no viable function was
4234 // found, notify the user that she needs to explicitly
4235 // initialize this base/member.
4236 CXXConstructorDecl *Constructor
4237 = cast<CXXConstructorDecl>(S.CurContext);
4238 if (Entity.getKind() == InitializedEntity::EK_Base) {
4239 S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
4240 << Constructor->isImplicit()
4241 << S.Context.getTypeDeclType(Constructor->getParent())
4242 << /*base=*/0
4243 << Entity.getType();
4244
4245 RecordDecl *BaseDecl
4246 = Entity.getBaseSpecifier()->getType()->getAs<RecordType>()
4247 ->getDecl();
4248 S.Diag(BaseDecl->getLocation(), diag::note_previous_decl)
4249 << S.Context.getTagDeclType(BaseDecl);
4250 } else {
4251 S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
4252 << Constructor->isImplicit()
4253 << S.Context.getTypeDeclType(Constructor->getParent())
4254 << /*member=*/1
4255 << Entity.getName();
4256 S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl);
4257
4258 if (const RecordType *Record
4259 = Entity.getType()->getAs<RecordType>())
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004260 S.Diag(Record->getDecl()->getLocation(),
Douglas Gregor7ae2d772010-01-31 09:12:51 +00004261 diag::note_previous_decl)
4262 << S.Context.getTagDeclType(Record->getDecl());
4263 }
4264 break;
4265 }
4266
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004267 S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
4268 << DestType << ArgsRange;
John McCall5c32be02010-08-24 20:38:10 +00004269 FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004270 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004271
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004272 case OR_Deleted: {
4273 S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
4274 << true << DestType << ArgsRange;
4275 OverloadCandidateSet::iterator Best;
John McCall5c32be02010-08-24 20:38:10 +00004276 OverloadingResult Ovl
4277 = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004278 if (Ovl == OR_Deleted) {
4279 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
4280 << Best->Function->isDeleted();
4281 } else {
4282 llvm_unreachable("Inconsistent overload resolution?");
4283 }
4284 break;
4285 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004286
Douglas Gregor1e7ffa72009-12-14 20:49:26 +00004287 case OR_Success:
4288 llvm_unreachable("Conversion did not fail!");
4289 break;
4290 }
4291 break;
4292 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004293
Douglas Gregor85dabae2009-12-16 01:38:02 +00004294 case FK_DefaultInitOfConst:
Douglas Gregor7ae2d772010-01-31 09:12:51 +00004295 if (Entity.getKind() == InitializedEntity::EK_Member &&
4296 isa<CXXConstructorDecl>(S.CurContext)) {
4297 // This is implicit default-initialization of a const member in
4298 // a constructor. Complain that it needs to be explicitly
4299 // initialized.
4300 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext);
4301 S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor)
4302 << Constructor->isImplicit()
4303 << S.Context.getTypeDeclType(Constructor->getParent())
4304 << /*const=*/1
4305 << Entity.getName();
4306 S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl)
4307 << Entity.getName();
4308 } else {
4309 S.Diag(Kind.getLocation(), diag::err_default_init_const)
4310 << DestType << (bool)DestType->getAs<RecordType>();
4311 }
Douglas Gregor85dabae2009-12-16 01:38:02 +00004312 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004313
Douglas Gregor3f4f03a2010-05-20 22:12:02 +00004314 case FK_Incomplete:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004315 S.RequireCompleteType(Kind.getLocation(), DestType,
Douglas Gregor3f4f03a2010-05-20 22:12:02 +00004316 diag::err_init_incomplete_type);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004317 break;
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004318 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004319
Douglas Gregor4f4946a2010-04-22 00:20:18 +00004320 PrintInitLocationNote(S, Entity);
Douglas Gregor3e1e5272009-12-09 23:02:17 +00004321 return true;
4322}
Douglas Gregore1314a62009-12-18 05:02:21 +00004323
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004324void InitializationSequence::dump(llvm::raw_ostream &OS) const {
4325 switch (SequenceKind) {
4326 case FailedSequence: {
4327 OS << "Failed sequence: ";
4328 switch (Failure) {
4329 case FK_TooManyInitsForReference:
4330 OS << "too many initializers for reference";
4331 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004332
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004333 case FK_ArrayNeedsInitList:
4334 OS << "array requires initializer list";
4335 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004336
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004337 case FK_ArrayNeedsInitListOrStringLiteral:
4338 OS << "array requires initializer list or string literal";
4339 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004340
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004341 case FK_AddressOfOverloadFailed:
4342 OS << "address of overloaded function failed";
4343 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004344
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004345 case FK_ReferenceInitOverloadFailed:
4346 OS << "overload resolution for reference initialization failed";
4347 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004348
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004349 case FK_NonConstLValueReferenceBindingToTemporary:
4350 OS << "non-const lvalue reference bound to temporary";
4351 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004352
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004353 case FK_NonConstLValueReferenceBindingToUnrelated:
4354 OS << "non-const lvalue reference bound to unrelated type";
4355 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004356
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004357 case FK_RValueReferenceBindingToLValue:
4358 OS << "rvalue reference bound to an lvalue";
4359 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004360
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004361 case FK_ReferenceInitDropsQualifiers:
4362 OS << "reference initialization drops qualifiers";
4363 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004364
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004365 case FK_ReferenceInitFailed:
4366 OS << "reference initialization failed";
4367 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004368
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004369 case FK_ConversionFailed:
4370 OS << "conversion failed";
4371 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004372
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004373 case FK_TooManyInitsForScalar:
4374 OS << "too many initializers for scalar";
4375 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004376
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004377 case FK_ReferenceBindingToInitList:
4378 OS << "referencing binding to initializer list";
4379 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004380
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004381 case FK_InitListBadDestinationType:
4382 OS << "initializer list for non-aggregate, non-scalar type";
4383 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004384
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004385 case FK_UserConversionOverloadFailed:
4386 OS << "overloading failed for user-defined conversion";
4387 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004388
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004389 case FK_ConstructorOverloadFailed:
4390 OS << "constructor overloading failed";
4391 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004392
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004393 case FK_DefaultInitOfConst:
4394 OS << "default initialization of a const variable";
4395 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004396
Douglas Gregor3f4f03a2010-05-20 22:12:02 +00004397 case FK_Incomplete:
4398 OS << "initialization of incomplete type";
4399 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004400 }
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004401 OS << '\n';
4402 return;
4403 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004404
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004405 case DependentSequence:
4406 OS << "Dependent sequence: ";
4407 return;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004408
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004409 case UserDefinedConversion:
4410 OS << "User-defined conversion sequence: ";
4411 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004412
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004413 case ConstructorInitialization:
4414 OS << "Constructor initialization sequence: ";
4415 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004416
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004417 case ReferenceBinding:
4418 OS << "Reference binding: ";
4419 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004420
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004421 case ListInitialization:
4422 OS << "List initialization: ";
4423 break;
4424
4425 case ZeroInitialization:
4426 OS << "Zero initialization\n";
4427 return;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004428
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004429 case NoInitialization:
4430 OS << "No initialization\n";
4431 return;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004432
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004433 case StandardConversion:
4434 OS << "Standard conversion: ";
4435 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004436
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004437 case CAssignment:
4438 OS << "C assignment: ";
4439 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004440
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004441 case StringInit:
4442 OS << "String initialization: ";
4443 break;
4444 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004445
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004446 for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) {
4447 if (S != step_begin()) {
4448 OS << " -> ";
4449 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004450
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004451 switch (S->Kind) {
4452 case SK_ResolveAddressOfOverloadedFunction:
4453 OS << "resolve address of overloaded function";
4454 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004455
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004456 case SK_CastDerivedToBaseRValue:
4457 OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")";
4458 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004459
Sebastian Redlc57d34b2010-07-20 04:20:21 +00004460 case SK_CastDerivedToBaseXValue:
4461 OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")";
4462 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004463
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004464 case SK_CastDerivedToBaseLValue:
4465 OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")";
4466 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004467
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004468 case SK_BindReference:
4469 OS << "bind reference to lvalue";
4470 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004471
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004472 case SK_BindReferenceToTemporary:
4473 OS << "bind reference to a temporary";
4474 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004475
Douglas Gregorc9cd64e2010-04-18 07:40:54 +00004476 case SK_ExtraneousCopyToTemporary:
4477 OS << "extraneous C++03 copy to temporary";
4478 break;
4479
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004480 case SK_UserConversion:
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004481 OS << "user-defined conversion via " << S->Function.Function;
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004482 break;
Sebastian Redlc57d34b2010-07-20 04:20:21 +00004483
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004484 case SK_QualificationConversionRValue:
4485 OS << "qualification conversion (rvalue)";
4486
Sebastian Redlc57d34b2010-07-20 04:20:21 +00004487 case SK_QualificationConversionXValue:
4488 OS << "qualification conversion (xvalue)";
4489
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004490 case SK_QualificationConversionLValue:
4491 OS << "qualification conversion (lvalue)";
4492 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004493
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004494 case SK_ConversionSequence:
4495 OS << "implicit conversion sequence (";
4496 S->ICS->DebugPrint(); // FIXME: use OS
4497 OS << ")";
4498 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004499
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004500 case SK_ListInitialization:
4501 OS << "list initialization";
4502 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004503
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004504 case SK_ConstructorInitialization:
4505 OS << "constructor initialization";
4506 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004507
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004508 case SK_ZeroInitialization:
4509 OS << "zero initialization";
4510 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004511
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004512 case SK_CAssignment:
4513 OS << "C assignment";
4514 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004515
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004516 case SK_StringInit:
4517 OS << "string initialization";
4518 break;
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004519
4520 case SK_ObjCObjectConversion:
4521 OS << "Objective-C object conversion";
4522 break;
Douglas Gregor65eb86e2010-01-29 19:14:02 +00004523 }
4524 }
4525}
4526
4527void InitializationSequence::dump() const {
4528 dump(llvm::errs());
4529}
4530
Douglas Gregore1314a62009-12-18 05:02:21 +00004531//===----------------------------------------------------------------------===//
4532// Initialization helper functions
4533//===----------------------------------------------------------------------===//
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004534ExprResult
Douglas Gregore1314a62009-12-18 05:02:21 +00004535Sema::PerformCopyInitialization(const InitializedEntity &Entity,
4536 SourceLocation EqualLoc,
John McCalldadc5752010-08-24 06:29:42 +00004537 ExprResult Init) {
Douglas Gregore1314a62009-12-18 05:02:21 +00004538 if (Init.isInvalid())
4539 return ExprError();
4540
John McCall1f425642010-11-11 03:21:53 +00004541 Expr *InitE = Init.get();
Douglas Gregore1314a62009-12-18 05:02:21 +00004542 assert(InitE && "No initialization expression?");
4543
4544 if (EqualLoc.isInvalid())
4545 EqualLoc = InitE->getLocStart();
4546
4547 InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
4548 EqualLoc);
4549 InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
4550 Init.release();
John McCallfaf5fb42010-08-26 23:41:50 +00004551 return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1));
Douglas Gregore1314a62009-12-18 05:02:21 +00004552}