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