Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // 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 Lattner | e76e9bf | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 14 | // This file also implements Sema::CheckInitializerTypes. |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "Sema.h" |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Designator.h" |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 73bb5e6 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 23 | #include <map> |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 24 | using namespace clang; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 25 | |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Sema Initialization Checking |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 30 | static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) { |
Chris Lattner | 7a7c145 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 31 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
| 32 | if (!AT) return 0; |
| 33 | |
Eli Friedman | 95acf98 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 34 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 35 | return 0; |
| 36 | |
Chris Lattner | 7a7c145 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 37 | // See if this is a string literal or @encode. |
| 38 | Init = Init->IgnoreParens(); |
| 39 | |
| 40 | // 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 Lattner | ff065f7 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 46 | if (SL == 0) return 0; |
Eli Friedman | d16b089 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 47 | |
| 48 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | 7a7c145 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 49 | // 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 Friedman | d16b089 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 52 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | 7a7c145 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 53 | |
Eli Friedman | d16b089 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 54 | // 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 Lattner | 7a7c145 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 60 | return Init; |
| 61 | |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Chris Lattner | 160da07 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 65 | static bool CheckSingleInitializer(Expr *&Init, QualType DeclType, |
| 66 | bool DirectInit, Sema &S) { |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 67 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 68 | // it can promote the expression. |
| 69 | QualType InitType = Init->getType(); |
| 70 | |
Chris Lattner | 160da07 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 71 | if (S.getLangOptions().CPlusPlus) { |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 72 | // FIXME: I dislike this error message. A lot. |
Chris Lattner | 160da07 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 73 | if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit)) |
| 74 | return S.Diag(Init->getSourceRange().getBegin(), |
| 75 | diag::err_typecheck_convert_incompatible) |
| 76 | << DeclType << Init->getType() << "initializing" |
| 77 | << Init->getSourceRange(); |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
Chris Lattner | 160da07 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 81 | Sema::AssignConvertType ConvTy = |
| 82 | S.CheckSingleAssignmentConstraints(DeclType, Init); |
| 83 | return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 84 | InitType, Init, "initializing"); |
| 85 | } |
| 86 | |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) { |
| 88 | // Get the length of the string as parsed. |
| 89 | uint64_t StrLength = |
| 90 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 91 | |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 93 | const ArrayType *AT = S.Context.getAsArrayType(DeclT); |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 94 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
| 95 | // C99 6.7.8p14. We have an array of character type with unknown size |
| 96 | // being initialized to a string literal. |
| 97 | llvm::APSInt ConstVal(32); |
Chris Lattner | d20fac4 | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 98 | ConstVal = StrLength; |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 99 | // Return a new array type (C99 6.7.8p22). |
Douglas Gregor | 1d38113 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 100 | DeclT = S.Context.getConstantArrayWithoutExprType(IAT->getElementType(), |
| 101 | ConstVal, |
| 102 | ArrayType::Normal, 0); |
Chris Lattner | d20fac4 | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 103 | return; |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | d20fac4 | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 105 | |
Eli Friedman | 95acf98 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 106 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
| 107 | |
| 108 | // C99 6.7.8p14. We have an array of character type with known size. However, |
| 109 | // the size may be smaller or larger than the string we are initializing. |
| 110 | // FIXME: Avoid truncation for 64-bit length strings. |
| 111 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 112 | S.Diag(Str->getSourceRange().getBegin(), |
| 113 | diag::warn_initializer_string_for_char_array_too_long) |
| 114 | << Str->getSourceRange(); |
| 115 | |
| 116 | // Set the type to the actual size that we are initializing. If we have |
| 117 | // something like: |
| 118 | // char x[1] = "foo"; |
| 119 | // then this will set the string literal's type to char[1]. |
| 120 | Str->setType(DeclT); |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType, |
| 124 | SourceLocation InitLoc, |
Anders Carlsson | 8cc1f0d | 2009-05-30 20:41:30 +0000 | [diff] [blame] | 125 | DeclarationName InitEntity, bool DirectInit) { |
Douglas Gregor | 3a7a06e | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 126 | if (DeclType->isDependentType() || |
| 127 | Init->isTypeDependent() || Init->isValueDependent()) |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 128 | return false; |
| 129 | |
| 130 | // C++ [dcl.init.ref]p1: |
Sebastian Redl | ce6fff0 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 131 | // A variable declared to be a T& or T&&, that is "reference to type T" |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 132 | // (8.3.2), shall be initialized by an object, or function, of |
| 133 | // type T or by an object that can be converted into a T. |
| 134 | if (DeclType->isReferenceType()) |
| 135 | return CheckReferenceInit(Init, DeclType, 0, false, DirectInit); |
| 136 | |
| 137 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 138 | // of unknown size ("[]") or an object type that is not a variable array type. |
| 139 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
| 140 | return Diag(InitLoc, diag::err_variable_object_no_init) |
| 141 | << VAT->getSizeExpr()->getSourceRange(); |
| 142 | |
| 143 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 144 | if (!InitList) { |
| 145 | // FIXME: Handle wide strings |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 146 | if (Expr *Str = IsStringInit(Init, DeclType, Context)) { |
| 147 | CheckStringInit(Str, DeclType, *this); |
Chris Lattner | d20fac4 | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 148 | return false; |
| 149 | } |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 150 | |
| 151 | // C++ [dcl.init]p14: |
| 152 | // -- If the destination type is a (possibly cv-qualified) class |
| 153 | // type: |
| 154 | if (getLangOptions().CPlusPlus && DeclType->isRecordType()) { |
| 155 | QualType DeclTypeC = Context.getCanonicalType(DeclType); |
| 156 | QualType InitTypeC = Context.getCanonicalType(Init->getType()); |
| 157 | |
| 158 | // -- If the initialization is direct-initialization, or if it is |
| 159 | // copy-initialization where the cv-unqualified version of the |
| 160 | // source type is the same class as, or a derived class of, the |
| 161 | // class of the destination, constructors are considered. |
| 162 | if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) || |
| 163 | IsDerivedFrom(InitTypeC, DeclTypeC)) { |
Anders Carlsson | 5868dfc | 2009-05-27 16:38:58 +0000 | [diff] [blame] | 164 | const CXXRecordDecl *RD = |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 165 | cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 5868dfc | 2009-05-27 16:38:58 +0000 | [diff] [blame] | 166 | |
| 167 | // No need to make a CXXConstructExpr if both the ctor and dtor are |
| 168 | // trivial. |
| 169 | if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor()) |
| 170 | return false; |
| 171 | |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 172 | CXXConstructorDecl *Constructor |
| 173 | = PerformInitializationByConstructor(DeclType, &Init, 1, |
| 174 | InitLoc, Init->getSourceRange(), |
| 175 | InitEntity, |
| 176 | DirectInit? IK_Direct : IK_Copy); |
Anders Carlsson | 73bb5e6 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 177 | if (!Constructor) |
| 178 | return true; |
Anders Carlsson | bd9f51a | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 179 | Init = BuildCXXConstructExpr(DeclType, Constructor, &Init, 1); |
Anders Carlsson | 73bb5e6 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 180 | return false; |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // -- Otherwise (i.e., for the remaining copy-initialization |
| 184 | // cases), user-defined conversion sequences that can |
| 185 | // convert from the source type to the destination type or |
| 186 | // (when a conversion function is used) to a derived class |
| 187 | // thereof are enumerated as described in 13.3.1.4, and the |
| 188 | // best one is chosen through overload resolution |
| 189 | // (13.3). If the conversion cannot be done or is |
| 190 | // ambiguous, the initialization is ill-formed. The |
| 191 | // function selected is called with the initializer |
| 192 | // expression as its argument; if the function is a |
| 193 | // constructor, the call initializes a temporary of the |
| 194 | // destination type. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 195 | // FIXME: We're pretending to do copy elision here; return to this when we |
| 196 | // have ASTs for such things. |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 197 | if (!PerformImplicitConversion(Init, DeclType, "initializing")) |
| 198 | return false; |
| 199 | |
| 200 | if (InitEntity) |
| 201 | return Diag(InitLoc, diag::err_cannot_initialize_decl) |
Chris Lattner | 6434a1b | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 202 | << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 203 | << Init->getType() << Init->getSourceRange(); |
| 204 | return Diag(InitLoc, diag::err_cannot_initialize_decl_noname) |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 205 | << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 206 | << Init->getType() << Init->getSourceRange(); |
| 207 | } |
| 208 | |
| 209 | // C99 6.7.8p16. |
| 210 | if (DeclType->isArrayType()) |
| 211 | return Diag(Init->getLocStart(), diag::err_array_init_list_required) |
Chris Lattner | 6434a1b | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 212 | << Init->getSourceRange(); |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 160da07 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 214 | return CheckSingleInitializer(Init, DeclType, DirectInit, *this); |
Chris Lattner | d3a0050 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool hadError = CheckInitList(InitList, DeclType); |
| 218 | Init = InitList; |
| 219 | return hadError; |
| 220 | } |
| 221 | |
| 222 | //===----------------------------------------------------------------------===// |
| 223 | // Semantic checking for initializer lists. |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 226 | /// @brief Semantic checking for initializer lists. |
| 227 | /// |
| 228 | /// The InitListChecker class contains a set of routines that each |
| 229 | /// handle the initialization of a certain kind of entity, e.g., |
| 230 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 231 | /// InitListChecker itself performs a recursive walk of the subobject |
| 232 | /// structure of the type to be initialized, while stepping through |
| 233 | /// the initializer list one element at a time. The IList and Index |
| 234 | /// parameters to each of the Check* routines contain the active |
| 235 | /// (syntactic) initializer list and the index into that initializer |
| 236 | /// list that represents the current initializer. Each routine is |
| 237 | /// responsible for moving that Index forward as it consumes elements. |
| 238 | /// |
| 239 | /// Each Check* routine also has a StructuredList/StructuredIndex |
| 240 | /// arguments, which contains the current the "structured" (semantic) |
| 241 | /// initializer list and the index into that initializer list where we |
| 242 | /// are copying initializers as we map them over to the semantic |
| 243 | /// list. Once we have completed our recursive walk of the subobject |
| 244 | /// structure, we will have constructed a full semantic initializer |
| 245 | /// list. |
| 246 | /// |
| 247 | /// C99 designators cause changes in the initializer list traversal, |
| 248 | /// because they make the initialization "jump" into a specific |
| 249 | /// subobject and then continue the initialization from that |
| 250 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 251 | /// designated subobject and manages backing out the recursion to |
| 252 | /// initialize the subobjects after the one designated. |
Chris Lattner | e76e9bf | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 253 | namespace { |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 254 | class InitListChecker { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 255 | Sema &SemaRef; |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 256 | bool hadError; |
| 257 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 258 | InitListExpr *FullyStructuredList; |
| 259 | |
| 260 | void CheckImplicitInitList(InitListExpr *ParentIList, QualType T, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 261 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 262 | unsigned &StructuredIndex, |
| 263 | bool TopLevelObject = false); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 264 | void CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 265 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 266 | unsigned &StructuredIndex, |
| 267 | bool TopLevelObject = false); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 268 | void CheckListElementTypes(InitListExpr *IList, QualType &DeclType, |
| 269 | bool SubobjectIsDesignatorContext, |
| 270 | unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 271 | InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 272 | unsigned &StructuredIndex, |
| 273 | bool TopLevelObject = false); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 274 | void CheckSubElementType(InitListExpr *IList, QualType ElemType, |
| 275 | unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 276 | InitListExpr *StructuredList, |
| 277 | unsigned &StructuredIndex); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 278 | void CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 279 | unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 280 | InitListExpr *StructuredList, |
| 281 | unsigned &StructuredIndex); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 282 | void CheckReferenceType(InitListExpr *IList, QualType DeclType, |
| 283 | unsigned &Index, |
| 284 | InitListExpr *StructuredList, |
| 285 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 286 | void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 287 | InitListExpr *StructuredList, |
| 288 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 289 | void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType, |
| 290 | RecordDecl::field_iterator Field, |
| 291 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 293 | unsigned &StructuredIndex, |
| 294 | bool TopLevelObject = false); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 295 | void CheckArrayType(InitListExpr *IList, QualType &DeclType, |
| 296 | llvm::APSInt elementIndex, |
| 297 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 298 | InitListExpr *StructuredList, |
| 299 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 300 | bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 301 | unsigned DesigIdx, |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 302 | QualType &CurrentObjectType, |
| 303 | RecordDecl::field_iterator *NextField, |
| 304 | llvm::APSInt *NextElementIndex, |
| 305 | unsigned &Index, |
| 306 | InitListExpr *StructuredList, |
| 307 | unsigned &StructuredIndex, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 308 | bool FinishSubobjectInit, |
| 309 | bool TopLevelObject); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 310 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 311 | QualType CurrentObjectType, |
| 312 | InitListExpr *StructuredList, |
| 313 | unsigned StructuredIndex, |
| 314 | SourceRange InitRange); |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 315 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 316 | unsigned &StructuredIndex, |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 317 | Expr *expr); |
| 318 | int numArrayElements(QualType DeclType); |
| 319 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 320 | |
| 321 | void FillInValueInitializations(InitListExpr *ILE); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 322 | public: |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 323 | InitListChecker(Sema &S, InitListExpr *IL, QualType &T); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 324 | bool HadError() { return hadError; } |
| 325 | |
| 326 | // @brief Retrieves the fully-structured initializer list used for |
| 327 | // semantic analysis and code generation. |
| 328 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 329 | }; |
Chris Lattner | e76e9bf | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 330 | } // end anonymous namespace |
Chris Lattner | 1aa25a7 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 332 | /// Recursively replaces NULL values within the given initializer list |
| 333 | /// with expressions that perform value-initialization of the |
| 334 | /// appropriate type. |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 335 | void InitListChecker::FillInValueInitializations(InitListExpr *ILE) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 336 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 337 | "Should not have void type"); |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 338 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 339 | if (ILE->getSyntacticForm()) |
| 340 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
| 341 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 342 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 343 | unsigned Init = 0, NumInits = ILE->getNumInits(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 344 | for (RecordDecl::field_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 345 | Field = RType->getDecl()->field_begin(), |
| 346 | FieldEnd = RType->getDecl()->field_end(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 347 | Field != FieldEnd; ++Field) { |
| 348 | if (Field->isUnnamedBitfield()) |
| 349 | continue; |
| 350 | |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 351 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 352 | if (Field->getType()->isReferenceType()) { |
| 353 | // C++ [dcl.init.aggr]p9: |
| 354 | // If an incomplete or empty initializer-list leaves a |
| 355 | // member of reference type uninitialized, the program is |
| 356 | // ill-formed. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 357 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 358 | << Field->getType() |
| 359 | << ILE->getSyntacticForm()->getSourceRange(); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 360 | SemaRef.Diag(Field->getLocation(), |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 361 | diag::note_uninit_reference_member); |
| 362 | hadError = true; |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 363 | return; |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 364 | } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) { |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 365 | hadError = true; |
| 366 | return; |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 367 | } |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 368 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 369 | // FIXME: If value-initialization involves calling a constructor, should |
| 370 | // we make that call explicit in the representation (even when it means |
| 371 | // extending the initializer list)? |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 372 | if (Init < NumInits && !hadError) |
| 373 | ILE->setInit(Init, |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 374 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType())); |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 375 | } else if (InitListExpr *InnerILE |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 376 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 377 | FillInValueInitializations(InnerILE); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 378 | ++Init; |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 379 | |
| 380 | // Only look at the first initialization of a union. |
| 381 | if (RType->getDecl()->isUnion()) |
| 382 | break; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | QualType ElementType; |
| 389 | |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 390 | unsigned NumInits = ILE->getNumInits(); |
| 391 | unsigned NumElements = NumInits; |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 392 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 393 | ElementType = AType->getElementType(); |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 394 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 395 | NumElements = CAType->getSize().getZExtValue(); |
| 396 | } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 397 | ElementType = VType->getElementType(); |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 398 | NumElements = VType->getNumElements(); |
| 399 | } else |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 400 | ElementType = ILE->getType(); |
| 401 | |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 402 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
| 403 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 404 | if (SemaRef.CheckValueInitialization(ElementType, Loc)) { |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 405 | hadError = true; |
| 406 | return; |
| 407 | } |
| 408 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 409 | // FIXME: If value-initialization involves calling a constructor, should |
| 410 | // we make that call explicit in the representation (even when it means |
| 411 | // extending the initializer list)? |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 412 | if (Init < NumInits && !hadError) |
| 413 | ILE->setInit(Init, |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 414 | new (SemaRef.Context) ImplicitValueInitExpr(ElementType)); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 415 | } else if (InitListExpr *InnerILE |
| 416 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 417 | FillInValueInitializations(InnerILE); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Chris Lattner | 1aa25a7 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 422 | InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T) |
| 423 | : SemaRef(S) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 424 | hadError = false; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 425 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 426 | unsigned newIndex = 0; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 427 | unsigned newStructuredIndex = 0; |
| 428 | FullyStructuredList |
Douglas Gregor | ea765e1 | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 429 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 430 | CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex, |
| 431 | /*TopLevelObject=*/true); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 433 | if (!hadError) |
| 434 | FillInValueInitializations(FullyStructuredList); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 438 | // FIXME: use a proper constant |
| 439 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 440 | if (const ConstantArrayType *CAT = |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 441 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 442 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 443 | } |
| 444 | return maxElements; |
| 445 | } |
| 446 | |
| 447 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 448 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 449 | int InitializableMembers = 0; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 450 | for (RecordDecl::field_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 451 | Field = structDecl->field_begin(), |
| 452 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 453 | Field != FieldEnd; ++Field) { |
| 454 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 455 | ++InitializableMembers; |
| 456 | } |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 457 | if (structDecl->isUnion()) |
Eli Friedman | 9f5250b | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 458 | return std::min(InitializableMembers, 1); |
| 459 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 463 | QualType T, unsigned &Index, |
| 464 | InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 465 | unsigned &StructuredIndex, |
| 466 | bool TopLevelObject) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 467 | int maxElements = 0; |
| 468 | |
| 469 | if (T->isArrayType()) |
| 470 | maxElements = numArrayElements(T); |
| 471 | else if (T->isStructureType() || T->isUnionType()) |
| 472 | maxElements = numStructUnionElements(T); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 473 | else if (T->isVectorType()) |
| 474 | maxElements = T->getAsVectorType()->getNumElements(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 475 | else |
| 476 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 477 | |
Eli Friedman | f8df28c | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 478 | if (maxElements == 0) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 479 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | f8df28c | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 480 | diag::err_implicit_empty_initializer); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 481 | ++Index; |
Eli Friedman | f8df28c | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 482 | hadError = true; |
| 483 | return; |
| 484 | } |
| 485 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 486 | // Build a structured initializer list corresponding to this subobject. |
| 487 | InitListExpr *StructuredSubobjectInitList |
| 488 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 489 | StructuredIndex, |
Douglas Gregor | ea765e1 | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 490 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 491 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 492 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 494 | // Check the element types and build the structural subobject. |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 495 | unsigned StartIndex = Index; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 496 | CheckListElementTypes(ParentIList, T, false, Index, |
| 497 | StructuredSubobjectInitList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 498 | StructuredSubobjectInitIndex, |
| 499 | TopLevelObject); |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 500 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | cd2c527 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 501 | StructuredSubobjectInitList->setType(T); |
| 502 | |
Douglas Gregor | ea765e1 | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 503 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 504 | // range corresponds with the end of the last initializer it used. |
| 505 | if (EndIndex < ParentIList->getNumInits()) { |
| 506 | SourceLocation EndLoc |
| 507 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 508 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 509 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Steve Naroff | 5609952 | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 512 | void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 513 | unsigned &Index, |
| 514 | InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 515 | unsigned &StructuredIndex, |
| 516 | bool TopLevelObject) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 517 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 518 | SyntacticToSemantic[IList] = StructuredList; |
| 519 | StructuredList->setSyntacticForm(IList); |
| 520 | CheckListElementTypes(IList, T, true, Index, StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 521 | StructuredIndex, TopLevelObject); |
Steve Naroff | 5609952 | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 522 | IList->setType(T); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 523 | StructuredList->setType(T); |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 524 | if (hadError) |
| 525 | return; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 526 | |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 527 | if (Index < IList->getNumInits()) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 528 | // We have leftover initializers |
Eli Friedman | 579534a | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 529 | if (StructuredIndex == 1 && |
| 530 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | c25bf6d | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 531 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | 579534a | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 532 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | c25bf6d | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 533 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | 579534a | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 534 | hadError = true; |
| 535 | } |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 536 | // Special-case |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 537 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 538 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | b9ea6bc | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 539 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | 09f078c | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 540 | // Don't complain for incomplete types, since we'll get an error |
| 541 | // elsewhere |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 542 | QualType CurrentObjectType = StructuredList->getType(); |
| 543 | int initKind = |
| 544 | CurrentObjectType->isArrayType()? 0 : |
| 545 | CurrentObjectType->isVectorType()? 1 : |
| 546 | CurrentObjectType->isScalarType()? 2 : |
| 547 | CurrentObjectType->isUnionType()? 3 : |
| 548 | 4; |
Douglas Gregor | c25bf6d | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 549 | |
| 550 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | 579534a | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 551 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 552 | DK = diag::err_excess_initializers; |
| 553 | hadError = true; |
| 554 | } |
Nate Begeman | 48fd8c9 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 555 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 556 | DK = diag::err_excess_initializers; |
| 557 | hadError = true; |
| 558 | } |
Douglas Gregor | c25bf6d | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 560 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 561 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
Eli Friedman | 455f762 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 564 | |
Eli Friedman | 90bcb89 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 565 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 566 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 567 | << IList->getSourceRange() |
| 568 | << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart())) |
| 569 | << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd())); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 572 | void InitListChecker::CheckListElementTypes(InitListExpr *IList, |
| 573 | QualType &DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 574 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 575 | unsigned &Index, |
| 576 | InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 577 | unsigned &StructuredIndex, |
| 578 | bool TopLevelObject) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 579 | if (DeclType->isScalarType()) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 580 | CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 581 | } else if (DeclType->isVectorType()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 582 | CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | e7ef500 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 583 | } else if (DeclType->isAggregateType()) { |
| 584 | if (DeclType->isRecordType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 585 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 586 | CheckStructUnionTypes(IList, DeclType, RD->field_begin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 587 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 588 | StructuredList, StructuredIndex, |
| 589 | TopLevelObject); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 590 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 591 | llvm::APSInt Zero( |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 592 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 593 | false); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 594 | CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index, |
| 595 | StructuredList, StructuredIndex); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 596 | } else |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 597 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | ff5b3a8 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 598 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 599 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 600 | ++Index; |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 601 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 602 | << DeclType; |
Eli Friedman | b9ea6bc | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 603 | hadError = true; |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 604 | } else if (DeclType->isRecordType()) { |
| 605 | // C++ [dcl.init]p14: |
| 606 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 607 | // is a brace-enclosed list, see 8.5.1. |
| 608 | // |
| 609 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 610 | // we have an initializer list and a destination type that is not |
| 611 | // an aggregate. |
| 612 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 613 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 614 | << DeclType << IList->getSourceRange(); |
| 615 | hadError = true; |
| 616 | } else if (DeclType->isReferenceType()) { |
| 617 | CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 618 | } else { |
| 619 | // In C, all types are either scalars or aggregates, but |
| 620 | // additional handling is needed here for C++ (and possibly others?). |
| 621 | assert(0 && "Unsupported initializer type"); |
| 622 | } |
| 623 | } |
| 624 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 625 | void InitListChecker::CheckSubElementType(InitListExpr *IList, |
| 626 | QualType ElemType, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 627 | unsigned &Index, |
| 628 | InitListExpr *StructuredList, |
| 629 | unsigned &StructuredIndex) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 630 | Expr *expr = IList->getInit(Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 631 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 632 | unsigned newIndex = 0; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 633 | unsigned newStructuredIndex = 0; |
| 634 | InitListExpr *newStructuredList |
| 635 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 636 | StructuredList, StructuredIndex, |
| 637 | SubInitList->getSourceRange()); |
| 638 | CheckExplicitInitList(SubInitList, ElemType, newIndex, |
| 639 | newStructuredList, newStructuredIndex); |
| 640 | ++StructuredIndex; |
| 641 | ++Index; |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 642 | } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) { |
| 643 | CheckStringInit(Str, ElemType, SemaRef); |
Chris Lattner | 45d6fd6 | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 644 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 645 | ++Index; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 646 | } else if (ElemType->isScalarType()) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 647 | CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 648 | } else if (ElemType->isReferenceType()) { |
| 649 | CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 650 | } else { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 651 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 652 | // C++ [dcl.init.aggr]p12: |
| 653 | // All implicit type conversions (clause 4) are considered when |
| 654 | // initializing the aggregate member with an ini- tializer from |
| 655 | // an initializer-list. If the initializer can initialize a |
| 656 | // member, the member is initialized. [...] |
| 657 | ImplicitConversionSequence ICS |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 658 | = SemaRef.TryCopyInitialization(expr, ElemType); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 659 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 660 | if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS, |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 661 | "initializing")) |
| 662 | hadError = true; |
| 663 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 664 | ++Index; |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | // Fall through for subaggregate initialization |
| 669 | } else { |
| 670 | // C99 6.7.8p13: |
| 671 | // |
| 672 | // The initializer for a structure or union object that has |
| 673 | // automatic storage duration shall be either an initializer |
| 674 | // list as described below, or a single expression that has |
| 675 | // compatible structure or union type. In the latter case, the |
| 676 | // initial value of the object, including unnamed members, is |
| 677 | // that of the expression. |
Eli Friedman | 2a55381 | 2009-06-13 10:38:46 +0000 | [diff] [blame] | 678 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Eli Friedman | 95acf98 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 679 | SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) { |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 680 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 681 | ++Index; |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | // Fall through for subaggregate initialization |
| 686 | } |
| 687 | |
| 688 | // C++ [dcl.init.aggr]p12: |
| 689 | // |
| 690 | // [...] Otherwise, if the member is itself a non-empty |
| 691 | // subaggregate, brace elision is assumed and the initializer is |
| 692 | // considered for the initialization of the first member of |
| 693 | // the subaggregate. |
| 694 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
| 695 | CheckImplicitInitList(IList, ElemType, Index, StructuredList, |
| 696 | StructuredIndex); |
| 697 | ++StructuredIndex; |
| 698 | } else { |
| 699 | // We cannot initialize this element, so let |
| 700 | // PerformCopyInitialization produce the appropriate diagnostic. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 701 | SemaRef.PerformCopyInitialization(expr, ElemType, "initializing"); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 702 | hadError = true; |
| 703 | ++Index; |
| 704 | ++StructuredIndex; |
| 705 | } |
| 706 | } |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 709 | void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 710 | unsigned &Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 711 | InitListExpr *StructuredList, |
| 712 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 713 | if (Index < IList->getNumInits()) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 714 | Expr *expr = IList->getInit(Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 715 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 716 | SemaRef.Diag(IList->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 717 | diag::err_many_braces_around_scalar_init) |
| 718 | << IList->getSourceRange(); |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 719 | hadError = true; |
| 720 | ++Index; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 721 | ++StructuredIndex; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 722 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 723 | } else if (isa<DesignatedInitExpr>(expr)) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 724 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 725 | diag::err_designator_for_scalar_init) |
| 726 | << DeclType << expr->getSourceRange(); |
| 727 | hadError = true; |
| 728 | ++Index; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 729 | ++StructuredIndex; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 730 | return; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 731 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 732 | |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 733 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 734 | if (CheckSingleInitializer(expr, DeclType, false, SemaRef)) |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 735 | hadError = true; // types weren't compatible. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 736 | else if (savExpr != expr) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 737 | // The type was promoted, update initializer list. |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 738 | IList->setInit(Index, expr); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 739 | } |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 740 | if (hadError) |
| 741 | ++StructuredIndex; |
| 742 | else |
| 743 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 744 | ++Index; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 745 | } else { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 746 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 747 | << IList->getSourceRange(); |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 748 | hadError = true; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 749 | ++Index; |
| 750 | ++StructuredIndex; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 751 | return; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 752 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 755 | void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType, |
| 756 | unsigned &Index, |
| 757 | InitListExpr *StructuredList, |
| 758 | unsigned &StructuredIndex) { |
| 759 | if (Index < IList->getNumInits()) { |
| 760 | Expr *expr = IList->getInit(Index); |
| 761 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 762 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 763 | << DeclType << IList->getSourceRange(); |
| 764 | hadError = true; |
| 765 | ++Index; |
| 766 | ++StructuredIndex; |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 771 | if (SemaRef.CheckReferenceInit(expr, DeclType)) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 772 | hadError = true; |
| 773 | else if (savExpr != expr) { |
| 774 | // The type was promoted, update initializer list. |
| 775 | IList->setInit(Index, expr); |
| 776 | } |
| 777 | if (hadError) |
| 778 | ++StructuredIndex; |
| 779 | else |
| 780 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 781 | ++Index; |
| 782 | } else { |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 783 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 784 | // general, it would be useful to pass location information down the stack, |
| 785 | // so that we know the location (or decl) of the "current object" being |
| 786 | // initialized. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 787 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 788 | diag::err_init_reference_member_uninitialized) |
| 789 | << DeclType |
| 790 | << IList->getSourceRange(); |
| 791 | hadError = true; |
| 792 | ++Index; |
| 793 | ++StructuredIndex; |
| 794 | return; |
| 795 | } |
| 796 | } |
| 797 | |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 798 | void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 799 | unsigned &Index, |
| 800 | InitListExpr *StructuredList, |
| 801 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 802 | if (Index < IList->getNumInits()) { |
| 803 | const VectorType *VT = DeclType->getAsVectorType(); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 804 | unsigned maxElements = VT->getNumElements(); |
| 805 | unsigned numEltsInit = 0; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 806 | QualType elementType = VT->getElementType(); |
| 807 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 808 | if (!SemaRef.getLangOptions().OpenCL) { |
| 809 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 810 | // Don't attempt to go past the end of the init list |
| 811 | if (Index >= IList->getNumInits()) |
| 812 | break; |
| 813 | CheckSubElementType(IList, elementType, Index, |
| 814 | StructuredList, StructuredIndex); |
| 815 | } |
| 816 | } else { |
| 817 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 818 | for (unsigned i = 0; i < maxElements; ++i) { |
| 819 | // Don't attempt to go past the end of the init list |
| 820 | if (Index >= IList->getNumInits()) |
| 821 | break; |
| 822 | QualType IType = IList->getInit(Index)->getType(); |
| 823 | if (!IType->isVectorType()) { |
| 824 | CheckSubElementType(IList, elementType, Index, |
| 825 | StructuredList, StructuredIndex); |
| 826 | ++numEltsInit; |
| 827 | } else { |
| 828 | const VectorType *IVT = IType->getAsVectorType(); |
| 829 | unsigned numIElts = IVT->getNumElements(); |
| 830 | QualType VecType = SemaRef.Context.getExtVectorType(elementType, |
| 831 | numIElts); |
| 832 | CheckSubElementType(IList, VecType, Index, |
| 833 | StructuredList, StructuredIndex); |
| 834 | numEltsInit += numIElts; |
| 835 | } |
| 836 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 837 | } |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 838 | |
| 839 | // OpenCL & AltiVec require all elements to be initialized. |
| 840 | if (numEltsInit != maxElements) |
| 841 | if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec) |
| 842 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 843 | diag::err_vector_incorrect_num_initializers) |
| 844 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
| 848 | void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 849 | llvm::APSInt elementIndex, |
| 850 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 851 | unsigned &Index, |
| 852 | InitListExpr *StructuredList, |
| 853 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 854 | // Check for the special-case of initializing an array with a string. |
| 855 | if (Index < IList->getNumInits()) { |
Chris Lattner | 19ae2fc | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 856 | if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType, |
| 857 | SemaRef.Context)) { |
| 858 | CheckStringInit(Str, DeclType, SemaRef); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 859 | // We place the string literal directly into the resulting |
| 860 | // initializer list. This is the only place where the structure |
| 861 | // of the structured initializer list doesn't match exactly, |
| 862 | // because doing so would involve allocating one character |
| 863 | // constant for each string. |
Chris Lattner | 45d6fd6 | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 864 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 865 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 866 | ++Index; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 867 | return; |
| 868 | } |
| 869 | } |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 870 | if (const VariableArrayType *VAT = |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 871 | SemaRef.Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 872 | // Check for VLAs; in standard C it would be possible to check this |
| 873 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 874 | // them in all sorts of strange places). |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 875 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 876 | diag::err_variable_object_no_init) |
| 877 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 878 | hadError = true; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 879 | ++Index; |
| 880 | ++StructuredIndex; |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 884 | // We might know the maximum number of elements in advance. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 885 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 886 | elementIndex.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 887 | bool maxElementsKnown = false; |
| 888 | if (const ConstantArrayType *CAT = |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 889 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 890 | maxElements = CAT->getSize(); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 891 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 892 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 893 | maxElementsKnown = true; |
| 894 | } |
| 895 | |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 896 | QualType elementType = SemaRef.Context.getAsArrayType(DeclType) |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 897 | ->getElementType(); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 898 | while (Index < IList->getNumInits()) { |
| 899 | Expr *Init = IList->getInit(Index); |
| 900 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 901 | // If we're not the subobject that matches up with the '{' for |
| 902 | // the designator, we shouldn't be handling the |
| 903 | // designator. Return immediately. |
| 904 | if (!SubobjectIsDesignatorContext) |
| 905 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 907 | // Handle this designated initializer. elementIndex will be |
| 908 | // updated to be the next array element we'll initialize. |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 909 | if (CheckDesignatedInitializer(IList, DIE, 0, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 910 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 911 | StructuredList, StructuredIndex, true, |
| 912 | false)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 913 | hadError = true; |
| 914 | continue; |
| 915 | } |
| 916 | |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 917 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 918 | maxElements.extend(elementIndex.getBitWidth()); |
| 919 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 920 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 921 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 923 | // If the array is of incomplete type, keep track of the number of |
| 924 | // elements in the initializer. |
| 925 | if (!maxElementsKnown && elementIndex > maxElements) |
| 926 | maxElements = elementIndex; |
| 927 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 928 | continue; |
| 929 | } |
| 930 | |
| 931 | // If we know the maximum number of elements, and we've already |
| 932 | // hit it, stop consuming elements in the initializer list. |
| 933 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 934 | break; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 935 | |
| 936 | // Check this element. |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 937 | CheckSubElementType(IList, elementType, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 938 | StructuredList, StructuredIndex); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 939 | ++elementIndex; |
| 940 | |
| 941 | // If the array is of incomplete type, keep track of the number of |
| 942 | // elements in the initializer. |
| 943 | if (!maxElementsKnown && elementIndex > maxElements) |
| 944 | maxElements = elementIndex; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 945 | } |
Eli Friedman | b4c71b3 | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 946 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 947 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 948 | // be calculated here. |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 949 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 950 | if (maxElements == Zero) { |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 951 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 952 | // but is supported by GNU. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 953 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 954 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 955 | } |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 957 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 958 | ArrayType::Normal, 0); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
| 962 | void InitListChecker::CheckStructUnionTypes(InitListExpr *IList, |
| 963 | QualType DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 964 | RecordDecl::field_iterator Field, |
| 965 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 966 | unsigned &Index, |
| 967 | InitListExpr *StructuredList, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 968 | unsigned &StructuredIndex, |
| 969 | bool TopLevelObject) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 970 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 971 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 972 | // If the record is invalid, some of it's members are invalid. To avoid |
| 973 | // confusion, we forgo checking the intializer for the entire record. |
| 974 | if (structDecl->isInvalidDecl()) { |
| 975 | hadError = true; |
| 976 | return; |
| 977 | } |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 978 | |
| 979 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 980 | // Value-initialize the first named member of the union. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 981 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 982 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 983 | Field != FieldEnd; ++Field) { |
| 984 | if (Field->getDeclName()) { |
| 985 | StructuredList->setInitializedFieldInUnion(*Field); |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | return; |
| 990 | } |
| 991 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 992 | // If structDecl is a forward declaration, this loop won't do |
| 993 | // anything except look at designated initializers; That's okay, |
| 994 | // because an error should get printed out elsewhere. It might be |
| 995 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 996 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 997 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0ecc9e9 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 998 | bool InitializedSomething = false; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 999 | while (Index < IList->getNumInits()) { |
| 1000 | Expr *Init = IList->getInit(Index); |
| 1001 | |
| 1002 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1003 | // If we're not the subobject that matches up with the '{' for |
| 1004 | // the designator, we shouldn't be handling the |
| 1005 | // designator. Return immediately. |
| 1006 | if (!SubobjectIsDesignatorContext) |
| 1007 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1009 | // Handle this designated initializer. Field will be updated to |
| 1010 | // the next field that we'll be initializing. |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1011 | if (CheckDesignatedInitializer(IList, DIE, 0, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1012 | DeclType, &Field, 0, Index, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1013 | StructuredList, StructuredIndex, |
| 1014 | true, TopLevelObject)) |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1015 | hadError = true; |
| 1016 | |
Douglas Gregor | 0ecc9e9 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1017 | InitializedSomething = true; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1018 | continue; |
| 1019 | } |
| 1020 | |
| 1021 | if (Field == FieldEnd) { |
| 1022 | // We've run out of fields. We're done. |
| 1023 | break; |
| 1024 | } |
| 1025 | |
Douglas Gregor | 0ecc9e9 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1026 | // We've already initialized a member of a union. We're done. |
| 1027 | if (InitializedSomething && DeclType->isUnionType()) |
| 1028 | break; |
| 1029 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1030 | // If we've hit the flexible array member at the end, we're done. |
| 1031 | if (Field->getType()->isIncompleteArrayType()) |
| 1032 | break; |
| 1033 | |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1034 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1035 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1036 | ++Field; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1037 | continue; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1038 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1040 | CheckSubElementType(IList, Field->getType(), Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1041 | StructuredList, StructuredIndex); |
Douglas Gregor | 0ecc9e9 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1042 | InitializedSomething = true; |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1043 | |
| 1044 | if (DeclType->isUnionType()) { |
| 1045 | // Initialize the first field within the union. |
| 1046 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1047 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1048 | |
| 1049 | ++Field; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1050 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1052 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | cd2c527 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1053 | Index >= IList->getNumInits()) |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1054 | return; |
| 1055 | |
| 1056 | // Handle GNU flexible array initializers. |
| 1057 | if (!TopLevelObject && |
Douglas Gregor | cd2c527 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1058 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1059 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1060 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1061 | diag::err_flexible_array_init_nonempty) |
| 1062 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1063 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1064 | << *Field; |
| 1065 | hadError = true; |
Douglas Gregor | cd2c527 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1066 | ++Index; |
| 1067 | return; |
| 1068 | } else { |
| 1069 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
| 1070 | diag::ext_flexible_array_init) |
| 1071 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1072 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1073 | << *Field; |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Douglas Gregor | cd2c527 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1076 | if (isa<InitListExpr>(IList->getInit(Index))) |
| 1077 | CheckSubElementType(IList, Field->getType(), Index, StructuredList, |
| 1078 | StructuredIndex); |
| 1079 | else |
| 1080 | CheckImplicitInitList(IList, Field->getType(), Index, StructuredList, |
| 1081 | StructuredIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1082 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1084 | /// \brief Expand a field designator that refers to a member of an |
| 1085 | /// anonymous struct or union into a series of field designators that |
| 1086 | /// refers to the field within the appropriate subobject. |
| 1087 | /// |
| 1088 | /// Field/FieldIndex will be updated to point to the (new) |
| 1089 | /// currently-designated field. |
| 1090 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
| 1091 | DesignatedInitExpr *DIE, |
| 1092 | unsigned DesigIdx, |
| 1093 | FieldDecl *Field, |
| 1094 | RecordDecl::field_iterator &FieldIter, |
| 1095 | unsigned &FieldIndex) { |
| 1096 | typedef DesignatedInitExpr::Designator Designator; |
| 1097 | |
| 1098 | // Build the path from the current object to the member of the |
| 1099 | // anonymous struct/union (backwards). |
| 1100 | llvm::SmallVector<FieldDecl *, 4> Path; |
| 1101 | SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path); |
| 1102 | |
| 1103 | // Build the replacement designators. |
| 1104 | llvm::SmallVector<Designator, 4> Replacements; |
| 1105 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 1106 | FI = Path.rbegin(), FIEnd = Path.rend(); |
| 1107 | FI != FIEnd; ++FI) { |
| 1108 | if (FI + 1 == FIEnd) |
| 1109 | Replacements.push_back(Designator((IdentifierInfo *)0, |
| 1110 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1111 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1112 | else |
| 1113 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1114 | SourceLocation())); |
| 1115 | Replacements.back().setField(*FI); |
| 1116 | } |
| 1117 | |
| 1118 | // Expand the current designator into the set of replacement |
| 1119 | // designators, so we have a full subobject path down to where the |
| 1120 | // member of the anonymous struct/union is actually stored. |
| 1121 | DIE->ExpandDesignator(DesigIdx, &Replacements[0], |
| 1122 | &Replacements[0] + Replacements.size()); |
| 1123 | |
| 1124 | // Update FieldIter/FieldIndex; |
| 1125 | RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext()); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1126 | FieldIter = Record->field_begin(); |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1127 | FieldIndex = 0; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1128 | for (RecordDecl::field_iterator FEnd = Record->field_end(); |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1129 | FieldIter != FEnd; ++FieldIter) { |
| 1130 | if (FieldIter->isUnnamedBitfield()) |
| 1131 | continue; |
| 1132 | |
| 1133 | if (*FieldIter == Path.back()) |
| 1134 | return; |
| 1135 | |
| 1136 | ++FieldIndex; |
| 1137 | } |
| 1138 | |
| 1139 | assert(false && "Unable to find anonymous struct/union field"); |
| 1140 | } |
| 1141 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1142 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1143 | /// |
| 1144 | /// Determines whether the designated initializer @p DIE, which |
| 1145 | /// resides at the given @p Index within the initializer list @p |
| 1146 | /// IList, is well-formed for a current object of type @p DeclType |
| 1147 | /// (C99 6.7.8). The actual subobject that this designator refers to |
| 1148 | /// within the current subobject is returned in either |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1149 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1150 | /// |
| 1151 | /// @param IList The initializer list in which this designated |
| 1152 | /// initializer occurs. |
| 1153 | /// |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1154 | /// @param DIE The designated initializer expression. |
| 1155 | /// |
| 1156 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1157 | /// |
| 1158 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1159 | /// into which the designation in @p DIE should refer. |
| 1160 | /// |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1161 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1162 | /// a field, this will be set to the field declaration corresponding |
| 1163 | /// to the field named by the designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1164 | /// |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1165 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1166 | /// DIE is an array designator or GNU array-range designator, this |
| 1167 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1168 | /// |
| 1169 | /// @param Index Index into @p IList where the designated initializer |
| 1170 | /// @p DIE occurs. |
| 1171 | /// |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1172 | /// @param StructuredList The initializer list expression that |
| 1173 | /// describes all of the subobject initializers in the order they'll |
| 1174 | /// actually be initialized. |
| 1175 | /// |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1176 | /// @returns true if there was an error, false otherwise. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1177 | bool |
| 1178 | InitListChecker::CheckDesignatedInitializer(InitListExpr *IList, |
| 1179 | DesignatedInitExpr *DIE, |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1180 | unsigned DesigIdx, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1181 | QualType &CurrentObjectType, |
| 1182 | RecordDecl::field_iterator *NextField, |
| 1183 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1184 | unsigned &Index, |
| 1185 | InitListExpr *StructuredList, |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1186 | unsigned &StructuredIndex, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1187 | bool FinishSubobjectInit, |
| 1188 | bool TopLevelObject) { |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1189 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1190 | // Check the actual initialization for the designated object type. |
| 1191 | bool prevHadError = hadError; |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Temporarily remove the designator expression from the |
| 1194 | // initializer list that the child calls see, so that we don't try |
| 1195 | // to re-process the designator. |
| 1196 | unsigned OldIndex = Index; |
| 1197 | IList->setInit(OldIndex, DIE->getInit()); |
| 1198 | |
| 1199 | CheckSubElementType(IList, CurrentObjectType, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1200 | StructuredList, StructuredIndex); |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1201 | |
| 1202 | // Restore the designated initializer expression in the syntactic |
| 1203 | // form of the initializer list. |
| 1204 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1205 | DIE->setInit(IList->getInit(OldIndex)); |
| 1206 | IList->setInit(OldIndex, DIE); |
| 1207 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1208 | return hadError && !prevHadError; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1211 | bool IsFirstDesignator = (DesigIdx == 0); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1212 | assert((IsFirstDesignator || StructuredList) && |
| 1213 | "Need a non-designated initializer list to start from"); |
| 1214 | |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1215 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1216 | // Determine the structural initializer list that corresponds to the |
| 1217 | // current subobject. |
| 1218 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Douglas Gregor | ea765e1 | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1219 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1220 | StructuredList, StructuredIndex, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1221 | SourceRange(D->getStartLocation(), |
| 1222 | DIE->getSourceRange().getEnd())); |
| 1223 | assert(StructuredList && "Expected a structured initializer list"); |
| 1224 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1225 | if (D->isFieldDesignator()) { |
| 1226 | // C99 6.7.8p7: |
| 1227 | // |
| 1228 | // If a designator has the form |
| 1229 | // |
| 1230 | // . identifier |
| 1231 | // |
| 1232 | // then the current object (defined below) shall have |
| 1233 | // structure or union type and the identifier shall be the |
| 1234 | // name of a member of that type. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1235 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1236 | if (!RT) { |
| 1237 | SourceLocation Loc = D->getDotLoc(); |
| 1238 | if (Loc.isInvalid()) |
| 1239 | Loc = D->getFieldLoc(); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1240 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1241 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1242 | ++Index; |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1246 | // Note: we perform a linear search of the fields here, despite |
| 1247 | // the fact that we have a faster lookup method, because we always |
| 1248 | // need to compute the field's index. |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1249 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1250 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1251 | unsigned FieldIndex = 0; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1252 | RecordDecl::field_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1253 | Field = RT->getDecl()->field_begin(), |
| 1254 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1255 | for (; Field != FieldEnd; ++Field) { |
| 1256 | if (Field->isUnnamedBitfield()) |
| 1257 | continue; |
| 1258 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1259 | if (KnownField == *Field || Field->getIdentifier() == FieldName) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1260 | break; |
| 1261 | |
| 1262 | ++FieldIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1265 | if (Field == FieldEnd) { |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1266 | // There was no normal field in the struct with the designated |
| 1267 | // name. Perform another lookup for this name, which may find |
| 1268 | // something that we can't designate (e.g., a member function), |
| 1269 | // may find nothing, or may find a member of an anonymous |
| 1270 | // struct/union. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1271 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1272 | if (Lookup.first == Lookup.second) { |
| 1273 | // Name lookup didn't find anything. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1274 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1275 | << FieldName << CurrentObjectType; |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1276 | ++Index; |
| 1277 | return true; |
| 1278 | } else if (!KnownField && isa<FieldDecl>(*Lookup.first) && |
| 1279 | cast<RecordDecl>((*Lookup.first)->getDeclContext()) |
| 1280 | ->isAnonymousStructOrUnion()) { |
| 1281 | // Handle an field designator that refers to a member of an |
| 1282 | // anonymous struct or union. |
| 1283 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, |
| 1284 | cast<FieldDecl>(*Lookup.first), |
| 1285 | Field, FieldIndex); |
Eli Friedman | bd45b55 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1286 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1287 | } else { |
| 1288 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1289 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1290 | << FieldName; |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1291 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1292 | diag::note_field_designator_found); |
Eli Friedman | bd45b55 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1293 | ++Index; |
| 1294 | return true; |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } else if (!KnownField && |
| 1297 | cast<RecordDecl>((*Field)->getDeclContext()) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1298 | ->isAnonymousStructOrUnion()) { |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1299 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field, |
| 1300 | Field, FieldIndex); |
| 1301 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1302 | } |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1303 | |
| 1304 | // All of the fields of a union are located at the same place in |
| 1305 | // the initializer list. |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1306 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1307 | FieldIndex = 0; |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1308 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1309 | } |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1311 | // Update the designator with the field declaration. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1312 | D->setField(*Field); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1314 | // Make sure that our non-designated initializer list has space |
| 1315 | // for a subobject corresponding to this field. |
| 1316 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1317 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1318 | |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1319 | // This designator names a flexible array member. |
| 1320 | if (Field->getType()->isIncompleteArrayType()) { |
| 1321 | bool Invalid = false; |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1322 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1323 | // We can't designate an object within the flexible array |
| 1324 | // member (because GCC doesn't allow it). |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1325 | DesignatedInitExpr::Designator *NextD |
| 1326 | = DIE->getDesignator(DesigIdx + 1); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1327 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1328 | diag::err_designator_into_flexible_array_member) |
| 1329 | << SourceRange(NextD->getStartLocation(), |
| 1330 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1331 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1332 | << *Field; |
| 1333 | Invalid = true; |
| 1334 | } |
| 1335 | |
| 1336 | if (!hadError && !isa<InitListExpr>(DIE->getInit())) { |
| 1337 | // The initializer is not an initializer list. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1338 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1339 | diag::err_flexible_array_init_needs_braces) |
| 1340 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1341 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1342 | << *Field; |
| 1343 | Invalid = true; |
| 1344 | } |
| 1345 | |
| 1346 | // Handle GNU flexible array initializers. |
| 1347 | if (!Invalid && !TopLevelObject && |
| 1348 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1349 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1350 | diag::err_flexible_array_init_nonempty) |
| 1351 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1352 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1353 | << *Field; |
| 1354 | Invalid = true; |
| 1355 | } |
| 1356 | |
| 1357 | if (Invalid) { |
| 1358 | ++Index; |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
| 1362 | // Initialize the array. |
| 1363 | bool prevHadError = hadError; |
| 1364 | unsigned newStructuredIndex = FieldIndex; |
| 1365 | unsigned OldIndex = Index; |
| 1366 | IList->setInit(Index, DIE->getInit()); |
| 1367 | CheckSubElementType(IList, Field->getType(), Index, |
| 1368 | StructuredList, newStructuredIndex); |
| 1369 | IList->setInit(OldIndex, DIE); |
| 1370 | if (hadError && !prevHadError) { |
| 1371 | ++Field; |
| 1372 | ++FieldIndex; |
| 1373 | if (NextField) |
| 1374 | *NextField = Field; |
| 1375 | StructuredIndex = FieldIndex; |
| 1376 | return true; |
| 1377 | } |
| 1378 | } else { |
| 1379 | // Recurse to check later designated subobjects. |
| 1380 | QualType FieldType = (*Field)->getType(); |
| 1381 | unsigned newStructuredIndex = FieldIndex; |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1382 | if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0, |
| 1383 | Index, StructuredList, newStructuredIndex, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1384 | true, false)) |
| 1385 | return true; |
| 1386 | } |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Find the position of the next field to be initialized in this |
| 1389 | // subobject. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1390 | ++Field; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1391 | ++FieldIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1392 | |
| 1393 | // If this the first designator, our caller will continue checking |
| 1394 | // the rest of this struct/class/union subobject. |
| 1395 | if (IsFirstDesignator) { |
| 1396 | if (NextField) |
| 1397 | *NextField = Field; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1398 | StructuredIndex = FieldIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1399 | return false; |
| 1400 | } |
| 1401 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1402 | if (!FinishSubobjectInit) |
| 1403 | return false; |
| 1404 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1405 | // We've already initialized something in the union; we're done. |
| 1406 | if (RT->getDecl()->isUnion()) |
| 1407 | return hadError; |
| 1408 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1409 | // Check the remaining fields within this class/struct/union subobject. |
| 1410 | bool prevHadError = hadError; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1411 | CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index, |
| 1412 | StructuredList, FieldIndex); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1413 | return hadError && !prevHadError; |
| 1414 | } |
| 1415 | |
| 1416 | // C99 6.7.8p6: |
| 1417 | // |
| 1418 | // If a designator has the form |
| 1419 | // |
| 1420 | // [ constant-expression ] |
| 1421 | // |
| 1422 | // then the current object (defined below) shall have array |
| 1423 | // type and the expression shall be an integer constant |
| 1424 | // expression. If the array is of unknown size, any |
| 1425 | // nonnegative value is valid. |
| 1426 | // |
| 1427 | // Additionally, cope with the GNU extension that permits |
| 1428 | // designators of the form |
| 1429 | // |
| 1430 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1431 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1432 | if (!AT) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1433 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1434 | << CurrentObjectType; |
| 1435 | ++Index; |
| 1436 | return true; |
| 1437 | } |
| 1438 | |
| 1439 | Expr *IndexExpr = 0; |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1440 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1441 | if (D->isArrayDesignator()) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1442 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1443 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1444 | DesignatedEndIndex = DesignatedStartIndex; |
| 1445 | } else { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1446 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1449 | DesignatedStartIndex = |
| 1450 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
| 1451 | DesignatedEndIndex = |
| 1452 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1453 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1454 | |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1455 | if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue()) |
Douglas Gregor | 9fddded | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1456 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1459 | if (isa<ConstantArrayType>(AT)) { |
| 1460 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1461 | DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1462 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1463 | DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1464 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1465 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1466 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1467 | diag::err_array_designator_too_large) |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1468 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1469 | << IndexExpr->getSourceRange(); |
| 1470 | ++Index; |
| 1471 | return true; |
| 1472 | } |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1473 | } else { |
| 1474 | // Make sure the bit-widths and signedness match. |
| 1475 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
| 1476 | DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1477 | else if (DesignatedStartIndex.getBitWidth() < |
| 1478 | DesignatedEndIndex.getBitWidth()) |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1479 | DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
| 1480 | DesignatedStartIndex.setIsUnsigned(true); |
| 1481 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1484 | // Make sure that our non-designated initializer list has space |
| 1485 | // for a subobject corresponding to this array element. |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1486 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1487 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1488 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1489 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1490 | // Repeatedly perform subobject initializations in the range |
| 1491 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1493 | // Move to the next designator |
| 1494 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1495 | unsigned OldIndex = Index; |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1496 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1497 | // Recurse to check later designated subobjects. |
| 1498 | QualType ElementType = AT->getElementType(); |
| 1499 | Index = OldIndex; |
Douglas Gregor | aa35727 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1500 | if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0, |
| 1501 | Index, StructuredList, ElementIndex, |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1502 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1503 | false)) |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1504 | return true; |
| 1505 | |
| 1506 | // Move to the next index in the array that we'll be initializing. |
| 1507 | ++DesignatedStartIndex; |
| 1508 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1509 | } |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1510 | |
| 1511 | // If this the first designator, our caller will continue checking |
| 1512 | // the rest of this array subobject. |
| 1513 | if (IsFirstDesignator) { |
| 1514 | if (NextElementIndex) |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1515 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1516 | StructuredIndex = ElementIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1517 | return false; |
| 1518 | } |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (!FinishSubobjectInit) |
| 1521 | return false; |
| 1522 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1523 | // Check the remaining elements within this array subobject. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1524 | bool prevHadError = hadError; |
Douglas Gregor | d7e76c5 | 2009-02-09 19:45:19 +0000 | [diff] [blame] | 1525 | CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1526 | StructuredList, ElementIndex); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1527 | return hadError && !prevHadError; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1530 | // Get the structured initializer list for a subobject of type |
| 1531 | // @p CurrentObjectType. |
| 1532 | InitListExpr * |
| 1533 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1534 | QualType CurrentObjectType, |
| 1535 | InitListExpr *StructuredList, |
| 1536 | unsigned StructuredIndex, |
| 1537 | SourceRange InitRange) { |
| 1538 | Expr *ExistingInit = 0; |
| 1539 | if (!StructuredList) |
| 1540 | ExistingInit = SyntacticToSemantic[IList]; |
| 1541 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1542 | ExistingInit = StructuredList->getInit(StructuredIndex); |
| 1543 | |
| 1544 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1545 | return Result; |
| 1546 | |
| 1547 | if (ExistingInit) { |
| 1548 | // We are creating an initializer list that initializes the |
| 1549 | // subobjects of the current object, but there was already an |
| 1550 | // initialization that completely initialized the current |
| 1551 | // subobject, e.g., by a compound literal: |
| 1552 | // |
| 1553 | // struct X { int a, b; }; |
| 1554 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 1555 | // |
| 1556 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1557 | // designated initializer re-initializes the whole |
| 1558 | // subobject [0], overwriting previous initializers. |
Douglas Gregor | ea765e1 | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1559 | SemaRef.Diag(InitRange.getBegin(), |
| 1560 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1561 | << InitRange; |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1562 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1563 | diag::note_previous_initializer) |
Douglas Gregor | 756283b | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1564 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1565 | << ExistingInit->getSourceRange(); |
| 1566 | } |
| 1567 | |
| 1568 | InitListExpr *Result |
Douglas Gregor | ea765e1 | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1569 | = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0, |
| 1570 | InitRange.getEnd()); |
| 1571 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1572 | Result->setType(CurrentObjectType); |
| 1573 | |
Douglas Gregor | ee0792c | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1574 | // Pre-allocate storage for the structured initializer list. |
| 1575 | unsigned NumElements = 0; |
Douglas Gregor | 1e5c776 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1576 | unsigned NumInits = 0; |
| 1577 | if (!StructuredList) |
| 1578 | NumInits = IList->getNumInits(); |
| 1579 | else if (Index < IList->getNumInits()) { |
| 1580 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1581 | NumInits = SubList->getNumInits(); |
| 1582 | } |
| 1583 | |
Douglas Gregor | ee0792c | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1584 | if (const ArrayType *AType |
| 1585 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1586 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1587 | NumElements = CAType->getSize().getZExtValue(); |
| 1588 | // Simple heuristic so that we don't allocate a very large |
| 1589 | // initializer with many empty entries at the end. |
Douglas Gregor | 1e5c776 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1590 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | ee0792c | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1591 | NumElements = 0; |
| 1592 | } |
| 1593 | } else if (const VectorType *VType = CurrentObjectType->getAsVectorType()) |
| 1594 | NumElements = VType->getNumElements(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1595 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | ee0792c | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1596 | RecordDecl *RDecl = RType->getDecl(); |
| 1597 | if (RDecl->isUnion()) |
| 1598 | NumElements = 1; |
| 1599 | else |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1600 | NumElements = std::distance(RDecl->field_begin(), |
| 1601 | RDecl->field_end()); |
Douglas Gregor | ee0792c | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Douglas Gregor | 1e5c776 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1604 | if (NumElements < NumInits) |
Douglas Gregor | ee0792c | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1605 | NumElements = IList->getNumInits(); |
| 1606 | |
| 1607 | Result->reserveInits(NumElements); |
| 1608 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1609 | // Link this new initializer list into the structured initializer |
| 1610 | // lists. |
| 1611 | if (StructuredList) |
| 1612 | StructuredList->updateInit(StructuredIndex, Result); |
| 1613 | else { |
| 1614 | Result->setSyntacticForm(IList); |
| 1615 | SyntacticToSemantic[IList] = Result; |
| 1616 | } |
| 1617 | |
| 1618 | return Result; |
| 1619 | } |
| 1620 | |
| 1621 | /// Update the initializer at index @p StructuredIndex within the |
| 1622 | /// structured initializer list to the value @p expr. |
| 1623 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1624 | unsigned &StructuredIndex, |
| 1625 | Expr *expr) { |
| 1626 | // No structured initializer list to update |
| 1627 | if (!StructuredList) |
| 1628 | return; |
| 1629 | |
| 1630 | if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) { |
| 1631 | // This initializer overwrites a previous initializer. Warn. |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1632 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1633 | diag::warn_initializer_overrides) |
| 1634 | << expr->getSourceRange(); |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1635 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1636 | diag::note_previous_initializer) |
Douglas Gregor | 756283b | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1637 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1638 | << PrevInit->getSourceRange(); |
| 1639 | } |
| 1640 | |
| 1641 | ++StructuredIndex; |
| 1642 | } |
| 1643 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1644 | /// Check that the given Index expression is a valid array designator |
| 1645 | /// value. This is essentailly just a wrapper around |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1646 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1647 | /// and produces a reasonable diagnostic if there is a |
| 1648 | /// failure. Returns true if there was an error, false otherwise. If |
| 1649 | /// everything went okay, Value will receive the value of the constant |
| 1650 | /// expression. |
| 1651 | static bool |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1652 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1653 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1654 | |
| 1655 | // Make sure this is an integer constant expression. |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1656 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1657 | return true; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1658 | |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1659 | if (Value.isSigned() && Value.isNegative()) |
| 1660 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1661 | << Value.toString(10) << Index->getSourceRange(); |
| 1662 | |
Douglas Gregor | e498e37 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1663 | Value.setIsUnsigned(true); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1664 | return false; |
| 1665 | } |
| 1666 | |
| 1667 | Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 1668 | SourceLocation Loc, |
Douglas Gregor | 5f34f0e | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 1669 | bool GNUSyntax, |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1670 | OwningExprResult Init) { |
| 1671 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1672 | |
| 1673 | bool Invalid = false; |
| 1674 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1675 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1676 | |
| 1677 | // Build designators and check array designator expressions. |
| 1678 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1679 | const Designator &D = Desig.getDesignator(Idx); |
| 1680 | switch (D.getKind()) { |
| 1681 | case Designator::FieldDesignator: |
| 1682 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
| 1683 | D.getFieldLoc())); |
| 1684 | break; |
| 1685 | |
| 1686 | case Designator::ArrayDesignator: { |
| 1687 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1688 | llvm::APSInt IndexValue; |
Douglas Gregor | 3a7a06e | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1689 | if (!Index->isTypeDependent() && |
| 1690 | !Index->isValueDependent() && |
| 1691 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1692 | Invalid = true; |
| 1693 | else { |
| 1694 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 1695 | D.getLBracketLoc(), |
| 1696 | D.getRBracketLoc())); |
| 1697 | InitExpressions.push_back(Index); |
| 1698 | } |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | case Designator::ArrayRangeDesignator: { |
| 1703 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1704 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1705 | llvm::APSInt StartValue; |
| 1706 | llvm::APSInt EndValue; |
Douglas Gregor | 3a7a06e | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1707 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1708 | StartIndex->isValueDependent(); |
| 1709 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1710 | EndIndex->isValueDependent(); |
| 1711 | if ((!StartDependent && |
| 1712 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1713 | (!EndDependent && |
| 1714 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1715 | Invalid = true; |
Douglas Gregor | ea0528d | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1716 | else { |
| 1717 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 3a7a06e | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1718 | if (StartDependent || EndDependent) { |
| 1719 | // Nothing to compute. |
| 1720 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Douglas Gregor | ea0528d | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1721 | EndValue.extend(StartValue.getBitWidth()); |
| 1722 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 1723 | StartValue.extend(EndValue.getBitWidth()); |
| 1724 | |
Douglas Gregor | 1401c06 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1725 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | ea0528d | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1726 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
| 1727 | << StartValue.toString(10) << EndValue.toString(10) |
| 1728 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1729 | Invalid = true; |
| 1730 | } else { |
| 1731 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 1732 | D.getLBracketLoc(), |
| 1733 | D.getEllipsisLoc(), |
| 1734 | D.getRBracketLoc())); |
| 1735 | InitExpressions.push_back(StartIndex); |
| 1736 | InitExpressions.push_back(EndIndex); |
| 1737 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1738 | } |
| 1739 | break; |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | if (Invalid || Init.isInvalid()) |
| 1745 | return ExprError(); |
| 1746 | |
| 1747 | // Clear out the expressions within the designation. |
| 1748 | Desig.ClearExprs(*this); |
| 1749 | |
| 1750 | DesignatedInitExpr *DIE |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1751 | = DesignatedInitExpr::Create(Context, |
| 1752 | Designators.data(), Designators.size(), |
| 1753 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1754 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1755 | return Owned(DIE); |
| 1756 | } |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1757 | |
| 1758 | bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) { |
Chris Lattner | 2e2766a | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1759 | InitListChecker CheckInitList(*this, InitList, DeclType); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1760 | if (!CheckInitList.HadError()) |
| 1761 | InitList = CheckInitList.getFullyStructuredList(); |
| 1762 | |
| 1763 | return CheckInitList.HadError(); |
| 1764 | } |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1765 | |
| 1766 | /// \brief Diagnose any semantic errors with value-initialization of |
| 1767 | /// the given type. |
| 1768 | /// |
| 1769 | /// Value-initialization effectively zero-initializes any types |
| 1770 | /// without user-declared constructors, and calls the default |
| 1771 | /// constructor for a for any type that has a user-declared |
| 1772 | /// constructor (C++ [dcl.init]p5). Value-initialization can fail when |
| 1773 | /// a type with a user-declared constructor does not have an |
| 1774 | /// accessible, non-deleted default constructor. In C, everything can |
| 1775 | /// be value-initialized, which corresponds to C's notion of |
| 1776 | /// initializing objects with static storage duration when no |
| 1777 | /// initializer is provided for that object. |
| 1778 | /// |
| 1779 | /// \returns true if there was an error, false otherwise. |
| 1780 | bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) { |
| 1781 | // C++ [dcl.init]p5: |
| 1782 | // |
| 1783 | // To value-initialize an object of type T means: |
| 1784 | |
| 1785 | // -- if T is an array type, then each element is value-initialized; |
| 1786 | if (const ArrayType *AT = Context.getAsArrayType(Type)) |
| 1787 | return CheckValueInitialization(AT->getElementType(), Loc); |
| 1788 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1789 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
Douglas Gregor | 2e04759 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1790 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1791 | // -- if T is a class type (clause 9) with a user-declared |
| 1792 | // constructor (12.1), then the default constructor for T is |
| 1793 | // called (and the initialization is ill-formed if T has no |
| 1794 | // accessible default constructor); |
Douglas Gregor | 2e04759 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1795 | if (ClassDecl->hasUserDeclaredConstructor()) |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1796 | // FIXME: Eventually, we'll need to put the constructor decl into the |
| 1797 | // AST. |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1798 | return PerformInitializationByConstructor(Type, 0, 0, Loc, |
| 1799 | SourceRange(Loc), |
| 1800 | DeclarationName(), |
| 1801 | IK_Direct); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | if (Type->isReferenceType()) { |
| 1806 | // C++ [dcl.init]p5: |
| 1807 | // [...] A program that calls for default-initialization or |
| 1808 | // value-initialization of an entity of reference type is |
| 1809 | // ill-formed. [...] |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1810 | // FIXME: Once we have code that goes through this path, add an actual |
| 1811 | // diagnostic :) |
Douglas Gregor | 538a4c2 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | return false; |
| 1815 | } |