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