blob: 1f4ab078efa777a6170866f0a3ffd26a1a8cc2b0 [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"
Chris Lattner19ae2fc2009-02-24 23:10:27 +000021#include "clang/AST/ExprObjC.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 Lattner19ae2fc2009-02-24 23:10:27 +000029static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
Chris Lattner7a7c1452009-02-26 23:26:43 +000030 const ArrayType *AT = Context.getAsArrayType(DeclType);
31 if (!AT) return 0;
32
33 // See if this is a string literal or @encode.
34 Init = Init->IgnoreParens();
35
36 // Handle @encode, which is a narrow string.
37 if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
38 return Init;
39
40 // Otherwise we can only handle string literals.
41 StringLiteral *SL = dyn_cast<StringLiteral>(Init);
42
43 // char array can be initialized with a narrow string.
44 // Only allow char x[] = "foo"; not char x[] = L"foo";
45 if (!SL->isWide())
46 return AT->getElementType()->isCharType() ? Init : 0;
47
48 // wchar_t array can be initialized with a wide string: C99 6.7.8p15:
49 // "An array with element type compatible with wchar_t may be initialized by a
50 // wide string literal, optionally enclosed in braces."
Chris Lattnerb1fe0472009-02-26 23:36:02 +000051 if (Context.typesAreCompatible(Context.getWCharType(), AT->getElementType()))
Chris Lattner7a7c1452009-02-26 23:26:43 +000052 // Only allow wchar_t x[] = L"foo"; not wchar_t x[] = "foo";
53 return Init;
54
Chris Lattnerd3a00502009-02-24 22:27:37 +000055 return 0;
56}
57
Chris Lattner160da072009-02-24 22:46:58 +000058static bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
59 bool DirectInit, Sema &S) {
Chris Lattnerd3a00502009-02-24 22:27:37 +000060 // Get the type before calling CheckSingleAssignmentConstraints(), since
61 // it can promote the expression.
62 QualType InitType = Init->getType();
63
Chris Lattner160da072009-02-24 22:46:58 +000064 if (S.getLangOptions().CPlusPlus) {
Chris Lattnerd3a00502009-02-24 22:27:37 +000065 // FIXME: I dislike this error message. A lot.
Chris Lattner160da072009-02-24 22:46:58 +000066 if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit))
67 return S.Diag(Init->getSourceRange().getBegin(),
68 diag::err_typecheck_convert_incompatible)
69 << DeclType << Init->getType() << "initializing"
70 << Init->getSourceRange();
Chris Lattnerd3a00502009-02-24 22:27:37 +000071 return false;
72 }
73
Chris Lattner160da072009-02-24 22:46:58 +000074 Sema::AssignConvertType ConvTy =
75 S.CheckSingleAssignmentConstraints(DeclType, Init);
76 return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
Chris Lattnerd3a00502009-02-24 22:27:37 +000077 InitType, Init, "initializing");
78}
79
Chris Lattner19ae2fc2009-02-24 23:10:27 +000080static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
81 // Get the length of the string as parsed.
82 uint64_t StrLength =
83 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
84
Chris Lattnerd3a00502009-02-24 22:27:37 +000085
Chris Lattner19ae2fc2009-02-24 23:10:27 +000086 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattnerd3a00502009-02-24 22:27:37 +000087 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
88 // C99 6.7.8p14. We have an array of character type with unknown size
89 // being initialized to a string literal.
90 llvm::APSInt ConstVal(32);
Chris Lattnerd20fac42009-02-24 23:01:39 +000091 ConstVal = StrLength;
Chris Lattnerd3a00502009-02-24 22:27:37 +000092 // Return a new array type (C99 6.7.8p22).
Chris Lattner45d6fd62009-02-24 22:41:04 +000093 DeclT = S.Context.getConstantArrayType(IAT->getElementType(), ConstVal,
94 ArrayType::Normal, 0);
Chris Lattnerd20fac42009-02-24 23:01:39 +000095 return;
Chris Lattnerd3a00502009-02-24 22:27:37 +000096 }
Chris Lattnerd20fac42009-02-24 23:01:39 +000097
98 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
99
100 // C99 6.7.8p14. We have an array of character type with known size. However,
101 // the size may be smaller or larger than the string we are initializing.
102 // FIXME: Avoid truncation for 64-bit length strings.
Chris Lattner19ae2fc2009-02-24 23:10:27 +0000103 if (StrLength-1 > CAT->getSize().getZExtValue())
Chris Lattnerd20fac42009-02-24 23:01:39 +0000104 S.Diag(Str->getSourceRange().getBegin(),
105 diag::warn_initializer_string_for_char_array_too_long)
106 << Str->getSourceRange();
107
108 // Set the type to the actual size that we are initializing. If we have
109 // something like:
110 // char x[1] = "foo";
111 // then this will set the string literal's type to char[1].
Chris Lattner45d6fd62009-02-24 22:41:04 +0000112 Str->setType(DeclT);
Chris Lattnerd3a00502009-02-24 22:27:37 +0000113}
114
115bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
116 SourceLocation InitLoc,
117 DeclarationName InitEntity,
118 bool DirectInit) {
119 if (DeclType->isDependentType() || Init->isTypeDependent())
120 return false;
121
122 // C++ [dcl.init.ref]p1:
123 // A variable declared to be a T&, that is "reference to type T"
124 // (8.3.2), shall be initialized by an object, or function, of
125 // type T or by an object that can be converted into a T.
126 if (DeclType->isReferenceType())
127 return CheckReferenceInit(Init, DeclType, 0, false, DirectInit);
128
129 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
130 // of unknown size ("[]") or an object type that is not a variable array type.
131 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
132 return Diag(InitLoc, diag::err_variable_object_no_init)
133 << VAT->getSizeExpr()->getSourceRange();
134
135 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
136 if (!InitList) {
137 // FIXME: Handle wide strings
Chris Lattner19ae2fc2009-02-24 23:10:27 +0000138 if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
139 CheckStringInit(Str, DeclType, *this);
Chris Lattnerd20fac42009-02-24 23:01:39 +0000140 return false;
141 }
Chris Lattnerd3a00502009-02-24 22:27:37 +0000142
143 // C++ [dcl.init]p14:
144 // -- If the destination type is a (possibly cv-qualified) class
145 // type:
146 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
147 QualType DeclTypeC = Context.getCanonicalType(DeclType);
148 QualType InitTypeC = Context.getCanonicalType(Init->getType());
149
150 // -- If the initialization is direct-initialization, or if it is
151 // copy-initialization where the cv-unqualified version of the
152 // source type is the same class as, or a derived class of, the
153 // class of the destination, constructors are considered.
154 if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
155 IsDerivedFrom(InitTypeC, DeclTypeC)) {
156 CXXConstructorDecl *Constructor
157 = PerformInitializationByConstructor(DeclType, &Init, 1,
158 InitLoc, Init->getSourceRange(),
159 InitEntity,
160 DirectInit? IK_Direct : IK_Copy);
161 return Constructor == 0;
162 }
163
164 // -- Otherwise (i.e., for the remaining copy-initialization
165 // cases), user-defined conversion sequences that can
166 // convert from the source type to the destination type or
167 // (when a conversion function is used) to a derived class
168 // thereof are enumerated as described in 13.3.1.4, and the
169 // best one is chosen through overload resolution
170 // (13.3). If the conversion cannot be done or is
171 // ambiguous, the initialization is ill-formed. The
172 // function selected is called with the initializer
173 // expression as its argument; if the function is a
174 // constructor, the call initializes a temporary of the
175 // destination type.
176 // FIXME: We're pretending to do copy elision here; return to
177 // this when we have ASTs for such things.
178 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
179 return false;
180
181 if (InitEntity)
182 return Diag(InitLoc, diag::err_cannot_initialize_decl)
183 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
184 << Init->getType() << Init->getSourceRange();
185 else
186 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
187 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
188 << Init->getType() << Init->getSourceRange();
189 }
190
191 // C99 6.7.8p16.
192 if (DeclType->isArrayType())
193 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
194 << Init->getSourceRange();
195
Chris Lattner160da072009-02-24 22:46:58 +0000196 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Chris Lattnerd3a00502009-02-24 22:27:37 +0000197 }
198
199 bool hadError = CheckInitList(InitList, DeclType);
200 Init = InitList;
201 return hadError;
202}
203
204//===----------------------------------------------------------------------===//
205// Semantic checking for initializer lists.
206//===----------------------------------------------------------------------===//
207
Douglas Gregoraaa20962009-01-29 01:05:33 +0000208/// @brief Semantic checking for initializer lists.
209///
210/// The InitListChecker class contains a set of routines that each
211/// handle the initialization of a certain kind of entity, e.g.,
212/// arrays, vectors, struct/union types, scalars, etc. The
213/// InitListChecker itself performs a recursive walk of the subobject
214/// structure of the type to be initialized, while stepping through
215/// the initializer list one element at a time. The IList and Index
216/// parameters to each of the Check* routines contain the active
217/// (syntactic) initializer list and the index into that initializer
218/// list that represents the current initializer. Each routine is
219/// responsible for moving that Index forward as it consumes elements.
220///
221/// Each Check* routine also has a StructuredList/StructuredIndex
222/// arguments, which contains the current the "structured" (semantic)
223/// initializer list and the index into that initializer list where we
224/// are copying initializers as we map them over to the semantic
225/// list. Once we have completed our recursive walk of the subobject
226/// structure, we will have constructed a full semantic initializer
227/// list.
228///
229/// C99 designators cause changes in the initializer list traversal,
230/// because they make the initialization "jump" into a specific
231/// subobject and then continue the initialization from that
232/// point. CheckDesignatedInitializer() recursively steps into the
233/// designated subobject and manages backing out the recursion to
234/// initialize the subobjects after the one designated.
Chris Lattnere76e9bf2009-02-24 22:48:58 +0000235namespace {
Douglas Gregor849afc32009-01-29 00:45:39 +0000236class InitListChecker {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000237 Sema &SemaRef;
Douglas Gregor849afc32009-01-29 00:45:39 +0000238 bool hadError;
239 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
240 InitListExpr *FullyStructuredList;
241
242 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000243 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000244 unsigned &StructuredIndex,
245 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000246 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000247 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000248 unsigned &StructuredIndex,
249 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000250 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
251 bool SubobjectIsDesignatorContext,
252 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000253 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000254 unsigned &StructuredIndex,
255 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000256 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
257 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000258 InitListExpr *StructuredList,
259 unsigned &StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000260 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor849afc32009-01-29 00:45:39 +0000261 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000262 InitListExpr *StructuredList,
263 unsigned &StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000264 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
265 unsigned &Index,
266 InitListExpr *StructuredList,
267 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +0000268 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000269 InitListExpr *StructuredList,
270 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +0000271 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
272 RecordDecl::field_iterator Field,
273 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000274 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000275 unsigned &StructuredIndex,
276 bool TopLevelObject = false);
Douglas Gregor849afc32009-01-29 00:45:39 +0000277 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
278 llvm::APSInt elementIndex,
279 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +0000280 InitListExpr *StructuredList,
281 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +0000282 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
283 DesignatedInitExpr::designators_iterator D,
284 QualType &CurrentObjectType,
285 RecordDecl::field_iterator *NextField,
286 llvm::APSInt *NextElementIndex,
287 unsigned &Index,
288 InitListExpr *StructuredList,
289 unsigned &StructuredIndex,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000290 bool FinishSubobjectInit,
291 bool TopLevelObject);
Douglas Gregor849afc32009-01-29 00:45:39 +0000292 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
293 QualType CurrentObjectType,
294 InitListExpr *StructuredList,
295 unsigned StructuredIndex,
296 SourceRange InitRange);
Douglas Gregoraaa20962009-01-29 01:05:33 +0000297 void UpdateStructuredListElement(InitListExpr *StructuredList,
298 unsigned &StructuredIndex,
Douglas Gregor849afc32009-01-29 00:45:39 +0000299 Expr *expr);
300 int numArrayElements(QualType DeclType);
301 int numStructUnionElements(QualType DeclType);
Douglas Gregord45210d2009-01-30 22:09:00 +0000302
303 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregor849afc32009-01-29 00:45:39 +0000304public:
Chris Lattner2e2766a2009-02-24 22:50:46 +0000305 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregor849afc32009-01-29 00:45:39 +0000306 bool HadError() { return hadError; }
307
308 // @brief Retrieves the fully-structured initializer list used for
309 // semantic analysis and code generation.
310 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
311};
Chris Lattnere76e9bf2009-02-24 22:48:58 +0000312} // end anonymous namespace
Chris Lattner1aa25a72009-01-29 05:10:57 +0000313
Douglas Gregorf603b472009-01-28 21:54:33 +0000314/// Recursively replaces NULL values within the given initializer list
315/// with expressions that perform value-initialization of the
316/// appropriate type.
Douglas Gregord45210d2009-01-30 22:09:00 +0000317void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000318 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregord45210d2009-01-30 22:09:00 +0000319 "Should not have void type");
Douglas Gregor538a4c22009-02-02 17:43:21 +0000320 SourceLocation Loc = ILE->getSourceRange().getBegin();
321 if (ILE->getSyntacticForm())
322 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
323
Douglas Gregorf603b472009-01-28 21:54:33 +0000324 if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
325 unsigned Init = 0, NumInits = ILE->getNumInits();
326 for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(),
327 FieldEnd = RType->getDecl()->field_end();
328 Field != FieldEnd; ++Field) {
329 if (Field->isUnnamedBitfield())
330 continue;
331
Douglas Gregor538a4c22009-02-02 17:43:21 +0000332 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregord45210d2009-01-30 22:09:00 +0000333 if (Field->getType()->isReferenceType()) {
334 // C++ [dcl.init.aggr]p9:
335 // If an incomplete or empty initializer-list leaves a
336 // member of reference type uninitialized, the program is
337 // ill-formed.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000338 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregord45210d2009-01-30 22:09:00 +0000339 << Field->getType()
340 << ILE->getSyntacticForm()->getSourceRange();
Chris Lattner2e2766a2009-02-24 22:50:46 +0000341 SemaRef.Diag(Field->getLocation(),
Douglas Gregord45210d2009-01-30 22:09:00 +0000342 diag::note_uninit_reference_member);
343 hadError = true;
Douglas Gregor538a4c22009-02-02 17:43:21 +0000344 return;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000345 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregor538a4c22009-02-02 17:43:21 +0000346 hadError = true;
347 return;
Douglas Gregord45210d2009-01-30 22:09:00 +0000348 }
Douglas Gregor538a4c22009-02-02 17:43:21 +0000349
350 // FIXME: If value-initialization involves calling a
351 // constructor, should we make that call explicit in the
352 // representation (even when it means extending the
353 // initializer list)?
354 if (Init < NumInits && !hadError)
355 ILE->setInit(Init,
Chris Lattner2e2766a2009-02-24 22:50:46 +0000356 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Douglas Gregor538a4c22009-02-02 17:43:21 +0000357 } else if (InitListExpr *InnerILE
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000358 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord45210d2009-01-30 22:09:00 +0000359 FillInValueInitializations(InnerILE);
Douglas Gregorf603b472009-01-28 21:54:33 +0000360 ++Init;
Douglas Gregord45210d2009-01-30 22:09:00 +0000361
362 // Only look at the first initialization of a union.
363 if (RType->getDecl()->isUnion())
364 break;
Douglas Gregorf603b472009-01-28 21:54:33 +0000365 }
366
367 return;
368 }
369
370 QualType ElementType;
371
Douglas Gregor538a4c22009-02-02 17:43:21 +0000372 unsigned NumInits = ILE->getNumInits();
373 unsigned NumElements = NumInits;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000374 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000375 ElementType = AType->getElementType();
Douglas Gregor538a4c22009-02-02 17:43:21 +0000376 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
377 NumElements = CAType->getSize().getZExtValue();
378 } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000379 ElementType = VType->getElementType();
Douglas Gregor538a4c22009-02-02 17:43:21 +0000380 NumElements = VType->getNumElements();
381 } else
Douglas Gregorf603b472009-01-28 21:54:33 +0000382 ElementType = ILE->getType();
383
Douglas Gregor538a4c22009-02-02 17:43:21 +0000384 for (unsigned Init = 0; Init != NumElements; ++Init) {
385 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000386 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregor538a4c22009-02-02 17:43:21 +0000387 hadError = true;
388 return;
389 }
390
391 // FIXME: If value-initialization involves calling a
392 // constructor, should we make that call explicit in the
393 // representation (even when it means extending the
394 // initializer list)?
395 if (Init < NumInits && !hadError)
396 ILE->setInit(Init,
Chris Lattner2e2766a2009-02-24 22:50:46 +0000397 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Douglas Gregor538a4c22009-02-02 17:43:21 +0000398 }
Chris Lattner1aa25a72009-01-29 05:10:57 +0000399 else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord45210d2009-01-30 22:09:00 +0000400 FillInValueInitializations(InnerILE);
Douglas Gregorf603b472009-01-28 21:54:33 +0000401 }
402}
403
Chris Lattner1aa25a72009-01-29 05:10:57 +0000404
Chris Lattner2e2766a2009-02-24 22:50:46 +0000405InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
406 : SemaRef(S) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000407 hadError = false;
Eli Friedmand8535af2008-05-19 20:00:43 +0000408
Eli Friedman683cedf2008-05-19 19:16:24 +0000409 unsigned newIndex = 0;
Douglas Gregorf603b472009-01-28 21:54:33 +0000410 unsigned newStructuredIndex = 0;
411 FullyStructuredList
412 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, SourceRange());
Douglas Gregorbe69b162009-02-04 22:46:25 +0000413 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
414 /*TopLevelObject=*/true);
Eli Friedmand8535af2008-05-19 20:00:43 +0000415
Douglas Gregord45210d2009-01-30 22:09:00 +0000416 if (!hadError)
417 FillInValueInitializations(FullyStructuredList);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000418}
419
420int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman46f81662008-05-25 13:22:35 +0000421 // FIXME: use a proper constant
422 int maxElements = 0x7FFFFFFF;
Chris Lattnera1923f62008-08-04 07:31:14 +0000423 if (const ConstantArrayType *CAT =
Chris Lattner2e2766a2009-02-24 22:50:46 +0000424 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000425 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
426 }
427 return maxElements;
428}
429
430int InitListChecker::numStructUnionElements(QualType DeclType) {
431 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Douglas Gregorf603b472009-01-28 21:54:33 +0000432 int InitializableMembers = 0;
433 for (RecordDecl::field_iterator Field = structDecl->field_begin(),
434 FieldEnd = structDecl->field_end();
435 Field != FieldEnd; ++Field) {
436 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
437 ++InitializableMembers;
438 }
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000439 if (structDecl->isUnion())
Eli Friedman9f5250b2008-05-25 14:03:31 +0000440 return std::min(InitializableMembers, 1);
441 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000442}
443
444void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregorf603b472009-01-28 21:54:33 +0000445 QualType T, unsigned &Index,
446 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000447 unsigned &StructuredIndex,
448 bool TopLevelObject) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000449 int maxElements = 0;
450
451 if (T->isArrayType())
452 maxElements = numArrayElements(T);
453 else if (T->isStructureType() || T->isUnionType())
454 maxElements = numStructUnionElements(T);
Eli Friedman683cedf2008-05-19 19:16:24 +0000455 else if (T->isVectorType())
456 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000457 else
458 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman683cedf2008-05-19 19:16:24 +0000459
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000460 if (maxElements == 0) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000461 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000462 diag::err_implicit_empty_initializer);
Douglas Gregorf603b472009-01-28 21:54:33 +0000463 ++Index;
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000464 hadError = true;
465 return;
466 }
467
Douglas Gregorf603b472009-01-28 21:54:33 +0000468 // Build a structured initializer list corresponding to this subobject.
469 InitListExpr *StructuredSubobjectInitList
470 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
471 StructuredIndex,
472 ParentIList->getInit(Index)->getSourceRange());
473 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedman683cedf2008-05-19 19:16:24 +0000474
Douglas Gregorf603b472009-01-28 21:54:33 +0000475 // Check the element types and build the structural subobject.
Douglas Gregor538a4c22009-02-02 17:43:21 +0000476 unsigned StartIndex = Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000477 CheckListElementTypes(ParentIList, T, false, Index,
478 StructuredSubobjectInitList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000479 StructuredSubobjectInitIndex,
480 TopLevelObject);
Douglas Gregor538a4c22009-02-02 17:43:21 +0000481 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
482
483 // Update the structured sub-object initialize so that it's ending
484 // range corresponds with the end of the last initializer it used.
485 if (EndIndex < ParentIList->getNumInits()) {
486 SourceLocation EndLoc
487 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
488 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
489 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000490}
491
Steve Naroff56099522008-05-06 00:23:44 +0000492void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregorf603b472009-01-28 21:54:33 +0000493 unsigned &Index,
494 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000495 unsigned &StructuredIndex,
496 bool TopLevelObject) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000497 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregorf603b472009-01-28 21:54:33 +0000498 SyntacticToSemantic[IList] = StructuredList;
499 StructuredList->setSyntacticForm(IList);
500 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000501 StructuredIndex, TopLevelObject);
Steve Naroff56099522008-05-06 00:23:44 +0000502 IList->setType(T);
Douglas Gregorf603b472009-01-28 21:54:33 +0000503 StructuredList->setType(T);
Eli Friedman46f81662008-05-25 13:22:35 +0000504 if (hadError)
505 return;
Eli Friedmand8535af2008-05-19 20:00:43 +0000506
Eli Friedman46f81662008-05-25 13:22:35 +0000507 if (Index < IList->getNumInits()) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000508 // We have leftover initializers
509 if (IList->getNumInits() > 0 &&
Chris Lattner2e2766a2009-02-24 22:50:46 +0000510 IsStringInit(IList->getInit(Index), T, SemaRef.Context)) {
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000511 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000512 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000513 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000514 // Special-case
Chris Lattner2e2766a2009-02-24 22:50:46 +0000515 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000516 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8535af2008-05-19 20:00:43 +0000517 hadError = true;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000518 } else if (!T->isIncompleteType()) {
Douglas Gregor09f078c2009-01-30 22:26:29 +0000519 // Don't complain for incomplete types, since we'll get an error
520 // elsewhere
Douglas Gregorbe69b162009-02-04 22:46:25 +0000521 QualType CurrentObjectType = StructuredList->getType();
522 int initKind =
523 CurrentObjectType->isArrayType()? 0 :
524 CurrentObjectType->isVectorType()? 1 :
525 CurrentObjectType->isScalarType()? 2 :
526 CurrentObjectType->isUnionType()? 3 :
527 4;
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000528
529 unsigned DK = diag::warn_excess_initializers;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000530 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregorc25bf6d2009-02-18 22:23:55 +0000531 DK = diag::err_excess_initializers;
532
Chris Lattner2e2766a2009-02-24 22:50:46 +0000533 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregorbe69b162009-02-04 22:46:25 +0000534 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmand8535af2008-05-19 20:00:43 +0000535 }
536 }
Eli Friedman455f7622008-05-19 20:20:43 +0000537
Eli Friedman46f81662008-05-25 13:22:35 +0000538 if (T->isScalarType())
Chris Lattner2e2766a2009-02-24 22:50:46 +0000539 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000540 << IList->getSourceRange();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000541}
542
Eli Friedman683cedf2008-05-19 19:16:24 +0000543void InitListChecker::CheckListElementTypes(InitListExpr *IList,
544 QualType &DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000545 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000546 unsigned &Index,
547 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000548 unsigned &StructuredIndex,
549 bool TopLevelObject) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000550 if (DeclType->isScalarType()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000551 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmand8535af2008-05-19 20:00:43 +0000552 } else if (DeclType->isVectorType()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000553 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregore7ef5002009-01-30 17:31:00 +0000554 } else if (DeclType->isAggregateType()) {
555 if (DeclType->isRecordType()) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000556 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
557 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000558 SubobjectIsDesignatorContext, Index,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000559 StructuredList, StructuredIndex,
560 TopLevelObject);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000561 } else if (DeclType->isArrayType()) {
Douglas Gregor5a203a62009-01-23 16:54:12 +0000562 llvm::APSInt Zero(
Chris Lattner2e2766a2009-02-24 22:50:46 +0000563 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregor5a203a62009-01-23 16:54:12 +0000564 false);
Douglas Gregorf603b472009-01-28 21:54:33 +0000565 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
566 StructuredList, StructuredIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000567 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000568 else
Douglas Gregorf603b472009-01-28 21:54:33 +0000569 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroffff5b3a82008-08-10 16:05:48 +0000570 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
571 // This type is invalid, issue a diagnostic.
Douglas Gregorf603b472009-01-28 21:54:33 +0000572 ++Index;
Chris Lattner2e2766a2009-02-24 22:50:46 +0000573 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000574 << DeclType;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000575 hadError = true;
Douglas Gregord45210d2009-01-30 22:09:00 +0000576 } else if (DeclType->isRecordType()) {
577 // C++ [dcl.init]p14:
578 // [...] If the class is an aggregate (8.5.1), and the initializer
579 // is a brace-enclosed list, see 8.5.1.
580 //
581 // Note: 8.5.1 is handled below; here, we diagnose the case where
582 // we have an initializer list and a destination type that is not
583 // an aggregate.
584 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000585 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord45210d2009-01-30 22:09:00 +0000586 << DeclType << IList->getSourceRange();
587 hadError = true;
588 } else if (DeclType->isReferenceType()) {
589 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000590 } else {
591 // In C, all types are either scalars or aggregates, but
592 // additional handling is needed here for C++ (and possibly others?).
593 assert(0 && "Unsupported initializer type");
594 }
595}
596
Eli Friedman683cedf2008-05-19 19:16:24 +0000597void InitListChecker::CheckSubElementType(InitListExpr *IList,
598 QualType ElemType,
Douglas Gregorf603b472009-01-28 21:54:33 +0000599 unsigned &Index,
600 InitListExpr *StructuredList,
601 unsigned &StructuredIndex) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000602 Expr *expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000603 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
604 unsigned newIndex = 0;
Douglas Gregorf603b472009-01-28 21:54:33 +0000605 unsigned newStructuredIndex = 0;
606 InitListExpr *newStructuredList
607 = getStructuredSubobjectInit(IList, Index, ElemType,
608 StructuredList, StructuredIndex,
609 SubInitList->getSourceRange());
610 CheckExplicitInitList(SubInitList, ElemType, newIndex,
611 newStructuredList, newStructuredIndex);
612 ++StructuredIndex;
613 ++Index;
Chris Lattner19ae2fc2009-02-24 23:10:27 +0000614 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
615 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattner45d6fd62009-02-24 22:41:04 +0000616 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregorf603b472009-01-28 21:54:33 +0000617 ++Index;
Eli Friedmand8535af2008-05-19 20:00:43 +0000618 } else if (ElemType->isScalarType()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000619 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000620 } else if (ElemType->isReferenceType()) {
621 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedman683cedf2008-05-19 19:16:24 +0000622 } else {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000623 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregord45210d2009-01-30 22:09:00 +0000624 // C++ [dcl.init.aggr]p12:
625 // All implicit type conversions (clause 4) are considered when
626 // initializing the aggregate member with an ini- tializer from
627 // an initializer-list. If the initializer can initialize a
628 // member, the member is initialized. [...]
629 ImplicitConversionSequence ICS
Chris Lattner2e2766a2009-02-24 22:50:46 +0000630 = SemaRef.TryCopyInitialization(expr, ElemType);
Douglas Gregord45210d2009-01-30 22:09:00 +0000631 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000632 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregord45210d2009-01-30 22:09:00 +0000633 "initializing"))
634 hadError = true;
635 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
636 ++Index;
637 return;
638 }
639
640 // Fall through for subaggregate initialization
641 } else {
642 // C99 6.7.8p13:
643 //
644 // The initializer for a structure or union object that has
645 // automatic storage duration shall be either an initializer
646 // list as described below, or a single expression that has
647 // compatible structure or union type. In the latter case, the
648 // initial value of the object, including unnamed members, is
649 // that of the expression.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000650 QualType ExprType = SemaRef.Context.getCanonicalType(expr->getType());
651 QualType ElemTypeCanon = SemaRef.Context.getCanonicalType(ElemType);
652 if (SemaRef.Context.typesAreCompatible(ExprType.getUnqualifiedType(),
Douglas Gregord45210d2009-01-30 22:09:00 +0000653 ElemTypeCanon.getUnqualifiedType())) {
654 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
655 ++Index;
656 return;
657 }
658
659 // Fall through for subaggregate initialization
660 }
661
662 // C++ [dcl.init.aggr]p12:
663 //
664 // [...] Otherwise, if the member is itself a non-empty
665 // subaggregate, brace elision is assumed and the initializer is
666 // considered for the initialization of the first member of
667 // the subaggregate.
668 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
669 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
670 StructuredIndex);
671 ++StructuredIndex;
672 } else {
673 // We cannot initialize this element, so let
674 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000675 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregord45210d2009-01-30 22:09:00 +0000676 hadError = true;
677 ++Index;
678 ++StructuredIndex;
679 }
680 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000681}
682
Douglas Gregord45210d2009-01-30 22:09:00 +0000683void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor36859eb2009-01-29 00:39:20 +0000684 unsigned &Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000685 InitListExpr *StructuredList,
686 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000687 if (Index < IList->getNumInits()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000688 Expr *expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000689 if (isa<InitListExpr>(expr)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000690 SemaRef.Diag(IList->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000691 diag::err_many_braces_around_scalar_init)
692 << IList->getSourceRange();
Eli Friedman71de9eb2008-05-19 20:12:18 +0000693 hadError = true;
694 ++Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000695 ++StructuredIndex;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000696 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000697 } else if (isa<DesignatedInitExpr>(expr)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000698 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000699 diag::err_designator_for_scalar_init)
700 << DeclType << expr->getSourceRange();
701 hadError = true;
702 ++Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000703 ++StructuredIndex;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000704 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000705 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000706
Eli Friedmand8535af2008-05-19 20:00:43 +0000707 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000708 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedman71de9eb2008-05-19 20:12:18 +0000709 hadError = true; // types weren't compatible.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000710 else if (savExpr != expr) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000711 // The type was promoted, update initializer list.
Douglas Gregor36859eb2009-01-29 00:39:20 +0000712 IList->setInit(Index, expr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000713 }
Douglas Gregorf603b472009-01-28 21:54:33 +0000714 if (hadError)
715 ++StructuredIndex;
716 else
717 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000718 ++Index;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000719 } else {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000720 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000721 << IList->getSourceRange();
Eli Friedman71de9eb2008-05-19 20:12:18 +0000722 hadError = true;
Douglas Gregorf603b472009-01-28 21:54:33 +0000723 ++Index;
724 ++StructuredIndex;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000725 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000726 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000727}
728
Douglas Gregord45210d2009-01-30 22:09:00 +0000729void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
730 unsigned &Index,
731 InitListExpr *StructuredList,
732 unsigned &StructuredIndex) {
733 if (Index < IList->getNumInits()) {
734 Expr *expr = IList->getInit(Index);
735 if (isa<InitListExpr>(expr)) {
Chris Lattner2e2766a2009-02-24 22:50:46 +0000736 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord45210d2009-01-30 22:09:00 +0000737 << DeclType << IList->getSourceRange();
738 hadError = true;
739 ++Index;
740 ++StructuredIndex;
741 return;
742 }
743
744 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000745 if (SemaRef.CheckReferenceInit(expr, DeclType))
Douglas Gregord45210d2009-01-30 22:09:00 +0000746 hadError = true;
747 else if (savExpr != expr) {
748 // The type was promoted, update initializer list.
749 IList->setInit(Index, expr);
750 }
751 if (hadError)
752 ++StructuredIndex;
753 else
754 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
755 ++Index;
756 } else {
757 // FIXME: It would be wonderful if we could point at the actual
758 // member. In general, it would be useful to pass location
759 // information down the stack, so that we know the location (or
760 // decl) of the "current object" being initialized.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000761 SemaRef.Diag(IList->getLocStart(),
Douglas Gregord45210d2009-01-30 22:09:00 +0000762 diag::err_init_reference_member_uninitialized)
763 << DeclType
764 << IList->getSourceRange();
765 hadError = true;
766 ++Index;
767 ++StructuredIndex;
768 return;
769 }
770}
771
Steve Naroffc4d4a482008-05-01 22:18:59 +0000772void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregorf603b472009-01-28 21:54:33 +0000773 unsigned &Index,
774 InitListExpr *StructuredList,
775 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000776 if (Index < IList->getNumInits()) {
777 const VectorType *VT = DeclType->getAsVectorType();
778 int maxElements = VT->getNumElements();
779 QualType elementType = VT->getElementType();
780
781 for (int i = 0; i < maxElements; ++i) {
782 // Don't attempt to go past the end of the init list
783 if (Index >= IList->getNumInits())
784 break;
Douglas Gregor36859eb2009-01-29 00:39:20 +0000785 CheckSubElementType(IList, elementType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000786 StructuredList, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000787 }
788 }
789}
790
791void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000792 llvm::APSInt elementIndex,
793 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000794 unsigned &Index,
795 InitListExpr *StructuredList,
796 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000797 // Check for the special-case of initializing an array with a string.
798 if (Index < IList->getNumInits()) {
Chris Lattner19ae2fc2009-02-24 23:10:27 +0000799 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
800 SemaRef.Context)) {
801 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregorf603b472009-01-28 21:54:33 +0000802 // We place the string literal directly into the resulting
803 // initializer list. This is the only place where the structure
804 // of the structured initializer list doesn't match exactly,
805 // because doing so would involve allocating one character
806 // constant for each string.
Chris Lattner45d6fd62009-02-24 22:41:04 +0000807 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner2e2766a2009-02-24 22:50:46 +0000808 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000809 ++Index;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000810 return;
811 }
812 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000813 if (const VariableArrayType *VAT =
Chris Lattner2e2766a2009-02-24 22:50:46 +0000814 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman46f81662008-05-25 13:22:35 +0000815 // Check for VLAs; in standard C it would be possible to check this
816 // earlier, but I don't know where clang accepts VLAs (gcc accepts
817 // them in all sorts of strange places).
Chris Lattner2e2766a2009-02-24 22:50:46 +0000818 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000819 diag::err_variable_object_no_init)
820 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman46f81662008-05-25 13:22:35 +0000821 hadError = true;
Douglas Gregorf603b472009-01-28 21:54:33 +0000822 ++Index;
823 ++StructuredIndex;
Eli Friedman46f81662008-05-25 13:22:35 +0000824 return;
825 }
826
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000827 // We might know the maximum number of elements in advance.
Douglas Gregorf603b472009-01-28 21:54:33 +0000828 llvm::APSInt maxElements(elementIndex.getBitWidth(),
829 elementIndex.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000830 bool maxElementsKnown = false;
831 if (const ConstantArrayType *CAT =
Chris Lattner2e2766a2009-02-24 22:50:46 +0000832 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000833 maxElements = CAT->getSize();
Douglas Gregor5a203a62009-01-23 16:54:12 +0000834 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregor69722702009-01-23 18:58:42 +0000835 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000836 maxElementsKnown = true;
837 }
838
Chris Lattner2e2766a2009-02-24 22:50:46 +0000839 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnera1923f62008-08-04 07:31:14 +0000840 ->getElementType();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000841 while (Index < IList->getNumInits()) {
842 Expr *Init = IList->getInit(Index);
843 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000844 // If we're not the subobject that matches up with the '{' for
845 // the designator, we shouldn't be handling the
846 // designator. Return immediately.
847 if (!SubobjectIsDesignatorContext)
848 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000849
Douglas Gregor710f6d42009-01-22 23:26:18 +0000850 // Handle this designated initializer. elementIndex will be
851 // updated to be the next array element we'll initialize.
852 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000853 DeclType, 0, &elementIndex, Index,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000854 StructuredList, StructuredIndex, true,
855 false)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000856 hadError = true;
857 continue;
858 }
859
Douglas Gregor5a203a62009-01-23 16:54:12 +0000860 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
861 maxElements.extend(elementIndex.getBitWidth());
862 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
863 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregor69722702009-01-23 18:58:42 +0000864 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor5a203a62009-01-23 16:54:12 +0000865
Douglas Gregor710f6d42009-01-22 23:26:18 +0000866 // If the array is of incomplete type, keep track of the number of
867 // elements in the initializer.
868 if (!maxElementsKnown && elementIndex > maxElements)
869 maxElements = elementIndex;
870
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000871 continue;
872 }
873
874 // If we know the maximum number of elements, and we've already
875 // hit it, stop consuming elements in the initializer list.
876 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroffc4d4a482008-05-01 22:18:59 +0000877 break;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000878
879 // Check this element.
Douglas Gregor36859eb2009-01-29 00:39:20 +0000880 CheckSubElementType(IList, elementType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000881 StructuredList, StructuredIndex);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000882 ++elementIndex;
883
884 // If the array is of incomplete type, keep track of the number of
885 // elements in the initializer.
886 if (!maxElementsKnown && elementIndex > maxElements)
887 maxElements = elementIndex;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000888 }
889 if (DeclType->isIncompleteArrayType()) {
890 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000891 // be calculated here.
Douglas Gregor69722702009-01-23 18:58:42 +0000892 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000893 if (maxElements == Zero) {
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000894 // Sizing an array implicitly to zero is not allowed by ISO C,
895 // but is supported by GNU.
Chris Lattner2e2766a2009-02-24 22:50:46 +0000896 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000897 diag::ext_typecheck_zero_array_size);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000898 }
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000899
Chris Lattner2e2766a2009-02-24 22:50:46 +0000900 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000901 ArrayType::Normal, 0);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000902 }
903}
904
905void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
906 QualType DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000907 RecordDecl::field_iterator Field,
908 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000909 unsigned &Index,
910 InitListExpr *StructuredList,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000911 unsigned &StructuredIndex,
912 bool TopLevelObject) {
Eli Friedman683cedf2008-05-19 19:16:24 +0000913 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000914
Eli Friedman683cedf2008-05-19 19:16:24 +0000915 // If the record is invalid, some of it's members are invalid. To avoid
916 // confusion, we forgo checking the intializer for the entire record.
917 if (structDecl->isInvalidDecl()) {
918 hadError = true;
919 return;
920 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000921
922 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
923 // Value-initialize the first named member of the union.
924 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
925 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
926 Field != FieldEnd; ++Field) {
927 if (Field->getDeclName()) {
928 StructuredList->setInitializedFieldInUnion(*Field);
929 break;
930 }
931 }
932 return;
933 }
934
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000935 // If structDecl is a forward declaration, this loop won't do
936 // anything except look at designated initializers; That's okay,
937 // because an error should get printed out elsewhere. It might be
938 // worthwhile to skip over the rest of the initializer, though.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000939 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
Douglas Gregor710f6d42009-01-22 23:26:18 +0000940 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000941 bool InitializedSomething = false;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000942 while (Index < IList->getNumInits()) {
943 Expr *Init = IList->getInit(Index);
944
945 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000946 // If we're not the subobject that matches up with the '{' for
947 // the designator, we shouldn't be handling the
948 // designator. Return immediately.
949 if (!SubobjectIsDesignatorContext)
950 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000951
Douglas Gregor710f6d42009-01-22 23:26:18 +0000952 // Handle this designated initializer. Field will be updated to
953 // the next field that we'll be initializing.
954 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000955 DeclType, &Field, 0, Index,
Douglas Gregorbe69b162009-02-04 22:46:25 +0000956 StructuredList, StructuredIndex,
957 true, TopLevelObject))
Douglas Gregor710f6d42009-01-22 23:26:18 +0000958 hadError = true;
959
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000960 InitializedSomething = true;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000961 continue;
962 }
963
964 if (Field == FieldEnd) {
965 // We've run out of fields. We're done.
966 break;
967 }
968
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000969 // We've already initialized a member of a union. We're done.
970 if (InitializedSomething && DeclType->isUnionType())
971 break;
972
Douglas Gregor8acb7272008-12-11 16:49:14 +0000973 // If we've hit the flexible array member at the end, we're done.
974 if (Field->getType()->isIncompleteArrayType())
975 break;
976
Douglas Gregor82462762009-01-29 16:53:55 +0000977 if (Field->isUnnamedBitfield()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000978 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000979 ++Field;
Eli Friedman683cedf2008-05-19 19:16:24 +0000980 continue;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000981 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000982
Douglas Gregor36859eb2009-01-29 00:39:20 +0000983 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000984 StructuredList, StructuredIndex);
Douglas Gregor0ecc9e92009-02-12 19:00:39 +0000985 InitializedSomething = true;
Douglas Gregor82462762009-01-29 16:53:55 +0000986
987 if (DeclType->isUnionType()) {
988 // Initialize the first field within the union.
989 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor82462762009-01-29 16:53:55 +0000990 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000991
992 ++Field;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000993 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000994
Douglas Gregorbe69b162009-02-04 22:46:25 +0000995 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
996 Index >= IList->getNumInits() ||
997 !isa<InitListExpr>(IList->getInit(Index)))
998 return;
999
1000 // Handle GNU flexible array initializers.
1001 if (!TopLevelObject &&
1002 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001003 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001004 diag::err_flexible_array_init_nonempty)
1005 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001006 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001007 << *Field;
1008 hadError = true;
1009 }
1010
1011 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1012 StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +00001013}
Steve Naroffc4d4a482008-05-01 22:18:59 +00001014
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001015/// @brief Check the well-formedness of a C99 designated initializer.
1016///
1017/// Determines whether the designated initializer @p DIE, which
1018/// resides at the given @p Index within the initializer list @p
1019/// IList, is well-formed for a current object of type @p DeclType
1020/// (C99 6.7.8). The actual subobject that this designator refers to
1021/// within the current subobject is returned in either
Douglas Gregorf603b472009-01-28 21:54:33 +00001022/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001023///
1024/// @param IList The initializer list in which this designated
1025/// initializer occurs.
1026///
1027/// @param DIE The designated initializer and its initialization
1028/// expression.
1029///
1030/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1031/// into which the designation in @p DIE should refer.
1032///
Douglas Gregor710f6d42009-01-22 23:26:18 +00001033/// @param NextField If non-NULL and the first designator in @p DIE is
1034/// a field, this will be set to the field declaration corresponding
1035/// to the field named by the designator.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001036///
Douglas Gregor710f6d42009-01-22 23:26:18 +00001037/// @param NextElementIndex If non-NULL and the first designator in @p
1038/// DIE is an array designator or GNU array-range designator, this
1039/// will be set to the last index initialized by this designator.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001040///
1041/// @param Index Index into @p IList where the designated initializer
1042/// @p DIE occurs.
1043///
Douglas Gregorf603b472009-01-28 21:54:33 +00001044/// @param StructuredList The initializer list expression that
1045/// describes all of the subobject initializers in the order they'll
1046/// actually be initialized.
1047///
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001048/// @returns true if there was an error, false otherwise.
Douglas Gregor710f6d42009-01-22 23:26:18 +00001049bool
1050InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
1051 DesignatedInitExpr *DIE,
Chris Lattnerd20fac42009-02-24 23:01:39 +00001052 DesignatedInitExpr::designators_iterator D,
Douglas Gregor710f6d42009-01-22 23:26:18 +00001053 QualType &CurrentObjectType,
1054 RecordDecl::field_iterator *NextField,
1055 llvm::APSInt *NextElementIndex,
Douglas Gregorf603b472009-01-28 21:54:33 +00001056 unsigned &Index,
1057 InitListExpr *StructuredList,
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001058 unsigned &StructuredIndex,
Douglas Gregorbe69b162009-02-04 22:46:25 +00001059 bool FinishSubobjectInit,
1060 bool TopLevelObject) {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001061 if (D == DIE->designators_end()) {
1062 // Check the actual initialization for the designated object type.
1063 bool prevHadError = hadError;
Douglas Gregor36859eb2009-01-29 00:39:20 +00001064
1065 // Temporarily remove the designator expression from the
1066 // initializer list that the child calls see, so that we don't try
1067 // to re-process the designator.
1068 unsigned OldIndex = Index;
1069 IList->setInit(OldIndex, DIE->getInit());
1070
1071 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +00001072 StructuredList, StructuredIndex);
Douglas Gregor36859eb2009-01-29 00:39:20 +00001073
1074 // Restore the designated initializer expression in the syntactic
1075 // form of the initializer list.
1076 if (IList->getInit(OldIndex) != DIE->getInit())
1077 DIE->setInit(IList->getInit(OldIndex));
1078 IList->setInit(OldIndex, DIE);
1079
Douglas Gregor710f6d42009-01-22 23:26:18 +00001080 return hadError && !prevHadError;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001081 }
1082
Douglas Gregorf603b472009-01-28 21:54:33 +00001083 bool IsFirstDesignator = (D == DIE->designators_begin());
1084 assert((IsFirstDesignator || StructuredList) &&
1085 "Need a non-designated initializer list to start from");
1086
1087 // Determine the structural initializer list that corresponds to the
1088 // current subobject.
1089 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
1090 : getStructuredSubobjectInit(IList, Index, CurrentObjectType, StructuredList,
1091 StructuredIndex,
1092 SourceRange(D->getStartLocation(),
1093 DIE->getSourceRange().getEnd()));
1094 assert(StructuredList && "Expected a structured initializer list");
1095
Douglas Gregor710f6d42009-01-22 23:26:18 +00001096 if (D->isFieldDesignator()) {
1097 // C99 6.7.8p7:
1098 //
1099 // If a designator has the form
1100 //
1101 // . identifier
1102 //
1103 // then the current object (defined below) shall have
1104 // structure or union type and the identifier shall be the
1105 // name of a member of that type.
1106 const RecordType *RT = CurrentObjectType->getAsRecordType();
1107 if (!RT) {
1108 SourceLocation Loc = D->getDotLoc();
1109 if (Loc.isInvalid())
1110 Loc = D->getFieldLoc();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001111 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1112 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001113 ++Index;
1114 return true;
1115 }
1116
Douglas Gregorf603b472009-01-28 21:54:33 +00001117 // Note: we perform a linear search of the fields here, despite
1118 // the fact that we have a faster lookup method, because we always
1119 // need to compute the field's index.
Douglas Gregor710f6d42009-01-22 23:26:18 +00001120 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregorf603b472009-01-28 21:54:33 +00001121 unsigned FieldIndex = 0;
1122 RecordDecl::field_iterator Field = RT->getDecl()->field_begin(),
1123 FieldEnd = RT->getDecl()->field_end();
1124 for (; Field != FieldEnd; ++Field) {
1125 if (Field->isUnnamedBitfield())
1126 continue;
1127
1128 if (Field->getIdentifier() == FieldName)
1129 break;
1130
1131 ++FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001132 }
1133
Douglas Gregorf603b472009-01-28 21:54:33 +00001134 if (Field == FieldEnd) {
1135 // We did not find the field we're looking for. Produce a
1136 // suitable diagnostic and return a failure.
1137 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1138 if (Lookup.first == Lookup.second) {
1139 // Name lookup didn't find anything.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001140 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregorf603b472009-01-28 21:54:33 +00001141 << FieldName << CurrentObjectType;
1142 } else {
1143 // Name lookup found something, but it wasn't a field.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001144 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregorf603b472009-01-28 21:54:33 +00001145 << FieldName;
Chris Lattner2e2766a2009-02-24 22:50:46 +00001146 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001147 diag::note_field_designator_found);
1148 }
1149
1150 ++Index;
1151 return true;
1152 } else if (cast<RecordDecl>((*Field)->getDeclContext())
1153 ->isAnonymousStructOrUnion()) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001154 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_anon_class)
Douglas Gregorf603b472009-01-28 21:54:33 +00001155 << FieldName
1156 << (cast<RecordDecl>((*Field)->getDeclContext())->isUnion()? 2 :
Chris Lattner2e2766a2009-02-24 22:50:46 +00001157 (int)SemaRef.getLangOptions().CPlusPlus);
1158 SemaRef.Diag((*Field)->getLocation(), diag::note_field_designator_found);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001159 ++Index;
1160 return true;
1161 }
Douglas Gregorf603b472009-01-28 21:54:33 +00001162
1163 // All of the fields of a union are located at the same place in
1164 // the initializer list.
Douglas Gregor82462762009-01-29 16:53:55 +00001165 if (RT->getDecl()->isUnion()) {
Douglas Gregorf603b472009-01-28 21:54:33 +00001166 FieldIndex = 0;
Douglas Gregor82462762009-01-29 16:53:55 +00001167 StructuredList->setInitializedFieldInUnion(*Field);
1168 }
Douglas Gregorf603b472009-01-28 21:54:33 +00001169
Douglas Gregor710f6d42009-01-22 23:26:18 +00001170 // Update the designator with the field declaration.
Douglas Gregorf603b472009-01-28 21:54:33 +00001171 D->setField(*Field);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001172
Douglas Gregorf603b472009-01-28 21:54:33 +00001173 // Make sure that our non-designated initializer list has space
1174 // for a subobject corresponding to this field.
1175 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner2e2766a2009-02-24 22:50:46 +00001176 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregorf603b472009-01-28 21:54:33 +00001177
Douglas Gregorbe69b162009-02-04 22:46:25 +00001178 // This designator names a flexible array member.
1179 if (Field->getType()->isIncompleteArrayType()) {
1180 bool Invalid = false;
1181 DesignatedInitExpr::designators_iterator NextD = D;
1182 ++NextD;
1183 if (NextD != DIE->designators_end()) {
1184 // We can't designate an object within the flexible array
1185 // member (because GCC doesn't allow it).
Chris Lattner2e2766a2009-02-24 22:50:46 +00001186 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001187 diag::err_designator_into_flexible_array_member)
1188 << SourceRange(NextD->getStartLocation(),
1189 DIE->getSourceRange().getEnd());
Chris Lattner2e2766a2009-02-24 22:50:46 +00001190 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001191 << *Field;
1192 Invalid = true;
1193 }
1194
1195 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1196 // The initializer is not an initializer list.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001197 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001198 diag::err_flexible_array_init_needs_braces)
1199 << DIE->getInit()->getSourceRange();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001200 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001201 << *Field;
1202 Invalid = true;
1203 }
1204
1205 // Handle GNU flexible array initializers.
1206 if (!Invalid && !TopLevelObject &&
1207 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001208 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregorbe69b162009-02-04 22:46:25 +00001209 diag::err_flexible_array_init_nonempty)
1210 << DIE->getSourceRange().getBegin();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001211 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorbe69b162009-02-04 22:46:25 +00001212 << *Field;
1213 Invalid = true;
1214 }
1215
1216 if (Invalid) {
1217 ++Index;
1218 return true;
1219 }
1220
1221 // Initialize the array.
1222 bool prevHadError = hadError;
1223 unsigned newStructuredIndex = FieldIndex;
1224 unsigned OldIndex = Index;
1225 IList->setInit(Index, DIE->getInit());
1226 CheckSubElementType(IList, Field->getType(), Index,
1227 StructuredList, newStructuredIndex);
1228 IList->setInit(OldIndex, DIE);
1229 if (hadError && !prevHadError) {
1230 ++Field;
1231 ++FieldIndex;
1232 if (NextField)
1233 *NextField = Field;
1234 StructuredIndex = FieldIndex;
1235 return true;
1236 }
1237 } else {
1238 // Recurse to check later designated subobjects.
1239 QualType FieldType = (*Field)->getType();
1240 unsigned newStructuredIndex = FieldIndex;
1241 if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index,
1242 StructuredList, newStructuredIndex,
1243 true, false))
1244 return true;
1245 }
Douglas Gregor710f6d42009-01-22 23:26:18 +00001246
1247 // Find the position of the next field to be initialized in this
1248 // subobject.
Douglas Gregor710f6d42009-01-22 23:26:18 +00001249 ++Field;
Douglas Gregorf603b472009-01-28 21:54:33 +00001250 ++FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001251
1252 // If this the first designator, our caller will continue checking
1253 // the rest of this struct/class/union subobject.
1254 if (IsFirstDesignator) {
1255 if (NextField)
1256 *NextField = Field;
Douglas Gregorf603b472009-01-28 21:54:33 +00001257 StructuredIndex = FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001258 return false;
1259 }
1260
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001261 if (!FinishSubobjectInit)
1262 return false;
1263
Douglas Gregor710f6d42009-01-22 23:26:18 +00001264 // Check the remaining fields within this class/struct/union subobject.
1265 bool prevHadError = hadError;
Douglas Gregorf603b472009-01-28 21:54:33 +00001266 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1267 StructuredList, FieldIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001268 return hadError && !prevHadError;
1269 }
1270
1271 // C99 6.7.8p6:
1272 //
1273 // If a designator has the form
1274 //
1275 // [ constant-expression ]
1276 //
1277 // then the current object (defined below) shall have array
1278 // type and the expression shall be an integer constant
1279 // expression. If the array is of unknown size, any
1280 // nonnegative value is valid.
1281 //
1282 // Additionally, cope with the GNU extension that permits
1283 // designators of the form
1284 //
1285 // [ constant-expression ... constant-expression ]
Chris Lattner2e2766a2009-02-24 22:50:46 +00001286 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001287 if (!AT) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001288 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor710f6d42009-01-22 23:26:18 +00001289 << CurrentObjectType;
1290 ++Index;
1291 return true;
1292 }
1293
1294 Expr *IndexExpr = 0;
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001295 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1296 if (D->isArrayDesignator()) {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001297 IndexExpr = DIE->getArrayIndex(*D);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001298
1299 bool ConstExpr
Chris Lattner2e2766a2009-02-24 22:50:46 +00001300 = IndexExpr->isIntegerConstantExpr(DesignatedStartIndex, SemaRef.Context);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001301 assert(ConstExpr && "Expression must be constant"); (void)ConstExpr;
1302
1303 DesignatedEndIndex = DesignatedStartIndex;
1304 } else {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001305 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001306
1307 bool StartConstExpr
1308 = DIE->getArrayRangeStart(*D)->isIntegerConstantExpr(DesignatedStartIndex,
Chris Lattner2e2766a2009-02-24 22:50:46 +00001309 SemaRef.Context);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001310 assert(StartConstExpr && "Expression must be constant"); (void)StartConstExpr;
1311
1312 bool EndConstExpr
1313 = DIE->getArrayRangeEnd(*D)->isIntegerConstantExpr(DesignatedEndIndex,
Chris Lattner2e2766a2009-02-24 22:50:46 +00001314 SemaRef.Context);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001315 assert(EndConstExpr && "Expression must be constant"); (void)EndConstExpr;
1316
Douglas Gregor710f6d42009-01-22 23:26:18 +00001317 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001318
1319 if (DesignatedStartIndex.getZExtValue() != DesignatedEndIndex.getZExtValue())
Douglas Gregor9fddded2009-01-29 19:42:23 +00001320 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor710f6d42009-01-22 23:26:18 +00001321 }
1322
Douglas Gregor710f6d42009-01-22 23:26:18 +00001323 if (isa<ConstantArrayType>(AT)) {
1324 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001325 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1326 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1327 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1328 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1329 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001330 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor710f6d42009-01-22 23:26:18 +00001331 diag::err_array_designator_too_large)
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001332 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor710f6d42009-01-22 23:26:18 +00001333 << IndexExpr->getSourceRange();
1334 ++Index;
1335 return true;
1336 }
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001337 } else {
1338 // Make sure the bit-widths and signedness match.
1339 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1340 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
1341 else if (DesignatedStartIndex.getBitWidth() < DesignatedEndIndex.getBitWidth())
1342 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1343 DesignatedStartIndex.setIsUnsigned(true);
1344 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001345 }
1346
Douglas Gregorf603b472009-01-28 21:54:33 +00001347 // Make sure that our non-designated initializer list has space
1348 // for a subobject corresponding to this array element.
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001349 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Chris Lattner2e2766a2009-02-24 22:50:46 +00001350 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001351 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregorf603b472009-01-28 21:54:33 +00001352
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001353 // Repeatedly perform subobject initializations in the range
1354 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor710f6d42009-01-22 23:26:18 +00001355
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001356 // Move to the next designator
1357 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1358 unsigned OldIndex = Index;
1359 ++D;
1360 while (DesignatedStartIndex <= DesignatedEndIndex) {
1361 // Recurse to check later designated subobjects.
1362 QualType ElementType = AT->getElementType();
1363 Index = OldIndex;
1364 if (CheckDesignatedInitializer(IList, DIE, D, ElementType, 0, 0, Index,
1365 StructuredList, ElementIndex,
Douglas Gregorbe69b162009-02-04 22:46:25 +00001366 (DesignatedStartIndex == DesignatedEndIndex),
1367 false))
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001368 return true;
1369
1370 // Move to the next index in the array that we'll be initializing.
1371 ++DesignatedStartIndex;
1372 ElementIndex = DesignatedStartIndex.getZExtValue();
1373 }
Douglas Gregor710f6d42009-01-22 23:26:18 +00001374
1375 // If this the first designator, our caller will continue checking
1376 // the rest of this array subobject.
1377 if (IsFirstDesignator) {
1378 if (NextElementIndex)
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001379 *NextElementIndex = DesignatedStartIndex;
Douglas Gregorf603b472009-01-28 21:54:33 +00001380 StructuredIndex = ElementIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001381 return false;
1382 }
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001383
1384 if (!FinishSubobjectInit)
1385 return false;
1386
Douglas Gregor710f6d42009-01-22 23:26:18 +00001387 // Check the remaining elements within this array subobject.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001388 bool prevHadError = hadError;
Douglas Gregord7e76c52009-02-09 19:45:19 +00001389 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +00001390 StructuredList, ElementIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001391 return hadError && !prevHadError;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001392}
1393
Douglas Gregorf603b472009-01-28 21:54:33 +00001394// Get the structured initializer list for a subobject of type
1395// @p CurrentObjectType.
1396InitListExpr *
1397InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1398 QualType CurrentObjectType,
1399 InitListExpr *StructuredList,
1400 unsigned StructuredIndex,
1401 SourceRange InitRange) {
1402 Expr *ExistingInit = 0;
1403 if (!StructuredList)
1404 ExistingInit = SyntacticToSemantic[IList];
1405 else if (StructuredIndex < StructuredList->getNumInits())
1406 ExistingInit = StructuredList->getInit(StructuredIndex);
1407
1408 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1409 return Result;
1410
1411 if (ExistingInit) {
1412 // We are creating an initializer list that initializes the
1413 // subobjects of the current object, but there was already an
1414 // initialization that completely initialized the current
1415 // subobject, e.g., by a compound literal:
1416 //
1417 // struct X { int a, b; };
1418 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
1419 //
1420 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1421 // designated initializer re-initializes the whole
1422 // subobject [0], overwriting previous initializers.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001423 SemaRef.Diag(InitRange.getBegin(), diag::warn_subobject_initializer_overrides)
Douglas Gregorf603b472009-01-28 21:54:33 +00001424 << InitRange;
Chris Lattner2e2766a2009-02-24 22:50:46 +00001425 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001426 diag::note_previous_initializer)
Douglas Gregor756283b2009-01-28 23:43:32 +00001427 << /*FIXME:has side effects=*/0
Douglas Gregorf603b472009-01-28 21:54:33 +00001428 << ExistingInit->getSourceRange();
1429 }
1430
Douglas Gregor538a4c22009-02-02 17:43:21 +00001431 SourceLocation StartLoc;
1432 if (Index < IList->getNumInits())
1433 StartLoc = IList->getInit(Index)->getSourceRange().getBegin();
Douglas Gregorf603b472009-01-28 21:54:33 +00001434 InitListExpr *Result
Chris Lattner2e2766a2009-02-24 22:50:46 +00001435 = new (SemaRef.Context) InitListExpr(StartLoc, 0, 0,
Douglas Gregor538a4c22009-02-02 17:43:21 +00001436 IList->getSourceRange().getEnd());
Douglas Gregorf603b472009-01-28 21:54:33 +00001437 Result->setType(CurrentObjectType);
1438
1439 // Link this new initializer list into the structured initializer
1440 // lists.
1441 if (StructuredList)
1442 StructuredList->updateInit(StructuredIndex, Result);
1443 else {
1444 Result->setSyntacticForm(IList);
1445 SyntacticToSemantic[IList] = Result;
1446 }
1447
1448 return Result;
1449}
1450
1451/// Update the initializer at index @p StructuredIndex within the
1452/// structured initializer list to the value @p expr.
1453void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1454 unsigned &StructuredIndex,
1455 Expr *expr) {
1456 // No structured initializer list to update
1457 if (!StructuredList)
1458 return;
1459
1460 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1461 // This initializer overwrites a previous initializer. Warn.
Chris Lattner2e2766a2009-02-24 22:50:46 +00001462 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001463 diag::warn_initializer_overrides)
1464 << expr->getSourceRange();
Chris Lattner2e2766a2009-02-24 22:50:46 +00001465 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregorf603b472009-01-28 21:54:33 +00001466 diag::note_previous_initializer)
Douglas Gregor756283b2009-01-28 23:43:32 +00001467 << /*FIXME:has side effects=*/0
Douglas Gregorf603b472009-01-28 21:54:33 +00001468 << PrevInit->getSourceRange();
1469 }
1470
1471 ++StructuredIndex;
1472}
1473
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001474/// Check that the given Index expression is a valid array designator
1475/// value. This is essentailly just a wrapper around
1476/// Expr::isIntegerConstantExpr that also checks for negative values
1477/// and produces a reasonable diagnostic if there is a
1478/// failure. Returns true if there was an error, false otherwise. If
1479/// everything went okay, Value will receive the value of the constant
1480/// expression.
1481static bool
1482CheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) {
1483 SourceLocation Loc = Index->getSourceRange().getBegin();
1484
1485 // Make sure this is an integer constant expression.
1486 if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc))
1487 return Self.Diag(Loc, diag::err_array_designator_nonconstant)
1488 << Index->getSourceRange();
1489
1490 // Make sure this constant expression is non-negative.
Douglas Gregor69722702009-01-23 18:58:42 +00001491 llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()),
1492 Value.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001493 if (Value < Zero)
1494 return Self.Diag(Loc, diag::err_array_designator_negative)
1495 << Value.toString(10) << Index->getSourceRange();
1496
Douglas Gregore498e372009-01-23 21:04:18 +00001497 Value.setIsUnsigned(true);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001498 return false;
1499}
1500
1501Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1502 SourceLocation Loc,
1503 bool UsedColonSyntax,
1504 OwningExprResult Init) {
1505 typedef DesignatedInitExpr::Designator ASTDesignator;
1506
1507 bool Invalid = false;
1508 llvm::SmallVector<ASTDesignator, 32> Designators;
1509 llvm::SmallVector<Expr *, 32> InitExpressions;
1510
1511 // Build designators and check array designator expressions.
1512 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1513 const Designator &D = Desig.getDesignator(Idx);
1514 switch (D.getKind()) {
1515 case Designator::FieldDesignator:
1516 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
1517 D.getFieldLoc()));
1518 break;
1519
1520 case Designator::ArrayDesignator: {
1521 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1522 llvm::APSInt IndexValue;
1523 if (CheckArrayDesignatorExpr(*this, Index, IndexValue))
1524 Invalid = true;
1525 else {
1526 Designators.push_back(ASTDesignator(InitExpressions.size(),
1527 D.getLBracketLoc(),
1528 D.getRBracketLoc()));
1529 InitExpressions.push_back(Index);
1530 }
1531 break;
1532 }
1533
1534 case Designator::ArrayRangeDesignator: {
1535 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1536 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1537 llvm::APSInt StartValue;
1538 llvm::APSInt EndValue;
1539 if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) ||
1540 CheckArrayDesignatorExpr(*this, EndIndex, EndValue))
1541 Invalid = true;
Douglas Gregorea0528d2009-01-23 22:22:29 +00001542 else {
1543 // Make sure we're comparing values with the same bit width.
1544 if (StartValue.getBitWidth() > EndValue.getBitWidth())
1545 EndValue.extend(StartValue.getBitWidth());
1546 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1547 StartValue.extend(EndValue.getBitWidth());
1548
1549 if (EndValue < StartValue) {
1550 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1551 << StartValue.toString(10) << EndValue.toString(10)
1552 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1553 Invalid = true;
1554 } else {
1555 Designators.push_back(ASTDesignator(InitExpressions.size(),
1556 D.getLBracketLoc(),
1557 D.getEllipsisLoc(),
1558 D.getRBracketLoc()));
1559 InitExpressions.push_back(StartIndex);
1560 InitExpressions.push_back(EndIndex);
1561 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001562 }
1563 break;
1564 }
1565 }
1566 }
1567
1568 if (Invalid || Init.isInvalid())
1569 return ExprError();
1570
1571 // Clear out the expressions within the designation.
1572 Desig.ClearExprs(*this);
1573
1574 DesignatedInitExpr *DIE
1575 = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(),
1576 &InitExpressions[0], InitExpressions.size(),
1577 Loc, UsedColonSyntax,
1578 static_cast<Expr *>(Init.release()));
1579 return Owned(DIE);
1580}
Douglas Gregor849afc32009-01-29 00:45:39 +00001581
1582bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattner2e2766a2009-02-24 22:50:46 +00001583 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregor849afc32009-01-29 00:45:39 +00001584 if (!CheckInitList.HadError())
1585 InitList = CheckInitList.getFullyStructuredList();
1586
1587 return CheckInitList.HadError();
1588}
Douglas Gregor538a4c22009-02-02 17:43:21 +00001589
1590/// \brief Diagnose any semantic errors with value-initialization of
1591/// the given type.
1592///
1593/// Value-initialization effectively zero-initializes any types
1594/// without user-declared constructors, and calls the default
1595/// constructor for a for any type that has a user-declared
1596/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1597/// a type with a user-declared constructor does not have an
1598/// accessible, non-deleted default constructor. In C, everything can
1599/// be value-initialized, which corresponds to C's notion of
1600/// initializing objects with static storage duration when no
1601/// initializer is provided for that object.
1602///
1603/// \returns true if there was an error, false otherwise.
1604bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1605 // C++ [dcl.init]p5:
1606 //
1607 // To value-initialize an object of type T means:
1608
1609 // -- if T is an array type, then each element is value-initialized;
1610 if (const ArrayType *AT = Context.getAsArrayType(Type))
1611 return CheckValueInitialization(AT->getElementType(), Loc);
1612
1613 if (const RecordType *RT = Type->getAsRecordType()) {
1614 if (const CXXRecordType *CXXRec = dyn_cast<CXXRecordType>(RT)) {
1615 // -- if T is a class type (clause 9) with a user-declared
1616 // constructor (12.1), then the default constructor for T is
1617 // called (and the initialization is ill-formed if T has no
1618 // accessible default constructor);
1619 if (CXXRec->getDecl()->hasUserDeclaredConstructor())
1620 // FIXME: Eventually, we'll need to put the constructor decl
1621 // into the AST.
1622 return PerformInitializationByConstructor(Type, 0, 0, Loc,
1623 SourceRange(Loc),
1624 DeclarationName(),
1625 IK_Direct);
1626 }
1627 }
1628
1629 if (Type->isReferenceType()) {
1630 // C++ [dcl.init]p5:
1631 // [...] A program that calls for default-initialization or
1632 // value-initialization of an entity of reference type is
1633 // ill-formed. [...]
Douglas Gregorbd4b0852009-02-02 21:35:47 +00001634 // FIXME: Once we have code that goes through this path, add an
1635 // actual diagnostic :)
Douglas Gregor538a4c22009-02-02 17:43:21 +00001636 }
1637
1638 return false;
1639}