Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "Sema.h" |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Designator.h" |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 20 | #include <map> |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 21 | using namespace clang; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 22 | |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 23 | /// @brief Semantic checking for initializer lists. |
| 24 | /// |
| 25 | /// The InitListChecker class contains a set of routines that each |
| 26 | /// handle the initialization of a certain kind of entity, e.g., |
| 27 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 28 | /// InitListChecker itself performs a recursive walk of the subobject |
| 29 | /// structure of the type to be initialized, while stepping through |
| 30 | /// the initializer list one element at a time. The IList and Index |
| 31 | /// parameters to each of the Check* routines contain the active |
| 32 | /// (syntactic) initializer list and the index into that initializer |
| 33 | /// list that represents the current initializer. Each routine is |
| 34 | /// responsible for moving that Index forward as it consumes elements. |
| 35 | /// |
| 36 | /// Each Check* routine also has a StructuredList/StructuredIndex |
| 37 | /// arguments, which contains the current the "structured" (semantic) |
| 38 | /// initializer list and the index into that initializer list where we |
| 39 | /// are copying initializers as we map them over to the semantic |
| 40 | /// list. Once we have completed our recursive walk of the subobject |
| 41 | /// structure, we will have constructed a full semantic initializer |
| 42 | /// list. |
| 43 | /// |
| 44 | /// C99 designators cause changes in the initializer list traversal, |
| 45 | /// because they make the initialization "jump" into a specific |
| 46 | /// subobject and then continue the initialization from that |
| 47 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 48 | /// designated subobject and manages backing out the recursion to |
| 49 | /// initialize the subobjects after the one designated. |
Chris Lattner | 1aa25a7 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 50 | namespace clang { |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 51 | class InitListChecker { |
| 52 | Sema *SemaRef; |
| 53 | bool hadError; |
| 54 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 55 | InitListExpr *FullyStructuredList; |
| 56 | |
| 57 | void CheckImplicitInitList(InitListExpr *ParentIList, QualType T, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 58 | unsigned &Index, InitListExpr *StructuredList, |
| 59 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 60 | void CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 61 | unsigned &Index, InitListExpr *StructuredList, |
| 62 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 63 | void CheckListElementTypes(InitListExpr *IList, QualType &DeclType, |
| 64 | bool SubobjectIsDesignatorContext, |
| 65 | unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 66 | InitListExpr *StructuredList, |
| 67 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 68 | void CheckSubElementType(InitListExpr *IList, QualType ElemType, |
| 69 | unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 70 | InitListExpr *StructuredList, |
| 71 | unsigned &StructuredIndex); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 72 | void CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 73 | unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 74 | InitListExpr *StructuredList, |
| 75 | unsigned &StructuredIndex); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 76 | void CheckReferenceType(InitListExpr *IList, QualType DeclType, |
| 77 | unsigned &Index, |
| 78 | InitListExpr *StructuredList, |
| 79 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 80 | void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 81 | InitListExpr *StructuredList, |
| 82 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 83 | void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType, |
| 84 | RecordDecl::field_iterator Field, |
| 85 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 86 | InitListExpr *StructuredList, |
| 87 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 88 | void CheckArrayType(InitListExpr *IList, QualType &DeclType, |
| 89 | llvm::APSInt elementIndex, |
| 90 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 91 | InitListExpr *StructuredList, |
| 92 | unsigned &StructuredIndex); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 93 | bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE, |
| 94 | DesignatedInitExpr::designators_iterator D, |
| 95 | QualType &CurrentObjectType, |
| 96 | RecordDecl::field_iterator *NextField, |
| 97 | llvm::APSInt *NextElementIndex, |
| 98 | unsigned &Index, |
| 99 | InitListExpr *StructuredList, |
| 100 | unsigned &StructuredIndex, |
| 101 | bool FinishSubobjectInit = true); |
| 102 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 103 | QualType CurrentObjectType, |
| 104 | InitListExpr *StructuredList, |
| 105 | unsigned StructuredIndex, |
| 106 | SourceRange InitRange); |
Douglas Gregor | aaa2096 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 107 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 108 | unsigned &StructuredIndex, |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 109 | Expr *expr); |
| 110 | int numArrayElements(QualType DeclType); |
| 111 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 112 | |
| 113 | void FillInValueInitializations(InitListExpr *ILE); |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 114 | public: |
| 115 | InitListChecker(Sema *S, InitListExpr *IL, QualType &T); |
| 116 | bool HadError() { return hadError; } |
| 117 | |
| 118 | // @brief Retrieves the fully-structured initializer list used for |
| 119 | // semantic analysis and code generation. |
| 120 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 121 | }; |
Chris Lattner | 1aa25a7 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 124 | /// Recursively replaces NULL values within the given initializer list |
| 125 | /// with expressions that perform value-initialization of the |
| 126 | /// appropriate type. |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 127 | void InitListChecker::FillInValueInitializations(InitListExpr *ILE) { |
| 128 | assert((ILE->getType() != SemaRef->Context.VoidTy) && |
| 129 | "Should not have void type"); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 130 | if (const RecordType *RType = ILE->getType()->getAsRecordType()) { |
| 131 | unsigned Init = 0, NumInits = ILE->getNumInits(); |
| 132 | for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(), |
| 133 | FieldEnd = RType->getDecl()->field_end(); |
| 134 | Field != FieldEnd; ++Field) { |
| 135 | if (Field->isUnnamedBitfield()) |
| 136 | continue; |
| 137 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 138 | if (Init >= NumInits) { |
| 139 | if (Field->getType()->isReferenceType()) { |
| 140 | // C++ [dcl.init.aggr]p9: |
| 141 | // If an incomplete or empty initializer-list leaves a |
| 142 | // member of reference type uninitialized, the program is |
| 143 | // ill-formed. |
| 144 | SemaRef->Diag(ILE->getSyntacticForm()->getLocStart(), |
| 145 | diag::err_init_reference_member_uninitialized) |
| 146 | << Field->getType() |
| 147 | << ILE->getSyntacticForm()->getSourceRange(); |
| 148 | SemaRef->Diag(Field->getLocation(), |
| 149 | diag::note_uninit_reference_member); |
| 150 | hadError = true; |
| 151 | } |
| 152 | } else if (!ILE->getInit(Init)) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 153 | ILE->setInit(Init, |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 154 | new (SemaRef->Context) ImplicitValueInitExpr(Field->getType())); |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 155 | else if (InitListExpr *InnerILE |
| 156 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 157 | FillInValueInitializations(InnerILE); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 158 | ++Init; |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 159 | |
| 160 | // Only look at the first initialization of a union. |
| 161 | if (RType->getDecl()->isUnion()) |
| 162 | break; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | QualType ElementType; |
| 169 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 170 | if (const ArrayType *AType = SemaRef->Context.getAsArrayType(ILE->getType())) |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 171 | ElementType = AType->getElementType(); |
| 172 | else if (const VectorType *VType = ILE->getType()->getAsVectorType()) |
| 173 | ElementType = VType->getElementType(); |
| 174 | else |
| 175 | ElementType = ILE->getType(); |
| 176 | |
| 177 | for (unsigned Init = 0, NumInits = ILE->getNumInits(); Init != NumInits; |
| 178 | ++Init) { |
| 179 | if (!ILE->getInit(Init)) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 180 | ILE->setInit(Init, |
| 181 | new (SemaRef->Context) ImplicitValueInitExpr(ElementType)); |
Chris Lattner | 1aa25a7 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 182 | else if (InitListExpr *InnerILE =dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 183 | FillInValueInitializations(InnerILE); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Chris Lattner | 1aa25a7 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 187 | |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 188 | InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) { |
| 189 | hadError = false; |
| 190 | SemaRef = S; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 191 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 192 | unsigned newIndex = 0; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 193 | unsigned newStructuredIndex = 0; |
| 194 | FullyStructuredList |
| 195 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, SourceRange()); |
| 196 | CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 197 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 198 | if (!hadError) |
| 199 | FillInValueInitializations(FullyStructuredList); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 203 | // FIXME: use a proper constant |
| 204 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 205 | if (const ConstantArrayType *CAT = |
| 206 | SemaRef->Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 207 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 208 | } |
| 209 | return maxElements; |
| 210 | } |
| 211 | |
| 212 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
| 213 | RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 214 | int InitializableMembers = 0; |
| 215 | for (RecordDecl::field_iterator Field = structDecl->field_begin(), |
| 216 | FieldEnd = structDecl->field_end(); |
| 217 | Field != FieldEnd; ++Field) { |
| 218 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 219 | ++InitializableMembers; |
| 220 | } |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 221 | if (structDecl->isUnion()) |
Eli Friedman | 9f5250b | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 222 | return std::min(InitializableMembers, 1); |
| 223 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 227 | QualType T, unsigned &Index, |
| 228 | InitListExpr *StructuredList, |
| 229 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 230 | int maxElements = 0; |
| 231 | |
| 232 | if (T->isArrayType()) |
| 233 | maxElements = numArrayElements(T); |
| 234 | else if (T->isStructureType() || T->isUnionType()) |
| 235 | maxElements = numStructUnionElements(T); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 236 | else if (T->isVectorType()) |
| 237 | maxElements = T->getAsVectorType()->getNumElements(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 238 | else |
| 239 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 240 | |
Eli Friedman | f8df28c | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 241 | if (maxElements == 0) { |
| 242 | SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(), |
| 243 | diag::err_implicit_empty_initializer); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 244 | ++Index; |
Eli Friedman | f8df28c | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 245 | hadError = true; |
| 246 | return; |
| 247 | } |
| 248 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 249 | // Build a structured initializer list corresponding to this subobject. |
| 250 | InitListExpr *StructuredSubobjectInitList |
| 251 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 252 | StructuredIndex, |
| 253 | ParentIList->getInit(Index)->getSourceRange()); |
| 254 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 256 | // Check the element types and build the structural subobject. |
| 257 | CheckListElementTypes(ParentIList, T, false, Index, |
| 258 | StructuredSubobjectInitList, |
| 259 | StructuredSubobjectInitIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Steve Naroff | 5609952 | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 262 | void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 263 | unsigned &Index, |
| 264 | InitListExpr *StructuredList, |
| 265 | unsigned &StructuredIndex) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 266 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 267 | SyntacticToSemantic[IList] = StructuredList; |
| 268 | StructuredList->setSyntacticForm(IList); |
| 269 | CheckListElementTypes(IList, T, true, Index, StructuredList, |
| 270 | StructuredIndex); |
Steve Naroff | 5609952 | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 271 | IList->setType(T); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 272 | StructuredList->setType(T); |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 273 | if (hadError) |
| 274 | return; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 275 | |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 276 | if (Index < IList->getNumInits()) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 277 | // We have leftover initializers |
| 278 | if (IList->getNumInits() > 0 && |
| 279 | SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) { |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 280 | // Special-case |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 281 | SemaRef->Diag(IList->getInit(Index)->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 282 | diag::err_excess_initializers_in_char_array_initializer) |
| 283 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 284 | hadError = true; |
Eli Friedman | b9ea6bc | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 285 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | 09f078c | 2009-01-30 22:26:29 +0000 | [diff] [blame^] | 286 | // Don't complain for incomplete types, since we'll get an error |
| 287 | // elsewhere |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 288 | SemaRef->Diag(IList->getInit(Index)->getLocStart(), |
Douglas Gregor | 09f078c | 2009-01-30 22:26:29 +0000 | [diff] [blame^] | 289 | diag::err_excess_initializers) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 290 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Eli Friedman | 455f762 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 293 | |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 294 | if (T->isScalarType()) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 295 | SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
| 296 | << IList->getSourceRange(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 299 | void InitListChecker::CheckListElementTypes(InitListExpr *IList, |
| 300 | QualType &DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 301 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 302 | unsigned &Index, |
| 303 | InitListExpr *StructuredList, |
| 304 | unsigned &StructuredIndex) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 305 | if (DeclType->isScalarType()) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 306 | CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 307 | } else if (DeclType->isVectorType()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 308 | CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | e7ef500 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 309 | } else if (DeclType->isAggregateType()) { |
| 310 | if (DeclType->isRecordType()) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 311 | RecordDecl *RD = DeclType->getAsRecordType()->getDecl(); |
| 312 | CheckStructUnionTypes(IList, DeclType, RD->field_begin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 313 | SubobjectIsDesignatorContext, Index, |
| 314 | StructuredList, StructuredIndex); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 315 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 316 | llvm::APSInt Zero( |
| 317 | SemaRef->Context.getTypeSize(SemaRef->Context.getSizeType()), |
| 318 | false); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 319 | CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index, |
| 320 | StructuredList, StructuredIndex); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 321 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 322 | else |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 323 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | ff5b3a8 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 324 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 325 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 326 | ++Index; |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 327 | SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 328 | << DeclType; |
Eli Friedman | b9ea6bc | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 329 | hadError = true; |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 330 | } else if (DeclType->isRecordType()) { |
| 331 | // C++ [dcl.init]p14: |
| 332 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 333 | // is a brace-enclosed list, see 8.5.1. |
| 334 | // |
| 335 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 336 | // we have an initializer list and a destination type that is not |
| 337 | // an aggregate. |
| 338 | // FIXME: In C++0x, this is yet another form of initialization. |
| 339 | SemaRef->Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 340 | << DeclType << IList->getSourceRange(); |
| 341 | hadError = true; |
| 342 | } else if (DeclType->isReferenceType()) { |
| 343 | CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 344 | } else { |
| 345 | // In C, all types are either scalars or aggregates, but |
| 346 | // additional handling is needed here for C++ (and possibly others?). |
| 347 | assert(0 && "Unsupported initializer type"); |
| 348 | } |
| 349 | } |
| 350 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 351 | void InitListChecker::CheckSubElementType(InitListExpr *IList, |
| 352 | QualType ElemType, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 353 | unsigned &Index, |
| 354 | InitListExpr *StructuredList, |
| 355 | unsigned &StructuredIndex) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 356 | Expr *expr = IList->getInit(Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 357 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 358 | unsigned newIndex = 0; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 359 | unsigned newStructuredIndex = 0; |
| 360 | InitListExpr *newStructuredList |
| 361 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 362 | StructuredList, StructuredIndex, |
| 363 | SubInitList->getSourceRange()); |
| 364 | CheckExplicitInitList(SubInitList, ElemType, newIndex, |
| 365 | newStructuredList, newStructuredIndex); |
| 366 | ++StructuredIndex; |
| 367 | ++Index; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 368 | } else if (StringLiteral *lit = |
| 369 | SemaRef->IsStringLiteralInit(expr, ElemType)) { |
| 370 | SemaRef->CheckStringLiteralInit(lit, ElemType); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 371 | UpdateStructuredListElement(StructuredList, StructuredIndex, lit); |
| 372 | ++Index; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 373 | } else if (ElemType->isScalarType()) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 374 | CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 375 | } else if (ElemType->isReferenceType()) { |
| 376 | CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 377 | } else { |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 378 | if (SemaRef->getLangOptions().CPlusPlus) { |
| 379 | // C++ [dcl.init.aggr]p12: |
| 380 | // All implicit type conversions (clause 4) are considered when |
| 381 | // initializing the aggregate member with an ini- tializer from |
| 382 | // an initializer-list. If the initializer can initialize a |
| 383 | // member, the member is initialized. [...] |
| 384 | ImplicitConversionSequence ICS |
| 385 | = SemaRef->TryCopyInitialization(expr, ElemType); |
| 386 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) { |
| 387 | if (SemaRef->PerformImplicitConversion(expr, ElemType, ICS, |
| 388 | "initializing")) |
| 389 | hadError = true; |
| 390 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 391 | ++Index; |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | // Fall through for subaggregate initialization |
| 396 | } else { |
| 397 | // C99 6.7.8p13: |
| 398 | // |
| 399 | // The initializer for a structure or union object that has |
| 400 | // automatic storage duration shall be either an initializer |
| 401 | // list as described below, or a single expression that has |
| 402 | // compatible structure or union type. In the latter case, the |
| 403 | // initial value of the object, including unnamed members, is |
| 404 | // that of the expression. |
| 405 | QualType ExprType = SemaRef->Context.getCanonicalType(expr->getType()); |
| 406 | QualType ElemTypeCanon = SemaRef->Context.getCanonicalType(ElemType); |
| 407 | if (SemaRef->Context.typesAreCompatible(ExprType.getUnqualifiedType(), |
| 408 | ElemTypeCanon.getUnqualifiedType())) { |
| 409 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 410 | ++Index; |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | // Fall through for subaggregate initialization |
| 415 | } |
| 416 | |
| 417 | // C++ [dcl.init.aggr]p12: |
| 418 | // |
| 419 | // [...] Otherwise, if the member is itself a non-empty |
| 420 | // subaggregate, brace elision is assumed and the initializer is |
| 421 | // considered for the initialization of the first member of |
| 422 | // the subaggregate. |
| 423 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
| 424 | CheckImplicitInitList(IList, ElemType, Index, StructuredList, |
| 425 | StructuredIndex); |
| 426 | ++StructuredIndex; |
| 427 | } else { |
| 428 | // We cannot initialize this element, so let |
| 429 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 430 | SemaRef->PerformCopyInitialization(expr, ElemType, "initializing"); |
| 431 | hadError = true; |
| 432 | ++Index; |
| 433 | ++StructuredIndex; |
| 434 | } |
| 435 | } |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 438 | void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 439 | unsigned &Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 440 | InitListExpr *StructuredList, |
| 441 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 442 | if (Index < IList->getNumInits()) { |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 443 | Expr *expr = IList->getInit(Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 444 | if (isa<InitListExpr>(expr)) { |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 445 | SemaRef->Diag(IList->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 446 | diag::err_many_braces_around_scalar_init) |
| 447 | << IList->getSourceRange(); |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 448 | hadError = true; |
| 449 | ++Index; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 450 | ++StructuredIndex; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 451 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 452 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 453 | SemaRef->Diag(expr->getSourceRange().getBegin(), |
| 454 | diag::err_designator_for_scalar_init) |
| 455 | << DeclType << expr->getSourceRange(); |
| 456 | hadError = true; |
| 457 | ++Index; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 458 | ++StructuredIndex; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 459 | return; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 460 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 461 | |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 462 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 463 | if (SemaRef->CheckSingleInitializer(expr, DeclType, false)) |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 464 | hadError = true; // types weren't compatible. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 465 | else if (savExpr != expr) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 466 | // The type was promoted, update initializer list. |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 467 | IList->setInit(Index, expr); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 468 | } |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 469 | if (hadError) |
| 470 | ++StructuredIndex; |
| 471 | else |
| 472 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 473 | ++Index; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 474 | } else { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 475 | SemaRef->Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
| 476 | << IList->getSourceRange(); |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 477 | hadError = true; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 478 | ++Index; |
| 479 | ++StructuredIndex; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 480 | return; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 481 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Douglas Gregor | d45210d | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 484 | void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType, |
| 485 | unsigned &Index, |
| 486 | InitListExpr *StructuredList, |
| 487 | unsigned &StructuredIndex) { |
| 488 | if (Index < IList->getNumInits()) { |
| 489 | Expr *expr = IList->getInit(Index); |
| 490 | if (isa<InitListExpr>(expr)) { |
| 491 | SemaRef->Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 492 | << DeclType << IList->getSourceRange(); |
| 493 | hadError = true; |
| 494 | ++Index; |
| 495 | ++StructuredIndex; |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
| 500 | if (SemaRef->CheckReferenceInit(expr, DeclType)) |
| 501 | hadError = true; |
| 502 | else if (savExpr != expr) { |
| 503 | // The type was promoted, update initializer list. |
| 504 | IList->setInit(Index, expr); |
| 505 | } |
| 506 | if (hadError) |
| 507 | ++StructuredIndex; |
| 508 | else |
| 509 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 510 | ++Index; |
| 511 | } else { |
| 512 | // FIXME: It would be wonderful if we could point at the actual |
| 513 | // member. In general, it would be useful to pass location |
| 514 | // information down the stack, so that we know the location (or |
| 515 | // decl) of the "current object" being initialized. |
| 516 | SemaRef->Diag(IList->getLocStart(), |
| 517 | diag::err_init_reference_member_uninitialized) |
| 518 | << DeclType |
| 519 | << IList->getSourceRange(); |
| 520 | hadError = true; |
| 521 | ++Index; |
| 522 | ++StructuredIndex; |
| 523 | return; |
| 524 | } |
| 525 | } |
| 526 | |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 527 | void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 528 | unsigned &Index, |
| 529 | InitListExpr *StructuredList, |
| 530 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 531 | if (Index < IList->getNumInits()) { |
| 532 | const VectorType *VT = DeclType->getAsVectorType(); |
| 533 | int maxElements = VT->getNumElements(); |
| 534 | QualType elementType = VT->getElementType(); |
| 535 | |
| 536 | for (int i = 0; i < maxElements; ++i) { |
| 537 | // Don't attempt to go past the end of the init list |
| 538 | if (Index >= IList->getNumInits()) |
| 539 | break; |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 540 | CheckSubElementType(IList, elementType, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 541 | StructuredList, StructuredIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 547 | llvm::APSInt elementIndex, |
| 548 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 549 | unsigned &Index, |
| 550 | InitListExpr *StructuredList, |
| 551 | unsigned &StructuredIndex) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 552 | // Check for the special-case of initializing an array with a string. |
| 553 | if (Index < IList->getNumInits()) { |
| 554 | if (StringLiteral *lit = |
| 555 | SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) { |
| 556 | SemaRef->CheckStringLiteralInit(lit, DeclType); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 557 | // We place the string literal directly into the resulting |
| 558 | // initializer list. This is the only place where the structure |
| 559 | // of the structured initializer list doesn't match exactly, |
| 560 | // because doing so would involve allocating one character |
| 561 | // constant for each string. |
| 562 | UpdateStructuredListElement(StructuredList, StructuredIndex, lit); |
| 563 | StructuredList->resizeInits(SemaRef->Context, StructuredIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 564 | ++Index; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | } |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 568 | if (const VariableArrayType *VAT = |
| 569 | SemaRef->Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 570 | // Check for VLAs; in standard C it would be possible to check this |
| 571 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 572 | // them in all sorts of strange places). |
| 573 | SemaRef->Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 574 | diag::err_variable_object_no_init) |
| 575 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 576 | hadError = true; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 577 | ++Index; |
| 578 | ++StructuredIndex; |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 582 | // We might know the maximum number of elements in advance. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 583 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 584 | elementIndex.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 585 | bool maxElementsKnown = false; |
| 586 | if (const ConstantArrayType *CAT = |
| 587 | SemaRef->Context.getAsConstantArrayType(DeclType)) { |
| 588 | maxElements = CAT->getSize(); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 589 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 590 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 591 | maxElementsKnown = true; |
| 592 | } |
| 593 | |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 594 | QualType elementType = SemaRef->Context.getAsArrayType(DeclType) |
| 595 | ->getElementType(); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 596 | while (Index < IList->getNumInits()) { |
| 597 | Expr *Init = IList->getInit(Index); |
| 598 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 599 | // If we're not the subobject that matches up with the '{' for |
| 600 | // the designator, we shouldn't be handling the |
| 601 | // designator. Return immediately. |
| 602 | if (!SubobjectIsDesignatorContext) |
| 603 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 605 | // Handle this designated initializer. elementIndex will be |
| 606 | // updated to be the next array element we'll initialize. |
| 607 | if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 608 | DeclType, 0, &elementIndex, Index, |
| 609 | StructuredList, StructuredIndex)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 610 | hadError = true; |
| 611 | continue; |
| 612 | } |
| 613 | |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 614 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 615 | maxElements.extend(elementIndex.getBitWidth()); |
| 616 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 617 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 618 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 620 | // If the array is of incomplete type, keep track of the number of |
| 621 | // elements in the initializer. |
| 622 | if (!maxElementsKnown && elementIndex > maxElements) |
| 623 | maxElements = elementIndex; |
| 624 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 625 | continue; |
| 626 | } |
| 627 | |
| 628 | // If we know the maximum number of elements, and we've already |
| 629 | // hit it, stop consuming elements in the initializer list. |
| 630 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 631 | break; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 632 | |
| 633 | // Check this element. |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 634 | CheckSubElementType(IList, elementType, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 635 | StructuredList, StructuredIndex); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 636 | ++elementIndex; |
| 637 | |
| 638 | // If the array is of incomplete type, keep track of the number of |
| 639 | // elements in the initializer. |
| 640 | if (!maxElementsKnown && elementIndex > maxElements) |
| 641 | maxElements = elementIndex; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 642 | } |
| 643 | if (DeclType->isIncompleteArrayType()) { |
| 644 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 645 | // be calculated here. |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 646 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 647 | if (maxElements == Zero) { |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 648 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 649 | // but is supported by GNU. |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 650 | SemaRef->Diag(IList->getLocStart(), |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 651 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 652 | } |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 653 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 654 | DeclType = SemaRef->Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 655 | ArrayType::Normal, 0); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | void InitListChecker::CheckStructUnionTypes(InitListExpr *IList, |
| 660 | QualType DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 661 | RecordDecl::field_iterator Field, |
| 662 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 663 | unsigned &Index, |
| 664 | InitListExpr *StructuredList, |
| 665 | unsigned &StructuredIndex) { |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 666 | RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 667 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 668 | // If the record is invalid, some of it's members are invalid. To avoid |
| 669 | // confusion, we forgo checking the intializer for the entire record. |
| 670 | if (structDecl->isInvalidDecl()) { |
| 671 | hadError = true; |
| 672 | return; |
| 673 | } |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 674 | |
| 675 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 676 | // Value-initialize the first named member of the union. |
| 677 | RecordDecl *RD = DeclType->getAsRecordType()->getDecl(); |
| 678 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 679 | Field != FieldEnd; ++Field) { |
| 680 | if (Field->getDeclName()) { |
| 681 | StructuredList->setInitializedFieldInUnion(*Field); |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | |
| 689 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 690 | // If structDecl is a forward declaration, this loop won't do |
| 691 | // anything except look at designated initializers; That's okay, |
| 692 | // because an error should get printed out elsewhere. It might be |
| 693 | // worthwhile to skip over the rest of the initializer, though. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 694 | RecordDecl *RD = DeclType->getAsRecordType()->getDecl(); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 695 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 696 | while (Index < IList->getNumInits()) { |
| 697 | Expr *Init = IList->getInit(Index); |
| 698 | |
| 699 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 700 | // If we're not the subobject that matches up with the '{' for |
| 701 | // the designator, we shouldn't be handling the |
| 702 | // designator. Return immediately. |
| 703 | if (!SubobjectIsDesignatorContext) |
| 704 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 705 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 706 | // Handle this designated initializer. Field will be updated to |
| 707 | // the next field that we'll be initializing. |
| 708 | if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 709 | DeclType, &Field, 0, Index, |
| 710 | StructuredList, StructuredIndex)) |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 711 | hadError = true; |
| 712 | |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 713 | // Abort early for unions: the designator handled the |
| 714 | // initialization of the appropriate field. |
| 715 | if (DeclType->isUnionType()) |
| 716 | break; |
| 717 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 718 | continue; |
| 719 | } |
| 720 | |
| 721 | if (Field == FieldEnd) { |
| 722 | // We've run out of fields. We're done. |
| 723 | break; |
| 724 | } |
| 725 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 726 | // If we've hit the flexible array member at the end, we're done. |
| 727 | if (Field->getType()->isIncompleteArrayType()) |
| 728 | break; |
| 729 | |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 730 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 732 | ++Field; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 733 | continue; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 734 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 736 | CheckSubElementType(IList, Field->getType(), Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 737 | StructuredList, StructuredIndex); |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 738 | |
| 739 | if (DeclType->isUnionType()) { |
| 740 | // Initialize the first field within the union. |
| 741 | StructuredList->setInitializedFieldInUnion(*Field); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 742 | break; |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 744 | |
| 745 | ++Field; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 746 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 747 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 748 | // FIXME: Implement flexible array initialization GCC extension (it's a |
| 749 | // really messy extension to implement, unfortunately...the necessary |
| 750 | // information isn't actually even here!) |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 751 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 752 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 753 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 754 | /// |
| 755 | /// Determines whether the designated initializer @p DIE, which |
| 756 | /// resides at the given @p Index within the initializer list @p |
| 757 | /// IList, is well-formed for a current object of type @p DeclType |
| 758 | /// (C99 6.7.8). The actual subobject that this designator refers to |
| 759 | /// within the current subobject is returned in either |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 760 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 761 | /// |
| 762 | /// @param IList The initializer list in which this designated |
| 763 | /// initializer occurs. |
| 764 | /// |
| 765 | /// @param DIE The designated initializer and its initialization |
| 766 | /// expression. |
| 767 | /// |
| 768 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 769 | /// into which the designation in @p DIE should refer. |
| 770 | /// |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 771 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 772 | /// a field, this will be set to the field declaration corresponding |
| 773 | /// to the field named by the designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 774 | /// |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 775 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 776 | /// DIE is an array designator or GNU array-range designator, this |
| 777 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 778 | /// |
| 779 | /// @param Index Index into @p IList where the designated initializer |
| 780 | /// @p DIE occurs. |
| 781 | /// |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 782 | /// @param StructuredList The initializer list expression that |
| 783 | /// describes all of the subobject initializers in the order they'll |
| 784 | /// actually be initialized. |
| 785 | /// |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 786 | /// @returns true if there was an error, false otherwise. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 787 | bool |
| 788 | InitListChecker::CheckDesignatedInitializer(InitListExpr *IList, |
| 789 | DesignatedInitExpr *DIE, |
| 790 | DesignatedInitExpr::designators_iterator D, |
| 791 | QualType &CurrentObjectType, |
| 792 | RecordDecl::field_iterator *NextField, |
| 793 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 794 | unsigned &Index, |
| 795 | InitListExpr *StructuredList, |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 796 | unsigned &StructuredIndex, |
| 797 | bool FinishSubobjectInit) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 798 | if (D == DIE->designators_end()) { |
| 799 | // Check the actual initialization for the designated object type. |
| 800 | bool prevHadError = hadError; |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 801 | |
| 802 | // Temporarily remove the designator expression from the |
| 803 | // initializer list that the child calls see, so that we don't try |
| 804 | // to re-process the designator. |
| 805 | unsigned OldIndex = Index; |
| 806 | IList->setInit(OldIndex, DIE->getInit()); |
| 807 | |
| 808 | CheckSubElementType(IList, CurrentObjectType, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 809 | StructuredList, StructuredIndex); |
Douglas Gregor | 36859eb | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 810 | |
| 811 | // Restore the designated initializer expression in the syntactic |
| 812 | // form of the initializer list. |
| 813 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 814 | DIE->setInit(IList->getInit(OldIndex)); |
| 815 | IList->setInit(OldIndex, DIE); |
| 816 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 817 | return hadError && !prevHadError; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 820 | bool IsFirstDesignator = (D == DIE->designators_begin()); |
| 821 | assert((IsFirstDesignator || StructuredList) && |
| 822 | "Need a non-designated initializer list to start from"); |
| 823 | |
| 824 | // Determine the structural initializer list that corresponds to the |
| 825 | // current subobject. |
| 826 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
| 827 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, StructuredList, |
| 828 | StructuredIndex, |
| 829 | SourceRange(D->getStartLocation(), |
| 830 | DIE->getSourceRange().getEnd())); |
| 831 | assert(StructuredList && "Expected a structured initializer list"); |
| 832 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 833 | if (D->isFieldDesignator()) { |
| 834 | // C99 6.7.8p7: |
| 835 | // |
| 836 | // If a designator has the form |
| 837 | // |
| 838 | // . identifier |
| 839 | // |
| 840 | // then the current object (defined below) shall have |
| 841 | // structure or union type and the identifier shall be the |
| 842 | // name of a member of that type. |
| 843 | const RecordType *RT = CurrentObjectType->getAsRecordType(); |
| 844 | if (!RT) { |
| 845 | SourceLocation Loc = D->getDotLoc(); |
| 846 | if (Loc.isInvalid()) |
| 847 | Loc = D->getFieldLoc(); |
| 848 | SemaRef->Diag(Loc, diag::err_field_designator_non_aggr) |
| 849 | << SemaRef->getLangOptions().CPlusPlus << CurrentObjectType; |
| 850 | ++Index; |
| 851 | return true; |
| 852 | } |
| 853 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 854 | // Note: we perform a linear search of the fields here, despite |
| 855 | // the fact that we have a faster lookup method, because we always |
| 856 | // need to compute the field's index. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 857 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 858 | unsigned FieldIndex = 0; |
| 859 | RecordDecl::field_iterator Field = RT->getDecl()->field_begin(), |
| 860 | FieldEnd = RT->getDecl()->field_end(); |
| 861 | for (; Field != FieldEnd; ++Field) { |
| 862 | if (Field->isUnnamedBitfield()) |
| 863 | continue; |
| 864 | |
| 865 | if (Field->getIdentifier() == FieldName) |
| 866 | break; |
| 867 | |
| 868 | ++FieldIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 871 | if (Field == FieldEnd) { |
| 872 | // We did not find the field we're looking for. Produce a |
| 873 | // suitable diagnostic and return a failure. |
| 874 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 875 | if (Lookup.first == Lookup.second) { |
| 876 | // Name lookup didn't find anything. |
| 877 | SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 878 | << FieldName << CurrentObjectType; |
| 879 | } else { |
| 880 | // Name lookup found something, but it wasn't a field. |
| 881 | SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 882 | << FieldName; |
| 883 | SemaRef->Diag((*Lookup.first)->getLocation(), |
| 884 | diag::note_field_designator_found); |
| 885 | } |
| 886 | |
| 887 | ++Index; |
| 888 | return true; |
| 889 | } else if (cast<RecordDecl>((*Field)->getDeclContext()) |
| 890 | ->isAnonymousStructOrUnion()) { |
| 891 | SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_anon_class) |
| 892 | << FieldName |
| 893 | << (cast<RecordDecl>((*Field)->getDeclContext())->isUnion()? 2 : |
| 894 | (int)SemaRef->getLangOptions().CPlusPlus); |
| 895 | SemaRef->Diag((*Field)->getLocation(), diag::note_field_designator_found); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 896 | ++Index; |
| 897 | return true; |
| 898 | } |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 899 | |
| 900 | // All of the fields of a union are located at the same place in |
| 901 | // the initializer list. |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 902 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 903 | FieldIndex = 0; |
Douglas Gregor | 8246276 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 904 | StructuredList->setInitializedFieldInUnion(*Field); |
| 905 | } |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 907 | // Update the designator with the field declaration. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 908 | D->setField(*Field); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 910 | // Make sure that our non-designated initializer list has space |
| 911 | // for a subobject corresponding to this field. |
| 912 | if (FieldIndex >= StructuredList->getNumInits()) |
| 913 | StructuredList->resizeInits(SemaRef->Context, FieldIndex + 1); |
| 914 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 915 | // Recurse to check later designated subobjects. |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 916 | QualType FieldType = (*Field)->getType(); |
| 917 | unsigned newStructuredIndex = FieldIndex; |
| 918 | if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index, |
| 919 | StructuredList, newStructuredIndex)) |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 920 | return true; |
| 921 | |
| 922 | // Find the position of the next field to be initialized in this |
| 923 | // subobject. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 924 | ++Field; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 925 | ++FieldIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 926 | |
| 927 | // If this the first designator, our caller will continue checking |
| 928 | // the rest of this struct/class/union subobject. |
| 929 | if (IsFirstDesignator) { |
| 930 | if (NextField) |
| 931 | *NextField = Field; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 932 | StructuredIndex = FieldIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 933 | return false; |
| 934 | } |
| 935 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 936 | if (!FinishSubobjectInit) |
| 937 | return false; |
| 938 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 939 | // Check the remaining fields within this class/struct/union subobject. |
| 940 | bool prevHadError = hadError; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 941 | CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index, |
| 942 | StructuredList, FieldIndex); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 943 | return hadError && !prevHadError; |
| 944 | } |
| 945 | |
| 946 | // C99 6.7.8p6: |
| 947 | // |
| 948 | // If a designator has the form |
| 949 | // |
| 950 | // [ constant-expression ] |
| 951 | // |
| 952 | // then the current object (defined below) shall have array |
| 953 | // type and the expression shall be an integer constant |
| 954 | // expression. If the array is of unknown size, any |
| 955 | // nonnegative value is valid. |
| 956 | // |
| 957 | // Additionally, cope with the GNU extension that permits |
| 958 | // designators of the form |
| 959 | // |
| 960 | // [ constant-expression ... constant-expression ] |
| 961 | const ArrayType *AT = SemaRef->Context.getAsArrayType(CurrentObjectType); |
| 962 | if (!AT) { |
| 963 | SemaRef->Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 964 | << CurrentObjectType; |
| 965 | ++Index; |
| 966 | return true; |
| 967 | } |
| 968 | |
| 969 | Expr *IndexExpr = 0; |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 970 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 971 | if (D->isArrayDesignator()) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 972 | IndexExpr = DIE->getArrayIndex(*D); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 973 | |
| 974 | bool ConstExpr |
| 975 | = IndexExpr->isIntegerConstantExpr(DesignatedStartIndex, SemaRef->Context); |
| 976 | assert(ConstExpr && "Expression must be constant"); (void)ConstExpr; |
| 977 | |
| 978 | DesignatedEndIndex = DesignatedStartIndex; |
| 979 | } else { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 980 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 981 | |
| 982 | bool StartConstExpr |
| 983 | = DIE->getArrayRangeStart(*D)->isIntegerConstantExpr(DesignatedStartIndex, |
| 984 | SemaRef->Context); |
| 985 | assert(StartConstExpr && "Expression must be constant"); (void)StartConstExpr; |
| 986 | |
| 987 | bool EndConstExpr |
| 988 | = DIE->getArrayRangeEnd(*D)->isIntegerConstantExpr(DesignatedEndIndex, |
| 989 | SemaRef->Context); |
| 990 | assert(EndConstExpr && "Expression must be constant"); (void)EndConstExpr; |
| 991 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 992 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 993 | |
| 994 | if (DesignatedStartIndex.getZExtValue() != DesignatedEndIndex.getZExtValue()) |
Douglas Gregor | 9fddded | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 995 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 998 | if (isa<ConstantArrayType>(AT)) { |
| 999 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1000 | DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1001 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1002 | DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1003 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1004 | if (DesignatedEndIndex >= MaxElements) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1005 | SemaRef->Diag(IndexExpr->getSourceRange().getBegin(), |
| 1006 | diag::err_array_designator_too_large) |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1007 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1008 | << IndexExpr->getSourceRange(); |
| 1009 | ++Index; |
| 1010 | return true; |
| 1011 | } |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1012 | } else { |
| 1013 | // Make sure the bit-widths and signedness match. |
| 1014 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
| 1015 | DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
| 1016 | else if (DesignatedStartIndex.getBitWidth() < DesignatedEndIndex.getBitWidth()) |
| 1017 | DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
| 1018 | DesignatedStartIndex.setIsUnsigned(true); |
| 1019 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1022 | // Make sure that our non-designated initializer list has space |
| 1023 | // for a subobject corresponding to this array element. |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1024 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
| 1025 | StructuredList->resizeInits(SemaRef->Context, |
| 1026 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1028 | // Repeatedly perform subobject initializations in the range |
| 1029 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1031 | // Move to the next designator |
| 1032 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1033 | unsigned OldIndex = Index; |
| 1034 | ++D; |
| 1035 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1036 | // Recurse to check later designated subobjects. |
| 1037 | QualType ElementType = AT->getElementType(); |
| 1038 | Index = OldIndex; |
| 1039 | if (CheckDesignatedInitializer(IList, DIE, D, ElementType, 0, 0, Index, |
| 1040 | StructuredList, ElementIndex, |
| 1041 | (DesignatedStartIndex == DesignatedEndIndex))) |
| 1042 | return true; |
| 1043 | |
| 1044 | // Move to the next index in the array that we'll be initializing. |
| 1045 | ++DesignatedStartIndex; |
| 1046 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1047 | } |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1048 | |
| 1049 | // If this the first designator, our caller will continue checking |
| 1050 | // the rest of this array subobject. |
| 1051 | if (IsFirstDesignator) { |
| 1052 | if (NextElementIndex) |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1053 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1054 | StructuredIndex = ElementIndex; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1055 | return false; |
| 1056 | } |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (!FinishSubobjectInit) |
| 1059 | return false; |
| 1060 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1061 | // Check the remaining elements within this array subobject. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1062 | bool prevHadError = hadError; |
Douglas Gregor | 36dd0c5 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1063 | CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, true, Index, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1064 | StructuredList, ElementIndex); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1065 | return hadError && !prevHadError; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1068 | // Get the structured initializer list for a subobject of type |
| 1069 | // @p CurrentObjectType. |
| 1070 | InitListExpr * |
| 1071 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1072 | QualType CurrentObjectType, |
| 1073 | InitListExpr *StructuredList, |
| 1074 | unsigned StructuredIndex, |
| 1075 | SourceRange InitRange) { |
| 1076 | Expr *ExistingInit = 0; |
| 1077 | if (!StructuredList) |
| 1078 | ExistingInit = SyntacticToSemantic[IList]; |
| 1079 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1080 | ExistingInit = StructuredList->getInit(StructuredIndex); |
| 1081 | |
| 1082 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1083 | return Result; |
| 1084 | |
| 1085 | if (ExistingInit) { |
| 1086 | // We are creating an initializer list that initializes the |
| 1087 | // subobjects of the current object, but there was already an |
| 1088 | // initialization that completely initialized the current |
| 1089 | // subobject, e.g., by a compound literal: |
| 1090 | // |
| 1091 | // struct X { int a, b; }; |
| 1092 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
| 1093 | // |
| 1094 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1095 | // designated initializer re-initializes the whole |
| 1096 | // subobject [0], overwriting previous initializers. |
| 1097 | SemaRef->Diag(InitRange.getBegin(), diag::warn_subobject_initializer_overrides) |
| 1098 | << InitRange; |
| 1099 | SemaRef->Diag(ExistingInit->getSourceRange().getBegin(), |
| 1100 | diag::note_previous_initializer) |
Douglas Gregor | 756283b | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1101 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1102 | << ExistingInit->getSourceRange(); |
| 1103 | } |
| 1104 | |
| 1105 | InitListExpr *Result |
| 1106 | = new (SemaRef->Context) InitListExpr(SourceLocation(), 0, 0, |
| 1107 | SourceLocation()); |
| 1108 | Result->setType(CurrentObjectType); |
| 1109 | |
| 1110 | // Link this new initializer list into the structured initializer |
| 1111 | // lists. |
| 1112 | if (StructuredList) |
| 1113 | StructuredList->updateInit(StructuredIndex, Result); |
| 1114 | else { |
| 1115 | Result->setSyntacticForm(IList); |
| 1116 | SyntacticToSemantic[IList] = Result; |
| 1117 | } |
| 1118 | |
| 1119 | return Result; |
| 1120 | } |
| 1121 | |
| 1122 | /// Update the initializer at index @p StructuredIndex within the |
| 1123 | /// structured initializer list to the value @p expr. |
| 1124 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1125 | unsigned &StructuredIndex, |
| 1126 | Expr *expr) { |
| 1127 | // No structured initializer list to update |
| 1128 | if (!StructuredList) |
| 1129 | return; |
| 1130 | |
| 1131 | if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) { |
| 1132 | // This initializer overwrites a previous initializer. Warn. |
| 1133 | SemaRef->Diag(expr->getSourceRange().getBegin(), |
| 1134 | diag::warn_initializer_overrides) |
| 1135 | << expr->getSourceRange(); |
| 1136 | SemaRef->Diag(PrevInit->getSourceRange().getBegin(), |
| 1137 | diag::note_previous_initializer) |
Douglas Gregor | 756283b | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1138 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1139 | << PrevInit->getSourceRange(); |
| 1140 | } |
| 1141 | |
| 1142 | ++StructuredIndex; |
| 1143 | } |
| 1144 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1145 | /// Check that the given Index expression is a valid array designator |
| 1146 | /// value. This is essentailly just a wrapper around |
| 1147 | /// Expr::isIntegerConstantExpr that also checks for negative values |
| 1148 | /// and produces a reasonable diagnostic if there is a |
| 1149 | /// failure. Returns true if there was an error, false otherwise. If |
| 1150 | /// everything went okay, Value will receive the value of the constant |
| 1151 | /// expression. |
| 1152 | static bool |
| 1153 | CheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) { |
| 1154 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1155 | |
| 1156 | // Make sure this is an integer constant expression. |
| 1157 | if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc)) |
| 1158 | return Self.Diag(Loc, diag::err_array_designator_nonconstant) |
| 1159 | << Index->getSourceRange(); |
| 1160 | |
| 1161 | // Make sure this constant expression is non-negative. |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1162 | llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()), |
| 1163 | Value.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1164 | if (Value < Zero) |
| 1165 | return Self.Diag(Loc, diag::err_array_designator_negative) |
| 1166 | << Value.toString(10) << Index->getSourceRange(); |
| 1167 | |
Douglas Gregor | e498e37 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1168 | Value.setIsUnsigned(true); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
| 1171 | |
| 1172 | Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 1173 | SourceLocation Loc, |
| 1174 | bool UsedColonSyntax, |
| 1175 | OwningExprResult Init) { |
| 1176 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1177 | |
| 1178 | bool Invalid = false; |
| 1179 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1180 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1181 | |
| 1182 | // Build designators and check array designator expressions. |
| 1183 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1184 | const Designator &D = Desig.getDesignator(Idx); |
| 1185 | switch (D.getKind()) { |
| 1186 | case Designator::FieldDesignator: |
| 1187 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
| 1188 | D.getFieldLoc())); |
| 1189 | break; |
| 1190 | |
| 1191 | case Designator::ArrayDesignator: { |
| 1192 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1193 | llvm::APSInt IndexValue; |
| 1194 | if (CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
| 1195 | Invalid = true; |
| 1196 | else { |
| 1197 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 1198 | D.getLBracketLoc(), |
| 1199 | D.getRBracketLoc())); |
| 1200 | InitExpressions.push_back(Index); |
| 1201 | } |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | case Designator::ArrayRangeDesignator: { |
| 1206 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1207 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1208 | llvm::APSInt StartValue; |
| 1209 | llvm::APSInt EndValue; |
| 1210 | if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) || |
| 1211 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue)) |
| 1212 | Invalid = true; |
Douglas Gregor | ea0528d | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1213 | else { |
| 1214 | // Make sure we're comparing values with the same bit width. |
| 1215 | if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
| 1216 | EndValue.extend(StartValue.getBitWidth()); |
| 1217 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 1218 | StartValue.extend(EndValue.getBitWidth()); |
| 1219 | |
| 1220 | if (EndValue < StartValue) { |
| 1221 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
| 1222 | << StartValue.toString(10) << EndValue.toString(10) |
| 1223 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1224 | Invalid = true; |
| 1225 | } else { |
| 1226 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 1227 | D.getLBracketLoc(), |
| 1228 | D.getEllipsisLoc(), |
| 1229 | D.getRBracketLoc())); |
| 1230 | InitExpressions.push_back(StartIndex); |
| 1231 | InitExpressions.push_back(EndIndex); |
| 1232 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1233 | } |
| 1234 | break; |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | if (Invalid || Init.isInvalid()) |
| 1240 | return ExprError(); |
| 1241 | |
| 1242 | // Clear out the expressions within the designation. |
| 1243 | Desig.ClearExprs(*this); |
| 1244 | |
| 1245 | DesignatedInitExpr *DIE |
| 1246 | = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(), |
| 1247 | &InitExpressions[0], InitExpressions.size(), |
| 1248 | Loc, UsedColonSyntax, |
| 1249 | static_cast<Expr *>(Init.release())); |
| 1250 | return Owned(DIE); |
| 1251 | } |
Douglas Gregor | 849afc3 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1252 | |
| 1253 | bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) { |
| 1254 | InitListChecker CheckInitList(this, InitList, DeclType); |
| 1255 | if (!CheckInitList.HadError()) |
| 1256 | InitList = CheckInitList.getFullyStructuredList(); |
| 1257 | |
| 1258 | return CheckInitList.HadError(); |
| 1259 | } |