blob: f4c375dfff16fd4f36cb71cd9806c2839e8a020a [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
18#include "Sema.h"
Douglas Gregor05c13a32009-01-22 00:58:24 +000019#include "clang/Parse/Designator.h"
Steve Naroff0cca7492008-05-01 22:18:59 +000020#include "clang/AST/ASTContext.h"
Chris Lattner79e079d2009-02-24 23:10:27 +000021#include "clang/AST/ExprObjC.h"
Douglas Gregorc34ee5e2009-01-29 00:45:39 +000022#include <map>
Douglas Gregor05c13a32009-01-22 00:58:24 +000023using namespace clang;
Steve Naroff0cca7492008-05-01 22:18:59 +000024
Chris Lattnerdd8e0062009-02-24 22:27:37 +000025//===----------------------------------------------------------------------===//
26// Sema Initialization Checking
27//===----------------------------------------------------------------------===//
28
Chris Lattner79e079d2009-02-24 23:10:27 +000029static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
Chris Lattnerdd8e0062009-02-24 22:27:37 +000030 if (const ArrayType *AT = Context.getAsArrayType(DeclType))
Chris Lattner79e079d2009-02-24 23:10:27 +000031 if (AT->getElementType()->isCharType()) {
32 Init = Init->IgnoreParens();
33 if (isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init))
34 return Init;
35 }
Chris Lattnerdd8e0062009-02-24 22:27:37 +000036 return 0;
37}
38
Chris Lattner95e8d652009-02-24 22:46:58 +000039static bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
40 bool DirectInit, Sema &S) {
Chris Lattnerdd8e0062009-02-24 22:27:37 +000041 // Get the type before calling CheckSingleAssignmentConstraints(), since
42 // it can promote the expression.
43 QualType InitType = Init->getType();
44
Chris Lattner95e8d652009-02-24 22:46:58 +000045 if (S.getLangOptions().CPlusPlus) {
Chris Lattnerdd8e0062009-02-24 22:27:37 +000046 // FIXME: I dislike this error message. A lot.
Chris Lattner95e8d652009-02-24 22:46:58 +000047 if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit))
48 return S.Diag(Init->getSourceRange().getBegin(),
49 diag::err_typecheck_convert_incompatible)
50 << DeclType << Init->getType() << "initializing"
51 << Init->getSourceRange();
Chris Lattnerdd8e0062009-02-24 22:27:37 +000052 return false;
53 }
54
Chris Lattner95e8d652009-02-24 22:46:58 +000055 Sema::AssignConvertType ConvTy =
56 S.CheckSingleAssignmentConstraints(DeclType, Init);
57 return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
Chris Lattnerdd8e0062009-02-24 22:27:37 +000058 InitType, Init, "initializing");
59}
60
Chris Lattner79e079d2009-02-24 23:10:27 +000061static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
62 // Get the length of the string as parsed.
63 uint64_t StrLength =
64 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
65
Chris Lattnerdd8e0062009-02-24 22:27:37 +000066
Chris Lattner79e079d2009-02-24 23:10:27 +000067 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +000068 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
69 // C99 6.7.8p14. We have an array of character type with unknown size
70 // being initialized to a string literal.
71 llvm::APSInt ConstVal(32);
Chris Lattner19da8cd2009-02-24 23:01:39 +000072 ConstVal = StrLength;
Chris Lattnerdd8e0062009-02-24 22:27:37 +000073 // Return a new array type (C99 6.7.8p22).
Chris Lattnerf71ae8d2009-02-24 22:41:04 +000074 DeclT = S.Context.getConstantArrayType(IAT->getElementType(), ConstVal,
75 ArrayType::Normal, 0);
Chris Lattner19da8cd2009-02-24 23:01:39 +000076 return;
Chris Lattnerdd8e0062009-02-24 22:27:37 +000077 }
Chris Lattner19da8cd2009-02-24 23:01:39 +000078
79 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
80
81 // C99 6.7.8p14. We have an array of character type with known size. However,
82 // the size may be smaller or larger than the string we are initializing.
83 // FIXME: Avoid truncation for 64-bit length strings.
Chris Lattner79e079d2009-02-24 23:10:27 +000084 if (StrLength-1 > CAT->getSize().getZExtValue())
Chris Lattner19da8cd2009-02-24 23:01:39 +000085 S.Diag(Str->getSourceRange().getBegin(),
86 diag::warn_initializer_string_for_char_array_too_long)
87 << Str->getSourceRange();
88
89 // Set the type to the actual size that we are initializing. If we have
90 // something like:
91 // char x[1] = "foo";
92 // then this will set the string literal's type to char[1].
Chris Lattnerf71ae8d2009-02-24 22:41:04 +000093 Str->setType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +000094}
95
96bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
97 SourceLocation InitLoc,
98 DeclarationName InitEntity,
99 bool DirectInit) {
100 if (DeclType->isDependentType() || Init->isTypeDependent())
101 return false;
102
103 // C++ [dcl.init.ref]p1:
104 // A variable declared to be a T&, that is "reference to type T"
105 // (8.3.2), shall be initialized by an object, or function, of
106 // type T or by an object that can be converted into a T.
107 if (DeclType->isReferenceType())
108 return CheckReferenceInit(Init, DeclType, 0, false, DirectInit);
109
110 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
111 // of unknown size ("[]") or an object type that is not a variable array type.
112 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
113 return Diag(InitLoc, diag::err_variable_object_no_init)
114 << VAT->getSizeExpr()->getSourceRange();
115
116 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
117 if (!InitList) {
118 // FIXME: Handle wide strings
Chris Lattner79e079d2009-02-24 23:10:27 +0000119 if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
120 CheckStringInit(Str, DeclType, *this);
Chris Lattner19da8cd2009-02-24 23:01:39 +0000121 return false;
122 }
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000123
124 // C++ [dcl.init]p14:
125 // -- If the destination type is a (possibly cv-qualified) class
126 // type:
127 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
128 QualType DeclTypeC = Context.getCanonicalType(DeclType);
129 QualType InitTypeC = Context.getCanonicalType(Init->getType());
130
131 // -- If the initialization is direct-initialization, or if it is
132 // copy-initialization where the cv-unqualified version of the
133 // source type is the same class as, or a derived class of, the
134 // class of the destination, constructors are considered.
135 if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
136 IsDerivedFrom(InitTypeC, DeclTypeC)) {
137 CXXConstructorDecl *Constructor
138 = PerformInitializationByConstructor(DeclType, &Init, 1,
139 InitLoc, Init->getSourceRange(),
140 InitEntity,
141 DirectInit? IK_Direct : IK_Copy);
142 return Constructor == 0;
143 }
144
145 // -- Otherwise (i.e., for the remaining copy-initialization
146 // cases), user-defined conversion sequences that can
147 // convert from the source type to the destination type or
148 // (when a conversion function is used) to a derived class
149 // thereof are enumerated as described in 13.3.1.4, and the
150 // best one is chosen through overload resolution
151 // (13.3). If the conversion cannot be done or is
152 // ambiguous, the initialization is ill-formed. The
153 // function selected is called with the initializer
154 // expression as its argument; if the function is a
155 // constructor, the call initializes a temporary of the
156 // destination type.
157 // FIXME: We're pretending to do copy elision here; return to
158 // this when we have ASTs for such things.
159 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
160 return false;
161
162 if (InitEntity)
163 return Diag(InitLoc, diag::err_cannot_initialize_decl)
164 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
165 << Init->getType() << Init->getSourceRange();
166 else
167 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
168 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
169 << Init->getType() << Init->getSourceRange();
170 }
171
172 // C99 6.7.8p16.
173 if (DeclType->isArrayType())
174 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
175 << Init->getSourceRange();
176
Chris Lattner95e8d652009-02-24 22:46:58 +0000177 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000178 }
179
180 bool hadError = CheckInitList(InitList, DeclType);
181 Init = InitList;
182 return hadError;
183}
184
185//===----------------------------------------------------------------------===//
186// Semantic checking for initializer lists.
187//===----------------------------------------------------------------------===//
188
Douglas Gregor9e80f722009-01-29 01:05:33 +0000189/// @brief Semantic checking for initializer lists.
190///
191/// The InitListChecker class contains a set of routines that each
192/// handle the initialization of a certain kind of entity, e.g.,
193/// arrays, vectors, struct/union types, scalars, etc. The
194/// InitListChecker itself performs a recursive walk of the subobject
195/// structure of the type to be initialized, while stepping through
196/// the initializer list one element at a time. The IList and Index
197/// parameters to each of the Check* routines contain the active
198/// (syntactic) initializer list and the index into that initializer
199/// list that represents the current initializer. Each routine is
200/// responsible for moving that Index forward as it consumes elements.
201///
202/// Each Check* routine also has a StructuredList/StructuredIndex
203/// arguments, which contains the current the "structured" (semantic)
204/// initializer list and the index into that initializer list where we
205/// are copying initializers as we map them over to the semantic
206/// list. Once we have completed our recursive walk of the subobject
207/// structure, we will have constructed a full semantic initializer
208/// list.
209///
210/// C99 designators cause changes in the initializer list traversal,
211/// because they make the initialization "jump" into a specific
212/// subobject and then continue the initialization from that
213/// point. CheckDesignatedInitializer() recursively steps into the
214/// designated subobject and manages backing out the recursion to
215/// initialize the subobjects after the one designated.
Chris Lattner8b419b92009-02-24 22:48:58 +0000216namespace {
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000217class InitListChecker {
Chris Lattner08202542009-02-24 22:50:46 +0000218 Sema &SemaRef;
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000219 bool hadError;
220 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
221 InitListExpr *FullyStructuredList;
222
223 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000224 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000225 unsigned &StructuredIndex,
226 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000227 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000228 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000229 unsigned &StructuredIndex,
230 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000231 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
232 bool SubobjectIsDesignatorContext,
233 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000234 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000235 unsigned &StructuredIndex,
236 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000237 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
238 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000239 InitListExpr *StructuredList,
240 unsigned &StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000241 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000242 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000243 InitListExpr *StructuredList,
244 unsigned &StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000245 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
246 unsigned &Index,
247 InitListExpr *StructuredList,
248 unsigned &StructuredIndex);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000249 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000250 InitListExpr *StructuredList,
251 unsigned &StructuredIndex);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000252 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
253 RecordDecl::field_iterator Field,
254 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000255 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000256 unsigned &StructuredIndex,
257 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000258 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
259 llvm::APSInt elementIndex,
260 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000261 InitListExpr *StructuredList,
262 unsigned &StructuredIndex);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000263 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
264 DesignatedInitExpr::designators_iterator D,
265 QualType &CurrentObjectType,
266 RecordDecl::field_iterator *NextField,
267 llvm::APSInt *NextElementIndex,
268 unsigned &Index,
269 InitListExpr *StructuredList,
270 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000271 bool FinishSubobjectInit,
272 bool TopLevelObject);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000273 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
274 QualType CurrentObjectType,
275 InitListExpr *StructuredList,
276 unsigned StructuredIndex,
277 SourceRange InitRange);
Douglas Gregor9e80f722009-01-29 01:05:33 +0000278 void UpdateStructuredListElement(InitListExpr *StructuredList,
279 unsigned &StructuredIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000280 Expr *expr);
281 int numArrayElements(QualType DeclType);
282 int numStructUnionElements(QualType DeclType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000283
284 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000285public:
Chris Lattner08202542009-02-24 22:50:46 +0000286 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000287 bool HadError() { return hadError; }
288
289 // @brief Retrieves the fully-structured initializer list used for
290 // semantic analysis and code generation.
291 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
292};
Chris Lattner8b419b92009-02-24 22:48:58 +0000293} // end anonymous namespace
Chris Lattner68355a52009-01-29 05:10:57 +0000294
Douglas Gregor4c678342009-01-28 21:54:33 +0000295/// Recursively replaces NULL values within the given initializer list
296/// with expressions that perform value-initialization of the
297/// appropriate type.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000298void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Chris Lattner08202542009-02-24 22:50:46 +0000299 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregor930d8b52009-01-30 22:09:00 +0000300 "Should not have void type");
Douglas Gregor87fd7032009-02-02 17:43:21 +0000301 SourceLocation Loc = ILE->getSourceRange().getBegin();
302 if (ILE->getSyntacticForm())
303 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
304
Douglas Gregor4c678342009-01-28 21:54:33 +0000305 if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
306 unsigned Init = 0, NumInits = ILE->getNumInits();
307 for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(),
308 FieldEnd = RType->getDecl()->field_end();
309 Field != FieldEnd; ++Field) {
310 if (Field->isUnnamedBitfield())
311 continue;
312
Douglas Gregor87fd7032009-02-02 17:43:21 +0000313 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000314 if (Field->getType()->isReferenceType()) {
315 // C++ [dcl.init.aggr]p9:
316 // If an incomplete or empty initializer-list leaves a
317 // member of reference type uninitialized, the program is
318 // ill-formed.
Chris Lattner08202542009-02-24 22:50:46 +0000319 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000320 << Field->getType()
321 << ILE->getSyntacticForm()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +0000322 SemaRef.Diag(Field->getLocation(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000323 diag::note_uninit_reference_member);
324 hadError = true;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000325 return;
Chris Lattner08202542009-02-24 22:50:46 +0000326 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000327 hadError = true;
328 return;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000329 }
Douglas Gregor87fd7032009-02-02 17:43:21 +0000330
331 // FIXME: If value-initialization involves calling a
332 // constructor, should we make that call explicit in the
333 // representation (even when it means extending the
334 // initializer list)?
335 if (Init < NumInits && !hadError)
336 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000337 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Douglas Gregor87fd7032009-02-02 17:43:21 +0000338 } else if (InitListExpr *InnerILE
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000339 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000340 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000341 ++Init;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000342
343 // Only look at the first initialization of a union.
344 if (RType->getDecl()->isUnion())
345 break;
Douglas Gregor4c678342009-01-28 21:54:33 +0000346 }
347
348 return;
349 }
350
351 QualType ElementType;
352
Douglas Gregor87fd7032009-02-02 17:43:21 +0000353 unsigned NumInits = ILE->getNumInits();
354 unsigned NumElements = NumInits;
Chris Lattner08202542009-02-24 22:50:46 +0000355 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000356 ElementType = AType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000357 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
358 NumElements = CAType->getSize().getZExtValue();
359 } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000360 ElementType = VType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000361 NumElements = VType->getNumElements();
362 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000363 ElementType = ILE->getType();
364
Douglas Gregor87fd7032009-02-02 17:43:21 +0000365 for (unsigned Init = 0; Init != NumElements; ++Init) {
366 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattner08202542009-02-24 22:50:46 +0000367 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000368 hadError = true;
369 return;
370 }
371
372 // FIXME: If value-initialization involves calling a
373 // constructor, should we make that call explicit in the
374 // representation (even when it means extending the
375 // initializer list)?
376 if (Init < NumInits && !hadError)
377 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000378 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Douglas Gregor87fd7032009-02-02 17:43:21 +0000379 }
Chris Lattner68355a52009-01-29 05:10:57 +0000380 else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000381 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000382 }
383}
384
Chris Lattner68355a52009-01-29 05:10:57 +0000385
Chris Lattner08202542009-02-24 22:50:46 +0000386InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
387 : SemaRef(S) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000388 hadError = false;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000389
Eli Friedmanb85f7072008-05-19 19:16:24 +0000390 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000391 unsigned newStructuredIndex = 0;
392 FullyStructuredList
393 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, SourceRange());
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000394 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
395 /*TopLevelObject=*/true);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000396
Douglas Gregor930d8b52009-01-30 22:09:00 +0000397 if (!hadError)
398 FillInValueInitializations(FullyStructuredList);
Steve Naroff0cca7492008-05-01 22:18:59 +0000399}
400
401int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +0000402 // FIXME: use a proper constant
403 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000404 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000405 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000406 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
407 }
408 return maxElements;
409}
410
411int InitListChecker::numStructUnionElements(QualType DeclType) {
412 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Douglas Gregor4c678342009-01-28 21:54:33 +0000413 int InitializableMembers = 0;
414 for (RecordDecl::field_iterator Field = structDecl->field_begin(),
415 FieldEnd = structDecl->field_end();
416 Field != FieldEnd; ++Field) {
417 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
418 ++InitializableMembers;
419 }
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000420 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +0000421 return std::min(InitializableMembers, 1);
422 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-05-01 22:18:59 +0000423}
424
425void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregor4c678342009-01-28 21:54:33 +0000426 QualType T, unsigned &Index,
427 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000428 unsigned &StructuredIndex,
429 bool TopLevelObject) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000430 int maxElements = 0;
431
432 if (T->isArrayType())
433 maxElements = numArrayElements(T);
434 else if (T->isStructureType() || T->isUnionType())
435 maxElements = numStructUnionElements(T);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000436 else if (T->isVectorType())
437 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +0000438 else
439 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +0000440
Eli Friedman402256f2008-05-25 13:49:22 +0000441 if (maxElements == 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000442 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedman402256f2008-05-25 13:49:22 +0000443 diag::err_implicit_empty_initializer);
Douglas Gregor4c678342009-01-28 21:54:33 +0000444 ++Index;
Eli Friedman402256f2008-05-25 13:49:22 +0000445 hadError = true;
446 return;
447 }
448
Douglas Gregor4c678342009-01-28 21:54:33 +0000449 // Build a structured initializer list corresponding to this subobject.
450 InitListExpr *StructuredSubobjectInitList
451 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
452 StructuredIndex,
453 ParentIList->getInit(Index)->getSourceRange());
454 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000455
Douglas Gregor4c678342009-01-28 21:54:33 +0000456 // Check the element types and build the structural subobject.
Douglas Gregor87fd7032009-02-02 17:43:21 +0000457 unsigned StartIndex = Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000458 CheckListElementTypes(ParentIList, T, false, Index,
459 StructuredSubobjectInitList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000460 StructuredSubobjectInitIndex,
461 TopLevelObject);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000462 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
463
464 // Update the structured sub-object initialize so that it's ending
465 // range corresponds with the end of the last initializer it used.
466 if (EndIndex < ParentIList->getNumInits()) {
467 SourceLocation EndLoc
468 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
469 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
470 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000471}
472
Steve Naroffa647caa2008-05-06 00:23:44 +0000473void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor4c678342009-01-28 21:54:33 +0000474 unsigned &Index,
475 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000476 unsigned &StructuredIndex,
477 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000478 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor4c678342009-01-28 21:54:33 +0000479 SyntacticToSemantic[IList] = StructuredList;
480 StructuredList->setSyntacticForm(IList);
481 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000482 StructuredIndex, TopLevelObject);
Steve Naroffa647caa2008-05-06 00:23:44 +0000483 IList->setType(T);
Douglas Gregor4c678342009-01-28 21:54:33 +0000484 StructuredList->setType(T);
Eli Friedman638e1442008-05-25 13:22:35 +0000485 if (hadError)
486 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000487
Eli Friedman638e1442008-05-25 13:22:35 +0000488 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000489 // We have leftover initializers
490 if (IList->getNumInits() > 0 &&
Chris Lattner08202542009-02-24 22:50:46 +0000491 IsStringInit(IList->getInit(Index), T, SemaRef.Context)) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000492 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Chris Lattner08202542009-02-24 22:50:46 +0000493 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000494 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000495 // Special-case
Chris Lattner08202542009-02-24 22:50:46 +0000496 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000497 << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000498 hadError = true;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000499 } else if (!T->isIncompleteType()) {
Douglas Gregorb574e562009-01-30 22:26:29 +0000500 // Don't complain for incomplete types, since we'll get an error
501 // elsewhere
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000502 QualType CurrentObjectType = StructuredList->getType();
503 int initKind =
504 CurrentObjectType->isArrayType()? 0 :
505 CurrentObjectType->isVectorType()? 1 :
506 CurrentObjectType->isScalarType()? 2 :
507 CurrentObjectType->isUnionType()? 3 :
508 4;
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000509
510 unsigned DK = diag::warn_excess_initializers;
Chris Lattner08202542009-02-24 22:50:46 +0000511 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000512 DK = diag::err_excess_initializers;
513
Chris Lattner08202542009-02-24 22:50:46 +0000514 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000515 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000516 }
517 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000518
Eli Friedman638e1442008-05-25 13:22:35 +0000519 if (T->isScalarType())
Chris Lattner08202542009-02-24 22:50:46 +0000520 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000521 << IList->getSourceRange();
Steve Naroff0cca7492008-05-01 22:18:59 +0000522}
523
Eli Friedmanb85f7072008-05-19 19:16:24 +0000524void InitListChecker::CheckListElementTypes(InitListExpr *IList,
525 QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000526 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000527 unsigned &Index,
528 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000529 unsigned &StructuredIndex,
530 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000531 if (DeclType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000532 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000533 } else if (DeclType->isVectorType()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000534 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregord7eb8462009-01-30 17:31:00 +0000535 } else if (DeclType->isAggregateType()) {
536 if (DeclType->isRecordType()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000537 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
538 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000539 SubobjectIsDesignatorContext, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000540 StructuredList, StructuredIndex,
541 TopLevelObject);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000542 } else if (DeclType->isArrayType()) {
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000543 llvm::APSInt Zero(
Chris Lattner08202542009-02-24 22:50:46 +0000544 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000545 false);
Douglas Gregor4c678342009-01-28 21:54:33 +0000546 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
547 StructuredList, StructuredIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000548 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000549 else
Douglas Gregor4c678342009-01-28 21:54:33 +0000550 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000551 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
552 // This type is invalid, issue a diagnostic.
Douglas Gregor4c678342009-01-28 21:54:33 +0000553 ++Index;
Chris Lattner08202542009-02-24 22:50:46 +0000554 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000555 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000556 hadError = true;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000557 } else if (DeclType->isRecordType()) {
558 // C++ [dcl.init]p14:
559 // [...] If the class is an aggregate (8.5.1), and the initializer
560 // is a brace-enclosed list, see 8.5.1.
561 //
562 // Note: 8.5.1 is handled below; here, we diagnose the case where
563 // we have an initializer list and a destination type that is not
564 // an aggregate.
565 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner08202542009-02-24 22:50:46 +0000566 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000567 << DeclType << IList->getSourceRange();
568 hadError = true;
569 } else if (DeclType->isReferenceType()) {
570 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000571 } else {
572 // In C, all types are either scalars or aggregates, but
573 // additional handling is needed here for C++ (and possibly others?).
574 assert(0 && "Unsupported initializer type");
575 }
576}
577
Eli Friedmanb85f7072008-05-19 19:16:24 +0000578void InitListChecker::CheckSubElementType(InitListExpr *IList,
579 QualType ElemType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000580 unsigned &Index,
581 InitListExpr *StructuredList,
582 unsigned &StructuredIndex) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000583 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000584 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
585 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000586 unsigned newStructuredIndex = 0;
587 InitListExpr *newStructuredList
588 = getStructuredSubobjectInit(IList, Index, ElemType,
589 StructuredList, StructuredIndex,
590 SubInitList->getSourceRange());
591 CheckExplicitInitList(SubInitList, ElemType, newIndex,
592 newStructuredList, newStructuredIndex);
593 ++StructuredIndex;
594 ++Index;
Chris Lattner79e079d2009-02-24 23:10:27 +0000595 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
596 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000597 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor4c678342009-01-28 21:54:33 +0000598 ++Index;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000599 } else if (ElemType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000600 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000601 } else if (ElemType->isReferenceType()) {
602 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000603 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000604 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000605 // C++ [dcl.init.aggr]p12:
606 // All implicit type conversions (clause 4) are considered when
607 // initializing the aggregate member with an ini- tializer from
608 // an initializer-list. If the initializer can initialize a
609 // member, the member is initialized. [...]
610 ImplicitConversionSequence ICS
Chris Lattner08202542009-02-24 22:50:46 +0000611 = SemaRef.TryCopyInitialization(expr, ElemType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000612 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Chris Lattner08202542009-02-24 22:50:46 +0000613 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000614 "initializing"))
615 hadError = true;
616 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
617 ++Index;
618 return;
619 }
620
621 // Fall through for subaggregate initialization
622 } else {
623 // C99 6.7.8p13:
624 //
625 // The initializer for a structure or union object that has
626 // automatic storage duration shall be either an initializer
627 // list as described below, or a single expression that has
628 // compatible structure or union type. In the latter case, the
629 // initial value of the object, including unnamed members, is
630 // that of the expression.
Chris Lattner08202542009-02-24 22:50:46 +0000631 QualType ExprType = SemaRef.Context.getCanonicalType(expr->getType());
632 QualType ElemTypeCanon = SemaRef.Context.getCanonicalType(ElemType);
633 if (SemaRef.Context.typesAreCompatible(ExprType.getUnqualifiedType(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000634 ElemTypeCanon.getUnqualifiedType())) {
635 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
636 ++Index;
637 return;
638 }
639
640 // Fall through for subaggregate initialization
641 }
642
643 // C++ [dcl.init.aggr]p12:
644 //
645 // [...] Otherwise, if the member is itself a non-empty
646 // subaggregate, brace elision is assumed and the initializer is
647 // considered for the initialization of the first member of
648 // the subaggregate.
649 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
650 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
651 StructuredIndex);
652 ++StructuredIndex;
653 } else {
654 // We cannot initialize this element, so let
655 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattner08202542009-02-24 22:50:46 +0000656 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregor930d8b52009-01-30 22:09:00 +0000657 hadError = true;
658 ++Index;
659 ++StructuredIndex;
660 }
661 }
Eli Friedmanb85f7072008-05-19 19:16:24 +0000662}
663
Douglas Gregor930d8b52009-01-30 22:09:00 +0000664void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000665 unsigned &Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000666 InitListExpr *StructuredList,
667 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000668 if (Index < IList->getNumInits()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000669 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000670 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000671 SemaRef.Diag(IList->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000672 diag::err_many_braces_around_scalar_init)
673 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000674 hadError = true;
675 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000676 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000677 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000678 } else if (isa<DesignatedInitExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000679 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor05c13a32009-01-22 00:58:24 +0000680 diag::err_designator_for_scalar_init)
681 << DeclType << expr->getSourceRange();
682 hadError = true;
683 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000684 ++StructuredIndex;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000685 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000686 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000687
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000688 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner08202542009-02-24 22:50:46 +0000689 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedmanbb504d32008-05-19 20:12:18 +0000690 hadError = true; // types weren't compatible.
Douglas Gregor05c13a32009-01-22 00:58:24 +0000691 else if (savExpr != expr) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000692 // The type was promoted, update initializer list.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000693 IList->setInit(Index, expr);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000694 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000695 if (hadError)
696 ++StructuredIndex;
697 else
698 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroff0cca7492008-05-01 22:18:59 +0000699 ++Index;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000700 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000701 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000702 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000703 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000704 ++Index;
705 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000706 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000707 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000708}
709
Douglas Gregor930d8b52009-01-30 22:09:00 +0000710void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
711 unsigned &Index,
712 InitListExpr *StructuredList,
713 unsigned &StructuredIndex) {
714 if (Index < IList->getNumInits()) {
715 Expr *expr = IList->getInit(Index);
716 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000717 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000718 << DeclType << IList->getSourceRange();
719 hadError = true;
720 ++Index;
721 ++StructuredIndex;
722 return;
723 }
724
725 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner08202542009-02-24 22:50:46 +0000726 if (SemaRef.CheckReferenceInit(expr, DeclType))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000727 hadError = true;
728 else if (savExpr != expr) {
729 // The type was promoted, update initializer list.
730 IList->setInit(Index, expr);
731 }
732 if (hadError)
733 ++StructuredIndex;
734 else
735 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
736 ++Index;
737 } else {
738 // FIXME: It would be wonderful if we could point at the actual
739 // member. In general, it would be useful to pass location
740 // information down the stack, so that we know the location (or
741 // decl) of the "current object" being initialized.
Chris Lattner08202542009-02-24 22:50:46 +0000742 SemaRef.Diag(IList->getLocStart(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000743 diag::err_init_reference_member_uninitialized)
744 << DeclType
745 << IList->getSourceRange();
746 hadError = true;
747 ++Index;
748 ++StructuredIndex;
749 return;
750 }
751}
752
Steve Naroff0cca7492008-05-01 22:18:59 +0000753void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000754 unsigned &Index,
755 InitListExpr *StructuredList,
756 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000757 if (Index < IList->getNumInits()) {
758 const VectorType *VT = DeclType->getAsVectorType();
759 int maxElements = VT->getNumElements();
760 QualType elementType = VT->getElementType();
761
762 for (int i = 0; i < maxElements; ++i) {
763 // Don't attempt to go past the end of the init list
764 if (Index >= IList->getNumInits())
765 break;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000766 CheckSubElementType(IList, elementType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000767 StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000768 }
769 }
770}
771
772void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000773 llvm::APSInt elementIndex,
774 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000775 unsigned &Index,
776 InitListExpr *StructuredList,
777 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000778 // Check for the special-case of initializing an array with a string.
779 if (Index < IList->getNumInits()) {
Chris Lattner79e079d2009-02-24 23:10:27 +0000780 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
781 SemaRef.Context)) {
782 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor4c678342009-01-28 21:54:33 +0000783 // We place the string literal directly into the resulting
784 // initializer list. This is the only place where the structure
785 // of the structured initializer list doesn't match exactly,
786 // because doing so would involve allocating one character
787 // constant for each string.
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000788 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner08202542009-02-24 22:50:46 +0000789 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000790 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000791 return;
792 }
793 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000794 if (const VariableArrayType *VAT =
Chris Lattner08202542009-02-24 22:50:46 +0000795 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman638e1442008-05-25 13:22:35 +0000796 // Check for VLAs; in standard C it would be possible to check this
797 // earlier, but I don't know where clang accepts VLAs (gcc accepts
798 // them in all sorts of strange places).
Chris Lattner08202542009-02-24 22:50:46 +0000799 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000800 diag::err_variable_object_no_init)
801 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000802 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000803 ++Index;
804 ++StructuredIndex;
Eli Friedman638e1442008-05-25 13:22:35 +0000805 return;
806 }
807
Douglas Gregor05c13a32009-01-22 00:58:24 +0000808 // We might know the maximum number of elements in advance.
Douglas Gregor4c678342009-01-28 21:54:33 +0000809 llvm::APSInt maxElements(elementIndex.getBitWidth(),
810 elementIndex.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000811 bool maxElementsKnown = false;
812 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000813 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000814 maxElements = CAT->getSize();
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000815 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000816 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000817 maxElementsKnown = true;
818 }
819
Chris Lattner08202542009-02-24 22:50:46 +0000820 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000821 ->getElementType();
Douglas Gregor05c13a32009-01-22 00:58:24 +0000822 while (Index < IList->getNumInits()) {
823 Expr *Init = IList->getInit(Index);
824 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000825 // If we're not the subobject that matches up with the '{' for
826 // the designator, we shouldn't be handling the
827 // designator. Return immediately.
828 if (!SubobjectIsDesignatorContext)
829 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000830
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000831 // Handle this designated initializer. elementIndex will be
832 // updated to be the next array element we'll initialize.
833 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000834 DeclType, 0, &elementIndex, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000835 StructuredList, StructuredIndex, true,
836 false)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000837 hadError = true;
838 continue;
839 }
840
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000841 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
842 maxElements.extend(elementIndex.getBitWidth());
843 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
844 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000845 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000846
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000847 // If the array is of incomplete type, keep track of the number of
848 // elements in the initializer.
849 if (!maxElementsKnown && elementIndex > maxElements)
850 maxElements = elementIndex;
851
Douglas Gregor05c13a32009-01-22 00:58:24 +0000852 continue;
853 }
854
855 // If we know the maximum number of elements, and we've already
856 // hit it, stop consuming elements in the initializer list.
857 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroff0cca7492008-05-01 22:18:59 +0000858 break;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000859
860 // Check this element.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000861 CheckSubElementType(IList, elementType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000862 StructuredList, StructuredIndex);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000863 ++elementIndex;
864
865 // If the array is of incomplete type, keep track of the number of
866 // elements in the initializer.
867 if (!maxElementsKnown && elementIndex > maxElements)
868 maxElements = elementIndex;
Steve Naroff0cca7492008-05-01 22:18:59 +0000869 }
870 if (DeclType->isIncompleteArrayType()) {
871 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000872 // be calculated here.
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000873 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000874 if (maxElements == Zero) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000875 // Sizing an array implicitly to zero is not allowed by ISO C,
876 // but is supported by GNU.
Chris Lattner08202542009-02-24 22:50:46 +0000877 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000878 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +0000879 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000880
Chris Lattner08202542009-02-24 22:50:46 +0000881 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000882 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +0000883 }
884}
885
886void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
887 QualType DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000888 RecordDecl::field_iterator Field,
889 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000890 unsigned &Index,
891 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000892 unsigned &StructuredIndex,
893 bool TopLevelObject) {
Eli Friedmanb85f7072008-05-19 19:16:24 +0000894 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroff0cca7492008-05-01 22:18:59 +0000895
Eli Friedmanb85f7072008-05-19 19:16:24 +0000896 // If the record is invalid, some of it's members are invalid. To avoid
897 // confusion, we forgo checking the intializer for the entire record.
898 if (structDecl->isInvalidDecl()) {
899 hadError = true;
900 return;
901 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000902
903 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
904 // Value-initialize the first named member of the union.
905 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
906 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
907 Field != FieldEnd; ++Field) {
908 if (Field->getDeclName()) {
909 StructuredList->setInitializedFieldInUnion(*Field);
910 break;
911 }
912 }
913 return;
914 }
915
Douglas Gregor05c13a32009-01-22 00:58:24 +0000916 // If structDecl is a forward declaration, this loop won't do
917 // anything except look at designated initializers; That's okay,
918 // because an error should get printed out elsewhere. It might be
919 // worthwhile to skip over the rest of the initializer, though.
Douglas Gregor44b43212008-12-11 16:49:14 +0000920 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000921 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregordfb5e592009-02-12 19:00:39 +0000922 bool InitializedSomething = false;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000923 while (Index < IList->getNumInits()) {
924 Expr *Init = IList->getInit(Index);
925
926 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000927 // If we're not the subobject that matches up with the '{' for
928 // the designator, we shouldn't be handling the
929 // designator. Return immediately.
930 if (!SubobjectIsDesignatorContext)
931 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000932
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000933 // Handle this designated initializer. Field will be updated to
934 // the next field that we'll be initializing.
935 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000936 DeclType, &Field, 0, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000937 StructuredList, StructuredIndex,
938 true, TopLevelObject))
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000939 hadError = true;
940
Douglas Gregordfb5e592009-02-12 19:00:39 +0000941 InitializedSomething = true;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000942 continue;
943 }
944
945 if (Field == FieldEnd) {
946 // We've run out of fields. We're done.
947 break;
948 }
949
Douglas Gregordfb5e592009-02-12 19:00:39 +0000950 // We've already initialized a member of a union. We're done.
951 if (InitializedSomething && DeclType->isUnionType())
952 break;
953
Douglas Gregor44b43212008-12-11 16:49:14 +0000954 // If we've hit the flexible array member at the end, we're done.
955 if (Field->getType()->isIncompleteArrayType())
956 break;
957
Douglas Gregor0bb76892009-01-29 16:53:55 +0000958 if (Field->isUnnamedBitfield()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000959 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregor05c13a32009-01-22 00:58:24 +0000960 ++Field;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000961 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +0000962 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000963
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000964 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000965 StructuredList, StructuredIndex);
Douglas Gregordfb5e592009-02-12 19:00:39 +0000966 InitializedSomething = true;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000967
968 if (DeclType->isUnionType()) {
969 // Initialize the first field within the union.
970 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +0000971 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000972
973 ++Field;
Steve Naroff0cca7492008-05-01 22:18:59 +0000974 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000975
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000976 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
977 Index >= IList->getNumInits() ||
978 !isa<InitListExpr>(IList->getInit(Index)))
979 return;
980
981 // Handle GNU flexible array initializers.
982 if (!TopLevelObject &&
983 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000984 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000985 diag::err_flexible_array_init_nonempty)
986 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +0000987 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000988 << *Field;
989 hadError = true;
990 }
991
992 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
993 StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000994}
Steve Naroff0cca7492008-05-01 22:18:59 +0000995
Douglas Gregor05c13a32009-01-22 00:58:24 +0000996/// @brief Check the well-formedness of a C99 designated initializer.
997///
998/// Determines whether the designated initializer @p DIE, which
999/// resides at the given @p Index within the initializer list @p
1000/// IList, is well-formed for a current object of type @p DeclType
1001/// (C99 6.7.8). The actual subobject that this designator refers to
1002/// within the current subobject is returned in either
Douglas Gregor4c678342009-01-28 21:54:33 +00001003/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregor05c13a32009-01-22 00:58:24 +00001004///
1005/// @param IList The initializer list in which this designated
1006/// initializer occurs.
1007///
1008/// @param DIE The designated initializer and its initialization
1009/// expression.
1010///
1011/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1012/// into which the designation in @p DIE should refer.
1013///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001014/// @param NextField If non-NULL and the first designator in @p DIE is
1015/// a field, this will be set to the field declaration corresponding
1016/// to the field named by the designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001017///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001018/// @param NextElementIndex If non-NULL and the first designator in @p
1019/// DIE is an array designator or GNU array-range designator, this
1020/// will be set to the last index initialized by this designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001021///
1022/// @param Index Index into @p IList where the designated initializer
1023/// @p DIE occurs.
1024///
Douglas Gregor4c678342009-01-28 21:54:33 +00001025/// @param StructuredList The initializer list expression that
1026/// describes all of the subobject initializers in the order they'll
1027/// actually be initialized.
1028///
Douglas Gregor05c13a32009-01-22 00:58:24 +00001029/// @returns true if there was an error, false otherwise.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001030bool
1031InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
1032 DesignatedInitExpr *DIE,
Chris Lattner19da8cd2009-02-24 23:01:39 +00001033 DesignatedInitExpr::designators_iterator D,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001034 QualType &CurrentObjectType,
1035 RecordDecl::field_iterator *NextField,
1036 llvm::APSInt *NextElementIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001037 unsigned &Index,
1038 InitListExpr *StructuredList,
Douglas Gregor34e79462009-01-28 23:36:17 +00001039 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001040 bool FinishSubobjectInit,
1041 bool TopLevelObject) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001042 if (D == DIE->designators_end()) {
1043 // Check the actual initialization for the designated object type.
1044 bool prevHadError = hadError;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001045
1046 // Temporarily remove the designator expression from the
1047 // initializer list that the child calls see, so that we don't try
1048 // to re-process the designator.
1049 unsigned OldIndex = Index;
1050 IList->setInit(OldIndex, DIE->getInit());
1051
1052 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001053 StructuredList, StructuredIndex);
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001054
1055 // Restore the designated initializer expression in the syntactic
1056 // form of the initializer list.
1057 if (IList->getInit(OldIndex) != DIE->getInit())
1058 DIE->setInit(IList->getInit(OldIndex));
1059 IList->setInit(OldIndex, DIE);
1060
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001061 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001062 }
1063
Douglas Gregor4c678342009-01-28 21:54:33 +00001064 bool IsFirstDesignator = (D == DIE->designators_begin());
1065 assert((IsFirstDesignator || StructuredList) &&
1066 "Need a non-designated initializer list to start from");
1067
1068 // Determine the structural initializer list that corresponds to the
1069 // current subobject.
1070 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
1071 : getStructuredSubobjectInit(IList, Index, CurrentObjectType, StructuredList,
1072 StructuredIndex,
1073 SourceRange(D->getStartLocation(),
1074 DIE->getSourceRange().getEnd()));
1075 assert(StructuredList && "Expected a structured initializer list");
1076
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001077 if (D->isFieldDesignator()) {
1078 // C99 6.7.8p7:
1079 //
1080 // If a designator has the form
1081 //
1082 // . identifier
1083 //
1084 // then the current object (defined below) shall have
1085 // structure or union type and the identifier shall be the
1086 // name of a member of that type.
1087 const RecordType *RT = CurrentObjectType->getAsRecordType();
1088 if (!RT) {
1089 SourceLocation Loc = D->getDotLoc();
1090 if (Loc.isInvalid())
1091 Loc = D->getFieldLoc();
Chris Lattner08202542009-02-24 22:50:46 +00001092 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1093 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001094 ++Index;
1095 return true;
1096 }
1097
Douglas Gregor4c678342009-01-28 21:54:33 +00001098 // Note: we perform a linear search of the fields here, despite
1099 // the fact that we have a faster lookup method, because we always
1100 // need to compute the field's index.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001101 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor4c678342009-01-28 21:54:33 +00001102 unsigned FieldIndex = 0;
1103 RecordDecl::field_iterator Field = RT->getDecl()->field_begin(),
1104 FieldEnd = RT->getDecl()->field_end();
1105 for (; Field != FieldEnd; ++Field) {
1106 if (Field->isUnnamedBitfield())
1107 continue;
1108
1109 if (Field->getIdentifier() == FieldName)
1110 break;
1111
1112 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001113 }
1114
Douglas Gregor4c678342009-01-28 21:54:33 +00001115 if (Field == FieldEnd) {
1116 // We did not find the field we're looking for. Produce a
1117 // suitable diagnostic and return a failure.
1118 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1119 if (Lookup.first == Lookup.second) {
1120 // Name lookup didn't find anything.
Chris Lattner08202542009-02-24 22:50:46 +00001121 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregor4c678342009-01-28 21:54:33 +00001122 << FieldName << CurrentObjectType;
1123 } else {
1124 // Name lookup found something, but it wasn't a field.
Chris Lattner08202542009-02-24 22:50:46 +00001125 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor4c678342009-01-28 21:54:33 +00001126 << FieldName;
Chris Lattner08202542009-02-24 22:50:46 +00001127 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001128 diag::note_field_designator_found);
1129 }
1130
1131 ++Index;
1132 return true;
1133 } else if (cast<RecordDecl>((*Field)->getDeclContext())
1134 ->isAnonymousStructOrUnion()) {
Chris Lattner08202542009-02-24 22:50:46 +00001135 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_anon_class)
Douglas Gregor4c678342009-01-28 21:54:33 +00001136 << FieldName
1137 << (cast<RecordDecl>((*Field)->getDeclContext())->isUnion()? 2 :
Chris Lattner08202542009-02-24 22:50:46 +00001138 (int)SemaRef.getLangOptions().CPlusPlus);
1139 SemaRef.Diag((*Field)->getLocation(), diag::note_field_designator_found);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001140 ++Index;
1141 return true;
1142 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001143
1144 // All of the fields of a union are located at the same place in
1145 // the initializer list.
Douglas Gregor0bb76892009-01-29 16:53:55 +00001146 if (RT->getDecl()->isUnion()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001147 FieldIndex = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001148 StructuredList->setInitializedFieldInUnion(*Field);
1149 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001150
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001151 // Update the designator with the field declaration.
Douglas Gregor4c678342009-01-28 21:54:33 +00001152 D->setField(*Field);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001153
Douglas Gregor4c678342009-01-28 21:54:33 +00001154 // Make sure that our non-designated initializer list has space
1155 // for a subobject corresponding to this field.
1156 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001157 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001158
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001159 // This designator names a flexible array member.
1160 if (Field->getType()->isIncompleteArrayType()) {
1161 bool Invalid = false;
1162 DesignatedInitExpr::designators_iterator NextD = D;
1163 ++NextD;
1164 if (NextD != DIE->designators_end()) {
1165 // We can't designate an object within the flexible array
1166 // member (because GCC doesn't allow it).
Chris Lattner08202542009-02-24 22:50:46 +00001167 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001168 diag::err_designator_into_flexible_array_member)
1169 << SourceRange(NextD->getStartLocation(),
1170 DIE->getSourceRange().getEnd());
Chris Lattner08202542009-02-24 22:50:46 +00001171 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001172 << *Field;
1173 Invalid = true;
1174 }
1175
1176 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1177 // The initializer is not an initializer list.
Chris Lattner08202542009-02-24 22:50:46 +00001178 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001179 diag::err_flexible_array_init_needs_braces)
1180 << DIE->getInit()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001181 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001182 << *Field;
1183 Invalid = true;
1184 }
1185
1186 // Handle GNU flexible array initializers.
1187 if (!Invalid && !TopLevelObject &&
1188 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Chris Lattner08202542009-02-24 22:50:46 +00001189 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001190 diag::err_flexible_array_init_nonempty)
1191 << DIE->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001192 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001193 << *Field;
1194 Invalid = true;
1195 }
1196
1197 if (Invalid) {
1198 ++Index;
1199 return true;
1200 }
1201
1202 // Initialize the array.
1203 bool prevHadError = hadError;
1204 unsigned newStructuredIndex = FieldIndex;
1205 unsigned OldIndex = Index;
1206 IList->setInit(Index, DIE->getInit());
1207 CheckSubElementType(IList, Field->getType(), Index,
1208 StructuredList, newStructuredIndex);
1209 IList->setInit(OldIndex, DIE);
1210 if (hadError && !prevHadError) {
1211 ++Field;
1212 ++FieldIndex;
1213 if (NextField)
1214 *NextField = Field;
1215 StructuredIndex = FieldIndex;
1216 return true;
1217 }
1218 } else {
1219 // Recurse to check later designated subobjects.
1220 QualType FieldType = (*Field)->getType();
1221 unsigned newStructuredIndex = FieldIndex;
1222 if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index,
1223 StructuredList, newStructuredIndex,
1224 true, false))
1225 return true;
1226 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001227
1228 // Find the position of the next field to be initialized in this
1229 // subobject.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001230 ++Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001231 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001232
1233 // If this the first designator, our caller will continue checking
1234 // the rest of this struct/class/union subobject.
1235 if (IsFirstDesignator) {
1236 if (NextField)
1237 *NextField = Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001238 StructuredIndex = FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001239 return false;
1240 }
1241
Douglas Gregor34e79462009-01-28 23:36:17 +00001242 if (!FinishSubobjectInit)
1243 return false;
1244
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001245 // Check the remaining fields within this class/struct/union subobject.
1246 bool prevHadError = hadError;
Douglas Gregor4c678342009-01-28 21:54:33 +00001247 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1248 StructuredList, FieldIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001249 return hadError && !prevHadError;
1250 }
1251
1252 // C99 6.7.8p6:
1253 //
1254 // If a designator has the form
1255 //
1256 // [ constant-expression ]
1257 //
1258 // then the current object (defined below) shall have array
1259 // type and the expression shall be an integer constant
1260 // expression. If the array is of unknown size, any
1261 // nonnegative value is valid.
1262 //
1263 // Additionally, cope with the GNU extension that permits
1264 // designators of the form
1265 //
1266 // [ constant-expression ... constant-expression ]
Chris Lattner08202542009-02-24 22:50:46 +00001267 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001268 if (!AT) {
Chris Lattner08202542009-02-24 22:50:46 +00001269 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001270 << CurrentObjectType;
1271 ++Index;
1272 return true;
1273 }
1274
1275 Expr *IndexExpr = 0;
Douglas Gregor34e79462009-01-28 23:36:17 +00001276 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1277 if (D->isArrayDesignator()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001278 IndexExpr = DIE->getArrayIndex(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001279
1280 bool ConstExpr
Chris Lattner08202542009-02-24 22:50:46 +00001281 = IndexExpr->isIntegerConstantExpr(DesignatedStartIndex, SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001282 assert(ConstExpr && "Expression must be constant"); (void)ConstExpr;
1283
1284 DesignatedEndIndex = DesignatedStartIndex;
1285 } else {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001286 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor34e79462009-01-28 23:36:17 +00001287
1288 bool StartConstExpr
1289 = DIE->getArrayRangeStart(*D)->isIntegerConstantExpr(DesignatedStartIndex,
Chris Lattner08202542009-02-24 22:50:46 +00001290 SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001291 assert(StartConstExpr && "Expression must be constant"); (void)StartConstExpr;
1292
1293 bool EndConstExpr
1294 = DIE->getArrayRangeEnd(*D)->isIntegerConstantExpr(DesignatedEndIndex,
Chris Lattner08202542009-02-24 22:50:46 +00001295 SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001296 assert(EndConstExpr && "Expression must be constant"); (void)EndConstExpr;
1297
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001298 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001299
1300 if (DesignatedStartIndex.getZExtValue() != DesignatedEndIndex.getZExtValue())
Douglas Gregora9c87802009-01-29 19:42:23 +00001301 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001302 }
1303
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001304 if (isa<ConstantArrayType>(AT)) {
1305 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor34e79462009-01-28 23:36:17 +00001306 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1307 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1308 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1309 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1310 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner08202542009-02-24 22:50:46 +00001311 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001312 diag::err_array_designator_too_large)
Douglas Gregor34e79462009-01-28 23:36:17 +00001313 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001314 << IndexExpr->getSourceRange();
1315 ++Index;
1316 return true;
1317 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001318 } else {
1319 // Make sure the bit-widths and signedness match.
1320 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1321 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
1322 else if (DesignatedStartIndex.getBitWidth() < DesignatedEndIndex.getBitWidth())
1323 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1324 DesignatedStartIndex.setIsUnsigned(true);
1325 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001326 }
1327
Douglas Gregor4c678342009-01-28 21:54:33 +00001328 // Make sure that our non-designated initializer list has space
1329 // for a subobject corresponding to this array element.
Douglas Gregor34e79462009-01-28 23:36:17 +00001330 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001331 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor34e79462009-01-28 23:36:17 +00001332 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001333
Douglas Gregor34e79462009-01-28 23:36:17 +00001334 // Repeatedly perform subobject initializations in the range
1335 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001336
Douglas Gregor34e79462009-01-28 23:36:17 +00001337 // Move to the next designator
1338 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1339 unsigned OldIndex = Index;
1340 ++D;
1341 while (DesignatedStartIndex <= DesignatedEndIndex) {
1342 // Recurse to check later designated subobjects.
1343 QualType ElementType = AT->getElementType();
1344 Index = OldIndex;
1345 if (CheckDesignatedInitializer(IList, DIE, D, ElementType, 0, 0, Index,
1346 StructuredList, ElementIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001347 (DesignatedStartIndex == DesignatedEndIndex),
1348 false))
Douglas Gregor34e79462009-01-28 23:36:17 +00001349 return true;
1350
1351 // Move to the next index in the array that we'll be initializing.
1352 ++DesignatedStartIndex;
1353 ElementIndex = DesignatedStartIndex.getZExtValue();
1354 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001355
1356 // If this the first designator, our caller will continue checking
1357 // the rest of this array subobject.
1358 if (IsFirstDesignator) {
1359 if (NextElementIndex)
Douglas Gregor34e79462009-01-28 23:36:17 +00001360 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor4c678342009-01-28 21:54:33 +00001361 StructuredIndex = ElementIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001362 return false;
1363 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001364
1365 if (!FinishSubobjectInit)
1366 return false;
1367
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001368 // Check the remaining elements within this array subobject.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001369 bool prevHadError = hadError;
Douglas Gregorfdf55692009-02-09 19:45:19 +00001370 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001371 StructuredList, ElementIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001372 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001373}
1374
Douglas Gregor4c678342009-01-28 21:54:33 +00001375// Get the structured initializer list for a subobject of type
1376// @p CurrentObjectType.
1377InitListExpr *
1378InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1379 QualType CurrentObjectType,
1380 InitListExpr *StructuredList,
1381 unsigned StructuredIndex,
1382 SourceRange InitRange) {
1383 Expr *ExistingInit = 0;
1384 if (!StructuredList)
1385 ExistingInit = SyntacticToSemantic[IList];
1386 else if (StructuredIndex < StructuredList->getNumInits())
1387 ExistingInit = StructuredList->getInit(StructuredIndex);
1388
1389 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1390 return Result;
1391
1392 if (ExistingInit) {
1393 // We are creating an initializer list that initializes the
1394 // subobjects of the current object, but there was already an
1395 // initialization that completely initialized the current
1396 // subobject, e.g., by a compound literal:
1397 //
1398 // struct X { int a, b; };
1399 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
1400 //
1401 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1402 // designated initializer re-initializes the whole
1403 // subobject [0], overwriting previous initializers.
Chris Lattner08202542009-02-24 22:50:46 +00001404 SemaRef.Diag(InitRange.getBegin(), diag::warn_subobject_initializer_overrides)
Douglas Gregor4c678342009-01-28 21:54:33 +00001405 << InitRange;
Chris Lattner08202542009-02-24 22:50:46 +00001406 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001407 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001408 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001409 << ExistingInit->getSourceRange();
1410 }
1411
Douglas Gregor87fd7032009-02-02 17:43:21 +00001412 SourceLocation StartLoc;
1413 if (Index < IList->getNumInits())
1414 StartLoc = IList->getInit(Index)->getSourceRange().getBegin();
Douglas Gregor4c678342009-01-28 21:54:33 +00001415 InitListExpr *Result
Chris Lattner08202542009-02-24 22:50:46 +00001416 = new (SemaRef.Context) InitListExpr(StartLoc, 0, 0,
Douglas Gregor87fd7032009-02-02 17:43:21 +00001417 IList->getSourceRange().getEnd());
Douglas Gregor4c678342009-01-28 21:54:33 +00001418 Result->setType(CurrentObjectType);
1419
1420 // Link this new initializer list into the structured initializer
1421 // lists.
1422 if (StructuredList)
1423 StructuredList->updateInit(StructuredIndex, Result);
1424 else {
1425 Result->setSyntacticForm(IList);
1426 SyntacticToSemantic[IList] = Result;
1427 }
1428
1429 return Result;
1430}
1431
1432/// Update the initializer at index @p StructuredIndex within the
1433/// structured initializer list to the value @p expr.
1434void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1435 unsigned &StructuredIndex,
1436 Expr *expr) {
1437 // No structured initializer list to update
1438 if (!StructuredList)
1439 return;
1440
1441 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1442 // This initializer overwrites a previous initializer. Warn.
Chris Lattner08202542009-02-24 22:50:46 +00001443 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001444 diag::warn_initializer_overrides)
1445 << expr->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001446 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001447 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001448 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001449 << PrevInit->getSourceRange();
1450 }
1451
1452 ++StructuredIndex;
1453}
1454
Douglas Gregor05c13a32009-01-22 00:58:24 +00001455/// Check that the given Index expression is a valid array designator
1456/// value. This is essentailly just a wrapper around
1457/// Expr::isIntegerConstantExpr that also checks for negative values
1458/// and produces a reasonable diagnostic if there is a
1459/// failure. Returns true if there was an error, false otherwise. If
1460/// everything went okay, Value will receive the value of the constant
1461/// expression.
1462static bool
1463CheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) {
1464 SourceLocation Loc = Index->getSourceRange().getBegin();
1465
1466 // Make sure this is an integer constant expression.
1467 if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc))
1468 return Self.Diag(Loc, diag::err_array_designator_nonconstant)
1469 << Index->getSourceRange();
1470
1471 // Make sure this constant expression is non-negative.
Douglas Gregore3fa2de2009-01-23 18:58:42 +00001472 llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()),
1473 Value.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001474 if (Value < Zero)
1475 return Self.Diag(Loc, diag::err_array_designator_negative)
1476 << Value.toString(10) << Index->getSourceRange();
1477
Douglas Gregor53d3d8e2009-01-23 21:04:18 +00001478 Value.setIsUnsigned(true);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001479 return false;
1480}
1481
1482Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1483 SourceLocation Loc,
1484 bool UsedColonSyntax,
1485 OwningExprResult Init) {
1486 typedef DesignatedInitExpr::Designator ASTDesignator;
1487
1488 bool Invalid = false;
1489 llvm::SmallVector<ASTDesignator, 32> Designators;
1490 llvm::SmallVector<Expr *, 32> InitExpressions;
1491
1492 // Build designators and check array designator expressions.
1493 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1494 const Designator &D = Desig.getDesignator(Idx);
1495 switch (D.getKind()) {
1496 case Designator::FieldDesignator:
1497 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
1498 D.getFieldLoc()));
1499 break;
1500
1501 case Designator::ArrayDesignator: {
1502 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1503 llvm::APSInt IndexValue;
1504 if (CheckArrayDesignatorExpr(*this, Index, IndexValue))
1505 Invalid = true;
1506 else {
1507 Designators.push_back(ASTDesignator(InitExpressions.size(),
1508 D.getLBracketLoc(),
1509 D.getRBracketLoc()));
1510 InitExpressions.push_back(Index);
1511 }
1512 break;
1513 }
1514
1515 case Designator::ArrayRangeDesignator: {
1516 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1517 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1518 llvm::APSInt StartValue;
1519 llvm::APSInt EndValue;
1520 if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) ||
1521 CheckArrayDesignatorExpr(*this, EndIndex, EndValue))
1522 Invalid = true;
Douglas Gregord6f584f2009-01-23 22:22:29 +00001523 else {
1524 // Make sure we're comparing values with the same bit width.
1525 if (StartValue.getBitWidth() > EndValue.getBitWidth())
1526 EndValue.extend(StartValue.getBitWidth());
1527 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1528 StartValue.extend(EndValue.getBitWidth());
1529
1530 if (EndValue < StartValue) {
1531 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1532 << StartValue.toString(10) << EndValue.toString(10)
1533 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1534 Invalid = true;
1535 } else {
1536 Designators.push_back(ASTDesignator(InitExpressions.size(),
1537 D.getLBracketLoc(),
1538 D.getEllipsisLoc(),
1539 D.getRBracketLoc()));
1540 InitExpressions.push_back(StartIndex);
1541 InitExpressions.push_back(EndIndex);
1542 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001543 }
1544 break;
1545 }
1546 }
1547 }
1548
1549 if (Invalid || Init.isInvalid())
1550 return ExprError();
1551
1552 // Clear out the expressions within the designation.
1553 Desig.ClearExprs(*this);
1554
1555 DesignatedInitExpr *DIE
1556 = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(),
1557 &InitExpressions[0], InitExpressions.size(),
1558 Loc, UsedColonSyntax,
1559 static_cast<Expr *>(Init.release()));
1560 return Owned(DIE);
1561}
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001562
1563bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattner08202542009-02-24 22:50:46 +00001564 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001565 if (!CheckInitList.HadError())
1566 InitList = CheckInitList.getFullyStructuredList();
1567
1568 return CheckInitList.HadError();
1569}
Douglas Gregor87fd7032009-02-02 17:43:21 +00001570
1571/// \brief Diagnose any semantic errors with value-initialization of
1572/// the given type.
1573///
1574/// Value-initialization effectively zero-initializes any types
1575/// without user-declared constructors, and calls the default
1576/// constructor for a for any type that has a user-declared
1577/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1578/// a type with a user-declared constructor does not have an
1579/// accessible, non-deleted default constructor. In C, everything can
1580/// be value-initialized, which corresponds to C's notion of
1581/// initializing objects with static storage duration when no
1582/// initializer is provided for that object.
1583///
1584/// \returns true if there was an error, false otherwise.
1585bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1586 // C++ [dcl.init]p5:
1587 //
1588 // To value-initialize an object of type T means:
1589
1590 // -- if T is an array type, then each element is value-initialized;
1591 if (const ArrayType *AT = Context.getAsArrayType(Type))
1592 return CheckValueInitialization(AT->getElementType(), Loc);
1593
1594 if (const RecordType *RT = Type->getAsRecordType()) {
1595 if (const CXXRecordType *CXXRec = dyn_cast<CXXRecordType>(RT)) {
1596 // -- if T is a class type (clause 9) with a user-declared
1597 // constructor (12.1), then the default constructor for T is
1598 // called (and the initialization is ill-formed if T has no
1599 // accessible default constructor);
1600 if (CXXRec->getDecl()->hasUserDeclaredConstructor())
1601 // FIXME: Eventually, we'll need to put the constructor decl
1602 // into the AST.
1603 return PerformInitializationByConstructor(Type, 0, 0, Loc,
1604 SourceRange(Loc),
1605 DeclarationName(),
1606 IK_Direct);
1607 }
1608 }
1609
1610 if (Type->isReferenceType()) {
1611 // C++ [dcl.init]p5:
1612 // [...] A program that calls for default-initialization or
1613 // value-initialization of an entity of reference type is
1614 // ill-formed. [...]
Douglas Gregord8635172009-02-02 21:35:47 +00001615 // FIXME: Once we have code that goes through this path, add an
1616 // actual diagnostic :)
Douglas Gregor87fd7032009-02-02 17:43:21 +00001617 }
1618
1619 return false;
1620}