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