blob: de6b962aa36beb5a108f1274004479ccf819f3cb [file] [log] [blame]
Steve Naroff0cca7492008-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 Lattnerdd8e0062009-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 Naroff0cca7492008-05-01 22:18:59 +000014//===----------------------------------------------------------------------===//
15
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Designator.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Initialization.h"
18#include "clang/Sema/Lookup.h"
John McCall2d887082010-08-25 22:03:47 +000019#include "clang/Sema/SemaInternal.h"
Tanya Lattner1e1d3962010-03-07 04:17:15 +000020#include "clang/Lex/Preprocessor.h"
Steve Naroff0cca7492008-05-01 22:18:59 +000021#include "clang/AST/ASTContext.h"
John McCall7cd088e2010-08-24 07:21:54 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson2078bb92009-05-27 16:10:08 +000023#include "clang/AST/ExprCXX.h"
Chris Lattner79e079d2009-02-24 23:10:27 +000024#include "clang/AST/ExprObjC.h"
Douglas Gregord6542d82009-12-22 15:35:07 +000025#include "clang/AST/TypeLoc.h"
Douglas Gregor20093b42009-12-09 23:02:17 +000026#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc34ee5e2009-01-29 00:45:39 +000027#include <map>
Douglas Gregor05c13a32009-01-22 00:58:24 +000028using namespace clang;
Steve Naroff0cca7492008-05-01 22:18:59 +000029
Chris Lattnerdd8e0062009-02-24 22:27:37 +000030//===----------------------------------------------------------------------===//
31// Sema Initialization Checking
32//===----------------------------------------------------------------------===//
33
John McCallce6c9b72011-02-21 07:22:22 +000034static Expr *IsStringInit(Expr *Init, const ArrayType *AT,
35 ASTContext &Context) {
Eli Friedman8718a6a2009-05-29 18:22:49 +000036 if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
37 return 0;
38
Chris Lattner8879e3b2009-02-26 23:26:43 +000039 // See if this is a string literal or @encode.
40 Init = Init->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +000041
Chris Lattner8879e3b2009-02-26 23:26:43 +000042 // Handle @encode, which is a narrow string.
43 if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
44 return Init;
45
46 // Otherwise we can only handle string literals.
47 StringLiteral *SL = dyn_cast<StringLiteral>(Init);
Chris Lattner220b6362009-02-26 23:42:47 +000048 if (SL == 0) return 0;
Eli Friedmanbb6415c2009-05-31 10:54:53 +000049
50 QualType ElemTy = Context.getCanonicalType(AT->getElementType());
Chris Lattner8879e3b2009-02-26 23:26:43 +000051 // char array can be initialized with a narrow string.
52 // Only allow char x[] = "foo"; not char x[] = L"foo";
53 if (!SL->isWide())
Eli Friedmanbb6415c2009-05-31 10:54:53 +000054 return ElemTy->isCharType() ? Init : 0;
Chris Lattner8879e3b2009-02-26 23:26:43 +000055
Eli Friedmanbb6415c2009-05-31 10:54:53 +000056 // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
57 // correction from DR343): "An array with element type compatible with a
58 // qualified or unqualified version of wchar_t may be initialized by a wide
59 // string literal, optionally enclosed in braces."
60 if (Context.typesAreCompatible(Context.getWCharType(),
61 ElemTy.getUnqualifiedType()))
Chris Lattner8879e3b2009-02-26 23:26:43 +000062 return Init;
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattnerdd8e0062009-02-24 22:27:37 +000064 return 0;
65}
66
John McCallce6c9b72011-02-21 07:22:22 +000067static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) {
68 const ArrayType *arrayType = Context.getAsArrayType(declType);
69 if (!arrayType) return 0;
70
71 return IsStringInit(init, arrayType, Context);
72}
73
John McCallfef8b342011-02-21 07:57:55 +000074static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
75 Sema &S) {
Chris Lattner79e079d2009-02-24 23:10:27 +000076 // Get the length of the string as parsed.
77 uint64_t StrLength =
78 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
79
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattnerdd8e0062009-02-24 22:27:37 +000081 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Mike Stump1eb44332009-09-09 15:08:12 +000082 // C99 6.7.8p14. We have an array of character type with unknown size
Chris Lattnerdd8e0062009-02-24 22:27:37 +000083 // being initialized to a string literal.
84 llvm::APSInt ConstVal(32);
Chris Lattner19da8cd2009-02-24 23:01:39 +000085 ConstVal = StrLength;
Chris Lattnerdd8e0062009-02-24 22:27:37 +000086 // Return a new array type (C99 6.7.8p22).
John McCall46a617a2009-10-16 00:14:28 +000087 DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
88 ConstVal,
89 ArrayType::Normal, 0);
Chris Lattner19da8cd2009-02-24 23:01:39 +000090 return;
Chris Lattnerdd8e0062009-02-24 22:27:37 +000091 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Eli Friedman8718a6a2009-05-29 18:22:49 +000093 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
Mike Stump1eb44332009-09-09 15:08:12 +000094
Eli Friedman8718a6a2009-05-29 18:22:49 +000095 // C99 6.7.8p14. We have an array of character type with known size. However,
96 // the size may be smaller or larger than the string we are initializing.
97 // FIXME: Avoid truncation for 64-bit length strings.
98 if (StrLength-1 > CAT->getSize().getZExtValue())
99 S.Diag(Str->getSourceRange().getBegin(),
100 diag::warn_initializer_string_for_char_array_too_long)
101 << Str->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Eli Friedman8718a6a2009-05-29 18:22:49 +0000103 // Set the type to the actual size that we are initializing. If we have
104 // something like:
105 // char x[1] = "foo";
106 // then this will set the string literal's type to char[1].
107 Str->setType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000108}
109
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000110//===----------------------------------------------------------------------===//
111// Semantic checking for initializer lists.
112//===----------------------------------------------------------------------===//
113
Douglas Gregor9e80f722009-01-29 01:05:33 +0000114/// @brief Semantic checking for initializer lists.
115///
116/// The InitListChecker class contains a set of routines that each
117/// handle the initialization of a certain kind of entity, e.g.,
118/// arrays, vectors, struct/union types, scalars, etc. The
119/// InitListChecker itself performs a recursive walk of the subobject
120/// structure of the type to be initialized, while stepping through
121/// the initializer list one element at a time. The IList and Index
122/// parameters to each of the Check* routines contain the active
123/// (syntactic) initializer list and the index into that initializer
124/// list that represents the current initializer. Each routine is
125/// responsible for moving that Index forward as it consumes elements.
126///
127/// Each Check* routine also has a StructuredList/StructuredIndex
Abramo Bagnara63e7d252011-01-27 19:55:10 +0000128/// arguments, which contains the current "structured" (semantic)
Douglas Gregor9e80f722009-01-29 01:05:33 +0000129/// initializer list and the index into that initializer list where we
130/// are copying initializers as we map them over to the semantic
131/// list. Once we have completed our recursive walk of the subobject
132/// structure, we will have constructed a full semantic initializer
133/// list.
134///
135/// C99 designators cause changes in the initializer list traversal,
136/// because they make the initialization "jump" into a specific
137/// subobject and then continue the initialization from that
138/// point. CheckDesignatedInitializer() recursively steps into the
139/// designated subobject and manages backing out the recursion to
140/// initialize the subobjects after the one designated.
Chris Lattner8b419b92009-02-24 22:48:58 +0000141namespace {
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000142class InitListChecker {
Chris Lattner08202542009-02-24 22:50:46 +0000143 Sema &SemaRef;
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000144 bool hadError;
145 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
146 InitListExpr *FullyStructuredList;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000148 void CheckImplicitInitList(const InitializedEntity &Entity,
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000149 InitListExpr *ParentIList, QualType T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000150 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000151 unsigned &StructuredIndex,
152 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000153 void CheckExplicitInitList(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000154 InitListExpr *IList, QualType &T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000155 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000156 unsigned &StructuredIndex,
157 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000158 void CheckListElementTypes(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000159 InitListExpr *IList, QualType &DeclType,
Mike Stump1eb44332009-09-09 15:08:12 +0000160 bool SubobjectIsDesignatorContext,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000161 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000162 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000163 unsigned &StructuredIndex,
164 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000165 void CheckSubElementType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000166 InitListExpr *IList, QualType ElemType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000167 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000168 InitListExpr *StructuredList,
169 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000170 void CheckScalarType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000171 InitListExpr *IList, QualType DeclType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000172 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000173 InitListExpr *StructuredList,
174 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000175 void CheckReferenceType(const InitializedEntity &Entity,
176 InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000177 unsigned &Index,
178 InitListExpr *StructuredList,
179 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000180 void CheckVectorType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000181 InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000182 InitListExpr *StructuredList,
183 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000184 void CheckStructUnionTypes(const InitializedEntity &Entity,
Anders Carlsson2bbae5d2010-01-23 20:20:40 +0000185 InitListExpr *IList, QualType DeclType,
Mike Stump1eb44332009-09-09 15:08:12 +0000186 RecordDecl::field_iterator Field,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000187 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000188 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000189 unsigned &StructuredIndex,
190 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000191 void CheckArrayType(const InitializedEntity &Entity,
Anders Carlsson784f6992010-01-23 20:13:41 +0000192 InitListExpr *IList, QualType &DeclType,
Mike Stump1eb44332009-09-09 15:08:12 +0000193 llvm::APSInt elementIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000194 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000195 InitListExpr *StructuredList,
196 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000197 bool CheckDesignatedInitializer(const InitializedEntity &Entity,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +0000198 InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +0000199 unsigned DesigIdx,
Mike Stump1eb44332009-09-09 15:08:12 +0000200 QualType &CurrentObjectType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000201 RecordDecl::field_iterator *NextField,
202 llvm::APSInt *NextElementIndex,
203 unsigned &Index,
204 InitListExpr *StructuredList,
205 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000206 bool FinishSubobjectInit,
207 bool TopLevelObject);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000208 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
209 QualType CurrentObjectType,
210 InitListExpr *StructuredList,
211 unsigned StructuredIndex,
212 SourceRange InitRange);
Douglas Gregor9e80f722009-01-29 01:05:33 +0000213 void UpdateStructuredListElement(InitListExpr *StructuredList,
214 unsigned &StructuredIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000215 Expr *expr);
216 int numArrayElements(QualType DeclType);
217 int numStructUnionElements(QualType DeclType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000218
Douglas Gregord6d37de2009-12-22 00:05:34 +0000219 void FillInValueInitForField(unsigned Init, FieldDecl *Field,
220 const InitializedEntity &ParentEntity,
221 InitListExpr *ILE, bool &RequiresSecondPass);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000222 void FillInValueInitializations(const InitializedEntity &Entity,
223 InitListExpr *ILE, bool &RequiresSecondPass);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000224public:
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000225 InitListChecker(Sema &S, const InitializedEntity &Entity,
226 InitListExpr *IL, QualType &T);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000227 bool HadError() { return hadError; }
228
229 // @brief Retrieves the fully-structured initializer list used for
230 // semantic analysis and code generation.
231 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
232};
Chris Lattner8b419b92009-02-24 22:48:58 +0000233} // end anonymous namespace
Chris Lattner68355a52009-01-29 05:10:57 +0000234
Douglas Gregord6d37de2009-12-22 00:05:34 +0000235void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
236 const InitializedEntity &ParentEntity,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000237 InitListExpr *ILE,
Douglas Gregord6d37de2009-12-22 00:05:34 +0000238 bool &RequiresSecondPass) {
239 SourceLocation Loc = ILE->getSourceRange().getBegin();
240 unsigned NumInits = ILE->getNumInits();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000241 InitializedEntity MemberEntity
Douglas Gregord6d37de2009-12-22 00:05:34 +0000242 = InitializedEntity::InitializeMember(Field, &ParentEntity);
243 if (Init >= NumInits || !ILE->getInit(Init)) {
244 // FIXME: We probably don't need to handle references
245 // specially here, since value-initialization of references is
246 // handled in InitializationSequence.
247 if (Field->getType()->isReferenceType()) {
248 // C++ [dcl.init.aggr]p9:
249 // If an incomplete or empty initializer-list leaves a
250 // member of reference type uninitialized, the program is
251 // ill-formed.
252 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
253 << Field->getType()
254 << ILE->getSyntacticForm()->getSourceRange();
255 SemaRef.Diag(Field->getLocation(),
256 diag::note_uninit_reference_member);
257 hadError = true;
258 return;
259 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000260
Douglas Gregord6d37de2009-12-22 00:05:34 +0000261 InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
262 true);
263 InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
264 if (!InitSeq) {
265 InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
266 hadError = true;
267 return;
268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000269
John McCall60d7b3a2010-08-24 06:29:42 +0000270 ExprResult MemberInit
John McCallf312b1e2010-08-26 23:41:50 +0000271 = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg());
Douglas Gregord6d37de2009-12-22 00:05:34 +0000272 if (MemberInit.isInvalid()) {
273 hadError = true;
274 return;
275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000276
Douglas Gregord6d37de2009-12-22 00:05:34 +0000277 if (hadError) {
278 // Do nothing
279 } else if (Init < NumInits) {
280 ILE->setInit(Init, MemberInit.takeAs<Expr>());
281 } else if (InitSeq.getKind()
282 == InitializationSequence::ConstructorInitialization) {
283 // Value-initialization requires a constructor call, so
284 // extend the initializer list to include the constructor
285 // call and make a note that we'll need to take another pass
286 // through the initializer list.
Ted Kremenek709210f2010-04-13 23:39:13 +0000287 ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>());
Douglas Gregord6d37de2009-12-22 00:05:34 +0000288 RequiresSecondPass = true;
289 }
290 } else if (InitListExpr *InnerILE
291 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000292 FillInValueInitializations(MemberEntity, InnerILE,
293 RequiresSecondPass);
Douglas Gregord6d37de2009-12-22 00:05:34 +0000294}
295
Douglas Gregor4c678342009-01-28 21:54:33 +0000296/// Recursively replaces NULL values within the given initializer list
297/// with expressions that perform value-initialization of the
298/// appropriate type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000299void
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000300InitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
301 InitListExpr *ILE,
302 bool &RequiresSecondPass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000303 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregor930d8b52009-01-30 22:09:00 +0000304 "Should not have void type");
Douglas Gregor87fd7032009-02-02 17:43:21 +0000305 SourceLocation Loc = ILE->getSourceRange().getBegin();
306 if (ILE->getSyntacticForm())
307 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek6217b802009-07-29 21:53:49 +0000309 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregord6d37de2009-12-22 00:05:34 +0000310 if (RType->getDecl()->isUnion() &&
311 ILE->getInitializedFieldInUnion())
312 FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
313 Entity, ILE, RequiresSecondPass);
314 else {
315 unsigned Init = 0;
316 for (RecordDecl::field_iterator
317 Field = RType->getDecl()->field_begin(),
318 FieldEnd = RType->getDecl()->field_end();
319 Field != FieldEnd; ++Field) {
320 if (Field->isUnnamedBitfield())
321 continue;
Douglas Gregor4c678342009-01-28 21:54:33 +0000322
Douglas Gregord6d37de2009-12-22 00:05:34 +0000323 if (hadError)
Douglas Gregor87fd7032009-02-02 17:43:21 +0000324 return;
Douglas Gregord6d37de2009-12-22 00:05:34 +0000325
326 FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
327 if (hadError)
Douglas Gregor87fd7032009-02-02 17:43:21 +0000328 return;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000329
Douglas Gregord6d37de2009-12-22 00:05:34 +0000330 ++Init;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000331
Douglas Gregord6d37de2009-12-22 00:05:34 +0000332 // Only look at the first initialization of a union.
333 if (RType->getDecl()->isUnion())
334 break;
335 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000336 }
337
338 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000339 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000340
341 QualType ElementType;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000343 InitializedEntity ElementEntity = Entity;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000344 unsigned NumInits = ILE->getNumInits();
345 unsigned NumElements = NumInits;
Chris Lattner08202542009-02-24 22:50:46 +0000346 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000347 ElementType = AType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000348 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
349 NumElements = CAType->getSize().getZExtValue();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000350 ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000351 0, Entity);
John McCall183700f2009-09-21 23:43:11 +0000352 } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000353 ElementType = VType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000354 NumElements = VType->getNumElements();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000355 ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000356 0, Entity);
Mike Stump1eb44332009-09-09 15:08:12 +0000357 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000358 ElementType = ILE->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000360
Douglas Gregor87fd7032009-02-02 17:43:21 +0000361 for (unsigned Init = 0; Init != NumElements; ++Init) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000362 if (hadError)
363 return;
364
Anders Carlssond3d824d2010-01-23 04:34:47 +0000365 if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
366 ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000367 ElementEntity.setElementIndex(Init);
368
Douglas Gregor87fd7032009-02-02 17:43:21 +0000369 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000370 InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
371 true);
372 InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
373 if (!InitSeq) {
374 InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000375 hadError = true;
376 return;
377 }
378
John McCall60d7b3a2010-08-24 06:29:42 +0000379 ExprResult ElementInit
John McCallf312b1e2010-08-26 23:41:50 +0000380 = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg());
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000381 if (ElementInit.isInvalid()) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000382 hadError = true;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000383 return;
384 }
385
386 if (hadError) {
387 // Do nothing
388 } else if (Init < NumInits) {
389 ILE->setInit(Init, ElementInit.takeAs<Expr>());
390 } else if (InitSeq.getKind()
391 == InitializationSequence::ConstructorInitialization) {
392 // Value-initialization requires a constructor call, so
393 // extend the initializer list to include the constructor
394 // call and make a note that we'll need to take another pass
395 // through the initializer list.
Ted Kremenek709210f2010-04-13 23:39:13 +0000396 ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>());
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000397 RequiresSecondPass = true;
398 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000399 } else if (InitListExpr *InnerILE
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000400 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
401 FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
Douglas Gregor4c678342009-01-28 21:54:33 +0000402 }
403}
404
Chris Lattner68355a52009-01-29 05:10:57 +0000405
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000406InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
407 InitListExpr *IL, QualType &T)
Chris Lattner08202542009-02-24 22:50:46 +0000408 : SemaRef(S) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000409 hadError = false;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000410
Eli Friedmanb85f7072008-05-19 19:16:24 +0000411 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000412 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000413 FullyStructuredList
Douglas Gregored8a93d2009-03-01 17:12:46 +0000414 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000415 CheckExplicitInitList(Entity, IL, T, newIndex,
Anders Carlsson46f46592010-01-23 19:55:29 +0000416 FullyStructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000417 /*TopLevelObject=*/true);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000418
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000419 if (!hadError) {
420 bool RequiresSecondPass = false;
421 FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
Douglas Gregor16006c92009-12-16 18:50:27 +0000422 if (RequiresSecondPass && !hadError)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000423 FillInValueInitializations(Entity, FullyStructuredList,
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000424 RequiresSecondPass);
425 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000426}
427
428int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +0000429 // FIXME: use a proper constant
430 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000431 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000432 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000433 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
434 }
435 return maxElements;
436}
437
438int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000439 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor4c678342009-01-28 21:54:33 +0000440 int InitializableMembers = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000441 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000442 Field = structDecl->field_begin(),
443 FieldEnd = structDecl->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000444 Field != FieldEnd; ++Field) {
445 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
446 ++InitializableMembers;
447 }
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000448 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +0000449 return std::min(InitializableMembers, 1);
450 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-05-01 22:18:59 +0000451}
452
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000453void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000454 InitListExpr *ParentIList,
Douglas Gregor4c678342009-01-28 21:54:33 +0000455 QualType T, unsigned &Index,
456 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000457 unsigned &StructuredIndex,
458 bool TopLevelObject) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000459 int maxElements = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Steve Naroff0cca7492008-05-01 22:18:59 +0000461 if (T->isArrayType())
462 maxElements = numArrayElements(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +0000463 else if (T->isRecordType())
Steve Naroff0cca7492008-05-01 22:18:59 +0000464 maxElements = numStructUnionElements(T);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000465 else if (T->isVectorType())
John McCall183700f2009-09-21 23:43:11 +0000466 maxElements = T->getAs<VectorType>()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +0000467 else
468 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +0000469
Eli Friedman402256f2008-05-25 13:49:22 +0000470 if (maxElements == 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000471 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedman402256f2008-05-25 13:49:22 +0000472 diag::err_implicit_empty_initializer);
Douglas Gregor4c678342009-01-28 21:54:33 +0000473 ++Index;
Eli Friedman402256f2008-05-25 13:49:22 +0000474 hadError = true;
475 return;
476 }
477
Douglas Gregor4c678342009-01-28 21:54:33 +0000478 // Build a structured initializer list corresponding to this subobject.
479 InitListExpr *StructuredSubobjectInitList
Mike Stump1eb44332009-09-09 15:08:12 +0000480 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
481 StructuredIndex,
Douglas Gregored8a93d2009-03-01 17:12:46 +0000482 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
483 ParentIList->getSourceRange().getEnd()));
Douglas Gregor4c678342009-01-28 21:54:33 +0000484 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000485
Douglas Gregor4c678342009-01-28 21:54:33 +0000486 // Check the element types and build the structural subobject.
Douglas Gregor87fd7032009-02-02 17:43:21 +0000487 unsigned StartIndex = Index;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000488 CheckListElementTypes(Entity, ParentIList, T,
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000489 /*SubobjectIsDesignatorContext=*/false, Index,
Mike Stump1eb44332009-09-09 15:08:12 +0000490 StructuredSubobjectInitList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000491 StructuredSubobjectInitIndex,
492 TopLevelObject);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000493 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregora6457962009-03-20 00:32:56 +0000494 StructuredSubobjectInitList->setType(T);
495
Douglas Gregored8a93d2009-03-01 17:12:46 +0000496 // Update the structured sub-object initializer so that it's ending
Douglas Gregor87fd7032009-02-02 17:43:21 +0000497 // range corresponds with the end of the last initializer it used.
498 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000499 SourceLocation EndLoc
Douglas Gregor87fd7032009-02-02 17:43:21 +0000500 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
501 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
502 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000503
Tanya Lattner1e1d3962010-03-07 04:17:15 +0000504 // Warn about missing braces.
505 if (T->isArrayType() || T->isRecordType()) {
Tanya Lattner47f164e2010-03-07 04:40:06 +0000506 SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
507 diag::warn_missing_braces)
Tanya Lattner1e1d3962010-03-07 04:17:15 +0000508 << StructuredSubobjectInitList->getSourceRange()
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000509 << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(),
Douglas Gregor849b2432010-03-31 17:46:05 +0000510 "{")
511 << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000512 StructuredSubobjectInitList->getLocEnd()),
Douglas Gregor849b2432010-03-31 17:46:05 +0000513 "}");
Tanya Lattner1e1d3962010-03-07 04:17:15 +0000514 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000515}
516
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000517void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000518 InitListExpr *IList, QualType &T,
Douglas Gregor4c678342009-01-28 21:54:33 +0000519 unsigned &Index,
520 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000521 unsigned &StructuredIndex,
522 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000523 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor4c678342009-01-28 21:54:33 +0000524 SyntacticToSemantic[IList] = StructuredList;
525 StructuredList->setSyntacticForm(IList);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000526 CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true,
Anders Carlsson46f46592010-01-23 19:55:29 +0000527 Index, StructuredList, StructuredIndex, TopLevelObject);
Douglas Gregor63982352010-07-13 18:40:04 +0000528 QualType ExprTy = T.getNonLValueExprType(SemaRef.Context);
529 IList->setType(ExprTy);
530 StructuredList->setType(ExprTy);
Eli Friedman638e1442008-05-25 13:22:35 +0000531 if (hadError)
532 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000533
Eli Friedman638e1442008-05-25 13:22:35 +0000534 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000535 // We have leftover initializers
Eli Friedmane5408582009-05-29 20:20:05 +0000536 if (StructuredIndex == 1 &&
537 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000538 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000539 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000540 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000541 hadError = true;
542 }
Eli Friedmanbb504d32008-05-19 20:12:18 +0000543 // Special-case
Chris Lattner08202542009-02-24 22:50:46 +0000544 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000545 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8dc2102008-05-20 05:25:56 +0000546 } else if (!T->isIncompleteType()) {
Douglas Gregorb574e562009-01-30 22:26:29 +0000547 // Don't complain for incomplete types, since we'll get an error
548 // elsewhere
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000549 QualType CurrentObjectType = StructuredList->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000550 int initKind =
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000551 CurrentObjectType->isArrayType()? 0 :
552 CurrentObjectType->isVectorType()? 1 :
553 CurrentObjectType->isScalarType()? 2 :
554 CurrentObjectType->isUnionType()? 3 :
555 4;
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000556
557 unsigned DK = diag::warn_excess_initializers;
Eli Friedmane5408582009-05-29 20:20:05 +0000558 if (SemaRef.getLangOptions().CPlusPlus) {
559 DK = diag::err_excess_initializers;
560 hadError = true;
561 }
Nate Begeman08634522009-07-07 21:53:06 +0000562 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
563 DK = diag::err_excess_initializers;
564 hadError = true;
565 }
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000566
Chris Lattner08202542009-02-24 22:50:46 +0000567 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000568 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000569 }
570 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000571
Eli Friedman759f2522009-05-16 11:45:48 +0000572 if (T->isScalarType() && !TopLevelObject)
Chris Lattner08202542009-02-24 22:50:46 +0000573 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregora3a83512009-04-01 23:51:29 +0000574 << IList->getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000575 << FixItHint::CreateRemoval(IList->getLocStart())
576 << FixItHint::CreateRemoval(IList->getLocEnd());
Steve Naroff0cca7492008-05-01 22:18:59 +0000577}
578
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000579void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000580 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000581 QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000582 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000583 unsigned &Index,
584 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000585 unsigned &StructuredIndex,
586 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000587 if (DeclType->isScalarType()) {
Anders Carlsson46f46592010-01-23 19:55:29 +0000588 CheckScalarType(Entity, IList, DeclType, Index,
589 StructuredList, StructuredIndex);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000590 } else if (DeclType->isVectorType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000591 CheckVectorType(Entity, IList, DeclType, Index,
Anders Carlsson46f46592010-01-23 19:55:29 +0000592 StructuredList, StructuredIndex);
Douglas Gregord7eb8462009-01-30 17:31:00 +0000593 } else if (DeclType->isAggregateType()) {
594 if (DeclType->isRecordType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000595 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Anders Carlsson2bbae5d2010-01-23 20:20:40 +0000596 CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000597 SubobjectIsDesignatorContext, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000598 StructuredList, StructuredIndex,
599 TopLevelObject);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000600 } else if (DeclType->isArrayType()) {
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000601 llvm::APSInt Zero(
Chris Lattner08202542009-02-24 22:50:46 +0000602 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000603 false);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000604 CheckArrayType(Entity, IList, DeclType, Zero,
Anders Carlsson784f6992010-01-23 20:13:41 +0000605 SubobjectIsDesignatorContext, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000606 StructuredList, StructuredIndex);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000607 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000608 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000609 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
610 // This type is invalid, issue a diagnostic.
Douglas Gregor4c678342009-01-28 21:54:33 +0000611 ++Index;
Chris Lattner08202542009-02-24 22:50:46 +0000612 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000613 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000614 hadError = true;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000615 } else if (DeclType->isRecordType()) {
616 // C++ [dcl.init]p14:
617 // [...] If the class is an aggregate (8.5.1), and the initializer
618 // is a brace-enclosed list, see 8.5.1.
619 //
620 // Note: 8.5.1 is handled below; here, we diagnose the case where
621 // we have an initializer list and a destination type that is not
622 // an aggregate.
623 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner08202542009-02-24 22:50:46 +0000624 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000625 << DeclType << IList->getSourceRange();
626 hadError = true;
627 } else if (DeclType->isReferenceType()) {
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000628 CheckReferenceType(Entity, IList, DeclType, Index,
629 StructuredList, StructuredIndex);
John McCallc12c5bb2010-05-15 11:32:37 +0000630 } else if (DeclType->isObjCObjectType()) {
Douglas Gregor4d9e7382010-05-03 18:24:37 +0000631 SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
632 << DeclType;
633 hadError = true;
Steve Naroff0cca7492008-05-01 22:18:59 +0000634 } else {
Douglas Gregor4d9e7382010-05-03 18:24:37 +0000635 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
636 << DeclType;
637 hadError = true;
Steve Naroff0cca7492008-05-01 22:18:59 +0000638 }
639}
640
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000641void InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000642 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000643 QualType ElemType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000644 unsigned &Index,
645 InitListExpr *StructuredList,
646 unsigned &StructuredIndex) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000647 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000648 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
649 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000650 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000651 InitListExpr *newStructuredList
Douglas Gregor4c678342009-01-28 21:54:33 +0000652 = getStructuredSubobjectInit(IList, Index, ElemType,
653 StructuredList, StructuredIndex,
654 SubInitList->getSourceRange());
Anders Carlsson46f46592010-01-23 19:55:29 +0000655 CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +0000656 newStructuredList, newStructuredIndex);
657 ++StructuredIndex;
658 ++Index;
John McCallfef8b342011-02-21 07:57:55 +0000659 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000660 } else if (ElemType->isScalarType()) {
John McCallfef8b342011-02-21 07:57:55 +0000661 return CheckScalarType(Entity, IList, ElemType, Index,
662 StructuredList, StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000663 } else if (ElemType->isReferenceType()) {
John McCallfef8b342011-02-21 07:57:55 +0000664 return CheckReferenceType(Entity, IList, ElemType, Index,
665 StructuredList, StructuredIndex);
666 }
Anders Carlssond28b4282009-08-27 17:18:13 +0000667
John McCallfef8b342011-02-21 07:57:55 +0000668 if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) {
669 // arrayType can be incomplete if we're initializing a flexible
670 // array member. There's nothing we can do with the completed
671 // type here, though.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000672
John McCallfef8b342011-02-21 07:57:55 +0000673 if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) {
674 CheckStringInit(Str, ElemType, arrayType, SemaRef);
675 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000676 ++Index;
John McCallfef8b342011-02-21 07:57:55 +0000677 return;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000678 }
John McCallfef8b342011-02-21 07:57:55 +0000679
680 // Fall through for subaggregate initialization.
681
682 } else if (SemaRef.getLangOptions().CPlusPlus) {
683 // C++ [dcl.init.aggr]p12:
684 // All implicit type conversions (clause 4) are considered when
685 // initializing the aggregate member with an ini- tializer from
686 // an initializer-list. If the initializer can initialize a
687 // member, the member is initialized. [...]
688
689 // FIXME: Better EqualLoc?
690 InitializationKind Kind =
691 InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation());
692 InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1);
693
694 if (Seq) {
695 ExprResult Result =
696 Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1));
697 if (Result.isInvalid())
698 hadError = true;
699
700 UpdateStructuredListElement(StructuredList, StructuredIndex,
701 Result.takeAs<Expr>());
702 ++Index;
703 return;
704 }
705
706 // Fall through for subaggregate initialization
707 } else {
708 // C99 6.7.8p13:
709 //
710 // The initializer for a structure or union object that has
711 // automatic storage duration shall be either an initializer
712 // list as described below, or a single expression that has
713 // compatible structure or union type. In the latter case, the
714 // initial value of the object, including unnamed members, is
715 // that of the expression.
716 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
717 SemaRef.CheckSingleAssignmentConstraints(ElemType, expr)
718 == Sema::Compatible) {
719 SemaRef.DefaultFunctionArrayLvalueConversion(expr);
720 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
721 ++Index;
722 return;
723 }
724
725 // Fall through for subaggregate initialization
726 }
727
728 // C++ [dcl.init.aggr]p12:
729 //
730 // [...] Otherwise, if the member is itself a non-empty
731 // subaggregate, brace elision is assumed and the initializer is
732 // considered for the initialization of the first member of
733 // the subaggregate.
734 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
735 CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
736 StructuredIndex);
737 ++StructuredIndex;
738 } else {
739 // We cannot initialize this element, so let
740 // PerformCopyInitialization produce the appropriate diagnostic.
741 SemaRef.PerformCopyInitialization(Entity, SourceLocation(),
742 SemaRef.Owned(expr));
743 hadError = true;
744 ++Index;
745 ++StructuredIndex;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000746 }
Eli Friedmanb85f7072008-05-19 19:16:24 +0000747}
748
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000749void InitListChecker::CheckScalarType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000750 InitListExpr *IList, QualType DeclType,
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000751 unsigned &Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000752 InitListExpr *StructuredList,
753 unsigned &StructuredIndex) {
John McCallb934c2d2010-11-11 00:46:36 +0000754 if (Index >= IList->getNumInits()) {
Chris Lattner08202542009-02-24 22:50:46 +0000755 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000756 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000757 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000758 ++Index;
759 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000760 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000761 }
John McCallb934c2d2010-11-11 00:46:36 +0000762
763 Expr *expr = IList->getInit(Index);
764 if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) {
765 SemaRef.Diag(SubIList->getLocStart(),
766 diag::warn_many_braces_around_scalar_init)
767 << SubIList->getSourceRange();
768
769 CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList,
770 StructuredIndex);
771 return;
772 } else if (isa<DesignatedInitExpr>(expr)) {
773 SemaRef.Diag(expr->getSourceRange().getBegin(),
774 diag::err_designator_for_scalar_init)
775 << DeclType << expr->getSourceRange();
776 hadError = true;
777 ++Index;
778 ++StructuredIndex;
779 return;
780 }
781
782 ExprResult Result =
783 SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
784 SemaRef.Owned(expr));
785
786 Expr *ResultExpr = 0;
787
788 if (Result.isInvalid())
789 hadError = true; // types weren't compatible.
790 else {
791 ResultExpr = Result.takeAs<Expr>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792
John McCallb934c2d2010-11-11 00:46:36 +0000793 if (ResultExpr != expr) {
794 // The type was promoted, update initializer list.
795 IList->setInit(Index, ResultExpr);
796 }
797 }
798 if (hadError)
799 ++StructuredIndex;
800 else
801 UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
802 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000803}
804
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000805void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
806 InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000807 unsigned &Index,
808 InitListExpr *StructuredList,
809 unsigned &StructuredIndex) {
810 if (Index < IList->getNumInits()) {
811 Expr *expr = IList->getInit(Index);
812 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000813 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000814 << DeclType << IList->getSourceRange();
815 hadError = true;
816 ++Index;
817 ++StructuredIndex;
818 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000819 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000820
John McCall60d7b3a2010-08-24 06:29:42 +0000821 ExprResult Result =
Anders Carlssona6fe0bf2010-01-29 02:47:33 +0000822 SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
823 SemaRef.Owned(expr));
824
825 if (Result.isInvalid())
Douglas Gregor930d8b52009-01-30 22:09:00 +0000826 hadError = true;
Anders Carlssona6fe0bf2010-01-29 02:47:33 +0000827
828 expr = Result.takeAs<Expr>();
829 IList->setInit(Index, expr);
830
Douglas Gregor930d8b52009-01-30 22:09:00 +0000831 if (hadError)
832 ++StructuredIndex;
833 else
834 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
835 ++Index;
836 } else {
Mike Stump390b4cc2009-05-16 07:39:55 +0000837 // FIXME: It would be wonderful if we could point at the actual member. In
838 // general, it would be useful to pass location information down the stack,
839 // so that we know the location (or decl) of the "current object" being
840 // initialized.
Mike Stump1eb44332009-09-09 15:08:12 +0000841 SemaRef.Diag(IList->getLocStart(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000842 diag::err_init_reference_member_uninitialized)
843 << DeclType
844 << IList->getSourceRange();
845 hadError = true;
846 ++Index;
847 ++StructuredIndex;
848 return;
849 }
850}
851
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000852void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000853 InitListExpr *IList, QualType DeclType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000854 unsigned &Index,
855 InitListExpr *StructuredList,
856 unsigned &StructuredIndex) {
John McCall20e047a2010-10-30 00:11:39 +0000857 if (Index >= IList->getNumInits())
858 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
John McCall20e047a2010-10-30 00:11:39 +0000860 const VectorType *VT = DeclType->getAs<VectorType>();
861 unsigned maxElements = VT->getNumElements();
862 unsigned numEltsInit = 0;
863 QualType elementType = VT->getElementType();
Anders Carlsson46f46592010-01-23 19:55:29 +0000864
John McCall20e047a2010-10-30 00:11:39 +0000865 if (!SemaRef.getLangOptions().OpenCL) {
866 // If the initializing element is a vector, try to copy-initialize
867 // instead of breaking it apart (which is doomed to failure anyway).
868 Expr *Init = IList->getInit(Index);
869 if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) {
870 ExprResult Result =
871 SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(),
872 SemaRef.Owned(Init));
873
874 Expr *ResultExpr = 0;
875 if (Result.isInvalid())
876 hadError = true; // types weren't compatible.
877 else {
878 ResultExpr = Result.takeAs<Expr>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879
John McCall20e047a2010-10-30 00:11:39 +0000880 if (ResultExpr != Init) {
881 // The type was promoted, update initializer list.
882 IList->setInit(Index, ResultExpr);
Nate Begeman2ef13e52009-08-10 23:49:36 +0000883 }
884 }
John McCall20e047a2010-10-30 00:11:39 +0000885 if (hadError)
886 ++StructuredIndex;
887 else
888 UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
889 ++Index;
890 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
John McCall20e047a2010-10-30 00:11:39 +0000893 InitializedEntity ElementEntity =
894 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895
John McCall20e047a2010-10-30 00:11:39 +0000896 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
897 // Don't attempt to go past the end of the init list
898 if (Index >= IList->getNumInits())
899 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000900
John McCall20e047a2010-10-30 00:11:39 +0000901 ElementEntity.setElementIndex(Index);
902 CheckSubElementType(ElementEntity, IList, elementType, Index,
903 StructuredList, StructuredIndex);
904 }
905 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000906 }
John McCall20e047a2010-10-30 00:11:39 +0000907
908 InitializedEntity ElementEntity =
909 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000910
John McCall20e047a2010-10-30 00:11:39 +0000911 // OpenCL initializers allows vectors to be constructed from vectors.
912 for (unsigned i = 0; i < maxElements; ++i) {
913 // Don't attempt to go past the end of the init list
914 if (Index >= IList->getNumInits())
915 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000916
John McCall20e047a2010-10-30 00:11:39 +0000917 ElementEntity.setElementIndex(Index);
918
919 QualType IType = IList->getInit(Index)->getType();
920 if (!IType->isVectorType()) {
921 CheckSubElementType(ElementEntity, IList, elementType, Index,
922 StructuredList, StructuredIndex);
923 ++numEltsInit;
924 } else {
925 QualType VecType;
926 const VectorType *IVT = IType->getAs<VectorType>();
927 unsigned numIElts = IVT->getNumElements();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000928
John McCall20e047a2010-10-30 00:11:39 +0000929 if (IType->isExtVectorType())
930 VecType = SemaRef.Context.getExtVectorType(elementType, numIElts);
931 else
932 VecType = SemaRef.Context.getVectorType(elementType, numIElts,
Bob Wilsone86d78c2010-11-10 21:56:12 +0000933 IVT->getVectorKind());
John McCall20e047a2010-10-30 00:11:39 +0000934 CheckSubElementType(ElementEntity, IList, VecType, Index,
935 StructuredList, StructuredIndex);
936 numEltsInit += numIElts;
937 }
938 }
939
940 // OpenCL requires all elements to be initialized.
941 if (numEltsInit != maxElements)
942 if (SemaRef.getLangOptions().OpenCL)
943 SemaRef.Diag(IList->getSourceRange().getBegin(),
944 diag::err_vector_incorrect_num_initializers)
945 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Naroff0cca7492008-05-01 22:18:59 +0000946}
947
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000948void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
Anders Carlsson784f6992010-01-23 20:13:41 +0000949 InitListExpr *IList, QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000950 llvm::APSInt elementIndex,
Mike Stump1eb44332009-09-09 15:08:12 +0000951 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000952 unsigned &Index,
953 InitListExpr *StructuredList,
954 unsigned &StructuredIndex) {
John McCallce6c9b72011-02-21 07:22:22 +0000955 const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType);
956
Steve Naroff0cca7492008-05-01 22:18:59 +0000957 // Check for the special-case of initializing an array with a string.
958 if (Index < IList->getNumInits()) {
John McCallce6c9b72011-02-21 07:22:22 +0000959 if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType,
Chris Lattner79e079d2009-02-24 23:10:27 +0000960 SemaRef.Context)) {
John McCallfef8b342011-02-21 07:57:55 +0000961 CheckStringInit(Str, DeclType, arrayType, SemaRef);
Douglas Gregor4c678342009-01-28 21:54:33 +0000962 // We place the string literal directly into the resulting
963 // initializer list. This is the only place where the structure
964 // of the structured initializer list doesn't match exactly,
965 // because doing so would involve allocating one character
966 // constant for each string.
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000967 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner08202542009-02-24 22:50:46 +0000968 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000969 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000970 return;
971 }
972 }
John McCallce6c9b72011-02-21 07:22:22 +0000973 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) {
Eli Friedman638e1442008-05-25 13:22:35 +0000974 // Check for VLAs; in standard C it would be possible to check this
975 // earlier, but I don't know where clang accepts VLAs (gcc accepts
976 // them in all sorts of strange places).
Chris Lattner08202542009-02-24 22:50:46 +0000977 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000978 diag::err_variable_object_no_init)
979 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000980 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000981 ++Index;
982 ++StructuredIndex;
Eli Friedman638e1442008-05-25 13:22:35 +0000983 return;
984 }
985
Douglas Gregor05c13a32009-01-22 00:58:24 +0000986 // We might know the maximum number of elements in advance.
Douglas Gregor4c678342009-01-28 21:54:33 +0000987 llvm::APSInt maxElements(elementIndex.getBitWidth(),
988 elementIndex.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000989 bool maxElementsKnown = false;
John McCallce6c9b72011-02-21 07:22:22 +0000990 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000991 maxElements = CAT->getSize();
Jay Foad9f71a8f2010-12-07 08:25:34 +0000992 elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000993 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000994 maxElementsKnown = true;
995 }
996
John McCallce6c9b72011-02-21 07:22:22 +0000997 QualType elementType = arrayType->getElementType();
Douglas Gregor05c13a32009-01-22 00:58:24 +0000998 while (Index < IList->getNumInits()) {
999 Expr *Init = IList->getInit(Index);
1000 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001001 // If we're not the subobject that matches up with the '{' for
1002 // the designator, we shouldn't be handling the
1003 // designator. Return immediately.
1004 if (!SubobjectIsDesignatorContext)
1005 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001006
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001007 // Handle this designated initializer. elementIndex will be
1008 // updated to be the next array element we'll initialize.
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001009 if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +00001010 DeclType, 0, &elementIndex, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001011 StructuredList, StructuredIndex, true,
1012 false)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001013 hadError = true;
1014 continue;
1015 }
1016
Douglas Gregorf6c717c2009-01-23 16:54:12 +00001017 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +00001018 maxElements = maxElements.extend(elementIndex.getBitWidth());
Douglas Gregorf6c717c2009-01-23 16:54:12 +00001019 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +00001020 elementIndex = elementIndex.extend(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +00001021 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorf6c717c2009-01-23 16:54:12 +00001022
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001023 // If the array is of incomplete type, keep track of the number of
1024 // elements in the initializer.
1025 if (!maxElementsKnown && elementIndex > maxElements)
1026 maxElements = elementIndex;
1027
Douglas Gregor05c13a32009-01-22 00:58:24 +00001028 continue;
1029 }
1030
1031 // If we know the maximum number of elements, and we've already
1032 // hit it, stop consuming elements in the initializer list.
1033 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroff0cca7492008-05-01 22:18:59 +00001034 break;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001035
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001036 InitializedEntity ElementEntity =
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001037 InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex,
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001038 Entity);
1039 // Check this element.
1040 CheckSubElementType(ElementEntity, IList, elementType, Index,
1041 StructuredList, StructuredIndex);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001042 ++elementIndex;
1043
1044 // If the array is of incomplete type, keep track of the number of
1045 // elements in the initializer.
1046 if (!maxElementsKnown && elementIndex > maxElements)
1047 maxElements = elementIndex;
Steve Naroff0cca7492008-05-01 22:18:59 +00001048 }
Eli Friedman587cbdf2009-05-29 20:17:55 +00001049 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +00001050 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001051 // be calculated here.
Douglas Gregore3fa2de2009-01-23 18:58:42 +00001052 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001053 if (maxElements == Zero) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001054 // Sizing an array implicitly to zero is not allowed by ISO C,
1055 // but is supported by GNU.
Chris Lattner08202542009-02-24 22:50:46 +00001056 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001057 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +00001058 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001059
Mike Stump1eb44332009-09-09 15:08:12 +00001060 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001061 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +00001062 }
1063}
1064
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001065void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
Anders Carlsson2bbae5d2010-01-23 20:20:40 +00001066 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001067 QualType DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001068 RecordDecl::field_iterator Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001069 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +00001070 unsigned &Index,
1071 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001072 unsigned &StructuredIndex,
1073 bool TopLevelObject) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001074 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Eli Friedmanb85f7072008-05-19 19:16:24 +00001076 // If the record is invalid, some of it's members are invalid. To avoid
1077 // confusion, we forgo checking the intializer for the entire record.
1078 if (structDecl->isInvalidDecl()) {
1079 hadError = true;
1080 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001081 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001082
1083 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1084 // Value-initialize the first named member of the union.
Ted Kremenek6217b802009-07-29 21:53:49 +00001085 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001086 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001087 Field != FieldEnd; ++Field) {
1088 if (Field->getDeclName()) {
1089 StructuredList->setInitializedFieldInUnion(*Field);
1090 break;
1091 }
1092 }
1093 return;
1094 }
1095
Douglas Gregor05c13a32009-01-22 00:58:24 +00001096 // If structDecl is a forward declaration, this loop won't do
1097 // anything except look at designated initializers; That's okay,
1098 // because an error should get printed out elsewhere. It might be
1099 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenek6217b802009-07-29 21:53:49 +00001100 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001101 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregordfb5e592009-02-12 19:00:39 +00001102 bool InitializedSomething = false;
John McCall80639de2010-03-11 19:32:38 +00001103 bool CheckForMissingFields = true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001104 while (Index < IList->getNumInits()) {
1105 Expr *Init = IList->getInit(Index);
1106
1107 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001108 // If we're not the subobject that matches up with the '{' for
1109 // the designator, we shouldn't be handling the
1110 // designator. Return immediately.
1111 if (!SubobjectIsDesignatorContext)
1112 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001113
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001114 // Handle this designated initializer. Field will be updated to
1115 // the next field that we'll be initializing.
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001116 if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +00001117 DeclType, &Field, 0, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001118 StructuredList, StructuredIndex,
1119 true, TopLevelObject))
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001120 hadError = true;
1121
Douglas Gregordfb5e592009-02-12 19:00:39 +00001122 InitializedSomething = true;
John McCall80639de2010-03-11 19:32:38 +00001123
1124 // Disable check for missing fields when designators are used.
1125 // This matches gcc behaviour.
1126 CheckForMissingFields = false;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001127 continue;
1128 }
1129
1130 if (Field == FieldEnd) {
1131 // We've run out of fields. We're done.
1132 break;
1133 }
1134
Douglas Gregordfb5e592009-02-12 19:00:39 +00001135 // We've already initialized a member of a union. We're done.
1136 if (InitializedSomething && DeclType->isUnionType())
1137 break;
1138
Douglas Gregor44b43212008-12-11 16:49:14 +00001139 // If we've hit the flexible array member at the end, we're done.
1140 if (Field->getType()->isIncompleteArrayType())
1141 break;
1142
Douglas Gregor0bb76892009-01-29 16:53:55 +00001143 if (Field->isUnnamedBitfield()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001144 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregor05c13a32009-01-22 00:58:24 +00001145 ++Field;
Eli Friedmanb85f7072008-05-19 19:16:24 +00001146 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +00001147 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001148
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001149 InitializedEntity MemberEntity =
1150 InitializedEntity::InitializeMember(*Field, &Entity);
1151 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1152 StructuredList, StructuredIndex);
Douglas Gregordfb5e592009-02-12 19:00:39 +00001153 InitializedSomething = true;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001154
1155 if (DeclType->isUnionType()) {
1156 // Initialize the first field within the union.
1157 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001158 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001159
1160 ++Field;
Steve Naroff0cca7492008-05-01 22:18:59 +00001161 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001162
John McCall80639de2010-03-11 19:32:38 +00001163 // Emit warnings for missing struct field initializers.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001164 if (InitializedSomething && CheckForMissingFields && Field != FieldEnd &&
John McCall80639de2010-03-11 19:32:38 +00001165 !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) {
1166 // It is possible we have one or more unnamed bitfields remaining.
1167 // Find first (if any) named field and emit warning.
1168 for (RecordDecl::field_iterator it = Field, end = RD->field_end();
1169 it != end; ++it) {
1170 if (!it->isUnnamedBitfield()) {
1171 SemaRef.Diag(IList->getSourceRange().getEnd(),
1172 diag::warn_missing_field_initializers) << it->getName();
1173 break;
1174 }
1175 }
1176 }
1177
Mike Stump1eb44332009-09-09 15:08:12 +00001178 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregora6457962009-03-20 00:32:56 +00001179 Index >= IList->getNumInits())
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001180 return;
1181
1182 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001183 if (!TopLevelObject &&
Douglas Gregora6457962009-03-20 00:32:56 +00001184 (!isa<InitListExpr>(IList->getInit(Index)) ||
1185 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001186 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001187 diag::err_flexible_array_init_nonempty)
1188 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001189 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001190 << *Field;
1191 hadError = true;
Douglas Gregora6457962009-03-20 00:32:56 +00001192 ++Index;
1193 return;
1194 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001195 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregora6457962009-03-20 00:32:56 +00001196 diag::ext_flexible_array_init)
1197 << IList->getInit(Index)->getSourceRange().getBegin();
1198 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1199 << *Field;
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001200 }
1201
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001202 InitializedEntity MemberEntity =
1203 InitializedEntity::InitializeMember(*Field, &Entity);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001204
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001205 if (isa<InitListExpr>(IList->getInit(Index)))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001206 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001207 StructuredList, StructuredIndex);
1208 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001209 CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
Anders Carlsson987dc6a2010-01-23 20:47:59 +00001210 StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +00001211}
Steve Naroff0cca7492008-05-01 22:18:59 +00001212
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001213/// \brief Expand a field designator that refers to a member of an
1214/// anonymous struct or union into a series of field designators that
1215/// refers to the field within the appropriate subobject.
1216///
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001217static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump1eb44332009-09-09 15:08:12 +00001218 DesignatedInitExpr *DIE,
1219 unsigned DesigIdx,
Francois Picheta0e27f02010-12-22 03:46:10 +00001220 IndirectFieldDecl *IndirectField) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001221 typedef DesignatedInitExpr::Designator Designator;
1222
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001223 // Build the replacement designators.
1224 llvm::SmallVector<Designator, 4> Replacements;
Francois Picheta0e27f02010-12-22 03:46:10 +00001225 for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(),
1226 PE = IndirectField->chain_end(); PI != PE; ++PI) {
1227 if (PI + 1 == PE)
Mike Stump1eb44332009-09-09 15:08:12 +00001228 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001229 DIE->getDesignator(DesigIdx)->getDotLoc(),
1230 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1231 else
1232 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1233 SourceLocation()));
Francois Picheta0e27f02010-12-22 03:46:10 +00001234 assert(isa<FieldDecl>(*PI));
1235 Replacements.back().setField(cast<FieldDecl>(*PI));
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001236 }
1237
1238 // Expand the current designator into the set of replacement
1239 // designators, so we have a full subobject path down to where the
1240 // member of the anonymous struct/union is actually stored.
Douglas Gregor319d57f2010-01-06 23:17:19 +00001241 DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001242 &Replacements[0] + Replacements.size());
Francois Picheta0e27f02010-12-22 03:46:10 +00001243}
Mike Stump1eb44332009-09-09 15:08:12 +00001244
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001245/// \brief Given an implicit anonymous field, search the IndirectField that
Francois Picheta0e27f02010-12-22 03:46:10 +00001246/// corresponds to FieldName.
1247static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField,
1248 IdentifierInfo *FieldName) {
1249 assert(AnonField->isAnonymousStructOrUnion());
1250 Decl *NextDecl = AnonField->getNextDeclInContext();
1251 while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) {
1252 if (FieldName && FieldName == IF->getAnonField()->getIdentifier())
1253 return IF;
1254 NextDecl = NextDecl->getNextDeclInContext();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001255 }
Francois Picheta0e27f02010-12-22 03:46:10 +00001256 return 0;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001257}
1258
Douglas Gregor05c13a32009-01-22 00:58:24 +00001259/// @brief Check the well-formedness of a C99 designated initializer.
1260///
1261/// Determines whether the designated initializer @p DIE, which
1262/// resides at the given @p Index within the initializer list @p
1263/// IList, is well-formed for a current object of type @p DeclType
1264/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump1eb44332009-09-09 15:08:12 +00001265/// within the current subobject is returned in either
Douglas Gregor4c678342009-01-28 21:54:33 +00001266/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregor05c13a32009-01-22 00:58:24 +00001267///
1268/// @param IList The initializer list in which this designated
1269/// initializer occurs.
1270///
Douglas Gregor71199712009-04-15 04:56:10 +00001271/// @param DIE The designated initializer expression.
1272///
1273/// @param DesigIdx The index of the current designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001274///
1275/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1276/// into which the designation in @p DIE should refer.
1277///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001278/// @param NextField If non-NULL and the first designator in @p DIE is
1279/// a field, this will be set to the field declaration corresponding
1280/// to the field named by the designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001281///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001282/// @param NextElementIndex If non-NULL and the first designator in @p
1283/// DIE is an array designator or GNU array-range designator, this
1284/// will be set to the last index initialized by this designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001285///
1286/// @param Index Index into @p IList where the designated initializer
1287/// @p DIE occurs.
1288///
Douglas Gregor4c678342009-01-28 21:54:33 +00001289/// @param StructuredList The initializer list expression that
1290/// describes all of the subobject initializers in the order they'll
1291/// actually be initialized.
1292///
Douglas Gregor05c13a32009-01-22 00:58:24 +00001293/// @returns true if there was an error, false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001294bool
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001295InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001296 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001297 DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +00001298 unsigned DesigIdx,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001299 QualType &CurrentObjectType,
1300 RecordDecl::field_iterator *NextField,
1301 llvm::APSInt *NextElementIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001302 unsigned &Index,
1303 InitListExpr *StructuredList,
Douglas Gregor34e79462009-01-28 23:36:17 +00001304 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001305 bool FinishSubobjectInit,
1306 bool TopLevelObject) {
Douglas Gregor71199712009-04-15 04:56:10 +00001307 if (DesigIdx == DIE->size()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001308 // Check the actual initialization for the designated object type.
1309 bool prevHadError = hadError;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001310
1311 // Temporarily remove the designator expression from the
1312 // initializer list that the child calls see, so that we don't try
1313 // to re-process the designator.
1314 unsigned OldIndex = Index;
1315 IList->setInit(OldIndex, DIE->getInit());
1316
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001317 CheckSubElementType(Entity, IList, CurrentObjectType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001318 StructuredList, StructuredIndex);
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001319
1320 // Restore the designated initializer expression in the syntactic
1321 // form of the initializer list.
1322 if (IList->getInit(OldIndex) != DIE->getInit())
1323 DIE->setInit(IList->getInit(OldIndex));
1324 IList->setInit(OldIndex, DIE);
1325
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001326 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001327 }
1328
Douglas Gregor71199712009-04-15 04:56:10 +00001329 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001330 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor4c678342009-01-28 21:54:33 +00001331 "Need a non-designated initializer list to start from");
1332
Douglas Gregor71199712009-04-15 04:56:10 +00001333 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001334 // Determine the structural initializer list that corresponds to the
1335 // current subobject.
1336 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump1eb44332009-09-09 15:08:12 +00001337 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001338 StructuredList, StructuredIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001339 SourceRange(D->getStartLocation(),
1340 DIE->getSourceRange().getEnd()));
1341 assert(StructuredList && "Expected a structured initializer list");
1342
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001343 if (D->isFieldDesignator()) {
1344 // C99 6.7.8p7:
1345 //
1346 // If a designator has the form
1347 //
1348 // . identifier
1349 //
1350 // then the current object (defined below) shall have
1351 // structure or union type and the identifier shall be the
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // name of a member of that type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001353 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001354 if (!RT) {
1355 SourceLocation Loc = D->getDotLoc();
1356 if (Loc.isInvalid())
1357 Loc = D->getFieldLoc();
Chris Lattner08202542009-02-24 22:50:46 +00001358 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1359 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001360 ++Index;
1361 return true;
1362 }
1363
Douglas Gregor4c678342009-01-28 21:54:33 +00001364 // Note: we perform a linear search of the fields here, despite
1365 // the fact that we have a faster lookup method, because we always
1366 // need to compute the field's index.
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001367 FieldDecl *KnownField = D->getField();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001368 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor4c678342009-01-28 21:54:33 +00001369 unsigned FieldIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001370 RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001371 Field = RT->getDecl()->field_begin(),
1372 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +00001373 for (; Field != FieldEnd; ++Field) {
1374 if (Field->isUnnamedBitfield())
1375 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001376
Francois Picheta0e27f02010-12-22 03:46:10 +00001377 // If we find a field representing an anonymous field, look in the
1378 // IndirectFieldDecl that follow for the designated initializer.
1379 if (!KnownField && Field->isAnonymousStructOrUnion()) {
1380 if (IndirectFieldDecl *IF =
1381 FindIndirectFieldDesignator(*Field, FieldName)) {
1382 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF);
1383 D = DIE->getDesignator(DesigIdx);
1384 break;
1385 }
1386 }
Douglas Gregor022d13d2010-10-08 20:44:28 +00001387 if (KnownField && KnownField == *Field)
1388 break;
1389 if (FieldName && FieldName == Field->getIdentifier())
Douglas Gregor4c678342009-01-28 21:54:33 +00001390 break;
1391
1392 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001393 }
1394
Douglas Gregor4c678342009-01-28 21:54:33 +00001395 if (Field == FieldEnd) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001396 // There was no normal field in the struct with the designated
1397 // name. Perform another lookup for this name, which may find
1398 // something that we can't designate (e.g., a member function),
1399 // may find nothing, or may find a member of an anonymous
Mike Stump1eb44332009-09-09 15:08:12 +00001400 // struct/union.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001401 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001402 FieldDecl *ReplacementField = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001403 if (Lookup.first == Lookup.second) {
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001404 // Name lookup didn't find anything. Determine whether this
1405 // was a typo for another field name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001406 LookupResult R(SemaRef, FieldName, D->getFieldLoc(),
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001407 Sema::LookupMemberName);
Douglas Gregoraaf87162010-04-14 20:04:41 +00001408 if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl(), false,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001409 Sema::CTC_NoKeywords) &&
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001410 (ReplacementField = R.getAsSingle<FieldDecl>()) &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001411 ReplacementField->getDeclContext()->getRedeclContext()
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001412 ->Equals(RT->getDecl())) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001413 SemaRef.Diag(D->getFieldLoc(),
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001414 diag::err_field_designator_unknown_suggest)
1415 << FieldName << CurrentObjectType << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +00001416 << FixItHint::CreateReplacement(D->getFieldLoc(),
1417 R.getLookupName().getAsString());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001418 SemaRef.Diag(ReplacementField->getLocation(),
Douglas Gregor67dd1d42010-01-07 00:17:44 +00001419 diag::note_previous_decl)
1420 << ReplacementField->getDeclName();
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001421 } else {
1422 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1423 << FieldName << CurrentObjectType;
1424 ++Index;
1425 return true;
1426 }
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001427 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001428
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001429 if (!ReplacementField) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001430 // Name lookup found something, but it wasn't a field.
Chris Lattner08202542009-02-24 22:50:46 +00001431 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor4c678342009-01-28 21:54:33 +00001432 << FieldName;
Mike Stump1eb44332009-09-09 15:08:12 +00001433 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001434 diag::note_field_designator_found);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001435 ++Index;
1436 return true;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001437 }
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001438
Francois Picheta0e27f02010-12-22 03:46:10 +00001439 if (!KnownField) {
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001440 // The replacement field comes from typo correction; find it
1441 // in the list of fields.
1442 FieldIndex = 0;
1443 Field = RT->getDecl()->field_begin();
1444 for (; Field != FieldEnd; ++Field) {
1445 if (Field->isUnnamedBitfield())
1446 continue;
1447
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001448 if (ReplacementField == *Field ||
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001449 Field->getIdentifier() == ReplacementField->getIdentifier())
1450 break;
1451
1452 ++FieldIndex;
1453 }
1454 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001455 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001456
1457 // All of the fields of a union are located at the same place in
1458 // the initializer list.
Douglas Gregor0bb76892009-01-29 16:53:55 +00001459 if (RT->getDecl()->isUnion()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001460 FieldIndex = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001461 StructuredList->setInitializedFieldInUnion(*Field);
1462 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001463
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001464 // Update the designator with the field declaration.
Douglas Gregor4c678342009-01-28 21:54:33 +00001465 D->setField(*Field);
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Douglas Gregor4c678342009-01-28 21:54:33 +00001467 // Make sure that our non-designated initializer list has space
1468 // for a subobject corresponding to this field.
1469 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001470 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001471
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001472 // This designator names a flexible array member.
1473 if (Field->getType()->isIncompleteArrayType()) {
1474 bool Invalid = false;
Douglas Gregor71199712009-04-15 04:56:10 +00001475 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001476 // We can't designate an object within the flexible array
1477 // member (because GCC doesn't allow it).
Mike Stump1eb44332009-09-09 15:08:12 +00001478 DesignatedInitExpr::Designator *NextD
Douglas Gregor71199712009-04-15 04:56:10 +00001479 = DIE->getDesignator(DesigIdx + 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001480 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001481 diag::err_designator_into_flexible_array_member)
Mike Stump1eb44332009-09-09 15:08:12 +00001482 << SourceRange(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001483 DIE->getSourceRange().getEnd());
Chris Lattner08202542009-02-24 22:50:46 +00001484 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001485 << *Field;
1486 Invalid = true;
1487 }
1488
Chris Lattner9046c222010-10-10 17:49:49 +00001489 if (!hadError && !isa<InitListExpr>(DIE->getInit()) &&
1490 !isa<StringLiteral>(DIE->getInit())) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001491 // The initializer is not an initializer list.
Chris Lattner08202542009-02-24 22:50:46 +00001492 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001493 diag::err_flexible_array_init_needs_braces)
1494 << DIE->getInit()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001495 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001496 << *Field;
1497 Invalid = true;
1498 }
1499
1500 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001501 if (!Invalid && !TopLevelObject &&
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001502 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +00001503 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001504 diag::err_flexible_array_init_nonempty)
1505 << DIE->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001506 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001507 << *Field;
1508 Invalid = true;
1509 }
1510
1511 if (Invalid) {
1512 ++Index;
1513 return true;
1514 }
1515
1516 // Initialize the array.
1517 bool prevHadError = hadError;
1518 unsigned newStructuredIndex = FieldIndex;
1519 unsigned OldIndex = Index;
1520 IList->setInit(Index, DIE->getInit());
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001521
1522 InitializedEntity MemberEntity =
1523 InitializedEntity::InitializeMember(*Field, &Entity);
1524 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001525 StructuredList, newStructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001526
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001527 IList->setInit(OldIndex, DIE);
1528 if (hadError && !prevHadError) {
1529 ++Field;
1530 ++FieldIndex;
1531 if (NextField)
1532 *NextField = Field;
1533 StructuredIndex = FieldIndex;
1534 return true;
1535 }
1536 } else {
1537 // Recurse to check later designated subobjects.
1538 QualType FieldType = (*Field)->getType();
1539 unsigned newStructuredIndex = FieldIndex;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001540
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001541 InitializedEntity MemberEntity =
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001542 InitializedEntity::InitializeMember(*Field, &Entity);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001543 if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1,
1544 FieldType, 0, 0, Index,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001545 StructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001546 true, false))
1547 return true;
1548 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001549
1550 // Find the position of the next field to be initialized in this
1551 // subobject.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001552 ++Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001553 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001554
1555 // If this the first designator, our caller will continue checking
1556 // the rest of this struct/class/union subobject.
1557 if (IsFirstDesignator) {
1558 if (NextField)
1559 *NextField = Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001560 StructuredIndex = FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001561 return false;
1562 }
1563
Douglas Gregor34e79462009-01-28 23:36:17 +00001564 if (!FinishSubobjectInit)
1565 return false;
1566
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001567 // We've already initialized something in the union; we're done.
1568 if (RT->getDecl()->isUnion())
1569 return hadError;
1570
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001571 // Check the remaining fields within this class/struct/union subobject.
1572 bool prevHadError = hadError;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001573
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001574 CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001575 StructuredList, FieldIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001576 return hadError && !prevHadError;
1577 }
1578
1579 // C99 6.7.8p6:
1580 //
1581 // If a designator has the form
1582 //
1583 // [ constant-expression ]
1584 //
1585 // then the current object (defined below) shall have array
1586 // type and the expression shall be an integer constant
1587 // expression. If the array is of unknown size, any
1588 // nonnegative value is valid.
1589 //
1590 // Additionally, cope with the GNU extension that permits
1591 // designators of the form
1592 //
1593 // [ constant-expression ... constant-expression ]
Chris Lattner08202542009-02-24 22:50:46 +00001594 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001595 if (!AT) {
Chris Lattner08202542009-02-24 22:50:46 +00001596 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001597 << CurrentObjectType;
1598 ++Index;
1599 return true;
1600 }
1601
1602 Expr *IndexExpr = 0;
Douglas Gregor34e79462009-01-28 23:36:17 +00001603 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1604 if (D->isArrayDesignator()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001605 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattner3bf68932009-04-25 21:59:05 +00001606 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001607 DesignatedEndIndex = DesignatedStartIndex;
1608 } else {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001609 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor34e79462009-01-28 23:36:17 +00001610
Mike Stump1eb44332009-09-09 15:08:12 +00001611 DesignatedStartIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001612 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001613 DesignatedEndIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001614 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001615 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001616
Chris Lattnere0fd8322011-02-19 22:28:58 +00001617 // Codegen can't handle evaluating array range designators that have side
1618 // effects, because we replicate the AST value for each initialized element.
1619 // As such, set the sawArrayRangeDesignator() bit if we initialize multiple
1620 // elements with something that has a side effect, so codegen can emit an
1621 // "error unsupported" error instead of miscompiling the app.
1622 if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&&
1623 DIE->getInit()->HasSideEffects(SemaRef.Context))
Douglas Gregora9c87802009-01-29 19:42:23 +00001624 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001625 }
1626
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001627 if (isa<ConstantArrayType>(AT)) {
1628 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Jay Foad9f71a8f2010-12-07 08:25:34 +00001629 DesignatedStartIndex
1630 = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
Douglas Gregor34e79462009-01-28 23:36:17 +00001631 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
Jay Foad9f71a8f2010-12-07 08:25:34 +00001632 DesignatedEndIndex
1633 = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
Douglas Gregor34e79462009-01-28 23:36:17 +00001634 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1635 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner08202542009-02-24 22:50:46 +00001636 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001637 diag::err_array_designator_too_large)
Douglas Gregor34e79462009-01-28 23:36:17 +00001638 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001639 << IndexExpr->getSourceRange();
1640 ++Index;
1641 return true;
1642 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001643 } else {
1644 // Make sure the bit-widths and signedness match.
1645 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +00001646 DesignatedEndIndex
1647 = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattner3bf68932009-04-25 21:59:05 +00001648 else if (DesignatedStartIndex.getBitWidth() <
1649 DesignatedEndIndex.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +00001650 DesignatedStartIndex
1651 = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
Douglas Gregor34e79462009-01-28 23:36:17 +00001652 DesignatedStartIndex.setIsUnsigned(true);
1653 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001654 }
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Douglas Gregor4c678342009-01-28 21:54:33 +00001656 // Make sure that our non-designated initializer list has space
1657 // for a subobject corresponding to this array element.
Douglas Gregor34e79462009-01-28 23:36:17 +00001658 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump1eb44332009-09-09 15:08:12 +00001659 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor34e79462009-01-28 23:36:17 +00001660 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001661
Douglas Gregor34e79462009-01-28 23:36:17 +00001662 // Repeatedly perform subobject initializations in the range
1663 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001664
Douglas Gregor34e79462009-01-28 23:36:17 +00001665 // Move to the next designator
1666 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1667 unsigned OldIndex = Index;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001668
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001669 InitializedEntity ElementEntity =
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001670 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001671
Douglas Gregor34e79462009-01-28 23:36:17 +00001672 while (DesignatedStartIndex <= DesignatedEndIndex) {
1673 // Recurse to check later designated subobjects.
1674 QualType ElementType = AT->getElementType();
1675 Index = OldIndex;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001676
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001677 ElementEntity.setElementIndex(ElementIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001678 if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1,
1679 ElementType, 0, 0, Index,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001680 StructuredList, ElementIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001681 (DesignatedStartIndex == DesignatedEndIndex),
1682 false))
Douglas Gregor34e79462009-01-28 23:36:17 +00001683 return true;
1684
1685 // Move to the next index in the array that we'll be initializing.
1686 ++DesignatedStartIndex;
1687 ElementIndex = DesignatedStartIndex.getZExtValue();
1688 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001689
1690 // If this the first designator, our caller will continue checking
1691 // the rest of this array subobject.
1692 if (IsFirstDesignator) {
1693 if (NextElementIndex)
Douglas Gregor34e79462009-01-28 23:36:17 +00001694 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor4c678342009-01-28 21:54:33 +00001695 StructuredIndex = ElementIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001696 return false;
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Douglas Gregor34e79462009-01-28 23:36:17 +00001699 if (!FinishSubobjectInit)
1700 return false;
1701
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001702 // Check the remaining elements within this array subobject.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001703 bool prevHadError = hadError;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001704 CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex,
Anders Carlsson784f6992010-01-23 20:13:41 +00001705 /*SubobjectIsDesignatorContext=*/false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001706 StructuredList, ElementIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001707 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001708}
1709
Douglas Gregor4c678342009-01-28 21:54:33 +00001710// Get the structured initializer list for a subobject of type
1711// @p CurrentObjectType.
1712InitListExpr *
1713InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1714 QualType CurrentObjectType,
1715 InitListExpr *StructuredList,
1716 unsigned StructuredIndex,
1717 SourceRange InitRange) {
1718 Expr *ExistingInit = 0;
1719 if (!StructuredList)
1720 ExistingInit = SyntacticToSemantic[IList];
1721 else if (StructuredIndex < StructuredList->getNumInits())
1722 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Douglas Gregor4c678342009-01-28 21:54:33 +00001724 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1725 return Result;
1726
1727 if (ExistingInit) {
1728 // We are creating an initializer list that initializes the
1729 // subobjects of the current object, but there was already an
1730 // initialization that completely initialized the current
1731 // subobject, e.g., by a compound literal:
Mike Stump1eb44332009-09-09 15:08:12 +00001732 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001733 // struct X { int a, b; };
1734 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump1eb44332009-09-09 15:08:12 +00001735 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001736 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1737 // designated initializer re-initializes the whole
1738 // subobject [0], overwriting previous initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001739 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregored8a93d2009-03-01 17:12:46 +00001740 diag::warn_subobject_initializer_overrides)
Douglas Gregor4c678342009-01-28 21:54:33 +00001741 << InitRange;
Mike Stump1eb44332009-09-09 15:08:12 +00001742 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001743 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001744 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001745 << ExistingInit->getSourceRange();
1746 }
1747
Mike Stump1eb44332009-09-09 15:08:12 +00001748 InitListExpr *Result
Ted Kremenek709210f2010-04-13 23:39:13 +00001749 = new (SemaRef.Context) InitListExpr(SemaRef.Context,
1750 InitRange.getBegin(), 0, 0,
Ted Kremenekba7bc552010-02-19 01:50:18 +00001751 InitRange.getEnd());
Douglas Gregored8a93d2009-03-01 17:12:46 +00001752
Douglas Gregor63982352010-07-13 18:40:04 +00001753 Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context));
Douglas Gregor4c678342009-01-28 21:54:33 +00001754
Douglas Gregorfa219202009-03-20 23:58:33 +00001755 // Pre-allocate storage for the structured initializer list.
1756 unsigned NumElements = 0;
Douglas Gregor08457732009-03-21 18:13:52 +00001757 unsigned NumInits = 0;
1758 if (!StructuredList)
1759 NumInits = IList->getNumInits();
1760 else if (Index < IList->getNumInits()) {
1761 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1762 NumInits = SubList->getNumInits();
1763 }
1764
Mike Stump1eb44332009-09-09 15:08:12 +00001765 if (const ArrayType *AType
Douglas Gregorfa219202009-03-20 23:58:33 +00001766 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1767 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1768 NumElements = CAType->getSize().getZExtValue();
1769 // Simple heuristic so that we don't allocate a very large
1770 // initializer with many empty entries at the end.
Douglas Gregor08457732009-03-21 18:13:52 +00001771 if (NumInits && NumElements > NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001772 NumElements = 0;
1773 }
John McCall183700f2009-09-21 23:43:11 +00001774 } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
Douglas Gregorfa219202009-03-20 23:58:33 +00001775 NumElements = VType->getNumElements();
Ted Kremenek6217b802009-07-29 21:53:49 +00001776 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregorfa219202009-03-20 23:58:33 +00001777 RecordDecl *RDecl = RType->getDecl();
1778 if (RDecl->isUnion())
1779 NumElements = 1;
1780 else
Mike Stump1eb44332009-09-09 15:08:12 +00001781 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001782 RDecl->field_end());
Douglas Gregorfa219202009-03-20 23:58:33 +00001783 }
1784
Douglas Gregor08457732009-03-21 18:13:52 +00001785 if (NumElements < NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001786 NumElements = IList->getNumInits();
1787
Ted Kremenek709210f2010-04-13 23:39:13 +00001788 Result->reserveInits(SemaRef.Context, NumElements);
Douglas Gregorfa219202009-03-20 23:58:33 +00001789
Douglas Gregor4c678342009-01-28 21:54:33 +00001790 // Link this new initializer list into the structured initializer
1791 // lists.
1792 if (StructuredList)
Ted Kremenek709210f2010-04-13 23:39:13 +00001793 StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result);
Douglas Gregor4c678342009-01-28 21:54:33 +00001794 else {
1795 Result->setSyntacticForm(IList);
1796 SyntacticToSemantic[IList] = Result;
1797 }
1798
1799 return Result;
1800}
1801
1802/// Update the initializer at index @p StructuredIndex within the
1803/// structured initializer list to the value @p expr.
1804void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1805 unsigned &StructuredIndex,
1806 Expr *expr) {
1807 // No structured initializer list to update
1808 if (!StructuredList)
1809 return;
1810
Ted Kremenek709210f2010-04-13 23:39:13 +00001811 if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
1812 StructuredIndex, expr)) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001813 // This initializer overwrites a previous initializer. Warn.
Mike Stump1eb44332009-09-09 15:08:12 +00001814 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001815 diag::warn_initializer_overrides)
1816 << expr->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001817 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001818 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001819 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001820 << PrevInit->getSourceRange();
1821 }
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Douglas Gregor4c678342009-01-28 21:54:33 +00001823 ++StructuredIndex;
1824}
1825
Douglas Gregor05c13a32009-01-22 00:58:24 +00001826/// Check that the given Index expression is a valid array designator
1827/// value. This is essentailly just a wrapper around
Chris Lattner3bf68932009-04-25 21:59:05 +00001828/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregor05c13a32009-01-22 00:58:24 +00001829/// and produces a reasonable diagnostic if there is a
1830/// failure. Returns true if there was an error, false otherwise. If
1831/// everything went okay, Value will receive the value of the constant
1832/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001833static bool
Chris Lattner3bf68932009-04-25 21:59:05 +00001834CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001835 SourceLocation Loc = Index->getSourceRange().getBegin();
1836
1837 // Make sure this is an integer constant expression.
Chris Lattner3bf68932009-04-25 21:59:05 +00001838 if (S.VerifyIntegerConstantExpression(Index, &Value))
1839 return true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001840
Chris Lattner3bf68932009-04-25 21:59:05 +00001841 if (Value.isSigned() && Value.isNegative())
1842 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001843 << Value.toString(10) << Index->getSourceRange();
1844
Douglas Gregor53d3d8e2009-01-23 21:04:18 +00001845 Value.setIsUnsigned(true);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001846 return false;
1847}
1848
John McCall60d7b3a2010-08-24 06:29:42 +00001849ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
Nick Lewycky7663f392010-11-20 01:29:55 +00001850 SourceLocation Loc,
1851 bool GNUSyntax,
1852 ExprResult Init) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001853 typedef DesignatedInitExpr::Designator ASTDesignator;
1854
1855 bool Invalid = false;
1856 llvm::SmallVector<ASTDesignator, 32> Designators;
1857 llvm::SmallVector<Expr *, 32> InitExpressions;
1858
1859 // Build designators and check array designator expressions.
1860 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1861 const Designator &D = Desig.getDesignator(Idx);
1862 switch (D.getKind()) {
1863 case Designator::FieldDesignator:
Mike Stump1eb44332009-09-09 15:08:12 +00001864 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001865 D.getFieldLoc()));
1866 break;
1867
1868 case Designator::ArrayDesignator: {
1869 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1870 llvm::APSInt IndexValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001871 if (!Index->isTypeDependent() &&
1872 !Index->isValueDependent() &&
1873 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001874 Invalid = true;
1875 else {
1876 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001877 D.getLBracketLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001878 D.getRBracketLoc()));
1879 InitExpressions.push_back(Index);
1880 }
1881 break;
1882 }
1883
1884 case Designator::ArrayRangeDesignator: {
1885 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1886 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1887 llvm::APSInt StartValue;
1888 llvm::APSInt EndValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001889 bool StartDependent = StartIndex->isTypeDependent() ||
1890 StartIndex->isValueDependent();
1891 bool EndDependent = EndIndex->isTypeDependent() ||
1892 EndIndex->isValueDependent();
1893 if ((!StartDependent &&
1894 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1895 (!EndDependent &&
1896 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001897 Invalid = true;
Douglas Gregord6f584f2009-01-23 22:22:29 +00001898 else {
1899 // Make sure we're comparing values with the same bit width.
Douglas Gregor9ea62762009-05-21 23:17:49 +00001900 if (StartDependent || EndDependent) {
1901 // Nothing to compute.
1902 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +00001903 EndValue = EndValue.extend(StartValue.getBitWidth());
Douglas Gregord6f584f2009-01-23 22:22:29 +00001904 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +00001905 StartValue = StartValue.extend(EndValue.getBitWidth());
Douglas Gregord6f584f2009-01-23 22:22:29 +00001906
Douglas Gregorc4bb7bf2009-05-21 23:30:39 +00001907 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregord6f584f2009-01-23 22:22:29 +00001908 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump1eb44332009-09-09 15:08:12 +00001909 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregord6f584f2009-01-23 22:22:29 +00001910 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1911 Invalid = true;
1912 } else {
1913 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001914 D.getLBracketLoc(),
Douglas Gregord6f584f2009-01-23 22:22:29 +00001915 D.getEllipsisLoc(),
1916 D.getRBracketLoc()));
1917 InitExpressions.push_back(StartIndex);
1918 InitExpressions.push_back(EndIndex);
1919 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001920 }
1921 break;
1922 }
1923 }
1924 }
1925
1926 if (Invalid || Init.isInvalid())
1927 return ExprError();
1928
1929 // Clear out the expressions within the designation.
1930 Desig.ClearExprs(*this);
1931
1932 DesignatedInitExpr *DIE
Jay Foadbeaaccd2009-05-21 09:52:38 +00001933 = DesignatedInitExpr::Create(Context,
1934 Designators.data(), Designators.size(),
1935 InitExpressions.data(), InitExpressions.size(),
Anders Carlssone9146f22009-05-01 19:49:17 +00001936 Loc, GNUSyntax, Init.takeAs<Expr>());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001937
Douglas Gregor2d75bbd2011-01-16 16:13:16 +00001938 if (getLangOptions().CPlusPlus)
1939 Diag(DIE->getLocStart(), diag::ext_designated_init)
1940 << DIE->getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001941
Douglas Gregor05c13a32009-01-22 00:58:24 +00001942 return Owned(DIE);
1943}
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001944
Douglas Gregorcb57fb92009-12-16 06:35:08 +00001945bool Sema::CheckInitList(const InitializedEntity &Entity,
1946 InitListExpr *&InitList, QualType &DeclType) {
1947 InitListChecker CheckInitList(*this, Entity, InitList, DeclType);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001948 if (!CheckInitList.HadError())
1949 InitList = CheckInitList.getFullyStructuredList();
1950
1951 return CheckInitList.HadError();
1952}
Douglas Gregor87fd7032009-02-02 17:43:21 +00001953
Douglas Gregor20093b42009-12-09 23:02:17 +00001954//===----------------------------------------------------------------------===//
1955// Initialization entity
1956//===----------------------------------------------------------------------===//
1957
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001958InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
Douglas Gregorcb57fb92009-12-16 06:35:08 +00001959 const InitializedEntity &Parent)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001960 : Parent(&Parent), Index(Index)
Douglas Gregorcb57fb92009-12-16 06:35:08 +00001961{
Anders Carlssond3d824d2010-01-23 04:34:47 +00001962 if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
1963 Kind = EK_ArrayElement;
Douglas Gregord6542d82009-12-22 15:35:07 +00001964 Type = AT->getElementType();
Anders Carlssond3d824d2010-01-23 04:34:47 +00001965 } else {
1966 Kind = EK_VectorElement;
Douglas Gregord6542d82009-12-22 15:35:07 +00001967 Type = Parent.getType()->getAs<VectorType>()->getElementType();
Anders Carlssond3d824d2010-01-23 04:34:47 +00001968 }
Douglas Gregor20093b42009-12-09 23:02:17 +00001969}
1970
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001971InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
Anders Carlsson711f34a2010-04-21 19:52:01 +00001972 CXXBaseSpecifier *Base,
1973 bool IsInheritedVirtualBase)
Douglas Gregor20093b42009-12-09 23:02:17 +00001974{
1975 InitializedEntity Result;
1976 Result.Kind = EK_Base;
Anders Carlsson711f34a2010-04-21 19:52:01 +00001977 Result.Base = reinterpret_cast<uintptr_t>(Base);
1978 if (IsInheritedVirtualBase)
1979 Result.Base |= 0x01;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001980
Douglas Gregord6542d82009-12-22 15:35:07 +00001981 Result.Type = Base->getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00001982 return Result;
1983}
1984
Douglas Gregor99a2e602009-12-16 01:38:02 +00001985DeclarationName InitializedEntity::getName() const {
1986 switch (getKind()) {
Douglas Gregor99a2e602009-12-16 01:38:02 +00001987 case EK_Parameter:
Douglas Gregora188ff22009-12-22 16:09:06 +00001988 if (!VariableOrMember)
1989 return DeclarationName();
1990 // Fall through
1991
1992 case EK_Variable:
Douglas Gregor99a2e602009-12-16 01:38:02 +00001993 case EK_Member:
1994 return VariableOrMember->getDeclName();
1995
1996 case EK_Result:
1997 case EK_Exception:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00001998 case EK_New:
Douglas Gregor99a2e602009-12-16 01:38:02 +00001999 case EK_Temporary:
2000 case EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00002001 case EK_ArrayElement:
2002 case EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00002003 case EK_BlockElement:
Douglas Gregor99a2e602009-12-16 01:38:02 +00002004 return DeclarationName();
2005 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002006
Douglas Gregor99a2e602009-12-16 01:38:02 +00002007 // Silence GCC warning
2008 return DeclarationName();
2009}
2010
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00002011DeclaratorDecl *InitializedEntity::getDecl() const {
2012 switch (getKind()) {
2013 case EK_Variable:
2014 case EK_Parameter:
2015 case EK_Member:
2016 return VariableOrMember;
2017
2018 case EK_Result:
2019 case EK_Exception:
2020 case EK_New:
2021 case EK_Temporary:
2022 case EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00002023 case EK_ArrayElement:
2024 case EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00002025 case EK_BlockElement:
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00002026 return 0;
2027 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002028
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00002029 // Silence GCC warning
2030 return 0;
2031}
2032
Douglas Gregor3c9034c2010-05-15 00:13:29 +00002033bool InitializedEntity::allowsNRVO() const {
2034 switch (getKind()) {
2035 case EK_Result:
2036 case EK_Exception:
2037 return LocAndNRVO.NRVO;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002038
Douglas Gregor3c9034c2010-05-15 00:13:29 +00002039 case EK_Variable:
2040 case EK_Parameter:
2041 case EK_Member:
2042 case EK_New:
2043 case EK_Temporary:
2044 case EK_Base:
2045 case EK_ArrayElement:
2046 case EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00002047 case EK_BlockElement:
Douglas Gregor3c9034c2010-05-15 00:13:29 +00002048 break;
2049 }
2050
2051 return false;
2052}
2053
Douglas Gregor20093b42009-12-09 23:02:17 +00002054//===----------------------------------------------------------------------===//
2055// Initialization sequence
2056//===----------------------------------------------------------------------===//
2057
2058void InitializationSequence::Step::Destroy() {
2059 switch (Kind) {
2060 case SK_ResolveAddressOfOverloadedFunction:
2061 case SK_CastDerivedToBaseRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002062 case SK_CastDerivedToBaseXValue:
Douglas Gregor20093b42009-12-09 23:02:17 +00002063 case SK_CastDerivedToBaseLValue:
2064 case SK_BindReference:
2065 case SK_BindReferenceToTemporary:
Douglas Gregor523d46a2010-04-18 07:40:54 +00002066 case SK_ExtraneousCopyToTemporary:
Douglas Gregor20093b42009-12-09 23:02:17 +00002067 case SK_UserConversion:
2068 case SK_QualificationConversionRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002069 case SK_QualificationConversionXValue:
Douglas Gregor20093b42009-12-09 23:02:17 +00002070 case SK_QualificationConversionLValue:
Douglas Gregord87b61f2009-12-10 17:56:55 +00002071 case SK_ListInitialization:
Douglas Gregor51c56d62009-12-14 20:49:26 +00002072 case SK_ConstructorInitialization:
Douglas Gregor71d17402009-12-15 00:01:57 +00002073 case SK_ZeroInitialization:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002074 case SK_CAssignment:
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002075 case SK_StringInit:
Douglas Gregor569c3162010-08-07 11:51:51 +00002076 case SK_ObjCObjectConversion:
Douglas Gregor20093b42009-12-09 23:02:17 +00002077 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002078
Douglas Gregor20093b42009-12-09 23:02:17 +00002079 case SK_ConversionSequence:
2080 delete ICS;
2081 }
2082}
2083
Douglas Gregorb70cf442010-03-26 20:14:36 +00002084bool InitializationSequence::isDirectReferenceBinding() const {
2085 return getKind() == ReferenceBinding && Steps.back().Kind == SK_BindReference;
2086}
2087
2088bool InitializationSequence::isAmbiguous() const {
2089 if (getKind() != FailedSequence)
2090 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002091
Douglas Gregorb70cf442010-03-26 20:14:36 +00002092 switch (getFailureKind()) {
2093 case FK_TooManyInitsForReference:
2094 case FK_ArrayNeedsInitList:
2095 case FK_ArrayNeedsInitListOrStringLiteral:
2096 case FK_AddressOfOverloadFailed: // FIXME: Could do better
2097 case FK_NonConstLValueReferenceBindingToTemporary:
2098 case FK_NonConstLValueReferenceBindingToUnrelated:
2099 case FK_RValueReferenceBindingToLValue:
2100 case FK_ReferenceInitDropsQualifiers:
2101 case FK_ReferenceInitFailed:
2102 case FK_ConversionFailed:
2103 case FK_TooManyInitsForScalar:
2104 case FK_ReferenceBindingToInitList:
2105 case FK_InitListBadDestinationType:
2106 case FK_DefaultInitOfConst:
Douglas Gregor72a43bb2010-05-20 22:12:02 +00002107 case FK_Incomplete:
Douglas Gregorb70cf442010-03-26 20:14:36 +00002108 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002109
Douglas Gregorb70cf442010-03-26 20:14:36 +00002110 case FK_ReferenceInitOverloadFailed:
2111 case FK_UserConversionOverloadFailed:
2112 case FK_ConstructorOverloadFailed:
2113 return FailedOverloadResult == OR_Ambiguous;
2114 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002115
Douglas Gregorb70cf442010-03-26 20:14:36 +00002116 return false;
2117}
2118
Douglas Gregord6e44a32010-04-16 22:09:46 +00002119bool InitializationSequence::isConstructorInitialization() const {
2120 return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization;
2121}
2122
Douglas Gregor20093b42009-12-09 23:02:17 +00002123void InitializationSequence::AddAddressOverloadResolutionStep(
John McCall6bb80172010-03-30 21:47:33 +00002124 FunctionDecl *Function,
2125 DeclAccessPair Found) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002126 Step S;
2127 S.Kind = SK_ResolveAddressOfOverloadedFunction;
2128 S.Type = Function->getType();
John McCall9aa472c2010-03-19 07:35:19 +00002129 S.Function.Function = Function;
John McCall6bb80172010-03-30 21:47:33 +00002130 S.Function.FoundDecl = Found;
Douglas Gregor20093b42009-12-09 23:02:17 +00002131 Steps.push_back(S);
2132}
2133
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002134void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
John McCall5baba9d2010-08-25 10:28:54 +00002135 ExprValueKind VK) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002136 Step S;
John McCall5baba9d2010-08-25 10:28:54 +00002137 switch (VK) {
2138 case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break;
2139 case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break;
2140 case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break;
Sebastian Redl906082e2010-07-20 04:20:21 +00002141 default: llvm_unreachable("No such category");
2142 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002143 S.Type = BaseType;
2144 Steps.push_back(S);
2145}
2146
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002147void InitializationSequence::AddReferenceBindingStep(QualType T,
Douglas Gregor20093b42009-12-09 23:02:17 +00002148 bool BindingTemporary) {
2149 Step S;
2150 S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
2151 S.Type = T;
2152 Steps.push_back(S);
2153}
2154
Douglas Gregor523d46a2010-04-18 07:40:54 +00002155void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) {
2156 Step S;
2157 S.Kind = SK_ExtraneousCopyToTemporary;
2158 S.Type = T;
2159 Steps.push_back(S);
2160}
2161
Eli Friedman03981012009-12-11 02:42:07 +00002162void InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +00002163 DeclAccessPair FoundDecl,
Eli Friedman03981012009-12-11 02:42:07 +00002164 QualType T) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002165 Step S;
2166 S.Kind = SK_UserConversion;
Eli Friedman03981012009-12-11 02:42:07 +00002167 S.Type = T;
John McCall9aa472c2010-03-19 07:35:19 +00002168 S.Function.Function = Function;
2169 S.Function.FoundDecl = FoundDecl;
Douglas Gregor20093b42009-12-09 23:02:17 +00002170 Steps.push_back(S);
2171}
2172
2173void InitializationSequence::AddQualificationConversionStep(QualType Ty,
John McCall5baba9d2010-08-25 10:28:54 +00002174 ExprValueKind VK) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002175 Step S;
John McCall38a4ffe2010-08-26 16:36:35 +00002176 S.Kind = SK_QualificationConversionRValue; // work around a gcc warning
John McCall5baba9d2010-08-25 10:28:54 +00002177 switch (VK) {
2178 case VK_RValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002179 S.Kind = SK_QualificationConversionRValue;
2180 break;
John McCall5baba9d2010-08-25 10:28:54 +00002181 case VK_XValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002182 S.Kind = SK_QualificationConversionXValue;
2183 break;
John McCall5baba9d2010-08-25 10:28:54 +00002184 case VK_LValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002185 S.Kind = SK_QualificationConversionLValue;
2186 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00002187 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002188 S.Type = Ty;
2189 Steps.push_back(S);
2190}
2191
2192void InitializationSequence::AddConversionSequenceStep(
2193 const ImplicitConversionSequence &ICS,
2194 QualType T) {
2195 Step S;
2196 S.Kind = SK_ConversionSequence;
2197 S.Type = T;
2198 S.ICS = new ImplicitConversionSequence(ICS);
2199 Steps.push_back(S);
2200}
2201
Douglas Gregord87b61f2009-12-10 17:56:55 +00002202void InitializationSequence::AddListInitializationStep(QualType T) {
2203 Step S;
2204 S.Kind = SK_ListInitialization;
2205 S.Type = T;
2206 Steps.push_back(S);
2207}
2208
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002209void
Douglas Gregor51c56d62009-12-14 20:49:26 +00002210InitializationSequence::AddConstructorInitializationStep(
2211 CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +00002212 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +00002213 QualType T) {
2214 Step S;
2215 S.Kind = SK_ConstructorInitialization;
2216 S.Type = T;
John McCall9aa472c2010-03-19 07:35:19 +00002217 S.Function.Function = Constructor;
2218 S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002219 Steps.push_back(S);
2220}
2221
Douglas Gregor71d17402009-12-15 00:01:57 +00002222void InitializationSequence::AddZeroInitializationStep(QualType T) {
2223 Step S;
2224 S.Kind = SK_ZeroInitialization;
2225 S.Type = T;
2226 Steps.push_back(S);
2227}
2228
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002229void InitializationSequence::AddCAssignmentStep(QualType T) {
2230 Step S;
2231 S.Kind = SK_CAssignment;
2232 S.Type = T;
2233 Steps.push_back(S);
2234}
2235
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002236void InitializationSequence::AddStringInitStep(QualType T) {
2237 Step S;
2238 S.Kind = SK_StringInit;
2239 S.Type = T;
2240 Steps.push_back(S);
2241}
2242
Douglas Gregor569c3162010-08-07 11:51:51 +00002243void InitializationSequence::AddObjCObjectConversionStep(QualType T) {
2244 Step S;
2245 S.Kind = SK_ObjCObjectConversion;
2246 S.Type = T;
2247 Steps.push_back(S);
2248}
2249
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002250void InitializationSequence::SetOverloadFailure(FailureKind Failure,
Douglas Gregor20093b42009-12-09 23:02:17 +00002251 OverloadingResult Result) {
2252 SequenceKind = FailedSequence;
2253 this->Failure = Failure;
2254 this->FailedOverloadResult = Result;
2255}
2256
2257//===----------------------------------------------------------------------===//
2258// Attempt initialization
2259//===----------------------------------------------------------------------===//
2260
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002261/// \brief Attempt list initialization (C++0x [dcl.init.list])
2262static void TryListInitialization(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00002263 const InitializedEntity &Entity,
2264 const InitializationKind &Kind,
2265 InitListExpr *InitList,
2266 InitializationSequence &Sequence) {
Douglas Gregord87b61f2009-12-10 17:56:55 +00002267 // FIXME: We only perform rudimentary checking of list
2268 // initializations at this point, then assume that any list
2269 // initialization of an array, aggregate, or scalar will be
Sebastian Redl36c28db2010-06-30 16:41:54 +00002270 // well-formed. When we actually "perform" list initialization, we'll
Douglas Gregord87b61f2009-12-10 17:56:55 +00002271 // do all of the necessary checking. C++0x initializer lists will
2272 // force us to perform more checking here.
2273 Sequence.setSequenceKind(InitializationSequence::ListInitialization);
2274
Douglas Gregord6542d82009-12-22 15:35:07 +00002275 QualType DestType = Entity.getType();
Douglas Gregord87b61f2009-12-10 17:56:55 +00002276
2277 // C++ [dcl.init]p13:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002278 // If T is a scalar type, then a declaration of the form
Douglas Gregord87b61f2009-12-10 17:56:55 +00002279 //
2280 // T x = { a };
2281 //
2282 // is equivalent to
2283 //
2284 // T x = a;
2285 if (DestType->isScalarType()) {
2286 if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) {
2287 Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
2288 return;
2289 }
2290
2291 // Assume scalar initialization from a single value works.
2292 } else if (DestType->isAggregateType()) {
2293 // Assume aggregate initialization works.
2294 } else if (DestType->isVectorType()) {
2295 // Assume vector initialization works.
2296 } else if (DestType->isReferenceType()) {
2297 // FIXME: C++0x defines behavior for this.
2298 Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
2299 return;
2300 } else if (DestType->isRecordType()) {
2301 // FIXME: C++0x defines behavior for this
2302 Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType);
2303 }
2304
2305 // Add a general "list initialization" step.
2306 Sequence.AddListInitializationStep(DestType);
Douglas Gregor20093b42009-12-09 23:02:17 +00002307}
2308
2309/// \brief Try a reference initialization that involves calling a conversion
2310/// function.
Douglas Gregor20093b42009-12-09 23:02:17 +00002311static OverloadingResult TryRefInitWithConversionFunction(Sema &S,
2312 const InitializedEntity &Entity,
2313 const InitializationKind &Kind,
2314 Expr *Initializer,
2315 bool AllowRValues,
2316 InitializationSequence &Sequence) {
Douglas Gregord6542d82009-12-22 15:35:07 +00002317 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00002318 QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
2319 QualType T1 = cv1T1.getUnqualifiedType();
2320 QualType cv2T2 = Initializer->getType();
2321 QualType T2 = cv2T2.getUnqualifiedType();
2322
2323 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +00002324 bool ObjCConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002325 assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
Douglas Gregor569c3162010-08-07 11:51:51 +00002326 T1, T2, DerivedToBase,
2327 ObjCConversion) &&
Douglas Gregor20093b42009-12-09 23:02:17 +00002328 "Must have incompatible references when binding via conversion");
Chandler Carruth60cfcec2009-12-13 01:37:04 +00002329 (void)DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +00002330 (void)ObjCConversion;
Douglas Gregor20093b42009-12-09 23:02:17 +00002331
2332 // Build the candidate set directly in the initialization sequence
2333 // structure, so that it will persist if we fail.
2334 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2335 CandidateSet.clear();
2336
2337 // Determine whether we are allowed to call explicit constructors or
2338 // explicit conversion operators.
2339 bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002340
Douglas Gregor20093b42009-12-09 23:02:17 +00002341 const RecordType *T1RecordType = 0;
Douglas Gregor6b6d01f2010-05-07 19:42:26 +00002342 if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) &&
2343 !S.RequireCompleteType(Kind.getLocation(), T1, 0)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002344 // The type we're converting to is a class type. Enumerate its constructors
2345 // to see if there is a suitable conversion.
2346 CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
John McCall572fc622010-08-17 07:23:57 +00002347
Douglas Gregor20093b42009-12-09 23:02:17 +00002348 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregore5eee5a2010-07-02 23:12:18 +00002349 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl);
Douglas Gregor20093b42009-12-09 23:02:17 +00002350 Con != ConEnd; ++Con) {
John McCall9aa472c2010-03-19 07:35:19 +00002351 NamedDecl *D = *Con;
2352 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
2353
Douglas Gregor20093b42009-12-09 23:02:17 +00002354 // Find the constructor (which may be a template).
2355 CXXConstructorDecl *Constructor = 0;
John McCall9aa472c2010-03-19 07:35:19 +00002356 FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor20093b42009-12-09 23:02:17 +00002357 if (ConstructorTmpl)
2358 Constructor = cast<CXXConstructorDecl>(
2359 ConstructorTmpl->getTemplatedDecl());
2360 else
John McCall9aa472c2010-03-19 07:35:19 +00002361 Constructor = cast<CXXConstructorDecl>(D);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002362
Douglas Gregor20093b42009-12-09 23:02:17 +00002363 if (!Constructor->isInvalidDecl() &&
2364 Constructor->isConvertingConstructor(AllowExplicit)) {
2365 if (ConstructorTmpl)
John McCall9aa472c2010-03-19 07:35:19 +00002366 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
John McCall86820f52010-01-26 01:37:31 +00002367 /*ExplicitArgs*/ 0,
Argyrios Kyrtzidisb72db892010-10-05 03:05:30 +00002368 &Initializer, 1, CandidateSet,
2369 /*SuppressUserConversions=*/true);
Douglas Gregor20093b42009-12-09 23:02:17 +00002370 else
John McCall9aa472c2010-03-19 07:35:19 +00002371 S.AddOverloadCandidate(Constructor, FoundDecl,
Argyrios Kyrtzidisb72db892010-10-05 03:05:30 +00002372 &Initializer, 1, CandidateSet,
2373 /*SuppressUserConversions=*/true);
Douglas Gregor20093b42009-12-09 23:02:17 +00002374 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002375 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002376 }
John McCall572fc622010-08-17 07:23:57 +00002377 if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl())
2378 return OR_No_Viable_Function;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002379
Douglas Gregor6b6d01f2010-05-07 19:42:26 +00002380 const RecordType *T2RecordType = 0;
2381 if ((T2RecordType = T2->getAs<RecordType>()) &&
2382 !S.RequireCompleteType(Kind.getLocation(), T2, 0)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002383 // The type we're converting from is a class type, enumerate its conversion
2384 // functions.
2385 CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
2386
John McCalleec51cf2010-01-20 00:46:10 +00002387 const UnresolvedSetImpl *Conversions
Douglas Gregor20093b42009-12-09 23:02:17 +00002388 = T2RecordDecl->getVisibleConversionFunctions();
John McCalleec51cf2010-01-20 00:46:10 +00002389 for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
2390 E = Conversions->end(); I != E; ++I) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002391 NamedDecl *D = *I;
2392 CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
2393 if (isa<UsingShadowDecl>(D))
2394 D = cast<UsingShadowDecl>(D)->getTargetDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002395
Douglas Gregor20093b42009-12-09 23:02:17 +00002396 FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2397 CXXConversionDecl *Conv;
2398 if (ConvTemplate)
2399 Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
2400 else
Sebastian Redl4680bf22010-06-30 18:13:39 +00002401 Conv = cast<CXXConversionDecl>(D);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002402
Douglas Gregor20093b42009-12-09 23:02:17 +00002403 // If the conversion function doesn't return a reference type,
2404 // it can't be considered for this conversion unless we're allowed to
2405 // consider rvalues.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002406 // FIXME: Do we need to make sure that we only consider conversion
2407 // candidates with reference-compatible results? That might be needed to
Douglas Gregor20093b42009-12-09 23:02:17 +00002408 // break recursion.
2409 if ((AllowExplicit || !Conv->isExplicit()) &&
2410 (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
2411 if (ConvTemplate)
John McCall9aa472c2010-03-19 07:35:19 +00002412 S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
John McCall86820f52010-01-26 01:37:31 +00002413 ActingDC, Initializer,
Douglas Gregor564cb062011-01-21 00:27:08 +00002414 DestType, CandidateSet);
Douglas Gregor20093b42009-12-09 23:02:17 +00002415 else
John McCall9aa472c2010-03-19 07:35:19 +00002416 S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
Douglas Gregor564cb062011-01-21 00:27:08 +00002417 Initializer, DestType, CandidateSet);
Douglas Gregor20093b42009-12-09 23:02:17 +00002418 }
2419 }
2420 }
John McCall572fc622010-08-17 07:23:57 +00002421 if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl())
2422 return OR_No_Viable_Function;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002423
Douglas Gregor20093b42009-12-09 23:02:17 +00002424 SourceLocation DeclLoc = Initializer->getLocStart();
2425
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002426 // Perform overload resolution. If it fails, return the failed result.
Douglas Gregor20093b42009-12-09 23:02:17 +00002427 OverloadCandidateSet::iterator Best;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002428 if (OverloadingResult Result
Douglas Gregor8fcc5162010-09-12 08:07:23 +00002429 = CandidateSet.BestViableFunction(S, DeclLoc, Best, true))
Douglas Gregor20093b42009-12-09 23:02:17 +00002430 return Result;
Eli Friedman03981012009-12-11 02:42:07 +00002431
Douglas Gregor20093b42009-12-09 23:02:17 +00002432 FunctionDecl *Function = Best->Function;
Eli Friedman03981012009-12-11 02:42:07 +00002433
2434 // Compute the returned type of the conversion.
Douglas Gregor20093b42009-12-09 23:02:17 +00002435 if (isa<CXXConversionDecl>(Function))
2436 T2 = Function->getResultType();
2437 else
2438 T2 = cv1T1;
Eli Friedman03981012009-12-11 02:42:07 +00002439
2440 // Add the user-defined conversion step.
John McCall9aa472c2010-03-19 07:35:19 +00002441 Sequence.AddUserConversionStep(Function, Best->FoundDecl,
Douglas Gregor63982352010-07-13 18:40:04 +00002442 T2.getNonLValueExprType(S.Context));
Eli Friedman03981012009-12-11 02:42:07 +00002443
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444 // Determine whether we need to perform derived-to-base or
Eli Friedman03981012009-12-11 02:42:07 +00002445 // cv-qualification adjustments.
John McCall5baba9d2010-08-25 10:28:54 +00002446 ExprValueKind VK = VK_RValue;
Sebastian Redl906082e2010-07-20 04:20:21 +00002447 if (T2->isLValueReferenceType())
John McCall5baba9d2010-08-25 10:28:54 +00002448 VK = VK_LValue;
Sebastian Redl906082e2010-07-20 04:20:21 +00002449 else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>())
John McCall5baba9d2010-08-25 10:28:54 +00002450 VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue;
Sebastian Redl906082e2010-07-20 04:20:21 +00002451
Douglas Gregor20093b42009-12-09 23:02:17 +00002452 bool NewDerivedToBase = false;
Douglas Gregor569c3162010-08-07 11:51:51 +00002453 bool NewObjCConversion = false;
Douglas Gregor20093b42009-12-09 23:02:17 +00002454 Sema::ReferenceCompareResult NewRefRelationship
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002455 = S.CompareReferenceRelationship(DeclLoc, T1,
Douglas Gregor63982352010-07-13 18:40:04 +00002456 T2.getNonLValueExprType(S.Context),
Douglas Gregor569c3162010-08-07 11:51:51 +00002457 NewDerivedToBase, NewObjCConversion);
Douglas Gregora1a9f032010-03-07 23:17:44 +00002458 if (NewRefRelationship == Sema::Ref_Incompatible) {
2459 // If the type we've converted to is not reference-related to the
2460 // type we're looking for, then there is another conversion step
2461 // we need to perform to produce a temporary of the right type
2462 // that we'll be binding to.
2463 ImplicitConversionSequence ICS;
2464 ICS.setStandard();
2465 ICS.Standard = Best->FinalConversion;
2466 T2 = ICS.Standard.getToType(2);
2467 Sequence.AddConversionSequenceStep(ICS, T2);
2468 } else if (NewDerivedToBase)
Douglas Gregor20093b42009-12-09 23:02:17 +00002469 Sequence.AddDerivedToBaseCastStep(
2470 S.Context.getQualifiedType(T1,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002471 T2.getNonReferenceType().getQualifiers()),
John McCall5baba9d2010-08-25 10:28:54 +00002472 VK);
Douglas Gregor569c3162010-08-07 11:51:51 +00002473 else if (NewObjCConversion)
2474 Sequence.AddObjCObjectConversionStep(
2475 S.Context.getQualifiedType(T1,
2476 T2.getNonReferenceType().getQualifiers()));
2477
Douglas Gregor20093b42009-12-09 23:02:17 +00002478 if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
John McCall5baba9d2010-08-25 10:28:54 +00002479 Sequence.AddQualificationConversionStep(cv1T1, VK);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002480
Douglas Gregor20093b42009-12-09 23:02:17 +00002481 Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
2482 return OR_Success;
2483}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002484
2485/// \brief Attempt reference initialization (C++0x [dcl.init.ref])
2486static void TryReferenceInitialization(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00002487 const InitializedEntity &Entity,
2488 const InitializationKind &Kind,
2489 Expr *Initializer,
2490 InitializationSequence &Sequence) {
2491 Sequence.setSequenceKind(InitializationSequence::ReferenceBinding);
Sebastian Redl4680bf22010-06-30 18:13:39 +00002492
Douglas Gregord6542d82009-12-22 15:35:07 +00002493 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00002494 QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
Chandler Carruth5535c382010-01-12 20:32:25 +00002495 Qualifiers T1Quals;
2496 QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
Douglas Gregor20093b42009-12-09 23:02:17 +00002497 QualType cv2T2 = Initializer->getType();
Chandler Carruth5535c382010-01-12 20:32:25 +00002498 Qualifiers T2Quals;
2499 QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
Douglas Gregor20093b42009-12-09 23:02:17 +00002500 SourceLocation DeclLoc = Initializer->getLocStart();
Sebastian Redl4680bf22010-06-30 18:13:39 +00002501
Douglas Gregor20093b42009-12-09 23:02:17 +00002502 // If the initializer is the address of an overloaded function, try
2503 // to resolve the overloaded function. If all goes well, T2 is the
2504 // type of the resulting function.
2505 if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) {
John McCall6bb80172010-03-30 21:47:33 +00002506 DeclAccessPair Found;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002507 if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer,
Douglas Gregor3afb9772010-11-08 15:20:28 +00002508 T1,
2509 false,
2510 Found)) {
2511 Sequence.AddAddressOverloadResolutionStep(Fn, Found);
2512 cv2T2 = Fn->getType();
2513 T2 = cv2T2.getUnqualifiedType();
2514 } else if (!T1->isRecordType()) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002515 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
2516 return;
2517 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002518 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002519
Douglas Gregor20093b42009-12-09 23:02:17 +00002520 // Compute some basic properties of the types and the initializer.
2521 bool isLValueRef = DestType->isLValueReferenceType();
2522 bool isRValueRef = !isLValueRef;
2523 bool DerivedToBase = false;
Douglas Gregor569c3162010-08-07 11:51:51 +00002524 bool ObjCConversion = false;
Sebastian Redl4680bf22010-06-30 18:13:39 +00002525 Expr::Classification InitCategory = Initializer->Classify(S.Context);
Douglas Gregor20093b42009-12-09 23:02:17 +00002526 Sema::ReferenceCompareResult RefRelationship
Douglas Gregor569c3162010-08-07 11:51:51 +00002527 = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase,
2528 ObjCConversion);
Sebastian Redl4680bf22010-06-30 18:13:39 +00002529
Douglas Gregor20093b42009-12-09 23:02:17 +00002530 // C++0x [dcl.init.ref]p5:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002531 // A reference to type "cv1 T1" is initialized by an expression of type
Douglas Gregor20093b42009-12-09 23:02:17 +00002532 // "cv2 T2" as follows:
2533 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002534 // - If the reference is an lvalue reference and the initializer
Douglas Gregor20093b42009-12-09 23:02:17 +00002535 // expression
Sebastian Redl4680bf22010-06-30 18:13:39 +00002536 // Note the analogous bullet points for rvlaue refs to functions. Because
2537 // there are no function rvalues in C++, rvalue refs to functions are treated
2538 // like lvalue refs.
Douglas Gregor20093b42009-12-09 23:02:17 +00002539 OverloadingResult ConvOvlResult = OR_Success;
Sebastian Redl4680bf22010-06-30 18:13:39 +00002540 bool T1Function = T1->isFunctionType();
2541 if (isLValueRef || T1Function) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002542 if (InitCategory.isLValue() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002543 (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002544 (Kind.isCStyleOrFunctionalCast() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002545 RefRelationship == Sema::Ref_Related))) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002546 // - is an lvalue (but is not a bit-field), and "cv1 T1" is
Douglas Gregor20093b42009-12-09 23:02:17 +00002547 // reference-compatible with "cv2 T2," or
2548 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002549 // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a
Douglas Gregor20093b42009-12-09 23:02:17 +00002550 // bit-field when we're determining whether the reference initialization
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002551 // can occur. However, we do pay attention to whether it is a bit-field
2552 // to decide whether we're actually binding to a temporary created from
2553 // the bit-field.
Douglas Gregor20093b42009-12-09 23:02:17 +00002554 if (DerivedToBase)
2555 Sequence.AddDerivedToBaseCastStep(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002556 S.Context.getQualifiedType(T1, T2Quals),
John McCall5baba9d2010-08-25 10:28:54 +00002557 VK_LValue);
Douglas Gregor569c3162010-08-07 11:51:51 +00002558 else if (ObjCConversion)
2559 Sequence.AddObjCObjectConversionStep(
2560 S.Context.getQualifiedType(T1, T2Quals));
2561
Chandler Carruth5535c382010-01-12 20:32:25 +00002562 if (T1Quals != T2Quals)
John McCall5baba9d2010-08-25 10:28:54 +00002563 Sequence.AddQualificationConversionStep(cv1T1, VK_LValue);
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002564 bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() &&
Anders Carlsson09380262010-01-31 17:18:49 +00002565 (Initializer->getBitField() || Initializer->refersToVectorElement());
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002566 Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary);
Douglas Gregor20093b42009-12-09 23:02:17 +00002567 return;
2568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002569
2570 // - has a class type (i.e., T2 is a class type), where T1 is not
2571 // reference-related to T2, and can be implicitly converted to an
2572 // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
2573 // with "cv3 T3" (this conversion is selected by enumerating the
Douglas Gregor20093b42009-12-09 23:02:17 +00002574 // applicable conversion functions (13.3.1.6) and choosing the best
2575 // one through overload resolution (13.3)),
Sebastian Redl4680bf22010-06-30 18:13:39 +00002576 // If we have an rvalue ref to function type here, the rhs must be
2577 // an rvalue.
2578 if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
2579 (isLValueRef || InitCategory.isRValue())) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002580 ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
Douglas Gregor20093b42009-12-09 23:02:17 +00002581 Initializer,
Sebastian Redl4680bf22010-06-30 18:13:39 +00002582 /*AllowRValues=*/isRValueRef,
Douglas Gregor20093b42009-12-09 23:02:17 +00002583 Sequence);
2584 if (ConvOvlResult == OR_Success)
2585 return;
John McCall1d318332010-01-12 00:44:57 +00002586 if (ConvOvlResult != OR_No_Viable_Function) {
2587 Sequence.SetOverloadFailure(
2588 InitializationSequence::FK_ReferenceInitOverloadFailed,
2589 ConvOvlResult);
2590 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002591 }
2592 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002593
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002594 // - Otherwise, the reference shall be an lvalue reference to a
Douglas Gregor20093b42009-12-09 23:02:17 +00002595 // non-volatile const type (i.e., cv1 shall be const), or the reference
Douglas Gregor69d83162011-01-20 16:08:06 +00002596 // shall be an rvalue reference.
Douglas Gregorb2855ad2011-01-21 00:52:42 +00002597 if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) {
Douglas Gregor3afb9772010-11-08 15:20:28 +00002598 if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
2599 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
2600 else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
Douglas Gregor20093b42009-12-09 23:02:17 +00002601 Sequence.SetOverloadFailure(
2602 InitializationSequence::FK_ReferenceInitOverloadFailed,
2603 ConvOvlResult);
Douglas Gregorb2855ad2011-01-21 00:52:42 +00002604 else
Sebastian Redl4680bf22010-06-30 18:13:39 +00002605 Sequence.SetFailed(InitCategory.isLValue()
Douglas Gregor20093b42009-12-09 23:02:17 +00002606 ? (RefRelationship == Sema::Ref_Related
2607 ? InitializationSequence::FK_ReferenceInitDropsQualifiers
2608 : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
2609 : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
Sebastian Redl4680bf22010-06-30 18:13:39 +00002610
Douglas Gregor20093b42009-12-09 23:02:17 +00002611 return;
2612 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002613
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002614 // - If the initializer expression
2615 // - is an xvalue, class prvalue, array prvalue, or function lvalue and
2616 // "cv1 T1" is reference-compatible with "cv2 T2"
2617 // Note: functions are handled below.
2618 if (!T1Function &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002619 (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002620 (Kind.isCStyleOrFunctionalCast() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002621 RefRelationship == Sema::Ref_Related)) &&
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002622 (InitCategory.isXValue() ||
2623 (InitCategory.isPRValue() && T2->isRecordType()) ||
2624 (InitCategory.isPRValue() && T2->isArrayType()))) {
2625 ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue;
2626 if (InitCategory.isPRValue() && T2->isRecordType()) {
Douglas Gregor523d46a2010-04-18 07:40:54 +00002627 // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the
2628 // compiler the freedom to perform a copy here or bind to the
2629 // object, while C++0x requires that we bind directly to the
2630 // object. Hence, we always bind to the object without making an
2631 // extra copy. However, in C++03 requires that we check for the
2632 // presence of a suitable copy constructor:
2633 //
2634 // The constructor that would be used to make the copy shall
2635 // be callable whether or not the copy is actually done.
Francois Pichetf57258b2010-12-31 10:43:42 +00002636 if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft)
Douglas Gregor523d46a2010-04-18 07:40:54 +00002637 Sequence.AddExtraneousCopyToTemporary(cv2T2);
Douglas Gregor20093b42009-12-09 23:02:17 +00002638 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002639
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002640 if (DerivedToBase)
2641 Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals),
2642 ValueKind);
2643 else if (ObjCConversion)
2644 Sequence.AddObjCObjectConversionStep(
2645 S.Context.getQualifiedType(T1, T2Quals));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002646
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002647 if (T1Quals != T2Quals)
2648 Sequence.AddQualificationConversionStep(cv1T1, ValueKind);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002649 Sequence.AddReferenceBindingStep(cv1T1,
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002650 /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType()));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002651 return;
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002652 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
2654 // - has a class type (i.e., T2 is a class type), where T1 is not
2655 // reference-related to T2, and can be implicitly converted to an
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002656 // xvalue, class prvalue, or function lvalue of type "cv3 T3",
2657 // where "cv1 T1" is reference-compatible with "cv3 T3",
Douglas Gregorc5db24d2011-01-20 16:44:54 +00002658 if (T2->isRecordType()) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002659 if (RefRelationship == Sema::Ref_Incompatible) {
2660 ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
2661 Kind, Initializer,
2662 /*AllowRValues=*/true,
2663 Sequence);
2664 if (ConvOvlResult)
2665 Sequence.SetOverloadFailure(
2666 InitializationSequence::FK_ReferenceInitOverloadFailed,
2667 ConvOvlResult);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002668
Douglas Gregor20093b42009-12-09 23:02:17 +00002669 return;
2670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002671
Douglas Gregor20093b42009-12-09 23:02:17 +00002672 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2673 return;
2674 }
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002675
2676 // - Otherwise, a temporary of type "cv1 T1" is created and initialized
Douglas Gregor20093b42009-12-09 23:02:17 +00002677 // from the initializer expression using the rules for a non-reference
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002678 // copy initialization (8.5). The reference is then bound to the
Douglas Gregor20093b42009-12-09 23:02:17 +00002679 // temporary. [...]
John McCall369371c2010-06-04 02:29:22 +00002680
Douglas Gregor20093b42009-12-09 23:02:17 +00002681 // Determine whether we are allowed to call explicit constructors or
2682 // explicit conversion operators.
2683 bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct);
John McCall369371c2010-06-04 02:29:22 +00002684
2685 InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
2686
2687 if (S.TryImplicitConversion(Sequence, TempEntity, Initializer,
2688 /*SuppressUserConversions*/ false,
2689 AllowExplicit,
Douglas Gregor14d0aee2011-01-27 00:58:17 +00002690 /*FIXME:InOverloadResolution=*/false,
2691 /*CStyle=*/Kind.isCStyleOrFunctionalCast())) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002692 // FIXME: Use the conversion function set stored in ICS to turn
2693 // this into an overloading ambiguity diagnostic. However, we need
2694 // to keep that set as an OverloadCandidateSet rather than as some
2695 // other kind of set.
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002696 if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
2697 Sequence.SetOverloadFailure(
2698 InitializationSequence::FK_ReferenceInitOverloadFailed,
2699 ConvOvlResult);
Douglas Gregor3afb9772010-11-08 15:20:28 +00002700 else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
2701 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002702 else
2703 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
Douglas Gregor20093b42009-12-09 23:02:17 +00002704 return;
2705 }
2706
2707 // [...] If T1 is reference-related to T2, cv1 must be the
2708 // same cv-qualification as, or greater cv-qualification
2709 // than, cv2; otherwise, the program is ill-formed.
Chandler Carruth5535c382010-01-12 20:32:25 +00002710 unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
2711 unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002712 if (RefRelationship == Sema::Ref_Related &&
Chandler Carruth5535c382010-01-12 20:32:25 +00002713 (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002714 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2715 return;
2716 }
2717
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002718 // [...] If T1 is reference-related to T2 and the reference is an rvalue
Douglas Gregorb2855ad2011-01-21 00:52:42 +00002719 // reference, the initializer expression shall not be an lvalue.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002720 if (RefRelationship >= Sema::Ref_Related && !isLValueRef &&
Douglas Gregorb2855ad2011-01-21 00:52:42 +00002721 InitCategory.isLValue()) {
2722 Sequence.SetFailed(
2723 InitializationSequence::FK_RValueReferenceBindingToLValue);
2724 return;
2725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002726
Douglas Gregor20093b42009-12-09 23:02:17 +00002727 Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
2728 return;
2729}
2730
2731/// \brief Attempt character array initialization from a string literal
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732/// (C++ [dcl.init.string], C99 6.7.8).
2733static void TryStringLiteralInitialization(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00002734 const InitializedEntity &Entity,
2735 const InitializationKind &Kind,
2736 Expr *Initializer,
2737 InitializationSequence &Sequence) {
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002738 Sequence.setSequenceKind(InitializationSequence::StringInit);
Douglas Gregord6542d82009-12-22 15:35:07 +00002739 Sequence.AddStringInitStep(Entity.getType());
Douglas Gregor20093b42009-12-09 23:02:17 +00002740}
2741
Douglas Gregor20093b42009-12-09 23:02:17 +00002742/// \brief Attempt initialization by constructor (C++ [dcl.init]), which
2743/// enumerates the constructors of the initialized entity and performs overload
2744/// resolution to select the best.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002745static void TryConstructorInitialization(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00002746 const InitializedEntity &Entity,
2747 const InitializationKind &Kind,
2748 Expr **Args, unsigned NumArgs,
Douglas Gregor71d17402009-12-15 00:01:57 +00002749 QualType DestType,
Douglas Gregor20093b42009-12-09 23:02:17 +00002750 InitializationSequence &Sequence) {
Douglas Gregor2f599792010-04-02 18:24:57 +00002751 Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752
Douglas Gregor51c56d62009-12-14 20:49:26 +00002753 // Build the candidate set directly in the initialization sequence
2754 // structure, so that it will persist if we fail.
2755 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2756 CandidateSet.clear();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757
Douglas Gregor51c56d62009-12-14 20:49:26 +00002758 // Determine whether we are allowed to call explicit constructors or
2759 // explicit conversion operators.
2760 bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct ||
2761 Kind.getKind() == InitializationKind::IK_Value ||
Douglas Gregor2f599792010-04-02 18:24:57 +00002762 Kind.getKind() == InitializationKind::IK_Default);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002763
2764 // The type we're constructing needs to be complete.
2765 if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Douglas Gregor72a43bb2010-05-20 22:12:02 +00002766 Sequence.SetFailed(InitializationSequence::FK_Incomplete);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002767 return;
2768 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002769
Douglas Gregor51c56d62009-12-14 20:49:26 +00002770 // The type we're converting to is a class type. Enumerate its constructors
2771 // to see if one is suitable.
Douglas Gregor51c56d62009-12-14 20:49:26 +00002772 const RecordType *DestRecordType = DestType->getAs<RecordType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773 assert(DestRecordType && "Constructor initialization requires record type");
Douglas Gregor51c56d62009-12-14 20:49:26 +00002774 CXXRecordDecl *DestRecordDecl
2775 = cast<CXXRecordDecl>(DestRecordType->getDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776
Douglas Gregor51c56d62009-12-14 20:49:26 +00002777 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregore5eee5a2010-07-02 23:12:18 +00002778 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002779 Con != ConEnd; ++Con) {
John McCall9aa472c2010-03-19 07:35:19 +00002780 NamedDecl *D = *Con;
2781 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
Douglas Gregord1a27222010-04-24 20:54:38 +00002782 bool SuppressUserConversions = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783
Douglas Gregor51c56d62009-12-14 20:49:26 +00002784 // Find the constructor (which may be a template).
2785 CXXConstructorDecl *Constructor = 0;
John McCall9aa472c2010-03-19 07:35:19 +00002786 FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002787 if (ConstructorTmpl)
2788 Constructor = cast<CXXConstructorDecl>(
2789 ConstructorTmpl->getTemplatedDecl());
Douglas Gregord1a27222010-04-24 20:54:38 +00002790 else {
John McCall9aa472c2010-03-19 07:35:19 +00002791 Constructor = cast<CXXConstructorDecl>(D);
Douglas Gregord1a27222010-04-24 20:54:38 +00002792
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002793 // If we're performing copy initialization using a copy constructor, we
Douglas Gregord1a27222010-04-24 20:54:38 +00002794 // suppress user-defined conversions on the arguments.
2795 // FIXME: Move constructors?
2796 if (Kind.getKind() == InitializationKind::IK_Copy &&
2797 Constructor->isCopyConstructor())
2798 SuppressUserConversions = true;
2799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002800
Douglas Gregor51c56d62009-12-14 20:49:26 +00002801 if (!Constructor->isInvalidDecl() &&
Douglas Gregor99a2e602009-12-16 01:38:02 +00002802 (AllowExplicit || !Constructor->isExplicit())) {
Douglas Gregor51c56d62009-12-14 20:49:26 +00002803 if (ConstructorTmpl)
John McCall9aa472c2010-03-19 07:35:19 +00002804 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
John McCall86820f52010-01-26 01:37:31 +00002805 /*ExplicitArgs*/ 0,
Douglas Gregord1a27222010-04-24 20:54:38 +00002806 Args, NumArgs, CandidateSet,
2807 SuppressUserConversions);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002808 else
John McCall9aa472c2010-03-19 07:35:19 +00002809 S.AddOverloadCandidate(Constructor, FoundDecl,
Douglas Gregord1a27222010-04-24 20:54:38 +00002810 Args, NumArgs, CandidateSet,
2811 SuppressUserConversions);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002812 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002813 }
2814
Douglas Gregor51c56d62009-12-14 20:49:26 +00002815 SourceLocation DeclLoc = Kind.getLocation();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816
2817 // Perform overload resolution. If it fails, return the failed result.
Douglas Gregor51c56d62009-12-14 20:49:26 +00002818 OverloadCandidateSet::iterator Best;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002819 if (OverloadingResult Result
John McCall120d63c2010-08-24 20:38:10 +00002820 = CandidateSet.BestViableFunction(S, DeclLoc, Best)) {
Douglas Gregor51c56d62009-12-14 20:49:26 +00002821 Sequence.SetOverloadFailure(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002822 InitializationSequence::FK_ConstructorOverloadFailed,
Douglas Gregor51c56d62009-12-14 20:49:26 +00002823 Result);
2824 return;
2825 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002826
2827 // C++0x [dcl.init]p6:
2828 // If a program calls for the default initialization of an object
2829 // of a const-qualified type T, T shall be a class type with a
2830 // user-provided default constructor.
2831 if (Kind.getKind() == InitializationKind::IK_Default &&
2832 Entity.getType().isConstQualified() &&
2833 cast<CXXConstructorDecl>(Best->Function)->isImplicit()) {
2834 Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2835 return;
2836 }
2837
Douglas Gregor51c56d62009-12-14 20:49:26 +00002838 // Add the constructor initialization step. Any cv-qualification conversion is
2839 // subsumed by the initialization.
Douglas Gregor2f599792010-04-02 18:24:57 +00002840 Sequence.AddConstructorInitializationStep(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002841 cast<CXXConstructorDecl>(Best->Function),
John McCall9aa472c2010-03-19 07:35:19 +00002842 Best->FoundDecl.getAccess(),
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002843 DestType);
Douglas Gregor20093b42009-12-09 23:02:17 +00002844}
2845
Douglas Gregor71d17402009-12-15 00:01:57 +00002846/// \brief Attempt value initialization (C++ [dcl.init]p7).
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002847static void TryValueInitialization(Sema &S,
Douglas Gregor71d17402009-12-15 00:01:57 +00002848 const InitializedEntity &Entity,
2849 const InitializationKind &Kind,
2850 InitializationSequence &Sequence) {
2851 // C++ [dcl.init]p5:
2852 //
2853 // To value-initialize an object of type T means:
Douglas Gregord6542d82009-12-22 15:35:07 +00002854 QualType T = Entity.getType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002855
Douglas Gregor71d17402009-12-15 00:01:57 +00002856 // -- if T is an array type, then each element is value-initialized;
2857 while (const ArrayType *AT = S.Context.getAsArrayType(T))
2858 T = AT->getElementType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002859
Douglas Gregor71d17402009-12-15 00:01:57 +00002860 if (const RecordType *RT = T->getAs<RecordType>()) {
2861 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2862 // -- if T is a class type (clause 9) with a user-declared
2863 // constructor (12.1), then the default constructor for T is
2864 // called (and the initialization is ill-formed if T has no
2865 // accessible default constructor);
2866 //
2867 // FIXME: we really want to refer to a single subobject of the array,
2868 // but Entity doesn't have a way to capture that (yet).
2869 if (ClassDecl->hasUserDeclaredConstructor())
2870 return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002871
Douglas Gregor16006c92009-12-16 18:50:27 +00002872 // -- if T is a (possibly cv-qualified) non-union class type
2873 // without a user-provided constructor, then the object is
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002874 // zero-initialized and, if T's implicitly-declared default
Douglas Gregor16006c92009-12-16 18:50:27 +00002875 // constructor is non-trivial, that constructor is called.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002876 if ((ClassDecl->getTagKind() == TTK_Class ||
Douglas Gregored8abf12010-07-08 06:14:04 +00002877 ClassDecl->getTagKind() == TTK_Struct)) {
Douglas Gregord6542d82009-12-22 15:35:07 +00002878 Sequence.AddZeroInitializationStep(Entity.getType());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002879 return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
Douglas Gregor16006c92009-12-16 18:50:27 +00002880 }
Douglas Gregor71d17402009-12-15 00:01:57 +00002881 }
2882 }
2883
Douglas Gregord6542d82009-12-22 15:35:07 +00002884 Sequence.AddZeroInitializationStep(Entity.getType());
Douglas Gregor71d17402009-12-15 00:01:57 +00002885 Sequence.setSequenceKind(InitializationSequence::ZeroInitialization);
2886}
2887
Douglas Gregor99a2e602009-12-16 01:38:02 +00002888/// \brief Attempt default initialization (C++ [dcl.init]p6).
2889static void TryDefaultInitialization(Sema &S,
2890 const InitializedEntity &Entity,
2891 const InitializationKind &Kind,
2892 InitializationSequence &Sequence) {
2893 assert(Kind.getKind() == InitializationKind::IK_Default);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002894
Douglas Gregor99a2e602009-12-16 01:38:02 +00002895 // C++ [dcl.init]p6:
2896 // To default-initialize an object of type T means:
2897 // - if T is an array type, each element is default-initialized;
Douglas Gregord6542d82009-12-22 15:35:07 +00002898 QualType DestType = Entity.getType();
Douglas Gregor99a2e602009-12-16 01:38:02 +00002899 while (const ArrayType *Array = S.Context.getAsArrayType(DestType))
2900 DestType = Array->getElementType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002901
Douglas Gregor99a2e602009-12-16 01:38:02 +00002902 // - if T is a (possibly cv-qualified) class type (Clause 9), the default
2903 // constructor for T is called (and the initialization is ill-formed if
2904 // T has no accessible default constructor);
Douglas Gregor60c93c92010-02-09 07:26:29 +00002905 if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) {
Chandler Carruth4e6fbce2010-08-23 07:55:51 +00002906 TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence);
2907 return;
Douglas Gregor99a2e602009-12-16 01:38:02 +00002908 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002909
Douglas Gregor99a2e602009-12-16 01:38:02 +00002910 // - otherwise, no initialization is performed.
2911 Sequence.setSequenceKind(InitializationSequence::NoInitialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002912
Douglas Gregor99a2e602009-12-16 01:38:02 +00002913 // If a program calls for the default initialization of an object of
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002914 // a const-qualified type T, T shall be a class type with a user-provided
Douglas Gregor99a2e602009-12-16 01:38:02 +00002915 // default constructor.
Douglas Gregor60c93c92010-02-09 07:26:29 +00002916 if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus)
Douglas Gregor99a2e602009-12-16 01:38:02 +00002917 Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2918}
2919
Douglas Gregor20093b42009-12-09 23:02:17 +00002920/// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
2921/// which enumerates all conversion functions and performs overload resolution
2922/// to select the best.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002923static void TryUserDefinedConversion(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00002924 const InitializedEntity &Entity,
2925 const InitializationKind &Kind,
2926 Expr *Initializer,
2927 InitializationSequence &Sequence) {
Douglas Gregor4a520a22009-12-14 17:27:33 +00002928 Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002929
Douglas Gregord6542d82009-12-22 15:35:07 +00002930 QualType DestType = Entity.getType();
Douglas Gregor4a520a22009-12-14 17:27:33 +00002931 assert(!DestType->isReferenceType() && "References are handled elsewhere");
2932 QualType SourceType = Initializer->getType();
2933 assert((DestType->isRecordType() || SourceType->isRecordType()) &&
2934 "Must have a class type to perform a user-defined conversion");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002935
Douglas Gregor4a520a22009-12-14 17:27:33 +00002936 // Build the candidate set directly in the initialization sequence
2937 // structure, so that it will persist if we fail.
2938 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2939 CandidateSet.clear();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002940
Douglas Gregor4a520a22009-12-14 17:27:33 +00002941 // Determine whether we are allowed to call explicit constructors or
2942 // explicit conversion operators.
2943 bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002944
Douglas Gregor4a520a22009-12-14 17:27:33 +00002945 if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
2946 // The type we're converting to is a class type. Enumerate its constructors
2947 // to see if there is a suitable conversion.
2948 CXXRecordDecl *DestRecordDecl
2949 = cast<CXXRecordDecl>(DestRecordType->getDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002950
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002951 // Try to complete the type we're converting to.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002952 if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002953 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregore5eee5a2010-07-02 23:12:18 +00002954 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002955 Con != ConEnd; ++Con) {
2956 NamedDecl *D = *Con;
2957 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002958
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002959 // Find the constructor (which may be a template).
2960 CXXConstructorDecl *Constructor = 0;
2961 FunctionTemplateDecl *ConstructorTmpl
2962 = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor4a520a22009-12-14 17:27:33 +00002963 if (ConstructorTmpl)
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002964 Constructor = cast<CXXConstructorDecl>(
2965 ConstructorTmpl->getTemplatedDecl());
Douglas Gregor4712c022010-07-01 03:43:00 +00002966 else
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002967 Constructor = cast<CXXConstructorDecl>(D);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002969 if (!Constructor->isInvalidDecl() &&
2970 Constructor->isConvertingConstructor(AllowExplicit)) {
2971 if (ConstructorTmpl)
2972 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
2973 /*ExplicitArgs*/ 0,
2974 &Initializer, 1, CandidateSet,
Douglas Gregor4712c022010-07-01 03:43:00 +00002975 /*SuppressUserConversions=*/true);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002976 else
2977 S.AddOverloadCandidate(Constructor, FoundDecl,
2978 &Initializer, 1, CandidateSet,
Douglas Gregor4712c022010-07-01 03:43:00 +00002979 /*SuppressUserConversions=*/true);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002980 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002981 }
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002982 }
Douglas Gregor4a520a22009-12-14 17:27:33 +00002983 }
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002984
2985 SourceLocation DeclLoc = Initializer->getLocStart();
2986
Douglas Gregor4a520a22009-12-14 17:27:33 +00002987 if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
2988 // The type we're converting from is a class type, enumerate its conversion
2989 // functions.
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002990
Eli Friedman33c2da92009-12-20 22:12:03 +00002991 // We can only enumerate the conversion functions for a complete type; if
2992 // the type isn't complete, simply skip this step.
2993 if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
2994 CXXRecordDecl *SourceRecordDecl
2995 = cast<CXXRecordDecl>(SourceRecordType->getDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002996
John McCalleec51cf2010-01-20 00:46:10 +00002997 const UnresolvedSetImpl *Conversions
Eli Friedman33c2da92009-12-20 22:12:03 +00002998 = SourceRecordDecl->getVisibleConversionFunctions();
John McCalleec51cf2010-01-20 00:46:10 +00002999 for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003000 E = Conversions->end();
Eli Friedman33c2da92009-12-20 22:12:03 +00003001 I != E; ++I) {
3002 NamedDecl *D = *I;
3003 CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
3004 if (isa<UsingShadowDecl>(D))
3005 D = cast<UsingShadowDecl>(D)->getTargetDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003006
Eli Friedman33c2da92009-12-20 22:12:03 +00003007 FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
3008 CXXConversionDecl *Conv;
Douglas Gregor4a520a22009-12-14 17:27:33 +00003009 if (ConvTemplate)
Eli Friedman33c2da92009-12-20 22:12:03 +00003010 Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
Douglas Gregor4a520a22009-12-14 17:27:33 +00003011 else
John McCall32daa422010-03-31 01:36:47 +00003012 Conv = cast<CXXConversionDecl>(D);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003013
Eli Friedman33c2da92009-12-20 22:12:03 +00003014 if (AllowExplicit || !Conv->isExplicit()) {
3015 if (ConvTemplate)
John McCall9aa472c2010-03-19 07:35:19 +00003016 S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
John McCall86820f52010-01-26 01:37:31 +00003017 ActingDC, Initializer, DestType,
Eli Friedman33c2da92009-12-20 22:12:03 +00003018 CandidateSet);
3019 else
John McCall9aa472c2010-03-19 07:35:19 +00003020 S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
John McCall86820f52010-01-26 01:37:31 +00003021 Initializer, DestType, CandidateSet);
Eli Friedman33c2da92009-12-20 22:12:03 +00003022 }
Douglas Gregor4a520a22009-12-14 17:27:33 +00003023 }
3024 }
3025 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003026
3027 // Perform overload resolution. If it fails, return the failed result.
Douglas Gregor4a520a22009-12-14 17:27:33 +00003028 OverloadCandidateSet::iterator Best;
John McCall1d318332010-01-12 00:44:57 +00003029 if (OverloadingResult Result
Douglas Gregor8fcc5162010-09-12 08:07:23 +00003030 = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) {
Douglas Gregor4a520a22009-12-14 17:27:33 +00003031 Sequence.SetOverloadFailure(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003032 InitializationSequence::FK_UserConversionOverloadFailed,
Douglas Gregor4a520a22009-12-14 17:27:33 +00003033 Result);
3034 return;
3035 }
John McCall1d318332010-01-12 00:44:57 +00003036
Douglas Gregor4a520a22009-12-14 17:27:33 +00003037 FunctionDecl *Function = Best->Function;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003038
Douglas Gregor4a520a22009-12-14 17:27:33 +00003039 if (isa<CXXConstructorDecl>(Function)) {
3040 // Add the user-defined conversion step. Any cv-qualification conversion is
3041 // subsumed by the initialization.
John McCall9aa472c2010-03-19 07:35:19 +00003042 Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType);
Douglas Gregor4a520a22009-12-14 17:27:33 +00003043 return;
3044 }
3045
3046 // Add the user-defined conversion step that calls the conversion function.
Douglas Gregor5291c3c2010-07-13 08:18:22 +00003047 QualType ConvType = Function->getCallResultType();
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003048 if (ConvType->getAs<RecordType>()) {
3049 // If we're converting to a class type, there may be an copy if
3050 // the resulting temporary object (possible to create an object of
3051 // a base class type). That copy is not a separate conversion, so
3052 // we just make a note of the actual destination type (possibly a
3053 // base class of the type returned by the conversion function) and
3054 // let the user-defined conversion step handle the conversion.
3055 Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType);
3056 return;
3057 }
Douglas Gregor4a520a22009-12-14 17:27:33 +00003058
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003059 Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003060
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003061 // If the conversion following the call to the conversion function
3062 // is interesting, add it as a separate step.
Douglas Gregor4a520a22009-12-14 17:27:33 +00003063 if (Best->FinalConversion.First || Best->FinalConversion.Second ||
3064 Best->FinalConversion.Third) {
3065 ImplicitConversionSequence ICS;
John McCall1d318332010-01-12 00:44:57 +00003066 ICS.setStandard();
Douglas Gregor4a520a22009-12-14 17:27:33 +00003067 ICS.Standard = Best->FinalConversion;
3068 Sequence.AddConversionSequenceStep(ICS, DestType);
3069 }
Douglas Gregor20093b42009-12-09 23:02:17 +00003070}
3071
Douglas Gregor20093b42009-12-09 23:02:17 +00003072InitializationSequence::InitializationSequence(Sema &S,
3073 const InitializedEntity &Entity,
3074 const InitializationKind &Kind,
3075 Expr **Args,
John McCall5769d612010-02-08 23:07:23 +00003076 unsigned NumArgs)
3077 : FailedCandidateSet(Kind.getLocation()) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003078 ASTContext &Context = S.Context;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079
Douglas Gregor20093b42009-12-09 23:02:17 +00003080 // C++0x [dcl.init]p16:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003081 // The semantics of initializers are as follows. The destination type is
3082 // the type of the object or reference being initialized and the source
Douglas Gregor20093b42009-12-09 23:02:17 +00003083 // type is the type of the initializer expression. The source type is not
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003084 // defined when the initializer is a braced-init-list or when it is a
Douglas Gregor20093b42009-12-09 23:02:17 +00003085 // parenthesized list of expressions.
Douglas Gregord6542d82009-12-22 15:35:07 +00003086 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003087
3088 if (DestType->isDependentType() ||
3089 Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
3090 SequenceKind = DependentSequence;
3091 return;
3092 }
3093
John McCall241d5582010-12-07 22:54:16 +00003094 for (unsigned I = 0; I != NumArgs; ++I)
3095 if (Args[I]->getObjectKind() == OK_ObjCProperty)
3096 S.ConvertPropertyForRValue(Args[I]);
3097
Douglas Gregor20093b42009-12-09 23:02:17 +00003098 QualType SourceType;
3099 Expr *Initializer = 0;
Douglas Gregor99a2e602009-12-16 01:38:02 +00003100 if (NumArgs == 1) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003101 Initializer = Args[0];
3102 if (!isa<InitListExpr>(Initializer))
3103 SourceType = Initializer->getType();
3104 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003105
3106 // - If the initializer is a braced-init-list, the object is
Douglas Gregor20093b42009-12-09 23:02:17 +00003107 // list-initialized (8.5.4).
3108 if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
3109 TryListInitialization(S, Entity, Kind, InitList, *this);
Douglas Gregord87b61f2009-12-10 17:56:55 +00003110 return;
Douglas Gregor20093b42009-12-09 23:02:17 +00003111 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003112
Douglas Gregor20093b42009-12-09 23:02:17 +00003113 // - If the destination type is a reference type, see 8.5.3.
3114 if (DestType->isReferenceType()) {
3115 // C++0x [dcl.init.ref]p1:
3116 // A variable declared to be a T& or T&&, that is, "reference to type T"
3117 // (8.3.2), shall be initialized by an object, or function, of type T or
3118 // by an object that can be converted into a T.
3119 // (Therefore, multiple arguments are not permitted.)
3120 if (NumArgs != 1)
3121 SetFailed(FK_TooManyInitsForReference);
3122 else
3123 TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
3124 return;
3125 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003126
Douglas Gregor20093b42009-12-09 23:02:17 +00003127 // - If the initializer is (), the object is value-initialized.
Douglas Gregor99a2e602009-12-16 01:38:02 +00003128 if (Kind.getKind() == InitializationKind::IK_Value ||
3129 (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003130 TryValueInitialization(S, Entity, Kind, *this);
3131 return;
3132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003133
Douglas Gregor99a2e602009-12-16 01:38:02 +00003134 // Handle default initialization.
Nick Lewycky7663f392010-11-20 01:29:55 +00003135 if (Kind.getKind() == InitializationKind::IK_Default) {
Douglas Gregor99a2e602009-12-16 01:38:02 +00003136 TryDefaultInitialization(S, Entity, Kind, *this);
3137 return;
3138 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003139
John McCallce6c9b72011-02-21 07:22:22 +00003140 // - If the destination type is an array of characters, an array of
3141 // char16_t, an array of char32_t, or an array of wchar_t, and the
3142 // initializer is a string literal, see 8.5.2.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003143 // - Otherwise, if the destination type is an array, the program is
Douglas Gregor20093b42009-12-09 23:02:17 +00003144 // ill-formed.
John McCallce6c9b72011-02-21 07:22:22 +00003145 if (const ArrayType *arrayType = Context.getAsArrayType(DestType)) {
3146 if (Initializer && IsStringInit(Initializer, arrayType, Context)) {
3147 TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
3148 return;
3149 }
3150
3151 if (arrayType->getElementType()->isAnyCharacterType())
Douglas Gregor20093b42009-12-09 23:02:17 +00003152 SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
3153 else
3154 SetFailed(FK_ArrayNeedsInitList);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003155
Douglas Gregor20093b42009-12-09 23:02:17 +00003156 return;
3157 }
Eli Friedmancfdc81a2009-12-19 08:11:05 +00003158
3159 // Handle initialization in C
3160 if (!S.getLangOptions().CPlusPlus) {
3161 setSequenceKind(CAssignment);
3162 AddCAssignmentStep(DestType);
3163 return;
3164 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003165
Douglas Gregor20093b42009-12-09 23:02:17 +00003166 // - If the destination type is a (possibly cv-qualified) class type:
3167 if (DestType->isRecordType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003168 // - If the initialization is direct-initialization, or if it is
3169 // copy-initialization where the cv-unqualified version of the
3170 // source type is the same class as, or a derived class of, the
Douglas Gregor20093b42009-12-09 23:02:17 +00003171 // class of the destination, constructors are considered. [...]
3172 if (Kind.getKind() == InitializationKind::IK_Direct ||
3173 (Kind.getKind() == InitializationKind::IK_Copy &&
3174 (Context.hasSameUnqualifiedType(SourceType, DestType) ||
3175 S.IsDerivedFrom(SourceType, DestType))))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003176 TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
Douglas Gregord6542d82009-12-22 15:35:07 +00003177 Entity.getType(), *this);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003178 // - Otherwise (i.e., for the remaining copy-initialization cases),
Douglas Gregor20093b42009-12-09 23:02:17 +00003179 // user-defined conversion sequences that can convert from the source
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003180 // type to the destination type or (when a conversion function is
Douglas Gregor20093b42009-12-09 23:02:17 +00003181 // used) to a derived class thereof are enumerated as described in
3182 // 13.3.1.4, and the best one is chosen through overload resolution
3183 // (13.3).
3184 else
3185 TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
3186 return;
3187 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003188
Douglas Gregor99a2e602009-12-16 01:38:02 +00003189 if (NumArgs > 1) {
3190 SetFailed(FK_TooManyInitsForScalar);
3191 return;
3192 }
3193 assert(NumArgs == 1 && "Zero-argument case handled above");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003194
3195 // - Otherwise, if the source type is a (possibly cv-qualified) class
Douglas Gregor20093b42009-12-09 23:02:17 +00003196 // type, conversion functions are considered.
Douglas Gregor99a2e602009-12-16 01:38:02 +00003197 if (!SourceType.isNull() && SourceType->isRecordType()) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003198 TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
3199 return;
3200 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003201
Douglas Gregor20093b42009-12-09 23:02:17 +00003202 // - Otherwise, the initial value of the object being initialized is the
Douglas Gregor4a520a22009-12-14 17:27:33 +00003203 // (possibly converted) value of the initializer expression. Standard
Douglas Gregor20093b42009-12-09 23:02:17 +00003204 // conversions (Clause 4) will be used, if necessary, to convert the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003205 // initializer expression to the cv-unqualified version of the
Douglas Gregor20093b42009-12-09 23:02:17 +00003206 // destination type; no user-defined conversions are considered.
John McCall369371c2010-06-04 02:29:22 +00003207 if (S.TryImplicitConversion(*this, Entity, Initializer,
3208 /*SuppressUserConversions*/ true,
3209 /*AllowExplicitConversions*/ false,
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003210 /*InOverloadResolution*/ false,
3211 /*CStyle=*/Kind.isCStyleOrFunctionalCast()))
Douglas Gregor8e960432010-11-08 03:40:48 +00003212 {
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003213 DeclAccessPair dap;
3214 if (Initializer->getType() == Context.OverloadTy &&
3215 !S.ResolveAddressOfOverloadedFunction(Initializer
3216 , DestType, false, dap))
Douglas Gregor8e960432010-11-08 03:40:48 +00003217 SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
3218 else
3219 SetFailed(InitializationSequence::FK_ConversionFailed);
3220 }
John McCall369371c2010-06-04 02:29:22 +00003221 else
3222 setSequenceKind(StandardConversion);
Douglas Gregor20093b42009-12-09 23:02:17 +00003223}
3224
3225InitializationSequence::~InitializationSequence() {
3226 for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(),
3227 StepEnd = Steps.end();
3228 Step != StepEnd; ++Step)
3229 Step->Destroy();
3230}
3231
3232//===----------------------------------------------------------------------===//
3233// Perform initialization
3234//===----------------------------------------------------------------------===//
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003235static Sema::AssignmentAction
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003236getAssignmentAction(const InitializedEntity &Entity) {
3237 switch(Entity.getKind()) {
3238 case InitializedEntity::EK_Variable:
3239 case InitializedEntity::EK_New:
Douglas Gregora3998bd2010-12-02 21:47:04 +00003240 case InitializedEntity::EK_Exception:
3241 case InitializedEntity::EK_Base:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003242 return Sema::AA_Initializing;
3243
3244 case InitializedEntity::EK_Parameter:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245 if (Entity.getDecl() &&
Douglas Gregor688fc9b2010-04-21 23:24:10 +00003246 isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
3247 return Sema::AA_Sending;
3248
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003249 return Sema::AA_Passing;
3250
3251 case InitializedEntity::EK_Result:
3252 return Sema::AA_Returning;
3253
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003254 case InitializedEntity::EK_Temporary:
3255 // FIXME: Can we tell apart casting vs. converting?
3256 return Sema::AA_Casting;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003257
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003258 case InitializedEntity::EK_Member:
Anders Carlssond3d824d2010-01-23 04:34:47 +00003259 case InitializedEntity::EK_ArrayElement:
3260 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003261 case InitializedEntity::EK_BlockElement:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003262 return Sema::AA_Initializing;
3263 }
3264
3265 return Sema::AA_Converting;
3266}
3267
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003268/// \brief Whether we should binding a created object as a temporary when
3269/// initializing the given entity.
Douglas Gregor2f599792010-04-02 18:24:57 +00003270static bool shouldBindAsTemporary(const InitializedEntity &Entity) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003271 switch (Entity.getKind()) {
Anders Carlsson1b36a2f2010-01-24 00:19:41 +00003272 case InitializedEntity::EK_ArrayElement:
3273 case InitializedEntity::EK_Member:
Douglas Gregor2f599792010-04-02 18:24:57 +00003274 case InitializedEntity::EK_Result:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003275 case InitializedEntity::EK_New:
3276 case InitializedEntity::EK_Variable:
3277 case InitializedEntity::EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00003278 case InitializedEntity::EK_VectorElement:
Anders Carlssona508b7d2010-02-06 23:23:06 +00003279 case InitializedEntity::EK_Exception:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003280 case InitializedEntity::EK_BlockElement:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003281 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003282
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003283 case InitializedEntity::EK_Parameter:
3284 case InitializedEntity::EK_Temporary:
3285 return true;
3286 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003287
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003288 llvm_unreachable("missed an InitializedEntity kind?");
3289}
3290
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003291/// \brief Whether the given entity, when initialized with an object
3292/// created for that initialization, requires destruction.
3293static bool shouldDestroyTemporary(const InitializedEntity &Entity) {
3294 switch (Entity.getKind()) {
3295 case InitializedEntity::EK_Member:
3296 case InitializedEntity::EK_Result:
3297 case InitializedEntity::EK_New:
3298 case InitializedEntity::EK_Base:
3299 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003300 case InitializedEntity::EK_BlockElement:
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003301 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003302
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003303 case InitializedEntity::EK_Variable:
3304 case InitializedEntity::EK_Parameter:
3305 case InitializedEntity::EK_Temporary:
3306 case InitializedEntity::EK_ArrayElement:
3307 case InitializedEntity::EK_Exception:
3308 return true;
3309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003310
3311 llvm_unreachable("missed an InitializedEntity kind?");
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003312}
3313
Douglas Gregor523d46a2010-04-18 07:40:54 +00003314/// \brief Make a (potentially elidable) temporary copy of the object
3315/// provided by the given initializer by calling the appropriate copy
3316/// constructor.
3317///
3318/// \param S The Sema object used for type-checking.
3319///
Abramo Bagnara63e7d252011-01-27 19:55:10 +00003320/// \param T The type of the temporary object, which must either be
Douglas Gregor523d46a2010-04-18 07:40:54 +00003321/// the type of the initializer expression or a superclass thereof.
3322///
3323/// \param Enter The entity being initialized.
3324///
3325/// \param CurInit The initializer expression.
3326///
3327/// \param IsExtraneousCopy Whether this is an "extraneous" copy that
3328/// is permitted in C++03 (but not C++0x) when binding a reference to
3329/// an rvalue.
3330///
3331/// \returns An expression that copies the initializer expression into
3332/// a temporary object, or an error expression if a copy could not be
3333/// created.
John McCall60d7b3a2010-08-24 06:29:42 +00003334static ExprResult CopyObject(Sema &S,
Douglas Gregor8fcc5162010-09-12 08:07:23 +00003335 QualType T,
3336 const InitializedEntity &Entity,
3337 ExprResult CurInit,
3338 bool IsExtraneousCopy) {
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003339 // Determine which class type we're copying to.
Anders Carlsson1b36a2f2010-01-24 00:19:41 +00003340 Expr *CurInitExpr = (Expr *)CurInit.get();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003341 CXXRecordDecl *Class = 0;
Douglas Gregor523d46a2010-04-18 07:40:54 +00003342 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor2f599792010-04-02 18:24:57 +00003343 Class = cast<CXXRecordDecl>(Record->getDecl());
3344 if (!Class)
3345 return move(CurInit);
3346
Douglas Gregorf5d8f462011-01-21 18:05:27 +00003347 // C++0x [class.copy]p32:
Douglas Gregor2f599792010-04-02 18:24:57 +00003348 // When certain criteria are met, an implementation is allowed to
3349 // omit the copy/move construction of a class object, even if the
3350 // copy/move constructor and/or destructor for the object have
3351 // side effects. [...]
3352 // - when a temporary class object that has not been bound to a
3353 // reference (12.2) would be copied/moved to a class object
3354 // with the same cv-unqualified type, the copy/move operation
3355 // can be omitted by constructing the temporary object
3356 // directly into the target of the omitted copy/move
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003357 //
Douglas Gregor2f599792010-04-02 18:24:57 +00003358 // Note that the other three bullets are handled elsewhere. Copy
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003359 // elision for return statements and throw expressions are handled as part
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003360 // of constructor initialization, while copy elision for exception handlers
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003361 // is handled by the run-time.
John McCall558d2ab2010-09-15 10:14:12 +00003362 bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003363 SourceLocation Loc;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003364 switch (Entity.getKind()) {
3365 case InitializedEntity::EK_Result:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003366 Loc = Entity.getReturnLoc();
3367 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003368
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003369 case InitializedEntity::EK_Exception:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003370 Loc = Entity.getThrowLoc();
3371 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003372
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003373 case InitializedEntity::EK_Variable:
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003374 Loc = Entity.getDecl()->getLocation();
3375 break;
3376
Anders Carlsson1b36a2f2010-01-24 00:19:41 +00003377 case InitializedEntity::EK_ArrayElement:
3378 case InitializedEntity::EK_Member:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003379 case InitializedEntity::EK_Parameter:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003380 case InitializedEntity::EK_Temporary:
Douglas Gregor2f599792010-04-02 18:24:57 +00003381 case InitializedEntity::EK_New:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003382 case InitializedEntity::EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00003383 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003384 case InitializedEntity::EK_BlockElement:
Douglas Gregor2f599792010-04-02 18:24:57 +00003385 Loc = CurInitExpr->getLocStart();
3386 break;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003387 }
Douglas Gregorf86fcb32010-04-24 21:09:25 +00003388
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003389 // Make sure that the type we are copying is complete.
Douglas Gregorf86fcb32010-04-24 21:09:25 +00003390 if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete)))
3391 return move(CurInit);
3392
Douglas Gregorcc15f012011-01-21 19:38:21 +00003393 // Perform overload resolution using the class's copy/move constructors.
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003394 DeclContext::lookup_iterator Con, ConEnd;
John McCall5769d612010-02-08 23:07:23 +00003395 OverloadCandidateSet CandidateSet(Loc);
Douglas Gregore5eee5a2010-07-02 23:12:18 +00003396 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003397 Con != ConEnd; ++Con) {
Douglas Gregorcc15f012011-01-21 19:38:21 +00003398 // Only consider copy/move constructors and constructor templates. Per
Douglas Gregor8ff338b2010-11-12 03:34:06 +00003399 // C++0x [dcl.init]p16, second bullet to class types, this
3400 // initialization is direct-initialization.
Douglas Gregor6493cc52010-11-08 17:16:59 +00003401 CXXConstructorDecl *Constructor = 0;
3402
3403 if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) {
Douglas Gregorcc15f012011-01-21 19:38:21 +00003404 // Handle copy/moveconstructors, only.
Douglas Gregor6493cc52010-11-08 17:16:59 +00003405 if (!Constructor || Constructor->isInvalidDecl() ||
Douglas Gregorcc15f012011-01-21 19:38:21 +00003406 !Constructor->isCopyOrMoveConstructor() ||
Douglas Gregor8ff338b2010-11-12 03:34:06 +00003407 !Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
Douglas Gregor6493cc52010-11-08 17:16:59 +00003408 continue;
3409
3410 DeclAccessPair FoundDecl
3411 = DeclAccessPair::make(Constructor, Constructor->getAccess());
3412 S.AddOverloadCandidate(Constructor, FoundDecl,
3413 &CurInitExpr, 1, CandidateSet);
3414 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003415 }
Douglas Gregor6493cc52010-11-08 17:16:59 +00003416
3417 // Handle constructor templates.
3418 FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con);
3419 if (ConstructorTmpl->isInvalidDecl())
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003420 continue;
John McCall9aa472c2010-03-19 07:35:19 +00003421
Douglas Gregor6493cc52010-11-08 17:16:59 +00003422 Constructor = cast<CXXConstructorDecl>(
3423 ConstructorTmpl->getTemplatedDecl());
Douglas Gregor8ff338b2010-11-12 03:34:06 +00003424 if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
Douglas Gregor6493cc52010-11-08 17:16:59 +00003425 continue;
3426
3427 // FIXME: Do we need to limit this to copy-constructor-like
3428 // candidates?
John McCall9aa472c2010-03-19 07:35:19 +00003429 DeclAccessPair FoundDecl
Douglas Gregor6493cc52010-11-08 17:16:59 +00003430 = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess());
3431 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0,
3432 &CurInitExpr, 1, CandidateSet, true);
Douglas Gregor2f599792010-04-02 18:24:57 +00003433 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003434
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003435 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00003436 switch (CandidateSet.BestViableFunction(S, Loc, Best)) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003437 case OR_Success:
3438 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003439
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003440 case OR_No_Viable_Function:
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003441 S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext()
3442 ? diag::ext_rvalue_to_reference_temp_copy_no_viable
3443 : diag::err_temp_copy_no_viable)
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003444 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003445 << CurInitExpr->getSourceRange();
John McCall120d63c2010-08-24 20:38:10 +00003446 CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1);
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003447 if (!IsExtraneousCopy || S.isSFINAEContext())
John McCallf312b1e2010-08-26 23:41:50 +00003448 return ExprError();
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003449 return move(CurInit);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003450
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003451 case OR_Ambiguous:
3452 S.Diag(Loc, diag::err_temp_copy_ambiguous)
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003453 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003454 << CurInitExpr->getSourceRange();
John McCall120d63c2010-08-24 20:38:10 +00003455 CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1);
John McCallf312b1e2010-08-26 23:41:50 +00003456 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003457
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003458 case OR_Deleted:
3459 S.Diag(Loc, diag::err_temp_copy_deleted)
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003460 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003461 << CurInitExpr->getSourceRange();
3462 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
3463 << Best->Function->isDeleted();
John McCallf312b1e2010-08-26 23:41:50 +00003464 return ExprError();
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003465 }
3466
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003467 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function);
John McCallca0408f2010-08-23 06:44:23 +00003468 ASTOwningVector<Expr*> ConstructorArgs(S);
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003469 CurInit.release(); // Ownership transferred into MultiExprArg, below.
Douglas Gregor523d46a2010-04-18 07:40:54 +00003470
Anders Carlsson9a68a672010-04-21 18:47:17 +00003471 S.CheckConstructorAccess(Loc, Constructor, Entity,
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003472 Best->FoundDecl.getAccess(), IsExtraneousCopy);
Douglas Gregor523d46a2010-04-18 07:40:54 +00003473
3474 if (IsExtraneousCopy) {
3475 // If this is a totally extraneous copy for C++03 reference
3476 // binding purposes, just return the original initialization
Douglas Gregor2559a702010-04-18 07:57:34 +00003477 // expression. We don't generate an (elided) copy operation here
3478 // because doing so would require us to pass down a flag to avoid
3479 // infinite recursion, where each step adds another extraneous,
3480 // elidable copy.
Douglas Gregor523d46a2010-04-18 07:40:54 +00003481
Douglas Gregor2559a702010-04-18 07:57:34 +00003482 // Instantiate the default arguments of any extra parameters in
3483 // the selected copy constructor, as if we were going to create a
3484 // proper call to the copy constructor.
3485 for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) {
3486 ParmVarDecl *Parm = Constructor->getParamDecl(I);
3487 if (S.RequireCompleteType(Loc, Parm->getType(),
3488 S.PDiag(diag::err_call_incomplete_argument)))
3489 break;
3490
3491 // Build the default argument expression; we don't actually care
3492 // if this succeeds or not, because this routine will complain
3493 // if there was a problem.
3494 S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm);
3495 }
3496
Douglas Gregor523d46a2010-04-18 07:40:54 +00003497 return S.Owned(CurInitExpr);
3498 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003499
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003500 // Determine the arguments required to actually perform the
Douglas Gregor523d46a2010-04-18 07:40:54 +00003501 // constructor call (we might have derived-to-base conversions, or
3502 // the copy constructor may have default arguments).
John McCallf312b1e2010-08-26 23:41:50 +00003503 if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1),
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003504 Loc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00003505 return ExprError();
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003506
Douglas Gregorb86cf0c2010-04-25 00:55:24 +00003507 // Actually perform the constructor call.
3508 CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable,
John McCall7a1fad32010-08-24 07:32:53 +00003509 move_arg(ConstructorArgs),
3510 /*ZeroInit*/ false,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003511 CXXConstructExpr::CK_Complete,
3512 SourceRange());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003513
Douglas Gregorb86cf0c2010-04-25 00:55:24 +00003514 // If we're supposed to bind temporaries, do so.
3515 if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity))
3516 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
3517 return move(CurInit);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003518}
Douglas Gregor20093b42009-12-09 23:02:17 +00003519
Douglas Gregora41a8c52010-04-22 00:20:18 +00003520void InitializationSequence::PrintInitLocationNote(Sema &S,
3521 const InitializedEntity &Entity) {
3522 if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) {
3523 if (Entity.getDecl()->getLocation().isInvalid())
3524 return;
3525
3526 if (Entity.getDecl()->getDeclName())
3527 S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here)
3528 << Entity.getDecl()->getDeclName();
3529 else
3530 S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here);
3531 }
3532}
3533
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003534ExprResult
Douglas Gregor20093b42009-12-09 23:02:17 +00003535InitializationSequence::Perform(Sema &S,
3536 const InitializedEntity &Entity,
3537 const InitializationKind &Kind,
John McCallf312b1e2010-08-26 23:41:50 +00003538 MultiExprArg Args,
Douglas Gregord87b61f2009-12-10 17:56:55 +00003539 QualType *ResultType) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003540 if (SequenceKind == FailedSequence) {
3541 unsigned NumArgs = Args.size();
3542 Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs);
John McCallf312b1e2010-08-26 23:41:50 +00003543 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003544 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003545
Douglas Gregor20093b42009-12-09 23:02:17 +00003546 if (SequenceKind == DependentSequence) {
Douglas Gregord87b61f2009-12-10 17:56:55 +00003547 // If the declaration is a non-dependent, incomplete array type
3548 // that has an initializer, then its type will be completed once
3549 // the initializer is instantiated.
Douglas Gregord6542d82009-12-22 15:35:07 +00003550 if (ResultType && !Entity.getType()->isDependentType() &&
Douglas Gregord87b61f2009-12-10 17:56:55 +00003551 Args.size() == 1) {
Douglas Gregord6542d82009-12-22 15:35:07 +00003552 QualType DeclType = Entity.getType();
Douglas Gregord87b61f2009-12-10 17:56:55 +00003553 if (const IncompleteArrayType *ArrayT
3554 = S.Context.getAsIncompleteArrayType(DeclType)) {
3555 // FIXME: We don't currently have the ability to accurately
3556 // compute the length of an initializer list without
3557 // performing full type-checking of the initializer list
3558 // (since we have to determine where braces are implicitly
3559 // introduced and such). So, we fall back to making the array
3560 // type a dependently-sized array type with no specified
3561 // bound.
3562 if (isa<InitListExpr>((Expr *)Args.get()[0])) {
3563 SourceRange Brackets;
Douglas Gregord6542d82009-12-22 15:35:07 +00003564
Douglas Gregord87b61f2009-12-10 17:56:55 +00003565 // Scavange the location of the brackets from the entity, if we can.
Douglas Gregord6542d82009-12-22 15:35:07 +00003566 if (DeclaratorDecl *DD = Entity.getDecl()) {
3567 if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) {
3568 TypeLoc TL = TInfo->getTypeLoc();
3569 if (IncompleteArrayTypeLoc *ArrayLoc
3570 = dyn_cast<IncompleteArrayTypeLoc>(&TL))
3571 Brackets = ArrayLoc->getBracketsRange();
3572 }
Douglas Gregord87b61f2009-12-10 17:56:55 +00003573 }
3574
3575 *ResultType
3576 = S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
3577 /*NumElts=*/0,
3578 ArrayT->getSizeModifier(),
3579 ArrayT->getIndexTypeCVRQualifiers(),
3580 Brackets);
3581 }
3582
3583 }
3584 }
3585
Eli Friedman08544622009-12-22 02:35:53 +00003586 if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast())
John McCall60d7b3a2010-08-24 06:29:42 +00003587 return ExprResult(Args.release()[0]);
Douglas Gregor20093b42009-12-09 23:02:17 +00003588
Douglas Gregor67fa05b2010-02-05 07:56:11 +00003589 if (Args.size() == 0)
3590 return S.Owned((Expr *)0);
3591
Douglas Gregor20093b42009-12-09 23:02:17 +00003592 unsigned NumArgs = Args.size();
3593 return S.Owned(new (S.Context) ParenListExpr(S.Context,
3594 SourceLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003595 (Expr **)Args.release(),
Douglas Gregor20093b42009-12-09 23:02:17 +00003596 NumArgs,
3597 SourceLocation()));
3598 }
3599
Douglas Gregor99a2e602009-12-16 01:38:02 +00003600 if (SequenceKind == NoInitialization)
3601 return S.Owned((Expr *)0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003602
Douglas Gregord6542d82009-12-22 15:35:07 +00003603 QualType DestType = Entity.getType().getNonReferenceType();
3604 // FIXME: Ugly hack around the fact that Entity.getType() is not
Eli Friedmana91eb542009-12-22 02:10:53 +00003605 // the same as Entity.getDecl()->getType() in cases involving type merging,
3606 // and we want latter when it makes sense.
Douglas Gregord87b61f2009-12-10 17:56:55 +00003607 if (ResultType)
Eli Friedmana91eb542009-12-22 02:10:53 +00003608 *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() :
Douglas Gregord6542d82009-12-22 15:35:07 +00003609 Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003610
John McCall60d7b3a2010-08-24 06:29:42 +00003611 ExprResult CurInit = S.Owned((Expr *)0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003612
Douglas Gregor99a2e602009-12-16 01:38:02 +00003613 assert(!Steps.empty() && "Cannot have an empty initialization sequence");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003614
3615 // For initialization steps that start with a single initializer,
Douglas Gregor99a2e602009-12-16 01:38:02 +00003616 // grab the only argument out the Args and place it into the "current"
3617 // initializer.
3618 switch (Steps.front().Kind) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003619 case SK_ResolveAddressOfOverloadedFunction:
3620 case SK_CastDerivedToBaseRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003621 case SK_CastDerivedToBaseXValue:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003622 case SK_CastDerivedToBaseLValue:
3623 case SK_BindReference:
3624 case SK_BindReferenceToTemporary:
Douglas Gregor523d46a2010-04-18 07:40:54 +00003625 case SK_ExtraneousCopyToTemporary:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003626 case SK_UserConversion:
3627 case SK_QualificationConversionLValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003628 case SK_QualificationConversionXValue:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003629 case SK_QualificationConversionRValue:
3630 case SK_ConversionSequence:
3631 case SK_ListInitialization:
3632 case SK_CAssignment:
Eli Friedmancfdc81a2009-12-19 08:11:05 +00003633 case SK_StringInit:
John McCallf6a16482010-12-04 03:47:34 +00003634 case SK_ObjCObjectConversion: {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003635 assert(Args.size() == 1);
John McCallf6a16482010-12-04 03:47:34 +00003636 Expr *CurInitExpr = Args.get()[0];
3637 if (!CurInitExpr) return ExprError();
3638
3639 // Read from a property when initializing something with it.
3640 if (CurInitExpr->getObjectKind() == OK_ObjCProperty)
3641 S.ConvertPropertyForRValue(CurInitExpr);
3642
3643 CurInit = ExprResult(CurInitExpr);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003644 break;
John McCallf6a16482010-12-04 03:47:34 +00003645 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003646
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003647 case SK_ConstructorInitialization:
3648 case SK_ZeroInitialization:
3649 break;
Douglas Gregor20093b42009-12-09 23:02:17 +00003650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003651
3652 // Walk through the computed steps for the initialization sequence,
Douglas Gregor20093b42009-12-09 23:02:17 +00003653 // performing the specified conversions along the way.
Douglas Gregor16006c92009-12-16 18:50:27 +00003654 bool ConstructorInitRequiresZeroInit = false;
Douglas Gregor20093b42009-12-09 23:02:17 +00003655 for (step_iterator Step = step_begin(), StepEnd = step_end();
3656 Step != StepEnd; ++Step) {
3657 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003658 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003659
John McCallf6a16482010-12-04 03:47:34 +00003660 Expr *CurInitExpr = CurInit.get();
Douglas Gregor99a2e602009-12-16 01:38:02 +00003661 QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003662
Douglas Gregor20093b42009-12-09 23:02:17 +00003663 switch (Step->Kind) {
3664 case SK_ResolveAddressOfOverloadedFunction:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003665 // Overload resolution determined which function invoke; update the
Douglas Gregor20093b42009-12-09 23:02:17 +00003666 // initializer to reflect that choice.
John McCall6bb80172010-03-30 21:47:33 +00003667 S.CheckAddressOfMemberAccess(CurInitExpr, Step->Function.FoundDecl);
John McCallb697e082010-05-06 18:15:07 +00003668 S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation());
John McCallb13b7372010-02-01 03:16:54 +00003669 CurInit = S.FixOverloadedFunctionReference(move(CurInit),
John McCall6bb80172010-03-30 21:47:33 +00003670 Step->Function.FoundDecl,
John McCall9aa472c2010-03-19 07:35:19 +00003671 Step->Function.Function);
Douglas Gregor20093b42009-12-09 23:02:17 +00003672 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003673
Douglas Gregor20093b42009-12-09 23:02:17 +00003674 case SK_CastDerivedToBaseRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003675 case SK_CastDerivedToBaseXValue:
Douglas Gregor20093b42009-12-09 23:02:17 +00003676 case SK_CastDerivedToBaseLValue: {
3677 // We have a derived-to-base cast that produces either an rvalue or an
3678 // lvalue. Perform that cast.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003679
John McCallf871d0c2010-08-07 06:22:56 +00003680 CXXCastPath BasePath;
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00003681
Douglas Gregor20093b42009-12-09 23:02:17 +00003682 // Casts to inaccessible base classes are allowed with C-style casts.
3683 bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
3684 if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
3685 CurInitExpr->getLocStart(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003686 CurInitExpr->getSourceRange(),
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00003687 &BasePath, IgnoreBaseAccess))
John McCallf312b1e2010-08-26 23:41:50 +00003688 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003689
Douglas Gregor6fb745b2010-05-13 16:44:06 +00003690 if (S.BasePathInvolvesVirtualBase(BasePath)) {
3691 QualType T = SourceType;
3692 if (const PointerType *Pointer = T->getAs<PointerType>())
3693 T = Pointer->getPointeeType();
3694 if (const RecordType *RecordTy = T->getAs<RecordType>())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003695 S.MarkVTableUsed(CurInitExpr->getLocStart(),
Douglas Gregor6fb745b2010-05-13 16:44:06 +00003696 cast<CXXRecordDecl>(RecordTy->getDecl()));
3697 }
3698
John McCall5baba9d2010-08-25 10:28:54 +00003699 ExprValueKind VK =
Sebastian Redl906082e2010-07-20 04:20:21 +00003700 Step->Kind == SK_CastDerivedToBaseLValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003701 VK_LValue :
Sebastian Redl906082e2010-07-20 04:20:21 +00003702 (Step->Kind == SK_CastDerivedToBaseXValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003703 VK_XValue :
3704 VK_RValue);
John McCallf871d0c2010-08-07 06:22:56 +00003705 CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
3706 Step->Type,
John McCall2de56d12010-08-25 11:45:40 +00003707 CK_DerivedToBase,
John McCall5baba9d2010-08-25 10:28:54 +00003708 CurInit.get(),
3709 &BasePath, VK));
Douglas Gregor20093b42009-12-09 23:02:17 +00003710 break;
3711 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003712
Douglas Gregor20093b42009-12-09 23:02:17 +00003713 case SK_BindReference:
3714 if (FieldDecl *BitField = CurInitExpr->getBitField()) {
3715 // References cannot bind to bit fields (C++ [dcl.init.ref]p5).
3716 S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
Douglas Gregord6542d82009-12-22 15:35:07 +00003717 << Entity.getType().isVolatileQualified()
Douglas Gregor20093b42009-12-09 23:02:17 +00003718 << BitField->getDeclName()
3719 << CurInitExpr->getSourceRange();
3720 S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
John McCallf312b1e2010-08-26 23:41:50 +00003721 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003722 }
Anders Carlssona6fe0bf2010-01-29 02:47:33 +00003723
Anders Carlsson09380262010-01-31 17:18:49 +00003724 if (CurInitExpr->refersToVectorElement()) {
John McCall41593e32010-02-02 19:02:38 +00003725 // References cannot bind to vector elements.
Anders Carlsson09380262010-01-31 17:18:49 +00003726 S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element)
3727 << Entity.getType().isVolatileQualified()
3728 << CurInitExpr->getSourceRange();
Douglas Gregora41a8c52010-04-22 00:20:18 +00003729 PrintInitLocationNote(S, Entity);
John McCallf312b1e2010-08-26 23:41:50 +00003730 return ExprError();
Anders Carlsson09380262010-01-31 17:18:49 +00003731 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003732
Douglas Gregor20093b42009-12-09 23:02:17 +00003733 // Reference binding does not have any corresponding ASTs.
3734
3735 // Check exception specifications
3736 if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
John McCallf312b1e2010-08-26 23:41:50 +00003737 return ExprError();
Anders Carlsson3aba0932010-01-31 18:34:51 +00003738
Douglas Gregor20093b42009-12-09 23:02:17 +00003739 break;
Anders Carlsson3aba0932010-01-31 18:34:51 +00003740
Douglas Gregor20093b42009-12-09 23:02:17 +00003741 case SK_BindReferenceToTemporary:
Anders Carlssona64a8692010-02-03 16:38:03 +00003742 // Reference binding does not have any corresponding ASTs.
3743
Douglas Gregor20093b42009-12-09 23:02:17 +00003744 // Check exception specifications
3745 if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
John McCallf312b1e2010-08-26 23:41:50 +00003746 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003747
Douglas Gregor20093b42009-12-09 23:02:17 +00003748 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003749
Douglas Gregor523d46a2010-04-18 07:40:54 +00003750 case SK_ExtraneousCopyToTemporary:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003751 CurInit = CopyObject(S, Step->Type, Entity, move(CurInit),
Douglas Gregor523d46a2010-04-18 07:40:54 +00003752 /*IsExtraneousCopy=*/true);
3753 break;
3754
Douglas Gregor20093b42009-12-09 23:02:17 +00003755 case SK_UserConversion: {
3756 // We have a user-defined conversion that invokes either a constructor
3757 // or a conversion function.
John McCalldaa8e4e2010-11-15 09:13:47 +00003758 CastKind CastKind;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003759 bool IsCopy = false;
John McCall9aa472c2010-03-19 07:35:19 +00003760 FunctionDecl *Fn = Step->Function.Function;
3761 DeclAccessPair FoundFn = Step->Function.FoundDecl;
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003762 bool CreatedObject = false;
Douglas Gregorf0e0b172010-03-25 00:20:38 +00003763 bool IsLvalue = false;
John McCallb13b7372010-02-01 03:16:54 +00003764 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003765 // Build a call to the selected constructor.
John McCallca0408f2010-08-23 06:44:23 +00003766 ASTOwningVector<Expr*> ConstructorArgs(S);
Douglas Gregor20093b42009-12-09 23:02:17 +00003767 SourceLocation Loc = CurInitExpr->getLocStart();
3768 CurInit.release(); // Ownership transferred into MultiExprArg, below.
John McCallb13b7372010-02-01 03:16:54 +00003769
Douglas Gregor20093b42009-12-09 23:02:17 +00003770 // Determine the arguments required to actually perform the constructor
3771 // call.
3772 if (S.CompleteConstructorCall(Constructor,
John McCallf312b1e2010-08-26 23:41:50 +00003773 MultiExprArg(&CurInitExpr, 1),
Douglas Gregor20093b42009-12-09 23:02:17 +00003774 Loc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00003775 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003776
Douglas Gregor20093b42009-12-09 23:02:17 +00003777 // Build the an expression that constructs a temporary.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003778 CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
John McCall7a1fad32010-08-24 07:32:53 +00003779 move_arg(ConstructorArgs),
3780 /*ZeroInit*/ false,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003781 CXXConstructExpr::CK_Complete,
3782 SourceRange());
Douglas Gregor20093b42009-12-09 23:02:17 +00003783 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003784 return ExprError();
John McCallb13b7372010-02-01 03:16:54 +00003785
Anders Carlsson9a68a672010-04-21 18:47:17 +00003786 S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity,
John McCall9aa472c2010-03-19 07:35:19 +00003787 FoundFn.getAccess());
John McCallb697e082010-05-06 18:15:07 +00003788 S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003789
John McCall2de56d12010-08-25 11:45:40 +00003790 CastKind = CK_ConstructorConversion;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003791 QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
3792 if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
3793 S.IsDerivedFrom(SourceType, Class))
3794 IsCopy = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003795
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003796 CreatedObject = true;
Douglas Gregor20093b42009-12-09 23:02:17 +00003797 } else {
3798 // Build a call to the conversion function.
John McCallb13b7372010-02-01 03:16:54 +00003799 CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn);
Douglas Gregorf0e0b172010-03-25 00:20:38 +00003800 IsLvalue = Conversion->getResultType()->isLValueReferenceType();
John McCall58e6f342010-03-16 05:22:47 +00003801 S.CheckMemberOperatorAccess(Kind.getLocation(), CurInitExpr, 0,
John McCall9aa472c2010-03-19 07:35:19 +00003802 FoundFn);
John McCallb697e082010-05-06 18:15:07 +00003803 S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003804
3805 // FIXME: Should we move this initialization into a separate
Douglas Gregor20093b42009-12-09 23:02:17 +00003806 // derived-to-base conversion? I believe the answer is "no", because
3807 // we don't want to turn off access control here for c-style casts.
Douglas Gregor5fccd362010-03-03 23:55:11 +00003808 if (S.PerformObjectArgumentInitialization(CurInitExpr, /*Qualifier=*/0,
John McCall6bb80172010-03-30 21:47:33 +00003809 FoundFn, Conversion))
John McCallf312b1e2010-08-26 23:41:50 +00003810 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003811
3812 // Do a little dance to make sure that CurInit has the proper
3813 // pointer.
3814 CurInit.release();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003815
Douglas Gregor20093b42009-12-09 23:02:17 +00003816 // Build the actual call to the conversion function.
Douglas Gregorf2ae5262011-01-20 00:18:04 +00003817 CurInit = S.BuildCXXMemberCallExpr(CurInitExpr, FoundFn, Conversion);
Douglas Gregor20093b42009-12-09 23:02:17 +00003818 if (CurInit.isInvalid() || !CurInit.get())
John McCallf312b1e2010-08-26 23:41:50 +00003819 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003820
John McCall2de56d12010-08-25 11:45:40 +00003821 CastKind = CK_UserDefinedConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003822
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003823 CreatedObject = Conversion->getResultType()->isRecordType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003824 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003825
3826 bool RequiresCopy = !IsCopy &&
Douglas Gregor2f599792010-04-02 18:24:57 +00003827 getKind() != InitializationSequence::ReferenceBinding;
3828 if (RequiresCopy || shouldBindAsTemporary(Entity))
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003829 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003830 else if (CreatedObject && shouldDestroyTemporary(Entity)) {
3831 CurInitExpr = static_cast<Expr *>(CurInit.get());
3832 QualType T = CurInitExpr->getType();
3833 if (const RecordType *Record = T->getAs<RecordType>()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003834 CXXDestructorDecl *Destructor
Douglas Gregordb89f282010-07-01 22:47:18 +00003835 = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl()));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003836 S.CheckDestructorAccess(CurInitExpr->getLocStart(), Destructor,
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003837 S.PDiag(diag::err_access_dtor_temp) << T);
3838 S.MarkDeclarationReferenced(CurInitExpr->getLocStart(), Destructor);
Douglas Gregor9b623632010-10-12 23:32:35 +00003839 S.DiagnoseUseOfDecl(Destructor, CurInitExpr->getLocStart());
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003840 }
3841 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003842
Douglas Gregor20093b42009-12-09 23:02:17 +00003843 CurInitExpr = CurInit.takeAs<Expr>();
Sebastian Redl906082e2010-07-20 04:20:21 +00003844 // FIXME: xvalues
John McCallf871d0c2010-08-07 06:22:56 +00003845 CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
3846 CurInitExpr->getType(),
3847 CastKind, CurInitExpr, 0,
John McCall5baba9d2010-08-25 10:28:54 +00003848 IsLvalue ? VK_LValue : VK_RValue));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003849
Douglas Gregor2f599792010-04-02 18:24:57 +00003850 if (RequiresCopy)
Douglas Gregor523d46a2010-04-18 07:40:54 +00003851 CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity,
3852 move(CurInit), /*IsExtraneousCopy=*/false);
Sebastian Redl906082e2010-07-20 04:20:21 +00003853
Douglas Gregor20093b42009-12-09 23:02:17 +00003854 break;
3855 }
Sebastian Redl906082e2010-07-20 04:20:21 +00003856
Douglas Gregor20093b42009-12-09 23:02:17 +00003857 case SK_QualificationConversionLValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003858 case SK_QualificationConversionXValue:
3859 case SK_QualificationConversionRValue: {
Douglas Gregor20093b42009-12-09 23:02:17 +00003860 // Perform a qualification conversion; these can never go wrong.
John McCall5baba9d2010-08-25 10:28:54 +00003861 ExprValueKind VK =
Sebastian Redl906082e2010-07-20 04:20:21 +00003862 Step->Kind == SK_QualificationConversionLValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003863 VK_LValue :
Sebastian Redl906082e2010-07-20 04:20:21 +00003864 (Step->Kind == SK_QualificationConversionXValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003865 VK_XValue :
3866 VK_RValue);
John McCall2de56d12010-08-25 11:45:40 +00003867 S.ImpCastExprToType(CurInitExpr, Step->Type, CK_NoOp, VK);
Douglas Gregor20093b42009-12-09 23:02:17 +00003868 CurInit.release();
3869 CurInit = S.Owned(CurInitExpr);
3870 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00003871 }
3872
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003873 case SK_ConversionSequence: {
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003874 if (S.PerformImplicitConversion(CurInitExpr, Step->Type, *Step->ICS,
Douglas Gregora3998bd2010-12-02 21:47:04 +00003875 getAssignmentAction(Entity),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003876 Kind.isCStyleOrFunctionalCast()))
John McCallf312b1e2010-08-26 23:41:50 +00003877 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003878
Douglas Gregor20093b42009-12-09 23:02:17 +00003879 CurInit.release();
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003880 CurInit = S.Owned(CurInitExpr);
Douglas Gregor20093b42009-12-09 23:02:17 +00003881 break;
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003883
Douglas Gregord87b61f2009-12-10 17:56:55 +00003884 case SK_ListInitialization: {
3885 InitListExpr *InitList = cast<InitListExpr>(CurInitExpr);
3886 QualType Ty = Step->Type;
Douglas Gregorcb57fb92009-12-16 06:35:08 +00003887 if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty))
John McCallf312b1e2010-08-26 23:41:50 +00003888 return ExprError();
Douglas Gregord87b61f2009-12-10 17:56:55 +00003889
3890 CurInit.release();
3891 CurInit = S.Owned(InitList);
3892 break;
3893 }
Douglas Gregor51c56d62009-12-14 20:49:26 +00003894
3895 case SK_ConstructorInitialization: {
Douglas Gregord6e44a32010-04-16 22:09:46 +00003896 unsigned NumArgs = Args.size();
Douglas Gregor51c56d62009-12-14 20:49:26 +00003897 CXXConstructorDecl *Constructor
John McCall9aa472c2010-03-19 07:35:19 +00003898 = cast<CXXConstructorDecl>(Step->Function.Function);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003899
Douglas Gregor51c56d62009-12-14 20:49:26 +00003900 // Build a call to the selected constructor.
John McCallca0408f2010-08-23 06:44:23 +00003901 ASTOwningVector<Expr*> ConstructorArgs(S);
Fariborz Jahanian0a2eb562010-07-21 18:40:47 +00003902 SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid())
3903 ? Kind.getEqualLoc()
3904 : Kind.getLocation();
Chandler Carruth4e6fbce2010-08-23 07:55:51 +00003905
3906 if (Kind.getKind() == InitializationKind::IK_Default) {
3907 // Force even a trivial, implicit default constructor to be
3908 // semantically checked. We do this explicitly because we don't build
3909 // the definition for completely trivial constructors.
3910 CXXRecordDecl *ClassDecl = Constructor->getParent();
3911 assert(ClassDecl && "No parent class for constructor.");
3912 if (Constructor->isImplicit() && Constructor->isDefaultConstructor() &&
3913 ClassDecl->hasTrivialConstructor() && !Constructor->isUsed(false))
3914 S.DefineImplicitDefaultConstructor(Loc, Constructor);
3915 }
3916
Douglas Gregor51c56d62009-12-14 20:49:26 +00003917 // Determine the arguments required to actually perform the constructor
3918 // call.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003919 if (S.CompleteConstructorCall(Constructor, move(Args),
Douglas Gregor51c56d62009-12-14 20:49:26 +00003920 Loc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00003921 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003922
3923
Douglas Gregor91be6f52010-03-02 17:18:33 +00003924 if (Entity.getKind() == InitializedEntity::EK_Temporary &&
Douglas Gregord6e44a32010-04-16 22:09:46 +00003925 NumArgs != 1 && // FIXME: Hack to work around cast weirdness
Douglas Gregor91be6f52010-03-02 17:18:33 +00003926 (Kind.getKind() == InitializationKind::IK_Direct ||
3927 Kind.getKind() == InitializationKind::IK_Value)) {
3928 // An explicitly-constructed temporary, e.g., X(1, 2).
3929 unsigned NumExprs = ConstructorArgs.size();
3930 Expr **Exprs = (Expr **)ConstructorArgs.take();
Fariborz Jahanian10f8e312010-07-21 18:31:47 +00003931 S.MarkDeclarationReferenced(Loc, Constructor);
Douglas Gregor9b623632010-10-12 23:32:35 +00003932 S.DiagnoseUseOfDecl(Constructor, Loc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003933
Douglas Gregorab6677e2010-09-08 00:15:04 +00003934 TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
3935 if (!TSInfo)
3936 TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003937
Douglas Gregor91be6f52010-03-02 17:18:33 +00003938 CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context,
3939 Constructor,
Douglas Gregorab6677e2010-09-08 00:15:04 +00003940 TSInfo,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003941 Exprs,
Douglas Gregor91be6f52010-03-02 17:18:33 +00003942 NumExprs,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003943 Kind.getParenRange(),
Douglas Gregor1c63b9c2010-04-27 20:36:09 +00003944 ConstructorInitRequiresZeroInit));
Anders Carlsson72e96fd2010-05-02 22:54:08 +00003945 } else {
3946 CXXConstructExpr::ConstructionKind ConstructKind =
3947 CXXConstructExpr::CK_Complete;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003948
Anders Carlsson72e96fd2010-05-02 22:54:08 +00003949 if (Entity.getKind() == InitializedEntity::EK_Base) {
3950 ConstructKind = Entity.getBaseSpecifier()->isVirtual() ?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003951 CXXConstructExpr::CK_VirtualBase :
Anders Carlsson72e96fd2010-05-02 22:54:08 +00003952 CXXConstructExpr::CK_NonVirtualBase;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003953 }
3954
Chandler Carruth428edaf2010-10-25 08:47:36 +00003955 // Only get the parenthesis range if it is a direct construction.
3956 SourceRange parenRange =
3957 Kind.getKind() == InitializationKind::IK_Direct ?
3958 Kind.getParenRange() : SourceRange();
3959
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003960 // If the entity allows NRVO, mark the construction as elidable
3961 // unconditionally.
3962 if (Entity.allowsNRVO())
3963 CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
3964 Constructor, /*Elidable=*/true,
3965 move_arg(ConstructorArgs),
3966 ConstructorInitRequiresZeroInit,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003967 ConstructKind,
3968 parenRange);
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003969 else
3970 CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003971 Constructor,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003972 move_arg(ConstructorArgs),
3973 ConstructorInitRequiresZeroInit,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003974 ConstructKind,
3975 parenRange);
Anders Carlsson72e96fd2010-05-02 22:54:08 +00003976 }
Douglas Gregor51c56d62009-12-14 20:49:26 +00003977 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003978 return ExprError();
John McCallb13b7372010-02-01 03:16:54 +00003979
3980 // Only check access if all of that succeeded.
Anders Carlsson9a68a672010-04-21 18:47:17 +00003981 S.CheckConstructorAccess(Loc, Constructor, Entity,
John McCall9aa472c2010-03-19 07:35:19 +00003982 Step->Function.FoundDecl.getAccess());
John McCallb697e082010-05-06 18:15:07 +00003983 S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003984
Douglas Gregor2f599792010-04-02 18:24:57 +00003985 if (shouldBindAsTemporary(Entity))
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003986 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003987
Douglas Gregor51c56d62009-12-14 20:49:26 +00003988 break;
3989 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003990
Douglas Gregor71d17402009-12-15 00:01:57 +00003991 case SK_ZeroInitialization: {
Douglas Gregor16006c92009-12-16 18:50:27 +00003992 step_iterator NextStep = Step;
3993 ++NextStep;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003994 if (NextStep != StepEnd &&
Douglas Gregor16006c92009-12-16 18:50:27 +00003995 NextStep->Kind == SK_ConstructorInitialization) {
3996 // The need for zero-initialization is recorded directly into
3997 // the call to the object's constructor within the next step.
3998 ConstructorInitRequiresZeroInit = true;
3999 } else if (Kind.getKind() == InitializationKind::IK_Value &&
4000 S.getLangOptions().CPlusPlus &&
4001 !Kind.isImplicitValueInit()) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00004002 TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
4003 if (!TSInfo)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004004 TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type,
Douglas Gregorab6677e2010-09-08 00:15:04 +00004005 Kind.getRange().getBegin());
4006
4007 CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr(
4008 TSInfo->getType().getNonLValueExprType(S.Context),
4009 TSInfo,
Douglas Gregor71d17402009-12-15 00:01:57 +00004010 Kind.getRange().getEnd()));
Douglas Gregor16006c92009-12-16 18:50:27 +00004011 } else {
Douglas Gregor71d17402009-12-15 00:01:57 +00004012 CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type));
Douglas Gregor16006c92009-12-16 18:50:27 +00004013 }
Douglas Gregor71d17402009-12-15 00:01:57 +00004014 break;
4015 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004016
4017 case SK_CAssignment: {
4018 QualType SourceType = CurInitExpr->getType();
4019 Sema::AssignConvertType ConvTy =
4020 S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr);
Douglas Gregoraa037312009-12-22 07:24:36 +00004021
4022 // If this is a call, allow conversion to a transparent union.
4023 if (ConvTy != Sema::Compatible &&
4024 Entity.getKind() == InitializedEntity::EK_Parameter &&
4025 S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr)
4026 == Sema::Compatible)
4027 ConvTy = Sema::Compatible;
4028
Douglas Gregora41a8c52010-04-22 00:20:18 +00004029 bool Complained;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004030 if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
4031 Step->Type, SourceType,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004032 CurInitExpr,
Douglas Gregora41a8c52010-04-22 00:20:18 +00004033 getAssignmentAction(Entity),
4034 &Complained)) {
4035 PrintInitLocationNote(S, Entity);
John McCallf312b1e2010-08-26 23:41:50 +00004036 return ExprError();
Douglas Gregora41a8c52010-04-22 00:20:18 +00004037 } else if (Complained)
4038 PrintInitLocationNote(S, Entity);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004039
4040 CurInit.release();
4041 CurInit = S.Owned(CurInitExpr);
4042 break;
4043 }
Eli Friedmancfdc81a2009-12-19 08:11:05 +00004044
4045 case SK_StringInit: {
4046 QualType Ty = Step->Type;
John McCallfef8b342011-02-21 07:57:55 +00004047 CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty,
4048 S.Context.getAsArrayType(Ty), S);
Eli Friedmancfdc81a2009-12-19 08:11:05 +00004049 break;
4050 }
Douglas Gregor569c3162010-08-07 11:51:51 +00004051
4052 case SK_ObjCObjectConversion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004053 S.ImpCastExprToType(CurInitExpr, Step->Type,
John McCall2de56d12010-08-25 11:45:40 +00004054 CK_ObjCObjectLValueCast,
Douglas Gregor569c3162010-08-07 11:51:51 +00004055 S.CastCategory(CurInitExpr));
4056 CurInit.release();
4057 CurInit = S.Owned(CurInitExpr);
4058 break;
Douglas Gregor20093b42009-12-09 23:02:17 +00004059 }
4060 }
John McCall15d7d122010-11-11 03:21:53 +00004061
4062 // Diagnose non-fatal problems with the completed initialization.
4063 if (Entity.getKind() == InitializedEntity::EK_Member &&
4064 cast<FieldDecl>(Entity.getDecl())->isBitField())
4065 S.CheckBitFieldInitialization(Kind.getLocation(),
4066 cast<FieldDecl>(Entity.getDecl()),
4067 CurInit.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004068
Douglas Gregor20093b42009-12-09 23:02:17 +00004069 return move(CurInit);
4070}
4071
4072//===----------------------------------------------------------------------===//
4073// Diagnose initialization failures
4074//===----------------------------------------------------------------------===//
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004075bool InitializationSequence::Diagnose(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00004076 const InitializedEntity &Entity,
4077 const InitializationKind &Kind,
4078 Expr **Args, unsigned NumArgs) {
4079 if (SequenceKind != FailedSequence)
4080 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004081
Douglas Gregord6542d82009-12-22 15:35:07 +00004082 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00004083 switch (Failure) {
4084 case FK_TooManyInitsForReference:
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004085 // FIXME: Customize for the initialized entity?
4086 if (NumArgs == 0)
4087 S.Diag(Kind.getLocation(), diag::err_reference_without_init)
4088 << DestType.getNonReferenceType();
4089 else // FIXME: diagnostic below could be better!
4090 S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
4091 << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
Douglas Gregor20093b42009-12-09 23:02:17 +00004092 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004093
Douglas Gregor20093b42009-12-09 23:02:17 +00004094 case FK_ArrayNeedsInitList:
4095 case FK_ArrayNeedsInitListOrStringLiteral:
4096 S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list)
4097 << (Failure == FK_ArrayNeedsInitListOrStringLiteral);
4098 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004099
John McCall6bb80172010-03-30 21:47:33 +00004100 case FK_AddressOfOverloadFailed: {
4101 DeclAccessPair Found;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004102 S.ResolveAddressOfOverloadedFunction(Args[0],
Douglas Gregor20093b42009-12-09 23:02:17 +00004103 DestType.getNonReferenceType(),
John McCall6bb80172010-03-30 21:47:33 +00004104 true,
4105 Found);
Douglas Gregor20093b42009-12-09 23:02:17 +00004106 break;
John McCall6bb80172010-03-30 21:47:33 +00004107 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004108
Douglas Gregor20093b42009-12-09 23:02:17 +00004109 case FK_ReferenceInitOverloadFailed:
Douglas Gregor4a520a22009-12-14 17:27:33 +00004110 case FK_UserConversionOverloadFailed:
Douglas Gregor20093b42009-12-09 23:02:17 +00004111 switch (FailedOverloadResult) {
4112 case OR_Ambiguous:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004113 if (Failure == FK_UserConversionOverloadFailed)
4114 S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
4115 << Args[0]->getType() << DestType
4116 << Args[0]->getSourceRange();
4117 else
4118 S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
4119 << DestType << Args[0]->getType()
4120 << Args[0]->getSourceRange();
4121
John McCall120d63c2010-08-24 20:38:10 +00004122 FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs);
Douglas Gregor20093b42009-12-09 23:02:17 +00004123 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004124
Douglas Gregor20093b42009-12-09 23:02:17 +00004125 case OR_No_Viable_Function:
4126 S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
4127 << Args[0]->getType() << DestType.getNonReferenceType()
4128 << Args[0]->getSourceRange();
John McCall120d63c2010-08-24 20:38:10 +00004129 FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor20093b42009-12-09 23:02:17 +00004130 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004131
Douglas Gregor20093b42009-12-09 23:02:17 +00004132 case OR_Deleted: {
4133 S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
4134 << Args[0]->getType() << DestType.getNonReferenceType()
4135 << Args[0]->getSourceRange();
4136 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00004137 OverloadingResult Ovl
Douglas Gregor8fcc5162010-09-12 08:07:23 +00004138 = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best,
4139 true);
Douglas Gregor20093b42009-12-09 23:02:17 +00004140 if (Ovl == OR_Deleted) {
4141 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
4142 << Best->Function->isDeleted();
4143 } else {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004144 llvm_unreachable("Inconsistent overload resolution?");
Douglas Gregor20093b42009-12-09 23:02:17 +00004145 }
4146 break;
4147 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004148
Douglas Gregor20093b42009-12-09 23:02:17 +00004149 case OR_Success:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004150 llvm_unreachable("Conversion did not fail!");
Douglas Gregor20093b42009-12-09 23:02:17 +00004151 break;
4152 }
4153 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004154
Douglas Gregor20093b42009-12-09 23:02:17 +00004155 case FK_NonConstLValueReferenceBindingToTemporary:
4156 case FK_NonConstLValueReferenceBindingToUnrelated:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004157 S.Diag(Kind.getLocation(),
Douglas Gregor20093b42009-12-09 23:02:17 +00004158 Failure == FK_NonConstLValueReferenceBindingToTemporary
4159 ? diag::err_lvalue_reference_bind_to_temporary
4160 : diag::err_lvalue_reference_bind_to_unrelated)
Douglas Gregoref06e242010-01-29 19:39:15 +00004161 << DestType.getNonReferenceType().isVolatileQualified()
Douglas Gregor20093b42009-12-09 23:02:17 +00004162 << DestType.getNonReferenceType()
4163 << Args[0]->getType()
4164 << Args[0]->getSourceRange();
4165 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004166
Douglas Gregor20093b42009-12-09 23:02:17 +00004167 case FK_RValueReferenceBindingToLValue:
4168 S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
Douglas Gregorfb5d7ef2011-01-21 01:04:33 +00004169 << DestType.getNonReferenceType() << Args[0]->getType()
Douglas Gregor20093b42009-12-09 23:02:17 +00004170 << Args[0]->getSourceRange();
4171 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004172
Douglas Gregor20093b42009-12-09 23:02:17 +00004173 case FK_ReferenceInitDropsQualifiers:
4174 S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
4175 << DestType.getNonReferenceType()
4176 << Args[0]->getType()
4177 << Args[0]->getSourceRange();
4178 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004179
Douglas Gregor20093b42009-12-09 23:02:17 +00004180 case FK_ReferenceInitFailed:
4181 S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
4182 << DestType.getNonReferenceType()
John McCall7eb0a9e2010-11-24 05:12:34 +00004183 << Args[0]->isLValue()
Douglas Gregor20093b42009-12-09 23:02:17 +00004184 << Args[0]->getType()
4185 << Args[0]->getSourceRange();
4186 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004187
Douglas Gregor1be8eec2011-02-19 21:32:49 +00004188 case FK_ConversionFailed: {
4189 QualType FromType = Args[0]->getType();
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004190 S.Diag(Kind.getLocation(), diag::err_init_conversion_failed)
4191 << (int)Entity.getKind()
Douglas Gregor20093b42009-12-09 23:02:17 +00004192 << DestType
John McCall7eb0a9e2010-11-24 05:12:34 +00004193 << Args[0]->isLValue()
Douglas Gregor1be8eec2011-02-19 21:32:49 +00004194 << FromType
Douglas Gregor20093b42009-12-09 23:02:17 +00004195 << Args[0]->getSourceRange();
Douglas Gregord87b61f2009-12-10 17:56:55 +00004196 break;
Douglas Gregor1be8eec2011-02-19 21:32:49 +00004197 }
Douglas Gregord87b61f2009-12-10 17:56:55 +00004198 case FK_TooManyInitsForScalar: {
Douglas Gregor99a2e602009-12-16 01:38:02 +00004199 SourceRange R;
4200
4201 if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
Douglas Gregor19311e72010-09-08 21:40:08 +00004202 R = SourceRange(InitList->getInit(0)->getLocEnd(),
Douglas Gregor99a2e602009-12-16 01:38:02 +00004203 InitList->getLocEnd());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004204 else
Douglas Gregor19311e72010-09-08 21:40:08 +00004205 R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd());
Douglas Gregord87b61f2009-12-10 17:56:55 +00004206
Douglas Gregor19311e72010-09-08 21:40:08 +00004207 R.setBegin(S.PP.getLocForEndOfToken(R.getBegin()));
4208 if (Kind.isCStyleOrFunctionalCast())
4209 S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg)
4210 << R;
4211 else
4212 S.Diag(Kind.getLocation(), diag::err_excess_initializers)
4213 << /*scalar=*/2 << R;
Douglas Gregord87b61f2009-12-10 17:56:55 +00004214 break;
4215 }
4216
4217 case FK_ReferenceBindingToInitList:
4218 S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
4219 << DestType.getNonReferenceType() << Args[0]->getSourceRange();
4220 break;
4221
4222 case FK_InitListBadDestinationType:
4223 S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
4224 << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
4225 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004226
Douglas Gregor51c56d62009-12-14 20:49:26 +00004227 case FK_ConstructorOverloadFailed: {
4228 SourceRange ArgsRange;
4229 if (NumArgs)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230 ArgsRange = SourceRange(Args[0]->getLocStart(),
Douglas Gregor51c56d62009-12-14 20:49:26 +00004231 Args[NumArgs - 1]->getLocEnd());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004232
Douglas Gregor51c56d62009-12-14 20:49:26 +00004233 // FIXME: Using "DestType" for the entity we're printing is probably
4234 // bad.
4235 switch (FailedOverloadResult) {
4236 case OR_Ambiguous:
4237 S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
4238 << DestType << ArgsRange;
John McCall120d63c2010-08-24 20:38:10 +00004239 FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates,
4240 Args, NumArgs);
Douglas Gregor51c56d62009-12-14 20:49:26 +00004241 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004242
Douglas Gregor51c56d62009-12-14 20:49:26 +00004243 case OR_No_Viable_Function:
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004244 if (Kind.getKind() == InitializationKind::IK_Default &&
4245 (Entity.getKind() == InitializedEntity::EK_Base ||
4246 Entity.getKind() == InitializedEntity::EK_Member) &&
4247 isa<CXXConstructorDecl>(S.CurContext)) {
4248 // This is implicit default initialization of a member or
4249 // base within a constructor. If no viable function was
4250 // found, notify the user that she needs to explicitly
4251 // initialize this base/member.
4252 CXXConstructorDecl *Constructor
4253 = cast<CXXConstructorDecl>(S.CurContext);
4254 if (Entity.getKind() == InitializedEntity::EK_Base) {
4255 S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
4256 << Constructor->isImplicit()
4257 << S.Context.getTypeDeclType(Constructor->getParent())
4258 << /*base=*/0
4259 << Entity.getType();
4260
4261 RecordDecl *BaseDecl
4262 = Entity.getBaseSpecifier()->getType()->getAs<RecordType>()
4263 ->getDecl();
4264 S.Diag(BaseDecl->getLocation(), diag::note_previous_decl)
4265 << S.Context.getTagDeclType(BaseDecl);
4266 } else {
4267 S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
4268 << Constructor->isImplicit()
4269 << S.Context.getTypeDeclType(Constructor->getParent())
4270 << /*member=*/1
4271 << Entity.getName();
4272 S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl);
4273
4274 if (const RecordType *Record
4275 = Entity.getType()->getAs<RecordType>())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276 S.Diag(Record->getDecl()->getLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004277 diag::note_previous_decl)
4278 << S.Context.getTagDeclType(Record->getDecl());
4279 }
4280 break;
4281 }
4282
Douglas Gregor51c56d62009-12-14 20:49:26 +00004283 S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
4284 << DestType << ArgsRange;
John McCall120d63c2010-08-24 20:38:10 +00004285 FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor51c56d62009-12-14 20:49:26 +00004286 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287
Douglas Gregor51c56d62009-12-14 20:49:26 +00004288 case OR_Deleted: {
4289 S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
4290 << true << DestType << ArgsRange;
4291 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00004292 OverloadingResult Ovl
4293 = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
Douglas Gregor51c56d62009-12-14 20:49:26 +00004294 if (Ovl == OR_Deleted) {
4295 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
4296 << Best->Function->isDeleted();
4297 } else {
4298 llvm_unreachable("Inconsistent overload resolution?");
4299 }
4300 break;
4301 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004302
Douglas Gregor51c56d62009-12-14 20:49:26 +00004303 case OR_Success:
4304 llvm_unreachable("Conversion did not fail!");
4305 break;
4306 }
4307 break;
4308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004309
Douglas Gregor99a2e602009-12-16 01:38:02 +00004310 case FK_DefaultInitOfConst:
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004311 if (Entity.getKind() == InitializedEntity::EK_Member &&
4312 isa<CXXConstructorDecl>(S.CurContext)) {
4313 // This is implicit default-initialization of a const member in
4314 // a constructor. Complain that it needs to be explicitly
4315 // initialized.
4316 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext);
4317 S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor)
4318 << Constructor->isImplicit()
4319 << S.Context.getTypeDeclType(Constructor->getParent())
4320 << /*const=*/1
4321 << Entity.getName();
4322 S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl)
4323 << Entity.getName();
4324 } else {
4325 S.Diag(Kind.getLocation(), diag::err_default_init_const)
4326 << DestType << (bool)DestType->getAs<RecordType>();
4327 }
Douglas Gregor99a2e602009-12-16 01:38:02 +00004328 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004329
Douglas Gregor72a43bb2010-05-20 22:12:02 +00004330 case FK_Incomplete:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004331 S.RequireCompleteType(Kind.getLocation(), DestType,
Douglas Gregor72a43bb2010-05-20 22:12:02 +00004332 diag::err_init_incomplete_type);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004333 break;
Douglas Gregor20093b42009-12-09 23:02:17 +00004334 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004335
Douglas Gregora41a8c52010-04-22 00:20:18 +00004336 PrintInitLocationNote(S, Entity);
Douglas Gregor20093b42009-12-09 23:02:17 +00004337 return true;
4338}
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004339
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004340void InitializationSequence::dump(llvm::raw_ostream &OS) const {
4341 switch (SequenceKind) {
4342 case FailedSequence: {
4343 OS << "Failed sequence: ";
4344 switch (Failure) {
4345 case FK_TooManyInitsForReference:
4346 OS << "too many initializers for reference";
4347 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004348
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004349 case FK_ArrayNeedsInitList:
4350 OS << "array requires initializer list";
4351 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004352
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004353 case FK_ArrayNeedsInitListOrStringLiteral:
4354 OS << "array requires initializer list or string literal";
4355 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004356
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004357 case FK_AddressOfOverloadFailed:
4358 OS << "address of overloaded function failed";
4359 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004360
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004361 case FK_ReferenceInitOverloadFailed:
4362 OS << "overload resolution for reference initialization failed";
4363 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004364
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004365 case FK_NonConstLValueReferenceBindingToTemporary:
4366 OS << "non-const lvalue reference bound to temporary";
4367 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004368
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004369 case FK_NonConstLValueReferenceBindingToUnrelated:
4370 OS << "non-const lvalue reference bound to unrelated type";
4371 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004372
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004373 case FK_RValueReferenceBindingToLValue:
4374 OS << "rvalue reference bound to an lvalue";
4375 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004376
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004377 case FK_ReferenceInitDropsQualifiers:
4378 OS << "reference initialization drops qualifiers";
4379 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004380
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004381 case FK_ReferenceInitFailed:
4382 OS << "reference initialization failed";
4383 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004384
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004385 case FK_ConversionFailed:
4386 OS << "conversion failed";
4387 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004388
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004389 case FK_TooManyInitsForScalar:
4390 OS << "too many initializers for scalar";
4391 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004392
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004393 case FK_ReferenceBindingToInitList:
4394 OS << "referencing binding to initializer list";
4395 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004396
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004397 case FK_InitListBadDestinationType:
4398 OS << "initializer list for non-aggregate, non-scalar type";
4399 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004400
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004401 case FK_UserConversionOverloadFailed:
4402 OS << "overloading failed for user-defined conversion";
4403 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004404
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004405 case FK_ConstructorOverloadFailed:
4406 OS << "constructor overloading failed";
4407 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004408
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004409 case FK_DefaultInitOfConst:
4410 OS << "default initialization of a const variable";
4411 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004412
Douglas Gregor72a43bb2010-05-20 22:12:02 +00004413 case FK_Incomplete:
4414 OS << "initialization of incomplete type";
4415 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004416 }
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004417 OS << '\n';
4418 return;
4419 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004420
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004421 case DependentSequence:
4422 OS << "Dependent sequence: ";
4423 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004424
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004425 case UserDefinedConversion:
4426 OS << "User-defined conversion sequence: ";
4427 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004428
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004429 case ConstructorInitialization:
4430 OS << "Constructor initialization sequence: ";
4431 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004433 case ReferenceBinding:
4434 OS << "Reference binding: ";
4435 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004436
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004437 case ListInitialization:
4438 OS << "List initialization: ";
4439 break;
4440
4441 case ZeroInitialization:
4442 OS << "Zero initialization\n";
4443 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004444
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004445 case NoInitialization:
4446 OS << "No initialization\n";
4447 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004448
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004449 case StandardConversion:
4450 OS << "Standard conversion: ";
4451 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004452
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004453 case CAssignment:
4454 OS << "C assignment: ";
4455 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004456
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004457 case StringInit:
4458 OS << "String initialization: ";
4459 break;
4460 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004461
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004462 for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) {
4463 if (S != step_begin()) {
4464 OS << " -> ";
4465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004466
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004467 switch (S->Kind) {
4468 case SK_ResolveAddressOfOverloadedFunction:
4469 OS << "resolve address of overloaded function";
4470 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004471
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004472 case SK_CastDerivedToBaseRValue:
4473 OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")";
4474 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004475
Sebastian Redl906082e2010-07-20 04:20:21 +00004476 case SK_CastDerivedToBaseXValue:
4477 OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")";
4478 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004479
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004480 case SK_CastDerivedToBaseLValue:
4481 OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")";
4482 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004483
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004484 case SK_BindReference:
4485 OS << "bind reference to lvalue";
4486 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004487
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004488 case SK_BindReferenceToTemporary:
4489 OS << "bind reference to a temporary";
4490 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004491
Douglas Gregor523d46a2010-04-18 07:40:54 +00004492 case SK_ExtraneousCopyToTemporary:
4493 OS << "extraneous C++03 copy to temporary";
4494 break;
4495
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004496 case SK_UserConversion:
Benjamin Kramer900fc632010-04-17 09:33:03 +00004497 OS << "user-defined conversion via " << S->Function.Function;
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004498 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00004499
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004500 case SK_QualificationConversionRValue:
4501 OS << "qualification conversion (rvalue)";
4502
Sebastian Redl906082e2010-07-20 04:20:21 +00004503 case SK_QualificationConversionXValue:
4504 OS << "qualification conversion (xvalue)";
4505
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004506 case SK_QualificationConversionLValue:
4507 OS << "qualification conversion (lvalue)";
4508 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004509
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004510 case SK_ConversionSequence:
4511 OS << "implicit conversion sequence (";
4512 S->ICS->DebugPrint(); // FIXME: use OS
4513 OS << ")";
4514 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004515
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004516 case SK_ListInitialization:
4517 OS << "list initialization";
4518 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004519
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004520 case SK_ConstructorInitialization:
4521 OS << "constructor initialization";
4522 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004523
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004524 case SK_ZeroInitialization:
4525 OS << "zero initialization";
4526 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004527
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004528 case SK_CAssignment:
4529 OS << "C assignment";
4530 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004531
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004532 case SK_StringInit:
4533 OS << "string initialization";
4534 break;
Douglas Gregor569c3162010-08-07 11:51:51 +00004535
4536 case SK_ObjCObjectConversion:
4537 OS << "Objective-C object conversion";
4538 break;
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004539 }
4540 }
4541}
4542
4543void InitializationSequence::dump() const {
4544 dump(llvm::errs());
4545}
4546
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004547//===----------------------------------------------------------------------===//
4548// Initialization helper functions
4549//===----------------------------------------------------------------------===//
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004550ExprResult
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004551Sema::PerformCopyInitialization(const InitializedEntity &Entity,
4552 SourceLocation EqualLoc,
John McCall60d7b3a2010-08-24 06:29:42 +00004553 ExprResult Init) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004554 if (Init.isInvalid())
4555 return ExprError();
4556
John McCall15d7d122010-11-11 03:21:53 +00004557 Expr *InitE = Init.get();
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004558 assert(InitE && "No initialization expression?");
4559
4560 if (EqualLoc.isInvalid())
4561 EqualLoc = InitE->getLocStart();
4562
4563 InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
4564 EqualLoc);
4565 InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
4566 Init.release();
John McCallf312b1e2010-08-26 23:41:50 +00004567 return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1));
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004568}