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