blob: fb482bf56a1779294022e548986d69cdbd408032 [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//
Chris Lattner8b419b92009-02-24 22:48:58 +000014// This file also implements Sema::CheckInitializerTypes.
Steve Naroff0cca7492008-05-01 22:18:59 +000015//
16//===----------------------------------------------------------------------===//
17
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Designator.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Initialization.h"
20#include "clang/Sema/Lookup.h"
John McCall2d887082010-08-25 22:03:47 +000021#include "clang/Sema/SemaInternal.h"
Tanya Lattner1e1d3962010-03-07 04:17:15 +000022#include "clang/Lex/Preprocessor.h"
Steve Naroff0cca7492008-05-01 22:18:59 +000023#include "clang/AST/ASTContext.h"
John McCall7cd088e2010-08-24 07:21:54 +000024#include "clang/AST/DeclObjC.h"
Anders Carlsson2078bb92009-05-27 16:10:08 +000025#include "clang/AST/ExprCXX.h"
Chris Lattner79e079d2009-02-24 23:10:27 +000026#include "clang/AST/ExprObjC.h"
Douglas Gregord6542d82009-12-22 15:35:07 +000027#include "clang/AST/TypeLoc.h"
Douglas Gregor20093b42009-12-09 23:02:17 +000028#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc34ee5e2009-01-29 00:45:39 +000029#include <map>
Douglas Gregor05c13a32009-01-22 00:58:24 +000030using namespace clang;
Steve Naroff0cca7492008-05-01 22:18:59 +000031
Chris Lattnerdd8e0062009-02-24 22:27:37 +000032//===----------------------------------------------------------------------===//
33// Sema Initialization Checking
34//===----------------------------------------------------------------------===//
35
Chris Lattner79e079d2009-02-24 23:10:27 +000036static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
Chris Lattner8879e3b2009-02-26 23:26:43 +000037 const ArrayType *AT = Context.getAsArrayType(DeclType);
38 if (!AT) return 0;
39
Eli Friedman8718a6a2009-05-29 18:22:49 +000040 if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
41 return 0;
42
Chris Lattner8879e3b2009-02-26 23:26:43 +000043 // See if this is a string literal or @encode.
44 Init = Init->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +000045
Chris Lattner8879e3b2009-02-26 23:26:43 +000046 // Handle @encode, which is a narrow string.
47 if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
48 return Init;
49
50 // Otherwise we can only handle string literals.
51 StringLiteral *SL = dyn_cast<StringLiteral>(Init);
Chris Lattner220b6362009-02-26 23:42:47 +000052 if (SL == 0) return 0;
Eli Friedmanbb6415c2009-05-31 10:54:53 +000053
54 QualType ElemTy = Context.getCanonicalType(AT->getElementType());
Chris Lattner8879e3b2009-02-26 23:26:43 +000055 // char array can be initialized with a narrow string.
56 // Only allow char x[] = "foo"; not char x[] = L"foo";
57 if (!SL->isWide())
Eli Friedmanbb6415c2009-05-31 10:54:53 +000058 return ElemTy->isCharType() ? Init : 0;
Chris Lattner8879e3b2009-02-26 23:26:43 +000059
Eli Friedmanbb6415c2009-05-31 10:54:53 +000060 // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
61 // correction from DR343): "An array with element type compatible with a
62 // qualified or unqualified version of wchar_t may be initialized by a wide
63 // string literal, optionally enclosed in braces."
64 if (Context.typesAreCompatible(Context.getWCharType(),
65 ElemTy.getUnqualifiedType()))
Chris Lattner8879e3b2009-02-26 23:26:43 +000066 return Init;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattnerdd8e0062009-02-24 22:27:37 +000068 return 0;
69}
70
Chris Lattner79e079d2009-02-24 23:10:27 +000071static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
72 // Get the length of the string as parsed.
73 uint64_t StrLength =
74 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
75
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattner79e079d2009-02-24 23:10:27 +000077 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +000078 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Mike Stump1eb44332009-09-09 15:08:12 +000079 // C99 6.7.8p14. We have an array of character type with unknown size
Chris Lattnerdd8e0062009-02-24 22:27:37 +000080 // being initialized to a string literal.
81 llvm::APSInt ConstVal(32);
Chris Lattner19da8cd2009-02-24 23:01:39 +000082 ConstVal = StrLength;
Chris Lattnerdd8e0062009-02-24 22:27:37 +000083 // Return a new array type (C99 6.7.8p22).
John McCall46a617a2009-10-16 00:14:28 +000084 DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
85 ConstVal,
86 ArrayType::Normal, 0);
Chris Lattner19da8cd2009-02-24 23:01:39 +000087 return;
Chris Lattnerdd8e0062009-02-24 22:27:37 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Eli Friedman8718a6a2009-05-29 18:22:49 +000090 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
Mike Stump1eb44332009-09-09 15:08:12 +000091
Eli Friedman8718a6a2009-05-29 18:22:49 +000092 // C99 6.7.8p14. We have an array of character type with known size. However,
93 // the size may be smaller or larger than the string we are initializing.
94 // FIXME: Avoid truncation for 64-bit length strings.
95 if (StrLength-1 > CAT->getSize().getZExtValue())
96 S.Diag(Str->getSourceRange().getBegin(),
97 diag::warn_initializer_string_for_char_array_too_long)
98 << Str->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +000099
Eli Friedman8718a6a2009-05-29 18:22:49 +0000100 // Set the type to the actual size that we are initializing. If we have
101 // something like:
102 // char x[1] = "foo";
103 // then this will set the string literal's type to char[1].
104 Str->setType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000105}
106
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000107//===----------------------------------------------------------------------===//
108// Semantic checking for initializer lists.
109//===----------------------------------------------------------------------===//
110
Douglas Gregor9e80f722009-01-29 01:05:33 +0000111/// @brief Semantic checking for initializer lists.
112///
113/// The InitListChecker class contains a set of routines that each
114/// handle the initialization of a certain kind of entity, e.g.,
115/// arrays, vectors, struct/union types, scalars, etc. The
116/// InitListChecker itself performs a recursive walk of the subobject
117/// structure of the type to be initialized, while stepping through
118/// the initializer list one element at a time. The IList and Index
119/// parameters to each of the Check* routines contain the active
120/// (syntactic) initializer list and the index into that initializer
121/// list that represents the current initializer. Each routine is
122/// responsible for moving that Index forward as it consumes elements.
123///
124/// Each Check* routine also has a StructuredList/StructuredIndex
125/// arguments, which contains the current the "structured" (semantic)
126/// initializer list and the index into that initializer list where we
127/// are copying initializers as we map them over to the semantic
128/// list. Once we have completed our recursive walk of the subobject
129/// structure, we will have constructed a full semantic initializer
130/// list.
131///
132/// C99 designators cause changes in the initializer list traversal,
133/// because they make the initialization "jump" into a specific
134/// subobject and then continue the initialization from that
135/// point. CheckDesignatedInitializer() recursively steps into the
136/// designated subobject and manages backing out the recursion to
137/// initialize the subobjects after the one designated.
Chris Lattner8b419b92009-02-24 22:48:58 +0000138namespace {
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000139class InitListChecker {
Chris Lattner08202542009-02-24 22:50:46 +0000140 Sema &SemaRef;
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000141 bool hadError;
142 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
143 InitListExpr *FullyStructuredList;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000145 void CheckImplicitInitList(const InitializedEntity &Entity,
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000146 InitListExpr *ParentIList, QualType T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000147 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000148 unsigned &StructuredIndex,
149 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000150 void CheckExplicitInitList(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000151 InitListExpr *IList, QualType &T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000152 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000153 unsigned &StructuredIndex,
154 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000155 void CheckListElementTypes(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000156 InitListExpr *IList, QualType &DeclType,
Mike Stump1eb44332009-09-09 15:08:12 +0000157 bool SubobjectIsDesignatorContext,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000158 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000159 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000160 unsigned &StructuredIndex,
161 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000162 void CheckSubElementType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000163 InitListExpr *IList, QualType ElemType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000164 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000165 InitListExpr *StructuredList,
166 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000167 void CheckScalarType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000168 InitListExpr *IList, QualType DeclType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000169 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000170 InitListExpr *StructuredList,
171 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000172 void CheckReferenceType(const InitializedEntity &Entity,
173 InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000174 unsigned &Index,
175 InitListExpr *StructuredList,
176 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000177 void CheckVectorType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000178 InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000179 InitListExpr *StructuredList,
180 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000181 void CheckStructUnionTypes(const InitializedEntity &Entity,
Anders Carlsson2bbae5d2010-01-23 20:20:40 +0000182 InitListExpr *IList, QualType DeclType,
Mike Stump1eb44332009-09-09 15:08:12 +0000183 RecordDecl::field_iterator Field,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000184 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000185 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000186 unsigned &StructuredIndex,
187 bool TopLevelObject = false);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000188 void CheckArrayType(const InitializedEntity &Entity,
Anders Carlsson784f6992010-01-23 20:13:41 +0000189 InitListExpr *IList, QualType &DeclType,
Mike Stump1eb44332009-09-09 15:08:12 +0000190 llvm::APSInt elementIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000191 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000192 InitListExpr *StructuredList,
193 unsigned &StructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000194 bool CheckDesignatedInitializer(const InitializedEntity &Entity,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +0000195 InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +0000196 unsigned DesigIdx,
Mike Stump1eb44332009-09-09 15:08:12 +0000197 QualType &CurrentObjectType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000198 RecordDecl::field_iterator *NextField,
199 llvm::APSInt *NextElementIndex,
200 unsigned &Index,
201 InitListExpr *StructuredList,
202 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000203 bool FinishSubobjectInit,
204 bool TopLevelObject);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000205 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
206 QualType CurrentObjectType,
207 InitListExpr *StructuredList,
208 unsigned StructuredIndex,
209 SourceRange InitRange);
Douglas Gregor9e80f722009-01-29 01:05:33 +0000210 void UpdateStructuredListElement(InitListExpr *StructuredList,
211 unsigned &StructuredIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000212 Expr *expr);
213 int numArrayElements(QualType DeclType);
214 int numStructUnionElements(QualType DeclType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000215
Douglas Gregord6d37de2009-12-22 00:05:34 +0000216 void FillInValueInitForField(unsigned Init, FieldDecl *Field,
217 const InitializedEntity &ParentEntity,
218 InitListExpr *ILE, bool &RequiresSecondPass);
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000219 void FillInValueInitializations(const InitializedEntity &Entity,
220 InitListExpr *ILE, bool &RequiresSecondPass);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000221public:
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000222 InitListChecker(Sema &S, const InitializedEntity &Entity,
223 InitListExpr *IL, QualType &T);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000224 bool HadError() { return hadError; }
225
226 // @brief Retrieves the fully-structured initializer list used for
227 // semantic analysis and code generation.
228 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
229};
Chris Lattner8b419b92009-02-24 22:48:58 +0000230} // end anonymous namespace
Chris Lattner68355a52009-01-29 05:10:57 +0000231
Douglas Gregord6d37de2009-12-22 00:05:34 +0000232void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
233 const InitializedEntity &ParentEntity,
234 InitListExpr *ILE,
235 bool &RequiresSecondPass) {
236 SourceLocation Loc = ILE->getSourceRange().getBegin();
237 unsigned NumInits = ILE->getNumInits();
238 InitializedEntity MemberEntity
239 = InitializedEntity::InitializeMember(Field, &ParentEntity);
240 if (Init >= NumInits || !ILE->getInit(Init)) {
241 // FIXME: We probably don't need to handle references
242 // specially here, since value-initialization of references is
243 // handled in InitializationSequence.
244 if (Field->getType()->isReferenceType()) {
245 // C++ [dcl.init.aggr]p9:
246 // If an incomplete or empty initializer-list leaves a
247 // member of reference type uninitialized, the program is
248 // ill-formed.
249 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
250 << Field->getType()
251 << ILE->getSyntacticForm()->getSourceRange();
252 SemaRef.Diag(Field->getLocation(),
253 diag::note_uninit_reference_member);
254 hadError = true;
255 return;
256 }
257
258 InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
259 true);
260 InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
261 if (!InitSeq) {
262 InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
263 hadError = true;
264 return;
265 }
266
John McCall60d7b3a2010-08-24 06:29:42 +0000267 ExprResult MemberInit
John McCallf312b1e2010-08-26 23:41:50 +0000268 = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg());
Douglas Gregord6d37de2009-12-22 00:05:34 +0000269 if (MemberInit.isInvalid()) {
270 hadError = true;
271 return;
272 }
273
274 if (hadError) {
275 // Do nothing
276 } else if (Init < NumInits) {
277 ILE->setInit(Init, MemberInit.takeAs<Expr>());
278 } else if (InitSeq.getKind()
279 == InitializationSequence::ConstructorInitialization) {
280 // Value-initialization requires a constructor call, so
281 // extend the initializer list to include the constructor
282 // call and make a note that we'll need to take another pass
283 // through the initializer list.
Ted Kremenek709210f2010-04-13 23:39:13 +0000284 ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>());
Douglas Gregord6d37de2009-12-22 00:05:34 +0000285 RequiresSecondPass = true;
286 }
287 } else if (InitListExpr *InnerILE
288 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
289 FillInValueInitializations(MemberEntity, InnerILE,
290 RequiresSecondPass);
291}
292
Douglas Gregor4c678342009-01-28 21:54:33 +0000293/// Recursively replaces NULL values within the given initializer list
294/// with expressions that perform value-initialization of the
295/// appropriate type.
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000296void
297InitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
298 InitListExpr *ILE,
299 bool &RequiresSecondPass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000300 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregor930d8b52009-01-30 22:09:00 +0000301 "Should not have void type");
Douglas Gregor87fd7032009-02-02 17:43:21 +0000302 SourceLocation Loc = ILE->getSourceRange().getBegin();
303 if (ILE->getSyntacticForm())
304 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek6217b802009-07-29 21:53:49 +0000306 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregord6d37de2009-12-22 00:05:34 +0000307 if (RType->getDecl()->isUnion() &&
308 ILE->getInitializedFieldInUnion())
309 FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
310 Entity, ILE, RequiresSecondPass);
311 else {
312 unsigned Init = 0;
313 for (RecordDecl::field_iterator
314 Field = RType->getDecl()->field_begin(),
315 FieldEnd = RType->getDecl()->field_end();
316 Field != FieldEnd; ++Field) {
317 if (Field->isUnnamedBitfield())
318 continue;
Douglas Gregor4c678342009-01-28 21:54:33 +0000319
Douglas Gregord6d37de2009-12-22 00:05:34 +0000320 if (hadError)
Douglas Gregor87fd7032009-02-02 17:43:21 +0000321 return;
Douglas Gregord6d37de2009-12-22 00:05:34 +0000322
323 FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
324 if (hadError)
Douglas Gregor87fd7032009-02-02 17:43:21 +0000325 return;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000326
Douglas Gregord6d37de2009-12-22 00:05:34 +0000327 ++Init;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000328
Douglas Gregord6d37de2009-12-22 00:05:34 +0000329 // Only look at the first initialization of a union.
330 if (RType->getDecl()->isUnion())
331 break;
332 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000333 }
334
335 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000336 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000337
338 QualType ElementType;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000340 InitializedEntity ElementEntity = Entity;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000341 unsigned NumInits = ILE->getNumInits();
342 unsigned NumElements = NumInits;
Chris Lattner08202542009-02-24 22:50:46 +0000343 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000344 ElementType = AType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000345 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
346 NumElements = CAType->getSize().getZExtValue();
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000347 ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
348 0, Entity);
John McCall183700f2009-09-21 23:43:11 +0000349 } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000350 ElementType = VType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000351 NumElements = VType->getNumElements();
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000352 ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
353 0, Entity);
Mike Stump1eb44332009-09-09 15:08:12 +0000354 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000355 ElementType = ILE->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000357
Douglas Gregor87fd7032009-02-02 17:43:21 +0000358 for (unsigned Init = 0; Init != NumElements; ++Init) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000359 if (hadError)
360 return;
361
Anders Carlssond3d824d2010-01-23 04:34:47 +0000362 if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
363 ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000364 ElementEntity.setElementIndex(Init);
365
Douglas Gregor87fd7032009-02-02 17:43:21 +0000366 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000367 InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
368 true);
369 InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
370 if (!InitSeq) {
371 InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000372 hadError = true;
373 return;
374 }
375
John McCall60d7b3a2010-08-24 06:29:42 +0000376 ExprResult ElementInit
John McCallf312b1e2010-08-26 23:41:50 +0000377 = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg());
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000378 if (ElementInit.isInvalid()) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000379 hadError = true;
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000380 return;
381 }
382
383 if (hadError) {
384 // Do nothing
385 } else if (Init < NumInits) {
386 ILE->setInit(Init, ElementInit.takeAs<Expr>());
387 } else if (InitSeq.getKind()
388 == InitializationSequence::ConstructorInitialization) {
389 // Value-initialization requires a constructor call, so
390 // extend the initializer list to include the constructor
391 // call and make a note that we'll need to take another pass
392 // through the initializer list.
Ted Kremenek709210f2010-04-13 23:39:13 +0000393 ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>());
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000394 RequiresSecondPass = true;
395 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000396 } else if (InitListExpr *InnerILE
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000397 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
398 FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
Douglas Gregor4c678342009-01-28 21:54:33 +0000399 }
400}
401
Chris Lattner68355a52009-01-29 05:10:57 +0000402
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000403InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
404 InitListExpr *IL, QualType &T)
Chris Lattner08202542009-02-24 22:50:46 +0000405 : SemaRef(S) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000406 hadError = false;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000407
Eli Friedmanb85f7072008-05-19 19:16:24 +0000408 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000409 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000410 FullyStructuredList
Douglas Gregored8a93d2009-03-01 17:12:46 +0000411 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000412 CheckExplicitInitList(Entity, IL, T, newIndex,
Anders Carlsson46f46592010-01-23 19:55:29 +0000413 FullyStructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000414 /*TopLevelObject=*/true);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000415
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000416 if (!hadError) {
417 bool RequiresSecondPass = false;
418 FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
Douglas Gregor16006c92009-12-16 18:50:27 +0000419 if (RequiresSecondPass && !hadError)
Douglas Gregorcb57fb92009-12-16 06:35:08 +0000420 FillInValueInitializations(Entity, FullyStructuredList,
421 RequiresSecondPass);
422 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000423}
424
425int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +0000426 // FIXME: use a proper constant
427 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000428 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000429 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000430 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
431 }
432 return maxElements;
433}
434
435int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000436 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor4c678342009-01-28 21:54:33 +0000437 int InitializableMembers = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000438 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000439 Field = structDecl->field_begin(),
440 FieldEnd = structDecl->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000441 Field != FieldEnd; ++Field) {
442 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
443 ++InitializableMembers;
444 }
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000445 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +0000446 return std::min(InitializableMembers, 1);
447 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-05-01 22:18:59 +0000448}
449
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000450void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000451 InitListExpr *ParentIList,
Douglas Gregor4c678342009-01-28 21:54:33 +0000452 QualType T, unsigned &Index,
453 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000454 unsigned &StructuredIndex,
455 bool TopLevelObject) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000456 int maxElements = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Steve Naroff0cca7492008-05-01 22:18:59 +0000458 if (T->isArrayType())
459 maxElements = numArrayElements(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +0000460 else if (T->isRecordType())
Steve Naroff0cca7492008-05-01 22:18:59 +0000461 maxElements = numStructUnionElements(T);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000462 else if (T->isVectorType())
John McCall183700f2009-09-21 23:43:11 +0000463 maxElements = T->getAs<VectorType>()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +0000464 else
465 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +0000466
Eli Friedman402256f2008-05-25 13:49:22 +0000467 if (maxElements == 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000468 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedman402256f2008-05-25 13:49:22 +0000469 diag::err_implicit_empty_initializer);
Douglas Gregor4c678342009-01-28 21:54:33 +0000470 ++Index;
Eli Friedman402256f2008-05-25 13:49:22 +0000471 hadError = true;
472 return;
473 }
474
Douglas Gregor4c678342009-01-28 21:54:33 +0000475 // Build a structured initializer list corresponding to this subobject.
476 InitListExpr *StructuredSubobjectInitList
Mike Stump1eb44332009-09-09 15:08:12 +0000477 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
478 StructuredIndex,
Douglas Gregored8a93d2009-03-01 17:12:46 +0000479 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
480 ParentIList->getSourceRange().getEnd()));
Douglas Gregor4c678342009-01-28 21:54:33 +0000481 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000482
Douglas Gregor4c678342009-01-28 21:54:33 +0000483 // Check the element types and build the structural subobject.
Douglas Gregor87fd7032009-02-02 17:43:21 +0000484 unsigned StartIndex = Index;
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000485 CheckListElementTypes(Entity, ParentIList, T,
486 /*SubobjectIsDesignatorContext=*/false, Index,
Mike Stump1eb44332009-09-09 15:08:12 +0000487 StructuredSubobjectInitList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000488 StructuredSubobjectInitIndex,
489 TopLevelObject);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000490 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregora6457962009-03-20 00:32:56 +0000491 StructuredSubobjectInitList->setType(T);
492
Douglas Gregored8a93d2009-03-01 17:12:46 +0000493 // Update the structured sub-object initializer so that it's ending
Douglas Gregor87fd7032009-02-02 17:43:21 +0000494 // range corresponds with the end of the last initializer it used.
495 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000496 SourceLocation EndLoc
Douglas Gregor87fd7032009-02-02 17:43:21 +0000497 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
498 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
499 }
Tanya Lattner1e1d3962010-03-07 04:17:15 +0000500
501 // Warn about missing braces.
502 if (T->isArrayType() || T->isRecordType()) {
Tanya Lattner47f164e2010-03-07 04:40:06 +0000503 SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
504 diag::warn_missing_braces)
Tanya Lattner1e1d3962010-03-07 04:17:15 +0000505 << StructuredSubobjectInitList->getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000506 << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(),
507 "{")
508 << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken(
Tanya Lattner1dcd0612010-03-07 04:47:12 +0000509 StructuredSubobjectInitList->getLocEnd()),
Douglas Gregor849b2432010-03-31 17:46:05 +0000510 "}");
Tanya Lattner1e1d3962010-03-07 04:17:15 +0000511 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000512}
513
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000514void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000515 InitListExpr *IList, QualType &T,
Douglas Gregor4c678342009-01-28 21:54:33 +0000516 unsigned &Index,
517 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000518 unsigned &StructuredIndex,
519 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000520 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor4c678342009-01-28 21:54:33 +0000521 SyntacticToSemantic[IList] = StructuredList;
522 StructuredList->setSyntacticForm(IList);
Anders Carlsson46f46592010-01-23 19:55:29 +0000523 CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true,
524 Index, StructuredList, StructuredIndex, TopLevelObject);
Douglas Gregor63982352010-07-13 18:40:04 +0000525 QualType ExprTy = T.getNonLValueExprType(SemaRef.Context);
526 IList->setType(ExprTy);
527 StructuredList->setType(ExprTy);
Eli Friedman638e1442008-05-25 13:22:35 +0000528 if (hadError)
529 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000530
Eli Friedman638e1442008-05-25 13:22:35 +0000531 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000532 // We have leftover initializers
Eli Friedmane5408582009-05-29 20:20:05 +0000533 if (StructuredIndex == 1 &&
534 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000535 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000536 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000537 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000538 hadError = true;
539 }
Eli Friedmanbb504d32008-05-19 20:12:18 +0000540 // Special-case
Chris Lattner08202542009-02-24 22:50:46 +0000541 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000542 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8dc2102008-05-20 05:25:56 +0000543 } else if (!T->isIncompleteType()) {
Douglas Gregorb574e562009-01-30 22:26:29 +0000544 // Don't complain for incomplete types, since we'll get an error
545 // elsewhere
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000546 QualType CurrentObjectType = StructuredList->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000547 int initKind =
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000548 CurrentObjectType->isArrayType()? 0 :
549 CurrentObjectType->isVectorType()? 1 :
550 CurrentObjectType->isScalarType()? 2 :
551 CurrentObjectType->isUnionType()? 3 :
552 4;
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000553
554 unsigned DK = diag::warn_excess_initializers;
Eli Friedmane5408582009-05-29 20:20:05 +0000555 if (SemaRef.getLangOptions().CPlusPlus) {
556 DK = diag::err_excess_initializers;
557 hadError = true;
558 }
Nate Begeman08634522009-07-07 21:53:06 +0000559 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
560 DK = diag::err_excess_initializers;
561 hadError = true;
562 }
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000563
Chris Lattner08202542009-02-24 22:50:46 +0000564 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000565 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000566 }
567 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000568
Eli Friedman759f2522009-05-16 11:45:48 +0000569 if (T->isScalarType() && !TopLevelObject)
Chris Lattner08202542009-02-24 22:50:46 +0000570 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregora3a83512009-04-01 23:51:29 +0000571 << IList->getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000572 << FixItHint::CreateRemoval(IList->getLocStart())
573 << FixItHint::CreateRemoval(IList->getLocEnd());
Steve Naroff0cca7492008-05-01 22:18:59 +0000574}
575
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000576void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000577 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000578 QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000579 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000580 unsigned &Index,
581 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000582 unsigned &StructuredIndex,
583 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000584 if (DeclType->isScalarType()) {
Anders Carlsson46f46592010-01-23 19:55:29 +0000585 CheckScalarType(Entity, IList, DeclType, Index,
586 StructuredList, StructuredIndex);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000587 } else if (DeclType->isVectorType()) {
Anders Carlsson46f46592010-01-23 19:55:29 +0000588 CheckVectorType(Entity, IList, DeclType, Index,
589 StructuredList, StructuredIndex);
Douglas Gregord7eb8462009-01-30 17:31:00 +0000590 } else if (DeclType->isAggregateType()) {
591 if (DeclType->isRecordType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000592 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Anders Carlsson2bbae5d2010-01-23 20:20:40 +0000593 CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000594 SubobjectIsDesignatorContext, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000595 StructuredList, StructuredIndex,
596 TopLevelObject);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000597 } else if (DeclType->isArrayType()) {
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000598 llvm::APSInt Zero(
Chris Lattner08202542009-02-24 22:50:46 +0000599 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000600 false);
Anders Carlsson784f6992010-01-23 20:13:41 +0000601 CheckArrayType(Entity, IList, DeclType, Zero,
602 SubobjectIsDesignatorContext, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000603 StructuredList, StructuredIndex);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000604 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000605 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000606 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
607 // This type is invalid, issue a diagnostic.
Douglas Gregor4c678342009-01-28 21:54:33 +0000608 ++Index;
Chris Lattner08202542009-02-24 22:50:46 +0000609 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000610 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000611 hadError = true;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000612 } else if (DeclType->isRecordType()) {
613 // C++ [dcl.init]p14:
614 // [...] If the class is an aggregate (8.5.1), and the initializer
615 // is a brace-enclosed list, see 8.5.1.
616 //
617 // Note: 8.5.1 is handled below; here, we diagnose the case where
618 // we have an initializer list and a destination type that is not
619 // an aggregate.
620 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner08202542009-02-24 22:50:46 +0000621 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000622 << DeclType << IList->getSourceRange();
623 hadError = true;
624 } else if (DeclType->isReferenceType()) {
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000625 CheckReferenceType(Entity, IList, DeclType, Index,
626 StructuredList, StructuredIndex);
John McCallc12c5bb2010-05-15 11:32:37 +0000627 } else if (DeclType->isObjCObjectType()) {
Douglas Gregor4d9e7382010-05-03 18:24:37 +0000628 SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
629 << DeclType;
630 hadError = true;
Steve Naroff0cca7492008-05-01 22:18:59 +0000631 } else {
Douglas Gregor4d9e7382010-05-03 18:24:37 +0000632 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
633 << DeclType;
634 hadError = true;
Steve Naroff0cca7492008-05-01 22:18:59 +0000635 }
636}
637
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000638void InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000639 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000640 QualType ElemType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000641 unsigned &Index,
642 InitListExpr *StructuredList,
643 unsigned &StructuredIndex) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000644 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000645 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
646 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000647 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000648 InitListExpr *newStructuredList
Douglas Gregor4c678342009-01-28 21:54:33 +0000649 = getStructuredSubobjectInit(IList, Index, ElemType,
650 StructuredList, StructuredIndex,
651 SubInitList->getSourceRange());
Anders Carlsson46f46592010-01-23 19:55:29 +0000652 CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +0000653 newStructuredList, newStructuredIndex);
654 ++StructuredIndex;
655 ++Index;
Chris Lattner79e079d2009-02-24 23:10:27 +0000656 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
657 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000658 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor4c678342009-01-28 21:54:33 +0000659 ++Index;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000660 } else if (ElemType->isScalarType()) {
Anders Carlsson46f46592010-01-23 19:55:29 +0000661 CheckScalarType(Entity, IList, ElemType, Index,
662 StructuredList, StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000663 } else if (ElemType->isReferenceType()) {
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000664 CheckReferenceType(Entity, IList, ElemType, Index,
665 StructuredList, StructuredIndex);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000666 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000667 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000668 // C++ [dcl.init.aggr]p12:
669 // All implicit type conversions (clause 4) are considered when
670 // initializing the aggregate member with an ini- tializer from
671 // an initializer-list. If the initializer can initialize a
672 // member, the member is initialized. [...]
Anders Carlssond28b4282009-08-27 17:18:13 +0000673
Anders Carlsson1b36a2f2010-01-24 00:19:41 +0000674 // FIXME: Better EqualLoc?
675 InitializationKind Kind =
676 InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation());
677 InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1);
678
679 if (Seq) {
John McCall60d7b3a2010-08-24 06:29:42 +0000680 ExprResult Result =
John McCallf312b1e2010-08-26 23:41:50 +0000681 Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1));
Anders Carlsson1b36a2f2010-01-24 00:19:41 +0000682 if (Result.isInvalid())
Douglas Gregor930d8b52009-01-30 22:09:00 +0000683 hadError = true;
Anders Carlsson1b36a2f2010-01-24 00:19:41 +0000684
685 UpdateStructuredListElement(StructuredList, StructuredIndex,
686 Result.takeAs<Expr>());
Douglas Gregor930d8b52009-01-30 22:09:00 +0000687 ++Index;
688 return;
689 }
690
691 // Fall through for subaggregate initialization
692 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000693 // C99 6.7.8p13:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000694 //
695 // The initializer for a structure or union object that has
696 // automatic storage duration shall be either an initializer
697 // list as described below, or a single expression that has
698 // compatible structure or union type. In the latter case, the
699 // initial value of the object, including unnamed members, is
700 // that of the expression.
Eli Friedman6b5374f2009-06-13 10:38:46 +0000701 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
Eli Friedman8718a6a2009-05-29 18:22:49 +0000702 SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000703 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
704 ++Index;
705 return;
706 }
707
708 // Fall through for subaggregate initialization
709 }
710
711 // C++ [dcl.init.aggr]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000712 //
Douglas Gregor930d8b52009-01-30 22:09:00 +0000713 // [...] Otherwise, if the member is itself a non-empty
714 // subaggregate, brace elision is assumed and the initializer is
715 // considered for the initialization of the first member of
716 // the subaggregate.
717 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
Anders Carlsson987dc6a2010-01-23 20:47:59 +0000718 CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000719 StructuredIndex);
720 ++StructuredIndex;
721 } else {
722 // We cannot initialize this element, so let
723 // PerformCopyInitialization produce the appropriate diagnostic.
Anders Carlssonca755fe2010-01-30 01:56:32 +0000724 SemaRef.PerformCopyInitialization(Entity, SourceLocation(),
725 SemaRef.Owned(expr));
726 IList->setInit(Index, 0);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000727 hadError = true;
728 ++Index;
729 ++StructuredIndex;
730 }
731 }
Eli Friedmanb85f7072008-05-19 19:16:24 +0000732}
733
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000734void InitListChecker::CheckScalarType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000735 InitListExpr *IList, QualType DeclType,
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000736 unsigned &Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000737 InitListExpr *StructuredList,
738 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000739 if (Index < IList->getNumInits()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000740 Expr *expr = IList->getInit(Index);
Eli Friedman09865a92010-08-14 03:14:53 +0000741 if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) {
742 SemaRef.Diag(SubIList->getLocStart(),
743 diag::warn_many_braces_around_scalar_init)
744 << SubIList->getSourceRange();
745
746 CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList,
747 StructuredIndex);
Eli Friedmanbb504d32008-05-19 20:12:18 +0000748 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000749 } else if (isa<DesignatedInitExpr>(expr)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000750 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor05c13a32009-01-22 00:58:24 +0000751 diag::err_designator_for_scalar_init)
752 << DeclType << expr->getSourceRange();
753 hadError = true;
754 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000755 ++StructuredIndex;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000756 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000757 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000758
John McCall60d7b3a2010-08-24 06:29:42 +0000759 ExprResult Result =
Eli Friedmana1635d92010-01-25 17:04:54 +0000760 SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
761 SemaRef.Owned(expr));
Anders Carlssonc07b8c02010-01-23 18:35:41 +0000762
Chandler Carruthb5719242010-02-13 07:23:01 +0000763 Expr *ResultExpr = 0;
Anders Carlssonc07b8c02010-01-23 18:35:41 +0000764
765 if (Result.isInvalid())
Eli Friedmanbb504d32008-05-19 20:12:18 +0000766 hadError = true; // types weren't compatible.
Anders Carlssonc07b8c02010-01-23 18:35:41 +0000767 else {
768 ResultExpr = Result.takeAs<Expr>();
769
770 if (ResultExpr != expr) {
771 // The type was promoted, update initializer list.
772 IList->setInit(Index, ResultExpr);
773 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000774 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000775 if (hadError)
776 ++StructuredIndex;
777 else
Anders Carlssonc07b8c02010-01-23 18:35:41 +0000778 UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
Steve Naroff0cca7492008-05-01 22:18:59 +0000779 ++Index;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000780 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000781 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000782 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000783 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000784 ++Index;
785 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000786 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000787 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000788}
789
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000790void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
791 InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000792 unsigned &Index,
793 InitListExpr *StructuredList,
794 unsigned &StructuredIndex) {
795 if (Index < IList->getNumInits()) {
796 Expr *expr = IList->getInit(Index);
797 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000798 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000799 << DeclType << IList->getSourceRange();
800 hadError = true;
801 ++Index;
802 ++StructuredIndex;
803 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000804 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000805
John McCall60d7b3a2010-08-24 06:29:42 +0000806 ExprResult Result =
Anders Carlssona6fe0bf2010-01-29 02:47:33 +0000807 SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
808 SemaRef.Owned(expr));
809
810 if (Result.isInvalid())
Douglas Gregor930d8b52009-01-30 22:09:00 +0000811 hadError = true;
Anders Carlssona6fe0bf2010-01-29 02:47:33 +0000812
813 expr = Result.takeAs<Expr>();
814 IList->setInit(Index, expr);
815
Douglas Gregor930d8b52009-01-30 22:09:00 +0000816 if (hadError)
817 ++StructuredIndex;
818 else
819 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
820 ++Index;
821 } else {
Mike Stump390b4cc2009-05-16 07:39:55 +0000822 // FIXME: It would be wonderful if we could point at the actual member. In
823 // general, it would be useful to pass location information down the stack,
824 // so that we know the location (or decl) of the "current object" being
825 // initialized.
Mike Stump1eb44332009-09-09 15:08:12 +0000826 SemaRef.Diag(IList->getLocStart(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000827 diag::err_init_reference_member_uninitialized)
828 << DeclType
829 << IList->getSourceRange();
830 hadError = true;
831 ++Index;
832 ++StructuredIndex;
833 return;
834 }
835}
836
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000837void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
Anders Carlsson46f46592010-01-23 19:55:29 +0000838 InitListExpr *IList, QualType DeclType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000839 unsigned &Index,
840 InitListExpr *StructuredList,
841 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000842 if (Index < IList->getNumInits()) {
John McCall183700f2009-09-21 23:43:11 +0000843 const VectorType *VT = DeclType->getAs<VectorType>();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000844 unsigned maxElements = VT->getNumElements();
845 unsigned numEltsInit = 0;
Steve Naroff0cca7492008-05-01 22:18:59 +0000846 QualType elementType = VT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Nate Begeman2ef13e52009-08-10 23:49:36 +0000848 if (!SemaRef.getLangOptions().OpenCL) {
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000849 InitializedEntity ElementEntity =
850 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
Anders Carlsson46f46592010-01-23 19:55:29 +0000851
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000852 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
853 // Don't attempt to go past the end of the init list
854 if (Index >= IList->getNumInits())
855 break;
Anders Carlsson46f46592010-01-23 19:55:29 +0000856
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000857 ElementEntity.setElementIndex(Index);
858 CheckSubElementType(ElementEntity, IList, elementType, Index,
859 StructuredList, StructuredIndex);
860 }
Nate Begeman2ef13e52009-08-10 23:49:36 +0000861 } else {
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000862 InitializedEntity ElementEntity =
863 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
864
Nate Begeman2ef13e52009-08-10 23:49:36 +0000865 // OpenCL initializers allows vectors to be constructed from vectors.
866 for (unsigned i = 0; i < maxElements; ++i) {
867 // Don't attempt to go past the end of the init list
868 if (Index >= IList->getNumInits())
869 break;
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000870
871 ElementEntity.setElementIndex(Index);
872
Nate Begeman2ef13e52009-08-10 23:49:36 +0000873 QualType IType = IList->getInit(Index)->getType();
874 if (!IType->isVectorType()) {
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000875 CheckSubElementType(ElementEntity, IList, elementType, Index,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000876 StructuredList, StructuredIndex);
877 ++numEltsInit;
878 } else {
Nate Begeman3e315522010-07-07 22:26:56 +0000879 QualType VecType;
John McCall183700f2009-09-21 23:43:11 +0000880 const VectorType *IVT = IType->getAs<VectorType>();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000881 unsigned numIElts = IVT->getNumElements();
Nate Begeman3e315522010-07-07 22:26:56 +0000882
883 if (IType->isExtVectorType())
884 VecType = SemaRef.Context.getExtVectorType(elementType, numIElts);
885 else
886 VecType = SemaRef.Context.getVectorType(elementType, numIElts,
887 IVT->getAltiVecSpecific());
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000888 CheckSubElementType(ElementEntity, IList, VecType, Index,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000889 StructuredList, StructuredIndex);
890 numEltsInit += numIElts;
891 }
892 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
John Thompsonf3afbea2010-04-20 23:21:17 +0000895 // OpenCL requires all elements to be initialized.
Nate Begeman2ef13e52009-08-10 23:49:36 +0000896 if (numEltsInit != maxElements)
Chris Lattnere12a7792010-04-20 05:19:10 +0000897 if (SemaRef.getLangOptions().OpenCL)
Nate Begeman2ef13e52009-08-10 23:49:36 +0000898 SemaRef.Diag(IList->getSourceRange().getBegin(),
899 diag::err_vector_incorrect_num_initializers)
900 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Naroff0cca7492008-05-01 22:18:59 +0000901 }
902}
903
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000904void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
Anders Carlsson784f6992010-01-23 20:13:41 +0000905 InitListExpr *IList, QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000906 llvm::APSInt elementIndex,
Mike Stump1eb44332009-09-09 15:08:12 +0000907 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000908 unsigned &Index,
909 InitListExpr *StructuredList,
910 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000911 // Check for the special-case of initializing an array with a string.
912 if (Index < IList->getNumInits()) {
Chris Lattner79e079d2009-02-24 23:10:27 +0000913 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
914 SemaRef.Context)) {
915 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor4c678342009-01-28 21:54:33 +0000916 // We place the string literal directly into the resulting
917 // initializer list. This is the only place where the structure
918 // of the structured initializer list doesn't match exactly,
919 // because doing so would involve allocating one character
920 // constant for each string.
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000921 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner08202542009-02-24 22:50:46 +0000922 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000923 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000924 return;
925 }
926 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000927 if (const VariableArrayType *VAT =
Chris Lattner08202542009-02-24 22:50:46 +0000928 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman638e1442008-05-25 13:22:35 +0000929 // Check for VLAs; in standard C it would be possible to check this
930 // earlier, but I don't know where clang accepts VLAs (gcc accepts
931 // them in all sorts of strange places).
Chris Lattner08202542009-02-24 22:50:46 +0000932 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000933 diag::err_variable_object_no_init)
934 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000935 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000936 ++Index;
937 ++StructuredIndex;
Eli Friedman638e1442008-05-25 13:22:35 +0000938 return;
939 }
940
Douglas Gregor05c13a32009-01-22 00:58:24 +0000941 // We might know the maximum number of elements in advance.
Douglas Gregor4c678342009-01-28 21:54:33 +0000942 llvm::APSInt maxElements(elementIndex.getBitWidth(),
943 elementIndex.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000944 bool maxElementsKnown = false;
945 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000946 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000947 maxElements = CAT->getSize();
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000948 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000949 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000950 maxElementsKnown = true;
951 }
952
Chris Lattner08202542009-02-24 22:50:46 +0000953 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000954 ->getElementType();
Douglas Gregor05c13a32009-01-22 00:58:24 +0000955 while (Index < IList->getNumInits()) {
956 Expr *Init = IList->getInit(Index);
957 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000958 // If we're not the subobject that matches up with the '{' for
959 // the designator, we shouldn't be handling the
960 // designator. Return immediately.
961 if (!SubobjectIsDesignatorContext)
962 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000963
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000964 // Handle this designated initializer. elementIndex will be
965 // updated to be the next array element we'll initialize.
Anders Carlsson9a8a70e2010-01-23 22:49:02 +0000966 if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +0000967 DeclType, 0, &elementIndex, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000968 StructuredList, StructuredIndex, true,
969 false)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000970 hadError = true;
971 continue;
972 }
973
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000974 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
975 maxElements.extend(elementIndex.getBitWidth());
976 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
977 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000978 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000979
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000980 // If the array is of incomplete type, keep track of the number of
981 // elements in the initializer.
982 if (!maxElementsKnown && elementIndex > maxElements)
983 maxElements = elementIndex;
984
Douglas Gregor05c13a32009-01-22 00:58:24 +0000985 continue;
986 }
987
988 // If we know the maximum number of elements, and we've already
989 // hit it, stop consuming elements in the initializer list.
990 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroff0cca7492008-05-01 22:18:59 +0000991 break;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000992
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000993 InitializedEntity ElementEntity =
Anders Carlsson784f6992010-01-23 20:13:41 +0000994 InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex,
Anders Carlsson8ff9e862010-01-23 23:23:01 +0000995 Entity);
996 // Check this element.
997 CheckSubElementType(ElementEntity, IList, elementType, Index,
998 StructuredList, StructuredIndex);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000999 ++elementIndex;
1000
1001 // If the array is of incomplete type, keep track of the number of
1002 // elements in the initializer.
1003 if (!maxElementsKnown && elementIndex > maxElements)
1004 maxElements = elementIndex;
Steve Naroff0cca7492008-05-01 22:18:59 +00001005 }
Eli Friedman587cbdf2009-05-29 20:17:55 +00001006 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +00001007 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001008 // be calculated here.
Douglas Gregore3fa2de2009-01-23 18:58:42 +00001009 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001010 if (maxElements == Zero) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001011 // Sizing an array implicitly to zero is not allowed by ISO C,
1012 // but is supported by GNU.
Chris Lattner08202542009-02-24 22:50:46 +00001013 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001014 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +00001015 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001016
Mike Stump1eb44332009-09-09 15:08:12 +00001017 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001018 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +00001019 }
1020}
1021
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001022void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
Anders Carlsson2bbae5d2010-01-23 20:20:40 +00001023 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001024 QualType DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001025 RecordDecl::field_iterator Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001026 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +00001027 unsigned &Index,
1028 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001029 unsigned &StructuredIndex,
1030 bool TopLevelObject) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001031 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Eli Friedmanb85f7072008-05-19 19:16:24 +00001033 // If the record is invalid, some of it's members are invalid. To avoid
1034 // confusion, we forgo checking the intializer for the entire record.
1035 if (structDecl->isInvalidDecl()) {
1036 hadError = true;
1037 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001038 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001039
1040 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1041 // Value-initialize the first named member of the union.
Ted Kremenek6217b802009-07-29 21:53:49 +00001042 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001043 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001044 Field != FieldEnd; ++Field) {
1045 if (Field->getDeclName()) {
1046 StructuredList->setInitializedFieldInUnion(*Field);
1047 break;
1048 }
1049 }
1050 return;
1051 }
1052
Douglas Gregor05c13a32009-01-22 00:58:24 +00001053 // If structDecl is a forward declaration, this loop won't do
1054 // anything except look at designated initializers; That's okay,
1055 // because an error should get printed out elsewhere. It might be
1056 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenek6217b802009-07-29 21:53:49 +00001057 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001058 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregordfb5e592009-02-12 19:00:39 +00001059 bool InitializedSomething = false;
John McCall80639de2010-03-11 19:32:38 +00001060 bool CheckForMissingFields = true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001061 while (Index < IList->getNumInits()) {
1062 Expr *Init = IList->getInit(Index);
1063
1064 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001065 // If we're not the subobject that matches up with the '{' for
1066 // the designator, we shouldn't be handling the
1067 // designator. Return immediately.
1068 if (!SubobjectIsDesignatorContext)
1069 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001070
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001071 // Handle this designated initializer. Field will be updated to
1072 // the next field that we'll be initializing.
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001073 if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +00001074 DeclType, &Field, 0, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001075 StructuredList, StructuredIndex,
1076 true, TopLevelObject))
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001077 hadError = true;
1078
Douglas Gregordfb5e592009-02-12 19:00:39 +00001079 InitializedSomething = true;
John McCall80639de2010-03-11 19:32:38 +00001080
1081 // Disable check for missing fields when designators are used.
1082 // This matches gcc behaviour.
1083 CheckForMissingFields = false;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001084 continue;
1085 }
1086
1087 if (Field == FieldEnd) {
1088 // We've run out of fields. We're done.
1089 break;
1090 }
1091
Douglas Gregordfb5e592009-02-12 19:00:39 +00001092 // We've already initialized a member of a union. We're done.
1093 if (InitializedSomething && DeclType->isUnionType())
1094 break;
1095
Douglas Gregor44b43212008-12-11 16:49:14 +00001096 // If we've hit the flexible array member at the end, we're done.
1097 if (Field->getType()->isIncompleteArrayType())
1098 break;
1099
Douglas Gregor0bb76892009-01-29 16:53:55 +00001100 if (Field->isUnnamedBitfield()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001101 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregor05c13a32009-01-22 00:58:24 +00001102 ++Field;
Eli Friedmanb85f7072008-05-19 19:16:24 +00001103 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +00001104 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001105
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001106 InitializedEntity MemberEntity =
1107 InitializedEntity::InitializeMember(*Field, &Entity);
1108 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1109 StructuredList, StructuredIndex);
Douglas Gregordfb5e592009-02-12 19:00:39 +00001110 InitializedSomething = true;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001111
1112 if (DeclType->isUnionType()) {
1113 // Initialize the first field within the union.
1114 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001115 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001116
1117 ++Field;
Steve Naroff0cca7492008-05-01 22:18:59 +00001118 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001119
John McCall80639de2010-03-11 19:32:38 +00001120 // Emit warnings for missing struct field initializers.
Douglas Gregor8e198902010-06-18 21:43:10 +00001121 if (InitializedSomething && CheckForMissingFields && Field != FieldEnd &&
John McCall80639de2010-03-11 19:32:38 +00001122 !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) {
1123 // It is possible we have one or more unnamed bitfields remaining.
1124 // Find first (if any) named field and emit warning.
1125 for (RecordDecl::field_iterator it = Field, end = RD->field_end();
1126 it != end; ++it) {
1127 if (!it->isUnnamedBitfield()) {
1128 SemaRef.Diag(IList->getSourceRange().getEnd(),
1129 diag::warn_missing_field_initializers) << it->getName();
1130 break;
1131 }
1132 }
1133 }
1134
Mike Stump1eb44332009-09-09 15:08:12 +00001135 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregora6457962009-03-20 00:32:56 +00001136 Index >= IList->getNumInits())
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001137 return;
1138
1139 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001140 if (!TopLevelObject &&
Douglas Gregora6457962009-03-20 00:32:56 +00001141 (!isa<InitListExpr>(IList->getInit(Index)) ||
1142 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001143 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001144 diag::err_flexible_array_init_nonempty)
1145 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001146 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001147 << *Field;
1148 hadError = true;
Douglas Gregora6457962009-03-20 00:32:56 +00001149 ++Index;
1150 return;
1151 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001152 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregora6457962009-03-20 00:32:56 +00001153 diag::ext_flexible_array_init)
1154 << IList->getInit(Index)->getSourceRange().getBegin();
1155 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1156 << *Field;
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001157 }
1158
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001159 InitializedEntity MemberEntity =
1160 InitializedEntity::InitializeMember(*Field, &Entity);
Anders Carlsson987dc6a2010-01-23 20:47:59 +00001161
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001162 if (isa<InitListExpr>(IList->getInit(Index)))
1163 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1164 StructuredList, StructuredIndex);
1165 else
1166 CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
Anders Carlsson987dc6a2010-01-23 20:47:59 +00001167 StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +00001168}
Steve Naroff0cca7492008-05-01 22:18:59 +00001169
Douglas Gregor022d13d2010-10-08 20:44:28 +00001170/// \brief Similar to Sema::BuildAnonymousStructUnionMemberPath() but builds a
1171/// relative path and has strict checks.
1172static void BuildRelativeAnonymousStructUnionMemberPath(FieldDecl *Field,
1173 llvm::SmallVectorImpl<FieldDecl *> &Path,
1174 DeclContext *BaseDC) {
1175 Path.push_back(Field);
1176 for (DeclContext *Ctx = Field->getDeclContext();
1177 !Ctx->Equals(BaseDC);
1178 Ctx = Ctx->getParent()) {
1179 ValueDecl *AnonObject =
1180 cast<RecordDecl>(Ctx)->getAnonymousStructOrUnionObject();
1181 FieldDecl *AnonField = cast<FieldDecl>(AnonObject);
1182 Path.push_back(AnonField);
1183 }
1184}
1185
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001186/// \brief Expand a field designator that refers to a member of an
1187/// anonymous struct or union into a series of field designators that
1188/// refers to the field within the appropriate subobject.
1189///
1190/// Field/FieldIndex will be updated to point to the (new)
1191/// currently-designated field.
1192static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump1eb44332009-09-09 15:08:12 +00001193 DesignatedInitExpr *DIE,
1194 unsigned DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001195 FieldDecl *Field,
1196 RecordDecl::field_iterator &FieldIter,
Douglas Gregor022d13d2010-10-08 20:44:28 +00001197 unsigned &FieldIndex,
1198 DeclContext *BaseDC) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001199 typedef DesignatedInitExpr::Designator Designator;
1200
1201 // Build the path from the current object to the member of the
1202 // anonymous struct/union (backwards).
1203 llvm::SmallVector<FieldDecl *, 4> Path;
Douglas Gregor022d13d2010-10-08 20:44:28 +00001204 BuildRelativeAnonymousStructUnionMemberPath(Field, Path, BaseDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001206 // Build the replacement designators.
1207 llvm::SmallVector<Designator, 4> Replacements;
1208 for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1209 FI = Path.rbegin(), FIEnd = Path.rend();
1210 FI != FIEnd; ++FI) {
1211 if (FI + 1 == FIEnd)
Mike Stump1eb44332009-09-09 15:08:12 +00001212 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001213 DIE->getDesignator(DesigIdx)->getDotLoc(),
1214 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1215 else
1216 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1217 SourceLocation()));
1218 Replacements.back().setField(*FI);
1219 }
1220
1221 // Expand the current designator into the set of replacement
1222 // designators, so we have a full subobject path down to where the
1223 // member of the anonymous struct/union is actually stored.
Douglas Gregor319d57f2010-01-06 23:17:19 +00001224 DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001225 &Replacements[0] + Replacements.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001227 // Update FieldIter/FieldIndex;
1228 RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001229 FieldIter = Record->field_begin();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001230 FieldIndex = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001231 for (RecordDecl::field_iterator FEnd = Record->field_end();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001232 FieldIter != FEnd; ++FieldIter) {
1233 if (FieldIter->isUnnamedBitfield())
1234 continue;
1235
1236 if (*FieldIter == Path.back())
1237 return;
1238
1239 ++FieldIndex;
1240 }
1241
1242 assert(false && "Unable to find anonymous struct/union field");
1243}
1244
Douglas Gregor05c13a32009-01-22 00:58:24 +00001245/// @brief Check the well-formedness of a C99 designated initializer.
1246///
1247/// Determines whether the designated initializer @p DIE, which
1248/// resides at the given @p Index within the initializer list @p
1249/// IList, is well-formed for a current object of type @p DeclType
1250/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump1eb44332009-09-09 15:08:12 +00001251/// within the current subobject is returned in either
Douglas Gregor4c678342009-01-28 21:54:33 +00001252/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregor05c13a32009-01-22 00:58:24 +00001253///
1254/// @param IList The initializer list in which this designated
1255/// initializer occurs.
1256///
Douglas Gregor71199712009-04-15 04:56:10 +00001257/// @param DIE The designated initializer expression.
1258///
1259/// @param DesigIdx The index of the current designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001260///
1261/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1262/// into which the designation in @p DIE should refer.
1263///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001264/// @param NextField If non-NULL and the first designator in @p DIE is
1265/// a field, this will be set to the field declaration corresponding
1266/// to the field named by the designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001267///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001268/// @param NextElementIndex If non-NULL and the first designator in @p
1269/// DIE is an array designator or GNU array-range designator, this
1270/// will be set to the last index initialized by this designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001271///
1272/// @param Index Index into @p IList where the designated initializer
1273/// @p DIE occurs.
1274///
Douglas Gregor4c678342009-01-28 21:54:33 +00001275/// @param StructuredList The initializer list expression that
1276/// describes all of the subobject initializers in the order they'll
1277/// actually be initialized.
1278///
Douglas Gregor05c13a32009-01-22 00:58:24 +00001279/// @returns true if there was an error, false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001280bool
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001281InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001282 InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001283 DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +00001284 unsigned DesigIdx,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001285 QualType &CurrentObjectType,
1286 RecordDecl::field_iterator *NextField,
1287 llvm::APSInt *NextElementIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001288 unsigned &Index,
1289 InitListExpr *StructuredList,
Douglas Gregor34e79462009-01-28 23:36:17 +00001290 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001291 bool FinishSubobjectInit,
1292 bool TopLevelObject) {
Douglas Gregor71199712009-04-15 04:56:10 +00001293 if (DesigIdx == DIE->size()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001294 // Check the actual initialization for the designated object type.
1295 bool prevHadError = hadError;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001296
1297 // Temporarily remove the designator expression from the
1298 // initializer list that the child calls see, so that we don't try
1299 // to re-process the designator.
1300 unsigned OldIndex = Index;
1301 IList->setInit(OldIndex, DIE->getInit());
1302
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001303 CheckSubElementType(Entity, IList, CurrentObjectType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001304 StructuredList, StructuredIndex);
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001305
1306 // Restore the designated initializer expression in the syntactic
1307 // form of the initializer list.
1308 if (IList->getInit(OldIndex) != DIE->getInit())
1309 DIE->setInit(IList->getInit(OldIndex));
1310 IList->setInit(OldIndex, DIE);
1311
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001312 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001313 }
1314
Douglas Gregor71199712009-04-15 04:56:10 +00001315 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001316 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor4c678342009-01-28 21:54:33 +00001317 "Need a non-designated initializer list to start from");
1318
Douglas Gregor71199712009-04-15 04:56:10 +00001319 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001320 // Determine the structural initializer list that corresponds to the
1321 // current subobject.
1322 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump1eb44332009-09-09 15:08:12 +00001323 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001324 StructuredList, StructuredIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001325 SourceRange(D->getStartLocation(),
1326 DIE->getSourceRange().getEnd()));
1327 assert(StructuredList && "Expected a structured initializer list");
1328
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001329 if (D->isFieldDesignator()) {
1330 // C99 6.7.8p7:
1331 //
1332 // If a designator has the form
1333 //
1334 // . identifier
1335 //
1336 // then the current object (defined below) shall have
1337 // structure or union type and the identifier shall be the
Mike Stump1eb44332009-09-09 15:08:12 +00001338 // name of a member of that type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001339 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001340 if (!RT) {
1341 SourceLocation Loc = D->getDotLoc();
1342 if (Loc.isInvalid())
1343 Loc = D->getFieldLoc();
Chris Lattner08202542009-02-24 22:50:46 +00001344 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1345 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001346 ++Index;
1347 return true;
1348 }
1349
Douglas Gregor4c678342009-01-28 21:54:33 +00001350 // Note: we perform a linear search of the fields here, despite
1351 // the fact that we have a faster lookup method, because we always
1352 // need to compute the field's index.
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001353 FieldDecl *KnownField = D->getField();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001354 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor4c678342009-01-28 21:54:33 +00001355 unsigned FieldIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001356 RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001357 Field = RT->getDecl()->field_begin(),
1358 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +00001359 for (; Field != FieldEnd; ++Field) {
1360 if (Field->isUnnamedBitfield())
1361 continue;
1362
Douglas Gregor022d13d2010-10-08 20:44:28 +00001363 if (KnownField && KnownField == *Field)
1364 break;
1365 if (FieldName && FieldName == Field->getIdentifier())
Douglas Gregor4c678342009-01-28 21:54:33 +00001366 break;
1367
1368 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001369 }
1370
Douglas Gregor4c678342009-01-28 21:54:33 +00001371 if (Field == FieldEnd) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001372 // There was no normal field in the struct with the designated
1373 // name. Perform another lookup for this name, which may find
1374 // something that we can't designate (e.g., a member function),
1375 // may find nothing, or may find a member of an anonymous
Mike Stump1eb44332009-09-09 15:08:12 +00001376 // struct/union.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001377 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001378 FieldDecl *ReplacementField = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001379 if (Lookup.first == Lookup.second) {
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001380 // Name lookup didn't find anything. Determine whether this
1381 // was a typo for another field name.
1382 LookupResult R(SemaRef, FieldName, D->getFieldLoc(),
1383 Sema::LookupMemberName);
Douglas Gregoraaf87162010-04-14 20:04:41 +00001384 if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl(), false,
1385 Sema::CTC_NoKeywords) &&
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001386 (ReplacementField = R.getAsSingle<FieldDecl>()) &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001387 ReplacementField->getDeclContext()->getRedeclContext()
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001388 ->Equals(RT->getDecl())) {
1389 SemaRef.Diag(D->getFieldLoc(),
1390 diag::err_field_designator_unknown_suggest)
1391 << FieldName << CurrentObjectType << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +00001392 << FixItHint::CreateReplacement(D->getFieldLoc(),
1393 R.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +00001394 SemaRef.Diag(ReplacementField->getLocation(),
1395 diag::note_previous_decl)
1396 << ReplacementField->getDeclName();
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001397 } else {
1398 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1399 << FieldName << CurrentObjectType;
1400 ++Index;
1401 return true;
1402 }
1403 } else if (!KnownField) {
1404 // Determine whether we found a field at all.
1405 ReplacementField = dyn_cast<FieldDecl>(*Lookup.first);
1406 }
1407
1408 if (!ReplacementField) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001409 // Name lookup found something, but it wasn't a field.
Chris Lattner08202542009-02-24 22:50:46 +00001410 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor4c678342009-01-28 21:54:33 +00001411 << FieldName;
Mike Stump1eb44332009-09-09 15:08:12 +00001412 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001413 diag::note_field_designator_found);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001414 ++Index;
1415 return true;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001416 }
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001417
1418 if (!KnownField &&
1419 cast<RecordDecl>((ReplacementField)->getDeclContext())
1420 ->isAnonymousStructOrUnion()) {
1421 // Handle an field designator that refers to a member of an
Douglas Gregor022d13d2010-10-08 20:44:28 +00001422 // anonymous struct or union. This is a C1X feature.
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001423 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
1424 ReplacementField,
Douglas Gregor022d13d2010-10-08 20:44:28 +00001425 Field, FieldIndex, RT->getDecl());
Douglas Gregorc171e3b2010-01-01 00:03:05 +00001426 D = DIE->getDesignator(DesigIdx);
1427 } else if (!KnownField) {
1428 // The replacement field comes from typo correction; find it
1429 // in the list of fields.
1430 FieldIndex = 0;
1431 Field = RT->getDecl()->field_begin();
1432 for (; Field != FieldEnd; ++Field) {
1433 if (Field->isUnnamedBitfield())
1434 continue;
1435
1436 if (ReplacementField == *Field ||
1437 Field->getIdentifier() == ReplacementField->getIdentifier())
1438 break;
1439
1440 ++FieldIndex;
1441 }
1442 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001443 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001444
1445 // All of the fields of a union are located at the same place in
1446 // the initializer list.
Douglas Gregor0bb76892009-01-29 16:53:55 +00001447 if (RT->getDecl()->isUnion()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001448 FieldIndex = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001449 StructuredList->setInitializedFieldInUnion(*Field);
1450 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001451
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001452 // Update the designator with the field declaration.
Douglas Gregor4c678342009-01-28 21:54:33 +00001453 D->setField(*Field);
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Douglas Gregor4c678342009-01-28 21:54:33 +00001455 // Make sure that our non-designated initializer list has space
1456 // for a subobject corresponding to this field.
1457 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001458 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001459
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001460 // This designator names a flexible array member.
1461 if (Field->getType()->isIncompleteArrayType()) {
1462 bool Invalid = false;
Douglas Gregor71199712009-04-15 04:56:10 +00001463 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001464 // We can't designate an object within the flexible array
1465 // member (because GCC doesn't allow it).
Mike Stump1eb44332009-09-09 15:08:12 +00001466 DesignatedInitExpr::Designator *NextD
Douglas Gregor71199712009-04-15 04:56:10 +00001467 = DIE->getDesignator(DesigIdx + 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001468 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001469 diag::err_designator_into_flexible_array_member)
Mike Stump1eb44332009-09-09 15:08:12 +00001470 << SourceRange(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001471 DIE->getSourceRange().getEnd());
Chris Lattner08202542009-02-24 22:50:46 +00001472 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001473 << *Field;
1474 Invalid = true;
1475 }
1476
Chris Lattner9046c222010-10-10 17:49:49 +00001477 if (!hadError && !isa<InitListExpr>(DIE->getInit()) &&
1478 !isa<StringLiteral>(DIE->getInit())) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001479 // The initializer is not an initializer list.
Chris Lattner08202542009-02-24 22:50:46 +00001480 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001481 diag::err_flexible_array_init_needs_braces)
1482 << DIE->getInit()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001483 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001484 << *Field;
1485 Invalid = true;
1486 }
1487
1488 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001489 if (!Invalid && !TopLevelObject &&
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001490 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +00001491 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001492 diag::err_flexible_array_init_nonempty)
1493 << DIE->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001494 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001495 << *Field;
1496 Invalid = true;
1497 }
1498
1499 if (Invalid) {
1500 ++Index;
1501 return true;
1502 }
1503
1504 // Initialize the array.
1505 bool prevHadError = hadError;
1506 unsigned newStructuredIndex = FieldIndex;
1507 unsigned OldIndex = Index;
1508 IList->setInit(Index, DIE->getInit());
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001509
1510 InitializedEntity MemberEntity =
1511 InitializedEntity::InitializeMember(*Field, &Entity);
1512 CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001513 StructuredList, newStructuredIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001514
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001515 IList->setInit(OldIndex, DIE);
1516 if (hadError && !prevHadError) {
1517 ++Field;
1518 ++FieldIndex;
1519 if (NextField)
1520 *NextField = Field;
1521 StructuredIndex = FieldIndex;
1522 return true;
1523 }
1524 } else {
1525 // Recurse to check later designated subobjects.
1526 QualType FieldType = (*Field)->getType();
1527 unsigned newStructuredIndex = FieldIndex;
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001528
1529 InitializedEntity MemberEntity =
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001530 InitializedEntity::InitializeMember(*Field, &Entity);
1531 if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001532 FieldType, 0, 0, Index,
1533 StructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001534 true, false))
1535 return true;
1536 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001537
1538 // Find the position of the next field to be initialized in this
1539 // subobject.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001540 ++Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001541 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001542
1543 // If this the first designator, our caller will continue checking
1544 // the rest of this struct/class/union subobject.
1545 if (IsFirstDesignator) {
1546 if (NextField)
1547 *NextField = Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001548 StructuredIndex = FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001549 return false;
1550 }
1551
Douglas Gregor34e79462009-01-28 23:36:17 +00001552 if (!FinishSubobjectInit)
1553 return false;
1554
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001555 // We've already initialized something in the union; we're done.
1556 if (RT->getDecl()->isUnion())
1557 return hadError;
1558
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001559 // Check the remaining fields within this class/struct/union subobject.
1560 bool prevHadError = hadError;
Anders Carlsson2bbae5d2010-01-23 20:20:40 +00001561
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001562 CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001563 StructuredList, FieldIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001564 return hadError && !prevHadError;
1565 }
1566
1567 // C99 6.7.8p6:
1568 //
1569 // If a designator has the form
1570 //
1571 // [ constant-expression ]
1572 //
1573 // then the current object (defined below) shall have array
1574 // type and the expression shall be an integer constant
1575 // expression. If the array is of unknown size, any
1576 // nonnegative value is valid.
1577 //
1578 // Additionally, cope with the GNU extension that permits
1579 // designators of the form
1580 //
1581 // [ constant-expression ... constant-expression ]
Chris Lattner08202542009-02-24 22:50:46 +00001582 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001583 if (!AT) {
Chris Lattner08202542009-02-24 22:50:46 +00001584 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001585 << CurrentObjectType;
1586 ++Index;
1587 return true;
1588 }
1589
1590 Expr *IndexExpr = 0;
Douglas Gregor34e79462009-01-28 23:36:17 +00001591 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1592 if (D->isArrayDesignator()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001593 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattner3bf68932009-04-25 21:59:05 +00001594 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001595 DesignatedEndIndex = DesignatedStartIndex;
1596 } else {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001597 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor34e79462009-01-28 23:36:17 +00001598
Mike Stump1eb44332009-09-09 15:08:12 +00001599
1600 DesignatedStartIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001601 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001602 DesignatedEndIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001603 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001604 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001605
Chris Lattner3bf68932009-04-25 21:59:05 +00001606 if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
Douglas Gregora9c87802009-01-29 19:42:23 +00001607 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001608 }
1609
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001610 if (isa<ConstantArrayType>(AT)) {
1611 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor34e79462009-01-28 23:36:17 +00001612 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1613 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1614 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1615 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1616 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner08202542009-02-24 22:50:46 +00001617 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001618 diag::err_array_designator_too_large)
Douglas Gregor34e79462009-01-28 23:36:17 +00001619 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001620 << IndexExpr->getSourceRange();
1621 ++Index;
1622 return true;
1623 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001624 } else {
1625 // Make sure the bit-widths and signedness match.
1626 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1627 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattner3bf68932009-04-25 21:59:05 +00001628 else if (DesignatedStartIndex.getBitWidth() <
1629 DesignatedEndIndex.getBitWidth())
Douglas Gregor34e79462009-01-28 23:36:17 +00001630 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1631 DesignatedStartIndex.setIsUnsigned(true);
1632 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001633 }
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Douglas Gregor4c678342009-01-28 21:54:33 +00001635 // Make sure that our non-designated initializer list has space
1636 // for a subobject corresponding to this array element.
Douglas Gregor34e79462009-01-28 23:36:17 +00001637 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump1eb44332009-09-09 15:08:12 +00001638 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor34e79462009-01-28 23:36:17 +00001639 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001640
Douglas Gregor34e79462009-01-28 23:36:17 +00001641 // Repeatedly perform subobject initializations in the range
1642 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001643
Douglas Gregor34e79462009-01-28 23:36:17 +00001644 // Move to the next designator
1645 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1646 unsigned OldIndex = Index;
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001647
1648 InitializedEntity ElementEntity =
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001649 InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001650
Douglas Gregor34e79462009-01-28 23:36:17 +00001651 while (DesignatedStartIndex <= DesignatedEndIndex) {
1652 // Recurse to check later designated subobjects.
1653 QualType ElementType = AT->getElementType();
1654 Index = OldIndex;
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001655
1656 ElementEntity.setElementIndex(ElementIndex);
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001657 if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1,
Anders Carlsson9a8a70e2010-01-23 22:49:02 +00001658 ElementType, 0, 0, Index,
1659 StructuredList, ElementIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001660 (DesignatedStartIndex == DesignatedEndIndex),
1661 false))
Douglas Gregor34e79462009-01-28 23:36:17 +00001662 return true;
1663
1664 // Move to the next index in the array that we'll be initializing.
1665 ++DesignatedStartIndex;
1666 ElementIndex = DesignatedStartIndex.getZExtValue();
1667 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001668
1669 // If this the first designator, our caller will continue checking
1670 // the rest of this array subobject.
1671 if (IsFirstDesignator) {
1672 if (NextElementIndex)
Douglas Gregor34e79462009-01-28 23:36:17 +00001673 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor4c678342009-01-28 21:54:33 +00001674 StructuredIndex = ElementIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001675 return false;
1676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Douglas Gregor34e79462009-01-28 23:36:17 +00001678 if (!FinishSubobjectInit)
1679 return false;
1680
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001681 // Check the remaining elements within this array subobject.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001682 bool prevHadError = hadError;
Anders Carlsson8ff9e862010-01-23 23:23:01 +00001683 CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex,
Anders Carlsson784f6992010-01-23 20:13:41 +00001684 /*SubobjectIsDesignatorContext=*/false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001685 StructuredList, ElementIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001686 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001687}
1688
Douglas Gregor4c678342009-01-28 21:54:33 +00001689// Get the structured initializer list for a subobject of type
1690// @p CurrentObjectType.
1691InitListExpr *
1692InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1693 QualType CurrentObjectType,
1694 InitListExpr *StructuredList,
1695 unsigned StructuredIndex,
1696 SourceRange InitRange) {
1697 Expr *ExistingInit = 0;
1698 if (!StructuredList)
1699 ExistingInit = SyntacticToSemantic[IList];
1700 else if (StructuredIndex < StructuredList->getNumInits())
1701 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Douglas Gregor4c678342009-01-28 21:54:33 +00001703 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1704 return Result;
1705
1706 if (ExistingInit) {
1707 // We are creating an initializer list that initializes the
1708 // subobjects of the current object, but there was already an
1709 // initialization that completely initialized the current
1710 // subobject, e.g., by a compound literal:
Mike Stump1eb44332009-09-09 15:08:12 +00001711 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001712 // struct X { int a, b; };
1713 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump1eb44332009-09-09 15:08:12 +00001714 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001715 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1716 // designated initializer re-initializes the whole
1717 // subobject [0], overwriting previous initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001718 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregored8a93d2009-03-01 17:12:46 +00001719 diag::warn_subobject_initializer_overrides)
Douglas Gregor4c678342009-01-28 21:54:33 +00001720 << InitRange;
Mike Stump1eb44332009-09-09 15:08:12 +00001721 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001722 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001723 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001724 << ExistingInit->getSourceRange();
1725 }
1726
Mike Stump1eb44332009-09-09 15:08:12 +00001727 InitListExpr *Result
Ted Kremenek709210f2010-04-13 23:39:13 +00001728 = new (SemaRef.Context) InitListExpr(SemaRef.Context,
1729 InitRange.getBegin(), 0, 0,
Ted Kremenekba7bc552010-02-19 01:50:18 +00001730 InitRange.getEnd());
Douglas Gregored8a93d2009-03-01 17:12:46 +00001731
Douglas Gregor63982352010-07-13 18:40:04 +00001732 Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context));
Douglas Gregor4c678342009-01-28 21:54:33 +00001733
Douglas Gregorfa219202009-03-20 23:58:33 +00001734 // Pre-allocate storage for the structured initializer list.
1735 unsigned NumElements = 0;
Douglas Gregor08457732009-03-21 18:13:52 +00001736 unsigned NumInits = 0;
1737 if (!StructuredList)
1738 NumInits = IList->getNumInits();
1739 else if (Index < IList->getNumInits()) {
1740 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1741 NumInits = SubList->getNumInits();
1742 }
1743
Mike Stump1eb44332009-09-09 15:08:12 +00001744 if (const ArrayType *AType
Douglas Gregorfa219202009-03-20 23:58:33 +00001745 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1746 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1747 NumElements = CAType->getSize().getZExtValue();
1748 // Simple heuristic so that we don't allocate a very large
1749 // initializer with many empty entries at the end.
Douglas Gregor08457732009-03-21 18:13:52 +00001750 if (NumInits && NumElements > NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001751 NumElements = 0;
1752 }
John McCall183700f2009-09-21 23:43:11 +00001753 } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
Douglas Gregorfa219202009-03-20 23:58:33 +00001754 NumElements = VType->getNumElements();
Ted Kremenek6217b802009-07-29 21:53:49 +00001755 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregorfa219202009-03-20 23:58:33 +00001756 RecordDecl *RDecl = RType->getDecl();
1757 if (RDecl->isUnion())
1758 NumElements = 1;
1759 else
Mike Stump1eb44332009-09-09 15:08:12 +00001760 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001761 RDecl->field_end());
Douglas Gregorfa219202009-03-20 23:58:33 +00001762 }
1763
Douglas Gregor08457732009-03-21 18:13:52 +00001764 if (NumElements < NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001765 NumElements = IList->getNumInits();
1766
Ted Kremenek709210f2010-04-13 23:39:13 +00001767 Result->reserveInits(SemaRef.Context, NumElements);
Douglas Gregorfa219202009-03-20 23:58:33 +00001768
Douglas Gregor4c678342009-01-28 21:54:33 +00001769 // Link this new initializer list into the structured initializer
1770 // lists.
1771 if (StructuredList)
Ted Kremenek709210f2010-04-13 23:39:13 +00001772 StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result);
Douglas Gregor4c678342009-01-28 21:54:33 +00001773 else {
1774 Result->setSyntacticForm(IList);
1775 SyntacticToSemantic[IList] = Result;
1776 }
1777
1778 return Result;
1779}
1780
1781/// Update the initializer at index @p StructuredIndex within the
1782/// structured initializer list to the value @p expr.
1783void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1784 unsigned &StructuredIndex,
1785 Expr *expr) {
1786 // No structured initializer list to update
1787 if (!StructuredList)
1788 return;
1789
Ted Kremenek709210f2010-04-13 23:39:13 +00001790 if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
1791 StructuredIndex, expr)) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001792 // This initializer overwrites a previous initializer. Warn.
Mike Stump1eb44332009-09-09 15:08:12 +00001793 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001794 diag::warn_initializer_overrides)
1795 << expr->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001796 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001797 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001798 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001799 << PrevInit->getSourceRange();
1800 }
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Douglas Gregor4c678342009-01-28 21:54:33 +00001802 ++StructuredIndex;
1803}
1804
Douglas Gregor05c13a32009-01-22 00:58:24 +00001805/// Check that the given Index expression is a valid array designator
1806/// value. This is essentailly just a wrapper around
Chris Lattner3bf68932009-04-25 21:59:05 +00001807/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregor05c13a32009-01-22 00:58:24 +00001808/// and produces a reasonable diagnostic if there is a
1809/// failure. Returns true if there was an error, false otherwise. If
1810/// everything went okay, Value will receive the value of the constant
1811/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001812static bool
Chris Lattner3bf68932009-04-25 21:59:05 +00001813CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001814 SourceLocation Loc = Index->getSourceRange().getBegin();
1815
1816 // Make sure this is an integer constant expression.
Chris Lattner3bf68932009-04-25 21:59:05 +00001817 if (S.VerifyIntegerConstantExpression(Index, &Value))
1818 return true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001819
Chris Lattner3bf68932009-04-25 21:59:05 +00001820 if (Value.isSigned() && Value.isNegative())
1821 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001822 << Value.toString(10) << Index->getSourceRange();
1823
Douglas Gregor53d3d8e2009-01-23 21:04:18 +00001824 Value.setIsUnsigned(true);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001825 return false;
1826}
1827
John McCall60d7b3a2010-08-24 06:29:42 +00001828ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001829 SourceLocation Loc,
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001830 bool GNUSyntax,
John McCall60d7b3a2010-08-24 06:29:42 +00001831 ExprResult Init) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001832 typedef DesignatedInitExpr::Designator ASTDesignator;
1833
1834 bool Invalid = false;
1835 llvm::SmallVector<ASTDesignator, 32> Designators;
1836 llvm::SmallVector<Expr *, 32> InitExpressions;
1837
1838 // Build designators and check array designator expressions.
1839 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1840 const Designator &D = Desig.getDesignator(Idx);
1841 switch (D.getKind()) {
1842 case Designator::FieldDesignator:
Mike Stump1eb44332009-09-09 15:08:12 +00001843 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001844 D.getFieldLoc()));
1845 break;
1846
1847 case Designator::ArrayDesignator: {
1848 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1849 llvm::APSInt IndexValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001850 if (!Index->isTypeDependent() &&
1851 !Index->isValueDependent() &&
1852 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001853 Invalid = true;
1854 else {
1855 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001856 D.getLBracketLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001857 D.getRBracketLoc()));
1858 InitExpressions.push_back(Index);
1859 }
1860 break;
1861 }
1862
1863 case Designator::ArrayRangeDesignator: {
1864 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1865 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1866 llvm::APSInt StartValue;
1867 llvm::APSInt EndValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001868 bool StartDependent = StartIndex->isTypeDependent() ||
1869 StartIndex->isValueDependent();
1870 bool EndDependent = EndIndex->isTypeDependent() ||
1871 EndIndex->isValueDependent();
1872 if ((!StartDependent &&
1873 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1874 (!EndDependent &&
1875 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001876 Invalid = true;
Douglas Gregord6f584f2009-01-23 22:22:29 +00001877 else {
1878 // Make sure we're comparing values with the same bit width.
Douglas Gregor9ea62762009-05-21 23:17:49 +00001879 if (StartDependent || EndDependent) {
1880 // Nothing to compute.
1881 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Douglas Gregord6f584f2009-01-23 22:22:29 +00001882 EndValue.extend(StartValue.getBitWidth());
1883 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1884 StartValue.extend(EndValue.getBitWidth());
1885
Douglas Gregorc4bb7bf2009-05-21 23:30:39 +00001886 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregord6f584f2009-01-23 22:22:29 +00001887 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump1eb44332009-09-09 15:08:12 +00001888 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregord6f584f2009-01-23 22:22:29 +00001889 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1890 Invalid = true;
1891 } else {
1892 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001893 D.getLBracketLoc(),
Douglas Gregord6f584f2009-01-23 22:22:29 +00001894 D.getEllipsisLoc(),
1895 D.getRBracketLoc()));
1896 InitExpressions.push_back(StartIndex);
1897 InitExpressions.push_back(EndIndex);
1898 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001899 }
1900 break;
1901 }
1902 }
1903 }
1904
1905 if (Invalid || Init.isInvalid())
1906 return ExprError();
1907
1908 // Clear out the expressions within the designation.
1909 Desig.ClearExprs(*this);
1910
1911 DesignatedInitExpr *DIE
Jay Foadbeaaccd2009-05-21 09:52:38 +00001912 = DesignatedInitExpr::Create(Context,
1913 Designators.data(), Designators.size(),
1914 InitExpressions.data(), InitExpressions.size(),
Anders Carlssone9146f22009-05-01 19:49:17 +00001915 Loc, GNUSyntax, Init.takeAs<Expr>());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001916 return Owned(DIE);
1917}
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001918
Douglas Gregorcb57fb92009-12-16 06:35:08 +00001919bool Sema::CheckInitList(const InitializedEntity &Entity,
1920 InitListExpr *&InitList, QualType &DeclType) {
1921 InitListChecker CheckInitList(*this, Entity, InitList, DeclType);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001922 if (!CheckInitList.HadError())
1923 InitList = CheckInitList.getFullyStructuredList();
1924
1925 return CheckInitList.HadError();
1926}
Douglas Gregor87fd7032009-02-02 17:43:21 +00001927
Douglas Gregor20093b42009-12-09 23:02:17 +00001928//===----------------------------------------------------------------------===//
1929// Initialization entity
1930//===----------------------------------------------------------------------===//
1931
Douglas Gregorcb57fb92009-12-16 06:35:08 +00001932InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
1933 const InitializedEntity &Parent)
Anders Carlssond3d824d2010-01-23 04:34:47 +00001934 : Parent(&Parent), Index(Index)
Douglas Gregorcb57fb92009-12-16 06:35:08 +00001935{
Anders Carlssond3d824d2010-01-23 04:34:47 +00001936 if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
1937 Kind = EK_ArrayElement;
Douglas Gregord6542d82009-12-22 15:35:07 +00001938 Type = AT->getElementType();
Anders Carlssond3d824d2010-01-23 04:34:47 +00001939 } else {
1940 Kind = EK_VectorElement;
Douglas Gregord6542d82009-12-22 15:35:07 +00001941 Type = Parent.getType()->getAs<VectorType>()->getElementType();
Anders Carlssond3d824d2010-01-23 04:34:47 +00001942 }
Douglas Gregor20093b42009-12-09 23:02:17 +00001943}
1944
1945InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
Anders Carlsson711f34a2010-04-21 19:52:01 +00001946 CXXBaseSpecifier *Base,
1947 bool IsInheritedVirtualBase)
Douglas Gregor20093b42009-12-09 23:02:17 +00001948{
1949 InitializedEntity Result;
1950 Result.Kind = EK_Base;
Anders Carlsson711f34a2010-04-21 19:52:01 +00001951 Result.Base = reinterpret_cast<uintptr_t>(Base);
1952 if (IsInheritedVirtualBase)
1953 Result.Base |= 0x01;
1954
Douglas Gregord6542d82009-12-22 15:35:07 +00001955 Result.Type = Base->getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00001956 return Result;
1957}
1958
Douglas Gregor99a2e602009-12-16 01:38:02 +00001959DeclarationName InitializedEntity::getName() const {
1960 switch (getKind()) {
Douglas Gregor99a2e602009-12-16 01:38:02 +00001961 case EK_Parameter:
Douglas Gregora188ff22009-12-22 16:09:06 +00001962 if (!VariableOrMember)
1963 return DeclarationName();
1964 // Fall through
1965
1966 case EK_Variable:
Douglas Gregor99a2e602009-12-16 01:38:02 +00001967 case EK_Member:
1968 return VariableOrMember->getDeclName();
1969
1970 case EK_Result:
1971 case EK_Exception:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00001972 case EK_New:
Douglas Gregor99a2e602009-12-16 01:38:02 +00001973 case EK_Temporary:
1974 case EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00001975 case EK_ArrayElement:
1976 case EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00001977 case EK_BlockElement:
Douglas Gregor99a2e602009-12-16 01:38:02 +00001978 return DeclarationName();
1979 }
1980
1981 // Silence GCC warning
1982 return DeclarationName();
1983}
1984
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00001985DeclaratorDecl *InitializedEntity::getDecl() const {
1986 switch (getKind()) {
1987 case EK_Variable:
1988 case EK_Parameter:
1989 case EK_Member:
1990 return VariableOrMember;
1991
1992 case EK_Result:
1993 case EK_Exception:
1994 case EK_New:
1995 case EK_Temporary:
1996 case EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00001997 case EK_ArrayElement:
1998 case EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00001999 case EK_BlockElement:
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00002000 return 0;
2001 }
2002
2003 // Silence GCC warning
2004 return 0;
2005}
2006
Douglas Gregor3c9034c2010-05-15 00:13:29 +00002007bool InitializedEntity::allowsNRVO() const {
2008 switch (getKind()) {
2009 case EK_Result:
2010 case EK_Exception:
2011 return LocAndNRVO.NRVO;
2012
2013 case EK_Variable:
2014 case EK_Parameter:
2015 case EK_Member:
2016 case EK_New:
2017 case EK_Temporary:
2018 case EK_Base:
2019 case EK_ArrayElement:
2020 case EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00002021 case EK_BlockElement:
Douglas Gregor3c9034c2010-05-15 00:13:29 +00002022 break;
2023 }
2024
2025 return false;
2026}
2027
Douglas Gregor20093b42009-12-09 23:02:17 +00002028//===----------------------------------------------------------------------===//
2029// Initialization sequence
2030//===----------------------------------------------------------------------===//
2031
2032void InitializationSequence::Step::Destroy() {
2033 switch (Kind) {
2034 case SK_ResolveAddressOfOverloadedFunction:
2035 case SK_CastDerivedToBaseRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002036 case SK_CastDerivedToBaseXValue:
Douglas Gregor20093b42009-12-09 23:02:17 +00002037 case SK_CastDerivedToBaseLValue:
2038 case SK_BindReference:
2039 case SK_BindReferenceToTemporary:
Douglas Gregor523d46a2010-04-18 07:40:54 +00002040 case SK_ExtraneousCopyToTemporary:
Douglas Gregor20093b42009-12-09 23:02:17 +00002041 case SK_UserConversion:
2042 case SK_QualificationConversionRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002043 case SK_QualificationConversionXValue:
Douglas Gregor20093b42009-12-09 23:02:17 +00002044 case SK_QualificationConversionLValue:
Douglas Gregord87b61f2009-12-10 17:56:55 +00002045 case SK_ListInitialization:
Douglas Gregor51c56d62009-12-14 20:49:26 +00002046 case SK_ConstructorInitialization:
Douglas Gregor71d17402009-12-15 00:01:57 +00002047 case SK_ZeroInitialization:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002048 case SK_CAssignment:
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002049 case SK_StringInit:
Douglas Gregor569c3162010-08-07 11:51:51 +00002050 case SK_ObjCObjectConversion:
Douglas Gregor20093b42009-12-09 23:02:17 +00002051 break;
2052
2053 case SK_ConversionSequence:
2054 delete ICS;
2055 }
2056}
2057
Douglas Gregorb70cf442010-03-26 20:14:36 +00002058bool InitializationSequence::isDirectReferenceBinding() const {
2059 return getKind() == ReferenceBinding && Steps.back().Kind == SK_BindReference;
2060}
2061
2062bool InitializationSequence::isAmbiguous() const {
2063 if (getKind() != FailedSequence)
2064 return false;
2065
2066 switch (getFailureKind()) {
2067 case FK_TooManyInitsForReference:
2068 case FK_ArrayNeedsInitList:
2069 case FK_ArrayNeedsInitListOrStringLiteral:
2070 case FK_AddressOfOverloadFailed: // FIXME: Could do better
2071 case FK_NonConstLValueReferenceBindingToTemporary:
2072 case FK_NonConstLValueReferenceBindingToUnrelated:
2073 case FK_RValueReferenceBindingToLValue:
2074 case FK_ReferenceInitDropsQualifiers:
2075 case FK_ReferenceInitFailed:
2076 case FK_ConversionFailed:
2077 case FK_TooManyInitsForScalar:
2078 case FK_ReferenceBindingToInitList:
2079 case FK_InitListBadDestinationType:
2080 case FK_DefaultInitOfConst:
Douglas Gregor72a43bb2010-05-20 22:12:02 +00002081 case FK_Incomplete:
Douglas Gregorb70cf442010-03-26 20:14:36 +00002082 return false;
2083
2084 case FK_ReferenceInitOverloadFailed:
2085 case FK_UserConversionOverloadFailed:
2086 case FK_ConstructorOverloadFailed:
2087 return FailedOverloadResult == OR_Ambiguous;
2088 }
2089
2090 return false;
2091}
2092
Douglas Gregord6e44a32010-04-16 22:09:46 +00002093bool InitializationSequence::isConstructorInitialization() const {
2094 return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization;
2095}
2096
Douglas Gregor20093b42009-12-09 23:02:17 +00002097void InitializationSequence::AddAddressOverloadResolutionStep(
John McCall6bb80172010-03-30 21:47:33 +00002098 FunctionDecl *Function,
2099 DeclAccessPair Found) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002100 Step S;
2101 S.Kind = SK_ResolveAddressOfOverloadedFunction;
2102 S.Type = Function->getType();
John McCall9aa472c2010-03-19 07:35:19 +00002103 S.Function.Function = Function;
John McCall6bb80172010-03-30 21:47:33 +00002104 S.Function.FoundDecl = Found;
Douglas Gregor20093b42009-12-09 23:02:17 +00002105 Steps.push_back(S);
2106}
2107
2108void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
John McCall5baba9d2010-08-25 10:28:54 +00002109 ExprValueKind VK) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002110 Step S;
John McCall5baba9d2010-08-25 10:28:54 +00002111 switch (VK) {
2112 case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break;
2113 case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break;
2114 case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break;
Sebastian Redl906082e2010-07-20 04:20:21 +00002115 default: llvm_unreachable("No such category");
2116 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002117 S.Type = BaseType;
2118 Steps.push_back(S);
2119}
2120
2121void InitializationSequence::AddReferenceBindingStep(QualType T,
2122 bool BindingTemporary) {
2123 Step S;
2124 S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
2125 S.Type = T;
2126 Steps.push_back(S);
2127}
2128
Douglas Gregor523d46a2010-04-18 07:40:54 +00002129void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) {
2130 Step S;
2131 S.Kind = SK_ExtraneousCopyToTemporary;
2132 S.Type = T;
2133 Steps.push_back(S);
2134}
2135
Eli Friedman03981012009-12-11 02:42:07 +00002136void InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
John McCall9aa472c2010-03-19 07:35:19 +00002137 DeclAccessPair FoundDecl,
Eli Friedman03981012009-12-11 02:42:07 +00002138 QualType T) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002139 Step S;
2140 S.Kind = SK_UserConversion;
Eli Friedman03981012009-12-11 02:42:07 +00002141 S.Type = T;
John McCall9aa472c2010-03-19 07:35:19 +00002142 S.Function.Function = Function;
2143 S.Function.FoundDecl = FoundDecl;
Douglas Gregor20093b42009-12-09 23:02:17 +00002144 Steps.push_back(S);
2145}
2146
2147void InitializationSequence::AddQualificationConversionStep(QualType Ty,
John McCall5baba9d2010-08-25 10:28:54 +00002148 ExprValueKind VK) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002149 Step S;
John McCall38a4ffe2010-08-26 16:36:35 +00002150 S.Kind = SK_QualificationConversionRValue; // work around a gcc warning
John McCall5baba9d2010-08-25 10:28:54 +00002151 switch (VK) {
2152 case VK_RValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002153 S.Kind = SK_QualificationConversionRValue;
2154 break;
John McCall5baba9d2010-08-25 10:28:54 +00002155 case VK_XValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002156 S.Kind = SK_QualificationConversionXValue;
2157 break;
John McCall5baba9d2010-08-25 10:28:54 +00002158 case VK_LValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00002159 S.Kind = SK_QualificationConversionLValue;
2160 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00002161 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002162 S.Type = Ty;
2163 Steps.push_back(S);
2164}
2165
2166void InitializationSequence::AddConversionSequenceStep(
2167 const ImplicitConversionSequence &ICS,
2168 QualType T) {
2169 Step S;
2170 S.Kind = SK_ConversionSequence;
2171 S.Type = T;
2172 S.ICS = new ImplicitConversionSequence(ICS);
2173 Steps.push_back(S);
2174}
2175
Douglas Gregord87b61f2009-12-10 17:56:55 +00002176void InitializationSequence::AddListInitializationStep(QualType T) {
2177 Step S;
2178 S.Kind = SK_ListInitialization;
2179 S.Type = T;
2180 Steps.push_back(S);
2181}
2182
Douglas Gregor51c56d62009-12-14 20:49:26 +00002183void
2184InitializationSequence::AddConstructorInitializationStep(
2185 CXXConstructorDecl *Constructor,
John McCallb13b7372010-02-01 03:16:54 +00002186 AccessSpecifier Access,
Douglas Gregor51c56d62009-12-14 20:49:26 +00002187 QualType T) {
2188 Step S;
2189 S.Kind = SK_ConstructorInitialization;
2190 S.Type = T;
John McCall9aa472c2010-03-19 07:35:19 +00002191 S.Function.Function = Constructor;
2192 S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002193 Steps.push_back(S);
2194}
2195
Douglas Gregor71d17402009-12-15 00:01:57 +00002196void InitializationSequence::AddZeroInitializationStep(QualType T) {
2197 Step S;
2198 S.Kind = SK_ZeroInitialization;
2199 S.Type = T;
2200 Steps.push_back(S);
2201}
2202
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002203void InitializationSequence::AddCAssignmentStep(QualType T) {
2204 Step S;
2205 S.Kind = SK_CAssignment;
2206 S.Type = T;
2207 Steps.push_back(S);
2208}
2209
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002210void InitializationSequence::AddStringInitStep(QualType T) {
2211 Step S;
2212 S.Kind = SK_StringInit;
2213 S.Type = T;
2214 Steps.push_back(S);
2215}
2216
Douglas Gregor569c3162010-08-07 11:51:51 +00002217void InitializationSequence::AddObjCObjectConversionStep(QualType T) {
2218 Step S;
2219 S.Kind = SK_ObjCObjectConversion;
2220 S.Type = T;
2221 Steps.push_back(S);
2222}
2223
Douglas Gregor20093b42009-12-09 23:02:17 +00002224void InitializationSequence::SetOverloadFailure(FailureKind Failure,
2225 OverloadingResult Result) {
2226 SequenceKind = FailedSequence;
2227 this->Failure = Failure;
2228 this->FailedOverloadResult = Result;
2229}
2230
2231//===----------------------------------------------------------------------===//
2232// Attempt initialization
2233//===----------------------------------------------------------------------===//
2234
2235/// \brief Attempt list initialization (C++0x [dcl.init.list])
Douglas Gregord87b61f2009-12-10 17:56:55 +00002236static void TryListInitialization(Sema &S,
Douglas Gregor20093b42009-12-09 23:02:17 +00002237 const InitializedEntity &Entity,
2238 const InitializationKind &Kind,
2239 InitListExpr *InitList,
2240 InitializationSequence &Sequence) {
Douglas Gregord87b61f2009-12-10 17:56:55 +00002241 // FIXME: We only perform rudimentary checking of list
2242 // initializations at this point, then assume that any list
2243 // initialization of an array, aggregate, or scalar will be
Sebastian Redl36c28db2010-06-30 16:41:54 +00002244 // well-formed. When we actually "perform" list initialization, we'll
Douglas Gregord87b61f2009-12-10 17:56:55 +00002245 // do all of the necessary checking. C++0x initializer lists will
2246 // force us to perform more checking here.
2247 Sequence.setSequenceKind(InitializationSequence::ListInitialization);
2248
Douglas Gregord6542d82009-12-22 15:35:07 +00002249 QualType DestType = Entity.getType();
Douglas Gregord87b61f2009-12-10 17:56:55 +00002250
2251 // C++ [dcl.init]p13:
2252 // If T is a scalar type, then a declaration of the form
2253 //
2254 // T x = { a };
2255 //
2256 // is equivalent to
2257 //
2258 // T x = a;
2259 if (DestType->isScalarType()) {
2260 if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) {
2261 Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
2262 return;
2263 }
2264
2265 // Assume scalar initialization from a single value works.
2266 } else if (DestType->isAggregateType()) {
2267 // Assume aggregate initialization works.
2268 } else if (DestType->isVectorType()) {
2269 // Assume vector initialization works.
2270 } else if (DestType->isReferenceType()) {
2271 // FIXME: C++0x defines behavior for this.
2272 Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
2273 return;
2274 } else if (DestType->isRecordType()) {
2275 // FIXME: C++0x defines behavior for this
2276 Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType);
2277 }
2278
2279 // Add a general "list initialization" step.
2280 Sequence.AddListInitializationStep(DestType);
Douglas Gregor20093b42009-12-09 23:02:17 +00002281}
2282
2283/// \brief Try a reference initialization that involves calling a conversion
2284/// function.
Douglas Gregor20093b42009-12-09 23:02:17 +00002285static OverloadingResult TryRefInitWithConversionFunction(Sema &S,
2286 const InitializedEntity &Entity,
2287 const InitializationKind &Kind,
2288 Expr *Initializer,
2289 bool AllowRValues,
2290 InitializationSequence &Sequence) {
Douglas Gregord6542d82009-12-22 15:35:07 +00002291 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00002292 QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
2293 QualType T1 = cv1T1.getUnqualifiedType();
2294 QualType cv2T2 = Initializer->getType();
2295 QualType T2 = cv2T2.getUnqualifiedType();
2296
2297 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +00002298 bool ObjCConversion;
Douglas Gregor20093b42009-12-09 23:02:17 +00002299 assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
Douglas Gregor569c3162010-08-07 11:51:51 +00002300 T1, T2, DerivedToBase,
2301 ObjCConversion) &&
Douglas Gregor20093b42009-12-09 23:02:17 +00002302 "Must have incompatible references when binding via conversion");
Chandler Carruth60cfcec2009-12-13 01:37:04 +00002303 (void)DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +00002304 (void)ObjCConversion;
Douglas Gregor20093b42009-12-09 23:02:17 +00002305
2306 // Build the candidate set directly in the initialization sequence
2307 // structure, so that it will persist if we fail.
2308 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2309 CandidateSet.clear();
2310
2311 // Determine whether we are allowed to call explicit constructors or
2312 // explicit conversion operators.
2313 bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
2314
2315 const RecordType *T1RecordType = 0;
Douglas Gregor6b6d01f2010-05-07 19:42:26 +00002316 if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) &&
2317 !S.RequireCompleteType(Kind.getLocation(), T1, 0)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002318 // The type we're converting to is a class type. Enumerate its constructors
2319 // to see if there is a suitable conversion.
2320 CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
John McCall572fc622010-08-17 07:23:57 +00002321
Douglas Gregor20093b42009-12-09 23:02:17 +00002322 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregore5eee5a2010-07-02 23:12:18 +00002323 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl);
Douglas Gregor20093b42009-12-09 23:02:17 +00002324 Con != ConEnd; ++Con) {
John McCall9aa472c2010-03-19 07:35:19 +00002325 NamedDecl *D = *Con;
2326 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
2327
Douglas Gregor20093b42009-12-09 23:02:17 +00002328 // Find the constructor (which may be a template).
2329 CXXConstructorDecl *Constructor = 0;
John McCall9aa472c2010-03-19 07:35:19 +00002330 FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor20093b42009-12-09 23:02:17 +00002331 if (ConstructorTmpl)
2332 Constructor = cast<CXXConstructorDecl>(
2333 ConstructorTmpl->getTemplatedDecl());
2334 else
John McCall9aa472c2010-03-19 07:35:19 +00002335 Constructor = cast<CXXConstructorDecl>(D);
Douglas Gregor20093b42009-12-09 23:02:17 +00002336
2337 if (!Constructor->isInvalidDecl() &&
2338 Constructor->isConvertingConstructor(AllowExplicit)) {
2339 if (ConstructorTmpl)
John McCall9aa472c2010-03-19 07:35:19 +00002340 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
John McCall86820f52010-01-26 01:37:31 +00002341 /*ExplicitArgs*/ 0,
Argyrios Kyrtzidisb72db892010-10-05 03:05:30 +00002342 &Initializer, 1, CandidateSet,
2343 /*SuppressUserConversions=*/true);
Douglas Gregor20093b42009-12-09 23:02:17 +00002344 else
John McCall9aa472c2010-03-19 07:35:19 +00002345 S.AddOverloadCandidate(Constructor, FoundDecl,
Argyrios Kyrtzidisb72db892010-10-05 03:05:30 +00002346 &Initializer, 1, CandidateSet,
2347 /*SuppressUserConversions=*/true);
Douglas Gregor20093b42009-12-09 23:02:17 +00002348 }
2349 }
2350 }
John McCall572fc622010-08-17 07:23:57 +00002351 if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl())
2352 return OR_No_Viable_Function;
Douglas Gregor20093b42009-12-09 23:02:17 +00002353
Douglas Gregor6b6d01f2010-05-07 19:42:26 +00002354 const RecordType *T2RecordType = 0;
2355 if ((T2RecordType = T2->getAs<RecordType>()) &&
2356 !S.RequireCompleteType(Kind.getLocation(), T2, 0)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002357 // The type we're converting from is a class type, enumerate its conversion
2358 // functions.
2359 CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
2360
2361 // Determine the type we are converting to. If we are allowed to
2362 // convert to an rvalue, take the type that the destination type
2363 // refers to.
2364 QualType ToType = AllowRValues? cv1T1 : DestType;
2365
John McCalleec51cf2010-01-20 00:46:10 +00002366 const UnresolvedSetImpl *Conversions
Douglas Gregor20093b42009-12-09 23:02:17 +00002367 = T2RecordDecl->getVisibleConversionFunctions();
John McCalleec51cf2010-01-20 00:46:10 +00002368 for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
2369 E = Conversions->end(); I != E; ++I) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002370 NamedDecl *D = *I;
2371 CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
2372 if (isa<UsingShadowDecl>(D))
2373 D = cast<UsingShadowDecl>(D)->getTargetDecl();
2374
2375 FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2376 CXXConversionDecl *Conv;
2377 if (ConvTemplate)
2378 Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
2379 else
Sebastian Redl4680bf22010-06-30 18:13:39 +00002380 Conv = cast<CXXConversionDecl>(D);
Douglas Gregor20093b42009-12-09 23:02:17 +00002381
2382 // If the conversion function doesn't return a reference type,
2383 // it can't be considered for this conversion unless we're allowed to
2384 // consider rvalues.
2385 // FIXME: Do we need to make sure that we only consider conversion
2386 // candidates with reference-compatible results? That might be needed to
2387 // break recursion.
2388 if ((AllowExplicit || !Conv->isExplicit()) &&
2389 (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
2390 if (ConvTemplate)
John McCall9aa472c2010-03-19 07:35:19 +00002391 S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
John McCall86820f52010-01-26 01:37:31 +00002392 ActingDC, Initializer,
Douglas Gregor20093b42009-12-09 23:02:17 +00002393 ToType, CandidateSet);
2394 else
John McCall9aa472c2010-03-19 07:35:19 +00002395 S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
Douglas Gregor692f85c2010-02-26 01:17:27 +00002396 Initializer, ToType, CandidateSet);
Douglas Gregor20093b42009-12-09 23:02:17 +00002397 }
2398 }
2399 }
John McCall572fc622010-08-17 07:23:57 +00002400 if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl())
2401 return OR_No_Viable_Function;
Douglas Gregor20093b42009-12-09 23:02:17 +00002402
2403 SourceLocation DeclLoc = Initializer->getLocStart();
2404
2405 // Perform overload resolution. If it fails, return the failed result.
2406 OverloadCandidateSet::iterator Best;
2407 if (OverloadingResult Result
Douglas Gregor8fcc5162010-09-12 08:07:23 +00002408 = CandidateSet.BestViableFunction(S, DeclLoc, Best, true))
Douglas Gregor20093b42009-12-09 23:02:17 +00002409 return Result;
Eli Friedman03981012009-12-11 02:42:07 +00002410
Douglas Gregor20093b42009-12-09 23:02:17 +00002411 FunctionDecl *Function = Best->Function;
Eli Friedman03981012009-12-11 02:42:07 +00002412
2413 // Compute the returned type of the conversion.
Douglas Gregor20093b42009-12-09 23:02:17 +00002414 if (isa<CXXConversionDecl>(Function))
2415 T2 = Function->getResultType();
2416 else
2417 T2 = cv1T1;
Eli Friedman03981012009-12-11 02:42:07 +00002418
2419 // Add the user-defined conversion step.
John McCall9aa472c2010-03-19 07:35:19 +00002420 Sequence.AddUserConversionStep(Function, Best->FoundDecl,
Douglas Gregor63982352010-07-13 18:40:04 +00002421 T2.getNonLValueExprType(S.Context));
Eli Friedman03981012009-12-11 02:42:07 +00002422
2423 // Determine whether we need to perform derived-to-base or
2424 // cv-qualification adjustments.
John McCall5baba9d2010-08-25 10:28:54 +00002425 ExprValueKind VK = VK_RValue;
Sebastian Redl906082e2010-07-20 04:20:21 +00002426 if (T2->isLValueReferenceType())
John McCall5baba9d2010-08-25 10:28:54 +00002427 VK = VK_LValue;
Sebastian Redl906082e2010-07-20 04:20:21 +00002428 else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>())
John McCall5baba9d2010-08-25 10:28:54 +00002429 VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue;
Sebastian Redl906082e2010-07-20 04:20:21 +00002430
Douglas Gregor20093b42009-12-09 23:02:17 +00002431 bool NewDerivedToBase = false;
Douglas Gregor569c3162010-08-07 11:51:51 +00002432 bool NewObjCConversion = false;
Douglas Gregor20093b42009-12-09 23:02:17 +00002433 Sema::ReferenceCompareResult NewRefRelationship
Douglas Gregor63982352010-07-13 18:40:04 +00002434 = S.CompareReferenceRelationship(DeclLoc, T1,
2435 T2.getNonLValueExprType(S.Context),
Douglas Gregor569c3162010-08-07 11:51:51 +00002436 NewDerivedToBase, NewObjCConversion);
Douglas Gregora1a9f032010-03-07 23:17:44 +00002437 if (NewRefRelationship == Sema::Ref_Incompatible) {
2438 // If the type we've converted to is not reference-related to the
2439 // type we're looking for, then there is another conversion step
2440 // we need to perform to produce a temporary of the right type
2441 // that we'll be binding to.
2442 ImplicitConversionSequence ICS;
2443 ICS.setStandard();
2444 ICS.Standard = Best->FinalConversion;
2445 T2 = ICS.Standard.getToType(2);
2446 Sequence.AddConversionSequenceStep(ICS, T2);
2447 } else if (NewDerivedToBase)
Douglas Gregor20093b42009-12-09 23:02:17 +00002448 Sequence.AddDerivedToBaseCastStep(
2449 S.Context.getQualifiedType(T1,
2450 T2.getNonReferenceType().getQualifiers()),
John McCall5baba9d2010-08-25 10:28:54 +00002451 VK);
Douglas Gregor569c3162010-08-07 11:51:51 +00002452 else if (NewObjCConversion)
2453 Sequence.AddObjCObjectConversionStep(
2454 S.Context.getQualifiedType(T1,
2455 T2.getNonReferenceType().getQualifiers()));
2456
Douglas Gregor20093b42009-12-09 23:02:17 +00002457 if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
John McCall5baba9d2010-08-25 10:28:54 +00002458 Sequence.AddQualificationConversionStep(cv1T1, VK);
Douglas Gregor20093b42009-12-09 23:02:17 +00002459
2460 Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
2461 return OR_Success;
2462}
2463
Sebastian Redl4680bf22010-06-30 18:13:39 +00002464/// \brief Attempt reference initialization (C++0x [dcl.init.ref])
Douglas Gregor20093b42009-12-09 23:02:17 +00002465static void TryReferenceInitialization(Sema &S,
2466 const InitializedEntity &Entity,
2467 const InitializationKind &Kind,
2468 Expr *Initializer,
2469 InitializationSequence &Sequence) {
2470 Sequence.setSequenceKind(InitializationSequence::ReferenceBinding);
Sebastian Redl4680bf22010-06-30 18:13:39 +00002471
Douglas Gregord6542d82009-12-22 15:35:07 +00002472 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00002473 QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
Chandler Carruth5535c382010-01-12 20:32:25 +00002474 Qualifiers T1Quals;
2475 QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
Douglas Gregor20093b42009-12-09 23:02:17 +00002476 QualType cv2T2 = Initializer->getType();
Chandler Carruth5535c382010-01-12 20:32:25 +00002477 Qualifiers T2Quals;
2478 QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
Douglas Gregor20093b42009-12-09 23:02:17 +00002479 SourceLocation DeclLoc = Initializer->getLocStart();
Sebastian Redl4680bf22010-06-30 18:13:39 +00002480
Douglas Gregor20093b42009-12-09 23:02:17 +00002481 // If the initializer is the address of an overloaded function, try
2482 // to resolve the overloaded function. If all goes well, T2 is the
2483 // type of the resulting function.
2484 if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) {
John McCall6bb80172010-03-30 21:47:33 +00002485 DeclAccessPair Found;
Douglas Gregor20093b42009-12-09 23:02:17 +00002486 FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer,
2487 T1,
John McCall6bb80172010-03-30 21:47:33 +00002488 false,
2489 Found);
Douglas Gregor20093b42009-12-09 23:02:17 +00002490 if (!Fn) {
2491 Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
2492 return;
2493 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002494
John McCall6bb80172010-03-30 21:47:33 +00002495 Sequence.AddAddressOverloadResolutionStep(Fn, Found);
Douglas Gregor20093b42009-12-09 23:02:17 +00002496 cv2T2 = Fn->getType();
2497 T2 = cv2T2.getUnqualifiedType();
2498 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002499
Douglas Gregor20093b42009-12-09 23:02:17 +00002500 // Compute some basic properties of the types and the initializer.
2501 bool isLValueRef = DestType->isLValueReferenceType();
2502 bool isRValueRef = !isLValueRef;
2503 bool DerivedToBase = false;
Douglas Gregor569c3162010-08-07 11:51:51 +00002504 bool ObjCConversion = false;
Sebastian Redl4680bf22010-06-30 18:13:39 +00002505 Expr::Classification InitCategory = Initializer->Classify(S.Context);
Douglas Gregor20093b42009-12-09 23:02:17 +00002506 Sema::ReferenceCompareResult RefRelationship
Douglas Gregor569c3162010-08-07 11:51:51 +00002507 = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase,
2508 ObjCConversion);
Sebastian Redl4680bf22010-06-30 18:13:39 +00002509
Douglas Gregor20093b42009-12-09 23:02:17 +00002510 // C++0x [dcl.init.ref]p5:
2511 // A reference to type "cv1 T1" is initialized by an expression of type
2512 // "cv2 T2" as follows:
2513 //
2514 // - If the reference is an lvalue reference and the initializer
2515 // expression
Sebastian Redl4680bf22010-06-30 18:13:39 +00002516 // Note the analogous bullet points for rvlaue refs to functions. Because
2517 // there are no function rvalues in C++, rvalue refs to functions are treated
2518 // like lvalue refs.
Douglas Gregor20093b42009-12-09 23:02:17 +00002519 OverloadingResult ConvOvlResult = OR_Success;
Sebastian Redl4680bf22010-06-30 18:13:39 +00002520 bool T1Function = T1->isFunctionType();
2521 if (isLValueRef || T1Function) {
2522 if (InitCategory.isLValue() &&
Douglas Gregor20093b42009-12-09 23:02:17 +00002523 RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
2524 // - is an lvalue (but is not a bit-field), and "cv1 T1" is
2525 // reference-compatible with "cv2 T2," or
2526 //
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002527 // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a
Douglas Gregor20093b42009-12-09 23:02:17 +00002528 // bit-field when we're determining whether the reference initialization
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002529 // can occur. However, we do pay attention to whether it is a bit-field
2530 // to decide whether we're actually binding to a temporary created from
2531 // the bit-field.
Douglas Gregor20093b42009-12-09 23:02:17 +00002532 if (DerivedToBase)
2533 Sequence.AddDerivedToBaseCastStep(
Chandler Carruth5535c382010-01-12 20:32:25 +00002534 S.Context.getQualifiedType(T1, T2Quals),
John McCall5baba9d2010-08-25 10:28:54 +00002535 VK_LValue);
Douglas Gregor569c3162010-08-07 11:51:51 +00002536 else if (ObjCConversion)
2537 Sequence.AddObjCObjectConversionStep(
2538 S.Context.getQualifiedType(T1, T2Quals));
2539
Chandler Carruth5535c382010-01-12 20:32:25 +00002540 if (T1Quals != T2Quals)
John McCall5baba9d2010-08-25 10:28:54 +00002541 Sequence.AddQualificationConversionStep(cv1T1, VK_LValue);
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002542 bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() &&
Anders Carlsson09380262010-01-31 17:18:49 +00002543 (Initializer->getBitField() || Initializer->refersToVectorElement());
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002544 Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary);
Douglas Gregor20093b42009-12-09 23:02:17 +00002545 return;
2546 }
2547
2548 // - has a class type (i.e., T2 is a class type), where T1 is not
2549 // reference-related to T2, and can be implicitly converted to an
2550 // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
2551 // with "cv3 T3" (this conversion is selected by enumerating the
2552 // applicable conversion functions (13.3.1.6) and choosing the best
2553 // one through overload resolution (13.3)),
Sebastian Redl4680bf22010-06-30 18:13:39 +00002554 // If we have an rvalue ref to function type here, the rhs must be
2555 // an rvalue.
2556 if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
2557 (isLValueRef || InitCategory.isRValue())) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002558 ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
2559 Initializer,
Sebastian Redl4680bf22010-06-30 18:13:39 +00002560 /*AllowRValues=*/isRValueRef,
Douglas Gregor20093b42009-12-09 23:02:17 +00002561 Sequence);
2562 if (ConvOvlResult == OR_Success)
2563 return;
John McCall1d318332010-01-12 00:44:57 +00002564 if (ConvOvlResult != OR_No_Viable_Function) {
2565 Sequence.SetOverloadFailure(
2566 InitializationSequence::FK_ReferenceInitOverloadFailed,
2567 ConvOvlResult);
2568 }
Douglas Gregor20093b42009-12-09 23:02:17 +00002569 }
2570 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002571
Douglas Gregor20093b42009-12-09 23:02:17 +00002572 // - Otherwise, the reference shall be an lvalue reference to a
2573 // non-volatile const type (i.e., cv1 shall be const), or the reference
2574 // shall be an rvalue reference and the initializer expression shall
Sebastian Redl4680bf22010-06-30 18:13:39 +00002575 // be an rvalue or have a function type.
2576 // We handled the function type stuff above.
Douglas Gregoref06e242010-01-29 19:39:15 +00002577 if (!((isLValueRef && T1Quals.hasConst() && !T1Quals.hasVolatile()) ||
Sebastian Redl4680bf22010-06-30 18:13:39 +00002578 (isRValueRef && InitCategory.isRValue()))) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002579 if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
2580 Sequence.SetOverloadFailure(
2581 InitializationSequence::FK_ReferenceInitOverloadFailed,
2582 ConvOvlResult);
2583 else if (isLValueRef)
Sebastian Redl4680bf22010-06-30 18:13:39 +00002584 Sequence.SetFailed(InitCategory.isLValue()
Douglas Gregor20093b42009-12-09 23:02:17 +00002585 ? (RefRelationship == Sema::Ref_Related
2586 ? InitializationSequence::FK_ReferenceInitDropsQualifiers
2587 : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
2588 : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
2589 else
2590 Sequence.SetFailed(
2591 InitializationSequence::FK_RValueReferenceBindingToLValue);
Sebastian Redl4680bf22010-06-30 18:13:39 +00002592
Douglas Gregor20093b42009-12-09 23:02:17 +00002593 return;
2594 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002595
2596 // - [If T1 is not a function type], if T2 is a class type and
2597 if (!T1Function && T2->isRecordType()) {
Sebastian Redl66d0acd2010-07-26 17:52:21 +00002598 bool isXValue = InitCategory.isXValue();
Douglas Gregor20093b42009-12-09 23:02:17 +00002599 // - the initializer expression is an rvalue and "cv1 T1" is
2600 // reference-compatible with "cv2 T2", or
Sebastian Redl4680bf22010-06-30 18:13:39 +00002601 if (InitCategory.isRValue() &&
Douglas Gregor20093b42009-12-09 23:02:17 +00002602 RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
Douglas Gregor523d46a2010-04-18 07:40:54 +00002603 // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the
2604 // compiler the freedom to perform a copy here or bind to the
2605 // object, while C++0x requires that we bind directly to the
2606 // object. Hence, we always bind to the object without making an
2607 // extra copy. However, in C++03 requires that we check for the
2608 // presence of a suitable copy constructor:
2609 //
2610 // The constructor that would be used to make the copy shall
2611 // be callable whether or not the copy is actually done.
2612 if (!S.getLangOptions().CPlusPlus0x)
2613 Sequence.AddExtraneousCopyToTemporary(cv2T2);
2614
Douglas Gregor20093b42009-12-09 23:02:17 +00002615 if (DerivedToBase)
2616 Sequence.AddDerivedToBaseCastStep(
Chandler Carruth5535c382010-01-12 20:32:25 +00002617 S.Context.getQualifiedType(T1, T2Quals),
John McCall5baba9d2010-08-25 10:28:54 +00002618 isXValue ? VK_XValue : VK_RValue);
Douglas Gregor569c3162010-08-07 11:51:51 +00002619 else if (ObjCConversion)
2620 Sequence.AddObjCObjectConversionStep(
2621 S.Context.getQualifiedType(T1, T2Quals));
2622
Chandler Carruth5535c382010-01-12 20:32:25 +00002623 if (T1Quals != T2Quals)
Sebastian Redl66d0acd2010-07-26 17:52:21 +00002624 Sequence.AddQualificationConversionStep(cv1T1,
John McCall5baba9d2010-08-25 10:28:54 +00002625 isXValue ? VK_XValue : VK_RValue);
Sebastian Redl66d0acd2010-07-26 17:52:21 +00002626 Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/!isXValue);
Douglas Gregor20093b42009-12-09 23:02:17 +00002627 return;
2628 }
Sebastian Redl4680bf22010-06-30 18:13:39 +00002629
Douglas Gregor20093b42009-12-09 23:02:17 +00002630 // - T1 is not reference-related to T2 and the initializer expression
2631 // can be implicitly converted to an rvalue of type "cv3 T3" (this
2632 // conversion is selected by enumerating the applicable conversion
2633 // functions (13.3.1.6) and choosing the best one through overload
2634 // resolution (13.3)),
2635 if (RefRelationship == Sema::Ref_Incompatible) {
2636 ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
2637 Kind, Initializer,
2638 /*AllowRValues=*/true,
2639 Sequence);
2640 if (ConvOvlResult)
2641 Sequence.SetOverloadFailure(
2642 InitializationSequence::FK_ReferenceInitOverloadFailed,
2643 ConvOvlResult);
2644
2645 return;
2646 }
2647
2648 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2649 return;
2650 }
2651
2652 // - If the initializer expression is an rvalue, with T2 an array type,
2653 // and "cv1 T1" is reference-compatible with "cv2 T2," the reference
2654 // is bound to the object represented by the rvalue (see 3.10).
2655 // FIXME: How can an array type be reference-compatible with anything?
2656 // Don't we mean the element types of T1 and T2?
2657
2658 // - Otherwise, a temporary of type “cv1 T1” is created and initialized
2659 // from the initializer expression using the rules for a non-reference
2660 // copy initialization (8.5). The reference is then bound to the
2661 // temporary. [...]
John McCall369371c2010-06-04 02:29:22 +00002662
Douglas Gregor20093b42009-12-09 23:02:17 +00002663 // Determine whether we are allowed to call explicit constructors or
2664 // explicit conversion operators.
2665 bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct);
John McCall369371c2010-06-04 02:29:22 +00002666
2667 InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
2668
2669 if (S.TryImplicitConversion(Sequence, TempEntity, Initializer,
2670 /*SuppressUserConversions*/ false,
2671 AllowExplicit,
2672 /*FIXME:InOverloadResolution=*/false)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002673 // FIXME: Use the conversion function set stored in ICS to turn
2674 // this into an overloading ambiguity diagnostic. However, we need
2675 // to keep that set as an OverloadCandidateSet rather than as some
2676 // other kind of set.
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002677 if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
2678 Sequence.SetOverloadFailure(
2679 InitializationSequence::FK_ReferenceInitOverloadFailed,
2680 ConvOvlResult);
2681 else
2682 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
Douglas Gregor20093b42009-12-09 23:02:17 +00002683 return;
2684 }
2685
2686 // [...] If T1 is reference-related to T2, cv1 must be the
2687 // same cv-qualification as, or greater cv-qualification
2688 // than, cv2; otherwise, the program is ill-formed.
Chandler Carruth5535c382010-01-12 20:32:25 +00002689 unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
2690 unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
Douglas Gregor20093b42009-12-09 23:02:17 +00002691 if (RefRelationship == Sema::Ref_Related &&
Chandler Carruth5535c382010-01-12 20:32:25 +00002692 (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
Douglas Gregor20093b42009-12-09 23:02:17 +00002693 Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2694 return;
2695 }
2696
Douglas Gregor20093b42009-12-09 23:02:17 +00002697 Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
2698 return;
2699}
2700
2701/// \brief Attempt character array initialization from a string literal
2702/// (C++ [dcl.init.string], C99 6.7.8).
2703static void TryStringLiteralInitialization(Sema &S,
2704 const InitializedEntity &Entity,
2705 const InitializationKind &Kind,
2706 Expr *Initializer,
2707 InitializationSequence &Sequence) {
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002708 Sequence.setSequenceKind(InitializationSequence::StringInit);
Douglas Gregord6542d82009-12-22 15:35:07 +00002709 Sequence.AddStringInitStep(Entity.getType());
Douglas Gregor20093b42009-12-09 23:02:17 +00002710}
2711
Douglas Gregor20093b42009-12-09 23:02:17 +00002712/// \brief Attempt initialization by constructor (C++ [dcl.init]), which
2713/// enumerates the constructors of the initialized entity and performs overload
2714/// resolution to select the best.
2715static void TryConstructorInitialization(Sema &S,
2716 const InitializedEntity &Entity,
2717 const InitializationKind &Kind,
2718 Expr **Args, unsigned NumArgs,
Douglas Gregor71d17402009-12-15 00:01:57 +00002719 QualType DestType,
Douglas Gregor20093b42009-12-09 23:02:17 +00002720 InitializationSequence &Sequence) {
Douglas Gregor2f599792010-04-02 18:24:57 +00002721 Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002722
2723 // Build the candidate set directly in the initialization sequence
2724 // structure, so that it will persist if we fail.
2725 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2726 CandidateSet.clear();
2727
2728 // Determine whether we are allowed to call explicit constructors or
2729 // explicit conversion operators.
2730 bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct ||
2731 Kind.getKind() == InitializationKind::IK_Value ||
Douglas Gregor2f599792010-04-02 18:24:57 +00002732 Kind.getKind() == InitializationKind::IK_Default);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002733
2734 // The type we're constructing needs to be complete.
2735 if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Douglas Gregor72a43bb2010-05-20 22:12:02 +00002736 Sequence.SetFailed(InitializationSequence::FK_Incomplete);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002737 return;
2738 }
Douglas Gregor51c56d62009-12-14 20:49:26 +00002739
2740 // The type we're converting to is a class type. Enumerate its constructors
2741 // to see if one is suitable.
Douglas Gregor51c56d62009-12-14 20:49:26 +00002742 const RecordType *DestRecordType = DestType->getAs<RecordType>();
2743 assert(DestRecordType && "Constructor initialization requires record type");
2744 CXXRecordDecl *DestRecordDecl
2745 = cast<CXXRecordDecl>(DestRecordType->getDecl());
2746
Douglas Gregor51c56d62009-12-14 20:49:26 +00002747 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregore5eee5a2010-07-02 23:12:18 +00002748 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002749 Con != ConEnd; ++Con) {
John McCall9aa472c2010-03-19 07:35:19 +00002750 NamedDecl *D = *Con;
2751 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
Douglas Gregord1a27222010-04-24 20:54:38 +00002752 bool SuppressUserConversions = false;
2753
Douglas Gregor51c56d62009-12-14 20:49:26 +00002754 // Find the constructor (which may be a template).
2755 CXXConstructorDecl *Constructor = 0;
John McCall9aa472c2010-03-19 07:35:19 +00002756 FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002757 if (ConstructorTmpl)
2758 Constructor = cast<CXXConstructorDecl>(
2759 ConstructorTmpl->getTemplatedDecl());
Douglas Gregord1a27222010-04-24 20:54:38 +00002760 else {
John McCall9aa472c2010-03-19 07:35:19 +00002761 Constructor = cast<CXXConstructorDecl>(D);
Douglas Gregord1a27222010-04-24 20:54:38 +00002762
2763 // If we're performing copy initialization using a copy constructor, we
2764 // suppress user-defined conversions on the arguments.
2765 // FIXME: Move constructors?
2766 if (Kind.getKind() == InitializationKind::IK_Copy &&
2767 Constructor->isCopyConstructor())
2768 SuppressUserConversions = true;
2769 }
Douglas Gregor51c56d62009-12-14 20:49:26 +00002770
2771 if (!Constructor->isInvalidDecl() &&
Douglas Gregor99a2e602009-12-16 01:38:02 +00002772 (AllowExplicit || !Constructor->isExplicit())) {
Douglas Gregor51c56d62009-12-14 20:49:26 +00002773 if (ConstructorTmpl)
John McCall9aa472c2010-03-19 07:35:19 +00002774 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
John McCall86820f52010-01-26 01:37:31 +00002775 /*ExplicitArgs*/ 0,
Douglas Gregord1a27222010-04-24 20:54:38 +00002776 Args, NumArgs, CandidateSet,
2777 SuppressUserConversions);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002778 else
John McCall9aa472c2010-03-19 07:35:19 +00002779 S.AddOverloadCandidate(Constructor, FoundDecl,
Douglas Gregord1a27222010-04-24 20:54:38 +00002780 Args, NumArgs, CandidateSet,
2781 SuppressUserConversions);
Douglas Gregor51c56d62009-12-14 20:49:26 +00002782 }
2783 }
2784
2785 SourceLocation DeclLoc = Kind.getLocation();
2786
2787 // Perform overload resolution. If it fails, return the failed result.
2788 OverloadCandidateSet::iterator Best;
2789 if (OverloadingResult Result
John McCall120d63c2010-08-24 20:38:10 +00002790 = CandidateSet.BestViableFunction(S, DeclLoc, Best)) {
Douglas Gregor51c56d62009-12-14 20:49:26 +00002791 Sequence.SetOverloadFailure(
2792 InitializationSequence::FK_ConstructorOverloadFailed,
2793 Result);
2794 return;
2795 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002796
2797 // C++0x [dcl.init]p6:
2798 // If a program calls for the default initialization of an object
2799 // of a const-qualified type T, T shall be a class type with a
2800 // user-provided default constructor.
2801 if (Kind.getKind() == InitializationKind::IK_Default &&
2802 Entity.getType().isConstQualified() &&
2803 cast<CXXConstructorDecl>(Best->Function)->isImplicit()) {
2804 Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2805 return;
2806 }
2807
Douglas Gregor51c56d62009-12-14 20:49:26 +00002808 // Add the constructor initialization step. Any cv-qualification conversion is
2809 // subsumed by the initialization.
Douglas Gregor2f599792010-04-02 18:24:57 +00002810 Sequence.AddConstructorInitializationStep(
Douglas Gregor51c56d62009-12-14 20:49:26 +00002811 cast<CXXConstructorDecl>(Best->Function),
John McCall9aa472c2010-03-19 07:35:19 +00002812 Best->FoundDecl.getAccess(),
Douglas Gregor18ef5e22009-12-18 05:02:21 +00002813 DestType);
Douglas Gregor20093b42009-12-09 23:02:17 +00002814}
2815
Douglas Gregor71d17402009-12-15 00:01:57 +00002816/// \brief Attempt value initialization (C++ [dcl.init]p7).
2817static void TryValueInitialization(Sema &S,
2818 const InitializedEntity &Entity,
2819 const InitializationKind &Kind,
2820 InitializationSequence &Sequence) {
2821 // C++ [dcl.init]p5:
2822 //
2823 // To value-initialize an object of type T means:
Douglas Gregord6542d82009-12-22 15:35:07 +00002824 QualType T = Entity.getType();
Douglas Gregor71d17402009-12-15 00:01:57 +00002825
2826 // -- if T is an array type, then each element is value-initialized;
2827 while (const ArrayType *AT = S.Context.getAsArrayType(T))
2828 T = AT->getElementType();
2829
2830 if (const RecordType *RT = T->getAs<RecordType>()) {
2831 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2832 // -- if T is a class type (clause 9) with a user-declared
2833 // constructor (12.1), then the default constructor for T is
2834 // called (and the initialization is ill-formed if T has no
2835 // accessible default constructor);
2836 //
2837 // FIXME: we really want to refer to a single subobject of the array,
2838 // but Entity doesn't have a way to capture that (yet).
2839 if (ClassDecl->hasUserDeclaredConstructor())
2840 return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
2841
Douglas Gregor16006c92009-12-16 18:50:27 +00002842 // -- if T is a (possibly cv-qualified) non-union class type
2843 // without a user-provided constructor, then the object is
2844 // zero-initialized and, if T’s implicitly-declared default
2845 // constructor is non-trivial, that constructor is called.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002846 if ((ClassDecl->getTagKind() == TTK_Class ||
Douglas Gregored8abf12010-07-08 06:14:04 +00002847 ClassDecl->getTagKind() == TTK_Struct)) {
Douglas Gregord6542d82009-12-22 15:35:07 +00002848 Sequence.AddZeroInitializationStep(Entity.getType());
Douglas Gregor16006c92009-12-16 18:50:27 +00002849 return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
2850 }
Douglas Gregor71d17402009-12-15 00:01:57 +00002851 }
2852 }
2853
Douglas Gregord6542d82009-12-22 15:35:07 +00002854 Sequence.AddZeroInitializationStep(Entity.getType());
Douglas Gregor71d17402009-12-15 00:01:57 +00002855 Sequence.setSequenceKind(InitializationSequence::ZeroInitialization);
2856}
2857
Douglas Gregor99a2e602009-12-16 01:38:02 +00002858/// \brief Attempt default initialization (C++ [dcl.init]p6).
2859static void TryDefaultInitialization(Sema &S,
2860 const InitializedEntity &Entity,
2861 const InitializationKind &Kind,
2862 InitializationSequence &Sequence) {
2863 assert(Kind.getKind() == InitializationKind::IK_Default);
2864
2865 // C++ [dcl.init]p6:
2866 // To default-initialize an object of type T means:
2867 // - if T is an array type, each element is default-initialized;
Douglas Gregord6542d82009-12-22 15:35:07 +00002868 QualType DestType = Entity.getType();
Douglas Gregor99a2e602009-12-16 01:38:02 +00002869 while (const ArrayType *Array = S.Context.getAsArrayType(DestType))
2870 DestType = Array->getElementType();
2871
2872 // - if T is a (possibly cv-qualified) class type (Clause 9), the default
2873 // constructor for T is called (and the initialization is ill-formed if
2874 // T has no accessible default constructor);
Douglas Gregor60c93c92010-02-09 07:26:29 +00002875 if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) {
Chandler Carruth4e6fbce2010-08-23 07:55:51 +00002876 TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence);
2877 return;
Douglas Gregor99a2e602009-12-16 01:38:02 +00002878 }
2879
2880 // - otherwise, no initialization is performed.
2881 Sequence.setSequenceKind(InitializationSequence::NoInitialization);
2882
2883 // If a program calls for the default initialization of an object of
2884 // a const-qualified type T, T shall be a class type with a user-provided
2885 // default constructor.
Douglas Gregor60c93c92010-02-09 07:26:29 +00002886 if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus)
Douglas Gregor99a2e602009-12-16 01:38:02 +00002887 Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2888}
2889
Douglas Gregor20093b42009-12-09 23:02:17 +00002890/// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
2891/// which enumerates all conversion functions and performs overload resolution
2892/// to select the best.
2893static void TryUserDefinedConversion(Sema &S,
2894 const InitializedEntity &Entity,
2895 const InitializationKind &Kind,
2896 Expr *Initializer,
2897 InitializationSequence &Sequence) {
Douglas Gregor4a520a22009-12-14 17:27:33 +00002898 Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
2899
Douglas Gregord6542d82009-12-22 15:35:07 +00002900 QualType DestType = Entity.getType();
Douglas Gregor4a520a22009-12-14 17:27:33 +00002901 assert(!DestType->isReferenceType() && "References are handled elsewhere");
2902 QualType SourceType = Initializer->getType();
2903 assert((DestType->isRecordType() || SourceType->isRecordType()) &&
2904 "Must have a class type to perform a user-defined conversion");
2905
2906 // Build the candidate set directly in the initialization sequence
2907 // structure, so that it will persist if we fail.
2908 OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2909 CandidateSet.clear();
2910
2911 // Determine whether we are allowed to call explicit constructors or
2912 // explicit conversion operators.
2913 bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
2914
2915 if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
2916 // The type we're converting to is a class type. Enumerate its constructors
2917 // to see if there is a suitable conversion.
2918 CXXRecordDecl *DestRecordDecl
2919 = cast<CXXRecordDecl>(DestRecordType->getDecl());
2920
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002921 // Try to complete the type we're converting to.
2922 if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002923 DeclContext::lookup_iterator Con, ConEnd;
Douglas Gregore5eee5a2010-07-02 23:12:18 +00002924 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002925 Con != ConEnd; ++Con) {
2926 NamedDecl *D = *Con;
2927 DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
Douglas Gregord1a27222010-04-24 20:54:38 +00002928
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002929 // Find the constructor (which may be a template).
2930 CXXConstructorDecl *Constructor = 0;
2931 FunctionTemplateDecl *ConstructorTmpl
2932 = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregor4a520a22009-12-14 17:27:33 +00002933 if (ConstructorTmpl)
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002934 Constructor = cast<CXXConstructorDecl>(
2935 ConstructorTmpl->getTemplatedDecl());
Douglas Gregor4712c022010-07-01 03:43:00 +00002936 else
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002937 Constructor = cast<CXXConstructorDecl>(D);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002938
2939 if (!Constructor->isInvalidDecl() &&
2940 Constructor->isConvertingConstructor(AllowExplicit)) {
2941 if (ConstructorTmpl)
2942 S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
2943 /*ExplicitArgs*/ 0,
2944 &Initializer, 1, CandidateSet,
Douglas Gregor4712c022010-07-01 03:43:00 +00002945 /*SuppressUserConversions=*/true);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002946 else
2947 S.AddOverloadCandidate(Constructor, FoundDecl,
2948 &Initializer, 1, CandidateSet,
Douglas Gregor4712c022010-07-01 03:43:00 +00002949 /*SuppressUserConversions=*/true);
Douglas Gregor087fb7d2010-04-26 14:36:57 +00002950 }
2951 }
2952 }
Douglas Gregor4a520a22009-12-14 17:27:33 +00002953 }
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002954
2955 SourceLocation DeclLoc = Initializer->getLocStart();
2956
Douglas Gregor4a520a22009-12-14 17:27:33 +00002957 if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
2958 // The type we're converting from is a class type, enumerate its conversion
2959 // functions.
Eli Friedmancfdc81a2009-12-19 08:11:05 +00002960
Eli Friedman33c2da92009-12-20 22:12:03 +00002961 // We can only enumerate the conversion functions for a complete type; if
2962 // the type isn't complete, simply skip this step.
2963 if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
2964 CXXRecordDecl *SourceRecordDecl
2965 = cast<CXXRecordDecl>(SourceRecordType->getDecl());
Douglas Gregor4a520a22009-12-14 17:27:33 +00002966
John McCalleec51cf2010-01-20 00:46:10 +00002967 const UnresolvedSetImpl *Conversions
Eli Friedman33c2da92009-12-20 22:12:03 +00002968 = SourceRecordDecl->getVisibleConversionFunctions();
John McCalleec51cf2010-01-20 00:46:10 +00002969 for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
Eli Friedman33c2da92009-12-20 22:12:03 +00002970 E = Conversions->end();
2971 I != E; ++I) {
2972 NamedDecl *D = *I;
2973 CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
2974 if (isa<UsingShadowDecl>(D))
2975 D = cast<UsingShadowDecl>(D)->getTargetDecl();
2976
2977 FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2978 CXXConversionDecl *Conv;
Douglas Gregor4a520a22009-12-14 17:27:33 +00002979 if (ConvTemplate)
Eli Friedman33c2da92009-12-20 22:12:03 +00002980 Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
Douglas Gregor4a520a22009-12-14 17:27:33 +00002981 else
John McCall32daa422010-03-31 01:36:47 +00002982 Conv = cast<CXXConversionDecl>(D);
Eli Friedman33c2da92009-12-20 22:12:03 +00002983
2984 if (AllowExplicit || !Conv->isExplicit()) {
2985 if (ConvTemplate)
John McCall9aa472c2010-03-19 07:35:19 +00002986 S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
John McCall86820f52010-01-26 01:37:31 +00002987 ActingDC, Initializer, DestType,
Eli Friedman33c2da92009-12-20 22:12:03 +00002988 CandidateSet);
2989 else
John McCall9aa472c2010-03-19 07:35:19 +00002990 S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
John McCall86820f52010-01-26 01:37:31 +00002991 Initializer, DestType, CandidateSet);
Eli Friedman33c2da92009-12-20 22:12:03 +00002992 }
Douglas Gregor4a520a22009-12-14 17:27:33 +00002993 }
2994 }
2995 }
2996
Douglas Gregor4a520a22009-12-14 17:27:33 +00002997 // Perform overload resolution. If it fails, return the failed result.
2998 OverloadCandidateSet::iterator Best;
John McCall1d318332010-01-12 00:44:57 +00002999 if (OverloadingResult Result
Douglas Gregor8fcc5162010-09-12 08:07:23 +00003000 = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) {
Douglas Gregor4a520a22009-12-14 17:27:33 +00003001 Sequence.SetOverloadFailure(
3002 InitializationSequence::FK_UserConversionOverloadFailed,
3003 Result);
3004 return;
3005 }
John McCall1d318332010-01-12 00:44:57 +00003006
Douglas Gregor4a520a22009-12-14 17:27:33 +00003007 FunctionDecl *Function = Best->Function;
3008
3009 if (isa<CXXConstructorDecl>(Function)) {
3010 // Add the user-defined conversion step. Any cv-qualification conversion is
3011 // subsumed by the initialization.
John McCall9aa472c2010-03-19 07:35:19 +00003012 Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType);
Douglas Gregor4a520a22009-12-14 17:27:33 +00003013 return;
3014 }
3015
3016 // Add the user-defined conversion step that calls the conversion function.
Douglas Gregor5291c3c2010-07-13 08:18:22 +00003017 QualType ConvType = Function->getCallResultType();
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003018 if (ConvType->getAs<RecordType>()) {
3019 // If we're converting to a class type, there may be an copy if
3020 // the resulting temporary object (possible to create an object of
3021 // a base class type). That copy is not a separate conversion, so
3022 // we just make a note of the actual destination type (possibly a
3023 // base class of the type returned by the conversion function) and
3024 // let the user-defined conversion step handle the conversion.
3025 Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType);
3026 return;
3027 }
Douglas Gregor4a520a22009-12-14 17:27:33 +00003028
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003029 Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType);
3030
3031 // If the conversion following the call to the conversion function
3032 // is interesting, add it as a separate step.
Douglas Gregor4a520a22009-12-14 17:27:33 +00003033 if (Best->FinalConversion.First || Best->FinalConversion.Second ||
3034 Best->FinalConversion.Third) {
3035 ImplicitConversionSequence ICS;
John McCall1d318332010-01-12 00:44:57 +00003036 ICS.setStandard();
Douglas Gregor4a520a22009-12-14 17:27:33 +00003037 ICS.Standard = Best->FinalConversion;
3038 Sequence.AddConversionSequenceStep(ICS, DestType);
3039 }
Douglas Gregor20093b42009-12-09 23:02:17 +00003040}
3041
Douglas Gregor20093b42009-12-09 23:02:17 +00003042InitializationSequence::InitializationSequence(Sema &S,
3043 const InitializedEntity &Entity,
3044 const InitializationKind &Kind,
3045 Expr **Args,
John McCall5769d612010-02-08 23:07:23 +00003046 unsigned NumArgs)
3047 : FailedCandidateSet(Kind.getLocation()) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003048 ASTContext &Context = S.Context;
3049
3050 // C++0x [dcl.init]p16:
3051 // The semantics of initializers are as follows. The destination type is
3052 // the type of the object or reference being initialized and the source
3053 // type is the type of the initializer expression. The source type is not
3054 // defined when the initializer is a braced-init-list or when it is a
3055 // parenthesized list of expressions.
Douglas Gregord6542d82009-12-22 15:35:07 +00003056 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003057
3058 if (DestType->isDependentType() ||
3059 Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
3060 SequenceKind = DependentSequence;
3061 return;
3062 }
3063
3064 QualType SourceType;
3065 Expr *Initializer = 0;
Douglas Gregor99a2e602009-12-16 01:38:02 +00003066 if (NumArgs == 1) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003067 Initializer = Args[0];
3068 if (!isa<InitListExpr>(Initializer))
3069 SourceType = Initializer->getType();
3070 }
3071
3072 // - If the initializer is a braced-init-list, the object is
3073 // list-initialized (8.5.4).
3074 if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
3075 TryListInitialization(S, Entity, Kind, InitList, *this);
Douglas Gregord87b61f2009-12-10 17:56:55 +00003076 return;
Douglas Gregor20093b42009-12-09 23:02:17 +00003077 }
3078
3079 // - If the destination type is a reference type, see 8.5.3.
3080 if (DestType->isReferenceType()) {
3081 // C++0x [dcl.init.ref]p1:
3082 // A variable declared to be a T& or T&&, that is, "reference to type T"
3083 // (8.3.2), shall be initialized by an object, or function, of type T or
3084 // by an object that can be converted into a T.
3085 // (Therefore, multiple arguments are not permitted.)
3086 if (NumArgs != 1)
3087 SetFailed(FK_TooManyInitsForReference);
3088 else
3089 TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
3090 return;
3091 }
3092
3093 // - If the destination type is an array of characters, an array of
3094 // char16_t, an array of char32_t, or an array of wchar_t, and the
3095 // initializer is a string literal, see 8.5.2.
3096 if (Initializer && IsStringInit(Initializer, DestType, Context)) {
3097 TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
3098 return;
3099 }
3100
3101 // - If the initializer is (), the object is value-initialized.
Douglas Gregor99a2e602009-12-16 01:38:02 +00003102 if (Kind.getKind() == InitializationKind::IK_Value ||
3103 (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003104 TryValueInitialization(S, Entity, Kind, *this);
3105 return;
3106 }
3107
Douglas Gregor99a2e602009-12-16 01:38:02 +00003108 // Handle default initialization.
3109 if (Kind.getKind() == InitializationKind::IK_Default){
3110 TryDefaultInitialization(S, Entity, Kind, *this);
3111 return;
3112 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003113
Douglas Gregor20093b42009-12-09 23:02:17 +00003114 // - Otherwise, if the destination type is an array, the program is
3115 // ill-formed.
3116 if (const ArrayType *AT = Context.getAsArrayType(DestType)) {
3117 if (AT->getElementType()->isAnyCharacterType())
3118 SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
3119 else
3120 SetFailed(FK_ArrayNeedsInitList);
3121
3122 return;
3123 }
Eli Friedmancfdc81a2009-12-19 08:11:05 +00003124
3125 // Handle initialization in C
3126 if (!S.getLangOptions().CPlusPlus) {
3127 setSequenceKind(CAssignment);
3128 AddCAssignmentStep(DestType);
3129 return;
3130 }
Douglas Gregor20093b42009-12-09 23:02:17 +00003131
3132 // - If the destination type is a (possibly cv-qualified) class type:
3133 if (DestType->isRecordType()) {
3134 // - If the initialization is direct-initialization, or if it is
3135 // copy-initialization where the cv-unqualified version of the
3136 // source type is the same class as, or a derived class of, the
3137 // class of the destination, constructors are considered. [...]
3138 if (Kind.getKind() == InitializationKind::IK_Direct ||
3139 (Kind.getKind() == InitializationKind::IK_Copy &&
3140 (Context.hasSameUnqualifiedType(SourceType, DestType) ||
3141 S.IsDerivedFrom(SourceType, DestType))))
Douglas Gregor71d17402009-12-15 00:01:57 +00003142 TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
Douglas Gregord6542d82009-12-22 15:35:07 +00003143 Entity.getType(), *this);
Douglas Gregor20093b42009-12-09 23:02:17 +00003144 // - Otherwise (i.e., for the remaining copy-initialization cases),
3145 // user-defined conversion sequences that can convert from the source
3146 // type to the destination type or (when a conversion function is
3147 // used) to a derived class thereof are enumerated as described in
3148 // 13.3.1.4, and the best one is chosen through overload resolution
3149 // (13.3).
3150 else
3151 TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
3152 return;
3153 }
3154
Douglas Gregor99a2e602009-12-16 01:38:02 +00003155 if (NumArgs > 1) {
3156 SetFailed(FK_TooManyInitsForScalar);
3157 return;
3158 }
3159 assert(NumArgs == 1 && "Zero-argument case handled above");
3160
Douglas Gregor20093b42009-12-09 23:02:17 +00003161 // - Otherwise, if the source type is a (possibly cv-qualified) class
3162 // type, conversion functions are considered.
Douglas Gregor99a2e602009-12-16 01:38:02 +00003163 if (!SourceType.isNull() && SourceType->isRecordType()) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003164 TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
3165 return;
3166 }
3167
3168 // - Otherwise, the initial value of the object being initialized is the
Douglas Gregor4a520a22009-12-14 17:27:33 +00003169 // (possibly converted) value of the initializer expression. Standard
Douglas Gregor20093b42009-12-09 23:02:17 +00003170 // conversions (Clause 4) will be used, if necessary, to convert the
3171 // initializer expression to the cv-unqualified version of the
3172 // destination type; no user-defined conversions are considered.
John McCall369371c2010-06-04 02:29:22 +00003173 if (S.TryImplicitConversion(*this, Entity, Initializer,
3174 /*SuppressUserConversions*/ true,
3175 /*AllowExplicitConversions*/ false,
3176 /*InOverloadResolution*/ false))
3177 SetFailed(InitializationSequence::FK_ConversionFailed);
3178 else
3179 setSequenceKind(StandardConversion);
Douglas Gregor20093b42009-12-09 23:02:17 +00003180}
3181
3182InitializationSequence::~InitializationSequence() {
3183 for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(),
3184 StepEnd = Steps.end();
3185 Step != StepEnd; ++Step)
3186 Step->Destroy();
3187}
3188
3189//===----------------------------------------------------------------------===//
3190// Perform initialization
3191//===----------------------------------------------------------------------===//
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003192static Sema::AssignmentAction
3193getAssignmentAction(const InitializedEntity &Entity) {
3194 switch(Entity.getKind()) {
3195 case InitializedEntity::EK_Variable:
3196 case InitializedEntity::EK_New:
3197 return Sema::AA_Initializing;
3198
3199 case InitializedEntity::EK_Parameter:
Douglas Gregor688fc9b2010-04-21 23:24:10 +00003200 if (Entity.getDecl() &&
3201 isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
3202 return Sema::AA_Sending;
3203
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003204 return Sema::AA_Passing;
3205
3206 case InitializedEntity::EK_Result:
3207 return Sema::AA_Returning;
3208
3209 case InitializedEntity::EK_Exception:
3210 case InitializedEntity::EK_Base:
3211 llvm_unreachable("No assignment action for C++-specific initialization");
3212 break;
3213
3214 case InitializedEntity::EK_Temporary:
3215 // FIXME: Can we tell apart casting vs. converting?
3216 return Sema::AA_Casting;
3217
3218 case InitializedEntity::EK_Member:
Anders Carlssond3d824d2010-01-23 04:34:47 +00003219 case InitializedEntity::EK_ArrayElement:
3220 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003221 case InitializedEntity::EK_BlockElement:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003222 return Sema::AA_Initializing;
3223 }
3224
3225 return Sema::AA_Converting;
3226}
3227
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003228/// \brief Whether we should binding a created object as a temporary when
3229/// initializing the given entity.
Douglas Gregor2f599792010-04-02 18:24:57 +00003230static bool shouldBindAsTemporary(const InitializedEntity &Entity) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003231 switch (Entity.getKind()) {
Anders Carlsson1b36a2f2010-01-24 00:19:41 +00003232 case InitializedEntity::EK_ArrayElement:
3233 case InitializedEntity::EK_Member:
Douglas Gregor2f599792010-04-02 18:24:57 +00003234 case InitializedEntity::EK_Result:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003235 case InitializedEntity::EK_New:
3236 case InitializedEntity::EK_Variable:
3237 case InitializedEntity::EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00003238 case InitializedEntity::EK_VectorElement:
Anders Carlssona508b7d2010-02-06 23:23:06 +00003239 case InitializedEntity::EK_Exception:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003240 case InitializedEntity::EK_BlockElement:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003241 return false;
3242
3243 case InitializedEntity::EK_Parameter:
3244 case InitializedEntity::EK_Temporary:
3245 return true;
3246 }
3247
3248 llvm_unreachable("missed an InitializedEntity kind?");
3249}
3250
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003251/// \brief Whether the given entity, when initialized with an object
3252/// created for that initialization, requires destruction.
3253static bool shouldDestroyTemporary(const InitializedEntity &Entity) {
3254 switch (Entity.getKind()) {
3255 case InitializedEntity::EK_Member:
3256 case InitializedEntity::EK_Result:
3257 case InitializedEntity::EK_New:
3258 case InitializedEntity::EK_Base:
3259 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003260 case InitializedEntity::EK_BlockElement:
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003261 return false;
3262
3263 case InitializedEntity::EK_Variable:
3264 case InitializedEntity::EK_Parameter:
3265 case InitializedEntity::EK_Temporary:
3266 case InitializedEntity::EK_ArrayElement:
3267 case InitializedEntity::EK_Exception:
3268 return true;
3269 }
3270
3271 llvm_unreachable("missed an InitializedEntity kind?");
3272}
3273
Douglas Gregor523d46a2010-04-18 07:40:54 +00003274/// \brief Make a (potentially elidable) temporary copy of the object
3275/// provided by the given initializer by calling the appropriate copy
3276/// constructor.
3277///
3278/// \param S The Sema object used for type-checking.
3279///
3280/// \param T The type of the temporary object, which must either by
3281/// the type of the initializer expression or a superclass thereof.
3282///
3283/// \param Enter The entity being initialized.
3284///
3285/// \param CurInit The initializer expression.
3286///
3287/// \param IsExtraneousCopy Whether this is an "extraneous" copy that
3288/// is permitted in C++03 (but not C++0x) when binding a reference to
3289/// an rvalue.
3290///
3291/// \returns An expression that copies the initializer expression into
3292/// a temporary object, or an error expression if a copy could not be
3293/// created.
John McCall60d7b3a2010-08-24 06:29:42 +00003294static ExprResult CopyObject(Sema &S,
Douglas Gregor8fcc5162010-09-12 08:07:23 +00003295 QualType T,
3296 const InitializedEntity &Entity,
3297 ExprResult CurInit,
3298 bool IsExtraneousCopy) {
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003299 // Determine which class type we're copying to.
Anders Carlsson1b36a2f2010-01-24 00:19:41 +00003300 Expr *CurInitExpr = (Expr *)CurInit.get();
Douglas Gregor2f599792010-04-02 18:24:57 +00003301 CXXRecordDecl *Class = 0;
Douglas Gregor523d46a2010-04-18 07:40:54 +00003302 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor2f599792010-04-02 18:24:57 +00003303 Class = cast<CXXRecordDecl>(Record->getDecl());
3304 if (!Class)
3305 return move(CurInit);
3306
3307 // C++0x [class.copy]p34:
3308 // When certain criteria are met, an implementation is allowed to
3309 // omit the copy/move construction of a class object, even if the
3310 // copy/move constructor and/or destructor for the object have
3311 // side effects. [...]
3312 // - when a temporary class object that has not been bound to a
3313 // reference (12.2) would be copied/moved to a class object
3314 // with the same cv-unqualified type, the copy/move operation
3315 // can be omitted by constructing the temporary object
3316 // directly into the target of the omitted copy/move
3317 //
3318 // Note that the other three bullets are handled elsewhere. Copy
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003319 // elision for return statements and throw expressions are handled as part
3320 // of constructor initialization, while copy elision for exception handlers
3321 // is handled by the run-time.
John McCall558d2ab2010-09-15 10:14:12 +00003322 bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003323 SourceLocation Loc;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003324 switch (Entity.getKind()) {
3325 case InitializedEntity::EK_Result:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003326 Loc = Entity.getReturnLoc();
3327 break;
3328
3329 case InitializedEntity::EK_Exception:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003330 Loc = Entity.getThrowLoc();
3331 break;
3332
3333 case InitializedEntity::EK_Variable:
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003334 Loc = Entity.getDecl()->getLocation();
3335 break;
3336
Anders Carlsson1b36a2f2010-01-24 00:19:41 +00003337 case InitializedEntity::EK_ArrayElement:
3338 case InitializedEntity::EK_Member:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003339 case InitializedEntity::EK_Parameter:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003340 case InitializedEntity::EK_Temporary:
Douglas Gregor2f599792010-04-02 18:24:57 +00003341 case InitializedEntity::EK_New:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003342 case InitializedEntity::EK_Base:
Anders Carlssond3d824d2010-01-23 04:34:47 +00003343 case InitializedEntity::EK_VectorElement:
Fariborz Jahanian310b1c42010-06-07 16:14:00 +00003344 case InitializedEntity::EK_BlockElement:
Douglas Gregor2f599792010-04-02 18:24:57 +00003345 Loc = CurInitExpr->getLocStart();
3346 break;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003347 }
Douglas Gregorf86fcb32010-04-24 21:09:25 +00003348
3349 // Make sure that the type we are copying is complete.
3350 if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete)))
3351 return move(CurInit);
3352
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003353 // Perform overload resolution using the class's copy constructors.
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003354 DeclContext::lookup_iterator Con, ConEnd;
John McCall5769d612010-02-08 23:07:23 +00003355 OverloadCandidateSet CandidateSet(Loc);
Douglas Gregore5eee5a2010-07-02 23:12:18 +00003356 for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003357 Con != ConEnd; ++Con) {
Douglas Gregor2f599792010-04-02 18:24:57 +00003358 // Only consider copy constructors.
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003359 CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(*Con);
3360 if (!Constructor || Constructor->isInvalidDecl() ||
Douglas Gregor153b3ba2010-04-18 02:16:12 +00003361 !Constructor->isCopyConstructor() ||
3362 !Constructor->isConvertingConstructor(/*AllowExplicit=*/false))
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003363 continue;
John McCall9aa472c2010-03-19 07:35:19 +00003364
3365 DeclAccessPair FoundDecl
3366 = DeclAccessPair::make(Constructor, Constructor->getAccess());
3367 S.AddOverloadCandidate(Constructor, FoundDecl,
John McCall86820f52010-01-26 01:37:31 +00003368 &CurInitExpr, 1, CandidateSet);
Douglas Gregor2f599792010-04-02 18:24:57 +00003369 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003370
3371 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00003372 switch (CandidateSet.BestViableFunction(S, Loc, Best)) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003373 case OR_Success:
3374 break;
3375
3376 case OR_No_Viable_Function:
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003377 S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext()
3378 ? diag::ext_rvalue_to_reference_temp_copy_no_viable
3379 : diag::err_temp_copy_no_viable)
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003380 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003381 << CurInitExpr->getSourceRange();
John McCall120d63c2010-08-24 20:38:10 +00003382 CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1);
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003383 if (!IsExtraneousCopy || S.isSFINAEContext())
John McCallf312b1e2010-08-26 23:41:50 +00003384 return ExprError();
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003385 return move(CurInit);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003386
3387 case OR_Ambiguous:
3388 S.Diag(Loc, diag::err_temp_copy_ambiguous)
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003389 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003390 << CurInitExpr->getSourceRange();
John McCall120d63c2010-08-24 20:38:10 +00003391 CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1);
John McCallf312b1e2010-08-26 23:41:50 +00003392 return ExprError();
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003393
3394 case OR_Deleted:
3395 S.Diag(Loc, diag::err_temp_copy_deleted)
Douglas Gregor7abfbdb2009-12-19 03:01:41 +00003396 << (int)Entity.getKind() << CurInitExpr->getType()
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003397 << CurInitExpr->getSourceRange();
3398 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
3399 << Best->Function->isDeleted();
John McCallf312b1e2010-08-26 23:41:50 +00003400 return ExprError();
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003401 }
3402
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003403 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function);
John McCallca0408f2010-08-23 06:44:23 +00003404 ASTOwningVector<Expr*> ConstructorArgs(S);
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003405 CurInit.release(); // Ownership transferred into MultiExprArg, below.
Douglas Gregor523d46a2010-04-18 07:40:54 +00003406
Anders Carlsson9a68a672010-04-21 18:47:17 +00003407 S.CheckConstructorAccess(Loc, Constructor, Entity,
Jeffrey Yasskin57d12fd2010-06-07 15:58:05 +00003408 Best->FoundDecl.getAccess(), IsExtraneousCopy);
Douglas Gregor523d46a2010-04-18 07:40:54 +00003409
3410 if (IsExtraneousCopy) {
3411 // If this is a totally extraneous copy for C++03 reference
3412 // binding purposes, just return the original initialization
Douglas Gregor2559a702010-04-18 07:57:34 +00003413 // expression. We don't generate an (elided) copy operation here
3414 // because doing so would require us to pass down a flag to avoid
3415 // infinite recursion, where each step adds another extraneous,
3416 // elidable copy.
Douglas Gregor523d46a2010-04-18 07:40:54 +00003417
Douglas Gregor2559a702010-04-18 07:57:34 +00003418 // Instantiate the default arguments of any extra parameters in
3419 // the selected copy constructor, as if we were going to create a
3420 // proper call to the copy constructor.
3421 for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) {
3422 ParmVarDecl *Parm = Constructor->getParamDecl(I);
3423 if (S.RequireCompleteType(Loc, Parm->getType(),
3424 S.PDiag(diag::err_call_incomplete_argument)))
3425 break;
3426
3427 // Build the default argument expression; we don't actually care
3428 // if this succeeds or not, because this routine will complain
3429 // if there was a problem.
3430 S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm);
3431 }
3432
Douglas Gregor523d46a2010-04-18 07:40:54 +00003433 return S.Owned(CurInitExpr);
3434 }
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003435
3436 // Determine the arguments required to actually perform the
Douglas Gregor523d46a2010-04-18 07:40:54 +00003437 // constructor call (we might have derived-to-base conversions, or
3438 // the copy constructor may have default arguments).
John McCallf312b1e2010-08-26 23:41:50 +00003439 if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1),
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003440 Loc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00003441 return ExprError();
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +00003442
Douglas Gregorb86cf0c2010-04-25 00:55:24 +00003443 // Actually perform the constructor call.
3444 CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable,
John McCall7a1fad32010-08-24 07:32:53 +00003445 move_arg(ConstructorArgs),
3446 /*ZeroInit*/ false,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003447 CXXConstructExpr::CK_Complete,
3448 SourceRange());
Douglas Gregorb86cf0c2010-04-25 00:55:24 +00003449
3450 // If we're supposed to bind temporaries, do so.
3451 if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity))
3452 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
3453 return move(CurInit);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003454}
Douglas Gregor20093b42009-12-09 23:02:17 +00003455
Douglas Gregora41a8c52010-04-22 00:20:18 +00003456void InitializationSequence::PrintInitLocationNote(Sema &S,
3457 const InitializedEntity &Entity) {
3458 if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) {
3459 if (Entity.getDecl()->getLocation().isInvalid())
3460 return;
3461
3462 if (Entity.getDecl()->getDeclName())
3463 S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here)
3464 << Entity.getDecl()->getDeclName();
3465 else
3466 S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here);
3467 }
3468}
3469
John McCall60d7b3a2010-08-24 06:29:42 +00003470ExprResult
Douglas Gregor20093b42009-12-09 23:02:17 +00003471InitializationSequence::Perform(Sema &S,
3472 const InitializedEntity &Entity,
3473 const InitializationKind &Kind,
John McCallf312b1e2010-08-26 23:41:50 +00003474 MultiExprArg Args,
Douglas Gregord87b61f2009-12-10 17:56:55 +00003475 QualType *ResultType) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003476 if (SequenceKind == FailedSequence) {
3477 unsigned NumArgs = Args.size();
3478 Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs);
John McCallf312b1e2010-08-26 23:41:50 +00003479 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003480 }
3481
3482 if (SequenceKind == DependentSequence) {
Douglas Gregord87b61f2009-12-10 17:56:55 +00003483 // If the declaration is a non-dependent, incomplete array type
3484 // that has an initializer, then its type will be completed once
3485 // the initializer is instantiated.
Douglas Gregord6542d82009-12-22 15:35:07 +00003486 if (ResultType && !Entity.getType()->isDependentType() &&
Douglas Gregord87b61f2009-12-10 17:56:55 +00003487 Args.size() == 1) {
Douglas Gregord6542d82009-12-22 15:35:07 +00003488 QualType DeclType = Entity.getType();
Douglas Gregord87b61f2009-12-10 17:56:55 +00003489 if (const IncompleteArrayType *ArrayT
3490 = S.Context.getAsIncompleteArrayType(DeclType)) {
3491 // FIXME: We don't currently have the ability to accurately
3492 // compute the length of an initializer list without
3493 // performing full type-checking of the initializer list
3494 // (since we have to determine where braces are implicitly
3495 // introduced and such). So, we fall back to making the array
3496 // type a dependently-sized array type with no specified
3497 // bound.
3498 if (isa<InitListExpr>((Expr *)Args.get()[0])) {
3499 SourceRange Brackets;
Douglas Gregord6542d82009-12-22 15:35:07 +00003500
Douglas Gregord87b61f2009-12-10 17:56:55 +00003501 // Scavange the location of the brackets from the entity, if we can.
Douglas Gregord6542d82009-12-22 15:35:07 +00003502 if (DeclaratorDecl *DD = Entity.getDecl()) {
3503 if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) {
3504 TypeLoc TL = TInfo->getTypeLoc();
3505 if (IncompleteArrayTypeLoc *ArrayLoc
3506 = dyn_cast<IncompleteArrayTypeLoc>(&TL))
3507 Brackets = ArrayLoc->getBracketsRange();
3508 }
Douglas Gregord87b61f2009-12-10 17:56:55 +00003509 }
3510
3511 *ResultType
3512 = S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
3513 /*NumElts=*/0,
3514 ArrayT->getSizeModifier(),
3515 ArrayT->getIndexTypeCVRQualifiers(),
3516 Brackets);
3517 }
3518
3519 }
3520 }
3521
Eli Friedman08544622009-12-22 02:35:53 +00003522 if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast())
John McCall60d7b3a2010-08-24 06:29:42 +00003523 return ExprResult(Args.release()[0]);
Douglas Gregor20093b42009-12-09 23:02:17 +00003524
Douglas Gregor67fa05b2010-02-05 07:56:11 +00003525 if (Args.size() == 0)
3526 return S.Owned((Expr *)0);
3527
Douglas Gregor20093b42009-12-09 23:02:17 +00003528 unsigned NumArgs = Args.size();
3529 return S.Owned(new (S.Context) ParenListExpr(S.Context,
3530 SourceLocation(),
3531 (Expr **)Args.release(),
3532 NumArgs,
3533 SourceLocation()));
3534 }
3535
Douglas Gregor99a2e602009-12-16 01:38:02 +00003536 if (SequenceKind == NoInitialization)
3537 return S.Owned((Expr *)0);
3538
Douglas Gregord6542d82009-12-22 15:35:07 +00003539 QualType DestType = Entity.getType().getNonReferenceType();
3540 // FIXME: Ugly hack around the fact that Entity.getType() is not
Eli Friedmana91eb542009-12-22 02:10:53 +00003541 // the same as Entity.getDecl()->getType() in cases involving type merging,
3542 // and we want latter when it makes sense.
Douglas Gregord87b61f2009-12-10 17:56:55 +00003543 if (ResultType)
Eli Friedmana91eb542009-12-22 02:10:53 +00003544 *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() :
Douglas Gregord6542d82009-12-22 15:35:07 +00003545 Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003546
John McCall60d7b3a2010-08-24 06:29:42 +00003547 ExprResult CurInit = S.Owned((Expr *)0);
Douglas Gregor99a2e602009-12-16 01:38:02 +00003548
3549 assert(!Steps.empty() && "Cannot have an empty initialization sequence");
3550
3551 // For initialization steps that start with a single initializer,
3552 // grab the only argument out the Args and place it into the "current"
3553 // initializer.
3554 switch (Steps.front().Kind) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003555 case SK_ResolveAddressOfOverloadedFunction:
3556 case SK_CastDerivedToBaseRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003557 case SK_CastDerivedToBaseXValue:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003558 case SK_CastDerivedToBaseLValue:
3559 case SK_BindReference:
3560 case SK_BindReferenceToTemporary:
Douglas Gregor523d46a2010-04-18 07:40:54 +00003561 case SK_ExtraneousCopyToTemporary:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003562 case SK_UserConversion:
3563 case SK_QualificationConversionLValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003564 case SK_QualificationConversionXValue:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003565 case SK_QualificationConversionRValue:
3566 case SK_ConversionSequence:
3567 case SK_ListInitialization:
3568 case SK_CAssignment:
Eli Friedmancfdc81a2009-12-19 08:11:05 +00003569 case SK_StringInit:
Douglas Gregor569c3162010-08-07 11:51:51 +00003570 case SK_ObjCObjectConversion:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003571 assert(Args.size() == 1);
John McCall60d7b3a2010-08-24 06:29:42 +00003572 CurInit = ExprResult(((Expr **)(Args.get()))[0]->Retain());
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003573 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003574 return ExprError();
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003575 break;
3576
3577 case SK_ConstructorInitialization:
3578 case SK_ZeroInitialization:
3579 break;
Douglas Gregor20093b42009-12-09 23:02:17 +00003580 }
3581
3582 // Walk through the computed steps for the initialization sequence,
3583 // performing the specified conversions along the way.
Douglas Gregor16006c92009-12-16 18:50:27 +00003584 bool ConstructorInitRequiresZeroInit = false;
Douglas Gregor20093b42009-12-09 23:02:17 +00003585 for (step_iterator Step = step_begin(), StepEnd = step_end();
3586 Step != StepEnd; ++Step) {
3587 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003588 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003589
3590 Expr *CurInitExpr = (Expr *)CurInit.get();
Douglas Gregor99a2e602009-12-16 01:38:02 +00003591 QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003592
3593 switch (Step->Kind) {
3594 case SK_ResolveAddressOfOverloadedFunction:
3595 // Overload resolution determined which function invoke; update the
3596 // initializer to reflect that choice.
John McCall6bb80172010-03-30 21:47:33 +00003597 S.CheckAddressOfMemberAccess(CurInitExpr, Step->Function.FoundDecl);
John McCallb697e082010-05-06 18:15:07 +00003598 S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation());
John McCallb13b7372010-02-01 03:16:54 +00003599 CurInit = S.FixOverloadedFunctionReference(move(CurInit),
John McCall6bb80172010-03-30 21:47:33 +00003600 Step->Function.FoundDecl,
John McCall9aa472c2010-03-19 07:35:19 +00003601 Step->Function.Function);
Douglas Gregor20093b42009-12-09 23:02:17 +00003602 break;
3603
3604 case SK_CastDerivedToBaseRValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003605 case SK_CastDerivedToBaseXValue:
Douglas Gregor20093b42009-12-09 23:02:17 +00003606 case SK_CastDerivedToBaseLValue: {
3607 // We have a derived-to-base cast that produces either an rvalue or an
3608 // lvalue. Perform that cast.
3609
John McCallf871d0c2010-08-07 06:22:56 +00003610 CXXCastPath BasePath;
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00003611
Douglas Gregor20093b42009-12-09 23:02:17 +00003612 // Casts to inaccessible base classes are allowed with C-style casts.
3613 bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
3614 if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
3615 CurInitExpr->getLocStart(),
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00003616 CurInitExpr->getSourceRange(),
3617 &BasePath, IgnoreBaseAccess))
John McCallf312b1e2010-08-26 23:41:50 +00003618 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003619
Douglas Gregor6fb745b2010-05-13 16:44:06 +00003620 if (S.BasePathInvolvesVirtualBase(BasePath)) {
3621 QualType T = SourceType;
3622 if (const PointerType *Pointer = T->getAs<PointerType>())
3623 T = Pointer->getPointeeType();
3624 if (const RecordType *RecordTy = T->getAs<RecordType>())
3625 S.MarkVTableUsed(CurInitExpr->getLocStart(),
3626 cast<CXXRecordDecl>(RecordTy->getDecl()));
3627 }
3628
John McCall5baba9d2010-08-25 10:28:54 +00003629 ExprValueKind VK =
Sebastian Redl906082e2010-07-20 04:20:21 +00003630 Step->Kind == SK_CastDerivedToBaseLValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003631 VK_LValue :
Sebastian Redl906082e2010-07-20 04:20:21 +00003632 (Step->Kind == SK_CastDerivedToBaseXValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003633 VK_XValue :
3634 VK_RValue);
John McCallf871d0c2010-08-07 06:22:56 +00003635 CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
3636 Step->Type,
John McCall2de56d12010-08-25 11:45:40 +00003637 CK_DerivedToBase,
John McCall5baba9d2010-08-25 10:28:54 +00003638 CurInit.get(),
3639 &BasePath, VK));
Douglas Gregor20093b42009-12-09 23:02:17 +00003640 break;
3641 }
3642
3643 case SK_BindReference:
3644 if (FieldDecl *BitField = CurInitExpr->getBitField()) {
3645 // References cannot bind to bit fields (C++ [dcl.init.ref]p5).
3646 S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
Douglas Gregord6542d82009-12-22 15:35:07 +00003647 << Entity.getType().isVolatileQualified()
Douglas Gregor20093b42009-12-09 23:02:17 +00003648 << BitField->getDeclName()
3649 << CurInitExpr->getSourceRange();
3650 S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
John McCallf312b1e2010-08-26 23:41:50 +00003651 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003652 }
Anders Carlssona6fe0bf2010-01-29 02:47:33 +00003653
Anders Carlsson09380262010-01-31 17:18:49 +00003654 if (CurInitExpr->refersToVectorElement()) {
John McCall41593e32010-02-02 19:02:38 +00003655 // References cannot bind to vector elements.
Anders Carlsson09380262010-01-31 17:18:49 +00003656 S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element)
3657 << Entity.getType().isVolatileQualified()
3658 << CurInitExpr->getSourceRange();
Douglas Gregora41a8c52010-04-22 00:20:18 +00003659 PrintInitLocationNote(S, Entity);
John McCallf312b1e2010-08-26 23:41:50 +00003660 return ExprError();
Anders Carlsson09380262010-01-31 17:18:49 +00003661 }
3662
Douglas Gregor20093b42009-12-09 23:02:17 +00003663 // Reference binding does not have any corresponding ASTs.
3664
3665 // Check exception specifications
3666 if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
John McCallf312b1e2010-08-26 23:41:50 +00003667 return ExprError();
Anders Carlsson3aba0932010-01-31 18:34:51 +00003668
Douglas Gregor20093b42009-12-09 23:02:17 +00003669 break;
Anders Carlsson3aba0932010-01-31 18:34:51 +00003670
Douglas Gregor20093b42009-12-09 23:02:17 +00003671 case SK_BindReferenceToTemporary:
Anders Carlssona64a8692010-02-03 16:38:03 +00003672 // Reference binding does not have any corresponding ASTs.
3673
Douglas Gregor20093b42009-12-09 23:02:17 +00003674 // Check exception specifications
3675 if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
John McCallf312b1e2010-08-26 23:41:50 +00003676 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003677
Douglas Gregor20093b42009-12-09 23:02:17 +00003678 break;
3679
Douglas Gregor523d46a2010-04-18 07:40:54 +00003680 case SK_ExtraneousCopyToTemporary:
3681 CurInit = CopyObject(S, Step->Type, Entity, move(CurInit),
3682 /*IsExtraneousCopy=*/true);
3683 break;
3684
Douglas Gregor20093b42009-12-09 23:02:17 +00003685 case SK_UserConversion: {
3686 // We have a user-defined conversion that invokes either a constructor
3687 // or a conversion function.
John McCall2de56d12010-08-25 11:45:40 +00003688 CastKind CastKind = CK_Unknown;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003689 bool IsCopy = false;
John McCall9aa472c2010-03-19 07:35:19 +00003690 FunctionDecl *Fn = Step->Function.Function;
3691 DeclAccessPair FoundFn = Step->Function.FoundDecl;
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003692 bool CreatedObject = false;
Douglas Gregorf0e0b172010-03-25 00:20:38 +00003693 bool IsLvalue = false;
John McCallb13b7372010-02-01 03:16:54 +00003694 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) {
Douglas Gregor20093b42009-12-09 23:02:17 +00003695 // Build a call to the selected constructor.
John McCallca0408f2010-08-23 06:44:23 +00003696 ASTOwningVector<Expr*> ConstructorArgs(S);
Douglas Gregor20093b42009-12-09 23:02:17 +00003697 SourceLocation Loc = CurInitExpr->getLocStart();
3698 CurInit.release(); // Ownership transferred into MultiExprArg, below.
John McCallb13b7372010-02-01 03:16:54 +00003699
Douglas Gregor20093b42009-12-09 23:02:17 +00003700 // Determine the arguments required to actually perform the constructor
3701 // call.
3702 if (S.CompleteConstructorCall(Constructor,
John McCallf312b1e2010-08-26 23:41:50 +00003703 MultiExprArg(&CurInitExpr, 1),
Douglas Gregor20093b42009-12-09 23:02:17 +00003704 Loc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00003705 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003706
3707 // Build the an expression that constructs a temporary.
3708 CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
John McCall7a1fad32010-08-24 07:32:53 +00003709 move_arg(ConstructorArgs),
3710 /*ZeroInit*/ false,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003711 CXXConstructExpr::CK_Complete,
3712 SourceRange());
Douglas Gregor20093b42009-12-09 23:02:17 +00003713 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003714 return ExprError();
John McCallb13b7372010-02-01 03:16:54 +00003715
Anders Carlsson9a68a672010-04-21 18:47:17 +00003716 S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity,
John McCall9aa472c2010-03-19 07:35:19 +00003717 FoundFn.getAccess());
John McCallb697e082010-05-06 18:15:07 +00003718 S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
Douglas Gregor20093b42009-12-09 23:02:17 +00003719
John McCall2de56d12010-08-25 11:45:40 +00003720 CastKind = CK_ConstructorConversion;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003721 QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
3722 if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
3723 S.IsDerivedFrom(SourceType, Class))
3724 IsCopy = true;
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003725
3726 CreatedObject = true;
Douglas Gregor20093b42009-12-09 23:02:17 +00003727 } else {
3728 // Build a call to the conversion function.
John McCallb13b7372010-02-01 03:16:54 +00003729 CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn);
Douglas Gregorf0e0b172010-03-25 00:20:38 +00003730 IsLvalue = Conversion->getResultType()->isLValueReferenceType();
John McCall58e6f342010-03-16 05:22:47 +00003731 S.CheckMemberOperatorAccess(Kind.getLocation(), CurInitExpr, 0,
John McCall9aa472c2010-03-19 07:35:19 +00003732 FoundFn);
John McCallb697e082010-05-06 18:15:07 +00003733 S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
John McCallb13b7372010-02-01 03:16:54 +00003734
Douglas Gregor20093b42009-12-09 23:02:17 +00003735 // FIXME: Should we move this initialization into a separate
3736 // derived-to-base conversion? I believe the answer is "no", because
3737 // we don't want to turn off access control here for c-style casts.
Douglas Gregor5fccd362010-03-03 23:55:11 +00003738 if (S.PerformObjectArgumentInitialization(CurInitExpr, /*Qualifier=*/0,
John McCall6bb80172010-03-30 21:47:33 +00003739 FoundFn, Conversion))
John McCallf312b1e2010-08-26 23:41:50 +00003740 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003741
3742 // Do a little dance to make sure that CurInit has the proper
3743 // pointer.
3744 CurInit.release();
3745
3746 // Build the actual call to the conversion function.
John McCall6bb80172010-03-30 21:47:33 +00003747 CurInit = S.Owned(S.BuildCXXMemberCallExpr(CurInitExpr, FoundFn,
3748 Conversion));
Douglas Gregor20093b42009-12-09 23:02:17 +00003749 if (CurInit.isInvalid() || !CurInit.get())
John McCallf312b1e2010-08-26 23:41:50 +00003750 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003751
John McCall2de56d12010-08-25 11:45:40 +00003752 CastKind = CK_UserDefinedConversion;
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003753
3754 CreatedObject = Conversion->getResultType()->isRecordType();
Douglas Gregor20093b42009-12-09 23:02:17 +00003755 }
3756
Douglas Gregor2f599792010-04-02 18:24:57 +00003757 bool RequiresCopy = !IsCopy &&
3758 getKind() != InitializationSequence::ReferenceBinding;
3759 if (RequiresCopy || shouldBindAsTemporary(Entity))
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003760 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003761 else if (CreatedObject && shouldDestroyTemporary(Entity)) {
3762 CurInitExpr = static_cast<Expr *>(CurInit.get());
3763 QualType T = CurInitExpr->getType();
3764 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregordb89f282010-07-01 22:47:18 +00003765 CXXDestructorDecl *Destructor
3766 = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl()));
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003767 S.CheckDestructorAccess(CurInitExpr->getLocStart(), Destructor,
3768 S.PDiag(diag::err_access_dtor_temp) << T);
3769 S.MarkDeclarationReferenced(CurInitExpr->getLocStart(), Destructor);
Douglas Gregor9b623632010-10-12 23:32:35 +00003770 S.DiagnoseUseOfDecl(Destructor, CurInitExpr->getLocStart());
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003771 }
3772 }
3773
Douglas Gregor20093b42009-12-09 23:02:17 +00003774 CurInitExpr = CurInit.takeAs<Expr>();
Sebastian Redl906082e2010-07-20 04:20:21 +00003775 // FIXME: xvalues
John McCallf871d0c2010-08-07 06:22:56 +00003776 CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
3777 CurInitExpr->getType(),
3778 CastKind, CurInitExpr, 0,
John McCall5baba9d2010-08-25 10:28:54 +00003779 IsLvalue ? VK_LValue : VK_RValue));
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003780
Douglas Gregor2f599792010-04-02 18:24:57 +00003781 if (RequiresCopy)
Douglas Gregor523d46a2010-04-18 07:40:54 +00003782 CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity,
3783 move(CurInit), /*IsExtraneousCopy=*/false);
Sebastian Redl906082e2010-07-20 04:20:21 +00003784
Douglas Gregor20093b42009-12-09 23:02:17 +00003785 break;
3786 }
Sebastian Redl906082e2010-07-20 04:20:21 +00003787
Douglas Gregor20093b42009-12-09 23:02:17 +00003788 case SK_QualificationConversionLValue:
Sebastian Redl906082e2010-07-20 04:20:21 +00003789 case SK_QualificationConversionXValue:
3790 case SK_QualificationConversionRValue: {
Douglas Gregor20093b42009-12-09 23:02:17 +00003791 // Perform a qualification conversion; these can never go wrong.
John McCall5baba9d2010-08-25 10:28:54 +00003792 ExprValueKind VK =
Sebastian Redl906082e2010-07-20 04:20:21 +00003793 Step->Kind == SK_QualificationConversionLValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003794 VK_LValue :
Sebastian Redl906082e2010-07-20 04:20:21 +00003795 (Step->Kind == SK_QualificationConversionXValue ?
John McCall5baba9d2010-08-25 10:28:54 +00003796 VK_XValue :
3797 VK_RValue);
John McCall2de56d12010-08-25 11:45:40 +00003798 S.ImpCastExprToType(CurInitExpr, Step->Type, CK_NoOp, VK);
Douglas Gregor20093b42009-12-09 23:02:17 +00003799 CurInit.release();
3800 CurInit = S.Owned(CurInitExpr);
3801 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00003802 }
3803
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003804 case SK_ConversionSequence: {
3805 bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
3806
3807 if (S.PerformImplicitConversion(CurInitExpr, Step->Type, *Step->ICS,
3808 Sema::AA_Converting, IgnoreBaseAccess))
John McCallf312b1e2010-08-26 23:41:50 +00003809 return ExprError();
Douglas Gregor20093b42009-12-09 23:02:17 +00003810
3811 CurInit.release();
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003812 CurInit = S.Owned(CurInitExpr);
Douglas Gregor20093b42009-12-09 23:02:17 +00003813 break;
Douglas Gregorf0e43e52010-04-16 19:30:02 +00003814 }
3815
Douglas Gregord87b61f2009-12-10 17:56:55 +00003816 case SK_ListInitialization: {
3817 InitListExpr *InitList = cast<InitListExpr>(CurInitExpr);
3818 QualType Ty = Step->Type;
Douglas Gregorcb57fb92009-12-16 06:35:08 +00003819 if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty))
John McCallf312b1e2010-08-26 23:41:50 +00003820 return ExprError();
Douglas Gregord87b61f2009-12-10 17:56:55 +00003821
3822 CurInit.release();
3823 CurInit = S.Owned(InitList);
3824 break;
3825 }
Douglas Gregor51c56d62009-12-14 20:49:26 +00003826
3827 case SK_ConstructorInitialization: {
Douglas Gregord6e44a32010-04-16 22:09:46 +00003828 unsigned NumArgs = Args.size();
Douglas Gregor51c56d62009-12-14 20:49:26 +00003829 CXXConstructorDecl *Constructor
John McCall9aa472c2010-03-19 07:35:19 +00003830 = cast<CXXConstructorDecl>(Step->Function.Function);
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003831
Douglas Gregor51c56d62009-12-14 20:49:26 +00003832 // Build a call to the selected constructor.
John McCallca0408f2010-08-23 06:44:23 +00003833 ASTOwningVector<Expr*> ConstructorArgs(S);
Fariborz Jahanian0a2eb562010-07-21 18:40:47 +00003834 SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid())
3835 ? Kind.getEqualLoc()
3836 : Kind.getLocation();
Chandler Carruth4e6fbce2010-08-23 07:55:51 +00003837
3838 if (Kind.getKind() == InitializationKind::IK_Default) {
3839 // Force even a trivial, implicit default constructor to be
3840 // semantically checked. We do this explicitly because we don't build
3841 // the definition for completely trivial constructors.
3842 CXXRecordDecl *ClassDecl = Constructor->getParent();
3843 assert(ClassDecl && "No parent class for constructor.");
3844 if (Constructor->isImplicit() && Constructor->isDefaultConstructor() &&
3845 ClassDecl->hasTrivialConstructor() && !Constructor->isUsed(false))
3846 S.DefineImplicitDefaultConstructor(Loc, Constructor);
3847 }
3848
Douglas Gregor51c56d62009-12-14 20:49:26 +00003849 // Determine the arguments required to actually perform the constructor
3850 // call.
3851 if (S.CompleteConstructorCall(Constructor, move(Args),
3852 Loc, ConstructorArgs))
John McCallf312b1e2010-08-26 23:41:50 +00003853 return ExprError();
Douglas Gregor51c56d62009-12-14 20:49:26 +00003854
Chandler Carruth4e6fbce2010-08-23 07:55:51 +00003855
Douglas Gregor91be6f52010-03-02 17:18:33 +00003856 if (Entity.getKind() == InitializedEntity::EK_Temporary &&
Douglas Gregord6e44a32010-04-16 22:09:46 +00003857 NumArgs != 1 && // FIXME: Hack to work around cast weirdness
Douglas Gregor91be6f52010-03-02 17:18:33 +00003858 (Kind.getKind() == InitializationKind::IK_Direct ||
3859 Kind.getKind() == InitializationKind::IK_Value)) {
3860 // An explicitly-constructed temporary, e.g., X(1, 2).
3861 unsigned NumExprs = ConstructorArgs.size();
3862 Expr **Exprs = (Expr **)ConstructorArgs.take();
Fariborz Jahanian10f8e312010-07-21 18:31:47 +00003863 S.MarkDeclarationReferenced(Loc, Constructor);
Douglas Gregor9b623632010-10-12 23:32:35 +00003864 S.DiagnoseUseOfDecl(Constructor, Loc);
3865
Douglas Gregorab6677e2010-09-08 00:15:04 +00003866 TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
3867 if (!TSInfo)
3868 TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc);
3869
Douglas Gregor91be6f52010-03-02 17:18:33 +00003870 CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context,
3871 Constructor,
Douglas Gregorab6677e2010-09-08 00:15:04 +00003872 TSInfo,
Douglas Gregor91be6f52010-03-02 17:18:33 +00003873 Exprs,
3874 NumExprs,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003875 Kind.getParenRange(),
Douglas Gregor1c63b9c2010-04-27 20:36:09 +00003876 ConstructorInitRequiresZeroInit));
Anders Carlsson72e96fd2010-05-02 22:54:08 +00003877 } else {
3878 CXXConstructExpr::ConstructionKind ConstructKind =
3879 CXXConstructExpr::CK_Complete;
3880
3881 if (Entity.getKind() == InitializedEntity::EK_Base) {
3882 ConstructKind = Entity.getBaseSpecifier()->isVirtual() ?
3883 CXXConstructExpr::CK_VirtualBase :
3884 CXXConstructExpr::CK_NonVirtualBase;
3885 }
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003886
Chandler Carruth428edaf2010-10-25 08:47:36 +00003887 // Only get the parenthesis range if it is a direct construction.
3888 SourceRange parenRange =
3889 Kind.getKind() == InitializationKind::IK_Direct ?
3890 Kind.getParenRange() : SourceRange();
3891
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003892 // If the entity allows NRVO, mark the construction as elidable
3893 // unconditionally.
3894 if (Entity.allowsNRVO())
3895 CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
3896 Constructor, /*Elidable=*/true,
3897 move_arg(ConstructorArgs),
3898 ConstructorInitRequiresZeroInit,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003899 ConstructKind,
3900 parenRange);
Douglas Gregor3c9034c2010-05-15 00:13:29 +00003901 else
3902 CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
3903 Constructor,
3904 move_arg(ConstructorArgs),
3905 ConstructorInitRequiresZeroInit,
Chandler Carruth428edaf2010-10-25 08:47:36 +00003906 ConstructKind,
3907 parenRange);
Anders Carlsson72e96fd2010-05-02 22:54:08 +00003908 }
Douglas Gregor51c56d62009-12-14 20:49:26 +00003909 if (CurInit.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00003910 return ExprError();
John McCallb13b7372010-02-01 03:16:54 +00003911
3912 // Only check access if all of that succeeded.
Anders Carlsson9a68a672010-04-21 18:47:17 +00003913 S.CheckConstructorAccess(Loc, Constructor, Entity,
John McCall9aa472c2010-03-19 07:35:19 +00003914 Step->Function.FoundDecl.getAccess());
John McCallb697e082010-05-06 18:15:07 +00003915 S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003916
Douglas Gregor2f599792010-04-02 18:24:57 +00003917 if (shouldBindAsTemporary(Entity))
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003918 CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
Douglas Gregor4154e0b2010-04-24 23:45:46 +00003919
Douglas Gregor51c56d62009-12-14 20:49:26 +00003920 break;
3921 }
Douglas Gregor71d17402009-12-15 00:01:57 +00003922
3923 case SK_ZeroInitialization: {
Douglas Gregor16006c92009-12-16 18:50:27 +00003924 step_iterator NextStep = Step;
3925 ++NextStep;
3926 if (NextStep != StepEnd &&
3927 NextStep->Kind == SK_ConstructorInitialization) {
3928 // The need for zero-initialization is recorded directly into
3929 // the call to the object's constructor within the next step.
3930 ConstructorInitRequiresZeroInit = true;
3931 } else if (Kind.getKind() == InitializationKind::IK_Value &&
3932 S.getLangOptions().CPlusPlus &&
3933 !Kind.isImplicitValueInit()) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00003934 TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
3935 if (!TSInfo)
3936 TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type,
3937 Kind.getRange().getBegin());
3938
3939 CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr(
3940 TSInfo->getType().getNonLValueExprType(S.Context),
3941 TSInfo,
Douglas Gregor71d17402009-12-15 00:01:57 +00003942 Kind.getRange().getEnd()));
Douglas Gregor16006c92009-12-16 18:50:27 +00003943 } else {
Douglas Gregor71d17402009-12-15 00:01:57 +00003944 CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type));
Douglas Gregor16006c92009-12-16 18:50:27 +00003945 }
Douglas Gregor71d17402009-12-15 00:01:57 +00003946 break;
3947 }
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003948
3949 case SK_CAssignment: {
3950 QualType SourceType = CurInitExpr->getType();
3951 Sema::AssignConvertType ConvTy =
3952 S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr);
Douglas Gregoraa037312009-12-22 07:24:36 +00003953
3954 // If this is a call, allow conversion to a transparent union.
3955 if (ConvTy != Sema::Compatible &&
3956 Entity.getKind() == InitializedEntity::EK_Parameter &&
3957 S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr)
3958 == Sema::Compatible)
3959 ConvTy = Sema::Compatible;
3960
Douglas Gregora41a8c52010-04-22 00:20:18 +00003961 bool Complained;
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003962 if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
3963 Step->Type, SourceType,
Douglas Gregora41a8c52010-04-22 00:20:18 +00003964 CurInitExpr,
3965 getAssignmentAction(Entity),
3966 &Complained)) {
3967 PrintInitLocationNote(S, Entity);
John McCallf312b1e2010-08-26 23:41:50 +00003968 return ExprError();
Douglas Gregora41a8c52010-04-22 00:20:18 +00003969 } else if (Complained)
3970 PrintInitLocationNote(S, Entity);
Douglas Gregor18ef5e22009-12-18 05:02:21 +00003971
3972 CurInit.release();
3973 CurInit = S.Owned(CurInitExpr);
3974 break;
3975 }
Eli Friedmancfdc81a2009-12-19 08:11:05 +00003976
3977 case SK_StringInit: {
3978 QualType Ty = Step->Type;
3979 CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, S);
3980 break;
3981 }
Douglas Gregor569c3162010-08-07 11:51:51 +00003982
3983 case SK_ObjCObjectConversion:
3984 S.ImpCastExprToType(CurInitExpr, Step->Type,
John McCall2de56d12010-08-25 11:45:40 +00003985 CK_ObjCObjectLValueCast,
Douglas Gregor569c3162010-08-07 11:51:51 +00003986 S.CastCategory(CurInitExpr));
3987 CurInit.release();
3988 CurInit = S.Owned(CurInitExpr);
3989 break;
Douglas Gregor20093b42009-12-09 23:02:17 +00003990 }
3991 }
3992
3993 return move(CurInit);
3994}
3995
3996//===----------------------------------------------------------------------===//
3997// Diagnose initialization failures
3998//===----------------------------------------------------------------------===//
3999bool InitializationSequence::Diagnose(Sema &S,
4000 const InitializedEntity &Entity,
4001 const InitializationKind &Kind,
4002 Expr **Args, unsigned NumArgs) {
4003 if (SequenceKind != FailedSequence)
4004 return false;
4005
Douglas Gregord6542d82009-12-22 15:35:07 +00004006 QualType DestType = Entity.getType();
Douglas Gregor20093b42009-12-09 23:02:17 +00004007 switch (Failure) {
4008 case FK_TooManyInitsForReference:
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004009 // FIXME: Customize for the initialized entity?
4010 if (NumArgs == 0)
4011 S.Diag(Kind.getLocation(), diag::err_reference_without_init)
4012 << DestType.getNonReferenceType();
4013 else // FIXME: diagnostic below could be better!
4014 S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
4015 << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
Douglas Gregor20093b42009-12-09 23:02:17 +00004016 break;
4017
4018 case FK_ArrayNeedsInitList:
4019 case FK_ArrayNeedsInitListOrStringLiteral:
4020 S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list)
4021 << (Failure == FK_ArrayNeedsInitListOrStringLiteral);
4022 break;
4023
John McCall6bb80172010-03-30 21:47:33 +00004024 case FK_AddressOfOverloadFailed: {
4025 DeclAccessPair Found;
Douglas Gregor20093b42009-12-09 23:02:17 +00004026 S.ResolveAddressOfOverloadedFunction(Args[0],
4027 DestType.getNonReferenceType(),
John McCall6bb80172010-03-30 21:47:33 +00004028 true,
4029 Found);
Douglas Gregor20093b42009-12-09 23:02:17 +00004030 break;
John McCall6bb80172010-03-30 21:47:33 +00004031 }
Douglas Gregor20093b42009-12-09 23:02:17 +00004032
4033 case FK_ReferenceInitOverloadFailed:
Douglas Gregor4a520a22009-12-14 17:27:33 +00004034 case FK_UserConversionOverloadFailed:
Douglas Gregor20093b42009-12-09 23:02:17 +00004035 switch (FailedOverloadResult) {
4036 case OR_Ambiguous:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004037 if (Failure == FK_UserConversionOverloadFailed)
4038 S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
4039 << Args[0]->getType() << DestType
4040 << Args[0]->getSourceRange();
4041 else
4042 S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
4043 << DestType << Args[0]->getType()
4044 << Args[0]->getSourceRange();
4045
John McCall120d63c2010-08-24 20:38:10 +00004046 FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs);
Douglas Gregor20093b42009-12-09 23:02:17 +00004047 break;
4048
4049 case OR_No_Viable_Function:
4050 S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
4051 << Args[0]->getType() << DestType.getNonReferenceType()
4052 << Args[0]->getSourceRange();
John McCall120d63c2010-08-24 20:38:10 +00004053 FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor20093b42009-12-09 23:02:17 +00004054 break;
4055
4056 case OR_Deleted: {
4057 S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
4058 << Args[0]->getType() << DestType.getNonReferenceType()
4059 << Args[0]->getSourceRange();
4060 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00004061 OverloadingResult Ovl
Douglas Gregor8fcc5162010-09-12 08:07:23 +00004062 = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best,
4063 true);
Douglas Gregor20093b42009-12-09 23:02:17 +00004064 if (Ovl == OR_Deleted) {
4065 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
4066 << Best->Function->isDeleted();
4067 } else {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004068 llvm_unreachable("Inconsistent overload resolution?");
Douglas Gregor20093b42009-12-09 23:02:17 +00004069 }
4070 break;
4071 }
4072
4073 case OR_Success:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004074 llvm_unreachable("Conversion did not fail!");
Douglas Gregor20093b42009-12-09 23:02:17 +00004075 break;
4076 }
4077 break;
4078
4079 case FK_NonConstLValueReferenceBindingToTemporary:
4080 case FK_NonConstLValueReferenceBindingToUnrelated:
4081 S.Diag(Kind.getLocation(),
4082 Failure == FK_NonConstLValueReferenceBindingToTemporary
4083 ? diag::err_lvalue_reference_bind_to_temporary
4084 : diag::err_lvalue_reference_bind_to_unrelated)
Douglas Gregoref06e242010-01-29 19:39:15 +00004085 << DestType.getNonReferenceType().isVolatileQualified()
Douglas Gregor20093b42009-12-09 23:02:17 +00004086 << DestType.getNonReferenceType()
4087 << Args[0]->getType()
4088 << Args[0]->getSourceRange();
4089 break;
4090
4091 case FK_RValueReferenceBindingToLValue:
4092 S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
4093 << Args[0]->getSourceRange();
4094 break;
4095
4096 case FK_ReferenceInitDropsQualifiers:
4097 S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
4098 << DestType.getNonReferenceType()
4099 << Args[0]->getType()
4100 << Args[0]->getSourceRange();
4101 break;
4102
4103 case FK_ReferenceInitFailed:
4104 S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
4105 << DestType.getNonReferenceType()
4106 << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid)
4107 << Args[0]->getType()
4108 << Args[0]->getSourceRange();
4109 break;
4110
4111 case FK_ConversionFailed:
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004112 S.Diag(Kind.getLocation(), diag::err_init_conversion_failed)
4113 << (int)Entity.getKind()
Douglas Gregor20093b42009-12-09 23:02:17 +00004114 << DestType
4115 << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid)
4116 << Args[0]->getType()
4117 << Args[0]->getSourceRange();
Douglas Gregord87b61f2009-12-10 17:56:55 +00004118 break;
4119
4120 case FK_TooManyInitsForScalar: {
Douglas Gregor99a2e602009-12-16 01:38:02 +00004121 SourceRange R;
4122
4123 if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
Douglas Gregor19311e72010-09-08 21:40:08 +00004124 R = SourceRange(InitList->getInit(0)->getLocEnd(),
Douglas Gregor99a2e602009-12-16 01:38:02 +00004125 InitList->getLocEnd());
Douglas Gregor19311e72010-09-08 21:40:08 +00004126 else
4127 R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd());
Douglas Gregord87b61f2009-12-10 17:56:55 +00004128
Douglas Gregor19311e72010-09-08 21:40:08 +00004129 R.setBegin(S.PP.getLocForEndOfToken(R.getBegin()));
4130 if (Kind.isCStyleOrFunctionalCast())
4131 S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg)
4132 << R;
4133 else
4134 S.Diag(Kind.getLocation(), diag::err_excess_initializers)
4135 << /*scalar=*/2 << R;
Douglas Gregord87b61f2009-12-10 17:56:55 +00004136 break;
4137 }
4138
4139 case FK_ReferenceBindingToInitList:
4140 S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
4141 << DestType.getNonReferenceType() << Args[0]->getSourceRange();
4142 break;
4143
4144 case FK_InitListBadDestinationType:
4145 S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
4146 << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
4147 break;
Douglas Gregor51c56d62009-12-14 20:49:26 +00004148
4149 case FK_ConstructorOverloadFailed: {
4150 SourceRange ArgsRange;
4151 if (NumArgs)
4152 ArgsRange = SourceRange(Args[0]->getLocStart(),
4153 Args[NumArgs - 1]->getLocEnd());
4154
4155 // FIXME: Using "DestType" for the entity we're printing is probably
4156 // bad.
4157 switch (FailedOverloadResult) {
4158 case OR_Ambiguous:
4159 S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
4160 << DestType << ArgsRange;
John McCall120d63c2010-08-24 20:38:10 +00004161 FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates,
4162 Args, NumArgs);
Douglas Gregor51c56d62009-12-14 20:49:26 +00004163 break;
4164
4165 case OR_No_Viable_Function:
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004166 if (Kind.getKind() == InitializationKind::IK_Default &&
4167 (Entity.getKind() == InitializedEntity::EK_Base ||
4168 Entity.getKind() == InitializedEntity::EK_Member) &&
4169 isa<CXXConstructorDecl>(S.CurContext)) {
4170 // This is implicit default initialization of a member or
4171 // base within a constructor. If no viable function was
4172 // found, notify the user that she needs to explicitly
4173 // initialize this base/member.
4174 CXXConstructorDecl *Constructor
4175 = cast<CXXConstructorDecl>(S.CurContext);
4176 if (Entity.getKind() == InitializedEntity::EK_Base) {
4177 S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
4178 << Constructor->isImplicit()
4179 << S.Context.getTypeDeclType(Constructor->getParent())
4180 << /*base=*/0
4181 << Entity.getType();
4182
4183 RecordDecl *BaseDecl
4184 = Entity.getBaseSpecifier()->getType()->getAs<RecordType>()
4185 ->getDecl();
4186 S.Diag(BaseDecl->getLocation(), diag::note_previous_decl)
4187 << S.Context.getTagDeclType(BaseDecl);
4188 } else {
4189 S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
4190 << Constructor->isImplicit()
4191 << S.Context.getTypeDeclType(Constructor->getParent())
4192 << /*member=*/1
4193 << Entity.getName();
4194 S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl);
4195
4196 if (const RecordType *Record
4197 = Entity.getType()->getAs<RecordType>())
4198 S.Diag(Record->getDecl()->getLocation(),
4199 diag::note_previous_decl)
4200 << S.Context.getTagDeclType(Record->getDecl());
4201 }
4202 break;
4203 }
4204
Douglas Gregor51c56d62009-12-14 20:49:26 +00004205 S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
4206 << DestType << ArgsRange;
John McCall120d63c2010-08-24 20:38:10 +00004207 FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs);
Douglas Gregor51c56d62009-12-14 20:49:26 +00004208 break;
4209
4210 case OR_Deleted: {
4211 S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
4212 << true << DestType << ArgsRange;
4213 OverloadCandidateSet::iterator Best;
John McCall120d63c2010-08-24 20:38:10 +00004214 OverloadingResult Ovl
4215 = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
Douglas Gregor51c56d62009-12-14 20:49:26 +00004216 if (Ovl == OR_Deleted) {
4217 S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
4218 << Best->Function->isDeleted();
4219 } else {
4220 llvm_unreachable("Inconsistent overload resolution?");
4221 }
4222 break;
4223 }
4224
4225 case OR_Success:
4226 llvm_unreachable("Conversion did not fail!");
4227 break;
4228 }
4229 break;
4230 }
Douglas Gregor99a2e602009-12-16 01:38:02 +00004231
4232 case FK_DefaultInitOfConst:
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00004233 if (Entity.getKind() == InitializedEntity::EK_Member &&
4234 isa<CXXConstructorDecl>(S.CurContext)) {
4235 // This is implicit default-initialization of a const member in
4236 // a constructor. Complain that it needs to be explicitly
4237 // initialized.
4238 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext);
4239 S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor)
4240 << Constructor->isImplicit()
4241 << S.Context.getTypeDeclType(Constructor->getParent())
4242 << /*const=*/1
4243 << Entity.getName();
4244 S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl)
4245 << Entity.getName();
4246 } else {
4247 S.Diag(Kind.getLocation(), diag::err_default_init_const)
4248 << DestType << (bool)DestType->getAs<RecordType>();
4249 }
Douglas Gregor99a2e602009-12-16 01:38:02 +00004250 break;
Douglas Gregor72a43bb2010-05-20 22:12:02 +00004251
4252 case FK_Incomplete:
4253 S.RequireCompleteType(Kind.getLocation(), DestType,
4254 diag::err_init_incomplete_type);
4255 break;
Douglas Gregor20093b42009-12-09 23:02:17 +00004256 }
4257
Douglas Gregora41a8c52010-04-22 00:20:18 +00004258 PrintInitLocationNote(S, Entity);
Douglas Gregor20093b42009-12-09 23:02:17 +00004259 return true;
4260}
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004261
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004262void InitializationSequence::dump(llvm::raw_ostream &OS) const {
4263 switch (SequenceKind) {
4264 case FailedSequence: {
4265 OS << "Failed sequence: ";
4266 switch (Failure) {
4267 case FK_TooManyInitsForReference:
4268 OS << "too many initializers for reference";
4269 break;
4270
4271 case FK_ArrayNeedsInitList:
4272 OS << "array requires initializer list";
4273 break;
4274
4275 case FK_ArrayNeedsInitListOrStringLiteral:
4276 OS << "array requires initializer list or string literal";
4277 break;
4278
4279 case FK_AddressOfOverloadFailed:
4280 OS << "address of overloaded function failed";
4281 break;
4282
4283 case FK_ReferenceInitOverloadFailed:
4284 OS << "overload resolution for reference initialization failed";
4285 break;
4286
4287 case FK_NonConstLValueReferenceBindingToTemporary:
4288 OS << "non-const lvalue reference bound to temporary";
4289 break;
4290
4291 case FK_NonConstLValueReferenceBindingToUnrelated:
4292 OS << "non-const lvalue reference bound to unrelated type";
4293 break;
4294
4295 case FK_RValueReferenceBindingToLValue:
4296 OS << "rvalue reference bound to an lvalue";
4297 break;
4298
4299 case FK_ReferenceInitDropsQualifiers:
4300 OS << "reference initialization drops qualifiers";
4301 break;
4302
4303 case FK_ReferenceInitFailed:
4304 OS << "reference initialization failed";
4305 break;
4306
4307 case FK_ConversionFailed:
4308 OS << "conversion failed";
4309 break;
4310
4311 case FK_TooManyInitsForScalar:
4312 OS << "too many initializers for scalar";
4313 break;
4314
4315 case FK_ReferenceBindingToInitList:
4316 OS << "referencing binding to initializer list";
4317 break;
4318
4319 case FK_InitListBadDestinationType:
4320 OS << "initializer list for non-aggregate, non-scalar type";
4321 break;
4322
4323 case FK_UserConversionOverloadFailed:
4324 OS << "overloading failed for user-defined conversion";
4325 break;
4326
4327 case FK_ConstructorOverloadFailed:
4328 OS << "constructor overloading failed";
4329 break;
4330
4331 case FK_DefaultInitOfConst:
4332 OS << "default initialization of a const variable";
4333 break;
Douglas Gregor72a43bb2010-05-20 22:12:02 +00004334
4335 case FK_Incomplete:
4336 OS << "initialization of incomplete type";
4337 break;
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004338 }
4339 OS << '\n';
4340 return;
4341 }
4342
4343 case DependentSequence:
4344 OS << "Dependent sequence: ";
4345 return;
4346
4347 case UserDefinedConversion:
4348 OS << "User-defined conversion sequence: ";
4349 break;
4350
4351 case ConstructorInitialization:
4352 OS << "Constructor initialization sequence: ";
4353 break;
4354
4355 case ReferenceBinding:
4356 OS << "Reference binding: ";
4357 break;
4358
4359 case ListInitialization:
4360 OS << "List initialization: ";
4361 break;
4362
4363 case ZeroInitialization:
4364 OS << "Zero initialization\n";
4365 return;
4366
4367 case NoInitialization:
4368 OS << "No initialization\n";
4369 return;
4370
4371 case StandardConversion:
4372 OS << "Standard conversion: ";
4373 break;
4374
4375 case CAssignment:
4376 OS << "C assignment: ";
4377 break;
4378
4379 case StringInit:
4380 OS << "String initialization: ";
4381 break;
4382 }
4383
4384 for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) {
4385 if (S != step_begin()) {
4386 OS << " -> ";
4387 }
4388
4389 switch (S->Kind) {
4390 case SK_ResolveAddressOfOverloadedFunction:
4391 OS << "resolve address of overloaded function";
4392 break;
4393
4394 case SK_CastDerivedToBaseRValue:
4395 OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")";
4396 break;
4397
Sebastian Redl906082e2010-07-20 04:20:21 +00004398 case SK_CastDerivedToBaseXValue:
4399 OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")";
4400 break;
4401
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004402 case SK_CastDerivedToBaseLValue:
4403 OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")";
4404 break;
4405
4406 case SK_BindReference:
4407 OS << "bind reference to lvalue";
4408 break;
4409
4410 case SK_BindReferenceToTemporary:
4411 OS << "bind reference to a temporary";
4412 break;
4413
Douglas Gregor523d46a2010-04-18 07:40:54 +00004414 case SK_ExtraneousCopyToTemporary:
4415 OS << "extraneous C++03 copy to temporary";
4416 break;
4417
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004418 case SK_UserConversion:
Benjamin Kramer900fc632010-04-17 09:33:03 +00004419 OS << "user-defined conversion via " << S->Function.Function;
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004420 break;
Sebastian Redl906082e2010-07-20 04:20:21 +00004421
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004422 case SK_QualificationConversionRValue:
4423 OS << "qualification conversion (rvalue)";
4424
Sebastian Redl906082e2010-07-20 04:20:21 +00004425 case SK_QualificationConversionXValue:
4426 OS << "qualification conversion (xvalue)";
4427
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004428 case SK_QualificationConversionLValue:
4429 OS << "qualification conversion (lvalue)";
4430 break;
4431
4432 case SK_ConversionSequence:
4433 OS << "implicit conversion sequence (";
4434 S->ICS->DebugPrint(); // FIXME: use OS
4435 OS << ")";
4436 break;
4437
4438 case SK_ListInitialization:
4439 OS << "list initialization";
4440 break;
4441
4442 case SK_ConstructorInitialization:
4443 OS << "constructor initialization";
4444 break;
4445
4446 case SK_ZeroInitialization:
4447 OS << "zero initialization";
4448 break;
4449
4450 case SK_CAssignment:
4451 OS << "C assignment";
4452 break;
4453
4454 case SK_StringInit:
4455 OS << "string initialization";
4456 break;
Douglas Gregor569c3162010-08-07 11:51:51 +00004457
4458 case SK_ObjCObjectConversion:
4459 OS << "Objective-C object conversion";
4460 break;
Douglas Gregorde4b1d82010-01-29 19:14:02 +00004461 }
4462 }
4463}
4464
4465void InitializationSequence::dump() const {
4466 dump(llvm::errs());
4467}
4468
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004469//===----------------------------------------------------------------------===//
4470// Initialization helper functions
4471//===----------------------------------------------------------------------===//
John McCall60d7b3a2010-08-24 06:29:42 +00004472ExprResult
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004473Sema::PerformCopyInitialization(const InitializedEntity &Entity,
4474 SourceLocation EqualLoc,
John McCall60d7b3a2010-08-24 06:29:42 +00004475 ExprResult Init) {
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004476 if (Init.isInvalid())
4477 return ExprError();
4478
4479 Expr *InitE = (Expr *)Init.get();
4480 assert(InitE && "No initialization expression?");
4481
4482 if (EqualLoc.isInvalid())
4483 EqualLoc = InitE->getLocStart();
4484
4485 InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
4486 EqualLoc);
4487 InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
4488 Init.release();
John McCallf312b1e2010-08-26 23:41:50 +00004489 return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1));
Douglas Gregor18ef5e22009-12-18 05:02:21 +00004490}