blob: fbc4680f8f2f071ae7fa522d6f0e64bf985f1a1f [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).
John McCall46a617a2009-10-16 00:14:28 +0000112 DeclT = S.Context.getConstantArrayType(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 Gregorcb78d882009-11-19 18:03:26 +0000139 Init->isTypeDependent() || Init->isValueDependent()) {
140 // We have either a dependent type or a type- or value-dependent
141 // initializer, so we don't perform any additional checking at
142 // this point.
143
144 // If the declaration is a non-dependent, incomplete array type
145 // that has an initializer, then its type will be completed once
146 // the initializer is instantiated, meaning that the type is
147 // dependent. Morph the declaration's type into a
148 // dependently-sized array type.
149 if (!DeclType->isDependentType()) {
150 if (const IncompleteArrayType *ArrayT
151 = Context.getAsIncompleteArrayType(DeclType)) {
152 DeclType = Context.getDependentSizedArrayType(ArrayT->getElementType(),
153 /*NumElts=*/0,
154 ArrayT->getSizeModifier(),
155 ArrayT->getIndexTypeCVRQualifiers(),
156 SourceRange());
157 }
158 }
159
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000160 return false;
Douglas Gregorcb78d882009-11-19 18:03:26 +0000161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000163 // C++ [dcl.init.ref]p1:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000164 // A variable declared to be a T& or T&&, that is "reference to type T"
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000165 // (8.3.2), shall be initialized by an object, or function, of
166 // type T or by an object that can be converted into a T.
167 if (DeclType->isReferenceType())
Douglas Gregor739d8282009-09-23 23:04:10 +0000168 return CheckReferenceInit(Init, DeclType, InitLoc,
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000169 /*SuppressUserConversions=*/false,
170 /*AllowExplicit=*/DirectInit,
171 /*ForceRValue=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000173 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
174 // of unknown size ("[]") or an object type that is not a variable array type.
175 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
176 return Diag(InitLoc, diag::err_variable_object_no_init)
177 << VAT->getSizeExpr()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000179 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
180 if (!InitList) {
181 // FIXME: Handle wide strings
Chris Lattner79e079d2009-02-24 23:10:27 +0000182 if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
183 CheckStringInit(Str, DeclType, *this);
Chris Lattner19da8cd2009-02-24 23:01:39 +0000184 return false;
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000187 // C++ [dcl.init]p14:
188 // -- If the destination type is a (possibly cv-qualified) class
189 // type:
190 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
191 QualType DeclTypeC = Context.getCanonicalType(DeclType);
192 QualType InitTypeC = Context.getCanonicalType(Init->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000194 // -- If the initialization is direct-initialization, or if it is
195 // copy-initialization where the cv-unqualified version of the
196 // source type is the same class as, or a derived class of, the
197 // class of the destination, constructors are considered.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000198 if ((DeclTypeC.getLocalUnqualifiedType()
199 == InitTypeC.getLocalUnqualifiedType()) ||
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000200 IsDerivedFrom(InitTypeC, DeclTypeC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000201 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000202 cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Anders Carlssonbffed8a2009-05-27 16:38:58 +0000204 // No need to make a CXXConstructExpr if both the ctor and dtor are
205 // trivial.
206 if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor())
207 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Douglas Gregor39da0b82009-09-09 23:08:42 +0000209 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
210
Mike Stump1eb44332009-09-09 15:08:12 +0000211 CXXConstructorDecl *Constructor
Douglas Gregor39da0b82009-09-09 23:08:42 +0000212 = PerformInitializationByConstructor(DeclType,
213 MultiExprArg(*this,
214 (void **)&Init, 1),
215 InitLoc, Init->getSourceRange(),
216 InitEntity,
217 DirectInit? IK_Direct : IK_Copy,
218 ConstructorArgs);
Anders Carlsson2078bb92009-05-27 16:10:08 +0000219 if (!Constructor)
220 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
222 OwningExprResult InitResult =
Anders Carlssonec8e5ea2009-09-05 07:40:38 +0000223 BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000224 DeclType, Constructor,
Douglas Gregor39da0b82009-09-09 23:08:42 +0000225 move_arg(ConstructorArgs));
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000226 if (InitResult.isInvalid())
227 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000229 Init = InitResult.takeAs<Expr>();
Anders Carlsson2078bb92009-05-27 16:10:08 +0000230 return false;
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000233 // -- Otherwise (i.e., for the remaining copy-initialization
234 // cases), user-defined conversion sequences that can
235 // convert from the source type to the destination type or
236 // (when a conversion function is used) to a derived class
237 // thereof are enumerated as described in 13.3.1.4, and the
238 // best one is chosen through overload resolution
239 // (13.3). If the conversion cannot be done or is
240 // ambiguous, the initialization is ill-formed. The
241 // function selected is called with the initializer
242 // expression as its argument; if the function is a
243 // constructor, the call initializes a temporary of the
244 // destination type.
Mike Stump390b4cc2009-05-16 07:39:55 +0000245 // FIXME: We're pretending to do copy elision here; return to this when we
246 // have ASTs for such things.
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000247 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
248 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000250 if (InitEntity)
251 return Diag(InitLoc, diag::err_cannot_initialize_decl)
Chris Lattnerb78d8332009-06-26 04:45:06 +0000252 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
253 << Init->getType() << Init->getSourceRange();
254 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000255 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
256 << Init->getType() << Init->getSourceRange();
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000259 // C99 6.7.8p16.
260 if (DeclType->isArrayType())
261 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
Chris Lattnerb78d8332009-06-26 04:45:06 +0000262 << Init->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner95e8d652009-02-24 22:46:58 +0000264 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Mike Stump1eb44332009-09-09 15:08:12 +0000265 }
266
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000267 bool hadError = CheckInitList(InitList, DeclType);
268 Init = InitList;
269 return hadError;
270}
271
272//===----------------------------------------------------------------------===//
273// Semantic checking for initializer lists.
274//===----------------------------------------------------------------------===//
275
Douglas Gregor9e80f722009-01-29 01:05:33 +0000276/// @brief Semantic checking for initializer lists.
277///
278/// The InitListChecker class contains a set of routines that each
279/// handle the initialization of a certain kind of entity, e.g.,
280/// arrays, vectors, struct/union types, scalars, etc. The
281/// InitListChecker itself performs a recursive walk of the subobject
282/// structure of the type to be initialized, while stepping through
283/// the initializer list one element at a time. The IList and Index
284/// parameters to each of the Check* routines contain the active
285/// (syntactic) initializer list and the index into that initializer
286/// list that represents the current initializer. Each routine is
287/// responsible for moving that Index forward as it consumes elements.
288///
289/// Each Check* routine also has a StructuredList/StructuredIndex
290/// arguments, which contains the current the "structured" (semantic)
291/// initializer list and the index into that initializer list where we
292/// are copying initializers as we map them over to the semantic
293/// list. Once we have completed our recursive walk of the subobject
294/// structure, we will have constructed a full semantic initializer
295/// list.
296///
297/// C99 designators cause changes in the initializer list traversal,
298/// because they make the initialization "jump" into a specific
299/// subobject and then continue the initialization from that
300/// point. CheckDesignatedInitializer() recursively steps into the
301/// designated subobject and manages backing out the recursion to
302/// initialize the subobjects after the one designated.
Chris Lattner8b419b92009-02-24 22:48:58 +0000303namespace {
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000304class InitListChecker {
Chris Lattner08202542009-02-24 22:50:46 +0000305 Sema &SemaRef;
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000306 bool hadError;
307 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
308 InitListExpr *FullyStructuredList;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
310 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000311 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000312 unsigned &StructuredIndex,
313 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000314 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000315 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000316 unsigned &StructuredIndex,
317 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000318 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
319 bool SubobjectIsDesignatorContext,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000320 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000321 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000322 unsigned &StructuredIndex,
323 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000324 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000325 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000326 InitListExpr *StructuredList,
327 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000328 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000329 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000330 InitListExpr *StructuredList,
331 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000332 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000333 unsigned &Index,
334 InitListExpr *StructuredList,
335 unsigned &StructuredIndex);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000336 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000337 InitListExpr *StructuredList,
338 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000339 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
340 RecordDecl::field_iterator Field,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000341 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000342 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000343 unsigned &StructuredIndex,
344 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000345 void CheckArrayType(InitListExpr *IList, QualType &DeclType,
346 llvm::APSInt elementIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000347 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000348 InitListExpr *StructuredList,
349 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000350 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +0000351 unsigned DesigIdx,
Mike Stump1eb44332009-09-09 15:08:12 +0000352 QualType &CurrentObjectType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000353 RecordDecl::field_iterator *NextField,
354 llvm::APSInt *NextElementIndex,
355 unsigned &Index,
356 InitListExpr *StructuredList,
357 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000358 bool FinishSubobjectInit,
359 bool TopLevelObject);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000360 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
361 QualType CurrentObjectType,
362 InitListExpr *StructuredList,
363 unsigned StructuredIndex,
364 SourceRange InitRange);
Douglas Gregor9e80f722009-01-29 01:05:33 +0000365 void UpdateStructuredListElement(InitListExpr *StructuredList,
366 unsigned &StructuredIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000367 Expr *expr);
368 int numArrayElements(QualType DeclType);
369 int numStructUnionElements(QualType DeclType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000370
371 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000372public:
Chris Lattner08202542009-02-24 22:50:46 +0000373 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000374 bool HadError() { return hadError; }
375
376 // @brief Retrieves the fully-structured initializer list used for
377 // semantic analysis and code generation.
378 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
379};
Chris Lattner8b419b92009-02-24 22:48:58 +0000380} // end anonymous namespace
Chris Lattner68355a52009-01-29 05:10:57 +0000381
Douglas Gregor4c678342009-01-28 21:54:33 +0000382/// Recursively replaces NULL values within the given initializer list
383/// with expressions that perform value-initialization of the
384/// appropriate type.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000385void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Mike Stump1eb44332009-09-09 15:08:12 +0000386 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregor930d8b52009-01-30 22:09:00 +0000387 "Should not have void type");
Douglas Gregor87fd7032009-02-02 17:43:21 +0000388 SourceLocation Loc = ILE->getSourceRange().getBegin();
389 if (ILE->getSyntacticForm())
390 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek6217b802009-07-29 21:53:49 +0000392 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000393 unsigned Init = 0, NumInits = ILE->getNumInits();
Mike Stump1eb44332009-09-09 15:08:12 +0000394 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000395 Field = RType->getDecl()->field_begin(),
396 FieldEnd = RType->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000397 Field != FieldEnd; ++Field) {
398 if (Field->isUnnamedBitfield())
399 continue;
400
Douglas Gregor87fd7032009-02-02 17:43:21 +0000401 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000402 if (Field->getType()->isReferenceType()) {
403 // C++ [dcl.init.aggr]p9:
404 // If an incomplete or empty initializer-list leaves a
405 // member of reference type uninitialized, the program is
Mike Stump1eb44332009-09-09 15:08:12 +0000406 // ill-formed.
Chris Lattner08202542009-02-24 22:50:46 +0000407 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000408 << Field->getType()
409 << ILE->getSyntacticForm()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000410 SemaRef.Diag(Field->getLocation(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000411 diag::note_uninit_reference_member);
412 hadError = true;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000413 return;
Chris Lattner08202542009-02-24 22:50:46 +0000414 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000415 hadError = true;
416 return;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000417 }
Douglas Gregor87fd7032009-02-02 17:43:21 +0000418
Mike Stump390b4cc2009-05-16 07:39:55 +0000419 // FIXME: If value-initialization involves calling a constructor, should
420 // we make that call explicit in the representation (even when it means
421 // extending the initializer list)?
Douglas Gregor87fd7032009-02-02 17:43:21 +0000422 if (Init < NumInits && !hadError)
Mike Stump1eb44332009-09-09 15:08:12 +0000423 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000424 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000425 } else if (InitListExpr *InnerILE
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000426 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000427 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000428 ++Init;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000429
430 // Only look at the first initialization of a union.
431 if (RType->getDecl()->isUnion())
432 break;
Douglas Gregor4c678342009-01-28 21:54:33 +0000433 }
434
435 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000436 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000437
438 QualType ElementType;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregor87fd7032009-02-02 17:43:21 +0000440 unsigned NumInits = ILE->getNumInits();
441 unsigned NumElements = NumInits;
Chris Lattner08202542009-02-24 22:50:46 +0000442 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000443 ElementType = AType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000444 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
445 NumElements = CAType->getSize().getZExtValue();
John McCall183700f2009-09-21 23:43:11 +0000446 } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000447 ElementType = VType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000448 NumElements = VType->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000449 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000450 ElementType = ILE->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Douglas Gregor87fd7032009-02-02 17:43:21 +0000452 for (unsigned Init = 0; Init != NumElements; ++Init) {
453 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattner08202542009-02-24 22:50:46 +0000454 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000455 hadError = true;
456 return;
457 }
458
Mike Stump390b4cc2009-05-16 07:39:55 +0000459 // FIXME: If value-initialization involves calling a constructor, should
460 // we make that call explicit in the representation (even when it means
461 // extending the initializer list)?
Douglas Gregor87fd7032009-02-02 17:43:21 +0000462 if (Init < NumInits && !hadError)
Mike Stump1eb44332009-09-09 15:08:12 +0000463 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000464 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000465 } else if (InitListExpr *InnerILE
466 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000467 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000468 }
469}
470
Chris Lattner68355a52009-01-29 05:10:57 +0000471
Chris Lattner08202542009-02-24 22:50:46 +0000472InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
473 : SemaRef(S) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000474 hadError = false;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000475
Eli Friedmanb85f7072008-05-19 19:16:24 +0000476 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000477 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000478 FullyStructuredList
Douglas Gregored8a93d2009-03-01 17:12:46 +0000479 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000480 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
481 /*TopLevelObject=*/true);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000482
Douglas Gregor930d8b52009-01-30 22:09:00 +0000483 if (!hadError)
484 FillInValueInitializations(FullyStructuredList);
Steve Naroff0cca7492008-05-01 22:18:59 +0000485}
486
487int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +0000488 // FIXME: use a proper constant
489 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000490 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000491 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000492 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
493 }
494 return maxElements;
495}
496
497int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000498 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor4c678342009-01-28 21:54:33 +0000499 int InitializableMembers = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000500 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000501 Field = structDecl->field_begin(),
502 FieldEnd = structDecl->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000503 Field != FieldEnd; ++Field) {
504 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
505 ++InitializableMembers;
506 }
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000507 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +0000508 return std::min(InitializableMembers, 1);
509 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-05-01 22:18:59 +0000510}
511
Mike Stump1eb44332009-09-09 15:08:12 +0000512void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregor4c678342009-01-28 21:54:33 +0000513 QualType T, unsigned &Index,
514 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000515 unsigned &StructuredIndex,
516 bool TopLevelObject) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000517 int maxElements = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Steve Naroff0cca7492008-05-01 22:18:59 +0000519 if (T->isArrayType())
520 maxElements = numArrayElements(T);
521 else if (T->isStructureType() || T->isUnionType())
522 maxElements = numStructUnionElements(T);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000523 else if (T->isVectorType())
John McCall183700f2009-09-21 23:43:11 +0000524 maxElements = T->getAs<VectorType>()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +0000525 else
526 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +0000527
Eli Friedman402256f2008-05-25 13:49:22 +0000528 if (maxElements == 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000529 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedman402256f2008-05-25 13:49:22 +0000530 diag::err_implicit_empty_initializer);
Douglas Gregor4c678342009-01-28 21:54:33 +0000531 ++Index;
Eli Friedman402256f2008-05-25 13:49:22 +0000532 hadError = true;
533 return;
534 }
535
Douglas Gregor4c678342009-01-28 21:54:33 +0000536 // Build a structured initializer list corresponding to this subobject.
537 InitListExpr *StructuredSubobjectInitList
Mike Stump1eb44332009-09-09 15:08:12 +0000538 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
539 StructuredIndex,
Douglas Gregored8a93d2009-03-01 17:12:46 +0000540 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
541 ParentIList->getSourceRange().getEnd()));
Douglas Gregor4c678342009-01-28 21:54:33 +0000542 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000543
Douglas Gregor4c678342009-01-28 21:54:33 +0000544 // Check the element types and build the structural subobject.
Douglas Gregor87fd7032009-02-02 17:43:21 +0000545 unsigned StartIndex = Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000546 CheckListElementTypes(ParentIList, T, false, Index,
Mike Stump1eb44332009-09-09 15:08:12 +0000547 StructuredSubobjectInitList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000548 StructuredSubobjectInitIndex,
549 TopLevelObject);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000550 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregora6457962009-03-20 00:32:56 +0000551 StructuredSubobjectInitList->setType(T);
552
Douglas Gregored8a93d2009-03-01 17:12:46 +0000553 // Update the structured sub-object initializer so that it's ending
Douglas Gregor87fd7032009-02-02 17:43:21 +0000554 // range corresponds with the end of the last initializer it used.
555 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000556 SourceLocation EndLoc
Douglas Gregor87fd7032009-02-02 17:43:21 +0000557 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
558 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
559 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000560}
561
Steve Naroffa647caa2008-05-06 00:23:44 +0000562void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor4c678342009-01-28 21:54:33 +0000563 unsigned &Index,
564 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000565 unsigned &StructuredIndex,
566 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000567 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor4c678342009-01-28 21:54:33 +0000568 SyntacticToSemantic[IList] = StructuredList;
569 StructuredList->setSyntacticForm(IList);
Mike Stump1eb44332009-09-09 15:08:12 +0000570 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000571 StructuredIndex, TopLevelObject);
Steve Naroffa647caa2008-05-06 00:23:44 +0000572 IList->setType(T);
Douglas Gregor4c678342009-01-28 21:54:33 +0000573 StructuredList->setType(T);
Eli Friedman638e1442008-05-25 13:22:35 +0000574 if (hadError)
575 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000576
Eli Friedman638e1442008-05-25 13:22:35 +0000577 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000578 // We have leftover initializers
Eli Friedmane5408582009-05-29 20:20:05 +0000579 if (StructuredIndex == 1 &&
580 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000581 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000582 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000583 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000584 hadError = true;
585 }
Eli Friedmanbb504d32008-05-19 20:12:18 +0000586 // Special-case
Chris Lattner08202542009-02-24 22:50:46 +0000587 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000588 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8dc2102008-05-20 05:25:56 +0000589 } else if (!T->isIncompleteType()) {
Douglas Gregorb574e562009-01-30 22:26:29 +0000590 // Don't complain for incomplete types, since we'll get an error
591 // elsewhere
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000592 QualType CurrentObjectType = StructuredList->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000593 int initKind =
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000594 CurrentObjectType->isArrayType()? 0 :
595 CurrentObjectType->isVectorType()? 1 :
596 CurrentObjectType->isScalarType()? 2 :
597 CurrentObjectType->isUnionType()? 3 :
598 4;
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000599
600 unsigned DK = diag::warn_excess_initializers;
Eli Friedmane5408582009-05-29 20:20:05 +0000601 if (SemaRef.getLangOptions().CPlusPlus) {
602 DK = diag::err_excess_initializers;
603 hadError = true;
604 }
Nate Begeman08634522009-07-07 21:53:06 +0000605 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
606 DK = diag::err_excess_initializers;
607 hadError = true;
608 }
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000609
Chris Lattner08202542009-02-24 22:50:46 +0000610 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000611 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000612 }
613 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000614
Eli Friedman759f2522009-05-16 11:45:48 +0000615 if (T->isScalarType() && !TopLevelObject)
Chris Lattner08202542009-02-24 22:50:46 +0000616 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregora3a83512009-04-01 23:51:29 +0000617 << IList->getSourceRange()
618 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart()))
619 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd()));
Steve Naroff0cca7492008-05-01 22:18:59 +0000620}
621
Eli Friedmanb85f7072008-05-19 19:16:24 +0000622void InitListChecker::CheckListElementTypes(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000623 QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000624 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000625 unsigned &Index,
626 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000627 unsigned &StructuredIndex,
628 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000629 if (DeclType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000630 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000631 } else if (DeclType->isVectorType()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000632 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregord7eb8462009-01-30 17:31:00 +0000633 } else if (DeclType->isAggregateType()) {
634 if (DeclType->isRecordType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000635 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000636 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000637 SubobjectIsDesignatorContext, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000638 StructuredList, StructuredIndex,
639 TopLevelObject);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000640 } else if (DeclType->isArrayType()) {
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000641 llvm::APSInt Zero(
Chris Lattner08202542009-02-24 22:50:46 +0000642 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000643 false);
Douglas Gregor4c678342009-01-28 21:54:33 +0000644 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
645 StructuredList, StructuredIndex);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000646 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000647 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000648 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
649 // This type is invalid, issue a diagnostic.
Douglas Gregor4c678342009-01-28 21:54:33 +0000650 ++Index;
Chris Lattner08202542009-02-24 22:50:46 +0000651 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000652 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000653 hadError = true;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000654 } else if (DeclType->isRecordType()) {
655 // C++ [dcl.init]p14:
656 // [...] If the class is an aggregate (8.5.1), and the initializer
657 // is a brace-enclosed list, see 8.5.1.
658 //
659 // Note: 8.5.1 is handled below; here, we diagnose the case where
660 // we have an initializer list and a destination type that is not
661 // an aggregate.
662 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner08202542009-02-24 22:50:46 +0000663 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000664 << DeclType << IList->getSourceRange();
665 hadError = true;
666 } else if (DeclType->isReferenceType()) {
667 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000668 } else {
669 // In C, all types are either scalars or aggregates, but
Mike Stump1eb44332009-09-09 15:08:12 +0000670 // additional handling is needed here for C++ (and possibly others?).
Steve Naroff0cca7492008-05-01 22:18:59 +0000671 assert(0 && "Unsupported initializer type");
672 }
673}
674
Eli Friedmanb85f7072008-05-19 19:16:24 +0000675void InitListChecker::CheckSubElementType(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000676 QualType ElemType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000677 unsigned &Index,
678 InitListExpr *StructuredList,
679 unsigned &StructuredIndex) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000680 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000681 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
682 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000683 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000684 InitListExpr *newStructuredList
Douglas Gregor4c678342009-01-28 21:54:33 +0000685 = getStructuredSubobjectInit(IList, Index, ElemType,
686 StructuredList, StructuredIndex,
687 SubInitList->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000688 CheckExplicitInitList(SubInitList, ElemType, newIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +0000689 newStructuredList, newStructuredIndex);
690 ++StructuredIndex;
691 ++Index;
Chris Lattner79e079d2009-02-24 23:10:27 +0000692 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
693 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000694 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor4c678342009-01-28 21:54:33 +0000695 ++Index;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000696 } else if (ElemType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000697 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000698 } else if (ElemType->isReferenceType()) {
699 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000700 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000701 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000702 // C++ [dcl.init.aggr]p12:
703 // All implicit type conversions (clause 4) are considered when
704 // initializing the aggregate member with an ini- tializer from
705 // an initializer-list. If the initializer can initialize a
706 // member, the member is initialized. [...]
Mike Stump1eb44332009-09-09 15:08:12 +0000707 ImplicitConversionSequence ICS
Anders Carlssond28b4282009-08-27 17:18:13 +0000708 = SemaRef.TryCopyInitialization(expr, ElemType,
709 /*SuppressUserConversions=*/false,
Anders Carlsson7b361b52009-08-27 17:37:39 +0000710 /*ForceRValue=*/false,
711 /*InOverloadResolution=*/false);
Anders Carlssond28b4282009-08-27 17:18:13 +0000712
Douglas Gregor930d8b52009-01-30 22:09:00 +0000713 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Mike Stump1eb44332009-09-09 15:08:12 +0000714 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000715 "initializing"))
716 hadError = true;
717 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
718 ++Index;
719 return;
720 }
721
722 // Fall through for subaggregate initialization
723 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000724 // C99 6.7.8p13:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000725 //
726 // The initializer for a structure or union object that has
727 // automatic storage duration shall be either an initializer
728 // list as described below, or a single expression that has
729 // compatible structure or union type. In the latter case, the
730 // initial value of the object, including unnamed members, is
731 // that of the expression.
Eli Friedman6b5374f2009-06-13 10:38:46 +0000732 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
Eli Friedman8718a6a2009-05-29 18:22:49 +0000733 SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000734 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
735 ++Index;
736 return;
737 }
738
739 // Fall through for subaggregate initialization
740 }
741
742 // C++ [dcl.init.aggr]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000743 //
Douglas Gregor930d8b52009-01-30 22:09:00 +0000744 // [...] Otherwise, if the member is itself a non-empty
745 // subaggregate, brace elision is assumed and the initializer is
746 // considered for the initialization of the first member of
747 // the subaggregate.
748 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000749 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000750 StructuredIndex);
751 ++StructuredIndex;
752 } else {
753 // We cannot initialize this element, so let
754 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattner08202542009-02-24 22:50:46 +0000755 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregor930d8b52009-01-30 22:09:00 +0000756 hadError = true;
757 ++Index;
758 ++StructuredIndex;
759 }
760 }
Eli Friedmanb85f7072008-05-19 19:16:24 +0000761}
762
Douglas Gregor930d8b52009-01-30 22:09:00 +0000763void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000764 unsigned &Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000765 InitListExpr *StructuredList,
766 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000767 if (Index < IList->getNumInits()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000768 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000769 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000770 SemaRef.Diag(IList->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000771 diag::err_many_braces_around_scalar_init)
772 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000773 hadError = true;
774 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000775 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000776 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000777 } else if (isa<DesignatedInitExpr>(expr)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000778 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor05c13a32009-01-22 00:58:24 +0000779 diag::err_designator_for_scalar_init)
780 << DeclType << expr->getSourceRange();
781 hadError = true;
782 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000783 ++StructuredIndex;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000784 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000785 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000786
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000787 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner08202542009-02-24 22:50:46 +0000788 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedmanbb504d32008-05-19 20:12:18 +0000789 hadError = true; // types weren't compatible.
Douglas Gregor05c13a32009-01-22 00:58:24 +0000790 else if (savExpr != expr) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000791 // The type was promoted, update initializer list.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000792 IList->setInit(Index, expr);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000793 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000794 if (hadError)
795 ++StructuredIndex;
796 else
797 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroff0cca7492008-05-01 22:18:59 +0000798 ++Index;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000799 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000800 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000801 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000802 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000803 ++Index;
804 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000805 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000806 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000807}
808
Douglas Gregor930d8b52009-01-30 22:09:00 +0000809void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
810 unsigned &Index,
811 InitListExpr *StructuredList,
812 unsigned &StructuredIndex) {
813 if (Index < IList->getNumInits()) {
814 Expr *expr = IList->getInit(Index);
815 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000816 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000817 << DeclType << IList->getSourceRange();
818 hadError = true;
819 ++Index;
820 ++StructuredIndex;
821 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000822 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000823
824 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000825 if (SemaRef.CheckReferenceInit(expr, DeclType,
Douglas Gregor739d8282009-09-23 23:04:10 +0000826 /*FIXME:*/expr->getLocStart(),
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000827 /*SuppressUserConversions=*/false,
828 /*AllowExplicit=*/false,
Mike Stump1eb44332009-09-09 15:08:12 +0000829 /*ForceRValue=*/false))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000830 hadError = true;
831 else if (savExpr != expr) {
832 // The type was promoted, update initializer list.
833 IList->setInit(Index, expr);
834 }
835 if (hadError)
836 ++StructuredIndex;
837 else
838 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
839 ++Index;
840 } else {
Mike Stump390b4cc2009-05-16 07:39:55 +0000841 // FIXME: It would be wonderful if we could point at the actual member. In
842 // general, it would be useful to pass location information down the stack,
843 // so that we know the location (or decl) of the "current object" being
844 // initialized.
Mike Stump1eb44332009-09-09 15:08:12 +0000845 SemaRef.Diag(IList->getLocStart(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000846 diag::err_init_reference_member_uninitialized)
847 << DeclType
848 << IList->getSourceRange();
849 hadError = true;
850 ++Index;
851 ++StructuredIndex;
852 return;
853 }
854}
855
Mike Stump1eb44332009-09-09 15:08:12 +0000856void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000857 unsigned &Index,
858 InitListExpr *StructuredList,
859 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000860 if (Index < IList->getNumInits()) {
John McCall183700f2009-09-21 23:43:11 +0000861 const VectorType *VT = DeclType->getAs<VectorType>();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000862 unsigned maxElements = VT->getNumElements();
863 unsigned numEltsInit = 0;
Steve Naroff0cca7492008-05-01 22:18:59 +0000864 QualType elementType = VT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Nate Begeman2ef13e52009-08-10 23:49:36 +0000866 if (!SemaRef.getLangOptions().OpenCL) {
867 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
868 // Don't attempt to go past the end of the init list
869 if (Index >= IList->getNumInits())
870 break;
871 CheckSubElementType(IList, elementType, Index,
872 StructuredList, StructuredIndex);
873 }
874 } else {
875 // OpenCL initializers allows vectors to be constructed from vectors.
876 for (unsigned i = 0; i < maxElements; ++i) {
877 // Don't attempt to go past the end of the init list
878 if (Index >= IList->getNumInits())
879 break;
880 QualType IType = IList->getInit(Index)->getType();
881 if (!IType->isVectorType()) {
882 CheckSubElementType(IList, elementType, Index,
883 StructuredList, StructuredIndex);
884 ++numEltsInit;
885 } else {
John McCall183700f2009-09-21 23:43:11 +0000886 const VectorType *IVT = IType->getAs<VectorType>();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000887 unsigned numIElts = IVT->getNumElements();
888 QualType VecType = SemaRef.Context.getExtVectorType(elementType,
889 numIElts);
890 CheckSubElementType(IList, VecType, Index,
891 StructuredList, StructuredIndex);
892 numEltsInit += numIElts;
893 }
894 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Nate Begeman2ef13e52009-08-10 23:49:36 +0000897 // OpenCL & AltiVec require all elements to be initialized.
898 if (numEltsInit != maxElements)
899 if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
900 SemaRef.Diag(IList->getSourceRange().getBegin(),
901 diag::err_vector_incorrect_num_initializers)
902 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Naroff0cca7492008-05-01 22:18:59 +0000903 }
904}
905
Mike Stump1eb44332009-09-09 15:08:12 +0000906void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000907 llvm::APSInt elementIndex,
Mike Stump1eb44332009-09-09 15:08:12 +0000908 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000909 unsigned &Index,
910 InitListExpr *StructuredList,
911 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000912 // Check for the special-case of initializing an array with a string.
913 if (Index < IList->getNumInits()) {
Chris Lattner79e079d2009-02-24 23:10:27 +0000914 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
915 SemaRef.Context)) {
916 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor4c678342009-01-28 21:54:33 +0000917 // We place the string literal directly into the resulting
918 // initializer list. This is the only place where the structure
919 // of the structured initializer list doesn't match exactly,
920 // because doing so would involve allocating one character
921 // constant for each string.
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000922 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner08202542009-02-24 22:50:46 +0000923 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000924 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000925 return;
926 }
927 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000928 if (const VariableArrayType *VAT =
Chris Lattner08202542009-02-24 22:50:46 +0000929 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman638e1442008-05-25 13:22:35 +0000930 // Check for VLAs; in standard C it would be possible to check this
931 // earlier, but I don't know where clang accepts VLAs (gcc accepts
932 // them in all sorts of strange places).
Chris Lattner08202542009-02-24 22:50:46 +0000933 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000934 diag::err_variable_object_no_init)
935 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000936 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000937 ++Index;
938 ++StructuredIndex;
Eli Friedman638e1442008-05-25 13:22:35 +0000939 return;
940 }
941
Douglas Gregor05c13a32009-01-22 00:58:24 +0000942 // We might know the maximum number of elements in advance.
Douglas Gregor4c678342009-01-28 21:54:33 +0000943 llvm::APSInt maxElements(elementIndex.getBitWidth(),
944 elementIndex.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000945 bool maxElementsKnown = false;
946 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000947 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000948 maxElements = CAT->getSize();
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000949 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000950 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000951 maxElementsKnown = true;
952 }
953
Chris Lattner08202542009-02-24 22:50:46 +0000954 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000955 ->getElementType();
Douglas Gregor05c13a32009-01-22 00:58:24 +0000956 while (Index < IList->getNumInits()) {
957 Expr *Init = IList->getInit(Index);
958 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000959 // If we're not the subobject that matches up with the '{' for
960 // the designator, we shouldn't be handling the
961 // designator. Return immediately.
962 if (!SubobjectIsDesignatorContext)
963 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000964
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000965 // Handle this designated initializer. elementIndex will be
966 // updated to be the next array element we'll initialize.
Mike Stump1eb44332009-09-09 15:08:12 +0000967 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +0000968 DeclType, 0, &elementIndex, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000969 StructuredList, StructuredIndex, true,
970 false)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000971 hadError = true;
972 continue;
973 }
974
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000975 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
976 maxElements.extend(elementIndex.getBitWidth());
977 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
978 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000979 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000980
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000981 // If the array is of incomplete type, keep track of the number of
982 // elements in the initializer.
983 if (!maxElementsKnown && elementIndex > maxElements)
984 maxElements = elementIndex;
985
Douglas Gregor05c13a32009-01-22 00:58:24 +0000986 continue;
987 }
988
989 // If we know the maximum number of elements, and we've already
990 // hit it, stop consuming elements in the initializer list.
991 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroff0cca7492008-05-01 22:18:59 +0000992 break;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000993
994 // Check this element.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000995 CheckSubElementType(IList, elementType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000996 StructuredList, StructuredIndex);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000997 ++elementIndex;
998
999 // If the array is of incomplete type, keep track of the number of
1000 // elements in the initializer.
1001 if (!maxElementsKnown && elementIndex > maxElements)
1002 maxElements = elementIndex;
Steve Naroff0cca7492008-05-01 22:18:59 +00001003 }
Eli Friedman587cbdf2009-05-29 20:17:55 +00001004 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +00001005 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001006 // be calculated here.
Douglas Gregore3fa2de2009-01-23 18:58:42 +00001007 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001008 if (maxElements == Zero) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001009 // Sizing an array implicitly to zero is not allowed by ISO C,
1010 // but is supported by GNU.
Chris Lattner08202542009-02-24 22:50:46 +00001011 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001012 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +00001013 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001014
Mike Stump1eb44332009-09-09 15:08:12 +00001015 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar396f0bf2008-08-18 20:28:46 +00001016 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +00001017 }
1018}
1019
Mike Stump1eb44332009-09-09 15:08:12 +00001020void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
1021 QualType DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001022 RecordDecl::field_iterator Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001023 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +00001024 unsigned &Index,
1025 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001026 unsigned &StructuredIndex,
1027 bool TopLevelObject) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001028 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Eli Friedmanb85f7072008-05-19 19:16:24 +00001030 // If the record is invalid, some of it's members are invalid. To avoid
1031 // confusion, we forgo checking the intializer for the entire record.
1032 if (structDecl->isInvalidDecl()) {
1033 hadError = true;
1034 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001035 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001036
1037 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1038 // Value-initialize the first named member of the union.
Ted Kremenek6217b802009-07-29 21:53:49 +00001039 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001040 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001041 Field != FieldEnd; ++Field) {
1042 if (Field->getDeclName()) {
1043 StructuredList->setInitializedFieldInUnion(*Field);
1044 break;
1045 }
1046 }
1047 return;
1048 }
1049
Douglas Gregor05c13a32009-01-22 00:58:24 +00001050 // If structDecl is a forward declaration, this loop won't do
1051 // anything except look at designated initializers; That's okay,
1052 // because an error should get printed out elsewhere. It might be
1053 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenek6217b802009-07-29 21:53:49 +00001054 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001055 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregordfb5e592009-02-12 19:00:39 +00001056 bool InitializedSomething = false;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001057 while (Index < IList->getNumInits()) {
1058 Expr *Init = IList->getInit(Index);
1059
1060 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001061 // If we're not the subobject that matches up with the '{' for
1062 // the designator, we shouldn't be handling the
1063 // designator. Return immediately.
1064 if (!SubobjectIsDesignatorContext)
1065 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001066
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001067 // Handle this designated initializer. Field will be updated to
1068 // the next field that we'll be initializing.
Mike Stump1eb44332009-09-09 15:08:12 +00001069 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +00001070 DeclType, &Field, 0, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001071 StructuredList, StructuredIndex,
1072 true, TopLevelObject))
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001073 hadError = true;
1074
Douglas Gregordfb5e592009-02-12 19:00:39 +00001075 InitializedSomething = true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001076 continue;
1077 }
1078
1079 if (Field == FieldEnd) {
1080 // We've run out of fields. We're done.
1081 break;
1082 }
1083
Douglas Gregordfb5e592009-02-12 19:00:39 +00001084 // We've already initialized a member of a union. We're done.
1085 if (InitializedSomething && DeclType->isUnionType())
1086 break;
1087
Douglas Gregor44b43212008-12-11 16:49:14 +00001088 // If we've hit the flexible array member at the end, we're done.
1089 if (Field->getType()->isIncompleteArrayType())
1090 break;
1091
Douglas Gregor0bb76892009-01-29 16:53:55 +00001092 if (Field->isUnnamedBitfield()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001093 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregor05c13a32009-01-22 00:58:24 +00001094 ++Field;
Eli Friedmanb85f7072008-05-19 19:16:24 +00001095 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +00001096 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001097
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001098 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001099 StructuredList, StructuredIndex);
Douglas Gregordfb5e592009-02-12 19:00:39 +00001100 InitializedSomething = true;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001101
1102 if (DeclType->isUnionType()) {
1103 // Initialize the first field within the union.
1104 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001105 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001106
1107 ++Field;
Steve Naroff0cca7492008-05-01 22:18:59 +00001108 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001109
Mike Stump1eb44332009-09-09 15:08:12 +00001110 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregora6457962009-03-20 00:32:56 +00001111 Index >= IList->getNumInits())
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001112 return;
1113
1114 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001115 if (!TopLevelObject &&
Douglas Gregora6457962009-03-20 00:32:56 +00001116 (!isa<InitListExpr>(IList->getInit(Index)) ||
1117 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001118 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001119 diag::err_flexible_array_init_nonempty)
1120 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001121 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001122 << *Field;
1123 hadError = true;
Douglas Gregora6457962009-03-20 00:32:56 +00001124 ++Index;
1125 return;
1126 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001127 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregora6457962009-03-20 00:32:56 +00001128 diag::ext_flexible_array_init)
1129 << IList->getInit(Index)->getSourceRange().getBegin();
1130 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1131 << *Field;
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001132 }
1133
Douglas Gregora6457962009-03-20 00:32:56 +00001134 if (isa<InitListExpr>(IList->getInit(Index)))
1135 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1136 StructuredIndex);
1137 else
1138 CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1139 StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +00001140}
Steve Naroff0cca7492008-05-01 22:18:59 +00001141
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001142/// \brief Expand a field designator that refers to a member of an
1143/// anonymous struct or union into a series of field designators that
1144/// refers to the field within the appropriate subobject.
1145///
1146/// Field/FieldIndex will be updated to point to the (new)
1147/// currently-designated field.
1148static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump1eb44332009-09-09 15:08:12 +00001149 DesignatedInitExpr *DIE,
1150 unsigned DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001151 FieldDecl *Field,
1152 RecordDecl::field_iterator &FieldIter,
1153 unsigned &FieldIndex) {
1154 typedef DesignatedInitExpr::Designator Designator;
1155
1156 // Build the path from the current object to the member of the
1157 // anonymous struct/union (backwards).
1158 llvm::SmallVector<FieldDecl *, 4> Path;
1159 SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001161 // Build the replacement designators.
1162 llvm::SmallVector<Designator, 4> Replacements;
1163 for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1164 FI = Path.rbegin(), FIEnd = Path.rend();
1165 FI != FIEnd; ++FI) {
1166 if (FI + 1 == FIEnd)
Mike Stump1eb44332009-09-09 15:08:12 +00001167 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001168 DIE->getDesignator(DesigIdx)->getDotLoc(),
1169 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1170 else
1171 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1172 SourceLocation()));
1173 Replacements.back().setField(*FI);
1174 }
1175
1176 // Expand the current designator into the set of replacement
1177 // designators, so we have a full subobject path down to where the
1178 // member of the anonymous struct/union is actually stored.
Mike Stump1eb44332009-09-09 15:08:12 +00001179 DIE->ExpandDesignator(DesigIdx, &Replacements[0],
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001180 &Replacements[0] + Replacements.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001182 // Update FieldIter/FieldIndex;
1183 RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001184 FieldIter = Record->field_begin();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001185 FieldIndex = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001186 for (RecordDecl::field_iterator FEnd = Record->field_end();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001187 FieldIter != FEnd; ++FieldIter) {
1188 if (FieldIter->isUnnamedBitfield())
1189 continue;
1190
1191 if (*FieldIter == Path.back())
1192 return;
1193
1194 ++FieldIndex;
1195 }
1196
1197 assert(false && "Unable to find anonymous struct/union field");
1198}
1199
Douglas Gregor05c13a32009-01-22 00:58:24 +00001200/// @brief Check the well-formedness of a C99 designated initializer.
1201///
1202/// Determines whether the designated initializer @p DIE, which
1203/// resides at the given @p Index within the initializer list @p
1204/// IList, is well-formed for a current object of type @p DeclType
1205/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump1eb44332009-09-09 15:08:12 +00001206/// within the current subobject is returned in either
Douglas Gregor4c678342009-01-28 21:54:33 +00001207/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregor05c13a32009-01-22 00:58:24 +00001208///
1209/// @param IList The initializer list in which this designated
1210/// initializer occurs.
1211///
Douglas Gregor71199712009-04-15 04:56:10 +00001212/// @param DIE The designated initializer expression.
1213///
1214/// @param DesigIdx The index of the current designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001215///
1216/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1217/// into which the designation in @p DIE should refer.
1218///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001219/// @param NextField If non-NULL and the first designator in @p DIE is
1220/// a field, this will be set to the field declaration corresponding
1221/// to the field named by the designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001222///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001223/// @param NextElementIndex If non-NULL and the first designator in @p
1224/// DIE is an array designator or GNU array-range designator, this
1225/// will be set to the last index initialized by this designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001226///
1227/// @param Index Index into @p IList where the designated initializer
1228/// @p DIE occurs.
1229///
Douglas Gregor4c678342009-01-28 21:54:33 +00001230/// @param StructuredList The initializer list expression that
1231/// describes all of the subobject initializers in the order they'll
1232/// actually be initialized.
1233///
Douglas Gregor05c13a32009-01-22 00:58:24 +00001234/// @returns true if there was an error, false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001235bool
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001236InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001237 DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +00001238 unsigned DesigIdx,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001239 QualType &CurrentObjectType,
1240 RecordDecl::field_iterator *NextField,
1241 llvm::APSInt *NextElementIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001242 unsigned &Index,
1243 InitListExpr *StructuredList,
Douglas Gregor34e79462009-01-28 23:36:17 +00001244 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001245 bool FinishSubobjectInit,
1246 bool TopLevelObject) {
Douglas Gregor71199712009-04-15 04:56:10 +00001247 if (DesigIdx == DIE->size()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001248 // Check the actual initialization for the designated object type.
1249 bool prevHadError = hadError;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001250
1251 // Temporarily remove the designator expression from the
1252 // initializer list that the child calls see, so that we don't try
1253 // to re-process the designator.
1254 unsigned OldIndex = Index;
1255 IList->setInit(OldIndex, DIE->getInit());
1256
1257 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001258 StructuredList, StructuredIndex);
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001259
1260 // Restore the designated initializer expression in the syntactic
1261 // form of the initializer list.
1262 if (IList->getInit(OldIndex) != DIE->getInit())
1263 DIE->setInit(IList->getInit(OldIndex));
1264 IList->setInit(OldIndex, DIE);
1265
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001266 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001267 }
1268
Douglas Gregor71199712009-04-15 04:56:10 +00001269 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001270 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor4c678342009-01-28 21:54:33 +00001271 "Need a non-designated initializer list to start from");
1272
Douglas Gregor71199712009-04-15 04:56:10 +00001273 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001274 // Determine the structural initializer list that corresponds to the
1275 // current subobject.
1276 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump1eb44332009-09-09 15:08:12 +00001277 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001278 StructuredList, StructuredIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001279 SourceRange(D->getStartLocation(),
1280 DIE->getSourceRange().getEnd()));
1281 assert(StructuredList && "Expected a structured initializer list");
1282
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001283 if (D->isFieldDesignator()) {
1284 // C99 6.7.8p7:
1285 //
1286 // If a designator has the form
1287 //
1288 // . identifier
1289 //
1290 // then the current object (defined below) shall have
1291 // structure or union type and the identifier shall be the
Mike Stump1eb44332009-09-09 15:08:12 +00001292 // name of a member of that type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001293 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001294 if (!RT) {
1295 SourceLocation Loc = D->getDotLoc();
1296 if (Loc.isInvalid())
1297 Loc = D->getFieldLoc();
Chris Lattner08202542009-02-24 22:50:46 +00001298 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1299 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001300 ++Index;
1301 return true;
1302 }
1303
Douglas Gregor4c678342009-01-28 21:54:33 +00001304 // Note: we perform a linear search of the fields here, despite
1305 // the fact that we have a faster lookup method, because we always
1306 // need to compute the field's index.
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001307 FieldDecl *KnownField = D->getField();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001308 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor4c678342009-01-28 21:54:33 +00001309 unsigned FieldIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001310 RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001311 Field = RT->getDecl()->field_begin(),
1312 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +00001313 for (; Field != FieldEnd; ++Field) {
1314 if (Field->isUnnamedBitfield())
1315 continue;
1316
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001317 if (KnownField == *Field || Field->getIdentifier() == FieldName)
Douglas Gregor4c678342009-01-28 21:54:33 +00001318 break;
1319
1320 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001321 }
1322
Douglas Gregor4c678342009-01-28 21:54:33 +00001323 if (Field == FieldEnd) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001324 // There was no normal field in the struct with the designated
1325 // name. Perform another lookup for this name, which may find
1326 // something that we can't designate (e.g., a member function),
1327 // may find nothing, or may find a member of an anonymous
Mike Stump1eb44332009-09-09 15:08:12 +00001328 // struct/union.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001329 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregor4c678342009-01-28 21:54:33 +00001330 if (Lookup.first == Lookup.second) {
1331 // Name lookup didn't find anything.
Chris Lattner08202542009-02-24 22:50:46 +00001332 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregor4c678342009-01-28 21:54:33 +00001333 << FieldName << CurrentObjectType;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001334 ++Index;
1335 return true;
1336 } else if (!KnownField && isa<FieldDecl>(*Lookup.first) &&
1337 cast<RecordDecl>((*Lookup.first)->getDeclContext())
1338 ->isAnonymousStructOrUnion()) {
1339 // Handle an field designator that refers to a member of an
1340 // anonymous struct or union.
Mike Stump1eb44332009-09-09 15:08:12 +00001341 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001342 cast<FieldDecl>(*Lookup.first),
1343 Field, FieldIndex);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001344 D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001345 } else {
1346 // Name lookup found something, but it wasn't a field.
Chris Lattner08202542009-02-24 22:50:46 +00001347 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor4c678342009-01-28 21:54:33 +00001348 << FieldName;
Mike Stump1eb44332009-09-09 15:08:12 +00001349 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001350 diag::note_field_designator_found);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001351 ++Index;
1352 return true;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001353 }
1354 } else if (!KnownField &&
1355 cast<RecordDecl>((*Field)->getDeclContext())
Douglas Gregor4c678342009-01-28 21:54:33 +00001356 ->isAnonymousStructOrUnion()) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001357 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1358 Field, FieldIndex);
1359 D = DIE->getDesignator(DesigIdx);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001360 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001361
1362 // All of the fields of a union are located at the same place in
1363 // the initializer list.
Douglas Gregor0bb76892009-01-29 16:53:55 +00001364 if (RT->getDecl()->isUnion()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001365 FieldIndex = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001366 StructuredList->setInitializedFieldInUnion(*Field);
1367 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001368
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001369 // Update the designator with the field declaration.
Douglas Gregor4c678342009-01-28 21:54:33 +00001370 D->setField(*Field);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregor4c678342009-01-28 21:54:33 +00001372 // Make sure that our non-designated initializer list has space
1373 // for a subobject corresponding to this field.
1374 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001375 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001376
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001377 // This designator names a flexible array member.
1378 if (Field->getType()->isIncompleteArrayType()) {
1379 bool Invalid = false;
Douglas Gregor71199712009-04-15 04:56:10 +00001380 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001381 // We can't designate an object within the flexible array
1382 // member (because GCC doesn't allow it).
Mike Stump1eb44332009-09-09 15:08:12 +00001383 DesignatedInitExpr::Designator *NextD
Douglas Gregor71199712009-04-15 04:56:10 +00001384 = DIE->getDesignator(DesigIdx + 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001385 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001386 diag::err_designator_into_flexible_array_member)
Mike Stump1eb44332009-09-09 15:08:12 +00001387 << SourceRange(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001388 DIE->getSourceRange().getEnd());
Chris Lattner08202542009-02-24 22:50:46 +00001389 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001390 << *Field;
1391 Invalid = true;
1392 }
1393
1394 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1395 // The initializer is not an initializer list.
Chris Lattner08202542009-02-24 22:50:46 +00001396 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001397 diag::err_flexible_array_init_needs_braces)
1398 << DIE->getInit()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001399 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001400 << *Field;
1401 Invalid = true;
1402 }
1403
1404 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001405 if (!Invalid && !TopLevelObject &&
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001406 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +00001407 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001408 diag::err_flexible_array_init_nonempty)
1409 << DIE->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001410 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001411 << *Field;
1412 Invalid = true;
1413 }
1414
1415 if (Invalid) {
1416 ++Index;
1417 return true;
1418 }
1419
1420 // Initialize the array.
1421 bool prevHadError = hadError;
1422 unsigned newStructuredIndex = FieldIndex;
1423 unsigned OldIndex = Index;
1424 IList->setInit(Index, DIE->getInit());
Mike Stump1eb44332009-09-09 15:08:12 +00001425 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001426 StructuredList, newStructuredIndex);
1427 IList->setInit(OldIndex, DIE);
1428 if (hadError && !prevHadError) {
1429 ++Field;
1430 ++FieldIndex;
1431 if (NextField)
1432 *NextField = Field;
1433 StructuredIndex = FieldIndex;
1434 return true;
1435 }
1436 } else {
1437 // Recurse to check later designated subobjects.
1438 QualType FieldType = (*Field)->getType();
1439 unsigned newStructuredIndex = FieldIndex;
Douglas Gregor71199712009-04-15 04:56:10 +00001440 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
1441 Index, StructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001442 true, false))
1443 return true;
1444 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001445
1446 // Find the position of the next field to be initialized in this
1447 // subobject.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001448 ++Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001449 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001450
1451 // If this the first designator, our caller will continue checking
1452 // the rest of this struct/class/union subobject.
1453 if (IsFirstDesignator) {
1454 if (NextField)
1455 *NextField = Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001456 StructuredIndex = FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001457 return false;
1458 }
1459
Douglas Gregor34e79462009-01-28 23:36:17 +00001460 if (!FinishSubobjectInit)
1461 return false;
1462
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001463 // We've already initialized something in the union; we're done.
1464 if (RT->getDecl()->isUnion())
1465 return hadError;
1466
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001467 // Check the remaining fields within this class/struct/union subobject.
1468 bool prevHadError = hadError;
Douglas Gregor4c678342009-01-28 21:54:33 +00001469 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1470 StructuredList, FieldIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001471 return hadError && !prevHadError;
1472 }
1473
1474 // C99 6.7.8p6:
1475 //
1476 // If a designator has the form
1477 //
1478 // [ constant-expression ]
1479 //
1480 // then the current object (defined below) shall have array
1481 // type and the expression shall be an integer constant
1482 // expression. If the array is of unknown size, any
1483 // nonnegative value is valid.
1484 //
1485 // Additionally, cope with the GNU extension that permits
1486 // designators of the form
1487 //
1488 // [ constant-expression ... constant-expression ]
Chris Lattner08202542009-02-24 22:50:46 +00001489 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001490 if (!AT) {
Chris Lattner08202542009-02-24 22:50:46 +00001491 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001492 << CurrentObjectType;
1493 ++Index;
1494 return true;
1495 }
1496
1497 Expr *IndexExpr = 0;
Douglas Gregor34e79462009-01-28 23:36:17 +00001498 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1499 if (D->isArrayDesignator()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001500 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattner3bf68932009-04-25 21:59:05 +00001501 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001502 DesignatedEndIndex = DesignatedStartIndex;
1503 } else {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001504 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor34e79462009-01-28 23:36:17 +00001505
Mike Stump1eb44332009-09-09 15:08:12 +00001506
1507 DesignatedStartIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001508 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001509 DesignatedEndIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001510 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001511 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001512
Chris Lattner3bf68932009-04-25 21:59:05 +00001513 if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
Douglas Gregora9c87802009-01-29 19:42:23 +00001514 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001515 }
1516
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001517 if (isa<ConstantArrayType>(AT)) {
1518 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor34e79462009-01-28 23:36:17 +00001519 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1520 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1521 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1522 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1523 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner08202542009-02-24 22:50:46 +00001524 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001525 diag::err_array_designator_too_large)
Douglas Gregor34e79462009-01-28 23:36:17 +00001526 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001527 << IndexExpr->getSourceRange();
1528 ++Index;
1529 return true;
1530 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001531 } else {
1532 // Make sure the bit-widths and signedness match.
1533 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1534 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattner3bf68932009-04-25 21:59:05 +00001535 else if (DesignatedStartIndex.getBitWidth() <
1536 DesignatedEndIndex.getBitWidth())
Douglas Gregor34e79462009-01-28 23:36:17 +00001537 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1538 DesignatedStartIndex.setIsUnsigned(true);
1539 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregor4c678342009-01-28 21:54:33 +00001542 // Make sure that our non-designated initializer list has space
1543 // for a subobject corresponding to this array element.
Douglas Gregor34e79462009-01-28 23:36:17 +00001544 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump1eb44332009-09-09 15:08:12 +00001545 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor34e79462009-01-28 23:36:17 +00001546 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001547
Douglas Gregor34e79462009-01-28 23:36:17 +00001548 // Repeatedly perform subobject initializations in the range
1549 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001550
Douglas Gregor34e79462009-01-28 23:36:17 +00001551 // Move to the next designator
1552 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1553 unsigned OldIndex = Index;
Douglas Gregor34e79462009-01-28 23:36:17 +00001554 while (DesignatedStartIndex <= DesignatedEndIndex) {
1555 // Recurse to check later designated subobjects.
1556 QualType ElementType = AT->getElementType();
1557 Index = OldIndex;
Douglas Gregor71199712009-04-15 04:56:10 +00001558 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
1559 Index, StructuredList, ElementIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001560 (DesignatedStartIndex == DesignatedEndIndex),
1561 false))
Douglas Gregor34e79462009-01-28 23:36:17 +00001562 return true;
1563
1564 // Move to the next index in the array that we'll be initializing.
1565 ++DesignatedStartIndex;
1566 ElementIndex = DesignatedStartIndex.getZExtValue();
1567 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001568
1569 // If this the first designator, our caller will continue checking
1570 // the rest of this array subobject.
1571 if (IsFirstDesignator) {
1572 if (NextElementIndex)
Douglas Gregor34e79462009-01-28 23:36:17 +00001573 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor4c678342009-01-28 21:54:33 +00001574 StructuredIndex = ElementIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001575 return false;
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregor34e79462009-01-28 23:36:17 +00001578 if (!FinishSubobjectInit)
1579 return false;
1580
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001581 // Check the remaining elements within this array subobject.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001582 bool prevHadError = hadError;
Douglas Gregorfdf55692009-02-09 19:45:19 +00001583 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001584 StructuredList, ElementIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001585 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001586}
1587
Douglas Gregor4c678342009-01-28 21:54:33 +00001588// Get the structured initializer list for a subobject of type
1589// @p CurrentObjectType.
1590InitListExpr *
1591InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1592 QualType CurrentObjectType,
1593 InitListExpr *StructuredList,
1594 unsigned StructuredIndex,
1595 SourceRange InitRange) {
1596 Expr *ExistingInit = 0;
1597 if (!StructuredList)
1598 ExistingInit = SyntacticToSemantic[IList];
1599 else if (StructuredIndex < StructuredList->getNumInits())
1600 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregor4c678342009-01-28 21:54:33 +00001602 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1603 return Result;
1604
1605 if (ExistingInit) {
1606 // We are creating an initializer list that initializes the
1607 // subobjects of the current object, but there was already an
1608 // initialization that completely initialized the current
1609 // subobject, e.g., by a compound literal:
Mike Stump1eb44332009-09-09 15:08:12 +00001610 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001611 // struct X { int a, b; };
1612 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump1eb44332009-09-09 15:08:12 +00001613 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001614 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1615 // designated initializer re-initializes the whole
1616 // subobject [0], overwriting previous initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001617 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregored8a93d2009-03-01 17:12:46 +00001618 diag::warn_subobject_initializer_overrides)
Douglas Gregor4c678342009-01-28 21:54:33 +00001619 << InitRange;
Mike Stump1eb44332009-09-09 15:08:12 +00001620 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001621 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001622 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001623 << ExistingInit->getSourceRange();
1624 }
1625
Mike Stump1eb44332009-09-09 15:08:12 +00001626 InitListExpr *Result
1627 = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001628 InitRange.getEnd());
1629
Douglas Gregor4c678342009-01-28 21:54:33 +00001630 Result->setType(CurrentObjectType);
1631
Douglas Gregorfa219202009-03-20 23:58:33 +00001632 // Pre-allocate storage for the structured initializer list.
1633 unsigned NumElements = 0;
Douglas Gregor08457732009-03-21 18:13:52 +00001634 unsigned NumInits = 0;
1635 if (!StructuredList)
1636 NumInits = IList->getNumInits();
1637 else if (Index < IList->getNumInits()) {
1638 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1639 NumInits = SubList->getNumInits();
1640 }
1641
Mike Stump1eb44332009-09-09 15:08:12 +00001642 if (const ArrayType *AType
Douglas Gregorfa219202009-03-20 23:58:33 +00001643 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1644 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1645 NumElements = CAType->getSize().getZExtValue();
1646 // Simple heuristic so that we don't allocate a very large
1647 // initializer with many empty entries at the end.
Douglas Gregor08457732009-03-21 18:13:52 +00001648 if (NumInits && NumElements > NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001649 NumElements = 0;
1650 }
John McCall183700f2009-09-21 23:43:11 +00001651 } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
Douglas Gregorfa219202009-03-20 23:58:33 +00001652 NumElements = VType->getNumElements();
Ted Kremenek6217b802009-07-29 21:53:49 +00001653 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregorfa219202009-03-20 23:58:33 +00001654 RecordDecl *RDecl = RType->getDecl();
1655 if (RDecl->isUnion())
1656 NumElements = 1;
1657 else
Mike Stump1eb44332009-09-09 15:08:12 +00001658 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001659 RDecl->field_end());
Douglas Gregorfa219202009-03-20 23:58:33 +00001660 }
1661
Douglas Gregor08457732009-03-21 18:13:52 +00001662 if (NumElements < NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001663 NumElements = IList->getNumInits();
1664
1665 Result->reserveInits(NumElements);
1666
Douglas Gregor4c678342009-01-28 21:54:33 +00001667 // Link this new initializer list into the structured initializer
1668 // lists.
1669 if (StructuredList)
1670 StructuredList->updateInit(StructuredIndex, Result);
1671 else {
1672 Result->setSyntacticForm(IList);
1673 SyntacticToSemantic[IList] = Result;
1674 }
1675
1676 return Result;
1677}
1678
1679/// Update the initializer at index @p StructuredIndex within the
1680/// structured initializer list to the value @p expr.
1681void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1682 unsigned &StructuredIndex,
1683 Expr *expr) {
1684 // No structured initializer list to update
1685 if (!StructuredList)
1686 return;
1687
1688 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1689 // This initializer overwrites a previous initializer. Warn.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001691 diag::warn_initializer_overrides)
1692 << expr->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001693 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001694 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001695 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001696 << PrevInit->getSourceRange();
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Douglas Gregor4c678342009-01-28 21:54:33 +00001699 ++StructuredIndex;
1700}
1701
Douglas Gregor05c13a32009-01-22 00:58:24 +00001702/// Check that the given Index expression is a valid array designator
1703/// value. This is essentailly just a wrapper around
Chris Lattner3bf68932009-04-25 21:59:05 +00001704/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregor05c13a32009-01-22 00:58:24 +00001705/// and produces a reasonable diagnostic if there is a
1706/// failure. Returns true if there was an error, false otherwise. If
1707/// everything went okay, Value will receive the value of the constant
1708/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001709static bool
Chris Lattner3bf68932009-04-25 21:59:05 +00001710CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001711 SourceLocation Loc = Index->getSourceRange().getBegin();
1712
1713 // Make sure this is an integer constant expression.
Chris Lattner3bf68932009-04-25 21:59:05 +00001714 if (S.VerifyIntegerConstantExpression(Index, &Value))
1715 return true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001716
Chris Lattner3bf68932009-04-25 21:59:05 +00001717 if (Value.isSigned() && Value.isNegative())
1718 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001719 << Value.toString(10) << Index->getSourceRange();
1720
Douglas Gregor53d3d8e2009-01-23 21:04:18 +00001721 Value.setIsUnsigned(true);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001722 return false;
1723}
1724
1725Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1726 SourceLocation Loc,
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001727 bool GNUSyntax,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001728 OwningExprResult Init) {
1729 typedef DesignatedInitExpr::Designator ASTDesignator;
1730
1731 bool Invalid = false;
1732 llvm::SmallVector<ASTDesignator, 32> Designators;
1733 llvm::SmallVector<Expr *, 32> InitExpressions;
1734
1735 // Build designators and check array designator expressions.
1736 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1737 const Designator &D = Desig.getDesignator(Idx);
1738 switch (D.getKind()) {
1739 case Designator::FieldDesignator:
Mike Stump1eb44332009-09-09 15:08:12 +00001740 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001741 D.getFieldLoc()));
1742 break;
1743
1744 case Designator::ArrayDesignator: {
1745 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1746 llvm::APSInt IndexValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001747 if (!Index->isTypeDependent() &&
1748 !Index->isValueDependent() &&
1749 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001750 Invalid = true;
1751 else {
1752 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001753 D.getLBracketLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001754 D.getRBracketLoc()));
1755 InitExpressions.push_back(Index);
1756 }
1757 break;
1758 }
1759
1760 case Designator::ArrayRangeDesignator: {
1761 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1762 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1763 llvm::APSInt StartValue;
1764 llvm::APSInt EndValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001765 bool StartDependent = StartIndex->isTypeDependent() ||
1766 StartIndex->isValueDependent();
1767 bool EndDependent = EndIndex->isTypeDependent() ||
1768 EndIndex->isValueDependent();
1769 if ((!StartDependent &&
1770 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1771 (!EndDependent &&
1772 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001773 Invalid = true;
Douglas Gregord6f584f2009-01-23 22:22:29 +00001774 else {
1775 // Make sure we're comparing values with the same bit width.
Douglas Gregor9ea62762009-05-21 23:17:49 +00001776 if (StartDependent || EndDependent) {
1777 // Nothing to compute.
1778 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Douglas Gregord6f584f2009-01-23 22:22:29 +00001779 EndValue.extend(StartValue.getBitWidth());
1780 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1781 StartValue.extend(EndValue.getBitWidth());
1782
Douglas Gregorc4bb7bf2009-05-21 23:30:39 +00001783 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregord6f584f2009-01-23 22:22:29 +00001784 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump1eb44332009-09-09 15:08:12 +00001785 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregord6f584f2009-01-23 22:22:29 +00001786 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1787 Invalid = true;
1788 } else {
1789 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001790 D.getLBracketLoc(),
Douglas Gregord6f584f2009-01-23 22:22:29 +00001791 D.getEllipsisLoc(),
1792 D.getRBracketLoc()));
1793 InitExpressions.push_back(StartIndex);
1794 InitExpressions.push_back(EndIndex);
1795 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001796 }
1797 break;
1798 }
1799 }
1800 }
1801
1802 if (Invalid || Init.isInvalid())
1803 return ExprError();
1804
1805 // Clear out the expressions within the designation.
1806 Desig.ClearExprs(*this);
1807
1808 DesignatedInitExpr *DIE
Jay Foadbeaaccd2009-05-21 09:52:38 +00001809 = DesignatedInitExpr::Create(Context,
1810 Designators.data(), Designators.size(),
1811 InitExpressions.data(), InitExpressions.size(),
Anders Carlssone9146f22009-05-01 19:49:17 +00001812 Loc, GNUSyntax, Init.takeAs<Expr>());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001813 return Owned(DIE);
1814}
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001815
1816bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattner08202542009-02-24 22:50:46 +00001817 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001818 if (!CheckInitList.HadError())
1819 InitList = CheckInitList.getFullyStructuredList();
1820
1821 return CheckInitList.HadError();
1822}
Douglas Gregor87fd7032009-02-02 17:43:21 +00001823
1824/// \brief Diagnose any semantic errors with value-initialization of
1825/// the given type.
1826///
1827/// Value-initialization effectively zero-initializes any types
1828/// without user-declared constructors, and calls the default
1829/// constructor for a for any type that has a user-declared
1830/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1831/// a type with a user-declared constructor does not have an
1832/// accessible, non-deleted default constructor. In C, everything can
1833/// be value-initialized, which corresponds to C's notion of
1834/// initializing objects with static storage duration when no
Mike Stump1eb44332009-09-09 15:08:12 +00001835/// initializer is provided for that object.
Douglas Gregor87fd7032009-02-02 17:43:21 +00001836///
1837/// \returns true if there was an error, false otherwise.
1838bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1839 // C++ [dcl.init]p5:
1840 //
1841 // To value-initialize an object of type T means:
1842
1843 // -- if T is an array type, then each element is value-initialized;
1844 if (const ArrayType *AT = Context.getAsArrayType(Type))
1845 return CheckValueInitialization(AT->getElementType(), Loc);
1846
Ted Kremenek6217b802009-07-29 21:53:49 +00001847 if (const RecordType *RT = Type->getAs<RecordType>()) {
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001848 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Douglas Gregor87fd7032009-02-02 17:43:21 +00001849 // -- if T is a class type (clause 9) with a user-declared
1850 // constructor (12.1), then the default constructor for T is
1851 // called (and the initialization is ill-formed if T has no
1852 // accessible default constructor);
Douglas Gregor39da0b82009-09-09 23:08:42 +00001853 if (ClassDecl->hasUserDeclaredConstructor()) {
1854 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
1855
1856 CXXConstructorDecl *Constructor
1857 = PerformInitializationByConstructor(Type,
1858 MultiExprArg(*this, 0, 0),
1859 Loc, SourceRange(Loc),
1860 DeclarationName(),
1861 IK_Direct,
1862 ConstructorArgs);
1863 if (!Constructor)
1864 return true;
1865
1866 OwningExprResult Init
1867 = BuildCXXConstructExpr(Loc, Type, Constructor,
1868 move_arg(ConstructorArgs));
1869 if (Init.isInvalid())
1870 return true;
1871
1872 // FIXME: Actually perform the value-initialization!
1873 return false;
1874 }
Douglas Gregor87fd7032009-02-02 17:43:21 +00001875 }
1876 }
1877
1878 if (Type->isReferenceType()) {
1879 // C++ [dcl.init]p5:
1880 // [...] A program that calls for default-initialization or
1881 // value-initialization of an entity of reference type is
1882 // ill-formed. [...]
Mike Stump390b4cc2009-05-16 07:39:55 +00001883 // FIXME: Once we have code that goes through this path, add an actual
1884 // diagnostic :)
Douglas Gregor87fd7032009-02-02 17:43:21 +00001885 }
1886
1887 return false;
1888}