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