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