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 | // |
| 10 | // This file implements semantic analysis for initializers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Expr.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 18 | #include <algorithm> // for std::count_if |
| 19 | #include <functional> // for std::mem_fun |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | |
| 23 | InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) { |
| 24 | hadError = false; |
| 25 | SemaRef = S; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 26 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 27 | unsigned newIndex = 0; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 28 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 29 | CheckExplicitInitList(IL, T, newIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 33 | // FIXME: use a proper constant |
| 34 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 35 | if (const ConstantArrayType *CAT = |
| 36 | SemaRef->Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 37 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 38 | } |
| 39 | return maxElements; |
| 40 | } |
| 41 | |
| 42 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
| 43 | RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl(); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 44 | const int InitializableMembers |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 45 | = std::count_if(structDecl->field_begin(), structDecl->field_end(), |
| 46 | std::mem_fun(&FieldDecl::getDeclName)); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 47 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 48 | return std::min(InitializableMembers, 1); |
| 49 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, |
| 53 | QualType T, unsigned &Index) { |
| 54 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 55 | int maxElements = 0; |
| 56 | |
| 57 | if (T->isArrayType()) |
| 58 | maxElements = numArrayElements(T); |
| 59 | else if (T->isStructureType() || T->isUnionType()) |
| 60 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 61 | else if (T->isVectorType()) |
| 62 | maxElements = T->getAsVectorType()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 63 | else |
| 64 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 65 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 66 | if (maxElements == 0) { |
| 67 | SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(), |
| 68 | diag::err_implicit_empty_initializer); |
| 69 | hadError = true; |
| 70 | return; |
| 71 | } |
| 72 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 73 | // Check the element types *before* we create the implicit init list; |
| 74 | // otherwise, we might end up taking the wrong number of elements |
| 75 | unsigned NewIndex = Index; |
| 76 | CheckListElementTypes(ParentIList, T, NewIndex); |
| 77 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 78 | for (int i = 0; i < maxElements; ++i) { |
| 79 | // Don't attempt to go past the end of the init list |
| 80 | if (Index >= ParentIList->getNumInits()) |
| 81 | break; |
| 82 | Expr* expr = ParentIList->getInit(Index); |
| 83 | |
| 84 | // Add the expr to the new implicit init list and remove if from the old. |
| 85 | InitExprs.push_back(expr); |
| 86 | ParentIList->removeInit(Index); |
| 87 | } |
| 88 | // Synthesize an "implicit" InitListExpr (marked by the invalid source locs). |
| 89 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 90 | &InitExprs[0], InitExprs.size(), |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 91 | SourceLocation(), |
| 92 | ParentIList->hadDesignators()); |
Douglas Gregor | 83233a4 | 2009-01-07 21:26:07 +0000 | [diff] [blame] | 93 | ILE->setImplicit(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 94 | ILE->setType(T); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 95 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 96 | // Modify the parent InitListExpr to point to the implicit InitListExpr. |
| 97 | ParentIList->addInit(Index, ILE); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Steve Naroff | a647caa | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 100 | void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 101 | unsigned &Index) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 102 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 103 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 104 | CheckListElementTypes(IList, T, Index); |
Steve Naroff | a647caa | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 105 | IList->setType(T); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 106 | if (hadError) |
| 107 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 108 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 109 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 110 | // We have leftover initializers |
| 111 | if (IList->getNumInits() > 0 && |
| 112 | SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) { |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 113 | // Special-case |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 114 | SemaRef->Diag(IList->getInit(Index)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 115 | diag::err_excess_initializers_in_char_array_initializer) |
| 116 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 117 | hadError = true; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 118 | } else if (!T->isIncompleteType()) { |
| 119 | // Don't warn for incomplete types, since we'll get an error elsewhere |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 120 | SemaRef->Diag(IList->getInit(Index)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 121 | diag::warn_excess_initializers) |
| 122 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 125 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 126 | if (T->isScalarType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 127 | SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
| 128 | << IList->getSourceRange(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 131 | void InitListChecker::CheckListElementTypes(InitListExpr *IList, |
| 132 | QualType &DeclType, |
| 133 | unsigned &Index) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 134 | if (DeclType->isScalarType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 135 | CheckScalarType(IList, DeclType, Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 136 | } else if (DeclType->isVectorType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 137 | CheckVectorType(IList, DeclType, Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 138 | } else if (DeclType->isAggregateType() || DeclType->isUnionType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 139 | if (DeclType->isStructureType() || DeclType->isUnionType()) |
| 140 | CheckStructUnionTypes(IList, DeclType, Index); |
| 141 | else if (DeclType->isArrayType()) |
| 142 | CheckArrayType(IList, DeclType, Index); |
| 143 | else |
| 144 | assert(0 && "Aggregate that isn't a function or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 145 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 146 | // This type is invalid, issue a diagnostic. |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 147 | Index++; |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 148 | SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 149 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 150 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 151 | } else { |
| 152 | // In C, all types are either scalars or aggregates, but |
| 153 | // additional handling is needed here for C++ (and possibly others?). |
| 154 | assert(0 && "Unsupported initializer type"); |
| 155 | } |
| 156 | } |
| 157 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 158 | void InitListChecker::CheckSubElementType(InitListExpr *IList, |
| 159 | QualType ElemType, |
| 160 | unsigned &Index) { |
| 161 | Expr* expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 162 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 163 | unsigned newIndex = 0; |
| 164 | CheckExplicitInitList(SubInitList, ElemType, newIndex); |
| 165 | Index++; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 166 | } else if (StringLiteral *lit = |
| 167 | SemaRef->IsStringLiteralInit(expr, ElemType)) { |
| 168 | SemaRef->CheckStringLiteralInit(lit, ElemType); |
| 169 | Index++; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 170 | } else if (ElemType->isScalarType()) { |
| 171 | CheckScalarType(IList, ElemType, Index); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 172 | } else if (expr->getType()->getAsRecordType() && |
Eli Friedman | c92e5e4 | 2008-06-09 03:52:40 +0000 | [diff] [blame] | 173 | SemaRef->Context.typesAreCompatible( |
| 174 | expr->getType().getUnqualifiedType(), |
| 175 | ElemType.getUnqualifiedType())) { |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 176 | Index++; |
| 177 | // FIXME: Add checking |
| 178 | } else { |
| 179 | CheckImplicitInitList(IList, ElemType, Index); |
| 180 | Index++; |
| 181 | } |
| 182 | } |
| 183 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 184 | void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType, |
| 185 | unsigned &Index) { |
| 186 | if (Index < IList->getNumInits()) { |
| 187 | Expr* expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 188 | if (isa<InitListExpr>(expr)) { |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 189 | SemaRef->Diag(IList->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 190 | diag::err_many_braces_around_scalar_init) |
| 191 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 192 | hadError = true; |
| 193 | ++Index; |
| 194 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 195 | } |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 196 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
| 197 | if (SemaRef->CheckSingleInitializer(expr, DeclType)) |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 198 | hadError = true; // types weren't compatible. |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 199 | else if (savExpr != expr) |
| 200 | // The type was promoted, update initializer list. |
| 201 | IList->setInit(Index, expr); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 202 | ++Index; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 203 | } else { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 204 | SemaRef->Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
| 205 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 206 | hadError = true; |
| 207 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 208 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType, |
| 212 | unsigned &Index) { |
| 213 | if (Index < IList->getNumInits()) { |
| 214 | const VectorType *VT = DeclType->getAsVectorType(); |
| 215 | int maxElements = VT->getNumElements(); |
| 216 | QualType elementType = VT->getElementType(); |
| 217 | |
| 218 | for (int i = 0; i < maxElements; ++i) { |
| 219 | // Don't attempt to go past the end of the init list |
| 220 | if (Index >= IList->getNumInits()) |
| 221 | break; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 222 | CheckSubElementType(IList, elementType, Index); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType, |
| 228 | unsigned &Index) { |
| 229 | // Check for the special-case of initializing an array with a string. |
| 230 | if (Index < IList->getNumInits()) { |
| 231 | if (StringLiteral *lit = |
| 232 | SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) { |
| 233 | SemaRef->CheckStringLiteralInit(lit, DeclType); |
| 234 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 238 | if (const VariableArrayType *VAT = |
| 239 | SemaRef->Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 240 | // Check for VLAs; in standard C it would be possible to check this |
| 241 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 242 | // them in all sorts of strange places). |
| 243 | SemaRef->Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 244 | diag::err_variable_object_no_init) |
| 245 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 246 | hadError = true; |
| 247 | return; |
| 248 | } |
| 249 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 250 | int maxElements = numArrayElements(DeclType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 251 | QualType elementType = SemaRef->Context.getAsArrayType(DeclType) |
| 252 | ->getElementType(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 253 | int numElements = 0; |
| 254 | for (int i = 0; i < maxElements; ++i, ++numElements) { |
| 255 | // Don't attempt to go past the end of the init list |
| 256 | if (Index >= IList->getNumInits()) |
| 257 | break; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 258 | CheckSubElementType(IList, elementType, Index); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 259 | } |
| 260 | if (DeclType->isIncompleteArrayType()) { |
| 261 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 262 | // be calculated here. |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 263 | if (numElements == 0) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 264 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 265 | // but is supported by GNU. |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 266 | SemaRef->Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 267 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 268 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 269 | |
| 270 | llvm::APSInt ConstVal(32); |
| 271 | ConstVal = numElements; |
| 272 | DeclType = SemaRef->Context.getConstantArrayType(elementType, ConstVal, |
| 273 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | void InitListChecker::CheckStructUnionTypes(InitListExpr *IList, |
| 278 | QualType DeclType, |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 279 | unsigned &Index) { |
| 280 | RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 281 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 282 | // If the record is invalid, some of it's members are invalid. To avoid |
| 283 | // confusion, we forgo checking the intializer for the entire record. |
| 284 | if (structDecl->isInvalidDecl()) { |
| 285 | hadError = true; |
| 286 | return; |
| 287 | } |
| 288 | // If structDecl is a forward declaration, this loop won't do anything; |
| 289 | // That's okay, because an error should get printed out elsewhere. It |
| 290 | // might be worthwhile to skip over the rest of the initializer, though. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 291 | RecordDecl *RD = DeclType->getAsRecordType()->getDecl(); |
| 292 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 293 | FieldEnd = RD->field_end(); |
| 294 | Field != FieldEnd; ++Field) { |
| 295 | // If we've hit the flexible array member at the end, we're done. |
| 296 | if (Field->getType()->isIncompleteArrayType()) |
| 297 | break; |
| 298 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 299 | // Don't attempt to go past the end of the init list |
| 300 | if (Index >= IList->getNumInits()) |
| 301 | break; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 302 | |
| 303 | if (!Field->getIdentifier()) { |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 304 | // Don't initialize unnamed fields, e.g. "int : 20;" |
| 305 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 306 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 307 | |
| 308 | CheckSubElementType(IList, Field->getType(), Index); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 309 | if (DeclType->isUnionType()) |
| 310 | break; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 311 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 312 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 313 | // FIXME: Implement flexible array initialization GCC extension (it's a |
| 314 | // really messy extension to implement, unfortunately...the necessary |
| 315 | // information isn't actually even here!) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 316 | } |
| 317 | } // end namespace clang |
| 318 | |