blob: 328609a1929b453a9c8bffaf40041e9d99880273 [file] [log] [blame]
Steve Narofff8ecff22008-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 Lattner0cb78032009-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 Lattner9ececce2009-02-24 22:48:58 +000014// This file also implements Sema::CheckInitializerTypes.
Steve Narofff8ecff22008-05-01 22:18:59 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "Sema.h"
Douglas Gregore4a0bb72009-01-22 00:58:24 +000019#include "clang/Parse/Designator.h"
Steve Narofff8ecff22008-05-01 22:18:59 +000020#include "clang/AST/ASTContext.h"
Anders Carlsson98cee2f2009-05-27 16:10:08 +000021#include "clang/AST/ExprCXX.h"
Chris Lattnerd8b741c82009-02-24 23:10:27 +000022#include "clang/AST/ExprObjC.h"
Douglas Gregor85df8d82009-01-29 00:45:39 +000023#include <map>
Douglas Gregore4a0bb72009-01-22 00:58:24 +000024using namespace clang;
Steve Narofff8ecff22008-05-01 22:18:59 +000025
Chris Lattner0cb78032009-02-24 22:27:37 +000026//===----------------------------------------------------------------------===//
27// Sema Initialization Checking
28//===----------------------------------------------------------------------===//
29
Chris Lattnerd8b741c82009-02-24 23:10:27 +000030static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
Chris Lattnera9196812009-02-26 23:26:43 +000031 const ArrayType *AT = Context.getAsArrayType(DeclType);
32 if (!AT) return 0;
33
Eli Friedman893abe42009-05-29 18:22:49 +000034 if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
35 return 0;
36
Chris Lattnera9196812009-02-26 23:26:43 +000037 // See if this is a string literal or @encode.
38 Init = Init->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +000039
Chris Lattnera9196812009-02-26 23:26:43 +000040 // Handle @encode, which is a narrow string.
41 if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
42 return Init;
43
44 // Otherwise we can only handle string literals.
45 StringLiteral *SL = dyn_cast<StringLiteral>(Init);
Chris Lattner012b3392009-02-26 23:42:47 +000046 if (SL == 0) return 0;
Eli Friedman42a84652009-05-31 10:54:53 +000047
48 QualType ElemTy = Context.getCanonicalType(AT->getElementType());
Chris Lattnera9196812009-02-26 23:26:43 +000049 // char array can be initialized with a narrow string.
50 // Only allow char x[] = "foo"; not char x[] = L"foo";
51 if (!SL->isWide())
Eli Friedman42a84652009-05-31 10:54:53 +000052 return ElemTy->isCharType() ? Init : 0;
Chris Lattnera9196812009-02-26 23:26:43 +000053
Eli Friedman42a84652009-05-31 10:54:53 +000054 // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
55 // correction from DR343): "An array with element type compatible with a
56 // qualified or unqualified version of wchar_t may be initialized by a wide
57 // string literal, optionally enclosed in braces."
58 if (Context.typesAreCompatible(Context.getWCharType(),
59 ElemTy.getUnqualifiedType()))
Chris Lattnera9196812009-02-26 23:26:43 +000060 return Init;
Mike Stump11289f42009-09-09 15:08:12 +000061
Chris Lattner0cb78032009-02-24 22:27:37 +000062 return 0;
63}
64
Mike Stump11289f42009-09-09 15:08:12 +000065static bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
Chris Lattner94d2f682009-02-24 22:46:58 +000066 bool DirectInit, Sema &S) {
Chris Lattner0cb78032009-02-24 22:27:37 +000067 // Get the type before calling CheckSingleAssignmentConstraints(), since
68 // it can promote the expression.
Mike Stump11289f42009-09-09 15:08:12 +000069 QualType InitType = Init->getType();
70
Chris Lattner94d2f682009-02-24 22:46:58 +000071 if (S.getLangOptions().CPlusPlus) {
Chris Lattner0cb78032009-02-24 22:27:37 +000072 // FIXME: I dislike this error message. A lot.
Chris Lattner94d2f682009-02-24 22:46:58 +000073 if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit))
74 return S.Diag(Init->getSourceRange().getBegin(),
75 diag::err_typecheck_convert_incompatible)
Mike Stump11289f42009-09-09 15:08:12 +000076 << DeclType << Init->getType() << "initializing"
Chris Lattner94d2f682009-02-24 22:46:58 +000077 << Init->getSourceRange();
Chris Lattner0cb78032009-02-24 22:27:37 +000078 return false;
79 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattner94d2f682009-02-24 22:46:58 +000081 Sema::AssignConvertType ConvTy =
82 S.CheckSingleAssignmentConstraints(DeclType, Init);
83 return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
Chris Lattner0cb78032009-02-24 22:27:37 +000084 InitType, Init, "initializing");
85}
86
Chris Lattnerd8b741c82009-02-24 23:10:27 +000087static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
88 // Get the length of the string as parsed.
89 uint64_t StrLength =
90 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
91
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattnerd8b741c82009-02-24 23:10:27 +000093 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattner0cb78032009-02-24 22:27:37 +000094 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Mike Stump11289f42009-09-09 15:08:12 +000095 // C99 6.7.8p14. We have an array of character type with unknown size
Chris Lattner0cb78032009-02-24 22:27:37 +000096 // being initialized to a string literal.
97 llvm::APSInt ConstVal(32);
Chris Lattner94e6c4b2009-02-24 23:01:39 +000098 ConstVal = StrLength;
Chris Lattner0cb78032009-02-24 22:27:37 +000099 // Return a new array type (C99 6.7.8p22).
Douglas Gregor04318252009-07-06 15:59:29 +0000100 DeclT = S.Context.getConstantArrayWithoutExprType(IAT->getElementType(),
101 ConstVal,
102 ArrayType::Normal, 0);
Chris Lattner94e6c4b2009-02-24 23:01:39 +0000103 return;
Chris Lattner0cb78032009-02-24 22:27:37 +0000104 }
Mike Stump11289f42009-09-09 15:08:12 +0000105
Eli Friedman893abe42009-05-29 18:22:49 +0000106 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Eli Friedman893abe42009-05-29 18:22:49 +0000108 // C99 6.7.8p14. We have an array of character type with known size. However,
109 // the size may be smaller or larger than the string we are initializing.
110 // FIXME: Avoid truncation for 64-bit length strings.
111 if (StrLength-1 > CAT->getSize().getZExtValue())
112 S.Diag(Str->getSourceRange().getBegin(),
113 diag::warn_initializer_string_for_char_array_too_long)
114 << Str->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000115
Eli Friedman893abe42009-05-29 18:22:49 +0000116 // Set the type to the actual size that we are initializing. If we have
117 // something like:
118 // char x[1] = "foo";
119 // then this will set the string literal's type to char[1].
120 Str->setType(DeclT);
Chris Lattner0cb78032009-02-24 22:27:37 +0000121}
122
123bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
124 SourceLocation InitLoc,
Anders Carlsson28486d42009-05-30 20:41:30 +0000125 DeclarationName InitEntity, bool DirectInit) {
Mike Stump11289f42009-09-09 15:08:12 +0000126 if (DeclType->isDependentType() ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +0000127 Init->isTypeDependent() || Init->isValueDependent())
Chris Lattner0cb78032009-02-24 22:27:37 +0000128 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattner0cb78032009-02-24 22:27:37 +0000130 // C++ [dcl.init.ref]p1:
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000131 // A variable declared to be a T& or T&&, that is "reference to type T"
Chris Lattner0cb78032009-02-24 22:27:37 +0000132 // (8.3.2), shall be initialized by an object, or function, of
133 // type T or by an object that can be converted into a T.
134 if (DeclType->isReferenceType())
Mike Stump11289f42009-09-09 15:08:12 +0000135 return CheckReferenceInit(Init, DeclType,
Anders Carlsson271e3a42009-08-27 17:30:43 +0000136 /*SuppressUserConversions=*/false,
137 /*AllowExplicit=*/DirectInit,
138 /*ForceRValue=*/false);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Chris Lattner0cb78032009-02-24 22:27:37 +0000140 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
141 // of unknown size ("[]") or an object type that is not a variable array type.
142 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
143 return Diag(InitLoc, diag::err_variable_object_no_init)
144 << VAT->getSizeExpr()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner0cb78032009-02-24 22:27:37 +0000146 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
147 if (!InitList) {
148 // FIXME: Handle wide strings
Chris Lattnerd8b741c82009-02-24 23:10:27 +0000149 if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
150 CheckStringInit(Str, DeclType, *this);
Chris Lattner94e6c4b2009-02-24 23:01:39 +0000151 return false;
152 }
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattner0cb78032009-02-24 22:27:37 +0000154 // C++ [dcl.init]p14:
155 // -- If the destination type is a (possibly cv-qualified) class
156 // type:
157 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
158 QualType DeclTypeC = Context.getCanonicalType(DeclType);
159 QualType InitTypeC = Context.getCanonicalType(Init->getType());
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattner0cb78032009-02-24 22:27:37 +0000161 // -- If the initialization is direct-initialization, or if it is
162 // copy-initialization where the cv-unqualified version of the
163 // source type is the same class as, or a derived class of, the
164 // class of the destination, constructors are considered.
165 if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
166 IsDerivedFrom(InitTypeC, DeclTypeC)) {
Mike Stump11289f42009-09-09 15:08:12 +0000167 const CXXRecordDecl *RD =
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000168 cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000169
Anders Carlsson50636132009-05-27 16:38:58 +0000170 // No need to make a CXXConstructExpr if both the ctor and dtor are
171 // trivial.
172 if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor())
173 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Douglas Gregor5d3507d2009-09-09 23:08:42 +0000175 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
176
Mike Stump11289f42009-09-09 15:08:12 +0000177 CXXConstructorDecl *Constructor
Douglas Gregor5d3507d2009-09-09 23:08:42 +0000178 = PerformInitializationByConstructor(DeclType,
179 MultiExprArg(*this,
180 (void **)&Init, 1),
181 InitLoc, Init->getSourceRange(),
182 InitEntity,
183 DirectInit? IK_Direct : IK_Copy,
184 ConstructorArgs);
Anders Carlsson98cee2f2009-05-27 16:10:08 +0000185 if (!Constructor)
186 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000187
188 OwningExprResult InitResult =
Anders Carlsson1b4ebfa2009-09-05 07:40:38 +0000189 BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +0000190 DeclType, Constructor,
Douglas Gregor5d3507d2009-09-09 23:08:42 +0000191 move_arg(ConstructorArgs));
Anders Carlsson6eb55572009-08-25 05:12:04 +0000192 if (InitResult.isInvalid())
193 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Anders Carlsson6eb55572009-08-25 05:12:04 +0000195 Init = InitResult.takeAs<Expr>();
Anders Carlsson98cee2f2009-05-27 16:10:08 +0000196 return false;
Chris Lattner0cb78032009-02-24 22:27:37 +0000197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner0cb78032009-02-24 22:27:37 +0000199 // -- Otherwise (i.e., for the remaining copy-initialization
200 // cases), user-defined conversion sequences that can
201 // convert from the source type to the destination type or
202 // (when a conversion function is used) to a derived class
203 // thereof are enumerated as described in 13.3.1.4, and the
204 // best one is chosen through overload resolution
205 // (13.3). If the conversion cannot be done or is
206 // ambiguous, the initialization is ill-formed. The
207 // function selected is called with the initializer
208 // expression as its argument; if the function is a
209 // constructor, the call initializes a temporary of the
210 // destination type.
Mike Stump87c57ac2009-05-16 07:39:55 +0000211 // FIXME: We're pretending to do copy elision here; return to this when we
212 // have ASTs for such things.
Chris Lattner0cb78032009-02-24 22:27:37 +0000213 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
214 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000215
Chris Lattner0cb78032009-02-24 22:27:37 +0000216 if (InitEntity)
217 return Diag(InitLoc, diag::err_cannot_initialize_decl)
Chris Lattnerd86a13e2009-06-26 04:45:06 +0000218 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
219 << Init->getType() << Init->getSourceRange();
220 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
Chris Lattner0cb78032009-02-24 22:27:37 +0000221 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
222 << Init->getType() << Init->getSourceRange();
223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Chris Lattner0cb78032009-02-24 22:27:37 +0000225 // C99 6.7.8p16.
226 if (DeclType->isArrayType())
227 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
Chris Lattnerd86a13e2009-06-26 04:45:06 +0000228 << Init->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000229
Chris Lattner94d2f682009-02-24 22:46:58 +0000230 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Mike Stump11289f42009-09-09 15:08:12 +0000231 }
232
Chris Lattner0cb78032009-02-24 22:27:37 +0000233 bool hadError = CheckInitList(InitList, DeclType);
234 Init = InitList;
235 return hadError;
236}
237
238//===----------------------------------------------------------------------===//
239// Semantic checking for initializer lists.
240//===----------------------------------------------------------------------===//
241
Douglas Gregorcde232f2009-01-29 01:05:33 +0000242/// @brief Semantic checking for initializer lists.
243///
244/// The InitListChecker class contains a set of routines that each
245/// handle the initialization of a certain kind of entity, e.g.,
246/// arrays, vectors, struct/union types, scalars, etc. The
247/// InitListChecker itself performs a recursive walk of the subobject
248/// structure of the type to be initialized, while stepping through
249/// the initializer list one element at a time. The IList and Index
250/// parameters to each of the Check* routines contain the active
251/// (syntactic) initializer list and the index into that initializer
252/// list that represents the current initializer. Each routine is
253/// responsible for moving that Index forward as it consumes elements.
254///
255/// Each Check* routine also has a StructuredList/StructuredIndex
256/// arguments, which contains the current the "structured" (semantic)
257/// initializer list and the index into that initializer list where we
258/// are copying initializers as we map them over to the semantic
259/// list. Once we have completed our recursive walk of the subobject
260/// structure, we will have constructed a full semantic initializer
261/// list.
262///
263/// C99 designators cause changes in the initializer list traversal,
264/// because they make the initialization "jump" into a specific
265/// subobject and then continue the initialization from that
266/// point. CheckDesignatedInitializer() recursively steps into the
267/// designated subobject and manages backing out the recursion to
268/// initialize the subobjects after the one designated.
Chris Lattner9ececce2009-02-24 22:48:58 +0000269namespace {
Douglas Gregor85df8d82009-01-29 00:45:39 +0000270class InitListChecker {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000271 Sema &SemaRef;
Douglas Gregor85df8d82009-01-29 00:45:39 +0000272 bool hadError;
273 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
274 InitListExpr *FullyStructuredList;
Mike Stump11289f42009-09-09 15:08:12 +0000275
276 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000277 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000278 unsigned &StructuredIndex,
279 bool TopLevelObject = false);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000280 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000281 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000282 unsigned &StructuredIndex,
283 bool TopLevelObject = false);
Mike Stump11289f42009-09-09 15:08:12 +0000284 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
285 bool SubobjectIsDesignatorContext,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000286 unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000287 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000288 unsigned &StructuredIndex,
289 bool TopLevelObject = false);
Mike Stump11289f42009-09-09 15:08:12 +0000290 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000291 unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000292 InitListExpr *StructuredList,
293 unsigned &StructuredIndex);
Mike Stump11289f42009-09-09 15:08:12 +0000294 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000295 unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000296 InitListExpr *StructuredList,
297 unsigned &StructuredIndex);
Mike Stump11289f42009-09-09 15:08:12 +0000298 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
Douglas Gregord14247a2009-01-30 22:09:00 +0000299 unsigned &Index,
300 InitListExpr *StructuredList,
301 unsigned &StructuredIndex);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000302 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000303 InitListExpr *StructuredList,
304 unsigned &StructuredIndex);
Mike Stump11289f42009-09-09 15:08:12 +0000305 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
306 RecordDecl::field_iterator Field,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000307 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000308 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000309 unsigned &StructuredIndex,
310 bool TopLevelObject = false);
Mike Stump11289f42009-09-09 15:08:12 +0000311 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
312 llvm::APSInt elementIndex,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000313 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregorcde232f2009-01-29 01:05:33 +0000314 InitListExpr *StructuredList,
315 unsigned &StructuredIndex);
Mike Stump11289f42009-09-09 15:08:12 +0000316 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregora5324162009-04-15 04:56:10 +0000317 unsigned DesigIdx,
Mike Stump11289f42009-09-09 15:08:12 +0000318 QualType &CurrentObjectType,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000319 RecordDecl::field_iterator *NextField,
320 llvm::APSInt *NextElementIndex,
321 unsigned &Index,
322 InitListExpr *StructuredList,
323 unsigned &StructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000324 bool FinishSubobjectInit,
325 bool TopLevelObject);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000326 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
327 QualType CurrentObjectType,
328 InitListExpr *StructuredList,
329 unsigned StructuredIndex,
330 SourceRange InitRange);
Douglas Gregorcde232f2009-01-29 01:05:33 +0000331 void UpdateStructuredListElement(InitListExpr *StructuredList,
332 unsigned &StructuredIndex,
Douglas Gregor85df8d82009-01-29 00:45:39 +0000333 Expr *expr);
334 int numArrayElements(QualType DeclType);
335 int numStructUnionElements(QualType DeclType);
Douglas Gregord14247a2009-01-30 22:09:00 +0000336
337 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000338public:
Chris Lattnerb0912a52009-02-24 22:50:46 +0000339 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregor85df8d82009-01-29 00:45:39 +0000340 bool HadError() { return hadError; }
341
342 // @brief Retrieves the fully-structured initializer list used for
343 // semantic analysis and code generation.
344 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
345};
Chris Lattner9ececce2009-02-24 22:48:58 +0000346} // end anonymous namespace
Chris Lattnerd9ae05b2009-01-29 05:10:57 +0000347
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000348/// Recursively replaces NULL values within the given initializer list
349/// with expressions that perform value-initialization of the
350/// appropriate type.
Douglas Gregord14247a2009-01-30 22:09:00 +0000351void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Mike Stump11289f42009-09-09 15:08:12 +0000352 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregord14247a2009-01-30 22:09:00 +0000353 "Should not have void type");
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000354 SourceLocation Loc = ILE->getSourceRange().getBegin();
355 if (ILE->getSyntacticForm())
356 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000358 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000359 unsigned Init = 0, NumInits = ILE->getNumInits();
Mike Stump11289f42009-09-09 15:08:12 +0000360 for (RecordDecl::field_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000361 Field = RType->getDecl()->field_begin(),
362 FieldEnd = RType->getDecl()->field_end();
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000363 Field != FieldEnd; ++Field) {
364 if (Field->isUnnamedBitfield())
365 continue;
366
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000367 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000368 if (Field->getType()->isReferenceType()) {
369 // C++ [dcl.init.aggr]p9:
370 // If an incomplete or empty initializer-list leaves a
371 // member of reference type uninitialized, the program is
Mike Stump11289f42009-09-09 15:08:12 +0000372 // ill-formed.
Chris Lattnerb0912a52009-02-24 22:50:46 +0000373 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregord14247a2009-01-30 22:09:00 +0000374 << Field->getType()
375 << ILE->getSyntacticForm()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000376 SemaRef.Diag(Field->getLocation(),
Douglas Gregord14247a2009-01-30 22:09:00 +0000377 diag::note_uninit_reference_member);
378 hadError = true;
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000379 return;
Chris Lattnerb0912a52009-02-24 22:50:46 +0000380 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000381 hadError = true;
382 return;
Douglas Gregord14247a2009-01-30 22:09:00 +0000383 }
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000384
Mike Stump87c57ac2009-05-16 07:39:55 +0000385 // FIXME: If value-initialization involves calling a constructor, should
386 // we make that call explicit in the representation (even when it means
387 // extending the initializer list)?
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000388 if (Init < NumInits && !hadError)
Mike Stump11289f42009-09-09 15:08:12 +0000389 ILE->setInit(Init,
Chris Lattnerb0912a52009-02-24 22:50:46 +0000390 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000391 } else if (InitListExpr *InnerILE
Douglas Gregor0202cb42009-01-29 17:44:32 +0000392 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord14247a2009-01-30 22:09:00 +0000393 FillInValueInitializations(InnerILE);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000394 ++Init;
Douglas Gregord14247a2009-01-30 22:09:00 +0000395
396 // Only look at the first initialization of a union.
397 if (RType->getDecl()->isUnion())
398 break;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000399 }
400
401 return;
Mike Stump11289f42009-09-09 15:08:12 +0000402 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000403
404 QualType ElementType;
Mike Stump11289f42009-09-09 15:08:12 +0000405
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000406 unsigned NumInits = ILE->getNumInits();
407 unsigned NumElements = NumInits;
Chris Lattnerb0912a52009-02-24 22:50:46 +0000408 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000409 ElementType = AType->getElementType();
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000410 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
411 NumElements = CAType->getSize().getZExtValue();
412 } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000413 ElementType = VType->getElementType();
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000414 NumElements = VType->getNumElements();
Mike Stump11289f42009-09-09 15:08:12 +0000415 } else
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000416 ElementType = ILE->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000417
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000418 for (unsigned Init = 0; Init != NumElements; ++Init) {
419 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000420 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000421 hadError = true;
422 return;
423 }
424
Mike Stump87c57ac2009-05-16 07:39:55 +0000425 // FIXME: If value-initialization involves calling a constructor, should
426 // we make that call explicit in the representation (even when it means
427 // extending the initializer list)?
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000428 if (Init < NumInits && !hadError)
Mike Stump11289f42009-09-09 15:08:12 +0000429 ILE->setInit(Init,
Chris Lattnerb0912a52009-02-24 22:50:46 +0000430 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Mike Stump12b8ce12009-08-04 21:02:39 +0000431 } else if (InitListExpr *InnerILE
432 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregord14247a2009-01-30 22:09:00 +0000433 FillInValueInitializations(InnerILE);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000434 }
435}
436
Chris Lattnerd9ae05b2009-01-29 05:10:57 +0000437
Chris Lattnerb0912a52009-02-24 22:50:46 +0000438InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
439 : SemaRef(S) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000440 hadError = false;
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000441
Eli Friedman23a9e312008-05-19 19:16:24 +0000442 unsigned newIndex = 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000443 unsigned newStructuredIndex = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000444 FullyStructuredList
Douglas Gregor5741efb2009-03-01 17:12:46 +0000445 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000446 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
447 /*TopLevelObject=*/true);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000448
Douglas Gregord14247a2009-01-30 22:09:00 +0000449 if (!hadError)
450 FillInValueInitializations(FullyStructuredList);
Steve Narofff8ecff22008-05-01 22:18:59 +0000451}
452
453int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman85f54972008-05-25 13:22:35 +0000454 // FIXME: use a proper constant
455 int maxElements = 0x7FFFFFFF;
Chris Lattner7adf0762008-08-04 07:31:14 +0000456 if (const ConstantArrayType *CAT =
Chris Lattnerb0912a52009-02-24 22:50:46 +0000457 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000458 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
459 }
460 return maxElements;
461}
462
463int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000464 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000465 int InitializableMembers = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000466 for (RecordDecl::field_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000467 Field = structDecl->field_begin(),
468 FieldEnd = structDecl->field_end();
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000469 Field != FieldEnd; ++Field) {
470 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
471 ++InitializableMembers;
472 }
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +0000473 if (structDecl->isUnion())
Eli Friedman0e56c822008-05-25 14:03:31 +0000474 return std::min(InitializableMembers, 1);
475 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Narofff8ecff22008-05-01 22:18:59 +0000476}
477
Mike Stump11289f42009-09-09 15:08:12 +0000478void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000479 QualType T, unsigned &Index,
480 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000481 unsigned &StructuredIndex,
482 bool TopLevelObject) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000483 int maxElements = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Steve Narofff8ecff22008-05-01 22:18:59 +0000485 if (T->isArrayType())
486 maxElements = numArrayElements(T);
487 else if (T->isStructureType() || T->isUnionType())
488 maxElements = numStructUnionElements(T);
Eli Friedman23a9e312008-05-19 19:16:24 +0000489 else if (T->isVectorType())
490 maxElements = T->getAsVectorType()->getNumElements();
Steve Narofff8ecff22008-05-01 22:18:59 +0000491 else
492 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman23a9e312008-05-19 19:16:24 +0000493
Eli Friedmane0f832b2008-05-25 13:49:22 +0000494 if (maxElements == 0) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000495 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedmane0f832b2008-05-25 13:49:22 +0000496 diag::err_implicit_empty_initializer);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000497 ++Index;
Eli Friedmane0f832b2008-05-25 13:49:22 +0000498 hadError = true;
499 return;
500 }
501
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000502 // Build a structured initializer list corresponding to this subobject.
503 InitListExpr *StructuredSubobjectInitList
Mike Stump11289f42009-09-09 15:08:12 +0000504 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
505 StructuredIndex,
Douglas Gregor5741efb2009-03-01 17:12:46 +0000506 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
507 ParentIList->getSourceRange().getEnd()));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000508 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedman23a9e312008-05-19 19:16:24 +0000509
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000510 // Check the element types and build the structural subobject.
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000511 unsigned StartIndex = Index;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000512 CheckListElementTypes(ParentIList, T, false, Index,
Mike Stump11289f42009-09-09 15:08:12 +0000513 StructuredSubobjectInitList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000514 StructuredSubobjectInitIndex,
515 TopLevelObject);
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000516 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregor07d8e3a2009-03-20 00:32:56 +0000517 StructuredSubobjectInitList->setType(T);
518
Douglas Gregor5741efb2009-03-01 17:12:46 +0000519 // Update the structured sub-object initializer so that it's ending
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000520 // range corresponds with the end of the last initializer it used.
521 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump11289f42009-09-09 15:08:12 +0000522 SourceLocation EndLoc
Douglas Gregora5c9e1a2009-02-02 17:43:21 +0000523 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
524 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
525 }
Steve Narofff8ecff22008-05-01 22:18:59 +0000526}
527
Steve Naroff125d73d2008-05-06 00:23:44 +0000528void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000529 unsigned &Index,
530 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000531 unsigned &StructuredIndex,
532 bool TopLevelObject) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000533 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000534 SyntacticToSemantic[IList] = StructuredList;
535 StructuredList->setSyntacticForm(IList);
Mike Stump11289f42009-09-09 15:08:12 +0000536 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000537 StructuredIndex, TopLevelObject);
Steve Naroff125d73d2008-05-06 00:23:44 +0000538 IList->setType(T);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000539 StructuredList->setType(T);
Eli Friedman85f54972008-05-25 13:22:35 +0000540 if (hadError)
541 return;
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000542
Eli Friedman85f54972008-05-25 13:22:35 +0000543 if (Index < IList->getNumInits()) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000544 // We have leftover initializers
Eli Friedmanbd327452009-05-29 20:20:05 +0000545 if (StructuredIndex == 1 &&
546 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000547 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmanbd327452009-05-29 20:20:05 +0000548 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000549 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmanbd327452009-05-29 20:20:05 +0000550 hadError = true;
551 }
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000552 // Special-case
Chris Lattnerb0912a52009-02-24 22:50:46 +0000553 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerf490e152008-11-19 05:27:50 +0000554 << IList->getInit(Index)->getSourceRange();
Eli Friedmand0e48ea2008-05-20 05:25:56 +0000555 } else if (!T->isIncompleteType()) {
Douglas Gregord42a0fb2009-01-30 22:26:29 +0000556 // Don't complain for incomplete types, since we'll get an error
557 // elsewhere
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000558 QualType CurrentObjectType = StructuredList->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000559 int initKind =
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000560 CurrentObjectType->isArrayType()? 0 :
561 CurrentObjectType->isVectorType()? 1 :
562 CurrentObjectType->isScalarType()? 2 :
563 CurrentObjectType->isUnionType()? 3 :
564 4;
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000565
566 unsigned DK = diag::warn_excess_initializers;
Eli Friedmanbd327452009-05-29 20:20:05 +0000567 if (SemaRef.getLangOptions().CPlusPlus) {
568 DK = diag::err_excess_initializers;
569 hadError = true;
570 }
Nate Begeman425038c2009-07-07 21:53:06 +0000571 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
572 DK = diag::err_excess_initializers;
573 hadError = true;
574 }
Douglas Gregor1cba5fe2009-02-18 22:23:55 +0000575
Chris Lattnerb0912a52009-02-24 22:50:46 +0000576 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000577 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000578 }
579 }
Eli Friedman6fcdec22008-05-19 20:20:43 +0000580
Eli Friedman0b4af8f2009-05-16 11:45:48 +0000581 if (T->isScalarType() && !TopLevelObject)
Chris Lattnerb0912a52009-02-24 22:50:46 +0000582 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregor170512f2009-04-01 23:51:29 +0000583 << IList->getSourceRange()
584 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart()))
585 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd()));
Steve Narofff8ecff22008-05-01 22:18:59 +0000586}
587
Eli Friedman23a9e312008-05-19 19:16:24 +0000588void InitListChecker::CheckListElementTypes(InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +0000589 QualType &DeclType,
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000590 bool SubobjectIsDesignatorContext,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000591 unsigned &Index,
592 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000593 unsigned &StructuredIndex,
594 bool TopLevelObject) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000595 if (DeclType->isScalarType()) {
Douglas Gregorf6d27522009-01-29 00:39:20 +0000596 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000597 } else if (DeclType->isVectorType()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000598 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregorddb24852009-01-30 17:31:00 +0000599 } else if (DeclType->isAggregateType()) {
600 if (DeclType->isRecordType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000601 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000602 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000603 SubobjectIsDesignatorContext, Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000604 StructuredList, StructuredIndex,
605 TopLevelObject);
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000606 } else if (DeclType->isArrayType()) {
Douglas Gregor033d1252009-01-23 16:54:12 +0000607 llvm::APSInt Zero(
Chris Lattnerb0912a52009-02-24 22:50:46 +0000608 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregor033d1252009-01-23 16:54:12 +0000609 false);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000610 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
611 StructuredList, StructuredIndex);
Mike Stump12b8ce12009-08-04 21:02:39 +0000612 } else
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000613 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroffeaf58532008-08-10 16:05:48 +0000614 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
615 // This type is invalid, issue a diagnostic.
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000616 ++Index;
Chris Lattnerb0912a52009-02-24 22:50:46 +0000617 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000618 << DeclType;
Eli Friedmand0e48ea2008-05-20 05:25:56 +0000619 hadError = true;
Douglas Gregord14247a2009-01-30 22:09:00 +0000620 } else if (DeclType->isRecordType()) {
621 // C++ [dcl.init]p14:
622 // [...] If the class is an aggregate (8.5.1), and the initializer
623 // is a brace-enclosed list, see 8.5.1.
624 //
625 // Note: 8.5.1 is handled below; here, we diagnose the case where
626 // we have an initializer list and a destination type that is not
627 // an aggregate.
628 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattnerb0912a52009-02-24 22:50:46 +0000629 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord14247a2009-01-30 22:09:00 +0000630 << DeclType << IList->getSourceRange();
631 hadError = true;
632 } else if (DeclType->isReferenceType()) {
633 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Narofff8ecff22008-05-01 22:18:59 +0000634 } else {
635 // In C, all types are either scalars or aggregates, but
Mike Stump11289f42009-09-09 15:08:12 +0000636 // additional handling is needed here for C++ (and possibly others?).
Steve Narofff8ecff22008-05-01 22:18:59 +0000637 assert(0 && "Unsupported initializer type");
638 }
639}
640
Eli Friedman23a9e312008-05-19 19:16:24 +0000641void InitListChecker::CheckSubElementType(InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +0000642 QualType ElemType,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000643 unsigned &Index,
644 InitListExpr *StructuredList,
645 unsigned &StructuredIndex) {
Douglas Gregorf6d27522009-01-29 00:39:20 +0000646 Expr *expr = IList->getInit(Index);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000647 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
648 unsigned newIndex = 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000649 unsigned newStructuredIndex = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000650 InitListExpr *newStructuredList
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000651 = getStructuredSubobjectInit(IList, Index, ElemType,
652 StructuredList, StructuredIndex,
653 SubInitList->getSourceRange());
Mike Stump11289f42009-09-09 15:08:12 +0000654 CheckExplicitInitList(SubInitList, ElemType, newIndex,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000655 newStructuredList, newStructuredIndex);
656 ++StructuredIndex;
657 ++Index;
Chris Lattnerd8b741c82009-02-24 23:10:27 +0000658 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
659 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattneredbf3ba2009-02-24 22:41:04 +0000660 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000661 ++Index;
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000662 } else if (ElemType->isScalarType()) {
Douglas Gregorf6d27522009-01-29 00:39:20 +0000663 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregord14247a2009-01-30 22:09:00 +0000664 } else if (ElemType->isReferenceType()) {
665 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedman23a9e312008-05-19 19:16:24 +0000666 } else {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000667 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000668 // C++ [dcl.init.aggr]p12:
669 // All implicit type conversions (clause 4) are considered when
670 // initializing the aggregate member with an ini- tializer from
671 // an initializer-list. If the initializer can initialize a
672 // member, the member is initialized. [...]
Mike Stump11289f42009-09-09 15:08:12 +0000673 ImplicitConversionSequence ICS
Anders Carlsson03068aa2009-08-27 17:18:13 +0000674 = SemaRef.TryCopyInitialization(expr, ElemType,
675 /*SuppressUserConversions=*/false,
Anders Carlsson20d13322009-08-27 17:37:39 +0000676 /*ForceRValue=*/false,
677 /*InOverloadResolution=*/false);
Anders Carlsson03068aa2009-08-27 17:18:13 +0000678
Douglas Gregord14247a2009-01-30 22:09:00 +0000679 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Mike Stump11289f42009-09-09 15:08:12 +0000680 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregord14247a2009-01-30 22:09:00 +0000681 "initializing"))
682 hadError = true;
683 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
684 ++Index;
685 return;
686 }
687
688 // Fall through for subaggregate initialization
689 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000690 // C99 6.7.8p13:
Douglas Gregord14247a2009-01-30 22:09:00 +0000691 //
692 // The initializer for a structure or union object that has
693 // automatic storage duration shall be either an initializer
694 // list as described below, or a single expression that has
695 // compatible structure or union type. In the latter case, the
696 // initial value of the object, including unnamed members, is
697 // that of the expression.
Eli Friedman9782caa2009-06-13 10:38:46 +0000698 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
Eli Friedman893abe42009-05-29 18:22:49 +0000699 SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
Douglas Gregord14247a2009-01-30 22:09:00 +0000700 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
701 ++Index;
702 return;
703 }
704
705 // Fall through for subaggregate initialization
706 }
707
708 // C++ [dcl.init.aggr]p12:
Mike Stump11289f42009-09-09 15:08:12 +0000709 //
Douglas Gregord14247a2009-01-30 22:09:00 +0000710 // [...] Otherwise, if the member is itself a non-empty
711 // subaggregate, brace elision is assumed and the initializer is
712 // considered for the initialization of the first member of
713 // the subaggregate.
714 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000715 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
Douglas Gregord14247a2009-01-30 22:09:00 +0000716 StructuredIndex);
717 ++StructuredIndex;
718 } else {
719 // We cannot initialize this element, so let
720 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattnerb0912a52009-02-24 22:50:46 +0000721 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregord14247a2009-01-30 22:09:00 +0000722 hadError = true;
723 ++Index;
724 ++StructuredIndex;
725 }
726 }
Eli Friedman23a9e312008-05-19 19:16:24 +0000727}
728
Douglas Gregord14247a2009-01-30 22:09:00 +0000729void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregorf6d27522009-01-29 00:39:20 +0000730 unsigned &Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000731 InitListExpr *StructuredList,
732 unsigned &StructuredIndex) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000733 if (Index < IList->getNumInits()) {
Douglas Gregorf6d27522009-01-29 00:39:20 +0000734 Expr *expr = IList->getInit(Index);
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000735 if (isa<InitListExpr>(expr)) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000736 SemaRef.Diag(IList->getLocStart(),
Chris Lattnerf490e152008-11-19 05:27:50 +0000737 diag::err_many_braces_around_scalar_init)
738 << IList->getSourceRange();
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000739 hadError = true;
740 ++Index;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000741 ++StructuredIndex;
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000742 return;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000743 } else if (isa<DesignatedInitExpr>(expr)) {
Mike Stump11289f42009-09-09 15:08:12 +0000744 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000745 diag::err_designator_for_scalar_init)
746 << DeclType << expr->getSourceRange();
747 hadError = true;
748 ++Index;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000749 ++StructuredIndex;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000750 return;
Steve Narofff8ecff22008-05-01 22:18:59 +0000751 }
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000752
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000753 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattnerb0912a52009-02-24 22:50:46 +0000754 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000755 hadError = true; // types weren't compatible.
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000756 else if (savExpr != expr) {
Eli Friedman5a36d3f2008-05-19 20:00:43 +0000757 // The type was promoted, update initializer list.
Douglas Gregorf6d27522009-01-29 00:39:20 +0000758 IList->setInit(Index, expr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000759 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000760 if (hadError)
761 ++StructuredIndex;
762 else
763 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Narofff8ecff22008-05-01 22:18:59 +0000764 ++Index;
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000765 } else {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000766 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerf490e152008-11-19 05:27:50 +0000767 << IList->getSourceRange();
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000768 hadError = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000769 ++Index;
770 ++StructuredIndex;
Eli Friedmanfeb4cc12008-05-19 20:12:18 +0000771 return;
Steve Narofff8ecff22008-05-01 22:18:59 +0000772 }
Steve Narofff8ecff22008-05-01 22:18:59 +0000773}
774
Douglas Gregord14247a2009-01-30 22:09:00 +0000775void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
776 unsigned &Index,
777 InitListExpr *StructuredList,
778 unsigned &StructuredIndex) {
779 if (Index < IList->getNumInits()) {
780 Expr *expr = IList->getInit(Index);
781 if (isa<InitListExpr>(expr)) {
Chris Lattnerb0912a52009-02-24 22:50:46 +0000782 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregord14247a2009-01-30 22:09:00 +0000783 << DeclType << IList->getSourceRange();
784 hadError = true;
785 ++Index;
786 ++StructuredIndex;
787 return;
Mike Stump11289f42009-09-09 15:08:12 +0000788 }
Douglas Gregord14247a2009-01-30 22:09:00 +0000789
790 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Anders Carlsson271e3a42009-08-27 17:30:43 +0000791 if (SemaRef.CheckReferenceInit(expr, DeclType,
792 /*SuppressUserConversions=*/false,
793 /*AllowExplicit=*/false,
Mike Stump11289f42009-09-09 15:08:12 +0000794 /*ForceRValue=*/false))
Douglas Gregord14247a2009-01-30 22:09:00 +0000795 hadError = true;
796 else if (savExpr != expr) {
797 // The type was promoted, update initializer list.
798 IList->setInit(Index, expr);
799 }
800 if (hadError)
801 ++StructuredIndex;
802 else
803 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
804 ++Index;
805 } else {
Mike Stump87c57ac2009-05-16 07:39:55 +0000806 // FIXME: It would be wonderful if we could point at the actual member. In
807 // general, it would be useful to pass location information down the stack,
808 // so that we know the location (or decl) of the "current object" being
809 // initialized.
Mike Stump11289f42009-09-09 15:08:12 +0000810 SemaRef.Diag(IList->getLocStart(),
Douglas Gregord14247a2009-01-30 22:09:00 +0000811 diag::err_init_reference_member_uninitialized)
812 << DeclType
813 << IList->getSourceRange();
814 hadError = true;
815 ++Index;
816 ++StructuredIndex;
817 return;
818 }
819}
820
Mike Stump11289f42009-09-09 15:08:12 +0000821void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000822 unsigned &Index,
823 InitListExpr *StructuredList,
824 unsigned &StructuredIndex) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000825 if (Index < IList->getNumInits()) {
826 const VectorType *VT = DeclType->getAsVectorType();
Nate Begeman5ec4b312009-08-10 23:49:36 +0000827 unsigned maxElements = VT->getNumElements();
828 unsigned numEltsInit = 0;
Steve Narofff8ecff22008-05-01 22:18:59 +0000829 QualType elementType = VT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000830
Nate Begeman5ec4b312009-08-10 23:49:36 +0000831 if (!SemaRef.getLangOptions().OpenCL) {
832 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
833 // Don't attempt to go past the end of the init list
834 if (Index >= IList->getNumInits())
835 break;
836 CheckSubElementType(IList, elementType, Index,
837 StructuredList, StructuredIndex);
838 }
839 } else {
840 // OpenCL initializers allows vectors to be constructed from vectors.
841 for (unsigned i = 0; i < maxElements; ++i) {
842 // Don't attempt to go past the end of the init list
843 if (Index >= IList->getNumInits())
844 break;
845 QualType IType = IList->getInit(Index)->getType();
846 if (!IType->isVectorType()) {
847 CheckSubElementType(IList, elementType, Index,
848 StructuredList, StructuredIndex);
849 ++numEltsInit;
850 } else {
851 const VectorType *IVT = IType->getAsVectorType();
852 unsigned numIElts = IVT->getNumElements();
853 QualType VecType = SemaRef.Context.getExtVectorType(elementType,
854 numIElts);
855 CheckSubElementType(IList, VecType, Index,
856 StructuredList, StructuredIndex);
857 numEltsInit += numIElts;
858 }
859 }
Steve Narofff8ecff22008-05-01 22:18:59 +0000860 }
Mike Stump11289f42009-09-09 15:08:12 +0000861
Nate Begeman5ec4b312009-08-10 23:49:36 +0000862 // OpenCL & AltiVec require all elements to be initialized.
863 if (numEltsInit != maxElements)
864 if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
865 SemaRef.Diag(IList->getSourceRange().getBegin(),
866 diag::err_vector_incorrect_num_initializers)
867 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Narofff8ecff22008-05-01 22:18:59 +0000868 }
869}
870
Mike Stump11289f42009-09-09 15:08:12 +0000871void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000872 llvm::APSInt elementIndex,
Mike Stump11289f42009-09-09 15:08:12 +0000873 bool SubobjectIsDesignatorContext,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000874 unsigned &Index,
875 InitListExpr *StructuredList,
876 unsigned &StructuredIndex) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000877 // Check for the special-case of initializing an array with a string.
878 if (Index < IList->getNumInits()) {
Chris Lattnerd8b741c82009-02-24 23:10:27 +0000879 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
880 SemaRef.Context)) {
881 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000882 // We place the string literal directly into the resulting
883 // initializer list. This is the only place where the structure
884 // of the structured initializer list doesn't match exactly,
885 // because doing so would involve allocating one character
886 // constant for each string.
Chris Lattneredbf3ba2009-02-24 22:41:04 +0000887 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattnerb0912a52009-02-24 22:50:46 +0000888 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Narofff8ecff22008-05-01 22:18:59 +0000889 ++Index;
Steve Narofff8ecff22008-05-01 22:18:59 +0000890 return;
891 }
892 }
Chris Lattner7adf0762008-08-04 07:31:14 +0000893 if (const VariableArrayType *VAT =
Chris Lattnerb0912a52009-02-24 22:50:46 +0000894 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman85f54972008-05-25 13:22:35 +0000895 // Check for VLAs; in standard C it would be possible to check this
896 // earlier, but I don't know where clang accepts VLAs (gcc accepts
897 // them in all sorts of strange places).
Chris Lattnerb0912a52009-02-24 22:50:46 +0000898 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerf490e152008-11-19 05:27:50 +0000899 diag::err_variable_object_no_init)
900 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman85f54972008-05-25 13:22:35 +0000901 hadError = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000902 ++Index;
903 ++StructuredIndex;
Eli Friedman85f54972008-05-25 13:22:35 +0000904 return;
905 }
906
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000907 // We might know the maximum number of elements in advance.
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000908 llvm::APSInt maxElements(elementIndex.getBitWidth(),
909 elementIndex.isUnsigned());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000910 bool maxElementsKnown = false;
911 if (const ConstantArrayType *CAT =
Chris Lattnerb0912a52009-02-24 22:50:46 +0000912 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000913 maxElements = CAT->getSize();
Douglas Gregor033d1252009-01-23 16:54:12 +0000914 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregor583cf0a2009-01-23 18:58:42 +0000915 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000916 maxElementsKnown = true;
917 }
918
Chris Lattnerb0912a52009-02-24 22:50:46 +0000919 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattner7adf0762008-08-04 07:31:14 +0000920 ->getElementType();
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000921 while (Index < IList->getNumInits()) {
922 Expr *Init = IList->getInit(Index);
923 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000924 // If we're not the subobject that matches up with the '{' for
925 // the designator, we shouldn't be handling the
926 // designator. Return immediately.
927 if (!SubobjectIsDesignatorContext)
928 return;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000929
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000930 // Handle this designated initializer. elementIndex will be
931 // updated to be the next array element we'll initialize.
Mike Stump11289f42009-09-09 15:08:12 +0000932 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000933 DeclType, 0, &elementIndex, Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000934 StructuredList, StructuredIndex, true,
935 false)) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000936 hadError = true;
937 continue;
938 }
939
Douglas Gregor033d1252009-01-23 16:54:12 +0000940 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
941 maxElements.extend(elementIndex.getBitWidth());
942 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
943 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregor583cf0a2009-01-23 18:58:42 +0000944 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor033d1252009-01-23 16:54:12 +0000945
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000946 // If the array is of incomplete type, keep track of the number of
947 // elements in the initializer.
948 if (!maxElementsKnown && elementIndex > maxElements)
949 maxElements = elementIndex;
950
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000951 continue;
952 }
953
954 // If we know the maximum number of elements, and we've already
955 // hit it, stop consuming elements in the initializer list.
956 if (maxElementsKnown && elementIndex == maxElements)
Steve Narofff8ecff22008-05-01 22:18:59 +0000957 break;
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000958
959 // Check this element.
Douglas Gregorf6d27522009-01-29 00:39:20 +0000960 CheckSubElementType(IList, elementType, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000961 StructuredList, StructuredIndex);
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000962 ++elementIndex;
963
964 // If the array is of incomplete type, keep track of the number of
965 // elements in the initializer.
966 if (!maxElementsKnown && elementIndex > maxElements)
967 maxElements = elementIndex;
Steve Narofff8ecff22008-05-01 22:18:59 +0000968 }
Eli Friedmanbe7e42b2009-05-29 20:17:55 +0000969 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Narofff8ecff22008-05-01 22:18:59 +0000970 // If this is an incomplete array type, the actual type needs to
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +0000971 // be calculated here.
Douglas Gregor583cf0a2009-01-23 18:58:42 +0000972 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000973 if (maxElements == Zero) {
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +0000974 // Sizing an array implicitly to zero is not allowed by ISO C,
975 // but is supported by GNU.
Chris Lattnerb0912a52009-02-24 22:50:46 +0000976 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +0000977 diag::ext_typecheck_zero_array_size);
Steve Narofff8ecff22008-05-01 22:18:59 +0000978 }
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +0000979
Mike Stump11289f42009-09-09 15:08:12 +0000980 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbaraa64b7e2008-08-18 20:28:46 +0000981 ArrayType::Normal, 0);
Steve Narofff8ecff22008-05-01 22:18:59 +0000982 }
983}
984
Mike Stump11289f42009-09-09 15:08:12 +0000985void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
986 QualType DeclType,
Douglas Gregord7fb85e2009-01-22 23:26:18 +0000987 RecordDecl::field_iterator Field,
Mike Stump11289f42009-09-09 15:08:12 +0000988 bool SubobjectIsDesignatorContext,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000989 unsigned &Index,
990 InitListExpr *StructuredList,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000991 unsigned &StructuredIndex,
992 bool TopLevelObject) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000993 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000994
Eli Friedman23a9e312008-05-19 19:16:24 +0000995 // If the record is invalid, some of it's members are invalid. To avoid
996 // confusion, we forgo checking the intializer for the entire record.
997 if (structDecl->isInvalidDecl()) {
998 hadError = true;
999 return;
Mike Stump11289f42009-09-09 15:08:12 +00001000 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001001
1002 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1003 // Value-initialize the first named member of the union.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001004 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001005 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor0202cb42009-01-29 17:44:32 +00001006 Field != FieldEnd; ++Field) {
1007 if (Field->getDeclName()) {
1008 StructuredList->setInitializedFieldInUnion(*Field);
1009 break;
1010 }
1011 }
1012 return;
1013 }
1014
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001015 // If structDecl is a forward declaration, this loop won't do
1016 // anything except look at designated initializers; That's okay,
1017 // because an error should get printed out elsewhere. It might be
1018 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001019 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001020 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregora9add4e2009-02-12 19:00:39 +00001021 bool InitializedSomething = false;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001022 while (Index < IList->getNumInits()) {
1023 Expr *Init = IList->getInit(Index);
1024
1025 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001026 // If we're not the subobject that matches up with the '{' for
1027 // the designator, we shouldn't be handling the
1028 // designator. Return immediately.
1029 if (!SubobjectIsDesignatorContext)
1030 return;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001031
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001032 // Handle this designated initializer. Field will be updated to
1033 // the next field that we'll be initializing.
Mike Stump11289f42009-09-09 15:08:12 +00001034 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001035 DeclType, &Field, 0, Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001036 StructuredList, StructuredIndex,
1037 true, TopLevelObject))
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001038 hadError = true;
1039
Douglas Gregora9add4e2009-02-12 19:00:39 +00001040 InitializedSomething = true;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001041 continue;
1042 }
1043
1044 if (Field == FieldEnd) {
1045 // We've run out of fields. We're done.
1046 break;
1047 }
1048
Douglas Gregora9add4e2009-02-12 19:00:39 +00001049 // We've already initialized a member of a union. We're done.
1050 if (InitializedSomething && DeclType->isUnionType())
1051 break;
1052
Douglas Gregor91f84212008-12-11 16:49:14 +00001053 // If we've hit the flexible array member at the end, we're done.
1054 if (Field->getType()->isIncompleteArrayType())
1055 break;
1056
Douglas Gregor51695702009-01-29 16:53:55 +00001057 if (Field->isUnnamedBitfield()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001058 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001059 ++Field;
Eli Friedman23a9e312008-05-19 19:16:24 +00001060 continue;
Steve Narofff8ecff22008-05-01 22:18:59 +00001061 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001062
Douglas Gregorf6d27522009-01-29 00:39:20 +00001063 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001064 StructuredList, StructuredIndex);
Douglas Gregora9add4e2009-02-12 19:00:39 +00001065 InitializedSomething = true;
Douglas Gregor51695702009-01-29 16:53:55 +00001066
1067 if (DeclType->isUnionType()) {
1068 // Initialize the first field within the union.
1069 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor51695702009-01-29 16:53:55 +00001070 }
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001071
1072 ++Field;
Steve Narofff8ecff22008-05-01 22:18:59 +00001073 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001074
Mike Stump11289f42009-09-09 15:08:12 +00001075 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001076 Index >= IList->getNumInits())
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001077 return;
1078
1079 // Handle GNU flexible array initializers.
Mike Stump11289f42009-09-09 15:08:12 +00001080 if (!TopLevelObject &&
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001081 (!isa<InitListExpr>(IList->getInit(Index)) ||
1082 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001083 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001084 diag::err_flexible_array_init_nonempty)
1085 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001086 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001087 << *Field;
1088 hadError = true;
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001089 ++Index;
1090 return;
1091 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001092 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001093 diag::ext_flexible_array_init)
1094 << IList->getInit(Index)->getSourceRange().getBegin();
1095 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1096 << *Field;
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001097 }
1098
Douglas Gregor07d8e3a2009-03-20 00:32:56 +00001099 if (isa<InitListExpr>(IList->getInit(Index)))
1100 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1101 StructuredIndex);
1102 else
1103 CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1104 StructuredIndex);
Steve Narofff8ecff22008-05-01 22:18:59 +00001105}
Steve Narofff8ecff22008-05-01 22:18:59 +00001106
Douglas Gregord5846a12009-04-15 06:41:24 +00001107/// \brief Expand a field designator that refers to a member of an
1108/// anonymous struct or union into a series of field designators that
1109/// refers to the field within the appropriate subobject.
1110///
1111/// Field/FieldIndex will be updated to point to the (new)
1112/// currently-designated field.
1113static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump11289f42009-09-09 15:08:12 +00001114 DesignatedInitExpr *DIE,
1115 unsigned DesigIdx,
Douglas Gregord5846a12009-04-15 06:41:24 +00001116 FieldDecl *Field,
1117 RecordDecl::field_iterator &FieldIter,
1118 unsigned &FieldIndex) {
1119 typedef DesignatedInitExpr::Designator Designator;
1120
1121 // Build the path from the current object to the member of the
1122 // anonymous struct/union (backwards).
1123 llvm::SmallVector<FieldDecl *, 4> Path;
1124 SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
Mike Stump11289f42009-09-09 15:08:12 +00001125
Douglas Gregord5846a12009-04-15 06:41:24 +00001126 // Build the replacement designators.
1127 llvm::SmallVector<Designator, 4> Replacements;
1128 for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1129 FI = Path.rbegin(), FIEnd = Path.rend();
1130 FI != FIEnd; ++FI) {
1131 if (FI + 1 == FIEnd)
Mike Stump11289f42009-09-09 15:08:12 +00001132 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregord5846a12009-04-15 06:41:24 +00001133 DIE->getDesignator(DesigIdx)->getDotLoc(),
1134 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1135 else
1136 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1137 SourceLocation()));
1138 Replacements.back().setField(*FI);
1139 }
1140
1141 // Expand the current designator into the set of replacement
1142 // designators, so we have a full subobject path down to where the
1143 // member of the anonymous struct/union is actually stored.
Mike Stump11289f42009-09-09 15:08:12 +00001144 DIE->ExpandDesignator(DesigIdx, &Replacements[0],
Douglas Gregord5846a12009-04-15 06:41:24 +00001145 &Replacements[0] + Replacements.size());
Mike Stump11289f42009-09-09 15:08:12 +00001146
Douglas Gregord5846a12009-04-15 06:41:24 +00001147 // Update FieldIter/FieldIndex;
1148 RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001149 FieldIter = Record->field_begin();
Douglas Gregord5846a12009-04-15 06:41:24 +00001150 FieldIndex = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001151 for (RecordDecl::field_iterator FEnd = Record->field_end();
Douglas Gregord5846a12009-04-15 06:41:24 +00001152 FieldIter != FEnd; ++FieldIter) {
1153 if (FieldIter->isUnnamedBitfield())
1154 continue;
1155
1156 if (*FieldIter == Path.back())
1157 return;
1158
1159 ++FieldIndex;
1160 }
1161
1162 assert(false && "Unable to find anonymous struct/union field");
1163}
1164
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001165/// @brief Check the well-formedness of a C99 designated initializer.
1166///
1167/// Determines whether the designated initializer @p DIE, which
1168/// resides at the given @p Index within the initializer list @p
1169/// IList, is well-formed for a current object of type @p DeclType
1170/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump11289f42009-09-09 15:08:12 +00001171/// within the current subobject is returned in either
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001172/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001173///
1174/// @param IList The initializer list in which this designated
1175/// initializer occurs.
1176///
Douglas Gregora5324162009-04-15 04:56:10 +00001177/// @param DIE The designated initializer expression.
1178///
1179/// @param DesigIdx The index of the current designator.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001180///
1181/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1182/// into which the designation in @p DIE should refer.
1183///
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001184/// @param NextField If non-NULL and the first designator in @p DIE is
1185/// a field, this will be set to the field declaration corresponding
1186/// to the field named by the designator.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001187///
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001188/// @param NextElementIndex If non-NULL and the first designator in @p
1189/// DIE is an array designator or GNU array-range designator, this
1190/// will be set to the last index initialized by this designator.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001191///
1192/// @param Index Index into @p IList where the designated initializer
1193/// @p DIE occurs.
1194///
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001195/// @param StructuredList The initializer list expression that
1196/// describes all of the subobject initializers in the order they'll
1197/// actually be initialized.
1198///
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001199/// @returns true if there was an error, false otherwise.
Mike Stump11289f42009-09-09 15:08:12 +00001200bool
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001201InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
Mike Stump11289f42009-09-09 15:08:12 +00001202 DesignatedInitExpr *DIE,
Douglas Gregora5324162009-04-15 04:56:10 +00001203 unsigned DesigIdx,
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001204 QualType &CurrentObjectType,
1205 RecordDecl::field_iterator *NextField,
1206 llvm::APSInt *NextElementIndex,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001207 unsigned &Index,
1208 InitListExpr *StructuredList,
Douglas Gregor17bd0942009-01-28 23:36:17 +00001209 unsigned &StructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001210 bool FinishSubobjectInit,
1211 bool TopLevelObject) {
Douglas Gregora5324162009-04-15 04:56:10 +00001212 if (DesigIdx == DIE->size()) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001213 // Check the actual initialization for the designated object type.
1214 bool prevHadError = hadError;
Douglas Gregorf6d27522009-01-29 00:39:20 +00001215
1216 // Temporarily remove the designator expression from the
1217 // initializer list that the child calls see, so that we don't try
1218 // to re-process the designator.
1219 unsigned OldIndex = Index;
1220 IList->setInit(OldIndex, DIE->getInit());
1221
1222 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001223 StructuredList, StructuredIndex);
Douglas Gregorf6d27522009-01-29 00:39:20 +00001224
1225 // Restore the designated initializer expression in the syntactic
1226 // form of the initializer list.
1227 if (IList->getInit(OldIndex) != DIE->getInit())
1228 DIE->setInit(IList->getInit(OldIndex));
1229 IList->setInit(OldIndex, DIE);
1230
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001231 return hadError && !prevHadError;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001232 }
1233
Douglas Gregora5324162009-04-15 04:56:10 +00001234 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump11289f42009-09-09 15:08:12 +00001235 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001236 "Need a non-designated initializer list to start from");
1237
Douglas Gregora5324162009-04-15 04:56:10 +00001238 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001239 // Determine the structural initializer list that corresponds to the
1240 // current subobject.
1241 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump11289f42009-09-09 15:08:12 +00001242 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregor5741efb2009-03-01 17:12:46 +00001243 StructuredList, StructuredIndex,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001244 SourceRange(D->getStartLocation(),
1245 DIE->getSourceRange().getEnd()));
1246 assert(StructuredList && "Expected a structured initializer list");
1247
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001248 if (D->isFieldDesignator()) {
1249 // C99 6.7.8p7:
1250 //
1251 // If a designator has the form
1252 //
1253 // . identifier
1254 //
1255 // then the current object (defined below) shall have
1256 // structure or union type and the identifier shall be the
Mike Stump11289f42009-09-09 15:08:12 +00001257 // name of a member of that type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001258 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001259 if (!RT) {
1260 SourceLocation Loc = D->getDotLoc();
1261 if (Loc.isInvalid())
1262 Loc = D->getFieldLoc();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001263 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1264 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001265 ++Index;
1266 return true;
1267 }
1268
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001269 // Note: we perform a linear search of the fields here, despite
1270 // the fact that we have a faster lookup method, because we always
1271 // need to compute the field's index.
Douglas Gregord5846a12009-04-15 06:41:24 +00001272 FieldDecl *KnownField = D->getField();
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001273 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001274 unsigned FieldIndex = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001275 RecordDecl::field_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001276 Field = RT->getDecl()->field_begin(),
1277 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001278 for (; Field != FieldEnd; ++Field) {
1279 if (Field->isUnnamedBitfield())
1280 continue;
1281
Douglas Gregord5846a12009-04-15 06:41:24 +00001282 if (KnownField == *Field || Field->getIdentifier() == FieldName)
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001283 break;
1284
1285 ++FieldIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001286 }
1287
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001288 if (Field == FieldEnd) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001289 // There was no normal field in the struct with the designated
1290 // name. Perform another lookup for this name, which may find
1291 // something that we can't designate (e.g., a member function),
1292 // may find nothing, or may find a member of an anonymous
Mike Stump11289f42009-09-09 15:08:12 +00001293 // struct/union.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001294 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001295 if (Lookup.first == Lookup.second) {
1296 // Name lookup didn't find anything.
Chris Lattnerb0912a52009-02-24 22:50:46 +00001297 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001298 << FieldName << CurrentObjectType;
Douglas Gregord5846a12009-04-15 06:41:24 +00001299 ++Index;
1300 return true;
1301 } else if (!KnownField && isa<FieldDecl>(*Lookup.first) &&
1302 cast<RecordDecl>((*Lookup.first)->getDeclContext())
1303 ->isAnonymousStructOrUnion()) {
1304 // Handle an field designator that refers to a member of an
1305 // anonymous struct or union.
Mike Stump11289f42009-09-09 15:08:12 +00001306 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
Douglas Gregord5846a12009-04-15 06:41:24 +00001307 cast<FieldDecl>(*Lookup.first),
1308 Field, FieldIndex);
Eli Friedman8d25b092009-04-16 17:49:48 +00001309 D = DIE->getDesignator(DesigIdx);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001310 } else {
1311 // Name lookup found something, but it wasn't a field.
Chris Lattnerb0912a52009-02-24 22:50:46 +00001312 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001313 << FieldName;
Mike Stump11289f42009-09-09 15:08:12 +00001314 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001315 diag::note_field_designator_found);
Eli Friedman8d25b092009-04-16 17:49:48 +00001316 ++Index;
1317 return true;
Douglas Gregord5846a12009-04-15 06:41:24 +00001318 }
1319 } else if (!KnownField &&
1320 cast<RecordDecl>((*Field)->getDeclContext())
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001321 ->isAnonymousStructOrUnion()) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001322 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1323 Field, FieldIndex);
1324 D = DIE->getDesignator(DesigIdx);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001325 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001326
1327 // All of the fields of a union are located at the same place in
1328 // the initializer list.
Douglas Gregor51695702009-01-29 16:53:55 +00001329 if (RT->getDecl()->isUnion()) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001330 FieldIndex = 0;
Douglas Gregor51695702009-01-29 16:53:55 +00001331 StructuredList->setInitializedFieldInUnion(*Field);
1332 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001333
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001334 // Update the designator with the field declaration.
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001335 D->setField(*Field);
Mike Stump11289f42009-09-09 15:08:12 +00001336
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001337 // Make sure that our non-designated initializer list has space
1338 // for a subobject corresponding to this field.
1339 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattnerb0912a52009-02-24 22:50:46 +00001340 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001341
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001342 // This designator names a flexible array member.
1343 if (Field->getType()->isIncompleteArrayType()) {
1344 bool Invalid = false;
Douglas Gregora5324162009-04-15 04:56:10 +00001345 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001346 // We can't designate an object within the flexible array
1347 // member (because GCC doesn't allow it).
Mike Stump11289f42009-09-09 15:08:12 +00001348 DesignatedInitExpr::Designator *NextD
Douglas Gregora5324162009-04-15 04:56:10 +00001349 = DIE->getDesignator(DesigIdx + 1);
Mike Stump11289f42009-09-09 15:08:12 +00001350 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001351 diag::err_designator_into_flexible_array_member)
Mike Stump11289f42009-09-09 15:08:12 +00001352 << SourceRange(NextD->getStartLocation(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001353 DIE->getSourceRange().getEnd());
Chris Lattnerb0912a52009-02-24 22:50:46 +00001354 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001355 << *Field;
1356 Invalid = true;
1357 }
1358
1359 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1360 // The initializer is not an initializer list.
Chris Lattnerb0912a52009-02-24 22:50:46 +00001361 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001362 diag::err_flexible_array_init_needs_braces)
1363 << DIE->getInit()->getSourceRange();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001364 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001365 << *Field;
1366 Invalid = true;
1367 }
1368
1369 // Handle GNU flexible array initializers.
Mike Stump11289f42009-09-09 15:08:12 +00001370 if (!Invalid && !TopLevelObject &&
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001371 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump11289f42009-09-09 15:08:12 +00001372 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001373 diag::err_flexible_array_init_nonempty)
1374 << DIE->getSourceRange().getBegin();
Chris Lattnerb0912a52009-02-24 22:50:46 +00001375 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001376 << *Field;
1377 Invalid = true;
1378 }
1379
1380 if (Invalid) {
1381 ++Index;
1382 return true;
1383 }
1384
1385 // Initialize the array.
1386 bool prevHadError = hadError;
1387 unsigned newStructuredIndex = FieldIndex;
1388 unsigned OldIndex = Index;
1389 IList->setInit(Index, DIE->getInit());
Mike Stump11289f42009-09-09 15:08:12 +00001390 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001391 StructuredList, newStructuredIndex);
1392 IList->setInit(OldIndex, DIE);
1393 if (hadError && !prevHadError) {
1394 ++Field;
1395 ++FieldIndex;
1396 if (NextField)
1397 *NextField = Field;
1398 StructuredIndex = FieldIndex;
1399 return true;
1400 }
1401 } else {
1402 // Recurse to check later designated subobjects.
1403 QualType FieldType = (*Field)->getType();
1404 unsigned newStructuredIndex = FieldIndex;
Douglas Gregora5324162009-04-15 04:56:10 +00001405 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
1406 Index, StructuredList, newStructuredIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001407 true, false))
1408 return true;
1409 }
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001410
1411 // Find the position of the next field to be initialized in this
1412 // subobject.
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001413 ++Field;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001414 ++FieldIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001415
1416 // If this the first designator, our caller will continue checking
1417 // the rest of this struct/class/union subobject.
1418 if (IsFirstDesignator) {
1419 if (NextField)
1420 *NextField = Field;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001421 StructuredIndex = FieldIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001422 return false;
1423 }
1424
Douglas Gregor17bd0942009-01-28 23:36:17 +00001425 if (!FinishSubobjectInit)
1426 return false;
1427
Douglas Gregord5846a12009-04-15 06:41:24 +00001428 // We've already initialized something in the union; we're done.
1429 if (RT->getDecl()->isUnion())
1430 return hadError;
1431
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001432 // Check the remaining fields within this class/struct/union subobject.
1433 bool prevHadError = hadError;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001434 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1435 StructuredList, FieldIndex);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001436 return hadError && !prevHadError;
1437 }
1438
1439 // C99 6.7.8p6:
1440 //
1441 // If a designator has the form
1442 //
1443 // [ constant-expression ]
1444 //
1445 // then the current object (defined below) shall have array
1446 // type and the expression shall be an integer constant
1447 // expression. If the array is of unknown size, any
1448 // nonnegative value is valid.
1449 //
1450 // Additionally, cope with the GNU extension that permits
1451 // designators of the form
1452 //
1453 // [ constant-expression ... constant-expression ]
Chris Lattnerb0912a52009-02-24 22:50:46 +00001454 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001455 if (!AT) {
Chris Lattnerb0912a52009-02-24 22:50:46 +00001456 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001457 << CurrentObjectType;
1458 ++Index;
1459 return true;
1460 }
1461
1462 Expr *IndexExpr = 0;
Douglas Gregor17bd0942009-01-28 23:36:17 +00001463 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1464 if (D->isArrayDesignator()) {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001465 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001466 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor17bd0942009-01-28 23:36:17 +00001467 DesignatedEndIndex = DesignatedStartIndex;
1468 } else {
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001469 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor17bd0942009-01-28 23:36:17 +00001470
Mike Stump11289f42009-09-09 15:08:12 +00001471
1472 DesignatedStartIndex =
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001473 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump11289f42009-09-09 15:08:12 +00001474 DesignatedEndIndex =
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001475 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001476 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor17bd0942009-01-28 23:36:17 +00001477
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001478 if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
Douglas Gregorbf7207a2009-01-29 19:42:23 +00001479 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001480 }
1481
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001482 if (isa<ConstantArrayType>(AT)) {
1483 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor17bd0942009-01-28 23:36:17 +00001484 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1485 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1486 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1487 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1488 if (DesignatedEndIndex >= MaxElements) {
Chris Lattnerb0912a52009-02-24 22:50:46 +00001489 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001490 diag::err_array_designator_too_large)
Douglas Gregor17bd0942009-01-28 23:36:17 +00001491 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001492 << IndexExpr->getSourceRange();
1493 ++Index;
1494 return true;
1495 }
Douglas Gregor17bd0942009-01-28 23:36:17 +00001496 } else {
1497 // Make sure the bit-widths and signedness match.
1498 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1499 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001500 else if (DesignatedStartIndex.getBitWidth() <
1501 DesignatedEndIndex.getBitWidth())
Douglas Gregor17bd0942009-01-28 23:36:17 +00001502 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1503 DesignatedStartIndex.setIsUnsigned(true);
1504 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001505 }
Mike Stump11289f42009-09-09 15:08:12 +00001506
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001507 // Make sure that our non-designated initializer list has space
1508 // for a subobject corresponding to this array element.
Douglas Gregor17bd0942009-01-28 23:36:17 +00001509 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump11289f42009-09-09 15:08:12 +00001510 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor17bd0942009-01-28 23:36:17 +00001511 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001512
Douglas Gregor17bd0942009-01-28 23:36:17 +00001513 // Repeatedly perform subobject initializations in the range
1514 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001515
Douglas Gregor17bd0942009-01-28 23:36:17 +00001516 // Move to the next designator
1517 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1518 unsigned OldIndex = Index;
Douglas Gregor17bd0942009-01-28 23:36:17 +00001519 while (DesignatedStartIndex <= DesignatedEndIndex) {
1520 // Recurse to check later designated subobjects.
1521 QualType ElementType = AT->getElementType();
1522 Index = OldIndex;
Douglas Gregora5324162009-04-15 04:56:10 +00001523 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
1524 Index, StructuredList, ElementIndex,
Douglas Gregorfc4f8a12009-02-04 22:46:25 +00001525 (DesignatedStartIndex == DesignatedEndIndex),
1526 false))
Douglas Gregor17bd0942009-01-28 23:36:17 +00001527 return true;
1528
1529 // Move to the next index in the array that we'll be initializing.
1530 ++DesignatedStartIndex;
1531 ElementIndex = DesignatedStartIndex.getZExtValue();
1532 }
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001533
1534 // If this the first designator, our caller will continue checking
1535 // the rest of this array subobject.
1536 if (IsFirstDesignator) {
1537 if (NextElementIndex)
Douglas Gregor17bd0942009-01-28 23:36:17 +00001538 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001539 StructuredIndex = ElementIndex;
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001540 return false;
1541 }
Mike Stump11289f42009-09-09 15:08:12 +00001542
Douglas Gregor17bd0942009-01-28 23:36:17 +00001543 if (!FinishSubobjectInit)
1544 return false;
1545
Douglas Gregord7fb85e2009-01-22 23:26:18 +00001546 // Check the remaining elements within this array subobject.
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001547 bool prevHadError = hadError;
Douglas Gregoraef040a2009-02-09 19:45:19 +00001548 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001549 StructuredList, ElementIndex);
Mike Stump11289f42009-09-09 15:08:12 +00001550 return hadError && !prevHadError;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001551}
1552
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001553// Get the structured initializer list for a subobject of type
1554// @p CurrentObjectType.
1555InitListExpr *
1556InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1557 QualType CurrentObjectType,
1558 InitListExpr *StructuredList,
1559 unsigned StructuredIndex,
1560 SourceRange InitRange) {
1561 Expr *ExistingInit = 0;
1562 if (!StructuredList)
1563 ExistingInit = SyntacticToSemantic[IList];
1564 else if (StructuredIndex < StructuredList->getNumInits())
1565 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump11289f42009-09-09 15:08:12 +00001566
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001567 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1568 return Result;
1569
1570 if (ExistingInit) {
1571 // We are creating an initializer list that initializes the
1572 // subobjects of the current object, but there was already an
1573 // initialization that completely initialized the current
1574 // subobject, e.g., by a compound literal:
Mike Stump11289f42009-09-09 15:08:12 +00001575 //
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001576 // struct X { int a, b; };
1577 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump11289f42009-09-09 15:08:12 +00001578 //
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001579 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1580 // designated initializer re-initializes the whole
1581 // subobject [0], overwriting previous initializers.
Mike Stump11289f42009-09-09 15:08:12 +00001582 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregor5741efb2009-03-01 17:12:46 +00001583 diag::warn_subobject_initializer_overrides)
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001584 << InitRange;
Mike Stump11289f42009-09-09 15:08:12 +00001585 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001586 diag::note_previous_initializer)
Douglas Gregore6af7a02009-01-28 23:43:32 +00001587 << /*FIXME:has side effects=*/0
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001588 << ExistingInit->getSourceRange();
1589 }
1590
Mike Stump11289f42009-09-09 15:08:12 +00001591 InitListExpr *Result
1592 = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
Douglas Gregor5741efb2009-03-01 17:12:46 +00001593 InitRange.getEnd());
1594
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001595 Result->setType(CurrentObjectType);
1596
Douglas Gregor6d00c992009-03-20 23:58:33 +00001597 // Pre-allocate storage for the structured initializer list.
1598 unsigned NumElements = 0;
Douglas Gregor221c9a52009-03-21 18:13:52 +00001599 unsigned NumInits = 0;
1600 if (!StructuredList)
1601 NumInits = IList->getNumInits();
1602 else if (Index < IList->getNumInits()) {
1603 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1604 NumInits = SubList->getNumInits();
1605 }
1606
Mike Stump11289f42009-09-09 15:08:12 +00001607 if (const ArrayType *AType
Douglas Gregor6d00c992009-03-20 23:58:33 +00001608 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1609 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1610 NumElements = CAType->getSize().getZExtValue();
1611 // Simple heuristic so that we don't allocate a very large
1612 // initializer with many empty entries at the end.
Douglas Gregor221c9a52009-03-21 18:13:52 +00001613 if (NumInits && NumElements > NumInits)
Douglas Gregor6d00c992009-03-20 23:58:33 +00001614 NumElements = 0;
1615 }
1616 } else if (const VectorType *VType = CurrentObjectType->getAsVectorType())
1617 NumElements = VType->getNumElements();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001618 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregor6d00c992009-03-20 23:58:33 +00001619 RecordDecl *RDecl = RType->getDecl();
1620 if (RDecl->isUnion())
1621 NumElements = 1;
1622 else
Mike Stump11289f42009-09-09 15:08:12 +00001623 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001624 RDecl->field_end());
Douglas Gregor6d00c992009-03-20 23:58:33 +00001625 }
1626
Douglas Gregor221c9a52009-03-21 18:13:52 +00001627 if (NumElements < NumInits)
Douglas Gregor6d00c992009-03-20 23:58:33 +00001628 NumElements = IList->getNumInits();
1629
1630 Result->reserveInits(NumElements);
1631
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001632 // Link this new initializer list into the structured initializer
1633 // lists.
1634 if (StructuredList)
1635 StructuredList->updateInit(StructuredIndex, Result);
1636 else {
1637 Result->setSyntacticForm(IList);
1638 SyntacticToSemantic[IList] = Result;
1639 }
1640
1641 return Result;
1642}
1643
1644/// Update the initializer at index @p StructuredIndex within the
1645/// structured initializer list to the value @p expr.
1646void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1647 unsigned &StructuredIndex,
1648 Expr *expr) {
1649 // No structured initializer list to update
1650 if (!StructuredList)
1651 return;
1652
1653 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1654 // This initializer overwrites a previous initializer. Warn.
Mike Stump11289f42009-09-09 15:08:12 +00001655 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001656 diag::warn_initializer_overrides)
1657 << expr->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001658 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001659 diag::note_previous_initializer)
Douglas Gregore6af7a02009-01-28 23:43:32 +00001660 << /*FIXME:has side effects=*/0
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001661 << PrevInit->getSourceRange();
1662 }
Mike Stump11289f42009-09-09 15:08:12 +00001663
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001664 ++StructuredIndex;
1665}
1666
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001667/// Check that the given Index expression is a valid array designator
1668/// value. This is essentailly just a wrapper around
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001669/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001670/// and produces a reasonable diagnostic if there is a
1671/// failure. Returns true if there was an error, false otherwise. If
1672/// everything went okay, Value will receive the value of the constant
1673/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00001674static bool
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001675CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001676 SourceLocation Loc = Index->getSourceRange().getBegin();
1677
1678 // Make sure this is an integer constant expression.
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001679 if (S.VerifyIntegerConstantExpression(Index, &Value))
1680 return true;
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001681
Chris Lattnerc71d08b2009-04-25 21:59:05 +00001682 if (Value.isSigned() && Value.isNegative())
1683 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001684 << Value.toString(10) << Index->getSourceRange();
1685
Douglas Gregor51650d32009-01-23 21:04:18 +00001686 Value.setIsUnsigned(true);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001687 return false;
1688}
1689
1690Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1691 SourceLocation Loc,
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00001692 bool GNUSyntax,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001693 OwningExprResult Init) {
1694 typedef DesignatedInitExpr::Designator ASTDesignator;
1695
1696 bool Invalid = false;
1697 llvm::SmallVector<ASTDesignator, 32> Designators;
1698 llvm::SmallVector<Expr *, 32> InitExpressions;
1699
1700 // Build designators and check array designator expressions.
1701 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1702 const Designator &D = Desig.getDesignator(Idx);
1703 switch (D.getKind()) {
1704 case Designator::FieldDesignator:
Mike Stump11289f42009-09-09 15:08:12 +00001705 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001706 D.getFieldLoc()));
1707 break;
1708
1709 case Designator::ArrayDesignator: {
1710 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1711 llvm::APSInt IndexValue;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001712 if (!Index->isTypeDependent() &&
1713 !Index->isValueDependent() &&
1714 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001715 Invalid = true;
1716 else {
1717 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump11289f42009-09-09 15:08:12 +00001718 D.getLBracketLoc(),
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001719 D.getRBracketLoc()));
1720 InitExpressions.push_back(Index);
1721 }
1722 break;
1723 }
1724
1725 case Designator::ArrayRangeDesignator: {
1726 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1727 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1728 llvm::APSInt StartValue;
1729 llvm::APSInt EndValue;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001730 bool StartDependent = StartIndex->isTypeDependent() ||
1731 StartIndex->isValueDependent();
1732 bool EndDependent = EndIndex->isTypeDependent() ||
1733 EndIndex->isValueDependent();
1734 if ((!StartDependent &&
1735 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1736 (!EndDependent &&
1737 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001738 Invalid = true;
Douglas Gregor7a95b082009-01-23 22:22:29 +00001739 else {
1740 // Make sure we're comparing values with the same bit width.
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001741 if (StartDependent || EndDependent) {
1742 // Nothing to compute.
1743 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Douglas Gregor7a95b082009-01-23 22:22:29 +00001744 EndValue.extend(StartValue.getBitWidth());
1745 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1746 StartValue.extend(EndValue.getBitWidth());
1747
Douglas Gregor0f9d4002009-05-21 23:30:39 +00001748 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregor7a95b082009-01-23 22:22:29 +00001749 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump11289f42009-09-09 15:08:12 +00001750 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregor7a95b082009-01-23 22:22:29 +00001751 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1752 Invalid = true;
1753 } else {
1754 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump11289f42009-09-09 15:08:12 +00001755 D.getLBracketLoc(),
Douglas Gregor7a95b082009-01-23 22:22:29 +00001756 D.getEllipsisLoc(),
1757 D.getRBracketLoc()));
1758 InitExpressions.push_back(StartIndex);
1759 InitExpressions.push_back(EndIndex);
1760 }
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001761 }
1762 break;
1763 }
1764 }
1765 }
1766
1767 if (Invalid || Init.isInvalid())
1768 return ExprError();
1769
1770 // Clear out the expressions within the designation.
1771 Desig.ClearExprs(*this);
1772
1773 DesignatedInitExpr *DIE
Jay Foad7d0479f2009-05-21 09:52:38 +00001774 = DesignatedInitExpr::Create(Context,
1775 Designators.data(), Designators.size(),
1776 InitExpressions.data(), InitExpressions.size(),
Anders Carlssonb781bcd2009-05-01 19:49:17 +00001777 Loc, GNUSyntax, Init.takeAs<Expr>());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001778 return Owned(DIE);
1779}
Douglas Gregor85df8d82009-01-29 00:45:39 +00001780
1781bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattnerb0912a52009-02-24 22:50:46 +00001782 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregor85df8d82009-01-29 00:45:39 +00001783 if (!CheckInitList.HadError())
1784 InitList = CheckInitList.getFullyStructuredList();
1785
1786 return CheckInitList.HadError();
1787}
Douglas Gregora5c9e1a2009-02-02 17:43:21 +00001788
1789/// \brief Diagnose any semantic errors with value-initialization of
1790/// the given type.
1791///
1792/// Value-initialization effectively zero-initializes any types
1793/// without user-declared constructors, and calls the default
1794/// constructor for a for any type that has a user-declared
1795/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1796/// a type with a user-declared constructor does not have an
1797/// accessible, non-deleted default constructor. In C, everything can
1798/// be value-initialized, which corresponds to C's notion of
1799/// initializing objects with static storage duration when no
Mike Stump11289f42009-09-09 15:08:12 +00001800/// initializer is provided for that object.
Douglas Gregora5c9e1a2009-02-02 17:43:21 +00001801///
1802/// \returns true if there was an error, false otherwise.
1803bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1804 // C++ [dcl.init]p5:
1805 //
1806 // To value-initialize an object of type T means:
1807
1808 // -- if T is an array type, then each element is value-initialized;
1809 if (const ArrayType *AT = Context.getAsArrayType(Type))
1810 return CheckValueInitialization(AT->getElementType(), Loc);
1811
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001812 if (const RecordType *RT = Type->getAs<RecordType>()) {
Douglas Gregor89ee6822009-02-28 01:32:25 +00001813 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Douglas Gregora5c9e1a2009-02-02 17:43:21 +00001814 // -- if T is a class type (clause 9) with a user-declared
1815 // constructor (12.1), then the default constructor for T is
1816 // called (and the initialization is ill-formed if T has no
1817 // accessible default constructor);
Douglas Gregor5d3507d2009-09-09 23:08:42 +00001818 if (ClassDecl->hasUserDeclaredConstructor()) {
1819 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
1820
1821 CXXConstructorDecl *Constructor
1822 = PerformInitializationByConstructor(Type,
1823 MultiExprArg(*this, 0, 0),
1824 Loc, SourceRange(Loc),
1825 DeclarationName(),
1826 IK_Direct,
1827 ConstructorArgs);
1828 if (!Constructor)
1829 return true;
1830
1831 OwningExprResult Init
1832 = BuildCXXConstructExpr(Loc, Type, Constructor,
1833 move_arg(ConstructorArgs));
1834 if (Init.isInvalid())
1835 return true;
1836
1837 // FIXME: Actually perform the value-initialization!
1838 return false;
1839 }
Douglas Gregora5c9e1a2009-02-02 17:43:21 +00001840 }
1841 }
1842
1843 if (Type->isReferenceType()) {
1844 // C++ [dcl.init]p5:
1845 // [...] A program that calls for default-initialization or
1846 // value-initialization of an entity of reference type is
1847 // ill-formed. [...]
Mike Stump87c57ac2009-05-16 07:39:55 +00001848 // FIXME: Once we have code that goes through this path, add an actual
1849 // diagnostic :)
Douglas Gregora5c9e1a2009-02-02 17:43:21 +00001850 }
1851
1852 return false;
1853}