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