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