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