blob: 3440d04f07bc988cda3a4192142a33c0cbfbd195 [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//
Douglas Gregoraaa20962009-01-29 01:05:33 +000010// This file implements semantic analysis for initializers. The entry
11// point is Sema::CheckInitList(), but all of the work is performed
12// within the InitListChecker class.
Steve Naroffc4d4a482008-05-01 22:18:59 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "Sema.h"
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000017#include "clang/Parse/Designator.h"
Steve Naroffc4d4a482008-05-01 22:18:59 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Expr.h"
Douglas Gregor849afc32009-01-29 00:45:39 +000020#include <map>
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +000021using namespace clang;
Steve Naroffc4d4a482008-05-01 22:18:59 +000022
Douglas Gregoraaa20962009-01-29 01:05:33 +000023/// @brief Semantic checking for initializer lists.
24///
25/// The InitListChecker class contains a set of routines that each
26/// handle the initialization of a certain kind of entity, e.g.,
27/// arrays, vectors, struct/union types, scalars, etc. The
28/// InitListChecker itself performs a recursive walk of the subobject
29/// structure of the type to be initialized, while stepping through
30/// the initializer list one element at a time. The IList and Index
31/// parameters to each of the Check* routines contain the active
32/// (syntactic) initializer list and the index into that initializer
33/// list that represents the current initializer. Each routine is
34/// responsible for moving that Index forward as it consumes elements.
35///
36/// Each Check* routine also has a StructuredList/StructuredIndex
37/// arguments, which contains the current the "structured" (semantic)
38/// initializer list and the index into that initializer list where we
39/// are copying initializers as we map them over to the semantic
40/// list. Once we have completed our recursive walk of the subobject
41/// structure, we will have constructed a full semantic initializer
42/// list.
43///
44/// C99 designators cause changes in the initializer list traversal,
45/// because they make the initialization "jump" into a specific
46/// subobject and then continue the initialization from that
47/// point. CheckDesignatedInitializer() recursively steps into the
48/// designated subobject and manages backing out the recursion to
49/// initialize the subobjects after the one designated.
Chris Lattner1aa25a72009-01-29 05:10:57 +000050namespace clang {
Douglas Gregor849afc32009-01-29 00:45:39 +000051class InitListChecker {
52 Sema *SemaRef;
53 bool hadError;
54 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
55 InitListExpr *FullyStructuredList;
56
57 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregoraaa20962009-01-29 01:05:33 +000058 unsigned &Index, InitListExpr *StructuredList,
59 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000060 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregoraaa20962009-01-29 01:05:33 +000061 unsigned &Index, InitListExpr *StructuredList,
62 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000063 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
64 bool SubobjectIsDesignatorContext,
65 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +000066 InitListExpr *StructuredList,
67 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000068 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
69 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +000070 InitListExpr *StructuredList,
71 unsigned &StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +000072 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor849afc32009-01-29 00:45:39 +000073 unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +000074 InitListExpr *StructuredList,
75 unsigned &StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +000076 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
77 unsigned &Index,
78 InitListExpr *StructuredList,
79 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000080 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +000081 InitListExpr *StructuredList,
82 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000083 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
84 RecordDecl::field_iterator Field,
85 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +000086 InitListExpr *StructuredList,
87 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000088 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
89 llvm::APSInt elementIndex,
90 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregoraaa20962009-01-29 01:05:33 +000091 InitListExpr *StructuredList,
92 unsigned &StructuredIndex);
Douglas Gregor849afc32009-01-29 00:45:39 +000093 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
94 DesignatedInitExpr::designators_iterator D,
95 QualType &CurrentObjectType,
96 RecordDecl::field_iterator *NextField,
97 llvm::APSInt *NextElementIndex,
98 unsigned &Index,
99 InitListExpr *StructuredList,
100 unsigned &StructuredIndex,
101 bool FinishSubobjectInit = true);
102 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
103 QualType CurrentObjectType,
104 InitListExpr *StructuredList,
105 unsigned StructuredIndex,
106 SourceRange InitRange);
Douglas Gregoraaa20962009-01-29 01:05:33 +0000107 void UpdateStructuredListElement(InitListExpr *StructuredList,
108 unsigned &StructuredIndex,
Douglas Gregor849afc32009-01-29 00:45:39 +0000109 Expr *expr);
110 int numArrayElements(QualType DeclType);
111 int numStructUnionElements(QualType DeclType);
Douglas Gregord45210d2009-01-30 22:09:00 +0000112
113 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregor849afc32009-01-29 00:45:39 +0000114public:
115 InitListChecker(Sema *S, InitListExpr *IL, QualType &T);
116 bool HadError() { return hadError; }
117
118 // @brief Retrieves the fully-structured initializer list used for
119 // semantic analysis and code generation.
120 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
121};
Chris Lattner1aa25a72009-01-29 05:10:57 +0000122}
123
Douglas Gregorf603b472009-01-28 21:54:33 +0000124/// Recursively replaces NULL values within the given initializer list
125/// with expressions that perform value-initialization of the
126/// appropriate type.
Douglas Gregord45210d2009-01-30 22:09:00 +0000127void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
128 assert((ILE->getType() != SemaRef->Context.VoidTy) &&
129 "Should not have void type");
Douglas Gregorf603b472009-01-28 21:54:33 +0000130 if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
131 unsigned Init = 0, NumInits = ILE->getNumInits();
132 for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(),
133 FieldEnd = RType->getDecl()->field_end();
134 Field != FieldEnd; ++Field) {
135 if (Field->isUnnamedBitfield())
136 continue;
137
Douglas Gregord45210d2009-01-30 22:09:00 +0000138 if (Init >= NumInits) {
139 if (Field->getType()->isReferenceType()) {
140 // C++ [dcl.init.aggr]p9:
141 // If an incomplete or empty initializer-list leaves a
142 // member of reference type uninitialized, the program is
143 // ill-formed.
144 SemaRef->Diag(ILE->getSyntacticForm()->getLocStart(),
145 diag::err_init_reference_member_uninitialized)
146 << Field->getType()
147 << ILE->getSyntacticForm()->getSourceRange();
148 SemaRef->Diag(Field->getLocation(),
149 diag::note_uninit_reference_member);
150 hadError = true;
151 }
152 } else if (!ILE->getInit(Init))
Douglas Gregorf603b472009-01-28 21:54:33 +0000153 ILE->setInit(Init,
Douglas Gregord45210d2009-01-30 22:09:00 +0000154 new (SemaRef->Context) ImplicitValueInitExpr(Field->getType()));
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000155 else if (InitListExpr *InnerILE
156 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord45210d2009-01-30 22:09:00 +0000157 FillInValueInitializations(InnerILE);
Douglas Gregorf603b472009-01-28 21:54:33 +0000158 ++Init;
Douglas Gregord45210d2009-01-30 22:09:00 +0000159
160 // Only look at the first initialization of a union.
161 if (RType->getDecl()->isUnion())
162 break;
Douglas Gregorf603b472009-01-28 21:54:33 +0000163 }
164
165 return;
166 }
167
168 QualType ElementType;
169
Douglas Gregord45210d2009-01-30 22:09:00 +0000170 if (const ArrayType *AType = SemaRef->Context.getAsArrayType(ILE->getType()))
Douglas Gregorf603b472009-01-28 21:54:33 +0000171 ElementType = AType->getElementType();
172 else if (const VectorType *VType = ILE->getType()->getAsVectorType())
173 ElementType = VType->getElementType();
174 else
175 ElementType = ILE->getType();
176
177 for (unsigned Init = 0, NumInits = ILE->getNumInits(); Init != NumInits;
178 ++Init) {
179 if (!ILE->getInit(Init))
Douglas Gregord45210d2009-01-30 22:09:00 +0000180 ILE->setInit(Init,
181 new (SemaRef->Context) ImplicitValueInitExpr(ElementType));
Chris Lattner1aa25a72009-01-29 05:10:57 +0000182 else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord45210d2009-01-30 22:09:00 +0000183 FillInValueInitializations(InnerILE);
Douglas Gregorf603b472009-01-28 21:54:33 +0000184 }
185}
186
Chris Lattner1aa25a72009-01-29 05:10:57 +0000187
Steve Naroffc4d4a482008-05-01 22:18:59 +0000188InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) {
189 hadError = false;
190 SemaRef = S;
Eli Friedmand8535af2008-05-19 20:00:43 +0000191
Eli Friedman683cedf2008-05-19 19:16:24 +0000192 unsigned newIndex = 0;
Douglas Gregorf603b472009-01-28 21:54:33 +0000193 unsigned newStructuredIndex = 0;
194 FullyStructuredList
195 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, SourceRange());
196 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex);
Eli Friedmand8535af2008-05-19 20:00:43 +0000197
Douglas Gregord45210d2009-01-30 22:09:00 +0000198 if (!hadError)
199 FillInValueInitializations(FullyStructuredList);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000200}
201
202int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman46f81662008-05-25 13:22:35 +0000203 // FIXME: use a proper constant
204 int maxElements = 0x7FFFFFFF;
Chris Lattnera1923f62008-08-04 07:31:14 +0000205 if (const ConstantArrayType *CAT =
206 SemaRef->Context.getAsConstantArrayType(DeclType)) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000207 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
208 }
209 return maxElements;
210}
211
212int InitListChecker::numStructUnionElements(QualType DeclType) {
213 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Douglas Gregorf603b472009-01-28 21:54:33 +0000214 int InitializableMembers = 0;
215 for (RecordDecl::field_iterator Field = structDecl->field_begin(),
216 FieldEnd = structDecl->field_end();
217 Field != FieldEnd; ++Field) {
218 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
219 ++InitializableMembers;
220 }
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000221 if (structDecl->isUnion())
Eli Friedman9f5250b2008-05-25 14:03:31 +0000222 return std::min(InitializableMembers, 1);
223 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000224}
225
226void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregorf603b472009-01-28 21:54:33 +0000227 QualType T, unsigned &Index,
228 InitListExpr *StructuredList,
229 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000230 int maxElements = 0;
231
232 if (T->isArrayType())
233 maxElements = numArrayElements(T);
234 else if (T->isStructureType() || T->isUnionType())
235 maxElements = numStructUnionElements(T);
Eli Friedman683cedf2008-05-19 19:16:24 +0000236 else if (T->isVectorType())
237 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000238 else
239 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman683cedf2008-05-19 19:16:24 +0000240
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000241 if (maxElements == 0) {
242 SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(),
243 diag::err_implicit_empty_initializer);
Douglas Gregorf603b472009-01-28 21:54:33 +0000244 ++Index;
Eli Friedmanf8df28c2008-05-25 13:49:22 +0000245 hadError = true;
246 return;
247 }
248
Douglas Gregorf603b472009-01-28 21:54:33 +0000249 // Build a structured initializer list corresponding to this subobject.
250 InitListExpr *StructuredSubobjectInitList
251 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
252 StructuredIndex,
253 ParentIList->getInit(Index)->getSourceRange());
254 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedman683cedf2008-05-19 19:16:24 +0000255
Douglas Gregorf603b472009-01-28 21:54:33 +0000256 // Check the element types and build the structural subobject.
257 CheckListElementTypes(ParentIList, T, false, Index,
258 StructuredSubobjectInitList,
259 StructuredSubobjectInitIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000260}
261
Steve Naroff56099522008-05-06 00:23:44 +0000262void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregorf603b472009-01-28 21:54:33 +0000263 unsigned &Index,
264 InitListExpr *StructuredList,
265 unsigned &StructuredIndex) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000266 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregorf603b472009-01-28 21:54:33 +0000267 SyntacticToSemantic[IList] = StructuredList;
268 StructuredList->setSyntacticForm(IList);
269 CheckListElementTypes(IList, T, true, Index, StructuredList,
270 StructuredIndex);
Steve Naroff56099522008-05-06 00:23:44 +0000271 IList->setType(T);
Douglas Gregorf603b472009-01-28 21:54:33 +0000272 StructuredList->setType(T);
Eli Friedman46f81662008-05-25 13:22:35 +0000273 if (hadError)
274 return;
Eli Friedmand8535af2008-05-19 20:00:43 +0000275
Eli Friedman46f81662008-05-25 13:22:35 +0000276 if (Index < IList->getNumInits()) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000277 // We have leftover initializers
278 if (IList->getNumInits() > 0 &&
279 SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000280 // Special-case
Eli Friedmand8535af2008-05-19 20:00:43 +0000281 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000282 diag::err_excess_initializers_in_char_array_initializer)
283 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8535af2008-05-19 20:00:43 +0000284 hadError = true;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000285 } else if (!T->isIncompleteType()) {
Douglas Gregor09f078c2009-01-30 22:26:29 +0000286 // Don't complain for incomplete types, since we'll get an error
287 // elsewhere
Eli Friedmand8535af2008-05-19 20:00:43 +0000288 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
Douglas Gregor09f078c2009-01-30 22:26:29 +0000289 diag::err_excess_initializers)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000290 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8535af2008-05-19 20:00:43 +0000291 }
292 }
Eli Friedman455f7622008-05-19 20:20:43 +0000293
Eli Friedman46f81662008-05-25 13:22:35 +0000294 if (T->isScalarType())
Chris Lattner9d2cf082008-11-19 05:27:50 +0000295 SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
296 << IList->getSourceRange();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000297}
298
Eli Friedman683cedf2008-05-19 19:16:24 +0000299void InitListChecker::CheckListElementTypes(InitListExpr *IList,
300 QualType &DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000301 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000302 unsigned &Index,
303 InitListExpr *StructuredList,
304 unsigned &StructuredIndex) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000305 if (DeclType->isScalarType()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000306 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmand8535af2008-05-19 20:00:43 +0000307 } else if (DeclType->isVectorType()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000308 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregore7ef5002009-01-30 17:31:00 +0000309 } else if (DeclType->isAggregateType()) {
310 if (DeclType->isRecordType()) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000311 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
312 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000313 SubobjectIsDesignatorContext, Index,
314 StructuredList, StructuredIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000315 } else if (DeclType->isArrayType()) {
Douglas Gregor5a203a62009-01-23 16:54:12 +0000316 llvm::APSInt Zero(
317 SemaRef->Context.getTypeSize(SemaRef->Context.getSizeType()),
318 false);
Douglas Gregorf603b472009-01-28 21:54:33 +0000319 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
320 StructuredList, StructuredIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000321 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000322 else
Douglas Gregorf603b472009-01-28 21:54:33 +0000323 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroffff5b3a82008-08-10 16:05:48 +0000324 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
325 // This type is invalid, issue a diagnostic.
Douglas Gregorf603b472009-01-28 21:54:33 +0000326 ++Index;
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000327 SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000328 << DeclType;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000329 hadError = true;
Douglas Gregord45210d2009-01-30 22:09:00 +0000330 } else if (DeclType->isRecordType()) {
331 // C++ [dcl.init]p14:
332 // [...] If the class is an aggregate (8.5.1), and the initializer
333 // is a brace-enclosed list, see 8.5.1.
334 //
335 // Note: 8.5.1 is handled below; here, we diagnose the case where
336 // we have an initializer list and a destination type that is not
337 // an aggregate.
338 // FIXME: In C++0x, this is yet another form of initialization.
339 SemaRef->Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
340 << DeclType << IList->getSourceRange();
341 hadError = true;
342 } else if (DeclType->isReferenceType()) {
343 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000344 } else {
345 // In C, all types are either scalars or aggregates, but
346 // additional handling is needed here for C++ (and possibly others?).
347 assert(0 && "Unsupported initializer type");
348 }
349}
350
Eli Friedman683cedf2008-05-19 19:16:24 +0000351void InitListChecker::CheckSubElementType(InitListExpr *IList,
352 QualType ElemType,
Douglas Gregorf603b472009-01-28 21:54:33 +0000353 unsigned &Index,
354 InitListExpr *StructuredList,
355 unsigned &StructuredIndex) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000356 Expr *expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000357 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
358 unsigned newIndex = 0;
Douglas Gregorf603b472009-01-28 21:54:33 +0000359 unsigned newStructuredIndex = 0;
360 InitListExpr *newStructuredList
361 = getStructuredSubobjectInit(IList, Index, ElemType,
362 StructuredList, StructuredIndex,
363 SubInitList->getSourceRange());
364 CheckExplicitInitList(SubInitList, ElemType, newIndex,
365 newStructuredList, newStructuredIndex);
366 ++StructuredIndex;
367 ++Index;
Eli Friedman683cedf2008-05-19 19:16:24 +0000368 } else if (StringLiteral *lit =
369 SemaRef->IsStringLiteralInit(expr, ElemType)) {
370 SemaRef->CheckStringLiteralInit(lit, ElemType);
Douglas Gregorf603b472009-01-28 21:54:33 +0000371 UpdateStructuredListElement(StructuredList, StructuredIndex, lit);
372 ++Index;
Eli Friedmand8535af2008-05-19 20:00:43 +0000373 } else if (ElemType->isScalarType()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000374 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregord45210d2009-01-30 22:09:00 +0000375 } else if (ElemType->isReferenceType()) {
376 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedman683cedf2008-05-19 19:16:24 +0000377 } else {
Douglas Gregord45210d2009-01-30 22:09:00 +0000378 if (SemaRef->getLangOptions().CPlusPlus) {
379 // C++ [dcl.init.aggr]p12:
380 // All implicit type conversions (clause 4) are considered when
381 // initializing the aggregate member with an ini- tializer from
382 // an initializer-list. If the initializer can initialize a
383 // member, the member is initialized. [...]
384 ImplicitConversionSequence ICS
385 = SemaRef->TryCopyInitialization(expr, ElemType);
386 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
387 if (SemaRef->PerformImplicitConversion(expr, ElemType, ICS,
388 "initializing"))
389 hadError = true;
390 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
391 ++Index;
392 return;
393 }
394
395 // Fall through for subaggregate initialization
396 } else {
397 // C99 6.7.8p13:
398 //
399 // The initializer for a structure or union object that has
400 // automatic storage duration shall be either an initializer
401 // list as described below, or a single expression that has
402 // compatible structure or union type. In the latter case, the
403 // initial value of the object, including unnamed members, is
404 // that of the expression.
405 QualType ExprType = SemaRef->Context.getCanonicalType(expr->getType());
406 QualType ElemTypeCanon = SemaRef->Context.getCanonicalType(ElemType);
407 if (SemaRef->Context.typesAreCompatible(ExprType.getUnqualifiedType(),
408 ElemTypeCanon.getUnqualifiedType())) {
409 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
410 ++Index;
411 return;
412 }
413
414 // Fall through for subaggregate initialization
415 }
416
417 // C++ [dcl.init.aggr]p12:
418 //
419 // [...] Otherwise, if the member is itself a non-empty
420 // subaggregate, brace elision is assumed and the initializer is
421 // considered for the initialization of the first member of
422 // the subaggregate.
423 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
424 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
425 StructuredIndex);
426 ++StructuredIndex;
427 } else {
428 // We cannot initialize this element, so let
429 // PerformCopyInitialization produce the appropriate diagnostic.
430 SemaRef->PerformCopyInitialization(expr, ElemType, "initializing");
431 hadError = true;
432 ++Index;
433 ++StructuredIndex;
434 }
435 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000436}
437
Douglas Gregord45210d2009-01-30 22:09:00 +0000438void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor36859eb2009-01-29 00:39:20 +0000439 unsigned &Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000440 InitListExpr *StructuredList,
441 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000442 if (Index < IList->getNumInits()) {
Douglas Gregor36859eb2009-01-29 00:39:20 +0000443 Expr *expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000444 if (isa<InitListExpr>(expr)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000445 SemaRef->Diag(IList->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000446 diag::err_many_braces_around_scalar_init)
447 << IList->getSourceRange();
Eli Friedman71de9eb2008-05-19 20:12:18 +0000448 hadError = true;
449 ++Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000450 ++StructuredIndex;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000451 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000452 } else if (isa<DesignatedInitExpr>(expr)) {
453 SemaRef->Diag(expr->getSourceRange().getBegin(),
454 diag::err_designator_for_scalar_init)
455 << DeclType << expr->getSourceRange();
456 hadError = true;
457 ++Index;
Douglas Gregorf603b472009-01-28 21:54:33 +0000458 ++StructuredIndex;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000459 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000460 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000461
Eli Friedmand8535af2008-05-19 20:00:43 +0000462 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Douglas Gregor6214d8a2009-01-14 15:45:31 +0000463 if (SemaRef->CheckSingleInitializer(expr, DeclType, false))
Eli Friedman71de9eb2008-05-19 20:12:18 +0000464 hadError = true; // types weren't compatible.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000465 else if (savExpr != expr) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000466 // The type was promoted, update initializer list.
Douglas Gregor36859eb2009-01-29 00:39:20 +0000467 IList->setInit(Index, expr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000468 }
Douglas Gregorf603b472009-01-28 21:54:33 +0000469 if (hadError)
470 ++StructuredIndex;
471 else
472 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000473 ++Index;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000474 } else {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000475 SemaRef->Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
476 << IList->getSourceRange();
Eli Friedman71de9eb2008-05-19 20:12:18 +0000477 hadError = true;
Douglas Gregorf603b472009-01-28 21:54:33 +0000478 ++Index;
479 ++StructuredIndex;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000480 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000481 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000482}
483
Douglas Gregord45210d2009-01-30 22:09:00 +0000484void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
485 unsigned &Index,
486 InitListExpr *StructuredList,
487 unsigned &StructuredIndex) {
488 if (Index < IList->getNumInits()) {
489 Expr *expr = IList->getInit(Index);
490 if (isa<InitListExpr>(expr)) {
491 SemaRef->Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
492 << DeclType << IList->getSourceRange();
493 hadError = true;
494 ++Index;
495 ++StructuredIndex;
496 return;
497 }
498
499 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
500 if (SemaRef->CheckReferenceInit(expr, DeclType))
501 hadError = true;
502 else if (savExpr != expr) {
503 // The type was promoted, update initializer list.
504 IList->setInit(Index, expr);
505 }
506 if (hadError)
507 ++StructuredIndex;
508 else
509 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
510 ++Index;
511 } else {
512 // FIXME: It would be wonderful if we could point at the actual
513 // member. In general, it would be useful to pass location
514 // information down the stack, so that we know the location (or
515 // decl) of the "current object" being initialized.
516 SemaRef->Diag(IList->getLocStart(),
517 diag::err_init_reference_member_uninitialized)
518 << DeclType
519 << IList->getSourceRange();
520 hadError = true;
521 ++Index;
522 ++StructuredIndex;
523 return;
524 }
525}
526
Steve Naroffc4d4a482008-05-01 22:18:59 +0000527void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregorf603b472009-01-28 21:54:33 +0000528 unsigned &Index,
529 InitListExpr *StructuredList,
530 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000531 if (Index < IList->getNumInits()) {
532 const VectorType *VT = DeclType->getAsVectorType();
533 int maxElements = VT->getNumElements();
534 QualType elementType = VT->getElementType();
535
536 for (int i = 0; i < maxElements; ++i) {
537 // Don't attempt to go past the end of the init list
538 if (Index >= IList->getNumInits())
539 break;
Douglas Gregor36859eb2009-01-29 00:39:20 +0000540 CheckSubElementType(IList, elementType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000541 StructuredList, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000542 }
543 }
544}
545
546void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000547 llvm::APSInt elementIndex,
548 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000549 unsigned &Index,
550 InitListExpr *StructuredList,
551 unsigned &StructuredIndex) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000552 // Check for the special-case of initializing an array with a string.
553 if (Index < IList->getNumInits()) {
554 if (StringLiteral *lit =
555 SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) {
556 SemaRef->CheckStringLiteralInit(lit, DeclType);
Douglas Gregorf603b472009-01-28 21:54:33 +0000557 // We place the string literal directly into the resulting
558 // initializer list. This is the only place where the structure
559 // of the structured initializer list doesn't match exactly,
560 // because doing so would involve allocating one character
561 // constant for each string.
562 UpdateStructuredListElement(StructuredList, StructuredIndex, lit);
563 StructuredList->resizeInits(SemaRef->Context, StructuredIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000564 ++Index;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000565 return;
566 }
567 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000568 if (const VariableArrayType *VAT =
569 SemaRef->Context.getAsVariableArrayType(DeclType)) {
Eli Friedman46f81662008-05-25 13:22:35 +0000570 // Check for VLAs; in standard C it would be possible to check this
571 // earlier, but I don't know where clang accepts VLAs (gcc accepts
572 // them in all sorts of strange places).
573 SemaRef->Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattner9d2cf082008-11-19 05:27:50 +0000574 diag::err_variable_object_no_init)
575 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman46f81662008-05-25 13:22:35 +0000576 hadError = true;
Douglas Gregorf603b472009-01-28 21:54:33 +0000577 ++Index;
578 ++StructuredIndex;
Eli Friedman46f81662008-05-25 13:22:35 +0000579 return;
580 }
581
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000582 // We might know the maximum number of elements in advance.
Douglas Gregorf603b472009-01-28 21:54:33 +0000583 llvm::APSInt maxElements(elementIndex.getBitWidth(),
584 elementIndex.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000585 bool maxElementsKnown = false;
586 if (const ConstantArrayType *CAT =
587 SemaRef->Context.getAsConstantArrayType(DeclType)) {
588 maxElements = CAT->getSize();
Douglas Gregor5a203a62009-01-23 16:54:12 +0000589 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregor69722702009-01-23 18:58:42 +0000590 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000591 maxElementsKnown = true;
592 }
593
Chris Lattnera1923f62008-08-04 07:31:14 +0000594 QualType elementType = SemaRef->Context.getAsArrayType(DeclType)
595 ->getElementType();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000596 while (Index < IList->getNumInits()) {
597 Expr *Init = IList->getInit(Index);
598 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000599 // If we're not the subobject that matches up with the '{' for
600 // the designator, we shouldn't be handling the
601 // designator. Return immediately.
602 if (!SubobjectIsDesignatorContext)
603 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000604
Douglas Gregor710f6d42009-01-22 23:26:18 +0000605 // Handle this designated initializer. elementIndex will be
606 // updated to be the next array element we'll initialize.
607 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000608 DeclType, 0, &elementIndex, Index,
609 StructuredList, StructuredIndex)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000610 hadError = true;
611 continue;
612 }
613
Douglas Gregor5a203a62009-01-23 16:54:12 +0000614 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
615 maxElements.extend(elementIndex.getBitWidth());
616 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
617 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregor69722702009-01-23 18:58:42 +0000618 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor5a203a62009-01-23 16:54:12 +0000619
Douglas Gregor710f6d42009-01-22 23:26:18 +0000620 // If the array is of incomplete type, keep track of the number of
621 // elements in the initializer.
622 if (!maxElementsKnown && elementIndex > maxElements)
623 maxElements = elementIndex;
624
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000625 continue;
626 }
627
628 // If we know the maximum number of elements, and we've already
629 // hit it, stop consuming elements in the initializer list.
630 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroffc4d4a482008-05-01 22:18:59 +0000631 break;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000632
633 // Check this element.
Douglas Gregor36859eb2009-01-29 00:39:20 +0000634 CheckSubElementType(IList, elementType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000635 StructuredList, StructuredIndex);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000636 ++elementIndex;
637
638 // If the array is of incomplete type, keep track of the number of
639 // elements in the initializer.
640 if (!maxElementsKnown && elementIndex > maxElements)
641 maxElements = elementIndex;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000642 }
643 if (DeclType->isIncompleteArrayType()) {
644 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000645 // be calculated here.
Douglas Gregor69722702009-01-23 18:58:42 +0000646 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000647 if (maxElements == Zero) {
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000648 // Sizing an array implicitly to zero is not allowed by ISO C,
649 // but is supported by GNU.
Steve Naroffc4d4a482008-05-01 22:18:59 +0000650 SemaRef->Diag(IList->getLocStart(),
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000651 diag::ext_typecheck_zero_array_size);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000652 }
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000653
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000654 DeclType = SemaRef->Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar604dacf2008-08-18 20:28:46 +0000655 ArrayType::Normal, 0);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000656 }
657}
658
659void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
660 QualType DeclType,
Douglas Gregor710f6d42009-01-22 23:26:18 +0000661 RecordDecl::field_iterator Field,
662 bool SubobjectIsDesignatorContext,
Douglas Gregorf603b472009-01-28 21:54:33 +0000663 unsigned &Index,
664 InitListExpr *StructuredList,
665 unsigned &StructuredIndex) {
Eli Friedman683cedf2008-05-19 19:16:24 +0000666 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000667
Eli Friedman683cedf2008-05-19 19:16:24 +0000668 // If the record is invalid, some of it's members are invalid. To avoid
669 // confusion, we forgo checking the intializer for the entire record.
670 if (structDecl->isInvalidDecl()) {
671 hadError = true;
672 return;
673 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000674
675 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
676 // Value-initialize the first named member of the union.
677 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
678 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
679 Field != FieldEnd; ++Field) {
680 if (Field->getDeclName()) {
681 StructuredList->setInitializedFieldInUnion(*Field);
682 break;
683 }
684 }
685 return;
686 }
687
688
689
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000690 // If structDecl is a forward declaration, this loop won't do
691 // anything except look at designated initializers; That's okay,
692 // because an error should get printed out elsewhere. It might be
693 // worthwhile to skip over the rest of the initializer, though.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000694 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
Douglas Gregor710f6d42009-01-22 23:26:18 +0000695 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000696 while (Index < IList->getNumInits()) {
697 Expr *Init = IList->getInit(Index);
698
699 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000700 // If we're not the subobject that matches up with the '{' for
701 // the designator, we shouldn't be handling the
702 // designator. Return immediately.
703 if (!SubobjectIsDesignatorContext)
704 return;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000705
Douglas Gregor710f6d42009-01-22 23:26:18 +0000706 // Handle this designated initializer. Field will be updated to
707 // the next field that we'll be initializing.
708 if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(),
Douglas Gregorf603b472009-01-28 21:54:33 +0000709 DeclType, &Field, 0, Index,
710 StructuredList, StructuredIndex))
Douglas Gregor710f6d42009-01-22 23:26:18 +0000711 hadError = true;
712
Douglas Gregor82462762009-01-29 16:53:55 +0000713 // Abort early for unions: the designator handled the
714 // initialization of the appropriate field.
715 if (DeclType->isUnionType())
716 break;
717
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000718 continue;
719 }
720
721 if (Field == FieldEnd) {
722 // We've run out of fields. We're done.
723 break;
724 }
725
Douglas Gregor8acb7272008-12-11 16:49:14 +0000726 // If we've hit the flexible array member at the end, we're done.
727 if (Field->getType()->isIncompleteArrayType())
728 break;
729
Douglas Gregor82462762009-01-29 16:53:55 +0000730 if (Field->isUnnamedBitfield()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000731 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000732 ++Field;
Eli Friedman683cedf2008-05-19 19:16:24 +0000733 continue;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000734 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000735
Douglas Gregor36859eb2009-01-29 00:39:20 +0000736 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000737 StructuredList, StructuredIndex);
Douglas Gregor82462762009-01-29 16:53:55 +0000738
739 if (DeclType->isUnionType()) {
740 // Initialize the first field within the union.
741 StructuredList->setInitializedFieldInUnion(*Field);
Eli Friedman683cedf2008-05-19 19:16:24 +0000742 break;
Douglas Gregor82462762009-01-29 16:53:55 +0000743 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000744
745 ++Field;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000746 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000747
Eli Friedman683cedf2008-05-19 19:16:24 +0000748 // FIXME: Implement flexible array initialization GCC extension (it's a
749 // really messy extension to implement, unfortunately...the necessary
750 // information isn't actually even here!)
Steve Naroffc4d4a482008-05-01 22:18:59 +0000751}
Steve Naroffc4d4a482008-05-01 22:18:59 +0000752
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000753/// @brief Check the well-formedness of a C99 designated initializer.
754///
755/// Determines whether the designated initializer @p DIE, which
756/// resides at the given @p Index within the initializer list @p
757/// IList, is well-formed for a current object of type @p DeclType
758/// (C99 6.7.8). The actual subobject that this designator refers to
759/// within the current subobject is returned in either
Douglas Gregorf603b472009-01-28 21:54:33 +0000760/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000761///
762/// @param IList The initializer list in which this designated
763/// initializer occurs.
764///
765/// @param DIE The designated initializer and its initialization
766/// expression.
767///
768/// @param DeclType The type of the "current object" (C99 6.7.8p17),
769/// into which the designation in @p DIE should refer.
770///
Douglas Gregor710f6d42009-01-22 23:26:18 +0000771/// @param NextField If non-NULL and the first designator in @p DIE is
772/// a field, this will be set to the field declaration corresponding
773/// to the field named by the designator.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000774///
Douglas Gregor710f6d42009-01-22 23:26:18 +0000775/// @param NextElementIndex If non-NULL and the first designator in @p
776/// DIE is an array designator or GNU array-range designator, this
777/// will be set to the last index initialized by this designator.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000778///
779/// @param Index Index into @p IList where the designated initializer
780/// @p DIE occurs.
781///
Douglas Gregorf603b472009-01-28 21:54:33 +0000782/// @param StructuredList The initializer list expression that
783/// describes all of the subobject initializers in the order they'll
784/// actually be initialized.
785///
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000786/// @returns true if there was an error, false otherwise.
Douglas Gregor710f6d42009-01-22 23:26:18 +0000787bool
788InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
789 DesignatedInitExpr *DIE,
790 DesignatedInitExpr::designators_iterator D,
791 QualType &CurrentObjectType,
792 RecordDecl::field_iterator *NextField,
793 llvm::APSInt *NextElementIndex,
Douglas Gregorf603b472009-01-28 21:54:33 +0000794 unsigned &Index,
795 InitListExpr *StructuredList,
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000796 unsigned &StructuredIndex,
797 bool FinishSubobjectInit) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000798 if (D == DIE->designators_end()) {
799 // Check the actual initialization for the designated object type.
800 bool prevHadError = hadError;
Douglas Gregor36859eb2009-01-29 00:39:20 +0000801
802 // Temporarily remove the designator expression from the
803 // initializer list that the child calls see, so that we don't try
804 // to re-process the designator.
805 unsigned OldIndex = Index;
806 IList->setInit(OldIndex, DIE->getInit());
807
808 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +0000809 StructuredList, StructuredIndex);
Douglas Gregor36859eb2009-01-29 00:39:20 +0000810
811 // Restore the designated initializer expression in the syntactic
812 // form of the initializer list.
813 if (IList->getInit(OldIndex) != DIE->getInit())
814 DIE->setInit(IList->getInit(OldIndex));
815 IList->setInit(OldIndex, DIE);
816
Douglas Gregor710f6d42009-01-22 23:26:18 +0000817 return hadError && !prevHadError;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000818 }
819
Douglas Gregorf603b472009-01-28 21:54:33 +0000820 bool IsFirstDesignator = (D == DIE->designators_begin());
821 assert((IsFirstDesignator || StructuredList) &&
822 "Need a non-designated initializer list to start from");
823
824 // Determine the structural initializer list that corresponds to the
825 // current subobject.
826 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
827 : getStructuredSubobjectInit(IList, Index, CurrentObjectType, StructuredList,
828 StructuredIndex,
829 SourceRange(D->getStartLocation(),
830 DIE->getSourceRange().getEnd()));
831 assert(StructuredList && "Expected a structured initializer list");
832
Douglas Gregor710f6d42009-01-22 23:26:18 +0000833 if (D->isFieldDesignator()) {
834 // C99 6.7.8p7:
835 //
836 // If a designator has the form
837 //
838 // . identifier
839 //
840 // then the current object (defined below) shall have
841 // structure or union type and the identifier shall be the
842 // name of a member of that type.
843 const RecordType *RT = CurrentObjectType->getAsRecordType();
844 if (!RT) {
845 SourceLocation Loc = D->getDotLoc();
846 if (Loc.isInvalid())
847 Loc = D->getFieldLoc();
848 SemaRef->Diag(Loc, diag::err_field_designator_non_aggr)
849 << SemaRef->getLangOptions().CPlusPlus << CurrentObjectType;
850 ++Index;
851 return true;
852 }
853
Douglas Gregorf603b472009-01-28 21:54:33 +0000854 // Note: we perform a linear search of the fields here, despite
855 // the fact that we have a faster lookup method, because we always
856 // need to compute the field's index.
Douglas Gregor710f6d42009-01-22 23:26:18 +0000857 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregorf603b472009-01-28 21:54:33 +0000858 unsigned FieldIndex = 0;
859 RecordDecl::field_iterator Field = RT->getDecl()->field_begin(),
860 FieldEnd = RT->getDecl()->field_end();
861 for (; Field != FieldEnd; ++Field) {
862 if (Field->isUnnamedBitfield())
863 continue;
864
865 if (Field->getIdentifier() == FieldName)
866 break;
867
868 ++FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +0000869 }
870
Douglas Gregorf603b472009-01-28 21:54:33 +0000871 if (Field == FieldEnd) {
872 // We did not find the field we're looking for. Produce a
873 // suitable diagnostic and return a failure.
874 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
875 if (Lookup.first == Lookup.second) {
876 // Name lookup didn't find anything.
877 SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
878 << FieldName << CurrentObjectType;
879 } else {
880 // Name lookup found something, but it wasn't a field.
881 SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
882 << FieldName;
883 SemaRef->Diag((*Lookup.first)->getLocation(),
884 diag::note_field_designator_found);
885 }
886
887 ++Index;
888 return true;
889 } else if (cast<RecordDecl>((*Field)->getDeclContext())
890 ->isAnonymousStructOrUnion()) {
891 SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_anon_class)
892 << FieldName
893 << (cast<RecordDecl>((*Field)->getDeclContext())->isUnion()? 2 :
894 (int)SemaRef->getLangOptions().CPlusPlus);
895 SemaRef->Diag((*Field)->getLocation(), diag::note_field_designator_found);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000896 ++Index;
897 return true;
898 }
Douglas Gregorf603b472009-01-28 21:54:33 +0000899
900 // All of the fields of a union are located at the same place in
901 // the initializer list.
Douglas Gregor82462762009-01-29 16:53:55 +0000902 if (RT->getDecl()->isUnion()) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000903 FieldIndex = 0;
Douglas Gregor82462762009-01-29 16:53:55 +0000904 StructuredList->setInitializedFieldInUnion(*Field);
905 }
Douglas Gregorf603b472009-01-28 21:54:33 +0000906
Douglas Gregor710f6d42009-01-22 23:26:18 +0000907 // Update the designator with the field declaration.
Douglas Gregorf603b472009-01-28 21:54:33 +0000908 D->setField(*Field);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000909
Douglas Gregorf603b472009-01-28 21:54:33 +0000910 // Make sure that our non-designated initializer list has space
911 // for a subobject corresponding to this field.
912 if (FieldIndex >= StructuredList->getNumInits())
913 StructuredList->resizeInits(SemaRef->Context, FieldIndex + 1);
914
Douglas Gregor710f6d42009-01-22 23:26:18 +0000915 // Recurse to check later designated subobjects.
Douglas Gregorf603b472009-01-28 21:54:33 +0000916 QualType FieldType = (*Field)->getType();
917 unsigned newStructuredIndex = FieldIndex;
918 if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index,
919 StructuredList, newStructuredIndex))
Douglas Gregor710f6d42009-01-22 23:26:18 +0000920 return true;
921
922 // Find the position of the next field to be initialized in this
923 // subobject.
Douglas Gregor710f6d42009-01-22 23:26:18 +0000924 ++Field;
Douglas Gregorf603b472009-01-28 21:54:33 +0000925 ++FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +0000926
927 // If this the first designator, our caller will continue checking
928 // the rest of this struct/class/union subobject.
929 if (IsFirstDesignator) {
930 if (NextField)
931 *NextField = Field;
Douglas Gregorf603b472009-01-28 21:54:33 +0000932 StructuredIndex = FieldIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +0000933 return false;
934 }
935
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000936 if (!FinishSubobjectInit)
937 return false;
938
Douglas Gregor710f6d42009-01-22 23:26:18 +0000939 // Check the remaining fields within this class/struct/union subobject.
940 bool prevHadError = hadError;
Douglas Gregorf603b472009-01-28 21:54:33 +0000941 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
942 StructuredList, FieldIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +0000943 return hadError && !prevHadError;
944 }
945
946 // C99 6.7.8p6:
947 //
948 // If a designator has the form
949 //
950 // [ constant-expression ]
951 //
952 // then the current object (defined below) shall have array
953 // type and the expression shall be an integer constant
954 // expression. If the array is of unknown size, any
955 // nonnegative value is valid.
956 //
957 // Additionally, cope with the GNU extension that permits
958 // designators of the form
959 //
960 // [ constant-expression ... constant-expression ]
961 const ArrayType *AT = SemaRef->Context.getAsArrayType(CurrentObjectType);
962 if (!AT) {
963 SemaRef->Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
964 << CurrentObjectType;
965 ++Index;
966 return true;
967 }
968
969 Expr *IndexExpr = 0;
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000970 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
971 if (D->isArrayDesignator()) {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000972 IndexExpr = DIE->getArrayIndex(*D);
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000973
974 bool ConstExpr
975 = IndexExpr->isIntegerConstantExpr(DesignatedStartIndex, SemaRef->Context);
976 assert(ConstExpr && "Expression must be constant"); (void)ConstExpr;
977
978 DesignatedEndIndex = DesignatedStartIndex;
979 } else {
Douglas Gregor710f6d42009-01-22 23:26:18 +0000980 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000981
982 bool StartConstExpr
983 = DIE->getArrayRangeStart(*D)->isIntegerConstantExpr(DesignatedStartIndex,
984 SemaRef->Context);
985 assert(StartConstExpr && "Expression must be constant"); (void)StartConstExpr;
986
987 bool EndConstExpr
988 = DIE->getArrayRangeEnd(*D)->isIntegerConstantExpr(DesignatedEndIndex,
989 SemaRef->Context);
990 assert(EndConstExpr && "Expression must be constant"); (void)EndConstExpr;
991
Douglas Gregor710f6d42009-01-22 23:26:18 +0000992 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor36dd0c52009-01-28 23:36:17 +0000993
994 if (DesignatedStartIndex.getZExtValue() != DesignatedEndIndex.getZExtValue())
Douglas Gregor9fddded2009-01-29 19:42:23 +0000995 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor710f6d42009-01-22 23:26:18 +0000996 }
997
Douglas Gregor710f6d42009-01-22 23:26:18 +0000998 if (isa<ConstantArrayType>(AT)) {
999 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001000 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1001 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1002 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1003 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1004 if (DesignatedEndIndex >= MaxElements) {
Douglas Gregor710f6d42009-01-22 23:26:18 +00001005 SemaRef->Diag(IndexExpr->getSourceRange().getBegin(),
1006 diag::err_array_designator_too_large)
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001007 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor710f6d42009-01-22 23:26:18 +00001008 << IndexExpr->getSourceRange();
1009 ++Index;
1010 return true;
1011 }
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001012 } else {
1013 // Make sure the bit-widths and signedness match.
1014 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1015 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
1016 else if (DesignatedStartIndex.getBitWidth() < DesignatedEndIndex.getBitWidth())
1017 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1018 DesignatedStartIndex.setIsUnsigned(true);
1019 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001020 }
1021
Douglas Gregorf603b472009-01-28 21:54:33 +00001022 // Make sure that our non-designated initializer list has space
1023 // for a subobject corresponding to this array element.
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001024 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
1025 StructuredList->resizeInits(SemaRef->Context,
1026 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregorf603b472009-01-28 21:54:33 +00001027
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001028 // Repeatedly perform subobject initializations in the range
1029 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor710f6d42009-01-22 23:26:18 +00001030
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001031 // Move to the next designator
1032 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1033 unsigned OldIndex = Index;
1034 ++D;
1035 while (DesignatedStartIndex <= DesignatedEndIndex) {
1036 // Recurse to check later designated subobjects.
1037 QualType ElementType = AT->getElementType();
1038 Index = OldIndex;
1039 if (CheckDesignatedInitializer(IList, DIE, D, ElementType, 0, 0, Index,
1040 StructuredList, ElementIndex,
1041 (DesignatedStartIndex == DesignatedEndIndex)))
1042 return true;
1043
1044 // Move to the next index in the array that we'll be initializing.
1045 ++DesignatedStartIndex;
1046 ElementIndex = DesignatedStartIndex.getZExtValue();
1047 }
Douglas Gregor710f6d42009-01-22 23:26:18 +00001048
1049 // If this the first designator, our caller will continue checking
1050 // the rest of this array subobject.
1051 if (IsFirstDesignator) {
1052 if (NextElementIndex)
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001053 *NextElementIndex = DesignatedStartIndex;
Douglas Gregorf603b472009-01-28 21:54:33 +00001054 StructuredIndex = ElementIndex;
Douglas Gregor710f6d42009-01-22 23:26:18 +00001055 return false;
1056 }
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001057
1058 if (!FinishSubobjectInit)
1059 return false;
1060
Douglas Gregor710f6d42009-01-22 23:26:18 +00001061 // Check the remaining elements within this array subobject.
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001062 bool prevHadError = hadError;
Douglas Gregor36dd0c52009-01-28 23:36:17 +00001063 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, true, Index,
Douglas Gregorf603b472009-01-28 21:54:33 +00001064 StructuredList, ElementIndex);
Douglas Gregor710f6d42009-01-22 23:26:18 +00001065 return hadError && !prevHadError;
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001066}
1067
Douglas Gregorf603b472009-01-28 21:54:33 +00001068// Get the structured initializer list for a subobject of type
1069// @p CurrentObjectType.
1070InitListExpr *
1071InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1072 QualType CurrentObjectType,
1073 InitListExpr *StructuredList,
1074 unsigned StructuredIndex,
1075 SourceRange InitRange) {
1076 Expr *ExistingInit = 0;
1077 if (!StructuredList)
1078 ExistingInit = SyntacticToSemantic[IList];
1079 else if (StructuredIndex < StructuredList->getNumInits())
1080 ExistingInit = StructuredList->getInit(StructuredIndex);
1081
1082 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1083 return Result;
1084
1085 if (ExistingInit) {
1086 // We are creating an initializer list that initializes the
1087 // subobjects of the current object, but there was already an
1088 // initialization that completely initialized the current
1089 // subobject, e.g., by a compound literal:
1090 //
1091 // struct X { int a, b; };
1092 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
1093 //
1094 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1095 // designated initializer re-initializes the whole
1096 // subobject [0], overwriting previous initializers.
1097 SemaRef->Diag(InitRange.getBegin(), diag::warn_subobject_initializer_overrides)
1098 << InitRange;
1099 SemaRef->Diag(ExistingInit->getSourceRange().getBegin(),
1100 diag::note_previous_initializer)
Douglas Gregor756283b2009-01-28 23:43:32 +00001101 << /*FIXME:has side effects=*/0
Douglas Gregorf603b472009-01-28 21:54:33 +00001102 << ExistingInit->getSourceRange();
1103 }
1104
1105 InitListExpr *Result
1106 = new (SemaRef->Context) InitListExpr(SourceLocation(), 0, 0,
1107 SourceLocation());
1108 Result->setType(CurrentObjectType);
1109
1110 // Link this new initializer list into the structured initializer
1111 // lists.
1112 if (StructuredList)
1113 StructuredList->updateInit(StructuredIndex, Result);
1114 else {
1115 Result->setSyntacticForm(IList);
1116 SyntacticToSemantic[IList] = Result;
1117 }
1118
1119 return Result;
1120}
1121
1122/// Update the initializer at index @p StructuredIndex within the
1123/// structured initializer list to the value @p expr.
1124void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1125 unsigned &StructuredIndex,
1126 Expr *expr) {
1127 // No structured initializer list to update
1128 if (!StructuredList)
1129 return;
1130
1131 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1132 // This initializer overwrites a previous initializer. Warn.
1133 SemaRef->Diag(expr->getSourceRange().getBegin(),
1134 diag::warn_initializer_overrides)
1135 << expr->getSourceRange();
1136 SemaRef->Diag(PrevInit->getSourceRange().getBegin(),
1137 diag::note_previous_initializer)
Douglas Gregor756283b2009-01-28 23:43:32 +00001138 << /*FIXME:has side effects=*/0
Douglas Gregorf603b472009-01-28 21:54:33 +00001139 << PrevInit->getSourceRange();
1140 }
1141
1142 ++StructuredIndex;
1143}
1144
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001145/// Check that the given Index expression is a valid array designator
1146/// value. This is essentailly just a wrapper around
1147/// Expr::isIntegerConstantExpr that also checks for negative values
1148/// and produces a reasonable diagnostic if there is a
1149/// failure. Returns true if there was an error, false otherwise. If
1150/// everything went okay, Value will receive the value of the constant
1151/// expression.
1152static bool
1153CheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) {
1154 SourceLocation Loc = Index->getSourceRange().getBegin();
1155
1156 // Make sure this is an integer constant expression.
1157 if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc))
1158 return Self.Diag(Loc, diag::err_array_designator_nonconstant)
1159 << Index->getSourceRange();
1160
1161 // Make sure this constant expression is non-negative.
Douglas Gregor69722702009-01-23 18:58:42 +00001162 llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()),
1163 Value.isUnsigned());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001164 if (Value < Zero)
1165 return Self.Diag(Loc, diag::err_array_designator_negative)
1166 << Value.toString(10) << Index->getSourceRange();
1167
Douglas Gregore498e372009-01-23 21:04:18 +00001168 Value.setIsUnsigned(true);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001169 return false;
1170}
1171
1172Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1173 SourceLocation Loc,
1174 bool UsedColonSyntax,
1175 OwningExprResult Init) {
1176 typedef DesignatedInitExpr::Designator ASTDesignator;
1177
1178 bool Invalid = false;
1179 llvm::SmallVector<ASTDesignator, 32> Designators;
1180 llvm::SmallVector<Expr *, 32> InitExpressions;
1181
1182 // Build designators and check array designator expressions.
1183 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1184 const Designator &D = Desig.getDesignator(Idx);
1185 switch (D.getKind()) {
1186 case Designator::FieldDesignator:
1187 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
1188 D.getFieldLoc()));
1189 break;
1190
1191 case Designator::ArrayDesignator: {
1192 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1193 llvm::APSInt IndexValue;
1194 if (CheckArrayDesignatorExpr(*this, Index, IndexValue))
1195 Invalid = true;
1196 else {
1197 Designators.push_back(ASTDesignator(InitExpressions.size(),
1198 D.getLBracketLoc(),
1199 D.getRBracketLoc()));
1200 InitExpressions.push_back(Index);
1201 }
1202 break;
1203 }
1204
1205 case Designator::ArrayRangeDesignator: {
1206 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1207 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1208 llvm::APSInt StartValue;
1209 llvm::APSInt EndValue;
1210 if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) ||
1211 CheckArrayDesignatorExpr(*this, EndIndex, EndValue))
1212 Invalid = true;
Douglas Gregorea0528d2009-01-23 22:22:29 +00001213 else {
1214 // Make sure we're comparing values with the same bit width.
1215 if (StartValue.getBitWidth() > EndValue.getBitWidth())
1216 EndValue.extend(StartValue.getBitWidth());
1217 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1218 StartValue.extend(EndValue.getBitWidth());
1219
1220 if (EndValue < StartValue) {
1221 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1222 << StartValue.toString(10) << EndValue.toString(10)
1223 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1224 Invalid = true;
1225 } else {
1226 Designators.push_back(ASTDesignator(InitExpressions.size(),
1227 D.getLBracketLoc(),
1228 D.getEllipsisLoc(),
1229 D.getRBracketLoc()));
1230 InitExpressions.push_back(StartIndex);
1231 InitExpressions.push_back(EndIndex);
1232 }
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001233 }
1234 break;
1235 }
1236 }
1237 }
1238
1239 if (Invalid || Init.isInvalid())
1240 return ExprError();
1241
1242 // Clear out the expressions within the designation.
1243 Desig.ClearExprs(*this);
1244
1245 DesignatedInitExpr *DIE
1246 = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(),
1247 &InitExpressions[0], InitExpressions.size(),
1248 Loc, UsedColonSyntax,
1249 static_cast<Expr *>(Init.release()));
1250 return Owned(DIE);
1251}
Douglas Gregor849afc32009-01-29 00:45:39 +00001252
1253bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
1254 InitListChecker CheckInitList(this, InitList, DeclType);
1255 if (!CheckInitList.HadError())
1256 InitList = CheckInitList.getFullyStructuredList();
1257
1258 return CheckInitList.HadError();
1259}