blob: d70ae6c153ea9d688d3635004d4c779cf40e5911 [file] [log] [blame]
Steve Naroffc4d4a482008-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 Lattnerd3a00502009-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 Lattnere76e9bf2009-02-24 22:48:58 +000014// This file also implements Sema::CheckInitializerTypes.
Steve Naroffc4d4a482008-05-01 22:18:59 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "Sema.h"
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000019#include "clang/Parse/Designator.h"
Steve Naroffc4d4a482008-05-01 22:18:59 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
Douglas Gregor849afc32009-01-29 00:45:39 +000022#include <map>
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000023using namespace clang;
Steve Naroffc4d4a482008-05-01 22:18:59 +000024
Chris Lattnerd3a00502009-02-24 22:27:37 +000025//===----------------------------------------------------------------------===//
26// Sema Initialization Checking
27//===----------------------------------------------------------------------===//
28
Chris Lattner89b91072009-02-24 22:36:59 +000029static StringLiteral *IsStringInit(Expr *Init, QualType DeclType,
30 ASTContext &Context) {
Chris Lattnerd3a00502009-02-24 22:27:37 +000031 if (const ArrayType *AT = Context.getAsArrayType(DeclType))
32 if (AT->getElementType()->isCharType())
33 return dyn_cast<StringLiteral>(Init->IgnoreParens());
34 return 0;
35}
36
Chris Lattner160da072009-02-24 22:46:58 +000037static bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
38 bool DirectInit, Sema &S) {
Chris Lattnerd3a00502009-02-24 22:27:37 +000039 // Get the type before calling CheckSingleAssignmentConstraints(), since
40 // it can promote the expression.
41 QualType InitType = Init->getType();
42
Chris Lattner160da072009-02-24 22:46:58 +000043 if (S.getLangOptions().CPlusPlus) {
Chris Lattnerd3a00502009-02-24 22:27:37 +000044 // FIXME: I dislike this error message. A lot.
Chris Lattner160da072009-02-24 22:46:58 +000045 if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit))
46 return S.Diag(Init->getSourceRange().getBegin(),
47 diag::err_typecheck_convert_incompatible)
48 << DeclType << Init->getType() << "initializing"
49 << Init->getSourceRange();
Chris Lattnerd3a00502009-02-24 22:27:37 +000050 return false;
51 }
52
Chris Lattner160da072009-02-24 22:46:58 +000053 Sema::AssignConvertType ConvTy =
54 S.CheckSingleAssignmentConstraints(DeclType, Init);
55 return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
Chris Lattnerd3a00502009-02-24 22:27:37 +000056 InitType, Init, "initializing");
57}
58
Chris Lattnerd20fac42009-02-24 23:01:39 +000059static void CheckStringInit(Expr *Str, unsigned StrLength,
60 QualType &DeclT, Sema &S) {
Chris Lattner45d6fd62009-02-24 22:41:04 +000061 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattnerd3a00502009-02-24 22:27:37 +000062
63 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
64 // C99 6.7.8p14. We have an array of character type with unknown size
65 // being initialized to a string literal.
66 llvm::APSInt ConstVal(32);
Chris Lattnerd20fac42009-02-24 23:01:39 +000067 ConstVal = StrLength;
Chris Lattnerd3a00502009-02-24 22:27:37 +000068 // Return a new array type (C99 6.7.8p22).
Chris Lattner45d6fd62009-02-24 22:41:04 +000069 DeclT = S.Context.getConstantArrayType(IAT->getElementType(), ConstVal,
70 ArrayType::Normal, 0);
Chris Lattnerd20fac42009-02-24 23:01:39 +000071 return;
Chris Lattnerd3a00502009-02-24 22:27:37 +000072 }
Chris Lattnerd20fac42009-02-24 23:01:39 +000073
74 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
75
76 // C99 6.7.8p14. We have an array of character type with known size. However,
77 // the size may be smaller or larger than the string we are initializing.
78 // FIXME: Avoid truncation for 64-bit length strings.
79 if (StrLength-1 > (unsigned)CAT->getSize().getZExtValue())
80 S.Diag(Str->getSourceRange().getBegin(),
81 diag::warn_initializer_string_for_char_array_too_long)
82 << Str->getSourceRange();
83
84 // Set the type to the actual size that we are initializing. If we have
85 // something like:
86 // char x[1] = "foo";
87 // then this will set the string literal's type to char[1].
Chris Lattner45d6fd62009-02-24 22:41:04 +000088 Str->setType(DeclT);
Chris Lattnerd3a00502009-02-24 22:27:37 +000089}
90
91bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
92 SourceLocation InitLoc,
93 DeclarationName InitEntity,
94 bool DirectInit) {
95 if (DeclType->isDependentType() || Init->isTypeDependent())
96 return false;
97
98 // C++ [dcl.init.ref]p1:
99 // A variable declared to be a T&, that is "reference to type T"
100 // (8.3.2), shall be initialized by an object, or function, of
101 // type T or by an object that can be converted into a T.
102 if (DeclType->isReferenceType())
103 return CheckReferenceInit(Init, DeclType, 0, false, DirectInit);
104
105 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
106 // of unknown size ("[]") or an object type that is not a variable array type.
107 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
108 return Diag(InitLoc, diag::err_variable_object_no_init)
109 << VAT->getSizeExpr()->getSourceRange();
110
111 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
112 if (!InitList) {
113 // FIXME: Handle wide strings
Chris Lattnerd20fac42009-02-24 23:01:39 +0000114 if (StringLiteral *Str = IsStringInit(Init, DeclType, Context)) {
115 CheckStringInit(Str, Str->getByteLength() + 1, DeclType, *this);
116 return false;
117 }
Chris Lattnerd3a00502009-02-24 22:27:37 +0000118
119 // C++ [dcl.init]p14:
120 // -- If the destination type is a (possibly cv-qualified) class
121 // type:
122 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
123 QualType DeclTypeC = Context.getCanonicalType(DeclType);
124 QualType InitTypeC = Context.getCanonicalType(Init->getType());
125
126 // -- If the initialization is direct-initialization, or if it is
127 // copy-initialization where the cv-unqualified version of the
128 // source type is the same class as, or a derived class of, the
129 // class of the destination, constructors are considered.
130 if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
131 IsDerivedFrom(InitTypeC, DeclTypeC)) {
132 CXXConstructorDecl *Constructor
133 = PerformInitializationByConstructor(DeclType, &Init, 1,
134 InitLoc, Init->getSourceRange(),
135 InitEntity,
136 DirectInit? IK_Direct : IK_Copy);
137 return Constructor == 0;
138 }
139
140 // -- Otherwise (i.e., for the remaining copy-initialization
141 // cases), user-defined conversion sequences that can
142 // convert from the source type to the destination type or
143 // (when a conversion function is used) to a derived class
144 // thereof are enumerated as described in 13.3.1.4, and the
145 // best one is chosen through overload resolution
146 // (13.3). If the conversion cannot be done or is
147 // ambiguous, the initialization is ill-formed. The
148 // function selected is called with the initializer
149 // expression as its argument; if the function is a
150 // constructor, the call initializes a temporary of the
151 // destination type.
152 // FIXME: We're pretending to do copy elision here; return to
153 // this when we have ASTs for such things.
154 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
155 return false;
156
157 if (InitEntity)
158 return Diag(InitLoc, diag::err_cannot_initialize_decl)
159 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
160 << Init->getType() << Init->getSourceRange();
161 else
162 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
163 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
164 << Init->getType() << Init->getSourceRange();
165 }
166
167 // C99 6.7.8p16.
168 if (DeclType->isArrayType())
169 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
170 << Init->getSourceRange();
171
Chris Lattner160da072009-02-24 22:46:58 +0000172 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Chris Lattnerd3a00502009-02-24 22:27:37 +0000173 }
174
175 bool hadError = CheckInitList(InitList, DeclType);
176 Init = InitList;
177 return hadError;
178}
179
180//===----------------------------------------------------------------------===//
181// Semantic checking for initializer lists.
182//===----------------------------------------------------------------------===//
183
Douglas Gregoraaa20962009-01-29 01:05:33 +0000184/// @brief Semantic checking for initializer lists.
185///
186/// The InitListChecker class contains a set of routines that each
187/// handle the initialization of a certain kind of entity, e.g.,
188/// arrays, vectors, struct/union types, scalars, etc. The
189/// InitListChecker itself performs a recursive walk of the subobject
190/// structure of the type to be initialized, while stepping through
191/// the initializer list one element at a time. The IList and Index
192/// parameters to each of the Check* routines contain the active
193/// (syntactic) initializer list and the index into that initializer
194/// list that represents the current initializer. Each routine is
195/// responsible for moving that Index forward as it consumes elements.
196///
197/// Each Check* routine also has a StructuredList/StructuredIndex
198/// arguments, which contains the current the "structured" (semantic)
199/// initializer list and the index into that initializer list where we
200/// are copying initializers as we map them over to the semantic
201/// list. Once we have completed our recursive walk of the subobject
202/// structure, we will have constructed a full semantic initializer
203/// list.
204///
205/// C99 designators cause changes in the initializer list traversal,
206/// because they make the initialization "jump" into a specific
207/// subobject and then continue the initialization from that
208/// point. CheckDesignatedInitializer() recursively steps into the
209/// designated subobject and manages backing out the recursion to
210/// initialize the subobjects after the one designated.
Chris Lattnere76e9bf2009-02-24 22:48:58 +0000211namespace {
Douglas Gregor849afc32009-01-29 00:45:39 +0000212class InitListChecker {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000213 Sema &SemaRef;
Douglas Gregor849afc32009-01-29 00:45:39 +0000214 bool hadError;
215 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
216 InitListExpr *FullyStructuredList;
217
218 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000219 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000220 unsigned &StructuredIndex,
221 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000222 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000223 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000224 unsigned &StructuredIndex,
225 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000226 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
227 bool SubobjectIsDesignatorContext,
228 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000229 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000230 unsigned &StructuredIndex,
231 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000232 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
233 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000234 InitListExpr *StructuredList,
235 unsigned &StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000236 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor849afc32009-01-29 00:45:39 +0000237 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000238 InitListExpr *StructuredList,
239 unsigned &StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000240 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
241 unsigned &Index,
242 InitListExpr *StructuredList,
243 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +0000244 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000245 InitListExpr *StructuredList,
246 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +0000247 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
248 RecordDecl::field_iterator Field,
249 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000250 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000251 unsigned &StructuredIndex,
252 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000253 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
254 llvm::APSInt elementIndex,
255 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000256 InitListExpr *StructuredList,
257 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +0000258 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
259 DesignatedInitExpr::designators_iterator D,
260 QualType &CurrentObjectType,
261 RecordDecl::field_iterator *NextField,
262 llvm::APSInt *NextElementIndex,
263 unsigned &Index,
264 InitListExpr *StructuredList,
265 unsigned &StructuredIndex,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000266 bool FinishSubobjectInit,
267 bool TopLevelObject);
Douglas Gregor849afc32009-01-29 00:45:39 +0000268 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
269 QualType CurrentObjectType,
270 InitListExpr *StructuredList,
271 unsigned StructuredIndex,
272 SourceRange InitRange);
Douglas Gregoraaa20962009-01-29 01:05:33 +0000273 void UpdateStructuredListElement(InitListExpr *StructuredList,
274 unsigned &StructuredIndex,
Douglas Gregor849afc32009-01-29 00:45:39 +0000275 Expr *expr);
276 int numArrayElements(QualType DeclType);
277 int numStructUnionElements(QualType DeclType);
Douglas Gregord45210d2009-01-30 22:09:00 +0000278
279 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregor849afc32009-01-29 00:45:39 +0000280public:
Chris Lattner2e2766a2009-02-24 22:50:46 +0000281 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregor849afc32009-01-29 00:45:39 +0000282 bool HadError() { return hadError; }
283
284 // @brief Retrieves the fully-structured initializer list used for
285 // semantic analysis and code generation.
286 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
287};
Chris Lattnere76e9bf2009-02-24 22:48:58 +0000288} // end anonymous namespace
Chris Lattner1aa25a72009-01-29 05:10:57 +0000289
Douglas Gregorf603b472009-01-28 21:54:33 +0000290/// Recursively replaces NULL values within the given initializer list
291/// with expressions that perform value-initialization of the
292/// appropriate type.
Douglas Gregord45210d2009-01-30 22:09:00 +0000293void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000294 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregord45210d2009-01-30 22:09:00 +0000295 "Should not have void type");
Douglas Gregor538a4c22009-02-02 17:43:21 +0000296 SourceLocation Loc = ILE->getSourceRange().getBegin();
297 if (ILE->getSyntacticForm())
298 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
299
Douglas Gregorf603b472009-01-28 21:54:33 +0000300 if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
301 unsigned Init = 0, NumInits = ILE->getNumInits();
302 for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(),
303 FieldEnd = RType->getDecl()->field_end();
304 Field != FieldEnd; ++Field) {
305 if (Field->isUnnamedBitfield())
306 continue;
307
Douglas Gregor538a4c22009-02-02 17:43:21 +0000308 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregord45210d2009-01-30 22:09:00 +0000309 if (Field->getType()->isReferenceType()) {
310 // C++ [dcl.init.aggr]p9:
311 // If an incomplete or empty initializer-list leaves a
312 // member of reference type uninitialized, the program is
313 // ill-formed.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000314 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregord45210d2009-01-30 22:09:00 +0000315 << Field->getType()
316 << ILE->getSyntacticForm()->getSourceRange();
Chris Lattner2e2766a2009-02-24 22:50:46 +0000317 SemaRef.Diag(Field->getLocation(),
Douglas Gregord45210d2009-01-30 22:09:00 +0000318 diag::note_uninit_reference_member);
319 hadError = true;
Douglas Gregor538a4c22009-02-02 17:43:21 +0000320 return;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000321 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregor538a4c22009-02-02 17:43:21 +0000322 hadError = true;
323 return;
Douglas Gregord45210d2009-01-30 22:09:00 +0000324 }
Douglas Gregor538a4c22009-02-02 17:43:21 +0000325
326 // FIXME: If value-initialization involves calling a
327 // constructor, should we make that call explicit in the
328 // representation (even when it means extending the
329 // initializer list)?
330 if (Init < NumInits && !hadError)
331 ILE->setInit(Init,
Chris Lattner2e2766a2009-02-24 22:50:46 +0000332 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Douglas Gregor538a4c22009-02-02 17:43:21 +0000333 } else if (InitListExpr *InnerILE
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000334 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord45210d2009-01-30 22:09:00 +0000335 FillInValueInitializations(InnerILE);
Douglas Gregorf603b472009-01-28 21:54:33 +0000336 ++Init;
Douglas Gregord45210d2009-01-30 22:09:00 +0000337
338 // Only look at the first initialization of a union.
339 if (RType->getDecl()->isUnion())
340 break;
Douglas Gregorf603b472009-01-28 21:54:33 +0000341 }
342
343 return;
344 }
345
346 QualType ElementType;
347
Douglas Gregor538a4c22009-02-02 17:43:21 +0000348 unsigned NumInits = ILE->getNumInits();
349 unsigned NumElements = NumInits;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000350 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000351 ElementType = AType->getElementType();
Douglas Gregor538a4c22009-02-02 17:43:21 +0000352 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
353 NumElements = CAType->getSize().getZExtValue();
354 } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000355 ElementType = VType->getElementType();
Douglas Gregor538a4c22009-02-02 17:43:21 +0000356 NumElements = VType->getNumElements();
357 } else
Douglas Gregorf603b472009-01-28 21:54:33 +0000358 ElementType = ILE->getType();
359
Douglas Gregor538a4c22009-02-02 17:43:21 +0000360 for (unsigned Init = 0; Init != NumElements; ++Init) {
361 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000362 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregor538a4c22009-02-02 17:43:21 +0000363 hadError = true;
364 return;
365 }
366
367 // FIXME: If value-initialization involves calling a
368 // constructor, should we make that call explicit in the
369 // representation (even when it means extending the
370 // initializer list)?
371 if (Init < NumInits && !hadError)
372 ILE->setInit(Init,
Chris Lattner2e2766a2009-02-24 22:50:46 +0000373 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Douglas Gregor538a4c22009-02-02 17:43:21 +0000374 }
Chris Lattner1aa25a72009-01-29 05:10:57 +0000375 else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord45210d2009-01-30 22:09:00 +0000376 FillInValueInitializations(InnerILE);
Douglas Gregorf603b472009-01-28 21:54:33 +0000377 }
378}
379
Chris Lattner1aa25a72009-01-29 05:10:57 +0000380
Chris Lattner2e2766a2009-02-24 22:50:46 +0000381InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
382 : SemaRef(S) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000383 hadError = false;
Eli Friedmand8535af2008-05-19 20:00:43 +0000384
Eli Friedman683cedf2008-05-19 19:16:24 +0000385 unsigned newIndex = 0;
Douglas Gregorf603b472009-01-28 21:54:33 +0000386 unsigned newStructuredIndex = 0;
387 FullyStructuredList
388 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, SourceRange());
Douglas Gregorbe69b162009-02-04 22:46:25 +0000389 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
390 /*TopLevelObject=*/true);
Eli Friedmand8535af2008-05-19 20:00:43 +0000391
Douglas Gregord45210d2009-01-30 22:09:00 +0000392 if (!hadError)
393 FillInValueInitializations(FullyStructuredList);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000394}
395
396int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman46f81662008-05-25 13:22:35 +0000397 // FIXME: use a proper constant
398 int maxElements = 0x7FFFFFFF;
Chris Lattnera1923f62008-08-04 07:31:14 +0000399 if (const ConstantArrayType *CAT =
Chris Lattner2e2766a2009-02-24 22:50:46 +0000400 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000401 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
402 }
403 return maxElements;
404}
405
406int InitListChecker::numStructUnionElements(QualType DeclType) {
407 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Douglas Gregorf603b472009-01-28 21:54:33 +0000408 int InitializableMembers = 0;
409 for (RecordDecl::field_iterator Field = structDecl->field_begin(),
410 FieldEnd = structDecl->field_end();
411 Field != FieldEnd; ++Field) {
412 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
413 ++InitializableMembers;
414 }
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000415 if (structDecl->isUnion())
Eli Friedman9f5250b2008-05-25 14:03:31 +0000416 return std::min(InitializableMembers, 1);
417 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000418}
419
420void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregorf603b472009-01-28 21:54:33 +0000421 QualType T, unsigned &Index,
422 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000423 unsigned &StructuredIndex,
424 bool TopLevelObject) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000425 int maxElements = 0;
426
427 if (T->isArrayType())
428 maxElements = numArrayElements(T);
429 else if (T->isStructureType() || T->isUnionType())
430 maxElements = numStructUnionElements(T);
Eli Friedman683cedf2008-05-19 19:16:24 +0000431 else if (T->isVectorType())
432 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000433 else
434 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman683cedf2008-05-19 19:16:24 +0000435
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000436 if (maxElements == 0) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000437 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000438 diag::err_implicit_empty_initializer);
Douglas Gregorf603b472009-01-28 21:54:33 +0000439 ++Index;
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000440 hadError = true;
441 return;
442 }
443
Douglas Gregorf603b472009-01-28 21:54:33 +0000444 // Build a structured initializer list corresponding to this subobject.
445 InitListExpr *StructuredSubobjectInitList
446 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
447 StructuredIndex,
448 ParentIList->getInit(Index)->getSourceRange());
449 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedman683cedf2008-05-19 19:16:24 +0000450
Douglas Gregorf603b472009-01-28 21:54:33 +0000451 // Check the element types and build the structural subobject.
Douglas Gregor538a4c22009-02-02 17:43:21 +0000452 unsigned StartIndex = Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000453 CheckListElementTypes(ParentIList, T, false, Index,
454 StructuredSubobjectInitList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000455 StructuredSubobjectInitIndex,
456 TopLevelObject);
Douglas Gregor538a4c22009-02-02 17:43:21 +0000457 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
458
459 // Update the structured sub-object initialize so that it's ending
460 // range corresponds with the end of the last initializer it used.
461 if (EndIndex < ParentIList->getNumInits()) {
462 SourceLocation EndLoc
463 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
464 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
465 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000466}
467
Steve Naroff56099522008-05-06 00:23:44 +0000468void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregorf603b472009-01-28 21:54:33 +0000469 unsigned &Index,
470 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000471 unsigned &StructuredIndex,
472 bool TopLevelObject) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000473 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregorf603b472009-01-28 21:54:33 +0000474 SyntacticToSemantic[IList] = StructuredList;
475 StructuredList->setSyntacticForm(IList);
476 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000477 StructuredIndex, TopLevelObject);
Steve Naroff56099522008-05-06 00:23:44 +0000478 IList->setType(T);
Douglas Gregorf603b472009-01-28 21:54:33 +0000479 StructuredList->setType(T);
Eli Friedman46f81662008-05-25 13:22:35 +0000480 if (hadError)
481 return;
Eli Friedmand8535af2008-05-19 20:00:43 +0000482
Eli Friedman46f81662008-05-25 13:22:35 +0000483 if (Index < IList->getNumInits()) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000484 // We have leftover initializers
485 if (IList->getNumInits() > 0 &&
Chris Lattner2e2766a2009-02-24 22:50:46 +0000486 IsStringInit(IList->getInit(Index), T, SemaRef.Context)) {
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000487 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000488 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000489 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000490 // Special-case
Chris Lattner2e2766a2009-02-24 22:50:46 +0000491 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000492 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8535af2008-05-19 20:00:43 +0000493 hadError = true;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000494 } else if (!T->isIncompleteType()) {
Douglas Gregor09f078c2009-01-30 22:26:29 +0000495 // Don't complain for incomplete types, since we'll get an error
496 // elsewhere
Douglas Gregorbe69b162009-02-04 22:46:25 +0000497 QualType CurrentObjectType = StructuredList->getType();
498 int initKind =
499 CurrentObjectType->isArrayType()? 0 :
500 CurrentObjectType->isVectorType()? 1 :
501 CurrentObjectType->isScalarType()? 2 :
502 CurrentObjectType->isUnionType()? 3 :
503 4;
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000504
505 unsigned DK = diag::warn_excess_initializers;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000506 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000507 DK = diag::err_excess_initializers;
508
Chris Lattner2e2766a2009-02-24 22:50:46 +0000509 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregorbe69b162009-02-04 22:46:25 +0000510 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmand8535af2008-05-19 20:00:43 +0000511 }
512 }
Eli Friedman455f7622008-05-19 20:20:43 +0000513
Eli Friedman46f81662008-05-25 13:22:35 +0000514 if (T->isScalarType())
Chris Lattner2e2766a2009-02-24 22:50:46 +0000515 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000516 << IList->getSourceRange();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000517}
518
Eli Friedman683cedf2008-05-19 19:16:24 +0000519void InitListChecker::CheckListElementTypes(InitListExpr *IList,
520 QualType &DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000521 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000522 unsigned &Index,
523 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000524 unsigned &StructuredIndex,
525 bool TopLevelObject) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000526 if (DeclType->isScalarType()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000527 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmand8535af2008-05-19 20:00:43 +0000528 } else if (DeclType->isVectorType()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000529 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregore7ef5002009-01-30 17:31:00 +0000530 } else if (DeclType->isAggregateType()) {
531 if (DeclType->isRecordType()) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000532 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
533 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000534 SubobjectIsDesignatorContext, Index,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000535 StructuredList, StructuredIndex,
536 TopLevelObject);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000537 } else if (DeclType->isArrayType()) {
Douglas Gregor5a203a62009-01-23 16:54:12 +0000538 llvm::APSInt Zero(
Chris Lattner2e2766a2009-02-24 22:50:46 +0000539 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregor5a203a62009-01-23 16:54:12 +0000540 false);
Douglas Gregorf603b472009-01-28 21:54:33 +0000541 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
542 StructuredList, StructuredIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000543 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000544 else
Douglas Gregorf603b472009-01-28 21:54:33 +0000545 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroffff5b3a82008-08-10 16:05:48 +0000546 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
547 // This type is invalid, issue a diagnostic.
Douglas Gregorf603b472009-01-28 21:54:33 +0000548 ++Index;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000549 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000550 << DeclType;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000551 hadError = true;
Douglas Gregord45210d2009-01-30 22:09:00 +0000552 } else if (DeclType->isRecordType()) {
553 // C++ [dcl.init]p14:
554 // [...] If the class is an aggregate (8.5.1), and the initializer
555 // is a brace-enclosed list, see 8.5.1.
556 //
557 // Note: 8.5.1 is handled below; here, we diagnose the case where
558 // we have an initializer list and a destination type that is not
559 // an aggregate.
560 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000561 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord45210d2009-01-30 22:09:00 +0000562 << DeclType << IList->getSourceRange();
563 hadError = true;
564 } else if (DeclType->isReferenceType()) {
565 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000566 } else {
567 // In C, all types are either scalars or aggregates, but
568 // additional handling is needed here for C++ (and possibly others?).
569 assert(0 && "Unsupported initializer type");
570 }
571}
572
Eli Friedman683cedf2008-05-19 19:16:24 +0000573void InitListChecker::CheckSubElementType(InitListExpr *IList,
574 QualType ElemType,
Douglas Gregorf603b472009-01-28 21:54:33 +0000575 unsigned &Index,
576 InitListExpr *StructuredList,
577 unsigned &StructuredIndex) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000578 Expr *expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000579 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
580 unsigned newIndex = 0;
Douglas Gregorf603b472009-01-28 21:54:33 +0000581 unsigned newStructuredIndex = 0;
582 InitListExpr *newStructuredList
583 = getStructuredSubobjectInit(IList, Index, ElemType,
584 StructuredList, StructuredIndex,
585 SubInitList->getSourceRange());
586 CheckExplicitInitList(SubInitList, ElemType, newIndex,
587 newStructuredList, newStructuredIndex);
588 ++StructuredIndex;
589 ++Index;
Chris Lattner45d6fd62009-02-24 22:41:04 +0000590 } else if (StringLiteral *Str = IsStringInit(expr, ElemType,
Chris Lattner2e2766a2009-02-24 22:50:46 +0000591 SemaRef.Context)) {
Chris Lattnerd20fac42009-02-24 23:01:39 +0000592 CheckStringInit(Str, Str->getByteLength() + 1, ElemType, SemaRef);
Chris Lattner45d6fd62009-02-24 22:41:04 +0000593 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregorf603b472009-01-28 21:54:33 +0000594 ++Index;
Eli Friedmand8535af2008-05-19 20:00:43 +0000595 } else if (ElemType->isScalarType()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000596 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000597 } else if (ElemType->isReferenceType()) {
598 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedman683cedf2008-05-19 19:16:24 +0000599 } else {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000600 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregord45210d2009-01-30 22:09:00 +0000601 // C++ [dcl.init.aggr]p12:
602 // All implicit type conversions (clause 4) are considered when
603 // initializing the aggregate member with an ini- tializer from
604 // an initializer-list. If the initializer can initialize a
605 // member, the member is initialized. [...]
606 ImplicitConversionSequence ICS
Chris Lattner2e2766a2009-02-24 22:50:46 +0000607 = SemaRef.TryCopyInitialization(expr, ElemType);
Douglas Gregord45210d2009-01-30 22:09:00 +0000608 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000609 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregord45210d2009-01-30 22:09:00 +0000610 "initializing"))
611 hadError = true;
612 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
613 ++Index;
614 return;
615 }
616
617 // Fall through for subaggregate initialization
618 } else {
619 // C99 6.7.8p13:
620 //
621 // The initializer for a structure or union object that has
622 // automatic storage duration shall be either an initializer
623 // list as described below, or a single expression that has
624 // compatible structure or union type. In the latter case, the
625 // initial value of the object, including unnamed members, is
626 // that of the expression.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000627 QualType ExprType = SemaRef.Context.getCanonicalType(expr->getType());
628 QualType ElemTypeCanon = SemaRef.Context.getCanonicalType(ElemType);
629 if (SemaRef.Context.typesAreCompatible(ExprType.getUnqualifiedType(),
Douglas Gregord45210d2009-01-30 22:09:00 +0000630 ElemTypeCanon.getUnqualifiedType())) {
631 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
632 ++Index;
633 return;
634 }
635
636 // Fall through for subaggregate initialization
637 }
638
639 // C++ [dcl.init.aggr]p12:
640 //
641 // [...] Otherwise, if the member is itself a non-empty
642 // subaggregate, brace elision is assumed and the initializer is
643 // considered for the initialization of the first member of
644 // the subaggregate.
645 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
646 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
647 StructuredIndex);
648 ++StructuredIndex;
649 } else {
650 // We cannot initialize this element, so let
651 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000652 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregord45210d2009-01-30 22:09:00 +0000653 hadError = true;
654 ++Index;
655 ++StructuredIndex;
656 }
657 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000658}
659
Douglas Gregord45210d2009-01-30 22:09:00 +0000660void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor36859eb2009-01-29 00:39:20 +0000661 unsigned &Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000662 InitListExpr *StructuredList,
663 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000664 if (Index < IList->getNumInits()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000665 Expr *expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000666 if (isa<InitListExpr>(expr)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000667 SemaRef.Diag(IList->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000668 diag::err_many_braces_around_scalar_init)
669 << IList->getSourceRange();
Eli Friedman71de9eb2008-05-19 20:12:18 +0000670 hadError = true;
671 ++Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000672 ++StructuredIndex;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000673 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000674 } else if (isa<DesignatedInitExpr>(expr)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000675 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000676 diag::err_designator_for_scalar_init)
677 << DeclType << expr->getSourceRange();
678 hadError = true;
679 ++Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000680 ++StructuredIndex;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000681 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000682 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000683
Eli Friedmand8535af2008-05-19 20:00:43 +0000684 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000685 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedman71de9eb2008-05-19 20:12:18 +0000686 hadError = true; // types weren't compatible.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000687 else if (savExpr != expr) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000688 // The type was promoted, update initializer list.
Douglas Gregor36859eb2009-01-29 00:39:20 +0000689 IList->setInit(Index, expr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000690 }
Douglas Gregorf603b472009-01-28 21:54:33 +0000691 if (hadError)
692 ++StructuredIndex;
693 else
694 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000695 ++Index;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000696 } else {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000697 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000698 << IList->getSourceRange();
Eli Friedman71de9eb2008-05-19 20:12:18 +0000699 hadError = true;
Douglas Gregorf603b472009-01-28 21:54:33 +0000700 ++Index;
701 ++StructuredIndex;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000702 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000703 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000704}
705
Douglas Gregord45210d2009-01-30 22:09:00 +0000706void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
707 unsigned &Index,
708 InitListExpr *StructuredList,
709 unsigned &StructuredIndex) {
710 if (Index < IList->getNumInits()) {
711 Expr *expr = IList->getInit(Index);
712 if (isa<InitListExpr>(expr)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000713 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord45210d2009-01-30 22:09:00 +0000714 << DeclType << IList->getSourceRange();
715 hadError = true;
716 ++Index;
717 ++StructuredIndex;
718 return;
719 }
720
721 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000722 if (SemaRef.CheckReferenceInit(expr, DeclType))
Douglas Gregord45210d2009-01-30 22:09:00 +0000723 hadError = true;
724 else if (savExpr != expr) {
725 // The type was promoted, update initializer list.
726 IList->setInit(Index, expr);
727 }
728 if (hadError)
729 ++StructuredIndex;
730 else
731 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
732 ++Index;
733 } else {
734 // FIXME: It would be wonderful if we could point at the actual
735 // member. In general, it would be useful to pass location
736 // information down the stack, so that we know the location (or
737 // decl) of the "current object" being initialized.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000738 SemaRef.Diag(IList->getLocStart(),
Douglas Gregord45210d2009-01-30 22:09:00 +0000739 diag::err_init_reference_member_uninitialized)
740 << DeclType
741 << IList->getSourceRange();
742 hadError = true;
743 ++Index;
744 ++StructuredIndex;
745 return;
746 }
747}
748
Steve Naroffc4d4a482008-05-01 22:18:59 +0000749void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregorf603b472009-01-28 21:54:33 +0000750 unsigned &Index,
751 InitListExpr *StructuredList,
752 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000753 if (Index < IList->getNumInits()) {
754 const VectorType *VT = DeclType->getAsVectorType();
755 int maxElements = VT->getNumElements();
756 QualType elementType = VT->getElementType();
757
758 for (int i = 0; i < maxElements; ++i) {
759 // Don't attempt to go past the end of the init list
760 if (Index >= IList->getNumInits())
761 break;
Douglas Gregor36859eb2009-01-29 00:39:20 +0000762 CheckSubElementType(IList, elementType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000763 StructuredList, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000764 }
765 }
766}
767
768void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000769 llvm::APSInt elementIndex,
770 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000771 unsigned &Index,
772 InitListExpr *StructuredList,
773 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000774 // Check for the special-case of initializing an array with a string.
775 if (Index < IList->getNumInits()) {
Chris Lattner45d6fd62009-02-24 22:41:04 +0000776 if (StringLiteral *Str = IsStringInit(IList->getInit(Index), DeclType,
Chris Lattner2e2766a2009-02-24 22:50:46 +0000777 SemaRef.Context)) {
Chris Lattnerd20fac42009-02-24 23:01:39 +0000778 CheckStringInit(Str, Str->getByteLength() + 1, DeclType, SemaRef);
Douglas Gregorf603b472009-01-28 21:54:33 +0000779 // We place the string literal directly into the resulting
780 // initializer list. This is the only place where the structure
781 // of the structured initializer list doesn't match exactly,
782 // because doing so would involve allocating one character
783 // constant for each string.
Chris Lattner45d6fd62009-02-24 22:41:04 +0000784 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner2e2766a2009-02-24 22:50:46 +0000785 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000786 ++Index;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000787 return;
788 }
789 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000790 if (const VariableArrayType *VAT =
Chris Lattner2e2766a2009-02-24 22:50:46 +0000791 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman46f81662008-05-25 13:22:35 +0000792 // Check for VLAs; in standard C it would be possible to check this
793 // earlier, but I don't know where clang accepts VLAs (gcc accepts
794 // them in all sorts of strange places).
Chris Lattner2e2766a2009-02-24 22:50:46 +0000795 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000796 diag::err_variable_object_no_init)
797 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman46f81662008-05-25 13:22:35 +0000798 hadError = true;
Douglas Gregorf603b472009-01-28 21:54:33 +0000799 ++Index;
800 ++StructuredIndex;
Eli Friedman46f81662008-05-25 13:22:35 +0000801 return;
802 }
803
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000804 // We might know the maximum number of elements in advance.
Douglas Gregorf603b472009-01-28 21:54:33 +0000805 llvm::APSInt maxElements(elementIndex.getBitWidth(),
806 elementIndex.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000807 bool maxElementsKnown = false;
808 if (const ConstantArrayType *CAT =
Chris Lattner2e2766a2009-02-24 22:50:46 +0000809 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000810 maxElements = CAT->getSize();
Douglas Gregor5a203a62009-01-23 16:54:12 +0000811 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregor69722702009-01-23 18:58:42 +0000812 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000813 maxElementsKnown = true;
814 }
815
Chris Lattner2e2766a2009-02-24 22:50:46 +0000816 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnera1923f62008-08-04 07:31:14 +0000817 ->getElementType();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000818 while (Index < IList->getNumInits()) {
819 Expr *Init = IList->getInit(Index);
820 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000821 // If we're not the subobject that matches up with the '{' for
822 // the designator, we shouldn't be handling the
823 // designator. Return immediately.
824 if (!SubobjectIsDesignatorContext)
825 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000826
Douglas Gregor710f6d42009-01-22 23:26:18 +0000827 // Handle this designated initializer. elementIndex will be
828 // updated to be the next array element we'll initialize.
829 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000830 DeclType, 0, &elementIndex, Index,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000831 StructuredList, StructuredIndex, true,
832 false)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000833 hadError = true;
834 continue;
835 }
836
Douglas Gregor5a203a62009-01-23 16:54:12 +0000837 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
838 maxElements.extend(elementIndex.getBitWidth());
839 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
840 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregor69722702009-01-23 18:58:42 +0000841 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor5a203a62009-01-23 16:54:12 +0000842
Douglas Gregor710f6d42009-01-22 23:26:18 +0000843 // If the array is of incomplete type, keep track of the number of
844 // elements in the initializer.
845 if (!maxElementsKnown && elementIndex > maxElements)
846 maxElements = elementIndex;
847
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000848 continue;
849 }
850
851 // If we know the maximum number of elements, and we've already
852 // hit it, stop consuming elements in the initializer list.
853 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroffc4d4a482008-05-01 22:18:59 +0000854 break;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000855
856 // Check this element.
Douglas Gregor36859eb2009-01-29 00:39:20 +0000857 CheckSubElementType(IList, elementType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000858 StructuredList, StructuredIndex);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000859 ++elementIndex;
860
861 // If the array is of incomplete type, keep track of the number of
862 // elements in the initializer.
863 if (!maxElementsKnown && elementIndex > maxElements)
864 maxElements = elementIndex;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000865 }
866 if (DeclType->isIncompleteArrayType()) {
867 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000868 // be calculated here.
Douglas Gregor69722702009-01-23 18:58:42 +0000869 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000870 if (maxElements == Zero) {
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000871 // Sizing an array implicitly to zero is not allowed by ISO C,
872 // but is supported by GNU.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000873 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000874 diag::ext_typecheck_zero_array_size);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000875 }
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000876
Chris Lattner2e2766a2009-02-24 22:50:46 +0000877 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000878 ArrayType::Normal, 0);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000879 }
880}
881
882void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
883 QualType DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000884 RecordDecl::field_iterator Field,
885 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000886 unsigned &Index,
887 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000888 unsigned &StructuredIndex,
889 bool TopLevelObject) {
Eli Friedman683cedf2008-05-19 19:16:24 +0000890 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000891
Eli Friedman683cedf2008-05-19 19:16:24 +0000892 // If the record is invalid, some of it's members are invalid. To avoid
893 // confusion, we forgo checking the intializer for the entire record.
894 if (structDecl->isInvalidDecl()) {
895 hadError = true;
896 return;
897 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000898
899 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
900 // Value-initialize the first named member of the union.
901 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
902 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
903 Field != FieldEnd; ++Field) {
904 if (Field->getDeclName()) {
905 StructuredList->setInitializedFieldInUnion(*Field);
906 break;
907 }
908 }
909 return;
910 }
911
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000912 // If structDecl is a forward declaration, this loop won't do
913 // anything except look at designated initializers; That's okay,
914 // because an error should get printed out elsewhere. It might be
915 // worthwhile to skip over the rest of the initializer, though.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000916 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
Douglas Gregor710f6d42009-01-22 23:26:18 +0000917 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000918 bool InitializedSomething = false;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000919 while (Index < IList->getNumInits()) {
920 Expr *Init = IList->getInit(Index);
921
922 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000923 // If we're not the subobject that matches up with the '{' for
924 // the designator, we shouldn't be handling the
925 // designator. Return immediately.
926 if (!SubobjectIsDesignatorContext)
927 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000928
Douglas Gregor710f6d42009-01-22 23:26:18 +0000929 // Handle this designated initializer. Field will be updated to
930 // the next field that we'll be initializing.
931 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000932 DeclType, &Field, 0, Index,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000933 StructuredList, StructuredIndex,
934 true, TopLevelObject))
Douglas Gregor710f6d42009-01-22 23:26:18 +0000935 hadError = true;
936
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000937 InitializedSomething = true;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000938 continue;
939 }
940
941 if (Field == FieldEnd) {
942 // We've run out of fields. We're done.
943 break;
944 }
945
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000946 // We've already initialized a member of a union. We're done.
947 if (InitializedSomething && DeclType->isUnionType())
948 break;
949
Douglas Gregor8acb7272008-12-11 16:49:14 +0000950 // If we've hit the flexible array member at the end, we're done.
951 if (Field->getType()->isIncompleteArrayType())
952 break;
953
Douglas Gregor82462762009-01-29 16:53:55 +0000954 if (Field->isUnnamedBitfield()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000955 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000956 ++Field;
Eli Friedman683cedf2008-05-19 19:16:24 +0000957 continue;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000958 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000959
Douglas Gregor36859eb2009-01-29 00:39:20 +0000960 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000961 StructuredList, StructuredIndex);
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000962 InitializedSomething = true;
Douglas Gregor82462762009-01-29 16:53:55 +0000963
964 if (DeclType->isUnionType()) {
965 // Initialize the first field within the union.
966 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor82462762009-01-29 16:53:55 +0000967 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000968
969 ++Field;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000970 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000971
Douglas Gregorbe69b162009-02-04 22:46:25 +0000972 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
973 Index >= IList->getNumInits() ||
974 !isa<InitListExpr>(IList->getInit(Index)))
975 return;
976
977 // Handle GNU flexible array initializers.
978 if (!TopLevelObject &&
979 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000980 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregorbe69b162009-02-04 22:46:25 +0000981 diag::err_flexible_array_init_nonempty)
982 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner2e2766a2009-02-24 22:50:46 +0000983 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +0000984 << *Field;
985 hadError = true;
986 }
987
988 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
989 StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000990}
Steve Naroffc4d4a482008-05-01 22:18:59 +0000991
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000992/// @brief Check the well-formedness of a C99 designated initializer.
993///
994/// Determines whether the designated initializer @p DIE, which
995/// resides at the given @p Index within the initializer list @p
996/// IList, is well-formed for a current object of type @p DeclType
997/// (C99 6.7.8). The actual subobject that this designator refers to
998/// within the current subobject is returned in either
Douglas Gregorf603b472009-01-28 21:54:33 +0000999/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001000///
1001/// @param IList The initializer list in which this designated
1002/// initializer occurs.
1003///
1004/// @param DIE The designated initializer and its initialization
1005/// expression.
1006///
1007/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1008/// into which the designation in @p DIE should refer.
1009///
Douglas Gregor710f6d42009-01-22 23:26:18 +00001010/// @param NextField If non-NULL and the first designator in @p DIE is
1011/// a field, this will be set to the field declaration corresponding
1012/// to the field named by the designator.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001013///
Douglas Gregor710f6d42009-01-22 23:26:18 +00001014/// @param NextElementIndex If non-NULL and the first designator in @p
1015/// DIE is an array designator or GNU array-range designator, this
1016/// will be set to the last index initialized by this designator.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001017///
1018/// @param Index Index into @p IList where the designated initializer
1019/// @p DIE occurs.
1020///
Douglas Gregorf603b472009-01-28 21:54:33 +00001021/// @param StructuredList The initializer list expression that
1022/// describes all of the subobject initializers in the order they'll
1023/// actually be initialized.
1024///
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001025/// @returns true if there was an error, false otherwise.
Douglas Gregor710f6d42009-01-22 23:26:18 +00001026bool
1027InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
1028 DesignatedInitExpr *DIE,
Chris Lattnerd20fac42009-02-24 23:01:39 +00001029 DesignatedInitExpr::designators_iterator D,
Douglas Gregor710f6d42009-01-22 23:26:18 +00001030 QualType &CurrentObjectType,
1031 RecordDecl::field_iterator *NextField,
1032 llvm::APSInt *NextElementIndex,
Douglas Gregorf603b472009-01-28 21:54:33 +00001033 unsigned &Index,
1034 InitListExpr *StructuredList,
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001035 unsigned &StructuredIndex,
Douglas Gregorbe69b162009-02-04 22:46:25 +00001036 bool FinishSubobjectInit,
1037 bool TopLevelObject) {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001038 if (D == DIE->designators_end()) {
1039 // Check the actual initialization for the designated object type.
1040 bool prevHadError = hadError;
Douglas Gregor36859eb2009-01-29 00:39:20 +00001041
1042 // Temporarily remove the designator expression from the
1043 // initializer list that the child calls see, so that we don't try
1044 // to re-process the designator.
1045 unsigned OldIndex = Index;
1046 IList->setInit(OldIndex, DIE->getInit());
1047
1048 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +00001049 StructuredList, StructuredIndex);
Douglas Gregor36859eb2009-01-29 00:39:20 +00001050
1051 // Restore the designated initializer expression in the syntactic
1052 // form of the initializer list.
1053 if (IList->getInit(OldIndex) != DIE->getInit())
1054 DIE->setInit(IList->getInit(OldIndex));
1055 IList->setInit(OldIndex, DIE);
1056
Douglas Gregor710f6d42009-01-22 23:26:18 +00001057 return hadError && !prevHadError;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001058 }
1059
Douglas Gregorf603b472009-01-28 21:54:33 +00001060 bool IsFirstDesignator = (D == DIE->designators_begin());
1061 assert((IsFirstDesignator || StructuredList) &&
1062 "Need a non-designated initializer list to start from");
1063
1064 // Determine the structural initializer list that corresponds to the
1065 // current subobject.
1066 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
1067 : getStructuredSubobjectInit(IList, Index, CurrentObjectType, StructuredList,
1068 StructuredIndex,
1069 SourceRange(D->getStartLocation(),
1070 DIE->getSourceRange().getEnd()));
1071 assert(StructuredList && "Expected a structured initializer list");
1072
Douglas Gregor710f6d42009-01-22 23:26:18 +00001073 if (D->isFieldDesignator()) {
1074 // C99 6.7.8p7:
1075 //
1076 // If a designator has the form
1077 //
1078 // . identifier
1079 //
1080 // then the current object (defined below) shall have
1081 // structure or union type and the identifier shall be the
1082 // name of a member of that type.
1083 const RecordType *RT = CurrentObjectType->getAsRecordType();
1084 if (!RT) {
1085 SourceLocation Loc = D->getDotLoc();
1086 if (Loc.isInvalid())
1087 Loc = D->getFieldLoc();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001088 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1089 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001090 ++Index;
1091 return true;
1092 }
1093
Douglas Gregorf603b472009-01-28 21:54:33 +00001094 // Note: we perform a linear search of the fields here, despite
1095 // the fact that we have a faster lookup method, because we always
1096 // need to compute the field's index.
Douglas Gregor710f6d42009-01-22 23:26:18 +00001097 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregorf603b472009-01-28 21:54:33 +00001098 unsigned FieldIndex = 0;
1099 RecordDecl::field_iterator Field = RT->getDecl()->field_begin(),
1100 FieldEnd = RT->getDecl()->field_end();
1101 for (; Field != FieldEnd; ++Field) {
1102 if (Field->isUnnamedBitfield())
1103 continue;
1104
1105 if (Field->getIdentifier() == FieldName)
1106 break;
1107
1108 ++FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001109 }
1110
Douglas Gregorf603b472009-01-28 21:54:33 +00001111 if (Field == FieldEnd) {
1112 // We did not find the field we're looking for. Produce a
1113 // suitable diagnostic and return a failure.
1114 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1115 if (Lookup.first == Lookup.second) {
1116 // Name lookup didn't find anything.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001117 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregorf603b472009-01-28 21:54:33 +00001118 << FieldName << CurrentObjectType;
1119 } else {
1120 // Name lookup found something, but it wasn't a field.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001121 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregorf603b472009-01-28 21:54:33 +00001122 << FieldName;
Chris Lattner2e2766a2009-02-24 22:50:46 +00001123 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001124 diag::note_field_designator_found);
1125 }
1126
1127 ++Index;
1128 return true;
1129 } else if (cast<RecordDecl>((*Field)->getDeclContext())
1130 ->isAnonymousStructOrUnion()) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001131 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_anon_class)
Douglas Gregorf603b472009-01-28 21:54:33 +00001132 << FieldName
1133 << (cast<RecordDecl>((*Field)->getDeclContext())->isUnion()? 2 :
Chris Lattner2e2766a2009-02-24 22:50:46 +00001134 (int)SemaRef.getLangOptions().CPlusPlus);
1135 SemaRef.Diag((*Field)->getLocation(), diag::note_field_designator_found);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001136 ++Index;
1137 return true;
1138 }
Douglas Gregorf603b472009-01-28 21:54:33 +00001139
1140 // All of the fields of a union are located at the same place in
1141 // the initializer list.
Douglas Gregor82462762009-01-29 16:53:55 +00001142 if (RT->getDecl()->isUnion()) {
Douglas Gregorf603b472009-01-28 21:54:33 +00001143 FieldIndex = 0;
Douglas Gregor82462762009-01-29 16:53:55 +00001144 StructuredList->setInitializedFieldInUnion(*Field);
1145 }
Douglas Gregorf603b472009-01-28 21:54:33 +00001146
Douglas Gregor710f6d42009-01-22 23:26:18 +00001147 // Update the designator with the field declaration.
Douglas Gregorf603b472009-01-28 21:54:33 +00001148 D->setField(*Field);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001149
Douglas Gregorf603b472009-01-28 21:54:33 +00001150 // Make sure that our non-designated initializer list has space
1151 // for a subobject corresponding to this field.
1152 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner2e2766a2009-02-24 22:50:46 +00001153 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregorf603b472009-01-28 21:54:33 +00001154
Douglas Gregorbe69b162009-02-04 22:46:25 +00001155 // This designator names a flexible array member.
1156 if (Field->getType()->isIncompleteArrayType()) {
1157 bool Invalid = false;
1158 DesignatedInitExpr::designators_iterator NextD = D;
1159 ++NextD;
1160 if (NextD != DIE->designators_end()) {
1161 // We can't designate an object within the flexible array
1162 // member (because GCC doesn't allow it).
Chris Lattner2e2766a2009-02-24 22:50:46 +00001163 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001164 diag::err_designator_into_flexible_array_member)
1165 << SourceRange(NextD->getStartLocation(),
1166 DIE->getSourceRange().getEnd());
Chris Lattner2e2766a2009-02-24 22:50:46 +00001167 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001168 << *Field;
1169 Invalid = true;
1170 }
1171
1172 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1173 // The initializer is not an initializer list.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001174 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001175 diag::err_flexible_array_init_needs_braces)
1176 << DIE->getInit()->getSourceRange();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001177 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001178 << *Field;
1179 Invalid = true;
1180 }
1181
1182 // Handle GNU flexible array initializers.
1183 if (!Invalid && !TopLevelObject &&
1184 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001185 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001186 diag::err_flexible_array_init_nonempty)
1187 << DIE->getSourceRange().getBegin();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001188 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001189 << *Field;
1190 Invalid = true;
1191 }
1192
1193 if (Invalid) {
1194 ++Index;
1195 return true;
1196 }
1197
1198 // Initialize the array.
1199 bool prevHadError = hadError;
1200 unsigned newStructuredIndex = FieldIndex;
1201 unsigned OldIndex = Index;
1202 IList->setInit(Index, DIE->getInit());
1203 CheckSubElementType(IList, Field->getType(), Index,
1204 StructuredList, newStructuredIndex);
1205 IList->setInit(OldIndex, DIE);
1206 if (hadError && !prevHadError) {
1207 ++Field;
1208 ++FieldIndex;
1209 if (NextField)
1210 *NextField = Field;
1211 StructuredIndex = FieldIndex;
1212 return true;
1213 }
1214 } else {
1215 // Recurse to check later designated subobjects.
1216 QualType FieldType = (*Field)->getType();
1217 unsigned newStructuredIndex = FieldIndex;
1218 if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index,
1219 StructuredList, newStructuredIndex,
1220 true, false))
1221 return true;
1222 }
Douglas Gregor710f6d42009-01-22 23:26:18 +00001223
1224 // Find the position of the next field to be initialized in this
1225 // subobject.
Douglas Gregor710f6d42009-01-22 23:26:18 +00001226 ++Field;
Douglas Gregorf603b472009-01-28 21:54:33 +00001227 ++FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001228
1229 // If this the first designator, our caller will continue checking
1230 // the rest of this struct/class/union subobject.
1231 if (IsFirstDesignator) {
1232 if (NextField)
1233 *NextField = Field;
Douglas Gregorf603b472009-01-28 21:54:33 +00001234 StructuredIndex = FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001235 return false;
1236 }
1237
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001238 if (!FinishSubobjectInit)
1239 return false;
1240
Douglas Gregor710f6d42009-01-22 23:26:18 +00001241 // Check the remaining fields within this class/struct/union subobject.
1242 bool prevHadError = hadError;
Douglas Gregorf603b472009-01-28 21:54:33 +00001243 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1244 StructuredList, FieldIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001245 return hadError && !prevHadError;
1246 }
1247
1248 // C99 6.7.8p6:
1249 //
1250 // If a designator has the form
1251 //
1252 // [ constant-expression ]
1253 //
1254 // then the current object (defined below) shall have array
1255 // type and the expression shall be an integer constant
1256 // expression. If the array is of unknown size, any
1257 // nonnegative value is valid.
1258 //
1259 // Additionally, cope with the GNU extension that permits
1260 // designators of the form
1261 //
1262 // [ constant-expression ... constant-expression ]
Chris Lattner2e2766a2009-02-24 22:50:46 +00001263 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001264 if (!AT) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001265 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor710f6d42009-01-22 23:26:18 +00001266 << CurrentObjectType;
1267 ++Index;
1268 return true;
1269 }
1270
1271 Expr *IndexExpr = 0;
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001272 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1273 if (D->isArrayDesignator()) {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001274 IndexExpr = DIE->getArrayIndex(*D);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001275
1276 bool ConstExpr
Chris Lattner2e2766a2009-02-24 22:50:46 +00001277 = IndexExpr->isIntegerConstantExpr(DesignatedStartIndex, SemaRef.Context);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001278 assert(ConstExpr && "Expression must be constant"); (void)ConstExpr;
1279
1280 DesignatedEndIndex = DesignatedStartIndex;
1281 } else {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001282 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001283
1284 bool StartConstExpr
1285 = DIE->getArrayRangeStart(*D)->isIntegerConstantExpr(DesignatedStartIndex,
Chris Lattner2e2766a2009-02-24 22:50:46 +00001286 SemaRef.Context);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001287 assert(StartConstExpr && "Expression must be constant"); (void)StartConstExpr;
1288
1289 bool EndConstExpr
1290 = DIE->getArrayRangeEnd(*D)->isIntegerConstantExpr(DesignatedEndIndex,
Chris Lattner2e2766a2009-02-24 22:50:46 +00001291 SemaRef.Context);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001292 assert(EndConstExpr && "Expression must be constant"); (void)EndConstExpr;
1293
Douglas Gregor710f6d42009-01-22 23:26:18 +00001294 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001295
1296 if (DesignatedStartIndex.getZExtValue() != DesignatedEndIndex.getZExtValue())
Douglas Gregor9fddded2009-01-29 19:42:23 +00001297 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor710f6d42009-01-22 23:26:18 +00001298 }
1299
Douglas Gregor710f6d42009-01-22 23:26:18 +00001300 if (isa<ConstantArrayType>(AT)) {
1301 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001302 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1303 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1304 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1305 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1306 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001307 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor710f6d42009-01-22 23:26:18 +00001308 diag::err_array_designator_too_large)
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001309 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor710f6d42009-01-22 23:26:18 +00001310 << IndexExpr->getSourceRange();
1311 ++Index;
1312 return true;
1313 }
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001314 } else {
1315 // Make sure the bit-widths and signedness match.
1316 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1317 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
1318 else if (DesignatedStartIndex.getBitWidth() < DesignatedEndIndex.getBitWidth())
1319 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1320 DesignatedStartIndex.setIsUnsigned(true);
1321 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001322 }
1323
Douglas Gregorf603b472009-01-28 21:54:33 +00001324 // Make sure that our non-designated initializer list has space
1325 // for a subobject corresponding to this array element.
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001326 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Chris Lattner2e2766a2009-02-24 22:50:46 +00001327 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001328 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregorf603b472009-01-28 21:54:33 +00001329
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001330 // Repeatedly perform subobject initializations in the range
1331 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor710f6d42009-01-22 23:26:18 +00001332
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001333 // Move to the next designator
1334 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1335 unsigned OldIndex = Index;
1336 ++D;
1337 while (DesignatedStartIndex <= DesignatedEndIndex) {
1338 // Recurse to check later designated subobjects.
1339 QualType ElementType = AT->getElementType();
1340 Index = OldIndex;
1341 if (CheckDesignatedInitializer(IList, DIE, D, ElementType, 0, 0, Index,
1342 StructuredList, ElementIndex,
Douglas Gregorbe69b162009-02-04 22:46:25 +00001343 (DesignatedStartIndex == DesignatedEndIndex),
1344 false))
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001345 return true;
1346
1347 // Move to the next index in the array that we'll be initializing.
1348 ++DesignatedStartIndex;
1349 ElementIndex = DesignatedStartIndex.getZExtValue();
1350 }
Douglas Gregor710f6d42009-01-22 23:26:18 +00001351
1352 // If this the first designator, our caller will continue checking
1353 // the rest of this array subobject.
1354 if (IsFirstDesignator) {
1355 if (NextElementIndex)
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001356 *NextElementIndex = DesignatedStartIndex;
Douglas Gregorf603b472009-01-28 21:54:33 +00001357 StructuredIndex = ElementIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001358 return false;
1359 }
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001360
1361 if (!FinishSubobjectInit)
1362 return false;
1363
Douglas Gregor710f6d42009-01-22 23:26:18 +00001364 // Check the remaining elements within this array subobject.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001365 bool prevHadError = hadError;
Douglas Gregord7e76c52009-02-09 19:45:19 +00001366 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +00001367 StructuredList, ElementIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001368 return hadError && !prevHadError;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001369}
1370
Douglas Gregorf603b472009-01-28 21:54:33 +00001371// Get the structured initializer list for a subobject of type
1372// @p CurrentObjectType.
1373InitListExpr *
1374InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1375 QualType CurrentObjectType,
1376 InitListExpr *StructuredList,
1377 unsigned StructuredIndex,
1378 SourceRange InitRange) {
1379 Expr *ExistingInit = 0;
1380 if (!StructuredList)
1381 ExistingInit = SyntacticToSemantic[IList];
1382 else if (StructuredIndex < StructuredList->getNumInits())
1383 ExistingInit = StructuredList->getInit(StructuredIndex);
1384
1385 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1386 return Result;
1387
1388 if (ExistingInit) {
1389 // We are creating an initializer list that initializes the
1390 // subobjects of the current object, but there was already an
1391 // initialization that completely initialized the current
1392 // subobject, e.g., by a compound literal:
1393 //
1394 // struct X { int a, b; };
1395 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
1396 //
1397 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1398 // designated initializer re-initializes the whole
1399 // subobject [0], overwriting previous initializers.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001400 SemaRef.Diag(InitRange.getBegin(), diag::warn_subobject_initializer_overrides)
Douglas Gregorf603b472009-01-28 21:54:33 +00001401 << InitRange;
Chris Lattner2e2766a2009-02-24 22:50:46 +00001402 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001403 diag::note_previous_initializer)
Douglas Gregor756283b2009-01-28 23:43:32 +00001404 << /*FIXME:has side effects=*/0
Douglas Gregorf603b472009-01-28 21:54:33 +00001405 << ExistingInit->getSourceRange();
1406 }
1407
Douglas Gregor538a4c22009-02-02 17:43:21 +00001408 SourceLocation StartLoc;
1409 if (Index < IList->getNumInits())
1410 StartLoc = IList->getInit(Index)->getSourceRange().getBegin();
Douglas Gregorf603b472009-01-28 21:54:33 +00001411 InitListExpr *Result
Chris Lattner2e2766a2009-02-24 22:50:46 +00001412 = new (SemaRef.Context) InitListExpr(StartLoc, 0, 0,
Douglas Gregor538a4c22009-02-02 17:43:21 +00001413 IList->getSourceRange().getEnd());
Douglas Gregorf603b472009-01-28 21:54:33 +00001414 Result->setType(CurrentObjectType);
1415
1416 // Link this new initializer list into the structured initializer
1417 // lists.
1418 if (StructuredList)
1419 StructuredList->updateInit(StructuredIndex, Result);
1420 else {
1421 Result->setSyntacticForm(IList);
1422 SyntacticToSemantic[IList] = Result;
1423 }
1424
1425 return Result;
1426}
1427
1428/// Update the initializer at index @p StructuredIndex within the
1429/// structured initializer list to the value @p expr.
1430void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1431 unsigned &StructuredIndex,
1432 Expr *expr) {
1433 // No structured initializer list to update
1434 if (!StructuredList)
1435 return;
1436
1437 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1438 // This initializer overwrites a previous initializer. Warn.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001439 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001440 diag::warn_initializer_overrides)
1441 << expr->getSourceRange();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001442 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001443 diag::note_previous_initializer)
Douglas Gregor756283b2009-01-28 23:43:32 +00001444 << /*FIXME:has side effects=*/0
Douglas Gregorf603b472009-01-28 21:54:33 +00001445 << PrevInit->getSourceRange();
1446 }
1447
1448 ++StructuredIndex;
1449}
1450
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001451/// Check that the given Index expression is a valid array designator
1452/// value. This is essentailly just a wrapper around
1453/// Expr::isIntegerConstantExpr that also checks for negative values
1454/// and produces a reasonable diagnostic if there is a
1455/// failure. Returns true if there was an error, false otherwise. If
1456/// everything went okay, Value will receive the value of the constant
1457/// expression.
1458static bool
1459CheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) {
1460 SourceLocation Loc = Index->getSourceRange().getBegin();
1461
1462 // Make sure this is an integer constant expression.
1463 if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc))
1464 return Self.Diag(Loc, diag::err_array_designator_nonconstant)
1465 << Index->getSourceRange();
1466
1467 // Make sure this constant expression is non-negative.
Douglas Gregor69722702009-01-23 18:58:42 +00001468 llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()),
1469 Value.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001470 if (Value < Zero)
1471 return Self.Diag(Loc, diag::err_array_designator_negative)
1472 << Value.toString(10) << Index->getSourceRange();
1473
Douglas Gregore498e372009-01-23 21:04:18 +00001474 Value.setIsUnsigned(true);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001475 return false;
1476}
1477
1478Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1479 SourceLocation Loc,
1480 bool UsedColonSyntax,
1481 OwningExprResult Init) {
1482 typedef DesignatedInitExpr::Designator ASTDesignator;
1483
1484 bool Invalid = false;
1485 llvm::SmallVector<ASTDesignator, 32> Designators;
1486 llvm::SmallVector<Expr *, 32> InitExpressions;
1487
1488 // Build designators and check array designator expressions.
1489 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1490 const Designator &D = Desig.getDesignator(Idx);
1491 switch (D.getKind()) {
1492 case Designator::FieldDesignator:
1493 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
1494 D.getFieldLoc()));
1495 break;
1496
1497 case Designator::ArrayDesignator: {
1498 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1499 llvm::APSInt IndexValue;
1500 if (CheckArrayDesignatorExpr(*this, Index, IndexValue))
1501 Invalid = true;
1502 else {
1503 Designators.push_back(ASTDesignator(InitExpressions.size(),
1504 D.getLBracketLoc(),
1505 D.getRBracketLoc()));
1506 InitExpressions.push_back(Index);
1507 }
1508 break;
1509 }
1510
1511 case Designator::ArrayRangeDesignator: {
1512 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1513 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1514 llvm::APSInt StartValue;
1515 llvm::APSInt EndValue;
1516 if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) ||
1517 CheckArrayDesignatorExpr(*this, EndIndex, EndValue))
1518 Invalid = true;
Douglas Gregorea0528d2009-01-23 22:22:29 +00001519 else {
1520 // Make sure we're comparing values with the same bit width.
1521 if (StartValue.getBitWidth() > EndValue.getBitWidth())
1522 EndValue.extend(StartValue.getBitWidth());
1523 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1524 StartValue.extend(EndValue.getBitWidth());
1525
1526 if (EndValue < StartValue) {
1527 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1528 << StartValue.toString(10) << EndValue.toString(10)
1529 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1530 Invalid = true;
1531 } else {
1532 Designators.push_back(ASTDesignator(InitExpressions.size(),
1533 D.getLBracketLoc(),
1534 D.getEllipsisLoc(),
1535 D.getRBracketLoc()));
1536 InitExpressions.push_back(StartIndex);
1537 InitExpressions.push_back(EndIndex);
1538 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001539 }
1540 break;
1541 }
1542 }
1543 }
1544
1545 if (Invalid || Init.isInvalid())
1546 return ExprError();
1547
1548 // Clear out the expressions within the designation.
1549 Desig.ClearExprs(*this);
1550
1551 DesignatedInitExpr *DIE
1552 = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(),
1553 &InitExpressions[0], InitExpressions.size(),
1554 Loc, UsedColonSyntax,
1555 static_cast<Expr *>(Init.release()));
1556 return Owned(DIE);
1557}
Douglas Gregor849afc32009-01-29 00:45:39 +00001558
1559bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001560 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregor849afc32009-01-29 00:45:39 +00001561 if (!CheckInitList.HadError())
1562 InitList = CheckInitList.getFullyStructuredList();
1563
1564 return CheckInitList.HadError();
1565}
Douglas Gregor538a4c22009-02-02 17:43:21 +00001566
1567/// \brief Diagnose any semantic errors with value-initialization of
1568/// the given type.
1569///
1570/// Value-initialization effectively zero-initializes any types
1571/// without user-declared constructors, and calls the default
1572/// constructor for a for any type that has a user-declared
1573/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1574/// a type with a user-declared constructor does not have an
1575/// accessible, non-deleted default constructor. In C, everything can
1576/// be value-initialized, which corresponds to C's notion of
1577/// initializing objects with static storage duration when no
1578/// initializer is provided for that object.
1579///
1580/// \returns true if there was an error, false otherwise.
1581bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1582 // C++ [dcl.init]p5:
1583 //
1584 // To value-initialize an object of type T means:
1585
1586 // -- if T is an array type, then each element is value-initialized;
1587 if (const ArrayType *AT = Context.getAsArrayType(Type))
1588 return CheckValueInitialization(AT->getElementType(), Loc);
1589
1590 if (const RecordType *RT = Type->getAsRecordType()) {
1591 if (const CXXRecordType *CXXRec = dyn_cast<CXXRecordType>(RT)) {
1592 // -- if T is a class type (clause 9) with a user-declared
1593 // constructor (12.1), then the default constructor for T is
1594 // called (and the initialization is ill-formed if T has no
1595 // accessible default constructor);
1596 if (CXXRec->getDecl()->hasUserDeclaredConstructor())
1597 // FIXME: Eventually, we'll need to put the constructor decl
1598 // into the AST.
1599 return PerformInitializationByConstructor(Type, 0, 0, Loc,
1600 SourceRange(Loc),
1601 DeclarationName(),
1602 IK_Direct);
1603 }
1604 }
1605
1606 if (Type->isReferenceType()) {
1607 // C++ [dcl.init]p5:
1608 // [...] A program that calls for default-initialization or
1609 // value-initialization of an entity of reference type is
1610 // ill-formed. [...]
Douglas Gregorbd4b0852009-02-02 21:35:47 +00001611 // FIXME: Once we have code that goes through this path, add an
1612 // actual diagnostic :)
Douglas Gregor538a4c22009-02-02 17:43:21 +00001613 }
1614
1615 return false;
1616}