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