blob: 6574524d14d0d67df2416d9346e2a9c491c2f89e [file] [log] [blame]
Steve Naroff0cca7492008-05-01 22:18:59 +00001//===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerdd8e0062009-02-24 22:27:37 +000010// This file implements semantic analysis for initializers. The main entry
11// point is Sema::CheckInitList(), but all of the work is performed
12// within the InitListChecker class.
13//
Chris Lattner8b419b92009-02-24 22:48:58 +000014// This file also implements Sema::CheckInitializerTypes.
Steve Naroff0cca7492008-05-01 22:18:59 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "Sema.h"
Douglas Gregor05c13a32009-01-22 00:58:24 +000019#include "clang/Parse/Designator.h"
Steve Naroff0cca7492008-05-01 22:18:59 +000020#include "clang/AST/ASTContext.h"
Anders Carlsson2078bb92009-05-27 16:10:08 +000021#include "clang/AST/ExprCXX.h"
Chris Lattner79e079d2009-02-24 23:10:27 +000022#include "clang/AST/ExprObjC.h"
Douglas Gregorc34ee5e2009-01-29 00:45:39 +000023#include <map>
Douglas Gregor05c13a32009-01-22 00:58:24 +000024using namespace clang;
Steve Naroff0cca7492008-05-01 22:18:59 +000025
Chris Lattnerdd8e0062009-02-24 22:27:37 +000026//===----------------------------------------------------------------------===//
27// Sema Initialization Checking
28//===----------------------------------------------------------------------===//
29
Chris Lattner79e079d2009-02-24 23:10:27 +000030static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
Chris Lattner8879e3b2009-02-26 23:26:43 +000031 const ArrayType *AT = Context.getAsArrayType(DeclType);
32 if (!AT) return 0;
33
Eli Friedman8718a6a2009-05-29 18:22:49 +000034 if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
35 return 0;
36
Chris Lattner8879e3b2009-02-26 23:26:43 +000037 // See if this is a string literal or @encode.
38 Init = Init->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +000039
Chris Lattner8879e3b2009-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 Lattner220b6362009-02-26 23:42:47 +000046 if (SL == 0) return 0;
Eli Friedmanbb6415c2009-05-31 10:54:53 +000047
48 QualType ElemTy = Context.getCanonicalType(AT->getElementType());
Chris Lattner8879e3b2009-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 Friedmanbb6415c2009-05-31 10:54:53 +000052 return ElemTy->isCharType() ? Init : 0;
Chris Lattner8879e3b2009-02-26 23:26:43 +000053
Eli Friedmanbb6415c2009-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 Lattner8879e3b2009-02-26 23:26:43 +000060 return Init;
Mike Stump1eb44332009-09-09 15:08:12 +000061
Chris Lattnerdd8e0062009-02-24 22:27:37 +000062 return 0;
63}
64
Mike Stump1eb44332009-09-09 15:08:12 +000065static bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
Chris Lattner95e8d652009-02-24 22:46:58 +000066 bool DirectInit, Sema &S) {
Chris Lattnerdd8e0062009-02-24 22:27:37 +000067 // Get the type before calling CheckSingleAssignmentConstraints(), since
68 // it can promote the expression.
Mike Stump1eb44332009-09-09 15:08:12 +000069 QualType InitType = Init->getType();
70
Chris Lattner95e8d652009-02-24 22:46:58 +000071 if (S.getLangOptions().CPlusPlus) {
Chris Lattnerdd8e0062009-02-24 22:27:37 +000072 // FIXME: I dislike this error message. A lot.
Fariborz Jahanian34acd3e2009-09-15 19:12:21 +000073 if (S.PerformImplicitConversion(Init, DeclType,
74 "initializing", DirectInit)) {
75 ImplicitConversionSequence ICS;
76 OverloadCandidateSet CandidateSet;
77 if (S.IsUserDefinedConversion(Init, DeclType, ICS.UserDefined,
78 CandidateSet,
79 true, false, false) != S.OR_Ambiguous)
80 return S.Diag(Init->getSourceRange().getBegin(),
81 diag::err_typecheck_convert_incompatible)
82 << DeclType << Init->getType() << "initializing"
83 << Init->getSourceRange();
84 S.Diag(Init->getSourceRange().getBegin(),
85 diag::err_typecheck_convert_ambiguous)
86 << DeclType << Init->getType() << Init->getSourceRange();
87 S.PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
88 return true;
89 }
Chris Lattnerdd8e0062009-02-24 22:27:37 +000090 return false;
91 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner95e8d652009-02-24 22:46:58 +000093 Sema::AssignConvertType ConvTy =
94 S.CheckSingleAssignmentConstraints(DeclType, Init);
95 return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
Chris Lattnerdd8e0062009-02-24 22:27:37 +000096 InitType, Init, "initializing");
97}
98
Chris Lattner79e079d2009-02-24 23:10:27 +000099static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
100 // Get the length of the string as parsed.
101 uint64_t StrLength =
102 cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
103
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattner79e079d2009-02-24 23:10:27 +0000105 const ArrayType *AT = S.Context.getAsArrayType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000106 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 // C99 6.7.8p14. We have an array of character type with unknown size
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000108 // being initialized to a string literal.
109 llvm::APSInt ConstVal(32);
Chris Lattner19da8cd2009-02-24 23:01:39 +0000110 ConstVal = StrLength;
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000111 // Return a new array type (C99 6.7.8p22).
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000112 DeclT = S.Context.getConstantArrayWithoutExprType(IAT->getElementType(),
113 ConstVal,
114 ArrayType::Normal, 0);
Chris Lattner19da8cd2009-02-24 23:01:39 +0000115 return;
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Eli Friedman8718a6a2009-05-29 18:22:49 +0000118 const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Eli Friedman8718a6a2009-05-29 18:22:49 +0000120 // C99 6.7.8p14. We have an array of character type with known size. However,
121 // the size may be smaller or larger than the string we are initializing.
122 // FIXME: Avoid truncation for 64-bit length strings.
123 if (StrLength-1 > CAT->getSize().getZExtValue())
124 S.Diag(Str->getSourceRange().getBegin(),
125 diag::warn_initializer_string_for_char_array_too_long)
126 << Str->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Eli Friedman8718a6a2009-05-29 18:22:49 +0000128 // Set the type to the actual size that we are initializing. If we have
129 // something like:
130 // char x[1] = "foo";
131 // then this will set the string literal's type to char[1].
132 Str->setType(DeclT);
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000133}
134
135bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
136 SourceLocation InitLoc,
Anders Carlsson0f5f2c62009-05-30 20:41:30 +0000137 DeclarationName InitEntity, bool DirectInit) {
Mike Stump1eb44332009-09-09 15:08:12 +0000138 if (DeclType->isDependentType() ||
Douglas Gregor9ea62762009-05-21 23:17:49 +0000139 Init->isTypeDependent() || Init->isValueDependent())
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000140 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000142 // C++ [dcl.init.ref]p1:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000143 // A variable declared to be a T& or T&&, that is "reference to type T"
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000144 // (8.3.2), shall be initialized by an object, or function, of
145 // type T or by an object that can be converted into a T.
146 if (DeclType->isReferenceType())
Mike Stump1eb44332009-09-09 15:08:12 +0000147 return CheckReferenceInit(Init, DeclType,
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000148 /*SuppressUserConversions=*/false,
149 /*AllowExplicit=*/DirectInit,
150 /*ForceRValue=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000152 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
153 // of unknown size ("[]") or an object type that is not a variable array type.
154 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
155 return Diag(InitLoc, diag::err_variable_object_no_init)
156 << VAT->getSizeExpr()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000158 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
159 if (!InitList) {
160 // FIXME: Handle wide strings
Chris Lattner79e079d2009-02-24 23:10:27 +0000161 if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
162 CheckStringInit(Str, DeclType, *this);
Chris Lattner19da8cd2009-02-24 23:01:39 +0000163 return false;
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000166 // C++ [dcl.init]p14:
167 // -- If the destination type is a (possibly cv-qualified) class
168 // type:
169 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
170 QualType DeclTypeC = Context.getCanonicalType(DeclType);
171 QualType InitTypeC = Context.getCanonicalType(Init->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000173 // -- If the initialization is direct-initialization, or if it is
174 // copy-initialization where the cv-unqualified version of the
175 // source type is the same class as, or a derived class of, the
176 // class of the destination, constructors are considered.
177 if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) ||
178 IsDerivedFrom(InitTypeC, DeclTypeC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000179 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000180 cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Anders Carlssonbffed8a2009-05-27 16:38:58 +0000182 // No need to make a CXXConstructExpr if both the ctor and dtor are
183 // trivial.
184 if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor())
185 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Douglas Gregor39da0b82009-09-09 23:08:42 +0000187 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
188
Mike Stump1eb44332009-09-09 15:08:12 +0000189 CXXConstructorDecl *Constructor
Douglas Gregor39da0b82009-09-09 23:08:42 +0000190 = PerformInitializationByConstructor(DeclType,
191 MultiExprArg(*this,
192 (void **)&Init, 1),
193 InitLoc, Init->getSourceRange(),
194 InitEntity,
195 DirectInit? IK_Direct : IK_Copy,
196 ConstructorArgs);
Anders Carlsson2078bb92009-05-27 16:10:08 +0000197 if (!Constructor)
198 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
200 OwningExprResult InitResult =
Anders Carlssonec8e5ea2009-09-05 07:40:38 +0000201 BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000202 DeclType, Constructor,
Douglas Gregor39da0b82009-09-09 23:08:42 +0000203 move_arg(ConstructorArgs));
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000204 if (InitResult.isInvalid())
205 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000207 Init = InitResult.takeAs<Expr>();
Anders Carlsson2078bb92009-05-27 16:10:08 +0000208 return false;
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000211 // -- Otherwise (i.e., for the remaining copy-initialization
212 // cases), user-defined conversion sequences that can
213 // convert from the source type to the destination type or
214 // (when a conversion function is used) to a derived class
215 // thereof are enumerated as described in 13.3.1.4, and the
216 // best one is chosen through overload resolution
217 // (13.3). If the conversion cannot be done or is
218 // ambiguous, the initialization is ill-formed. The
219 // function selected is called with the initializer
220 // expression as its argument; if the function is a
221 // constructor, the call initializes a temporary of the
222 // destination type.
Mike Stump390b4cc2009-05-16 07:39:55 +0000223 // FIXME: We're pretending to do copy elision here; return to this when we
224 // have ASTs for such things.
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000225 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
226 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000228 if (InitEntity)
229 return Diag(InitLoc, diag::err_cannot_initialize_decl)
Chris Lattnerb78d8332009-06-26 04:45:06 +0000230 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
231 << Init->getType() << Init->getSourceRange();
232 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000233 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
234 << Init->getType() << Init->getSourceRange();
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000237 // C99 6.7.8p16.
238 if (DeclType->isArrayType())
239 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
Chris Lattnerb78d8332009-06-26 04:45:06 +0000240 << Init->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner95e8d652009-02-24 22:46:58 +0000242 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Mike Stump1eb44332009-09-09 15:08:12 +0000243 }
244
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000245 bool hadError = CheckInitList(InitList, DeclType);
246 Init = InitList;
247 return hadError;
248}
249
250//===----------------------------------------------------------------------===//
251// Semantic checking for initializer lists.
252//===----------------------------------------------------------------------===//
253
Douglas Gregor9e80f722009-01-29 01:05:33 +0000254/// @brief Semantic checking for initializer lists.
255///
256/// The InitListChecker class contains a set of routines that each
257/// handle the initialization of a certain kind of entity, e.g.,
258/// arrays, vectors, struct/union types, scalars, etc. The
259/// InitListChecker itself performs a recursive walk of the subobject
260/// structure of the type to be initialized, while stepping through
261/// the initializer list one element at a time. The IList and Index
262/// parameters to each of the Check* routines contain the active
263/// (syntactic) initializer list and the index into that initializer
264/// list that represents the current initializer. Each routine is
265/// responsible for moving that Index forward as it consumes elements.
266///
267/// Each Check* routine also has a StructuredList/StructuredIndex
268/// arguments, which contains the current the "structured" (semantic)
269/// initializer list and the index into that initializer list where we
270/// are copying initializers as we map them over to the semantic
271/// list. Once we have completed our recursive walk of the subobject
272/// structure, we will have constructed a full semantic initializer
273/// list.
274///
275/// C99 designators cause changes in the initializer list traversal,
276/// because they make the initialization "jump" into a specific
277/// subobject and then continue the initialization from that
278/// point. CheckDesignatedInitializer() recursively steps into the
279/// designated subobject and manages backing out the recursion to
280/// initialize the subobjects after the one designated.
Chris Lattner8b419b92009-02-24 22:48:58 +0000281namespace {
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000282class InitListChecker {
Chris Lattner08202542009-02-24 22:50:46 +0000283 Sema &SemaRef;
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000284 bool hadError;
285 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
286 InitListExpr *FullyStructuredList;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
288 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000289 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000290 unsigned &StructuredIndex,
291 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000292 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000293 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000294 unsigned &StructuredIndex,
295 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000296 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
297 bool SubobjectIsDesignatorContext,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000298 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000299 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000300 unsigned &StructuredIndex,
301 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000302 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000303 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000304 InitListExpr *StructuredList,
305 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000306 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000307 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000308 InitListExpr *StructuredList,
309 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000310 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000311 unsigned &Index,
312 InitListExpr *StructuredList,
313 unsigned &StructuredIndex);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000314 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000315 InitListExpr *StructuredList,
316 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000317 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
318 RecordDecl::field_iterator Field,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000319 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000320 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000321 unsigned &StructuredIndex,
322 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000323 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
324 llvm::APSInt elementIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000325 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000326 InitListExpr *StructuredList,
327 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000328 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +0000329 unsigned DesigIdx,
Mike Stump1eb44332009-09-09 15:08:12 +0000330 QualType &CurrentObjectType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000331 RecordDecl::field_iterator *NextField,
332 llvm::APSInt *NextElementIndex,
333 unsigned &Index,
334 InitListExpr *StructuredList,
335 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000336 bool FinishSubobjectInit,
337 bool TopLevelObject);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000338 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
339 QualType CurrentObjectType,
340 InitListExpr *StructuredList,
341 unsigned StructuredIndex,
342 SourceRange InitRange);
Douglas Gregor9e80f722009-01-29 01:05:33 +0000343 void UpdateStructuredListElement(InitListExpr *StructuredList,
344 unsigned &StructuredIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000345 Expr *expr);
346 int numArrayElements(QualType DeclType);
347 int numStructUnionElements(QualType DeclType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000348
349 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000350public:
Chris Lattner08202542009-02-24 22:50:46 +0000351 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000352 bool HadError() { return hadError; }
353
354 // @brief Retrieves the fully-structured initializer list used for
355 // semantic analysis and code generation.
356 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
357};
Chris Lattner8b419b92009-02-24 22:48:58 +0000358} // end anonymous namespace
Chris Lattner68355a52009-01-29 05:10:57 +0000359
Douglas Gregor4c678342009-01-28 21:54:33 +0000360/// Recursively replaces NULL values within the given initializer list
361/// with expressions that perform value-initialization of the
362/// appropriate type.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000363void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Mike Stump1eb44332009-09-09 15:08:12 +0000364 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregor930d8b52009-01-30 22:09:00 +0000365 "Should not have void type");
Douglas Gregor87fd7032009-02-02 17:43:21 +0000366 SourceLocation Loc = ILE->getSourceRange().getBegin();
367 if (ILE->getSyntacticForm())
368 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek6217b802009-07-29 21:53:49 +0000370 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000371 unsigned Init = 0, NumInits = ILE->getNumInits();
Mike Stump1eb44332009-09-09 15:08:12 +0000372 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000373 Field = RType->getDecl()->field_begin(),
374 FieldEnd = RType->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000375 Field != FieldEnd; ++Field) {
376 if (Field->isUnnamedBitfield())
377 continue;
378
Douglas Gregor87fd7032009-02-02 17:43:21 +0000379 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000380 if (Field->getType()->isReferenceType()) {
381 // C++ [dcl.init.aggr]p9:
382 // If an incomplete or empty initializer-list leaves a
383 // member of reference type uninitialized, the program is
Mike Stump1eb44332009-09-09 15:08:12 +0000384 // ill-formed.
Chris Lattner08202542009-02-24 22:50:46 +0000385 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000386 << Field->getType()
387 << ILE->getSyntacticForm()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000388 SemaRef.Diag(Field->getLocation(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000389 diag::note_uninit_reference_member);
390 hadError = true;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000391 return;
Chris Lattner08202542009-02-24 22:50:46 +0000392 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000393 hadError = true;
394 return;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000395 }
Douglas Gregor87fd7032009-02-02 17:43:21 +0000396
Mike Stump390b4cc2009-05-16 07:39:55 +0000397 // FIXME: If value-initialization involves calling a constructor, should
398 // we make that call explicit in the representation (even when it means
399 // extending the initializer list)?
Douglas Gregor87fd7032009-02-02 17:43:21 +0000400 if (Init < NumInits && !hadError)
Mike Stump1eb44332009-09-09 15:08:12 +0000401 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000402 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000403 } else if (InitListExpr *InnerILE
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000404 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000405 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000406 ++Init;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000407
408 // Only look at the first initialization of a union.
409 if (RType->getDecl()->isUnion())
410 break;
Douglas Gregor4c678342009-01-28 21:54:33 +0000411 }
412
413 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000414 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000415
416 QualType ElementType;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Douglas Gregor87fd7032009-02-02 17:43:21 +0000418 unsigned NumInits = ILE->getNumInits();
419 unsigned NumElements = NumInits;
Chris Lattner08202542009-02-24 22:50:46 +0000420 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000421 ElementType = AType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000422 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
423 NumElements = CAType->getSize().getZExtValue();
424 } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000425 ElementType = VType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000426 NumElements = VType->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000427 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000428 ElementType = ILE->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregor87fd7032009-02-02 17:43:21 +0000430 for (unsigned Init = 0; Init != NumElements; ++Init) {
431 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattner08202542009-02-24 22:50:46 +0000432 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000433 hadError = true;
434 return;
435 }
436
Mike Stump390b4cc2009-05-16 07:39:55 +0000437 // FIXME: If value-initialization involves calling a constructor, should
438 // we make that call explicit in the representation (even when it means
439 // extending the initializer list)?
Douglas Gregor87fd7032009-02-02 17:43:21 +0000440 if (Init < NumInits && !hadError)
Mike Stump1eb44332009-09-09 15:08:12 +0000441 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000442 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000443 } else if (InitListExpr *InnerILE
444 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000445 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000446 }
447}
448
Chris Lattner68355a52009-01-29 05:10:57 +0000449
Chris Lattner08202542009-02-24 22:50:46 +0000450InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
451 : SemaRef(S) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000452 hadError = false;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000453
Eli Friedmanb85f7072008-05-19 19:16:24 +0000454 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000455 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000456 FullyStructuredList
Douglas Gregored8a93d2009-03-01 17:12:46 +0000457 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000458 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
459 /*TopLevelObject=*/true);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000460
Douglas Gregor930d8b52009-01-30 22:09:00 +0000461 if (!hadError)
462 FillInValueInitializations(FullyStructuredList);
Steve Naroff0cca7492008-05-01 22:18:59 +0000463}
464
465int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +0000466 // FIXME: use a proper constant
467 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000468 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000469 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000470 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
471 }
472 return maxElements;
473}
474
475int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000476 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor4c678342009-01-28 21:54:33 +0000477 int InitializableMembers = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000478 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000479 Field = structDecl->field_begin(),
480 FieldEnd = structDecl->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000481 Field != FieldEnd; ++Field) {
482 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
483 ++InitializableMembers;
484 }
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000485 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +0000486 return std::min(InitializableMembers, 1);
487 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-05-01 22:18:59 +0000488}
489
Mike Stump1eb44332009-09-09 15:08:12 +0000490void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregor4c678342009-01-28 21:54:33 +0000491 QualType T, unsigned &Index,
492 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000493 unsigned &StructuredIndex,
494 bool TopLevelObject) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000495 int maxElements = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Steve Naroff0cca7492008-05-01 22:18:59 +0000497 if (T->isArrayType())
498 maxElements = numArrayElements(T);
499 else if (T->isStructureType() || T->isUnionType())
500 maxElements = numStructUnionElements(T);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000501 else if (T->isVectorType())
502 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +0000503 else
504 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +0000505
Eli Friedman402256f2008-05-25 13:49:22 +0000506 if (maxElements == 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000507 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedman402256f2008-05-25 13:49:22 +0000508 diag::err_implicit_empty_initializer);
Douglas Gregor4c678342009-01-28 21:54:33 +0000509 ++Index;
Eli Friedman402256f2008-05-25 13:49:22 +0000510 hadError = true;
511 return;
512 }
513
Douglas Gregor4c678342009-01-28 21:54:33 +0000514 // Build a structured initializer list corresponding to this subobject.
515 InitListExpr *StructuredSubobjectInitList
Mike Stump1eb44332009-09-09 15:08:12 +0000516 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
517 StructuredIndex,
Douglas Gregored8a93d2009-03-01 17:12:46 +0000518 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
519 ParentIList->getSourceRange().getEnd()));
Douglas Gregor4c678342009-01-28 21:54:33 +0000520 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000521
Douglas Gregor4c678342009-01-28 21:54:33 +0000522 // Check the element types and build the structural subobject.
Douglas Gregor87fd7032009-02-02 17:43:21 +0000523 unsigned StartIndex = Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000524 CheckListElementTypes(ParentIList, T, false, Index,
Mike Stump1eb44332009-09-09 15:08:12 +0000525 StructuredSubobjectInitList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000526 StructuredSubobjectInitIndex,
527 TopLevelObject);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000528 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregora6457962009-03-20 00:32:56 +0000529 StructuredSubobjectInitList->setType(T);
530
Douglas Gregored8a93d2009-03-01 17:12:46 +0000531 // Update the structured sub-object initializer so that it's ending
Douglas Gregor87fd7032009-02-02 17:43:21 +0000532 // range corresponds with the end of the last initializer it used.
533 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000534 SourceLocation EndLoc
Douglas Gregor87fd7032009-02-02 17:43:21 +0000535 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
536 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
537 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000538}
539
Steve Naroffa647caa2008-05-06 00:23:44 +0000540void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor4c678342009-01-28 21:54:33 +0000541 unsigned &Index,
542 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000543 unsigned &StructuredIndex,
544 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000545 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor4c678342009-01-28 21:54:33 +0000546 SyntacticToSemantic[IList] = StructuredList;
547 StructuredList->setSyntacticForm(IList);
Mike Stump1eb44332009-09-09 15:08:12 +0000548 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000549 StructuredIndex, TopLevelObject);
Steve Naroffa647caa2008-05-06 00:23:44 +0000550 IList->setType(T);
Douglas Gregor4c678342009-01-28 21:54:33 +0000551 StructuredList->setType(T);
Eli Friedman638e1442008-05-25 13:22:35 +0000552 if (hadError)
553 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000554
Eli Friedman638e1442008-05-25 13:22:35 +0000555 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000556 // We have leftover initializers
Eli Friedmane5408582009-05-29 20:20:05 +0000557 if (StructuredIndex == 1 &&
558 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000559 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000560 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000561 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000562 hadError = true;
563 }
Eli Friedmanbb504d32008-05-19 20:12:18 +0000564 // Special-case
Chris Lattner08202542009-02-24 22:50:46 +0000565 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000566 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8dc2102008-05-20 05:25:56 +0000567 } else if (!T->isIncompleteType()) {
Douglas Gregorb574e562009-01-30 22:26:29 +0000568 // Don't complain for incomplete types, since we'll get an error
569 // elsewhere
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000570 QualType CurrentObjectType = StructuredList->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000571 int initKind =
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000572 CurrentObjectType->isArrayType()? 0 :
573 CurrentObjectType->isVectorType()? 1 :
574 CurrentObjectType->isScalarType()? 2 :
575 CurrentObjectType->isUnionType()? 3 :
576 4;
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000577
578 unsigned DK = diag::warn_excess_initializers;
Eli Friedmane5408582009-05-29 20:20:05 +0000579 if (SemaRef.getLangOptions().CPlusPlus) {
580 DK = diag::err_excess_initializers;
581 hadError = true;
582 }
Nate Begeman08634522009-07-07 21:53:06 +0000583 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
584 DK = diag::err_excess_initializers;
585 hadError = true;
586 }
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000587
Chris Lattner08202542009-02-24 22:50:46 +0000588 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000589 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000590 }
591 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000592
Eli Friedman759f2522009-05-16 11:45:48 +0000593 if (T->isScalarType() && !TopLevelObject)
Chris Lattner08202542009-02-24 22:50:46 +0000594 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregora3a83512009-04-01 23:51:29 +0000595 << IList->getSourceRange()
596 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart()))
597 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd()));
Steve Naroff0cca7492008-05-01 22:18:59 +0000598}
599
Eli Friedmanb85f7072008-05-19 19:16:24 +0000600void InitListChecker::CheckListElementTypes(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000601 QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000602 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000603 unsigned &Index,
604 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000605 unsigned &StructuredIndex,
606 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000607 if (DeclType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000608 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000609 } else if (DeclType->isVectorType()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000610 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregord7eb8462009-01-30 17:31:00 +0000611 } else if (DeclType->isAggregateType()) {
612 if (DeclType->isRecordType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000613 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000614 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000615 SubobjectIsDesignatorContext, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000616 StructuredList, StructuredIndex,
617 TopLevelObject);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000618 } else if (DeclType->isArrayType()) {
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000619 llvm::APSInt Zero(
Chris Lattner08202542009-02-24 22:50:46 +0000620 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000621 false);
Douglas Gregor4c678342009-01-28 21:54:33 +0000622 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
623 StructuredList, StructuredIndex);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000624 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000625 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000626 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
627 // This type is invalid, issue a diagnostic.
Douglas Gregor4c678342009-01-28 21:54:33 +0000628 ++Index;
Chris Lattner08202542009-02-24 22:50:46 +0000629 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000630 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000631 hadError = true;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000632 } else if (DeclType->isRecordType()) {
633 // C++ [dcl.init]p14:
634 // [...] If the class is an aggregate (8.5.1), and the initializer
635 // is a brace-enclosed list, see 8.5.1.
636 //
637 // Note: 8.5.1 is handled below; here, we diagnose the case where
638 // we have an initializer list and a destination type that is not
639 // an aggregate.
640 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner08202542009-02-24 22:50:46 +0000641 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000642 << DeclType << IList->getSourceRange();
643 hadError = true;
644 } else if (DeclType->isReferenceType()) {
645 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000646 } else {
647 // In C, all types are either scalars or aggregates, but
Mike Stump1eb44332009-09-09 15:08:12 +0000648 // additional handling is needed here for C++ (and possibly others?).
Steve Naroff0cca7492008-05-01 22:18:59 +0000649 assert(0 && "Unsupported initializer type");
650 }
651}
652
Eli Friedmanb85f7072008-05-19 19:16:24 +0000653void InitListChecker::CheckSubElementType(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000654 QualType ElemType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000655 unsigned &Index,
656 InitListExpr *StructuredList,
657 unsigned &StructuredIndex) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000658 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000659 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
660 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000661 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000662 InitListExpr *newStructuredList
Douglas Gregor4c678342009-01-28 21:54:33 +0000663 = getStructuredSubobjectInit(IList, Index, ElemType,
664 StructuredList, StructuredIndex,
665 SubInitList->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000666 CheckExplicitInitList(SubInitList, ElemType, newIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +0000667 newStructuredList, newStructuredIndex);
668 ++StructuredIndex;
669 ++Index;
Chris Lattner79e079d2009-02-24 23:10:27 +0000670 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
671 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000672 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor4c678342009-01-28 21:54:33 +0000673 ++Index;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000674 } else if (ElemType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000675 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000676 } else if (ElemType->isReferenceType()) {
677 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000678 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000679 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000680 // C++ [dcl.init.aggr]p12:
681 // All implicit type conversions (clause 4) are considered when
682 // initializing the aggregate member with an ini- tializer from
683 // an initializer-list. If the initializer can initialize a
684 // member, the member is initialized. [...]
Mike Stump1eb44332009-09-09 15:08:12 +0000685 ImplicitConversionSequence ICS
Anders Carlssond28b4282009-08-27 17:18:13 +0000686 = SemaRef.TryCopyInitialization(expr, ElemType,
687 /*SuppressUserConversions=*/false,
Anders Carlsson7b361b52009-08-27 17:37:39 +0000688 /*ForceRValue=*/false,
689 /*InOverloadResolution=*/false);
Anders Carlssond28b4282009-08-27 17:18:13 +0000690
Douglas Gregor930d8b52009-01-30 22:09:00 +0000691 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Mike Stump1eb44332009-09-09 15:08:12 +0000692 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000693 "initializing"))
694 hadError = true;
695 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
696 ++Index;
697 return;
698 }
699
700 // Fall through for subaggregate initialization
701 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000702 // C99 6.7.8p13:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000703 //
704 // The initializer for a structure or union object that has
705 // automatic storage duration shall be either an initializer
706 // list as described below, or a single expression that has
707 // compatible structure or union type. In the latter case, the
708 // initial value of the object, including unnamed members, is
709 // that of the expression.
Eli Friedman6b5374f2009-06-13 10:38:46 +0000710 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
Eli Friedman8718a6a2009-05-29 18:22:49 +0000711 SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000712 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
713 ++Index;
714 return;
715 }
716
717 // Fall through for subaggregate initialization
718 }
719
720 // C++ [dcl.init.aggr]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000721 //
Douglas Gregor930d8b52009-01-30 22:09:00 +0000722 // [...] Otherwise, if the member is itself a non-empty
723 // subaggregate, brace elision is assumed and the initializer is
724 // considered for the initialization of the first member of
725 // the subaggregate.
726 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000727 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000728 StructuredIndex);
729 ++StructuredIndex;
730 } else {
731 // We cannot initialize this element, so let
732 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattner08202542009-02-24 22:50:46 +0000733 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregor930d8b52009-01-30 22:09:00 +0000734 hadError = true;
735 ++Index;
736 ++StructuredIndex;
737 }
738 }
Eli Friedmanb85f7072008-05-19 19:16:24 +0000739}
740
Douglas Gregor930d8b52009-01-30 22:09:00 +0000741void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000742 unsigned &Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000743 InitListExpr *StructuredList,
744 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000745 if (Index < IList->getNumInits()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000746 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000747 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000748 SemaRef.Diag(IList->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000749 diag::err_many_braces_around_scalar_init)
750 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000751 hadError = true;
752 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000753 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000754 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000755 } else if (isa<DesignatedInitExpr>(expr)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000756 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor05c13a32009-01-22 00:58:24 +0000757 diag::err_designator_for_scalar_init)
758 << DeclType << expr->getSourceRange();
759 hadError = true;
760 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000761 ++StructuredIndex;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000762 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000763 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000764
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000765 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner08202542009-02-24 22:50:46 +0000766 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedmanbb504d32008-05-19 20:12:18 +0000767 hadError = true; // types weren't compatible.
Douglas Gregor05c13a32009-01-22 00:58:24 +0000768 else if (savExpr != expr) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000769 // The type was promoted, update initializer list.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000770 IList->setInit(Index, expr);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000771 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000772 if (hadError)
773 ++StructuredIndex;
774 else
775 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroff0cca7492008-05-01 22:18:59 +0000776 ++Index;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000777 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000778 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000779 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000780 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000781 ++Index;
782 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000783 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000784 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000785}
786
Douglas Gregor930d8b52009-01-30 22:09:00 +0000787void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
788 unsigned &Index,
789 InitListExpr *StructuredList,
790 unsigned &StructuredIndex) {
791 if (Index < IList->getNumInits()) {
792 Expr *expr = IList->getInit(Index);
793 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000794 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000795 << DeclType << IList->getSourceRange();
796 hadError = true;
797 ++Index;
798 ++StructuredIndex;
799 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000800 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000801
802 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000803 if (SemaRef.CheckReferenceInit(expr, DeclType,
804 /*SuppressUserConversions=*/false,
805 /*AllowExplicit=*/false,
Mike Stump1eb44332009-09-09 15:08:12 +0000806 /*ForceRValue=*/false))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000807 hadError = true;
808 else if (savExpr != expr) {
809 // The type was promoted, update initializer list.
810 IList->setInit(Index, expr);
811 }
812 if (hadError)
813 ++StructuredIndex;
814 else
815 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
816 ++Index;
817 } else {
Mike Stump390b4cc2009-05-16 07:39:55 +0000818 // FIXME: It would be wonderful if we could point at the actual member. In
819 // general, it would be useful to pass location information down the stack,
820 // so that we know the location (or decl) of the "current object" being
821 // initialized.
Mike Stump1eb44332009-09-09 15:08:12 +0000822 SemaRef.Diag(IList->getLocStart(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000823 diag::err_init_reference_member_uninitialized)
824 << DeclType
825 << IList->getSourceRange();
826 hadError = true;
827 ++Index;
828 ++StructuredIndex;
829 return;
830 }
831}
832
Mike Stump1eb44332009-09-09 15:08:12 +0000833void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000834 unsigned &Index,
835 InitListExpr *StructuredList,
836 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000837 if (Index < IList->getNumInits()) {
838 const VectorType *VT = DeclType->getAsVectorType();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000839 unsigned maxElements = VT->getNumElements();
840 unsigned numEltsInit = 0;
Steve Naroff0cca7492008-05-01 22:18:59 +0000841 QualType elementType = VT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Nate Begeman2ef13e52009-08-10 23:49:36 +0000843 if (!SemaRef.getLangOptions().OpenCL) {
844 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
845 // Don't attempt to go past the end of the init list
846 if (Index >= IList->getNumInits())
847 break;
848 CheckSubElementType(IList, elementType, Index,
849 StructuredList, StructuredIndex);
850 }
851 } else {
852 // OpenCL initializers allows vectors to be constructed from vectors.
853 for (unsigned i = 0; i < maxElements; ++i) {
854 // Don't attempt to go past the end of the init list
855 if (Index >= IList->getNumInits())
856 break;
857 QualType IType = IList->getInit(Index)->getType();
858 if (!IType->isVectorType()) {
859 CheckSubElementType(IList, elementType, Index,
860 StructuredList, StructuredIndex);
861 ++numEltsInit;
862 } else {
863 const VectorType *IVT = IType->getAsVectorType();
864 unsigned numIElts = IVT->getNumElements();
865 QualType VecType = SemaRef.Context.getExtVectorType(elementType,
866 numIElts);
867 CheckSubElementType(IList, VecType, Index,
868 StructuredList, StructuredIndex);
869 numEltsInit += numIElts;
870 }
871 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Nate Begeman2ef13e52009-08-10 23:49:36 +0000874 // OpenCL & AltiVec require all elements to be initialized.
875 if (numEltsInit != maxElements)
876 if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
877 SemaRef.Diag(IList->getSourceRange().getBegin(),
878 diag::err_vector_incorrect_num_initializers)
879 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Naroff0cca7492008-05-01 22:18:59 +0000880 }
881}
882
Mike Stump1eb44332009-09-09 15:08:12 +0000883void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000884 llvm::APSInt elementIndex,
Mike Stump1eb44332009-09-09 15:08:12 +0000885 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000886 unsigned &Index,
887 InitListExpr *StructuredList,
888 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000889 // Check for the special-case of initializing an array with a string.
890 if (Index < IList->getNumInits()) {
Chris Lattner79e079d2009-02-24 23:10:27 +0000891 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
892 SemaRef.Context)) {
893 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor4c678342009-01-28 21:54:33 +0000894 // We place the string literal directly into the resulting
895 // initializer list. This is the only place where the structure
896 // of the structured initializer list doesn't match exactly,
897 // because doing so would involve allocating one character
898 // constant for each string.
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000899 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner08202542009-02-24 22:50:46 +0000900 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000901 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000902 return;
903 }
904 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000905 if (const VariableArrayType *VAT =
Chris Lattner08202542009-02-24 22:50:46 +0000906 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman638e1442008-05-25 13:22:35 +0000907 // Check for VLAs; in standard C it would be possible to check this
908 // earlier, but I don't know where clang accepts VLAs (gcc accepts
909 // them in all sorts of strange places).
Chris Lattner08202542009-02-24 22:50:46 +0000910 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000911 diag::err_variable_object_no_init)
912 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000913 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000914 ++Index;
915 ++StructuredIndex;
Eli Friedman638e1442008-05-25 13:22:35 +0000916 return;
917 }
918
Douglas Gregor05c13a32009-01-22 00:58:24 +0000919 // We might know the maximum number of elements in advance.
Douglas Gregor4c678342009-01-28 21:54:33 +0000920 llvm::APSInt maxElements(elementIndex.getBitWidth(),
921 elementIndex.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000922 bool maxElementsKnown = false;
923 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000924 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000925 maxElements = CAT->getSize();
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000926 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000927 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000928 maxElementsKnown = true;
929 }
930
Chris Lattner08202542009-02-24 22:50:46 +0000931 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000932 ->getElementType();
Douglas Gregor05c13a32009-01-22 00:58:24 +0000933 while (Index < IList->getNumInits()) {
934 Expr *Init = IList->getInit(Index);
935 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000936 // If we're not the subobject that matches up with the '{' for
937 // the designator, we shouldn't be handling the
938 // designator. Return immediately.
939 if (!SubobjectIsDesignatorContext)
940 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000941
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000942 // Handle this designated initializer. elementIndex will be
943 // updated to be the next array element we'll initialize.
Mike Stump1eb44332009-09-09 15:08:12 +0000944 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +0000945 DeclType, 0, &elementIndex, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000946 StructuredList, StructuredIndex, true,
947 false)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000948 hadError = true;
949 continue;
950 }
951
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000952 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
953 maxElements.extend(elementIndex.getBitWidth());
954 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
955 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000956 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000957
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000958 // If the array is of incomplete type, keep track of the number of
959 // elements in the initializer.
960 if (!maxElementsKnown && elementIndex > maxElements)
961 maxElements = elementIndex;
962
Douglas Gregor05c13a32009-01-22 00:58:24 +0000963 continue;
964 }
965
966 // If we know the maximum number of elements, and we've already
967 // hit it, stop consuming elements in the initializer list.
968 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroff0cca7492008-05-01 22:18:59 +0000969 break;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000970
971 // Check this element.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000972 CheckSubElementType(IList, elementType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000973 StructuredList, StructuredIndex);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000974 ++elementIndex;
975
976 // If the array is of incomplete type, keep track of the number of
977 // elements in the initializer.
978 if (!maxElementsKnown && elementIndex > maxElements)
979 maxElements = elementIndex;
Steve Naroff0cca7492008-05-01 22:18:59 +0000980 }
Eli Friedman587cbdf2009-05-29 20:17:55 +0000981 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000982 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000983 // be calculated here.
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000984 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000985 if (maxElements == Zero) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000986 // Sizing an array implicitly to zero is not allowed by ISO C,
987 // but is supported by GNU.
Chris Lattner08202542009-02-24 22:50:46 +0000988 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000989 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +0000990 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000991
Mike Stump1eb44332009-09-09 15:08:12 +0000992 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000993 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +0000994 }
995}
996
Mike Stump1eb44332009-09-09 15:08:12 +0000997void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
998 QualType DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000999 RecordDecl::field_iterator Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001000 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +00001001 unsigned &Index,
1002 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001003 unsigned &StructuredIndex,
1004 bool TopLevelObject) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001005 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Eli Friedmanb85f7072008-05-19 19:16:24 +00001007 // If the record is invalid, some of it's members are invalid. To avoid
1008 // confusion, we forgo checking the intializer for the entire record.
1009 if (structDecl->isInvalidDecl()) {
1010 hadError = true;
1011 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001012 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001013
1014 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1015 // Value-initialize the first named member of the union.
Ted Kremenek6217b802009-07-29 21:53:49 +00001016 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001017 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001018 Field != FieldEnd; ++Field) {
1019 if (Field->getDeclName()) {
1020 StructuredList->setInitializedFieldInUnion(*Field);
1021 break;
1022 }
1023 }
1024 return;
1025 }
1026
Douglas Gregor05c13a32009-01-22 00:58:24 +00001027 // If structDecl is a forward declaration, this loop won't do
1028 // anything except look at designated initializers; That's okay,
1029 // because an error should get printed out elsewhere. It might be
1030 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenek6217b802009-07-29 21:53:49 +00001031 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001032 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregordfb5e592009-02-12 19:00:39 +00001033 bool InitializedSomething = false;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001034 while (Index < IList->getNumInits()) {
1035 Expr *Init = IList->getInit(Index);
1036
1037 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001038 // If we're not the subobject that matches up with the '{' for
1039 // the designator, we shouldn't be handling the
1040 // designator. Return immediately.
1041 if (!SubobjectIsDesignatorContext)
1042 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001043
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001044 // Handle this designated initializer. Field will be updated to
1045 // the next field that we'll be initializing.
Mike Stump1eb44332009-09-09 15:08:12 +00001046 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +00001047 DeclType, &Field, 0, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001048 StructuredList, StructuredIndex,
1049 true, TopLevelObject))
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001050 hadError = true;
1051
Douglas Gregordfb5e592009-02-12 19:00:39 +00001052 InitializedSomething = true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001053 continue;
1054 }
1055
1056 if (Field == FieldEnd) {
1057 // We've run out of fields. We're done.
1058 break;
1059 }
1060
Douglas Gregordfb5e592009-02-12 19:00:39 +00001061 // We've already initialized a member of a union. We're done.
1062 if (InitializedSomething && DeclType->isUnionType())
1063 break;
1064
Douglas Gregor44b43212008-12-11 16:49:14 +00001065 // If we've hit the flexible array member at the end, we're done.
1066 if (Field->getType()->isIncompleteArrayType())
1067 break;
1068
Douglas Gregor0bb76892009-01-29 16:53:55 +00001069 if (Field->isUnnamedBitfield()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001070 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregor05c13a32009-01-22 00:58:24 +00001071 ++Field;
Eli Friedmanb85f7072008-05-19 19:16:24 +00001072 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +00001073 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001074
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001075 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001076 StructuredList, StructuredIndex);
Douglas Gregordfb5e592009-02-12 19:00:39 +00001077 InitializedSomething = true;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001078
1079 if (DeclType->isUnionType()) {
1080 // Initialize the first field within the union.
1081 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001082 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001083
1084 ++Field;
Steve Naroff0cca7492008-05-01 22:18:59 +00001085 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001086
Mike Stump1eb44332009-09-09 15:08:12 +00001087 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregora6457962009-03-20 00:32:56 +00001088 Index >= IList->getNumInits())
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001089 return;
1090
1091 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001092 if (!TopLevelObject &&
Douglas Gregora6457962009-03-20 00:32:56 +00001093 (!isa<InitListExpr>(IList->getInit(Index)) ||
1094 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001095 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001096 diag::err_flexible_array_init_nonempty)
1097 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001098 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001099 << *Field;
1100 hadError = true;
Douglas Gregora6457962009-03-20 00:32:56 +00001101 ++Index;
1102 return;
1103 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001104 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregora6457962009-03-20 00:32:56 +00001105 diag::ext_flexible_array_init)
1106 << IList->getInit(Index)->getSourceRange().getBegin();
1107 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1108 << *Field;
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001109 }
1110
Douglas Gregora6457962009-03-20 00:32:56 +00001111 if (isa<InitListExpr>(IList->getInit(Index)))
1112 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1113 StructuredIndex);
1114 else
1115 CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1116 StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +00001117}
Steve Naroff0cca7492008-05-01 22:18:59 +00001118
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001119/// \brief Expand a field designator that refers to a member of an
1120/// anonymous struct or union into a series of field designators that
1121/// refers to the field within the appropriate subobject.
1122///
1123/// Field/FieldIndex will be updated to point to the (new)
1124/// currently-designated field.
1125static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump1eb44332009-09-09 15:08:12 +00001126 DesignatedInitExpr *DIE,
1127 unsigned DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001128 FieldDecl *Field,
1129 RecordDecl::field_iterator &FieldIter,
1130 unsigned &FieldIndex) {
1131 typedef DesignatedInitExpr::Designator Designator;
1132
1133 // Build the path from the current object to the member of the
1134 // anonymous struct/union (backwards).
1135 llvm::SmallVector<FieldDecl *, 4> Path;
1136 SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001138 // Build the replacement designators.
1139 llvm::SmallVector<Designator, 4> Replacements;
1140 for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1141 FI = Path.rbegin(), FIEnd = Path.rend();
1142 FI != FIEnd; ++FI) {
1143 if (FI + 1 == FIEnd)
Mike Stump1eb44332009-09-09 15:08:12 +00001144 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001145 DIE->getDesignator(DesigIdx)->getDotLoc(),
1146 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1147 else
1148 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1149 SourceLocation()));
1150 Replacements.back().setField(*FI);
1151 }
1152
1153 // Expand the current designator into the set of replacement
1154 // designators, so we have a full subobject path down to where the
1155 // member of the anonymous struct/union is actually stored.
Mike Stump1eb44332009-09-09 15:08:12 +00001156 DIE->ExpandDesignator(DesigIdx, &Replacements[0],
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001157 &Replacements[0] + Replacements.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001159 // Update FieldIter/FieldIndex;
1160 RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001161 FieldIter = Record->field_begin();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001162 FieldIndex = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001163 for (RecordDecl::field_iterator FEnd = Record->field_end();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001164 FieldIter != FEnd; ++FieldIter) {
1165 if (FieldIter->isUnnamedBitfield())
1166 continue;
1167
1168 if (*FieldIter == Path.back())
1169 return;
1170
1171 ++FieldIndex;
1172 }
1173
1174 assert(false && "Unable to find anonymous struct/union field");
1175}
1176
Douglas Gregor05c13a32009-01-22 00:58:24 +00001177/// @brief Check the well-formedness of a C99 designated initializer.
1178///
1179/// Determines whether the designated initializer @p DIE, which
1180/// resides at the given @p Index within the initializer list @p
1181/// IList, is well-formed for a current object of type @p DeclType
1182/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump1eb44332009-09-09 15:08:12 +00001183/// within the current subobject is returned in either
Douglas Gregor4c678342009-01-28 21:54:33 +00001184/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregor05c13a32009-01-22 00:58:24 +00001185///
1186/// @param IList The initializer list in which this designated
1187/// initializer occurs.
1188///
Douglas Gregor71199712009-04-15 04:56:10 +00001189/// @param DIE The designated initializer expression.
1190///
1191/// @param DesigIdx The index of the current designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001192///
1193/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1194/// into which the designation in @p DIE should refer.
1195///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001196/// @param NextField If non-NULL and the first designator in @p DIE is
1197/// a field, this will be set to the field declaration corresponding
1198/// to the field named by the designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001199///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001200/// @param NextElementIndex If non-NULL and the first designator in @p
1201/// DIE is an array designator or GNU array-range designator, this
1202/// will be set to the last index initialized by this designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001203///
1204/// @param Index Index into @p IList where the designated initializer
1205/// @p DIE occurs.
1206///
Douglas Gregor4c678342009-01-28 21:54:33 +00001207/// @param StructuredList The initializer list expression that
1208/// describes all of the subobject initializers in the order they'll
1209/// actually be initialized.
1210///
Douglas Gregor05c13a32009-01-22 00:58:24 +00001211/// @returns true if there was an error, false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001212bool
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001213InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001214 DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +00001215 unsigned DesigIdx,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001216 QualType &CurrentObjectType,
1217 RecordDecl::field_iterator *NextField,
1218 llvm::APSInt *NextElementIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001219 unsigned &Index,
1220 InitListExpr *StructuredList,
Douglas Gregor34e79462009-01-28 23:36:17 +00001221 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001222 bool FinishSubobjectInit,
1223 bool TopLevelObject) {
Douglas Gregor71199712009-04-15 04:56:10 +00001224 if (DesigIdx == DIE->size()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001225 // Check the actual initialization for the designated object type.
1226 bool prevHadError = hadError;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001227
1228 // Temporarily remove the designator expression from the
1229 // initializer list that the child calls see, so that we don't try
1230 // to re-process the designator.
1231 unsigned OldIndex = Index;
1232 IList->setInit(OldIndex, DIE->getInit());
1233
1234 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001235 StructuredList, StructuredIndex);
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001236
1237 // Restore the designated initializer expression in the syntactic
1238 // form of the initializer list.
1239 if (IList->getInit(OldIndex) != DIE->getInit())
1240 DIE->setInit(IList->getInit(OldIndex));
1241 IList->setInit(OldIndex, DIE);
1242
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001243 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001244 }
1245
Douglas Gregor71199712009-04-15 04:56:10 +00001246 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001247 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor4c678342009-01-28 21:54:33 +00001248 "Need a non-designated initializer list to start from");
1249
Douglas Gregor71199712009-04-15 04:56:10 +00001250 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001251 // Determine the structural initializer list that corresponds to the
1252 // current subobject.
1253 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump1eb44332009-09-09 15:08:12 +00001254 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001255 StructuredList, StructuredIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001256 SourceRange(D->getStartLocation(),
1257 DIE->getSourceRange().getEnd()));
1258 assert(StructuredList && "Expected a structured initializer list");
1259
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001260 if (D->isFieldDesignator()) {
1261 // C99 6.7.8p7:
1262 //
1263 // If a designator has the form
1264 //
1265 // . identifier
1266 //
1267 // then the current object (defined below) shall have
1268 // structure or union type and the identifier shall be the
Mike Stump1eb44332009-09-09 15:08:12 +00001269 // name of a member of that type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001270 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001271 if (!RT) {
1272 SourceLocation Loc = D->getDotLoc();
1273 if (Loc.isInvalid())
1274 Loc = D->getFieldLoc();
Chris Lattner08202542009-02-24 22:50:46 +00001275 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1276 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001277 ++Index;
1278 return true;
1279 }
1280
Douglas Gregor4c678342009-01-28 21:54:33 +00001281 // Note: we perform a linear search of the fields here, despite
1282 // the fact that we have a faster lookup method, because we always
1283 // need to compute the field's index.
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001284 FieldDecl *KnownField = D->getField();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001285 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor4c678342009-01-28 21:54:33 +00001286 unsigned FieldIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001287 RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001288 Field = RT->getDecl()->field_begin(),
1289 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +00001290 for (; Field != FieldEnd; ++Field) {
1291 if (Field->isUnnamedBitfield())
1292 continue;
1293
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001294 if (KnownField == *Field || Field->getIdentifier() == FieldName)
Douglas Gregor4c678342009-01-28 21:54:33 +00001295 break;
1296
1297 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001298 }
1299
Douglas Gregor4c678342009-01-28 21:54:33 +00001300 if (Field == FieldEnd) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001301 // There was no normal field in the struct with the designated
1302 // name. Perform another lookup for this name, which may find
1303 // something that we can't designate (e.g., a member function),
1304 // may find nothing, or may find a member of an anonymous
Mike Stump1eb44332009-09-09 15:08:12 +00001305 // struct/union.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001306 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregor4c678342009-01-28 21:54:33 +00001307 if (Lookup.first == Lookup.second) {
1308 // Name lookup didn't find anything.
Chris Lattner08202542009-02-24 22:50:46 +00001309 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregor4c678342009-01-28 21:54:33 +00001310 << FieldName << CurrentObjectType;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001311 ++Index;
1312 return true;
1313 } else if (!KnownField && isa<FieldDecl>(*Lookup.first) &&
1314 cast<RecordDecl>((*Lookup.first)->getDeclContext())
1315 ->isAnonymousStructOrUnion()) {
1316 // Handle an field designator that refers to a member of an
1317 // anonymous struct or union.
Mike Stump1eb44332009-09-09 15:08:12 +00001318 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001319 cast<FieldDecl>(*Lookup.first),
1320 Field, FieldIndex);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001321 D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001322 } else {
1323 // Name lookup found something, but it wasn't a field.
Chris Lattner08202542009-02-24 22:50:46 +00001324 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor4c678342009-01-28 21:54:33 +00001325 << FieldName;
Mike Stump1eb44332009-09-09 15:08:12 +00001326 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001327 diag::note_field_designator_found);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001328 ++Index;
1329 return true;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001330 }
1331 } else if (!KnownField &&
1332 cast<RecordDecl>((*Field)->getDeclContext())
Douglas Gregor4c678342009-01-28 21:54:33 +00001333 ->isAnonymousStructOrUnion()) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001334 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1335 Field, FieldIndex);
1336 D = DIE->getDesignator(DesigIdx);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001337 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001338
1339 // All of the fields of a union are located at the same place in
1340 // the initializer list.
Douglas Gregor0bb76892009-01-29 16:53:55 +00001341 if (RT->getDecl()->isUnion()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001342 FieldIndex = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001343 StructuredList->setInitializedFieldInUnion(*Field);
1344 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001345
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001346 // Update the designator with the field declaration.
Douglas Gregor4c678342009-01-28 21:54:33 +00001347 D->setField(*Field);
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Douglas Gregor4c678342009-01-28 21:54:33 +00001349 // Make sure that our non-designated initializer list has space
1350 // for a subobject corresponding to this field.
1351 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001352 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001353
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001354 // This designator names a flexible array member.
1355 if (Field->getType()->isIncompleteArrayType()) {
1356 bool Invalid = false;
Douglas Gregor71199712009-04-15 04:56:10 +00001357 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001358 // We can't designate an object within the flexible array
1359 // member (because GCC doesn't allow it).
Mike Stump1eb44332009-09-09 15:08:12 +00001360 DesignatedInitExpr::Designator *NextD
Douglas Gregor71199712009-04-15 04:56:10 +00001361 = DIE->getDesignator(DesigIdx + 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001362 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001363 diag::err_designator_into_flexible_array_member)
Mike Stump1eb44332009-09-09 15:08:12 +00001364 << SourceRange(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001365 DIE->getSourceRange().getEnd());
Chris Lattner08202542009-02-24 22:50:46 +00001366 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001367 << *Field;
1368 Invalid = true;
1369 }
1370
1371 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1372 // The initializer is not an initializer list.
Chris Lattner08202542009-02-24 22:50:46 +00001373 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001374 diag::err_flexible_array_init_needs_braces)
1375 << DIE->getInit()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001376 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001377 << *Field;
1378 Invalid = true;
1379 }
1380
1381 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001382 if (!Invalid && !TopLevelObject &&
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001383 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +00001384 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001385 diag::err_flexible_array_init_nonempty)
1386 << DIE->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001387 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001388 << *Field;
1389 Invalid = true;
1390 }
1391
1392 if (Invalid) {
1393 ++Index;
1394 return true;
1395 }
1396
1397 // Initialize the array.
1398 bool prevHadError = hadError;
1399 unsigned newStructuredIndex = FieldIndex;
1400 unsigned OldIndex = Index;
1401 IList->setInit(Index, DIE->getInit());
Mike Stump1eb44332009-09-09 15:08:12 +00001402 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001403 StructuredList, newStructuredIndex);
1404 IList->setInit(OldIndex, DIE);
1405 if (hadError && !prevHadError) {
1406 ++Field;
1407 ++FieldIndex;
1408 if (NextField)
1409 *NextField = Field;
1410 StructuredIndex = FieldIndex;
1411 return true;
1412 }
1413 } else {
1414 // Recurse to check later designated subobjects.
1415 QualType FieldType = (*Field)->getType();
1416 unsigned newStructuredIndex = FieldIndex;
Douglas Gregor71199712009-04-15 04:56:10 +00001417 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
1418 Index, StructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001419 true, false))
1420 return true;
1421 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001422
1423 // Find the position of the next field to be initialized in this
1424 // subobject.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001425 ++Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001426 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001427
1428 // If this the first designator, our caller will continue checking
1429 // the rest of this struct/class/union subobject.
1430 if (IsFirstDesignator) {
1431 if (NextField)
1432 *NextField = Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001433 StructuredIndex = FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001434 return false;
1435 }
1436
Douglas Gregor34e79462009-01-28 23:36:17 +00001437 if (!FinishSubobjectInit)
1438 return false;
1439
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001440 // We've already initialized something in the union; we're done.
1441 if (RT->getDecl()->isUnion())
1442 return hadError;
1443
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001444 // Check the remaining fields within this class/struct/union subobject.
1445 bool prevHadError = hadError;
Douglas Gregor4c678342009-01-28 21:54:33 +00001446 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1447 StructuredList, FieldIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001448 return hadError && !prevHadError;
1449 }
1450
1451 // C99 6.7.8p6:
1452 //
1453 // If a designator has the form
1454 //
1455 // [ constant-expression ]
1456 //
1457 // then the current object (defined below) shall have array
1458 // type and the expression shall be an integer constant
1459 // expression. If the array is of unknown size, any
1460 // nonnegative value is valid.
1461 //
1462 // Additionally, cope with the GNU extension that permits
1463 // designators of the form
1464 //
1465 // [ constant-expression ... constant-expression ]
Chris Lattner08202542009-02-24 22:50:46 +00001466 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001467 if (!AT) {
Chris Lattner08202542009-02-24 22:50:46 +00001468 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001469 << CurrentObjectType;
1470 ++Index;
1471 return true;
1472 }
1473
1474 Expr *IndexExpr = 0;
Douglas Gregor34e79462009-01-28 23:36:17 +00001475 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1476 if (D->isArrayDesignator()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001477 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattner3bf68932009-04-25 21:59:05 +00001478 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001479 DesignatedEndIndex = DesignatedStartIndex;
1480 } else {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001481 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor34e79462009-01-28 23:36:17 +00001482
Mike Stump1eb44332009-09-09 15:08:12 +00001483
1484 DesignatedStartIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001485 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001486 DesignatedEndIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001487 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001488 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001489
Chris Lattner3bf68932009-04-25 21:59:05 +00001490 if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
Douglas Gregora9c87802009-01-29 19:42:23 +00001491 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001492 }
1493
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001494 if (isa<ConstantArrayType>(AT)) {
1495 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor34e79462009-01-28 23:36:17 +00001496 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1497 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1498 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1499 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1500 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner08202542009-02-24 22:50:46 +00001501 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001502 diag::err_array_designator_too_large)
Douglas Gregor34e79462009-01-28 23:36:17 +00001503 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001504 << IndexExpr->getSourceRange();
1505 ++Index;
1506 return true;
1507 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001508 } else {
1509 // Make sure the bit-widths and signedness match.
1510 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1511 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattner3bf68932009-04-25 21:59:05 +00001512 else if (DesignatedStartIndex.getBitWidth() <
1513 DesignatedEndIndex.getBitWidth())
Douglas Gregor34e79462009-01-28 23:36:17 +00001514 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1515 DesignatedStartIndex.setIsUnsigned(true);
1516 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Douglas Gregor4c678342009-01-28 21:54:33 +00001519 // Make sure that our non-designated initializer list has space
1520 // for a subobject corresponding to this array element.
Douglas Gregor34e79462009-01-28 23:36:17 +00001521 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump1eb44332009-09-09 15:08:12 +00001522 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor34e79462009-01-28 23:36:17 +00001523 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001524
Douglas Gregor34e79462009-01-28 23:36:17 +00001525 // Repeatedly perform subobject initializations in the range
1526 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001527
Douglas Gregor34e79462009-01-28 23:36:17 +00001528 // Move to the next designator
1529 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1530 unsigned OldIndex = Index;
Douglas Gregor34e79462009-01-28 23:36:17 +00001531 while (DesignatedStartIndex <= DesignatedEndIndex) {
1532 // Recurse to check later designated subobjects.
1533 QualType ElementType = AT->getElementType();
1534 Index = OldIndex;
Douglas Gregor71199712009-04-15 04:56:10 +00001535 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
1536 Index, StructuredList, ElementIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001537 (DesignatedStartIndex == DesignatedEndIndex),
1538 false))
Douglas Gregor34e79462009-01-28 23:36:17 +00001539 return true;
1540
1541 // Move to the next index in the array that we'll be initializing.
1542 ++DesignatedStartIndex;
1543 ElementIndex = DesignatedStartIndex.getZExtValue();
1544 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001545
1546 // If this the first designator, our caller will continue checking
1547 // the rest of this array subobject.
1548 if (IsFirstDesignator) {
1549 if (NextElementIndex)
Douglas Gregor34e79462009-01-28 23:36:17 +00001550 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor4c678342009-01-28 21:54:33 +00001551 StructuredIndex = ElementIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001552 return false;
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Douglas Gregor34e79462009-01-28 23:36:17 +00001555 if (!FinishSubobjectInit)
1556 return false;
1557
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001558 // Check the remaining elements within this array subobject.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001559 bool prevHadError = hadError;
Douglas Gregorfdf55692009-02-09 19:45:19 +00001560 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001561 StructuredList, ElementIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001562 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001563}
1564
Douglas Gregor4c678342009-01-28 21:54:33 +00001565// Get the structured initializer list for a subobject of type
1566// @p CurrentObjectType.
1567InitListExpr *
1568InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1569 QualType CurrentObjectType,
1570 InitListExpr *StructuredList,
1571 unsigned StructuredIndex,
1572 SourceRange InitRange) {
1573 Expr *ExistingInit = 0;
1574 if (!StructuredList)
1575 ExistingInit = SyntacticToSemantic[IList];
1576 else if (StructuredIndex < StructuredList->getNumInits())
1577 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregor4c678342009-01-28 21:54:33 +00001579 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1580 return Result;
1581
1582 if (ExistingInit) {
1583 // We are creating an initializer list that initializes the
1584 // subobjects of the current object, but there was already an
1585 // initialization that completely initialized the current
1586 // subobject, e.g., by a compound literal:
Mike Stump1eb44332009-09-09 15:08:12 +00001587 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001588 // struct X { int a, b; };
1589 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump1eb44332009-09-09 15:08:12 +00001590 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001591 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1592 // designated initializer re-initializes the whole
1593 // subobject [0], overwriting previous initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001594 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregored8a93d2009-03-01 17:12:46 +00001595 diag::warn_subobject_initializer_overrides)
Douglas Gregor4c678342009-01-28 21:54:33 +00001596 << InitRange;
Mike Stump1eb44332009-09-09 15:08:12 +00001597 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001598 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001599 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001600 << ExistingInit->getSourceRange();
1601 }
1602
Mike Stump1eb44332009-09-09 15:08:12 +00001603 InitListExpr *Result
1604 = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001605 InitRange.getEnd());
1606
Douglas Gregor4c678342009-01-28 21:54:33 +00001607 Result->setType(CurrentObjectType);
1608
Douglas Gregorfa219202009-03-20 23:58:33 +00001609 // Pre-allocate storage for the structured initializer list.
1610 unsigned NumElements = 0;
Douglas Gregor08457732009-03-21 18:13:52 +00001611 unsigned NumInits = 0;
1612 if (!StructuredList)
1613 NumInits = IList->getNumInits();
1614 else if (Index < IList->getNumInits()) {
1615 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1616 NumInits = SubList->getNumInits();
1617 }
1618
Mike Stump1eb44332009-09-09 15:08:12 +00001619 if (const ArrayType *AType
Douglas Gregorfa219202009-03-20 23:58:33 +00001620 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1621 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1622 NumElements = CAType->getSize().getZExtValue();
1623 // Simple heuristic so that we don't allocate a very large
1624 // initializer with many empty entries at the end.
Douglas Gregor08457732009-03-21 18:13:52 +00001625 if (NumInits && NumElements > NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001626 NumElements = 0;
1627 }
1628 } else if (const VectorType *VType = CurrentObjectType->getAsVectorType())
1629 NumElements = VType->getNumElements();
Ted Kremenek6217b802009-07-29 21:53:49 +00001630 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregorfa219202009-03-20 23:58:33 +00001631 RecordDecl *RDecl = RType->getDecl();
1632 if (RDecl->isUnion())
1633 NumElements = 1;
1634 else
Mike Stump1eb44332009-09-09 15:08:12 +00001635 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001636 RDecl->field_end());
Douglas Gregorfa219202009-03-20 23:58:33 +00001637 }
1638
Douglas Gregor08457732009-03-21 18:13:52 +00001639 if (NumElements < NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001640 NumElements = IList->getNumInits();
1641
1642 Result->reserveInits(NumElements);
1643
Douglas Gregor4c678342009-01-28 21:54:33 +00001644 // Link this new initializer list into the structured initializer
1645 // lists.
1646 if (StructuredList)
1647 StructuredList->updateInit(StructuredIndex, Result);
1648 else {
1649 Result->setSyntacticForm(IList);
1650 SyntacticToSemantic[IList] = Result;
1651 }
1652
1653 return Result;
1654}
1655
1656/// Update the initializer at index @p StructuredIndex within the
1657/// structured initializer list to the value @p expr.
1658void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1659 unsigned &StructuredIndex,
1660 Expr *expr) {
1661 // No structured initializer list to update
1662 if (!StructuredList)
1663 return;
1664
1665 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1666 // This initializer overwrites a previous initializer. Warn.
Mike Stump1eb44332009-09-09 15:08:12 +00001667 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001668 diag::warn_initializer_overrides)
1669 << expr->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001670 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001671 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001672 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001673 << PrevInit->getSourceRange();
1674 }
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Douglas Gregor4c678342009-01-28 21:54:33 +00001676 ++StructuredIndex;
1677}
1678
Douglas Gregor05c13a32009-01-22 00:58:24 +00001679/// Check that the given Index expression is a valid array designator
1680/// value. This is essentailly just a wrapper around
Chris Lattner3bf68932009-04-25 21:59:05 +00001681/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregor05c13a32009-01-22 00:58:24 +00001682/// and produces a reasonable diagnostic if there is a
1683/// failure. Returns true if there was an error, false otherwise. If
1684/// everything went okay, Value will receive the value of the constant
1685/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001686static bool
Chris Lattner3bf68932009-04-25 21:59:05 +00001687CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001688 SourceLocation Loc = Index->getSourceRange().getBegin();
1689
1690 // Make sure this is an integer constant expression.
Chris Lattner3bf68932009-04-25 21:59:05 +00001691 if (S.VerifyIntegerConstantExpression(Index, &Value))
1692 return true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001693
Chris Lattner3bf68932009-04-25 21:59:05 +00001694 if (Value.isSigned() && Value.isNegative())
1695 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001696 << Value.toString(10) << Index->getSourceRange();
1697
Douglas Gregor53d3d8e2009-01-23 21:04:18 +00001698 Value.setIsUnsigned(true);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001699 return false;
1700}
1701
1702Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1703 SourceLocation Loc,
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001704 bool GNUSyntax,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001705 OwningExprResult Init) {
1706 typedef DesignatedInitExpr::Designator ASTDesignator;
1707
1708 bool Invalid = false;
1709 llvm::SmallVector<ASTDesignator, 32> Designators;
1710 llvm::SmallVector<Expr *, 32> InitExpressions;
1711
1712 // Build designators and check array designator expressions.
1713 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1714 const Designator &D = Desig.getDesignator(Idx);
1715 switch (D.getKind()) {
1716 case Designator::FieldDesignator:
Mike Stump1eb44332009-09-09 15:08:12 +00001717 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001718 D.getFieldLoc()));
1719 break;
1720
1721 case Designator::ArrayDesignator: {
1722 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1723 llvm::APSInt IndexValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001724 if (!Index->isTypeDependent() &&
1725 !Index->isValueDependent() &&
1726 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001727 Invalid = true;
1728 else {
1729 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001730 D.getLBracketLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001731 D.getRBracketLoc()));
1732 InitExpressions.push_back(Index);
1733 }
1734 break;
1735 }
1736
1737 case Designator::ArrayRangeDesignator: {
1738 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1739 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1740 llvm::APSInt StartValue;
1741 llvm::APSInt EndValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001742 bool StartDependent = StartIndex->isTypeDependent() ||
1743 StartIndex->isValueDependent();
1744 bool EndDependent = EndIndex->isTypeDependent() ||
1745 EndIndex->isValueDependent();
1746 if ((!StartDependent &&
1747 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1748 (!EndDependent &&
1749 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001750 Invalid = true;
Douglas Gregord6f584f2009-01-23 22:22:29 +00001751 else {
1752 // Make sure we're comparing values with the same bit width.
Douglas Gregor9ea62762009-05-21 23:17:49 +00001753 if (StartDependent || EndDependent) {
1754 // Nothing to compute.
1755 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Douglas Gregord6f584f2009-01-23 22:22:29 +00001756 EndValue.extend(StartValue.getBitWidth());
1757 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1758 StartValue.extend(EndValue.getBitWidth());
1759
Douglas Gregorc4bb7bf2009-05-21 23:30:39 +00001760 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregord6f584f2009-01-23 22:22:29 +00001761 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump1eb44332009-09-09 15:08:12 +00001762 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregord6f584f2009-01-23 22:22:29 +00001763 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1764 Invalid = true;
1765 } else {
1766 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001767 D.getLBracketLoc(),
Douglas Gregord6f584f2009-01-23 22:22:29 +00001768 D.getEllipsisLoc(),
1769 D.getRBracketLoc()));
1770 InitExpressions.push_back(StartIndex);
1771 InitExpressions.push_back(EndIndex);
1772 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001773 }
1774 break;
1775 }
1776 }
1777 }
1778
1779 if (Invalid || Init.isInvalid())
1780 return ExprError();
1781
1782 // Clear out the expressions within the designation.
1783 Desig.ClearExprs(*this);
1784
1785 DesignatedInitExpr *DIE
Jay Foadbeaaccd2009-05-21 09:52:38 +00001786 = DesignatedInitExpr::Create(Context,
1787 Designators.data(), Designators.size(),
1788 InitExpressions.data(), InitExpressions.size(),
Anders Carlssone9146f22009-05-01 19:49:17 +00001789 Loc, GNUSyntax, Init.takeAs<Expr>());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001790 return Owned(DIE);
1791}
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001792
1793bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattner08202542009-02-24 22:50:46 +00001794 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001795 if (!CheckInitList.HadError())
1796 InitList = CheckInitList.getFullyStructuredList();
1797
1798 return CheckInitList.HadError();
1799}
Douglas Gregor87fd7032009-02-02 17:43:21 +00001800
1801/// \brief Diagnose any semantic errors with value-initialization of
1802/// the given type.
1803///
1804/// Value-initialization effectively zero-initializes any types
1805/// without user-declared constructors, and calls the default
1806/// constructor for a for any type that has a user-declared
1807/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1808/// a type with a user-declared constructor does not have an
1809/// accessible, non-deleted default constructor. In C, everything can
1810/// be value-initialized, which corresponds to C's notion of
1811/// initializing objects with static storage duration when no
Mike Stump1eb44332009-09-09 15:08:12 +00001812/// initializer is provided for that object.
Douglas Gregor87fd7032009-02-02 17:43:21 +00001813///
1814/// \returns true if there was an error, false otherwise.
1815bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1816 // C++ [dcl.init]p5:
1817 //
1818 // To value-initialize an object of type T means:
1819
1820 // -- if T is an array type, then each element is value-initialized;
1821 if (const ArrayType *AT = Context.getAsArrayType(Type))
1822 return CheckValueInitialization(AT->getElementType(), Loc);
1823
Ted Kremenek6217b802009-07-29 21:53:49 +00001824 if (const RecordType *RT = Type->getAs<RecordType>()) {
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001825 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Douglas Gregor87fd7032009-02-02 17:43:21 +00001826 // -- if T is a class type (clause 9) with a user-declared
1827 // constructor (12.1), then the default constructor for T is
1828 // called (and the initialization is ill-formed if T has no
1829 // accessible default constructor);
Douglas Gregor39da0b82009-09-09 23:08:42 +00001830 if (ClassDecl->hasUserDeclaredConstructor()) {
1831 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
1832
1833 CXXConstructorDecl *Constructor
1834 = PerformInitializationByConstructor(Type,
1835 MultiExprArg(*this, 0, 0),
1836 Loc, SourceRange(Loc),
1837 DeclarationName(),
1838 IK_Direct,
1839 ConstructorArgs);
1840 if (!Constructor)
1841 return true;
1842
1843 OwningExprResult Init
1844 = BuildCXXConstructExpr(Loc, Type, Constructor,
1845 move_arg(ConstructorArgs));
1846 if (Init.isInvalid())
1847 return true;
1848
1849 // FIXME: Actually perform the value-initialization!
1850 return false;
1851 }
Douglas Gregor87fd7032009-02-02 17:43:21 +00001852 }
1853 }
1854
1855 if (Type->isReferenceType()) {
1856 // C++ [dcl.init]p5:
1857 // [...] A program that calls for default-initialization or
1858 // value-initialization of an entity of reference type is
1859 // ill-formed. [...]
Mike Stump390b4cc2009-05-16 07:39:55 +00001860 // FIXME: Once we have code that goes through this path, add an actual
1861 // diagnostic :)
Douglas Gregor87fd7032009-02-02 17:43:21 +00001862 }
1863
1864 return false;
1865}