blob: 0f973d6d9ba3bf6eb6edcd1f5b859689d3893545 [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 Gregor9ea62762009-05-21 23:17:49 +0000139 Init->isTypeDependent() || Init->isValueDependent())
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000140 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000142 // C++ [dcl.init.ref]p1:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000143 // A variable declared to be a T& or T&&, that is "reference to type T"
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000144 // (8.3.2), shall be initialized by an object, or function, of
145 // type T or by an object that can be converted into a T.
146 if (DeclType->isReferenceType())
Douglas Gregor739d8282009-09-23 23:04:10 +0000147 return CheckReferenceInit(Init, DeclType, InitLoc,
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000148 /*SuppressUserConversions=*/false,
149 /*AllowExplicit=*/DirectInit,
150 /*ForceRValue=*/false);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000152 // C99 6.7.8p3: The type of the entity to be initialized shall be an array
153 // of unknown size ("[]") or an object type that is not a variable array type.
154 if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
155 return Diag(InitLoc, diag::err_variable_object_no_init)
156 << VAT->getSizeExpr()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000158 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
159 if (!InitList) {
160 // FIXME: Handle wide strings
Chris Lattner79e079d2009-02-24 23:10:27 +0000161 if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
162 CheckStringInit(Str, DeclType, *this);
Chris Lattner19da8cd2009-02-24 23:01:39 +0000163 return false;
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000166 // C++ [dcl.init]p14:
167 // -- If the destination type is a (possibly cv-qualified) class
168 // type:
169 if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
170 QualType DeclTypeC = Context.getCanonicalType(DeclType);
171 QualType InitTypeC = Context.getCanonicalType(Init->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000173 // -- If the initialization is direct-initialization, or if it is
174 // copy-initialization where the cv-unqualified version of the
175 // source type is the same class as, or a derived class of, the
176 // class of the destination, constructors are considered.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000177 if ((DeclTypeC.getLocalUnqualifiedType()
178 == InitTypeC.getLocalUnqualifiedType()) ||
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000179 IsDerivedFrom(InitTypeC, DeclTypeC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000180 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000181 cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Anders Carlssonbffed8a2009-05-27 16:38:58 +0000183 // No need to make a CXXConstructExpr if both the ctor and dtor are
184 // trivial.
185 if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor())
186 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor39da0b82009-09-09 23:08:42 +0000188 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
189
Mike Stump1eb44332009-09-09 15:08:12 +0000190 CXXConstructorDecl *Constructor
Douglas Gregor39da0b82009-09-09 23:08:42 +0000191 = PerformInitializationByConstructor(DeclType,
192 MultiExprArg(*this,
193 (void **)&Init, 1),
194 InitLoc, Init->getSourceRange(),
195 InitEntity,
196 DirectInit? IK_Direct : IK_Copy,
197 ConstructorArgs);
Anders Carlsson2078bb92009-05-27 16:10:08 +0000198 if (!Constructor)
199 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000200
201 OwningExprResult InitResult =
Anders Carlssonec8e5ea2009-09-05 07:40:38 +0000202 BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000203 DeclType, Constructor,
Douglas Gregor39da0b82009-09-09 23:08:42 +0000204 move_arg(ConstructorArgs));
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000205 if (InitResult.isInvalid())
206 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000208 Init = InitResult.takeAs<Expr>();
Anders Carlsson2078bb92009-05-27 16:10:08 +0000209 return false;
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000212 // -- Otherwise (i.e., for the remaining copy-initialization
213 // cases), user-defined conversion sequences that can
214 // convert from the source type to the destination type or
215 // (when a conversion function is used) to a derived class
216 // thereof are enumerated as described in 13.3.1.4, and the
217 // best one is chosen through overload resolution
218 // (13.3). If the conversion cannot be done or is
219 // ambiguous, the initialization is ill-formed. The
220 // function selected is called with the initializer
221 // expression as its argument; if the function is a
222 // constructor, the call initializes a temporary of the
223 // destination type.
Mike Stump390b4cc2009-05-16 07:39:55 +0000224 // FIXME: We're pretending to do copy elision here; return to this when we
225 // have ASTs for such things.
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000226 if (!PerformImplicitConversion(Init, DeclType, "initializing"))
227 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000229 if (InitEntity)
230 return Diag(InitLoc, diag::err_cannot_initialize_decl)
Chris Lattnerb78d8332009-06-26 04:45:06 +0000231 << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
232 << Init->getType() << Init->getSourceRange();
233 return Diag(InitLoc, diag::err_cannot_initialize_decl_noname)
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000234 << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid)
235 << Init->getType() << Init->getSourceRange();
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000238 // C99 6.7.8p16.
239 if (DeclType->isArrayType())
240 return Diag(Init->getLocStart(), diag::err_array_init_list_required)
Chris Lattnerb78d8332009-06-26 04:45:06 +0000241 << Init->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattner95e8d652009-02-24 22:46:58 +0000243 return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
Mike Stump1eb44332009-09-09 15:08:12 +0000244 }
245
Chris Lattnerdd8e0062009-02-24 22:27:37 +0000246 bool hadError = CheckInitList(InitList, DeclType);
247 Init = InitList;
248 return hadError;
249}
250
251//===----------------------------------------------------------------------===//
252// Semantic checking for initializer lists.
253//===----------------------------------------------------------------------===//
254
Douglas Gregor9e80f722009-01-29 01:05:33 +0000255/// @brief Semantic checking for initializer lists.
256///
257/// The InitListChecker class contains a set of routines that each
258/// handle the initialization of a certain kind of entity, e.g.,
259/// arrays, vectors, struct/union types, scalars, etc. The
260/// InitListChecker itself performs a recursive walk of the subobject
261/// structure of the type to be initialized, while stepping through
262/// the initializer list one element at a time. The IList and Index
263/// parameters to each of the Check* routines contain the active
264/// (syntactic) initializer list and the index into that initializer
265/// list that represents the current initializer. Each routine is
266/// responsible for moving that Index forward as it consumes elements.
267///
268/// Each Check* routine also has a StructuredList/StructuredIndex
269/// arguments, which contains the current the "structured" (semantic)
270/// initializer list and the index into that initializer list where we
271/// are copying initializers as we map them over to the semantic
272/// list. Once we have completed our recursive walk of the subobject
273/// structure, we will have constructed a full semantic initializer
274/// list.
275///
276/// C99 designators cause changes in the initializer list traversal,
277/// because they make the initialization "jump" into a specific
278/// subobject and then continue the initialization from that
279/// point. CheckDesignatedInitializer() recursively steps into the
280/// designated subobject and manages backing out the recursion to
281/// initialize the subobjects after the one designated.
Chris Lattner8b419b92009-02-24 22:48:58 +0000282namespace {
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000283class InitListChecker {
Chris Lattner08202542009-02-24 22:50:46 +0000284 Sema &SemaRef;
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000285 bool hadError;
286 std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
287 InitListExpr *FullyStructuredList;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
289 void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000290 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000291 unsigned &StructuredIndex,
292 bool TopLevelObject = false);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000293 void CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000294 unsigned &Index, InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000295 unsigned &StructuredIndex,
296 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000297 void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
298 bool SubobjectIsDesignatorContext,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000299 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000300 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000301 unsigned &StructuredIndex,
302 bool TopLevelObject = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000303 void CheckSubElementType(InitListExpr *IList, QualType ElemType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000304 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000305 InitListExpr *StructuredList,
306 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000307 void CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000308 unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000309 InitListExpr *StructuredList,
310 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000311 void CheckReferenceType(InitListExpr *IList, QualType DeclType,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000312 unsigned &Index,
313 InitListExpr *StructuredList,
314 unsigned &StructuredIndex);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000315 void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000316 InitListExpr *StructuredList,
317 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000318 void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
319 RecordDecl::field_iterator Field,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000320 bool SubobjectIsDesignatorContext, 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 CheckArrayType(InitListExpr *IList, QualType &DeclType,
325 llvm::APSInt elementIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000326 bool SubobjectIsDesignatorContext, unsigned &Index,
Douglas Gregor9e80f722009-01-29 01:05:33 +0000327 InitListExpr *StructuredList,
328 unsigned &StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +0000329 bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +0000330 unsigned DesigIdx,
Mike Stump1eb44332009-09-09 15:08:12 +0000331 QualType &CurrentObjectType,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000332 RecordDecl::field_iterator *NextField,
333 llvm::APSInt *NextElementIndex,
334 unsigned &Index,
335 InitListExpr *StructuredList,
336 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000337 bool FinishSubobjectInit,
338 bool TopLevelObject);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000339 InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
340 QualType CurrentObjectType,
341 InitListExpr *StructuredList,
342 unsigned StructuredIndex,
343 SourceRange InitRange);
Douglas Gregor9e80f722009-01-29 01:05:33 +0000344 void UpdateStructuredListElement(InitListExpr *StructuredList,
345 unsigned &StructuredIndex,
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000346 Expr *expr);
347 int numArrayElements(QualType DeclType);
348 int numStructUnionElements(QualType DeclType);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000349
350 void FillInValueInitializations(InitListExpr *ILE);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000351public:
Chris Lattner08202542009-02-24 22:50:46 +0000352 InitListChecker(Sema &S, InitListExpr *IL, QualType &T);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +0000353 bool HadError() { return hadError; }
354
355 // @brief Retrieves the fully-structured initializer list used for
356 // semantic analysis and code generation.
357 InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
358};
Chris Lattner8b419b92009-02-24 22:48:58 +0000359} // end anonymous namespace
Chris Lattner68355a52009-01-29 05:10:57 +0000360
Douglas Gregor4c678342009-01-28 21:54:33 +0000361/// Recursively replaces NULL values within the given initializer list
362/// with expressions that perform value-initialization of the
363/// appropriate type.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000364void InitListChecker::FillInValueInitializations(InitListExpr *ILE) {
Mike Stump1eb44332009-09-09 15:08:12 +0000365 assert((ILE->getType() != SemaRef.Context.VoidTy) &&
Douglas Gregor930d8b52009-01-30 22:09:00 +0000366 "Should not have void type");
Douglas Gregor87fd7032009-02-02 17:43:21 +0000367 SourceLocation Loc = ILE->getSourceRange().getBegin();
368 if (ILE->getSyntacticForm())
369 Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek6217b802009-07-29 21:53:49 +0000371 if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000372 unsigned Init = 0, NumInits = ILE->getNumInits();
Mike Stump1eb44332009-09-09 15:08:12 +0000373 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000374 Field = RType->getDecl()->field_begin(),
375 FieldEnd = RType->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000376 Field != FieldEnd; ++Field) {
377 if (Field->isUnnamedBitfield())
378 continue;
379
Douglas Gregor87fd7032009-02-02 17:43:21 +0000380 if (Init >= NumInits || !ILE->getInit(Init)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000381 if (Field->getType()->isReferenceType()) {
382 // C++ [dcl.init.aggr]p9:
383 // If an incomplete or empty initializer-list leaves a
384 // member of reference type uninitialized, the program is
Mike Stump1eb44332009-09-09 15:08:12 +0000385 // ill-formed.
Chris Lattner08202542009-02-24 22:50:46 +0000386 SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000387 << Field->getType()
388 << ILE->getSyntacticForm()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000389 SemaRef.Diag(Field->getLocation(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000390 diag::note_uninit_reference_member);
391 hadError = true;
Douglas Gregor87fd7032009-02-02 17:43:21 +0000392 return;
Chris Lattner08202542009-02-24 22:50:46 +0000393 } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000394 hadError = true;
395 return;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000396 }
Douglas Gregor87fd7032009-02-02 17:43:21 +0000397
Mike Stump390b4cc2009-05-16 07:39:55 +0000398 // FIXME: If value-initialization involves calling a constructor, should
399 // we make that call explicit in the representation (even when it means
400 // extending the initializer list)?
Douglas Gregor87fd7032009-02-02 17:43:21 +0000401 if (Init < NumInits && !hadError)
Mike Stump1eb44332009-09-09 15:08:12 +0000402 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000403 new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000404 } else if (InitListExpr *InnerILE
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000405 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000406 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000407 ++Init;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000408
409 // Only look at the first initialization of a union.
410 if (RType->getDecl()->isUnion())
411 break;
Douglas Gregor4c678342009-01-28 21:54:33 +0000412 }
413
414 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000415 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000416
417 QualType ElementType;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor87fd7032009-02-02 17:43:21 +0000419 unsigned NumInits = ILE->getNumInits();
420 unsigned NumElements = NumInits;
Chris Lattner08202542009-02-24 22:50:46 +0000421 if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000422 ElementType = AType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000423 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
424 NumElements = CAType->getSize().getZExtValue();
John McCall183700f2009-09-21 23:43:11 +0000425 } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000426 ElementType = VType->getElementType();
Douglas Gregor87fd7032009-02-02 17:43:21 +0000427 NumElements = VType->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000428 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000429 ElementType = ILE->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregor87fd7032009-02-02 17:43:21 +0000431 for (unsigned Init = 0; Init != NumElements; ++Init) {
432 if (Init >= NumInits || !ILE->getInit(Init)) {
Chris Lattner08202542009-02-24 22:50:46 +0000433 if (SemaRef.CheckValueInitialization(ElementType, Loc)) {
Douglas Gregor87fd7032009-02-02 17:43:21 +0000434 hadError = true;
435 return;
436 }
437
Mike Stump390b4cc2009-05-16 07:39:55 +0000438 // FIXME: If value-initialization involves calling a constructor, should
439 // we make that call explicit in the representation (even when it means
440 // extending the initializer list)?
Douglas Gregor87fd7032009-02-02 17:43:21 +0000441 if (Init < NumInits && !hadError)
Mike Stump1eb44332009-09-09 15:08:12 +0000442 ILE->setInit(Init,
Chris Lattner08202542009-02-24 22:50:46 +0000443 new (SemaRef.Context) ImplicitValueInitExpr(ElementType));
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000444 } else if (InitListExpr *InnerILE
445 = dyn_cast<InitListExpr>(ILE->getInit(Init)))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000446 FillInValueInitializations(InnerILE);
Douglas Gregor4c678342009-01-28 21:54:33 +0000447 }
448}
449
Chris Lattner68355a52009-01-29 05:10:57 +0000450
Chris Lattner08202542009-02-24 22:50:46 +0000451InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T)
452 : SemaRef(S) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000453 hadError = false;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000454
Eli Friedmanb85f7072008-05-19 19:16:24 +0000455 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000456 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000457 FullyStructuredList
Douglas Gregored8a93d2009-03-01 17:12:46 +0000458 = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000459 CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
460 /*TopLevelObject=*/true);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000461
Douglas Gregor930d8b52009-01-30 22:09:00 +0000462 if (!hadError)
463 FillInValueInitializations(FullyStructuredList);
Steve Naroff0cca7492008-05-01 22:18:59 +0000464}
465
466int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +0000467 // FIXME: use a proper constant
468 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000469 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000470 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000471 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
472 }
473 return maxElements;
474}
475
476int InitListChecker::numStructUnionElements(QualType DeclType) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000477 RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
Douglas Gregor4c678342009-01-28 21:54:33 +0000478 int InitializableMembers = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000479 for (RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000480 Field = structDecl->field_begin(),
481 FieldEnd = structDecl->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +0000482 Field != FieldEnd; ++Field) {
483 if ((*Field)->getIdentifier() || !(*Field)->isBitField())
484 ++InitializableMembers;
485 }
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000486 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +0000487 return std::min(InitializableMembers, 1);
488 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-05-01 22:18:59 +0000489}
490
Mike Stump1eb44332009-09-09 15:08:12 +0000491void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
Douglas Gregor4c678342009-01-28 21:54:33 +0000492 QualType T, unsigned &Index,
493 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000494 unsigned &StructuredIndex,
495 bool TopLevelObject) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000496 int maxElements = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Steve Naroff0cca7492008-05-01 22:18:59 +0000498 if (T->isArrayType())
499 maxElements = numArrayElements(T);
500 else if (T->isStructureType() || T->isUnionType())
501 maxElements = numStructUnionElements(T);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000502 else if (T->isVectorType())
John McCall183700f2009-09-21 23:43:11 +0000503 maxElements = T->getAs<VectorType>()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +0000504 else
505 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +0000506
Eli Friedman402256f2008-05-25 13:49:22 +0000507 if (maxElements == 0) {
Chris Lattner08202542009-02-24 22:50:46 +0000508 SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
Eli Friedman402256f2008-05-25 13:49:22 +0000509 diag::err_implicit_empty_initializer);
Douglas Gregor4c678342009-01-28 21:54:33 +0000510 ++Index;
Eli Friedman402256f2008-05-25 13:49:22 +0000511 hadError = true;
512 return;
513 }
514
Douglas Gregor4c678342009-01-28 21:54:33 +0000515 // Build a structured initializer list corresponding to this subobject.
516 InitListExpr *StructuredSubobjectInitList
Mike Stump1eb44332009-09-09 15:08:12 +0000517 = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
518 StructuredIndex,
Douglas Gregored8a93d2009-03-01 17:12:46 +0000519 SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
520 ParentIList->getSourceRange().getEnd()));
Douglas Gregor4c678342009-01-28 21:54:33 +0000521 unsigned StructuredSubobjectInitIndex = 0;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000522
Douglas Gregor4c678342009-01-28 21:54:33 +0000523 // Check the element types and build the structural subobject.
Douglas Gregor87fd7032009-02-02 17:43:21 +0000524 unsigned StartIndex = Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000525 CheckListElementTypes(ParentIList, T, false, Index,
Mike Stump1eb44332009-09-09 15:08:12 +0000526 StructuredSubobjectInitList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000527 StructuredSubobjectInitIndex,
528 TopLevelObject);
Douglas Gregor87fd7032009-02-02 17:43:21 +0000529 unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
Douglas Gregora6457962009-03-20 00:32:56 +0000530 StructuredSubobjectInitList->setType(T);
531
Douglas Gregored8a93d2009-03-01 17:12:46 +0000532 // Update the structured sub-object initializer so that it's ending
Douglas Gregor87fd7032009-02-02 17:43:21 +0000533 // range corresponds with the end of the last initializer it used.
534 if (EndIndex < ParentIList->getNumInits()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000535 SourceLocation EndLoc
Douglas Gregor87fd7032009-02-02 17:43:21 +0000536 = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
537 StructuredSubobjectInitList->setRBraceLoc(EndLoc);
538 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000539}
540
Steve Naroffa647caa2008-05-06 00:23:44 +0000541void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Douglas Gregor4c678342009-01-28 21:54:33 +0000542 unsigned &Index,
543 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000544 unsigned &StructuredIndex,
545 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000546 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Douglas Gregor4c678342009-01-28 21:54:33 +0000547 SyntacticToSemantic[IList] = StructuredList;
548 StructuredList->setSyntacticForm(IList);
Mike Stump1eb44332009-09-09 15:08:12 +0000549 CheckListElementTypes(IList, T, true, Index, StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000550 StructuredIndex, TopLevelObject);
Steve Naroffa647caa2008-05-06 00:23:44 +0000551 IList->setType(T);
Douglas Gregor4c678342009-01-28 21:54:33 +0000552 StructuredList->setType(T);
Eli Friedman638e1442008-05-25 13:22:35 +0000553 if (hadError)
554 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000555
Eli Friedman638e1442008-05-25 13:22:35 +0000556 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000557 // We have leftover initializers
Eli Friedmane5408582009-05-29 20:20:05 +0000558 if (StructuredIndex == 1 &&
559 IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000560 unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000561 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000562 DK = diag::err_excess_initializers_in_char_array_initializer;
Eli Friedmane5408582009-05-29 20:20:05 +0000563 hadError = true;
564 }
Eli Friedmanbb504d32008-05-19 20:12:18 +0000565 // Special-case
Chris Lattner08202542009-02-24 22:50:46 +0000566 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000567 << IList->getInit(Index)->getSourceRange();
Eli Friedmand8dc2102008-05-20 05:25:56 +0000568 } else if (!T->isIncompleteType()) {
Douglas Gregorb574e562009-01-30 22:26:29 +0000569 // Don't complain for incomplete types, since we'll get an error
570 // elsewhere
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000571 QualType CurrentObjectType = StructuredList->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000572 int initKind =
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000573 CurrentObjectType->isArrayType()? 0 :
574 CurrentObjectType->isVectorType()? 1 :
575 CurrentObjectType->isScalarType()? 2 :
576 CurrentObjectType->isUnionType()? 3 :
577 4;
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000578
579 unsigned DK = diag::warn_excess_initializers;
Eli Friedmane5408582009-05-29 20:20:05 +0000580 if (SemaRef.getLangOptions().CPlusPlus) {
581 DK = diag::err_excess_initializers;
582 hadError = true;
583 }
Nate Begeman08634522009-07-07 21:53:06 +0000584 if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
585 DK = diag::err_excess_initializers;
586 hadError = true;
587 }
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000588
Chris Lattner08202542009-02-24 22:50:46 +0000589 SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000590 << initKind << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000591 }
592 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000593
Eli Friedman759f2522009-05-16 11:45:48 +0000594 if (T->isScalarType() && !TopLevelObject)
Chris Lattner08202542009-02-24 22:50:46 +0000595 SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
Douglas Gregora3a83512009-04-01 23:51:29 +0000596 << IList->getSourceRange()
597 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart()))
598 << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd()));
Steve Naroff0cca7492008-05-01 22:18:59 +0000599}
600
Eli Friedmanb85f7072008-05-19 19:16:24 +0000601void InitListChecker::CheckListElementTypes(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000602 QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000603 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000604 unsigned &Index,
605 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000606 unsigned &StructuredIndex,
607 bool TopLevelObject) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000608 if (DeclType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000609 CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000610 } else if (DeclType->isVectorType()) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000611 CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
Douglas Gregord7eb8462009-01-30 17:31:00 +0000612 } else if (DeclType->isAggregateType()) {
613 if (DeclType->isRecordType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000614 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000615 CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
Douglas Gregor4c678342009-01-28 21:54:33 +0000616 SubobjectIsDesignatorContext, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000617 StructuredList, StructuredIndex,
618 TopLevelObject);
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000619 } else if (DeclType->isArrayType()) {
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000620 llvm::APSInt Zero(
Chris Lattner08202542009-02-24 22:50:46 +0000621 SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000622 false);
Douglas Gregor4c678342009-01-28 21:54:33 +0000623 CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
624 StructuredList, StructuredIndex);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000625 } else
Douglas Gregor4c678342009-01-28 21:54:33 +0000626 assert(0 && "Aggregate that isn't a structure or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000627 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
628 // This type is invalid, issue a diagnostic.
Douglas Gregor4c678342009-01-28 21:54:33 +0000629 ++Index;
Chris Lattner08202542009-02-24 22:50:46 +0000630 SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000631 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000632 hadError = true;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000633 } else if (DeclType->isRecordType()) {
634 // C++ [dcl.init]p14:
635 // [...] If the class is an aggregate (8.5.1), and the initializer
636 // is a brace-enclosed list, see 8.5.1.
637 //
638 // Note: 8.5.1 is handled below; here, we diagnose the case where
639 // we have an initializer list and a destination type that is not
640 // an aggregate.
641 // FIXME: In C++0x, this is yet another form of initialization.
Chris Lattner08202542009-02-24 22:50:46 +0000642 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000643 << DeclType << IList->getSourceRange();
644 hadError = true;
645 } else if (DeclType->isReferenceType()) {
646 CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000647 } else {
648 // In C, all types are either scalars or aggregates, but
Mike Stump1eb44332009-09-09 15:08:12 +0000649 // additional handling is needed here for C++ (and possibly others?).
Steve Naroff0cca7492008-05-01 22:18:59 +0000650 assert(0 && "Unsupported initializer type");
651 }
652}
653
Eli Friedmanb85f7072008-05-19 19:16:24 +0000654void InitListChecker::CheckSubElementType(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +0000655 QualType ElemType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000656 unsigned &Index,
657 InitListExpr *StructuredList,
658 unsigned &StructuredIndex) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000659 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000660 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
661 unsigned newIndex = 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000662 unsigned newStructuredIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000663 InitListExpr *newStructuredList
Douglas Gregor4c678342009-01-28 21:54:33 +0000664 = getStructuredSubobjectInit(IList, Index, ElemType,
665 StructuredList, StructuredIndex,
666 SubInitList->getSourceRange());
Mike Stump1eb44332009-09-09 15:08:12 +0000667 CheckExplicitInitList(SubInitList, ElemType, newIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +0000668 newStructuredList, newStructuredIndex);
669 ++StructuredIndex;
670 ++Index;
Chris Lattner79e079d2009-02-24 23:10:27 +0000671 } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
672 CheckStringInit(Str, ElemType, SemaRef);
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000673 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Douglas Gregor4c678342009-01-28 21:54:33 +0000674 ++Index;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000675 } else if (ElemType->isScalarType()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000676 CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000677 } else if (ElemType->isReferenceType()) {
678 CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000679 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000680 if (SemaRef.getLangOptions().CPlusPlus) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000681 // C++ [dcl.init.aggr]p12:
682 // All implicit type conversions (clause 4) are considered when
683 // initializing the aggregate member with an ini- tializer from
684 // an initializer-list. If the initializer can initialize a
685 // member, the member is initialized. [...]
Mike Stump1eb44332009-09-09 15:08:12 +0000686 ImplicitConversionSequence ICS
Anders Carlssond28b4282009-08-27 17:18:13 +0000687 = SemaRef.TryCopyInitialization(expr, ElemType,
688 /*SuppressUserConversions=*/false,
Anders Carlsson7b361b52009-08-27 17:37:39 +0000689 /*ForceRValue=*/false,
690 /*InOverloadResolution=*/false);
Anders Carlssond28b4282009-08-27 17:18:13 +0000691
Douglas Gregor930d8b52009-01-30 22:09:00 +0000692 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
Mike Stump1eb44332009-09-09 15:08:12 +0000693 if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000694 "initializing"))
695 hadError = true;
696 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
697 ++Index;
698 return;
699 }
700
701 // Fall through for subaggregate initialization
702 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000703 // C99 6.7.8p13:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000704 //
705 // The initializer for a structure or union object that has
706 // automatic storage duration shall be either an initializer
707 // list as described below, or a single expression that has
708 // compatible structure or union type. In the latter case, the
709 // initial value of the object, including unnamed members, is
710 // that of the expression.
Eli Friedman6b5374f2009-06-13 10:38:46 +0000711 if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
Eli Friedman8718a6a2009-05-29 18:22:49 +0000712 SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000713 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
714 ++Index;
715 return;
716 }
717
718 // Fall through for subaggregate initialization
719 }
720
721 // C++ [dcl.init.aggr]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000722 //
Douglas Gregor930d8b52009-01-30 22:09:00 +0000723 // [...] Otherwise, if the member is itself a non-empty
724 // subaggregate, brace elision is assumed and the initializer is
725 // considered for the initialization of the first member of
726 // the subaggregate.
727 if (ElemType->isAggregateType() || ElemType->isVectorType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000728 CheckImplicitInitList(IList, ElemType, Index, StructuredList,
Douglas Gregor930d8b52009-01-30 22:09:00 +0000729 StructuredIndex);
730 ++StructuredIndex;
731 } else {
732 // We cannot initialize this element, so let
733 // PerformCopyInitialization produce the appropriate diagnostic.
Chris Lattner08202542009-02-24 22:50:46 +0000734 SemaRef.PerformCopyInitialization(expr, ElemType, "initializing");
Douglas Gregor930d8b52009-01-30 22:09:00 +0000735 hadError = true;
736 ++Index;
737 ++StructuredIndex;
738 }
739 }
Eli Friedmanb85f7072008-05-19 19:16:24 +0000740}
741
Douglas Gregor930d8b52009-01-30 22:09:00 +0000742void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000743 unsigned &Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000744 InitListExpr *StructuredList,
745 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000746 if (Index < IList->getNumInits()) {
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000747 Expr *expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000748 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000749 SemaRef.Diag(IList->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000750 diag::err_many_braces_around_scalar_init)
751 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000752 hadError = true;
753 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000754 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000755 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000756 } else if (isa<DesignatedInitExpr>(expr)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000757 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor05c13a32009-01-22 00:58:24 +0000758 diag::err_designator_for_scalar_init)
759 << DeclType << expr->getSourceRange();
760 hadError = true;
761 ++Index;
Douglas Gregor4c678342009-01-28 21:54:33 +0000762 ++StructuredIndex;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000763 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000764 }
Douglas Gregor05c13a32009-01-22 00:58:24 +0000765
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000766 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Chris Lattner08202542009-02-24 22:50:46 +0000767 if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
Eli Friedmanbb504d32008-05-19 20:12:18 +0000768 hadError = true; // types weren't compatible.
Douglas Gregor05c13a32009-01-22 00:58:24 +0000769 else if (savExpr != expr) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000770 // The type was promoted, update initializer list.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000771 IList->setInit(Index, expr);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000772 }
Douglas Gregor4c678342009-01-28 21:54:33 +0000773 if (hadError)
774 ++StructuredIndex;
775 else
776 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
Steve Naroff0cca7492008-05-01 22:18:59 +0000777 ++Index;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000778 } else {
Chris Lattner08202542009-02-24 22:50:46 +0000779 SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000780 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000781 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000782 ++Index;
783 ++StructuredIndex;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000784 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000785 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000786}
787
Douglas Gregor930d8b52009-01-30 22:09:00 +0000788void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
789 unsigned &Index,
790 InitListExpr *StructuredList,
791 unsigned &StructuredIndex) {
792 if (Index < IList->getNumInits()) {
793 Expr *expr = IList->getInit(Index);
794 if (isa<InitListExpr>(expr)) {
Chris Lattner08202542009-02-24 22:50:46 +0000795 SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
Douglas Gregor930d8b52009-01-30 22:09:00 +0000796 << DeclType << IList->getSourceRange();
797 hadError = true;
798 ++Index;
799 ++StructuredIndex;
800 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000801 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000802
803 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000804 if (SemaRef.CheckReferenceInit(expr, DeclType,
Douglas Gregor739d8282009-09-23 23:04:10 +0000805 /*FIXME:*/expr->getLocStart(),
Anders Carlsson2de3ace2009-08-27 17:30:43 +0000806 /*SuppressUserConversions=*/false,
807 /*AllowExplicit=*/false,
Mike Stump1eb44332009-09-09 15:08:12 +0000808 /*ForceRValue=*/false))
Douglas Gregor930d8b52009-01-30 22:09:00 +0000809 hadError = true;
810 else if (savExpr != expr) {
811 // The type was promoted, update initializer list.
812 IList->setInit(Index, expr);
813 }
814 if (hadError)
815 ++StructuredIndex;
816 else
817 UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
818 ++Index;
819 } else {
Mike Stump390b4cc2009-05-16 07:39:55 +0000820 // FIXME: It would be wonderful if we could point at the actual member. In
821 // general, it would be useful to pass location information down the stack,
822 // so that we know the location (or decl) of the "current object" being
823 // initialized.
Mike Stump1eb44332009-09-09 15:08:12 +0000824 SemaRef.Diag(IList->getLocStart(),
Douglas Gregor930d8b52009-01-30 22:09:00 +0000825 diag::err_init_reference_member_uninitialized)
826 << DeclType
827 << IList->getSourceRange();
828 hadError = true;
829 ++Index;
830 ++StructuredIndex;
831 return;
832 }
833}
834
Mike Stump1eb44332009-09-09 15:08:12 +0000835void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
Douglas Gregor4c678342009-01-28 21:54:33 +0000836 unsigned &Index,
837 InitListExpr *StructuredList,
838 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000839 if (Index < IList->getNumInits()) {
John McCall183700f2009-09-21 23:43:11 +0000840 const VectorType *VT = DeclType->getAs<VectorType>();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000841 unsigned maxElements = VT->getNumElements();
842 unsigned numEltsInit = 0;
Steve Naroff0cca7492008-05-01 22:18:59 +0000843 QualType elementType = VT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Nate Begeman2ef13e52009-08-10 23:49:36 +0000845 if (!SemaRef.getLangOptions().OpenCL) {
846 for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
847 // Don't attempt to go past the end of the init list
848 if (Index >= IList->getNumInits())
849 break;
850 CheckSubElementType(IList, elementType, Index,
851 StructuredList, StructuredIndex);
852 }
853 } else {
854 // OpenCL initializers allows vectors to be constructed from vectors.
855 for (unsigned i = 0; i < maxElements; ++i) {
856 // Don't attempt to go past the end of the init list
857 if (Index >= IList->getNumInits())
858 break;
859 QualType IType = IList->getInit(Index)->getType();
860 if (!IType->isVectorType()) {
861 CheckSubElementType(IList, elementType, Index,
862 StructuredList, StructuredIndex);
863 ++numEltsInit;
864 } else {
John McCall183700f2009-09-21 23:43:11 +0000865 const VectorType *IVT = IType->getAs<VectorType>();
Nate Begeman2ef13e52009-08-10 23:49:36 +0000866 unsigned numIElts = IVT->getNumElements();
867 QualType VecType = SemaRef.Context.getExtVectorType(elementType,
868 numIElts);
869 CheckSubElementType(IList, VecType, Index,
870 StructuredList, StructuredIndex);
871 numEltsInit += numIElts;
872 }
873 }
Steve Naroff0cca7492008-05-01 22:18:59 +0000874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Nate Begeman2ef13e52009-08-10 23:49:36 +0000876 // OpenCL & AltiVec require all elements to be initialized.
877 if (numEltsInit != maxElements)
878 if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
879 SemaRef.Diag(IList->getSourceRange().getBegin(),
880 diag::err_vector_incorrect_num_initializers)
881 << (numEltsInit < maxElements) << maxElements << numEltsInit;
Steve Naroff0cca7492008-05-01 22:18:59 +0000882 }
883}
884
Mike Stump1eb44332009-09-09 15:08:12 +0000885void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000886 llvm::APSInt elementIndex,
Mike Stump1eb44332009-09-09 15:08:12 +0000887 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +0000888 unsigned &Index,
889 InitListExpr *StructuredList,
890 unsigned &StructuredIndex) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000891 // Check for the special-case of initializing an array with a string.
892 if (Index < IList->getNumInits()) {
Chris Lattner79e079d2009-02-24 23:10:27 +0000893 if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
894 SemaRef.Context)) {
895 CheckStringInit(Str, DeclType, SemaRef);
Douglas Gregor4c678342009-01-28 21:54:33 +0000896 // We place the string literal directly into the resulting
897 // initializer list. This is the only place where the structure
898 // of the structured initializer list doesn't match exactly,
899 // because doing so would involve allocating one character
900 // constant for each string.
Chris Lattnerf71ae8d2009-02-24 22:41:04 +0000901 UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
Chris Lattner08202542009-02-24 22:50:46 +0000902 StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +0000903 ++Index;
Steve Naroff0cca7492008-05-01 22:18:59 +0000904 return;
905 }
906 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000907 if (const VariableArrayType *VAT =
Chris Lattner08202542009-02-24 22:50:46 +0000908 SemaRef.Context.getAsVariableArrayType(DeclType)) {
Eli Friedman638e1442008-05-25 13:22:35 +0000909 // Check for VLAs; in standard C it would be possible to check this
910 // earlier, but I don't know where clang accepts VLAs (gcc accepts
911 // them in all sorts of strange places).
Chris Lattner08202542009-02-24 22:50:46 +0000912 SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000913 diag::err_variable_object_no_init)
914 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000915 hadError = true;
Douglas Gregor4c678342009-01-28 21:54:33 +0000916 ++Index;
917 ++StructuredIndex;
Eli Friedman638e1442008-05-25 13:22:35 +0000918 return;
919 }
920
Douglas Gregor05c13a32009-01-22 00:58:24 +0000921 // We might know the maximum number of elements in advance.
Douglas Gregor4c678342009-01-28 21:54:33 +0000922 llvm::APSInt maxElements(elementIndex.getBitWidth(),
923 elementIndex.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000924 bool maxElementsKnown = false;
925 if (const ConstantArrayType *CAT =
Chris Lattner08202542009-02-24 22:50:46 +0000926 SemaRef.Context.getAsConstantArrayType(DeclType)) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000927 maxElements = CAT->getSize();
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000928 elementIndex.extOrTrunc(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000929 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000930 maxElementsKnown = true;
931 }
932
Chris Lattner08202542009-02-24 22:50:46 +0000933 QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000934 ->getElementType();
Douglas Gregor05c13a32009-01-22 00:58:24 +0000935 while (Index < IList->getNumInits()) {
936 Expr *Init = IList->getInit(Index);
937 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000938 // If we're not the subobject that matches up with the '{' for
939 // the designator, we shouldn't be handling the
940 // designator. Return immediately.
941 if (!SubobjectIsDesignatorContext)
942 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000943
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000944 // Handle this designated initializer. elementIndex will be
945 // updated to be the next array element we'll initialize.
Mike Stump1eb44332009-09-09 15:08:12 +0000946 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +0000947 DeclType, 0, &elementIndex, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000948 StructuredList, StructuredIndex, true,
949 false)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000950 hadError = true;
951 continue;
952 }
953
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000954 if (elementIndex.getBitWidth() > maxElements.getBitWidth())
955 maxElements.extend(elementIndex.getBitWidth());
956 else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
957 elementIndex.extend(maxElements.getBitWidth());
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000958 elementIndex.setIsUnsigned(maxElements.isUnsigned());
Douglas Gregorf6c717c2009-01-23 16:54:12 +0000959
Douglas Gregor87f55cf2009-01-22 23:26:18 +0000960 // If the array is of incomplete type, keep track of the number of
961 // elements in the initializer.
962 if (!maxElementsKnown && elementIndex > maxElements)
963 maxElements = elementIndex;
964
Douglas Gregor05c13a32009-01-22 00:58:24 +0000965 continue;
966 }
967
968 // If we know the maximum number of elements, and we've already
969 // hit it, stop consuming elements in the initializer list.
970 if (maxElementsKnown && elementIndex == maxElements)
Steve Naroff0cca7492008-05-01 22:18:59 +0000971 break;
Douglas Gregor05c13a32009-01-22 00:58:24 +0000972
973 // Check this element.
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +0000974 CheckSubElementType(IList, elementType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +0000975 StructuredList, StructuredIndex);
Douglas Gregor05c13a32009-01-22 00:58:24 +0000976 ++elementIndex;
977
978 // If the array is of incomplete type, keep track of the number of
979 // elements in the initializer.
980 if (!maxElementsKnown && elementIndex > maxElements)
981 maxElements = elementIndex;
Steve Naroff0cca7492008-05-01 22:18:59 +0000982 }
Eli Friedman587cbdf2009-05-29 20:17:55 +0000983 if (!hadError && DeclType->isIncompleteArrayType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000984 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000985 // be calculated here.
Douglas Gregore3fa2de2009-01-23 18:58:42 +0000986 llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000987 if (maxElements == Zero) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000988 // Sizing an array implicitly to zero is not allowed by ISO C,
989 // but is supported by GNU.
Chris Lattner08202542009-02-24 22:50:46 +0000990 SemaRef.Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000991 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +0000992 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000993
Mike Stump1eb44332009-09-09 15:08:12 +0000994 DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000995 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +0000996 }
997}
998
Mike Stump1eb44332009-09-09 15:08:12 +0000999void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
1000 QualType DeclType,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001001 RecordDecl::field_iterator Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001002 bool SubobjectIsDesignatorContext,
Douglas Gregor4c678342009-01-28 21:54:33 +00001003 unsigned &Index,
1004 InitListExpr *StructuredList,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001005 unsigned &StructuredIndex,
1006 bool TopLevelObject) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001007 RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Eli Friedmanb85f7072008-05-19 19:16:24 +00001009 // If the record is invalid, some of it's members are invalid. To avoid
1010 // confusion, we forgo checking the intializer for the entire record.
1011 if (structDecl->isInvalidDecl()) {
1012 hadError = true;
1013 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001014 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001015
1016 if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1017 // Value-initialize the first named member of the union.
Ted Kremenek6217b802009-07-29 21:53:49 +00001018 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001019 for (RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001020 Field != FieldEnd; ++Field) {
1021 if (Field->getDeclName()) {
1022 StructuredList->setInitializedFieldInUnion(*Field);
1023 break;
1024 }
1025 }
1026 return;
1027 }
1028
Douglas Gregor05c13a32009-01-22 00:58:24 +00001029 // If structDecl is a forward declaration, this loop won't do
1030 // anything except look at designated initializers; That's okay,
1031 // because an error should get printed out elsewhere. It might be
1032 // worthwhile to skip over the rest of the initializer, though.
Ted Kremenek6217b802009-07-29 21:53:49 +00001033 RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 RecordDecl::field_iterator FieldEnd = RD->field_end();
Douglas Gregordfb5e592009-02-12 19:00:39 +00001035 bool InitializedSomething = false;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001036 while (Index < IList->getNumInits()) {
1037 Expr *Init = IList->getInit(Index);
1038
1039 if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001040 // If we're not the subobject that matches up with the '{' for
1041 // the designator, we shouldn't be handling the
1042 // designator. Return immediately.
1043 if (!SubobjectIsDesignatorContext)
1044 return;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001045
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001046 // Handle this designated initializer. Field will be updated to
1047 // the next field that we'll be initializing.
Mike Stump1eb44332009-09-09 15:08:12 +00001048 if (CheckDesignatedInitializer(IList, DIE, 0,
Douglas Gregor4c678342009-01-28 21:54:33 +00001049 DeclType, &Field, 0, Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001050 StructuredList, StructuredIndex,
1051 true, TopLevelObject))
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001052 hadError = true;
1053
Douglas Gregordfb5e592009-02-12 19:00:39 +00001054 InitializedSomething = true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001055 continue;
1056 }
1057
1058 if (Field == FieldEnd) {
1059 // We've run out of fields. We're done.
1060 break;
1061 }
1062
Douglas Gregordfb5e592009-02-12 19:00:39 +00001063 // We've already initialized a member of a union. We're done.
1064 if (InitializedSomething && DeclType->isUnionType())
1065 break;
1066
Douglas Gregor44b43212008-12-11 16:49:14 +00001067 // If we've hit the flexible array member at the end, we're done.
1068 if (Field->getType()->isIncompleteArrayType())
1069 break;
1070
Douglas Gregor0bb76892009-01-29 16:53:55 +00001071 if (Field->isUnnamedBitfield()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001072 // Don't initialize unnamed bitfields, e.g. "int : 20;"
Douglas Gregor05c13a32009-01-22 00:58:24 +00001073 ++Field;
Eli Friedmanb85f7072008-05-19 19:16:24 +00001074 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +00001075 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001076
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001077 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001078 StructuredList, StructuredIndex);
Douglas Gregordfb5e592009-02-12 19:00:39 +00001079 InitializedSomething = true;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001080
1081 if (DeclType->isUnionType()) {
1082 // Initialize the first field within the union.
1083 StructuredList->setInitializedFieldInUnion(*Field);
Douglas Gregor0bb76892009-01-29 16:53:55 +00001084 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001085
1086 ++Field;
Steve Naroff0cca7492008-05-01 22:18:59 +00001087 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001088
Mike Stump1eb44332009-09-09 15:08:12 +00001089 if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
Douglas Gregora6457962009-03-20 00:32:56 +00001090 Index >= IList->getNumInits())
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001091 return;
1092
1093 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001094 if (!TopLevelObject &&
Douglas Gregora6457962009-03-20 00:32:56 +00001095 (!isa<InitListExpr>(IList->getInit(Index)) ||
1096 cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001097 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001098 diag::err_flexible_array_init_nonempty)
1099 << IList->getInit(Index)->getSourceRange().getBegin();
Chris Lattner08202542009-02-24 22:50:46 +00001100 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001101 << *Field;
1102 hadError = true;
Douglas Gregora6457962009-03-20 00:32:56 +00001103 ++Index;
1104 return;
1105 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001106 SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
Douglas Gregora6457962009-03-20 00:32:56 +00001107 diag::ext_flexible_array_init)
1108 << IList->getInit(Index)->getSourceRange().getBegin();
1109 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1110 << *Field;
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001111 }
1112
Douglas Gregora6457962009-03-20 00:32:56 +00001113 if (isa<InitListExpr>(IList->getInit(Index)))
1114 CheckSubElementType(IList, Field->getType(), Index, StructuredList,
1115 StructuredIndex);
1116 else
1117 CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
1118 StructuredIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +00001119}
Steve Naroff0cca7492008-05-01 22:18:59 +00001120
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001121/// \brief Expand a field designator that refers to a member of an
1122/// anonymous struct or union into a series of field designators that
1123/// refers to the field within the appropriate subobject.
1124///
1125/// Field/FieldIndex will be updated to point to the (new)
1126/// currently-designated field.
1127static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
Mike Stump1eb44332009-09-09 15:08:12 +00001128 DesignatedInitExpr *DIE,
1129 unsigned DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001130 FieldDecl *Field,
1131 RecordDecl::field_iterator &FieldIter,
1132 unsigned &FieldIndex) {
1133 typedef DesignatedInitExpr::Designator Designator;
1134
1135 // Build the path from the current object to the member of the
1136 // anonymous struct/union (backwards).
1137 llvm::SmallVector<FieldDecl *, 4> Path;
1138 SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001140 // Build the replacement designators.
1141 llvm::SmallVector<Designator, 4> Replacements;
1142 for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1143 FI = Path.rbegin(), FIEnd = Path.rend();
1144 FI != FIEnd; ++FI) {
1145 if (FI + 1 == FIEnd)
Mike Stump1eb44332009-09-09 15:08:12 +00001146 Replacements.push_back(Designator((IdentifierInfo *)0,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001147 DIE->getDesignator(DesigIdx)->getDotLoc(),
1148 DIE->getDesignator(DesigIdx)->getFieldLoc()));
1149 else
1150 Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1151 SourceLocation()));
1152 Replacements.back().setField(*FI);
1153 }
1154
1155 // Expand the current designator into the set of replacement
1156 // designators, so we have a full subobject path down to where the
1157 // member of the anonymous struct/union is actually stored.
Mike Stump1eb44332009-09-09 15:08:12 +00001158 DIE->ExpandDesignator(DesigIdx, &Replacements[0],
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001159 &Replacements[0] + Replacements.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001161 // Update FieldIter/FieldIndex;
1162 RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001163 FieldIter = Record->field_begin();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001164 FieldIndex = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001165 for (RecordDecl::field_iterator FEnd = Record->field_end();
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001166 FieldIter != FEnd; ++FieldIter) {
1167 if (FieldIter->isUnnamedBitfield())
1168 continue;
1169
1170 if (*FieldIter == Path.back())
1171 return;
1172
1173 ++FieldIndex;
1174 }
1175
1176 assert(false && "Unable to find anonymous struct/union field");
1177}
1178
Douglas Gregor05c13a32009-01-22 00:58:24 +00001179/// @brief Check the well-formedness of a C99 designated initializer.
1180///
1181/// Determines whether the designated initializer @p DIE, which
1182/// resides at the given @p Index within the initializer list @p
1183/// IList, is well-formed for a current object of type @p DeclType
1184/// (C99 6.7.8). The actual subobject that this designator refers to
Mike Stump1eb44332009-09-09 15:08:12 +00001185/// within the current subobject is returned in either
Douglas Gregor4c678342009-01-28 21:54:33 +00001186/// @p NextField or @p NextElementIndex (whichever is appropriate).
Douglas Gregor05c13a32009-01-22 00:58:24 +00001187///
1188/// @param IList The initializer list in which this designated
1189/// initializer occurs.
1190///
Douglas Gregor71199712009-04-15 04:56:10 +00001191/// @param DIE The designated initializer expression.
1192///
1193/// @param DesigIdx The index of the current designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001194///
1195/// @param DeclType The type of the "current object" (C99 6.7.8p17),
1196/// into which the designation in @p DIE should refer.
1197///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001198/// @param NextField If non-NULL and the first designator in @p DIE is
1199/// a field, this will be set to the field declaration corresponding
1200/// to the field named by the designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001201///
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001202/// @param NextElementIndex If non-NULL and the first designator in @p
1203/// DIE is an array designator or GNU array-range designator, this
1204/// will be set to the last index initialized by this designator.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001205///
1206/// @param Index Index into @p IList where the designated initializer
1207/// @p DIE occurs.
1208///
Douglas Gregor4c678342009-01-28 21:54:33 +00001209/// @param StructuredList The initializer list expression that
1210/// describes all of the subobject initializers in the order they'll
1211/// actually be initialized.
1212///
Douglas Gregor05c13a32009-01-22 00:58:24 +00001213/// @returns true if there was an error, false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00001214bool
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001215InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
Mike Stump1eb44332009-09-09 15:08:12 +00001216 DesignatedInitExpr *DIE,
Douglas Gregor71199712009-04-15 04:56:10 +00001217 unsigned DesigIdx,
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001218 QualType &CurrentObjectType,
1219 RecordDecl::field_iterator *NextField,
1220 llvm::APSInt *NextElementIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001221 unsigned &Index,
1222 InitListExpr *StructuredList,
Douglas Gregor34e79462009-01-28 23:36:17 +00001223 unsigned &StructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001224 bool FinishSubobjectInit,
1225 bool TopLevelObject) {
Douglas Gregor71199712009-04-15 04:56:10 +00001226 if (DesigIdx == DIE->size()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001227 // Check the actual initialization for the designated object type.
1228 bool prevHadError = hadError;
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001229
1230 // Temporarily remove the designator expression from the
1231 // initializer list that the child calls see, so that we don't try
1232 // to re-process the designator.
1233 unsigned OldIndex = Index;
1234 IList->setInit(OldIndex, DIE->getInit());
1235
1236 CheckSubElementType(IList, CurrentObjectType, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001237 StructuredList, StructuredIndex);
Douglas Gregor6fbdc6b2009-01-29 00:39:20 +00001238
1239 // Restore the designated initializer expression in the syntactic
1240 // form of the initializer list.
1241 if (IList->getInit(OldIndex) != DIE->getInit())
1242 DIE->setInit(IList->getInit(OldIndex));
1243 IList->setInit(OldIndex, DIE);
1244
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001245 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001246 }
1247
Douglas Gregor71199712009-04-15 04:56:10 +00001248 bool IsFirstDesignator = (DesigIdx == 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001249 assert((IsFirstDesignator || StructuredList) &&
Douglas Gregor4c678342009-01-28 21:54:33 +00001250 "Need a non-designated initializer list to start from");
1251
Douglas Gregor71199712009-04-15 04:56:10 +00001252 DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001253 // Determine the structural initializer list that corresponds to the
1254 // current subobject.
1255 StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
Mike Stump1eb44332009-09-09 15:08:12 +00001256 : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001257 StructuredList, StructuredIndex,
Douglas Gregor4c678342009-01-28 21:54:33 +00001258 SourceRange(D->getStartLocation(),
1259 DIE->getSourceRange().getEnd()));
1260 assert(StructuredList && "Expected a structured initializer list");
1261
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001262 if (D->isFieldDesignator()) {
1263 // C99 6.7.8p7:
1264 //
1265 // If a designator has the form
1266 //
1267 // . identifier
1268 //
1269 // then the current object (defined below) shall have
1270 // structure or union type and the identifier shall be the
Mike Stump1eb44332009-09-09 15:08:12 +00001271 // name of a member of that type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001272 const RecordType *RT = CurrentObjectType->getAs<RecordType>();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001273 if (!RT) {
1274 SourceLocation Loc = D->getDotLoc();
1275 if (Loc.isInvalid())
1276 Loc = D->getFieldLoc();
Chris Lattner08202542009-02-24 22:50:46 +00001277 SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1278 << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001279 ++Index;
1280 return true;
1281 }
1282
Douglas Gregor4c678342009-01-28 21:54:33 +00001283 // Note: we perform a linear search of the fields here, despite
1284 // the fact that we have a faster lookup method, because we always
1285 // need to compute the field's index.
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001286 FieldDecl *KnownField = D->getField();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001287 IdentifierInfo *FieldName = D->getFieldName();
Douglas Gregor4c678342009-01-28 21:54:33 +00001288 unsigned FieldIndex = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001289 RecordDecl::field_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001290 Field = RT->getDecl()->field_begin(),
1291 FieldEnd = RT->getDecl()->field_end();
Douglas Gregor4c678342009-01-28 21:54:33 +00001292 for (; Field != FieldEnd; ++Field) {
1293 if (Field->isUnnamedBitfield())
1294 continue;
1295
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001296 if (KnownField == *Field || Field->getIdentifier() == FieldName)
Douglas Gregor4c678342009-01-28 21:54:33 +00001297 break;
1298
1299 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001300 }
1301
Douglas Gregor4c678342009-01-28 21:54:33 +00001302 if (Field == FieldEnd) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001303 // There was no normal field in the struct with the designated
1304 // name. Perform another lookup for this name, which may find
1305 // something that we can't designate (e.g., a member function),
1306 // may find nothing, or may find a member of an anonymous
Mike Stump1eb44332009-09-09 15:08:12 +00001307 // struct/union.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001308 DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
Douglas Gregor4c678342009-01-28 21:54:33 +00001309 if (Lookup.first == Lookup.second) {
1310 // Name lookup didn't find anything.
Chris Lattner08202542009-02-24 22:50:46 +00001311 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
Douglas Gregor4c678342009-01-28 21:54:33 +00001312 << FieldName << CurrentObjectType;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001313 ++Index;
1314 return true;
1315 } else if (!KnownField && isa<FieldDecl>(*Lookup.first) &&
1316 cast<RecordDecl>((*Lookup.first)->getDeclContext())
1317 ->isAnonymousStructOrUnion()) {
1318 // Handle an field designator that refers to a member of an
1319 // anonymous struct or union.
Mike Stump1eb44332009-09-09 15:08:12 +00001320 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001321 cast<FieldDecl>(*Lookup.first),
1322 Field, FieldIndex);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001323 D = DIE->getDesignator(DesigIdx);
Douglas Gregor4c678342009-01-28 21:54:33 +00001324 } else {
1325 // Name lookup found something, but it wasn't a field.
Chris Lattner08202542009-02-24 22:50:46 +00001326 SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
Douglas Gregor4c678342009-01-28 21:54:33 +00001327 << FieldName;
Mike Stump1eb44332009-09-09 15:08:12 +00001328 SemaRef.Diag((*Lookup.first)->getLocation(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001329 diag::note_field_designator_found);
Eli Friedmanba79fc22009-04-16 17:49:48 +00001330 ++Index;
1331 return true;
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001332 }
1333 } else if (!KnownField &&
1334 cast<RecordDecl>((*Field)->getDeclContext())
Douglas Gregor4c678342009-01-28 21:54:33 +00001335 ->isAnonymousStructOrUnion()) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001336 ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1337 Field, FieldIndex);
1338 D = DIE->getDesignator(DesigIdx);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001339 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001340
1341 // All of the fields of a union are located at the same place in
1342 // the initializer list.
Douglas Gregor0bb76892009-01-29 16:53:55 +00001343 if (RT->getDecl()->isUnion()) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001344 FieldIndex = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +00001345 StructuredList->setInitializedFieldInUnion(*Field);
1346 }
Douglas Gregor4c678342009-01-28 21:54:33 +00001347
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001348 // Update the designator with the field declaration.
Douglas Gregor4c678342009-01-28 21:54:33 +00001349 D->setField(*Field);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Douglas Gregor4c678342009-01-28 21:54:33 +00001351 // Make sure that our non-designated initializer list has space
1352 // for a subobject corresponding to this field.
1353 if (FieldIndex >= StructuredList->getNumInits())
Chris Lattner08202542009-02-24 22:50:46 +00001354 StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001355
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001356 // This designator names a flexible array member.
1357 if (Field->getType()->isIncompleteArrayType()) {
1358 bool Invalid = false;
Douglas Gregor71199712009-04-15 04:56:10 +00001359 if ((DesigIdx + 1) != DIE->size()) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001360 // We can't designate an object within the flexible array
1361 // member (because GCC doesn't allow it).
Mike Stump1eb44332009-09-09 15:08:12 +00001362 DesignatedInitExpr::Designator *NextD
Douglas Gregor71199712009-04-15 04:56:10 +00001363 = DIE->getDesignator(DesigIdx + 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001364 SemaRef.Diag(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001365 diag::err_designator_into_flexible_array_member)
Mike Stump1eb44332009-09-09 15:08:12 +00001366 << SourceRange(NextD->getStartLocation(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001367 DIE->getSourceRange().getEnd());
Chris Lattner08202542009-02-24 22:50:46 +00001368 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001369 << *Field;
1370 Invalid = true;
1371 }
1372
1373 if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1374 // The initializer is not an initializer list.
Chris Lattner08202542009-02-24 22:50:46 +00001375 SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001376 diag::err_flexible_array_init_needs_braces)
1377 << DIE->getInit()->getSourceRange();
Chris Lattner08202542009-02-24 22:50:46 +00001378 SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001379 << *Field;
1380 Invalid = true;
1381 }
1382
1383 // Handle GNU flexible array initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001384 if (!Invalid && !TopLevelObject &&
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001385 cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +00001386 SemaRef.Diag(DIE->getSourceRange().getBegin(),
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001387 diag::err_flexible_array_init_nonempty)
1388 << DIE->getSourceRange().getBegin();
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 (Invalid) {
1395 ++Index;
1396 return true;
1397 }
1398
1399 // Initialize the array.
1400 bool prevHadError = hadError;
1401 unsigned newStructuredIndex = FieldIndex;
1402 unsigned OldIndex = Index;
1403 IList->setInit(Index, DIE->getInit());
Mike Stump1eb44332009-09-09 15:08:12 +00001404 CheckSubElementType(IList, Field->getType(), Index,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001405 StructuredList, newStructuredIndex);
1406 IList->setInit(OldIndex, DIE);
1407 if (hadError && !prevHadError) {
1408 ++Field;
1409 ++FieldIndex;
1410 if (NextField)
1411 *NextField = Field;
1412 StructuredIndex = FieldIndex;
1413 return true;
1414 }
1415 } else {
1416 // Recurse to check later designated subobjects.
1417 QualType FieldType = (*Field)->getType();
1418 unsigned newStructuredIndex = FieldIndex;
Douglas Gregor71199712009-04-15 04:56:10 +00001419 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
1420 Index, StructuredList, newStructuredIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001421 true, false))
1422 return true;
1423 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001424
1425 // Find the position of the next field to be initialized in this
1426 // subobject.
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001427 ++Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001428 ++FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001429
1430 // If this the first designator, our caller will continue checking
1431 // the rest of this struct/class/union subobject.
1432 if (IsFirstDesignator) {
1433 if (NextField)
1434 *NextField = Field;
Douglas Gregor4c678342009-01-28 21:54:33 +00001435 StructuredIndex = FieldIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001436 return false;
1437 }
1438
Douglas Gregor34e79462009-01-28 23:36:17 +00001439 if (!FinishSubobjectInit)
1440 return false;
1441
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001442 // We've already initialized something in the union; we're done.
1443 if (RT->getDecl()->isUnion())
1444 return hadError;
1445
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001446 // Check the remaining fields within this class/struct/union subobject.
1447 bool prevHadError = hadError;
Douglas Gregor4c678342009-01-28 21:54:33 +00001448 CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
1449 StructuredList, FieldIndex);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001450 return hadError && !prevHadError;
1451 }
1452
1453 // C99 6.7.8p6:
1454 //
1455 // If a designator has the form
1456 //
1457 // [ constant-expression ]
1458 //
1459 // then the current object (defined below) shall have array
1460 // type and the expression shall be an integer constant
1461 // expression. If the array is of unknown size, any
1462 // nonnegative value is valid.
1463 //
1464 // Additionally, cope with the GNU extension that permits
1465 // designators of the form
1466 //
1467 // [ constant-expression ... constant-expression ]
Chris Lattner08202542009-02-24 22:50:46 +00001468 const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001469 if (!AT) {
Chris Lattner08202542009-02-24 22:50:46 +00001470 SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001471 << CurrentObjectType;
1472 ++Index;
1473 return true;
1474 }
1475
1476 Expr *IndexExpr = 0;
Douglas Gregor34e79462009-01-28 23:36:17 +00001477 llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
1478 if (D->isArrayDesignator()) {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001479 IndexExpr = DIE->getArrayIndex(*D);
Chris Lattner3bf68932009-04-25 21:59:05 +00001480 DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
Douglas Gregor34e79462009-01-28 23:36:17 +00001481 DesignatedEndIndex = DesignatedStartIndex;
1482 } else {
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001483 assert(D->isArrayRangeDesignator() && "Need array-range designator");
Douglas Gregor34e79462009-01-28 23:36:17 +00001484
Mike Stump1eb44332009-09-09 15:08:12 +00001485
1486 DesignatedStartIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001487 DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001488 DesignatedEndIndex =
Chris Lattner3bf68932009-04-25 21:59:05 +00001489 DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001490 IndexExpr = DIE->getArrayRangeEnd(*D);
Douglas Gregor34e79462009-01-28 23:36:17 +00001491
Chris Lattner3bf68932009-04-25 21:59:05 +00001492 if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
Douglas Gregora9c87802009-01-29 19:42:23 +00001493 FullyStructuredList->sawArrayRangeDesignator();
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001494 }
1495
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001496 if (isa<ConstantArrayType>(AT)) {
1497 llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
Douglas Gregor34e79462009-01-28 23:36:17 +00001498 DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1499 DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1500 DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1501 DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
1502 if (DesignatedEndIndex >= MaxElements) {
Chris Lattner08202542009-02-24 22:50:46 +00001503 SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001504 diag::err_array_designator_too_large)
Douglas Gregor34e79462009-01-28 23:36:17 +00001505 << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001506 << IndexExpr->getSourceRange();
1507 ++Index;
1508 return true;
1509 }
Douglas Gregor34e79462009-01-28 23:36:17 +00001510 } else {
1511 // Make sure the bit-widths and signedness match.
1512 if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1513 DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
Chris Lattner3bf68932009-04-25 21:59:05 +00001514 else if (DesignatedStartIndex.getBitWidth() <
1515 DesignatedEndIndex.getBitWidth())
Douglas Gregor34e79462009-01-28 23:36:17 +00001516 DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1517 DesignatedStartIndex.setIsUnsigned(true);
1518 DesignatedEndIndex.setIsUnsigned(true);
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Douglas Gregor4c678342009-01-28 21:54:33 +00001521 // Make sure that our non-designated initializer list has space
1522 // for a subobject corresponding to this array element.
Douglas Gregor34e79462009-01-28 23:36:17 +00001523 if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
Mike Stump1eb44332009-09-09 15:08:12 +00001524 StructuredList->resizeInits(SemaRef.Context,
Douglas Gregor34e79462009-01-28 23:36:17 +00001525 DesignatedEndIndex.getZExtValue() + 1);
Douglas Gregor4c678342009-01-28 21:54:33 +00001526
Douglas Gregor34e79462009-01-28 23:36:17 +00001527 // Repeatedly perform subobject initializations in the range
1528 // [DesignatedStartIndex, DesignatedEndIndex].
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001529
Douglas Gregor34e79462009-01-28 23:36:17 +00001530 // Move to the next designator
1531 unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1532 unsigned OldIndex = Index;
Douglas Gregor34e79462009-01-28 23:36:17 +00001533 while (DesignatedStartIndex <= DesignatedEndIndex) {
1534 // Recurse to check later designated subobjects.
1535 QualType ElementType = AT->getElementType();
1536 Index = OldIndex;
Douglas Gregor71199712009-04-15 04:56:10 +00001537 if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
1538 Index, StructuredList, ElementIndex,
Douglas Gregoreeb15d42009-02-04 22:46:25 +00001539 (DesignatedStartIndex == DesignatedEndIndex),
1540 false))
Douglas Gregor34e79462009-01-28 23:36:17 +00001541 return true;
1542
1543 // Move to the next index in the array that we'll be initializing.
1544 ++DesignatedStartIndex;
1545 ElementIndex = DesignatedStartIndex.getZExtValue();
1546 }
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001547
1548 // If this the first designator, our caller will continue checking
1549 // the rest of this array subobject.
1550 if (IsFirstDesignator) {
1551 if (NextElementIndex)
Douglas Gregor34e79462009-01-28 23:36:17 +00001552 *NextElementIndex = DesignatedStartIndex;
Douglas Gregor4c678342009-01-28 21:54:33 +00001553 StructuredIndex = ElementIndex;
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001554 return false;
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregor34e79462009-01-28 23:36:17 +00001557 if (!FinishSubobjectInit)
1558 return false;
1559
Douglas Gregor87f55cf2009-01-22 23:26:18 +00001560 // Check the remaining elements within this array subobject.
Douglas Gregor05c13a32009-01-22 00:58:24 +00001561 bool prevHadError = hadError;
Douglas Gregorfdf55692009-02-09 19:45:19 +00001562 CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
Douglas Gregor4c678342009-01-28 21:54:33 +00001563 StructuredList, ElementIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001564 return hadError && !prevHadError;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001565}
1566
Douglas Gregor4c678342009-01-28 21:54:33 +00001567// Get the structured initializer list for a subobject of type
1568// @p CurrentObjectType.
1569InitListExpr *
1570InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1571 QualType CurrentObjectType,
1572 InitListExpr *StructuredList,
1573 unsigned StructuredIndex,
1574 SourceRange InitRange) {
1575 Expr *ExistingInit = 0;
1576 if (!StructuredList)
1577 ExistingInit = SyntacticToSemantic[IList];
1578 else if (StructuredIndex < StructuredList->getNumInits())
1579 ExistingInit = StructuredList->getInit(StructuredIndex);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Douglas Gregor4c678342009-01-28 21:54:33 +00001581 if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1582 return Result;
1583
1584 if (ExistingInit) {
1585 // We are creating an initializer list that initializes the
1586 // subobjects of the current object, but there was already an
1587 // initialization that completely initialized the current
1588 // subobject, e.g., by a compound literal:
Mike Stump1eb44332009-09-09 15:08:12 +00001589 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001590 // struct X { int a, b; };
1591 // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
Mike Stump1eb44332009-09-09 15:08:12 +00001592 //
Douglas Gregor4c678342009-01-28 21:54:33 +00001593 // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1594 // designated initializer re-initializes the whole
1595 // subobject [0], overwriting previous initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001596 SemaRef.Diag(InitRange.getBegin(),
Douglas Gregored8a93d2009-03-01 17:12:46 +00001597 diag::warn_subobject_initializer_overrides)
Douglas Gregor4c678342009-01-28 21:54:33 +00001598 << InitRange;
Mike Stump1eb44332009-09-09 15:08:12 +00001599 SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001600 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001601 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001602 << ExistingInit->getSourceRange();
1603 }
1604
Mike Stump1eb44332009-09-09 15:08:12 +00001605 InitListExpr *Result
1606 = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
Douglas Gregored8a93d2009-03-01 17:12:46 +00001607 InitRange.getEnd());
1608
Douglas Gregor4c678342009-01-28 21:54:33 +00001609 Result->setType(CurrentObjectType);
1610
Douglas Gregorfa219202009-03-20 23:58:33 +00001611 // Pre-allocate storage for the structured initializer list.
1612 unsigned NumElements = 0;
Douglas Gregor08457732009-03-21 18:13:52 +00001613 unsigned NumInits = 0;
1614 if (!StructuredList)
1615 NumInits = IList->getNumInits();
1616 else if (Index < IList->getNumInits()) {
1617 if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1618 NumInits = SubList->getNumInits();
1619 }
1620
Mike Stump1eb44332009-09-09 15:08:12 +00001621 if (const ArrayType *AType
Douglas Gregorfa219202009-03-20 23:58:33 +00001622 = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
1623 if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1624 NumElements = CAType->getSize().getZExtValue();
1625 // Simple heuristic so that we don't allocate a very large
1626 // initializer with many empty entries at the end.
Douglas Gregor08457732009-03-21 18:13:52 +00001627 if (NumInits && NumElements > NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001628 NumElements = 0;
1629 }
John McCall183700f2009-09-21 23:43:11 +00001630 } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
Douglas Gregorfa219202009-03-20 23:58:33 +00001631 NumElements = VType->getNumElements();
Ted Kremenek6217b802009-07-29 21:53:49 +00001632 else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
Douglas Gregorfa219202009-03-20 23:58:33 +00001633 RecordDecl *RDecl = RType->getDecl();
1634 if (RDecl->isUnion())
1635 NumElements = 1;
1636 else
Mike Stump1eb44332009-09-09 15:08:12 +00001637 NumElements = std::distance(RDecl->field_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001638 RDecl->field_end());
Douglas Gregorfa219202009-03-20 23:58:33 +00001639 }
1640
Douglas Gregor08457732009-03-21 18:13:52 +00001641 if (NumElements < NumInits)
Douglas Gregorfa219202009-03-20 23:58:33 +00001642 NumElements = IList->getNumInits();
1643
1644 Result->reserveInits(NumElements);
1645
Douglas Gregor4c678342009-01-28 21:54:33 +00001646 // Link this new initializer list into the structured initializer
1647 // lists.
1648 if (StructuredList)
1649 StructuredList->updateInit(StructuredIndex, Result);
1650 else {
1651 Result->setSyntacticForm(IList);
1652 SyntacticToSemantic[IList] = Result;
1653 }
1654
1655 return Result;
1656}
1657
1658/// Update the initializer at index @p StructuredIndex within the
1659/// structured initializer list to the value @p expr.
1660void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1661 unsigned &StructuredIndex,
1662 Expr *expr) {
1663 // No structured initializer list to update
1664 if (!StructuredList)
1665 return;
1666
1667 if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1668 // This initializer overwrites a previous initializer. Warn.
Mike Stump1eb44332009-09-09 15:08:12 +00001669 SemaRef.Diag(expr->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001670 diag::warn_initializer_overrides)
1671 << expr->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001672 SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
Douglas Gregor4c678342009-01-28 21:54:33 +00001673 diag::note_previous_initializer)
Douglas Gregor54f07282009-01-28 23:43:32 +00001674 << /*FIXME:has side effects=*/0
Douglas Gregor4c678342009-01-28 21:54:33 +00001675 << PrevInit->getSourceRange();
1676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Douglas Gregor4c678342009-01-28 21:54:33 +00001678 ++StructuredIndex;
1679}
1680
Douglas Gregor05c13a32009-01-22 00:58:24 +00001681/// Check that the given Index expression is a valid array designator
1682/// value. This is essentailly just a wrapper around
Chris Lattner3bf68932009-04-25 21:59:05 +00001683/// VerifyIntegerConstantExpression that also checks for negative values
Douglas Gregor05c13a32009-01-22 00:58:24 +00001684/// and produces a reasonable diagnostic if there is a
1685/// failure. Returns true if there was an error, false otherwise. If
1686/// everything went okay, Value will receive the value of the constant
1687/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001688static bool
Chris Lattner3bf68932009-04-25 21:59:05 +00001689CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001690 SourceLocation Loc = Index->getSourceRange().getBegin();
1691
1692 // Make sure this is an integer constant expression.
Chris Lattner3bf68932009-04-25 21:59:05 +00001693 if (S.VerifyIntegerConstantExpression(Index, &Value))
1694 return true;
Douglas Gregor05c13a32009-01-22 00:58:24 +00001695
Chris Lattner3bf68932009-04-25 21:59:05 +00001696 if (Value.isSigned() && Value.isNegative())
1697 return S.Diag(Loc, diag::err_array_designator_negative)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001698 << Value.toString(10) << Index->getSourceRange();
1699
Douglas Gregor53d3d8e2009-01-23 21:04:18 +00001700 Value.setIsUnsigned(true);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001701 return false;
1702}
1703
1704Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1705 SourceLocation Loc,
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001706 bool GNUSyntax,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001707 OwningExprResult Init) {
1708 typedef DesignatedInitExpr::Designator ASTDesignator;
1709
1710 bool Invalid = false;
1711 llvm::SmallVector<ASTDesignator, 32> Designators;
1712 llvm::SmallVector<Expr *, 32> InitExpressions;
1713
1714 // Build designators and check array designator expressions.
1715 for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1716 const Designator &D = Desig.getDesignator(Idx);
1717 switch (D.getKind()) {
1718 case Designator::FieldDesignator:
Mike Stump1eb44332009-09-09 15:08:12 +00001719 Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001720 D.getFieldLoc()));
1721 break;
1722
1723 case Designator::ArrayDesignator: {
1724 Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1725 llvm::APSInt IndexValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001726 if (!Index->isTypeDependent() &&
1727 !Index->isValueDependent() &&
1728 CheckArrayDesignatorExpr(*this, Index, IndexValue))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001729 Invalid = true;
1730 else {
1731 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001732 D.getLBracketLoc(),
Douglas Gregor05c13a32009-01-22 00:58:24 +00001733 D.getRBracketLoc()));
1734 InitExpressions.push_back(Index);
1735 }
1736 break;
1737 }
1738
1739 case Designator::ArrayRangeDesignator: {
1740 Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1741 Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1742 llvm::APSInt StartValue;
1743 llvm::APSInt EndValue;
Douglas Gregor9ea62762009-05-21 23:17:49 +00001744 bool StartDependent = StartIndex->isTypeDependent() ||
1745 StartIndex->isValueDependent();
1746 bool EndDependent = EndIndex->isTypeDependent() ||
1747 EndIndex->isValueDependent();
1748 if ((!StartDependent &&
1749 CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1750 (!EndDependent &&
1751 CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
Douglas Gregor05c13a32009-01-22 00:58:24 +00001752 Invalid = true;
Douglas Gregord6f584f2009-01-23 22:22:29 +00001753 else {
1754 // Make sure we're comparing values with the same bit width.
Douglas Gregor9ea62762009-05-21 23:17:49 +00001755 if (StartDependent || EndDependent) {
1756 // Nothing to compute.
1757 } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
Douglas Gregord6f584f2009-01-23 22:22:29 +00001758 EndValue.extend(StartValue.getBitWidth());
1759 else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1760 StartValue.extend(EndValue.getBitWidth());
1761
Douglas Gregorc4bb7bf2009-05-21 23:30:39 +00001762 if (!StartDependent && !EndDependent && EndValue < StartValue) {
Douglas Gregord6f584f2009-01-23 22:22:29 +00001763 Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
Mike Stump1eb44332009-09-09 15:08:12 +00001764 << StartValue.toString(10) << EndValue.toString(10)
Douglas Gregord6f584f2009-01-23 22:22:29 +00001765 << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1766 Invalid = true;
1767 } else {
1768 Designators.push_back(ASTDesignator(InitExpressions.size(),
Mike Stump1eb44332009-09-09 15:08:12 +00001769 D.getLBracketLoc(),
Douglas Gregord6f584f2009-01-23 22:22:29 +00001770 D.getEllipsisLoc(),
1771 D.getRBracketLoc()));
1772 InitExpressions.push_back(StartIndex);
1773 InitExpressions.push_back(EndIndex);
1774 }
Douglas Gregor05c13a32009-01-22 00:58:24 +00001775 }
1776 break;
1777 }
1778 }
1779 }
1780
1781 if (Invalid || Init.isInvalid())
1782 return ExprError();
1783
1784 // Clear out the expressions within the designation.
1785 Desig.ClearExprs(*this);
1786
1787 DesignatedInitExpr *DIE
Jay Foadbeaaccd2009-05-21 09:52:38 +00001788 = DesignatedInitExpr::Create(Context,
1789 Designators.data(), Designators.size(),
1790 InitExpressions.data(), InitExpressions.size(),
Anders Carlssone9146f22009-05-01 19:49:17 +00001791 Loc, GNUSyntax, Init.takeAs<Expr>());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001792 return Owned(DIE);
1793}
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001794
1795bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) {
Chris Lattner08202542009-02-24 22:50:46 +00001796 InitListChecker CheckInitList(*this, InitList, DeclType);
Douglas Gregorc34ee5e2009-01-29 00:45:39 +00001797 if (!CheckInitList.HadError())
1798 InitList = CheckInitList.getFullyStructuredList();
1799
1800 return CheckInitList.HadError();
1801}
Douglas Gregor87fd7032009-02-02 17:43:21 +00001802
1803/// \brief Diagnose any semantic errors with value-initialization of
1804/// the given type.
1805///
1806/// Value-initialization effectively zero-initializes any types
1807/// without user-declared constructors, and calls the default
1808/// constructor for a for any type that has a user-declared
1809/// constructor (C++ [dcl.init]p5). Value-initialization can fail when
1810/// a type with a user-declared constructor does not have an
1811/// accessible, non-deleted default constructor. In C, everything can
1812/// be value-initialized, which corresponds to C's notion of
1813/// initializing objects with static storage duration when no
Mike Stump1eb44332009-09-09 15:08:12 +00001814/// initializer is provided for that object.
Douglas Gregor87fd7032009-02-02 17:43:21 +00001815///
1816/// \returns true if there was an error, false otherwise.
1817bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) {
1818 // C++ [dcl.init]p5:
1819 //
1820 // To value-initialize an object of type T means:
1821
1822 // -- if T is an array type, then each element is value-initialized;
1823 if (const ArrayType *AT = Context.getAsArrayType(Type))
1824 return CheckValueInitialization(AT->getElementType(), Loc);
1825
Ted Kremenek6217b802009-07-29 21:53:49 +00001826 if (const RecordType *RT = Type->getAs<RecordType>()) {
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001827 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Douglas Gregor87fd7032009-02-02 17:43:21 +00001828 // -- if T is a class type (clause 9) with a user-declared
1829 // constructor (12.1), then the default constructor for T is
1830 // called (and the initialization is ill-formed if T has no
1831 // accessible default constructor);
Douglas Gregor39da0b82009-09-09 23:08:42 +00001832 if (ClassDecl->hasUserDeclaredConstructor()) {
1833 ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this);
1834
1835 CXXConstructorDecl *Constructor
1836 = PerformInitializationByConstructor(Type,
1837 MultiExprArg(*this, 0, 0),
1838 Loc, SourceRange(Loc),
1839 DeclarationName(),
1840 IK_Direct,
1841 ConstructorArgs);
1842 if (!Constructor)
1843 return true;
1844
1845 OwningExprResult Init
1846 = BuildCXXConstructExpr(Loc, Type, Constructor,
1847 move_arg(ConstructorArgs));
1848 if (Init.isInvalid())
1849 return true;
1850
1851 // FIXME: Actually perform the value-initialization!
1852 return false;
1853 }
Douglas Gregor87fd7032009-02-02 17:43:21 +00001854 }
1855 }
1856
1857 if (Type->isReferenceType()) {
1858 // C++ [dcl.init]p5:
1859 // [...] A program that calls for default-initialization or
1860 // value-initialization of an entity of reference type is
1861 // ill-formed. [...]
Mike Stump390b4cc2009-05-16 07:39:55 +00001862 // FIXME: Once we have code that goes through this path, add an actual
1863 // diagnostic :)
Douglas Gregor87fd7032009-02-02 17:43:21 +00001864 }
1865
1866 return false;
1867}