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