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