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