Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for initializers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Designator.h" |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Expr.h" |
Chris Lattner | 52a425b | 2009-01-27 18:30:58 +0000 | [diff] [blame^] | 18 | #include "clang/Basic/DiagnosticSema.h" |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 19 | #include <algorithm> // for std::count_if |
| 20 | #include <functional> // for std::mem_fun |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 22 | using namespace clang; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 23 | |
| 24 | InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) { |
| 25 | hadError = false; |
| 26 | SemaRef = S; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 27 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 28 | unsigned newIndex = 0; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 29 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 30 | CheckExplicitInitList(IL, T, newIndex); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 34 | // FIXME: use a proper constant |
| 35 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 36 | if (const ConstantArrayType *CAT = |
| 37 | SemaRef->Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 38 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 39 | } |
| 40 | return maxElements; |
| 41 | } |
| 42 | |
| 43 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
| 44 | RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl(); |
Douglas Gregor | 3967762 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 45 | const int InitializableMembers |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 46 | = std::count_if(structDecl->field_begin(), structDecl->field_end(), |
| 47 | std::mem_fun(&FieldDecl::getDeclName)); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 48 | if (structDecl->isUnion()) |
Eli Friedman | 9f5250b | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 49 | return std::min(InitializableMembers, 1); |
| 50 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, |
| 54 | QualType T, unsigned &Index) { |
| 55 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 56 | int maxElements = 0; |
| 57 | |
| 58 | if (T->isArrayType()) |
| 59 | maxElements = numArrayElements(T); |
| 60 | else if (T->isStructureType() || T->isUnionType()) |
| 61 | maxElements = numStructUnionElements(T); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 62 | else if (T->isVectorType()) |
| 63 | maxElements = T->getAsVectorType()->getNumElements(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 64 | else |
| 65 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 66 | |
Eli Friedman | f8df28c | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 67 | if (maxElements == 0) { |
| 68 | SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(), |
| 69 | diag::err_implicit_empty_initializer); |
| 70 | hadError = true; |
| 71 | return; |
| 72 | } |
| 73 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 74 | // Check the element types *before* we create the implicit init list; |
| 75 | // otherwise, we might end up taking the wrong number of elements |
| 76 | unsigned NewIndex = Index; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 77 | CheckListElementTypes(ParentIList, T, false, NewIndex); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 78 | |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 79 | for (int i = 0; i < maxElements; ++i) { |
| 80 | // Don't attempt to go past the end of the init list |
| 81 | if (Index >= ParentIList->getNumInits()) |
| 82 | break; |
| 83 | Expr* expr = ParentIList->getInit(Index); |
| 84 | |
| 85 | // Add the expr to the new implicit init list and remove if from the old. |
| 86 | InitExprs.push_back(expr); |
| 87 | ParentIList->removeInit(Index); |
| 88 | } |
| 89 | // Synthesize an "implicit" InitListExpr (marked by the invalid source locs). |
| 90 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 91 | &InitExprs[0], InitExprs.size(), |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 92 | SourceLocation(), |
| 93 | ParentIList->hadDesignators()); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 94 | ILE->setType(T); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 95 | |
Steve Naroff | c4d4a48 | 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 | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Steve Naroff | 5609952 | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 100 | void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 101 | unsigned &Index) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 102 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 104 | CheckListElementTypes(IList, T, true, Index); |
Steve Naroff | 5609952 | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 105 | IList->setType(T); |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 106 | if (hadError) |
| 107 | return; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 108 | |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 109 | if (Index < IList->getNumInits()) { |
Eli Friedman | d8535af | 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 | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 113 | // Special-case |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 114 | SemaRef->Diag(IList->getInit(Index)->getLocStart(), |
Chris Lattner | 9d2cf08 | 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 | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 117 | hadError = true; |
Eli Friedman | b9ea6bc | 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 | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 120 | SemaRef->Diag(IList->getInit(Index)->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 121 | diag::warn_excess_initializers) |
| 122 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Eli Friedman | 455f762 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 125 | |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 126 | if (T->isScalarType()) |
Chris Lattner | 9d2cf08 | 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 | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 131 | void InitListChecker::CheckListElementTypes(InitListExpr *IList, |
| 132 | QualType &DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 133 | bool SubobjectIsDesignatorContext, |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 134 | unsigned &Index) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 135 | if (DeclType->isScalarType()) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 136 | CheckScalarType(IList, DeclType, 0, Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 137 | } else if (DeclType->isVectorType()) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 138 | CheckVectorType(IList, DeclType, Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 139 | } else if (DeclType->isAggregateType() || DeclType->isUnionType()) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 140 | if (DeclType->isStructureType() || DeclType->isUnionType()) { |
| 141 | RecordDecl *RD = DeclType->getAsRecordType()->getDecl(); |
| 142 | CheckStructUnionTypes(IList, DeclType, RD->field_begin(), |
| 143 | SubobjectIsDesignatorContext, Index); |
| 144 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 145 | llvm::APSInt Zero( |
| 146 | SemaRef->Context.getTypeSize(SemaRef->Context.getSizeType()), |
| 147 | false); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 148 | CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index); |
| 149 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 150 | else |
| 151 | assert(0 && "Aggregate that isn't a function or array?!"); |
Steve Naroff | ff5b3a8 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 152 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 153 | // This type is invalid, issue a diagnostic. |
Eli Friedman | b9ea6bc | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 154 | Index++; |
Chris Lattner | 10f2c2e | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 155 | SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 156 | << DeclType; |
Eli Friedman | b9ea6bc | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 157 | hadError = true; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 158 | } else { |
| 159 | // In C, all types are either scalars or aggregates, but |
| 160 | // additional handling is needed here for C++ (and possibly others?). |
| 161 | assert(0 && "Unsupported initializer type"); |
| 162 | } |
| 163 | } |
| 164 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 165 | void InitListChecker::CheckSubElementType(InitListExpr *IList, |
| 166 | QualType ElemType, |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 167 | Expr *expr, |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 168 | unsigned &Index) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 169 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 170 | unsigned newIndex = 0; |
| 171 | CheckExplicitInitList(SubInitList, ElemType, newIndex); |
| 172 | Index++; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 173 | } else if (StringLiteral *lit = |
| 174 | SemaRef->IsStringLiteralInit(expr, ElemType)) { |
| 175 | SemaRef->CheckStringLiteralInit(lit, ElemType); |
| 176 | Index++; |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 177 | } else if (ElemType->isScalarType()) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 178 | CheckScalarType(IList, ElemType, expr, Index); |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 179 | } else if (expr->getType()->getAsRecordType() && |
Eli Friedman | 2ccfb51 | 2008-06-09 03:52:40 +0000 | [diff] [blame] | 180 | SemaRef->Context.typesAreCompatible( |
| 181 | expr->getType().getUnqualifiedType(), |
| 182 | ElemType.getUnqualifiedType())) { |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 183 | Index++; |
| 184 | // FIXME: Add checking |
| 185 | } else { |
| 186 | CheckImplicitInitList(IList, ElemType, Index); |
| 187 | Index++; |
| 188 | } |
| 189 | } |
| 190 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 191 | void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType, |
| 192 | Expr *expr, unsigned &Index) { |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 193 | if (Index < IList->getNumInits()) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 194 | if (!expr) |
| 195 | expr = IList->getInit(Index); |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 196 | if (isa<InitListExpr>(expr)) { |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 197 | SemaRef->Diag(IList->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 198 | diag::err_many_braces_around_scalar_init) |
| 199 | << IList->getSourceRange(); |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 200 | hadError = true; |
| 201 | ++Index; |
| 202 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 203 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 204 | SemaRef->Diag(expr->getSourceRange().getBegin(), |
| 205 | diag::err_designator_for_scalar_init) |
| 206 | << DeclType << expr->getSourceRange(); |
| 207 | hadError = true; |
| 208 | ++Index; |
| 209 | return; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 210 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 211 | |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 212 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 213 | if (SemaRef->CheckSingleInitializer(expr, DeclType, false)) |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 214 | hadError = true; // types weren't compatible. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 215 | else if (savExpr != expr) { |
Eli Friedman | d8535af | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 216 | // The type was promoted, update initializer list. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 217 | if (DesignatedInitExpr *DIE |
| 218 | = dyn_cast<DesignatedInitExpr>(IList->getInit(Index))) |
| 219 | DIE->setInit(expr); |
| 220 | else |
| 221 | IList->setInit(Index, expr); |
| 222 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 223 | ++Index; |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 224 | } else { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 225 | SemaRef->Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
| 226 | << IList->getSourceRange(); |
Eli Friedman | 71de9eb | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 227 | hadError = true; |
| 228 | return; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 229 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType, |
| 233 | unsigned &Index) { |
| 234 | if (Index < IList->getNumInits()) { |
| 235 | const VectorType *VT = DeclType->getAsVectorType(); |
| 236 | int maxElements = VT->getNumElements(); |
| 237 | QualType elementType = VT->getElementType(); |
| 238 | |
| 239 | for (int i = 0; i < maxElements; ++i) { |
| 240 | // Don't attempt to go past the end of the init list |
| 241 | if (Index >= IList->getNumInits()) |
| 242 | break; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 243 | CheckSubElementType(IList, elementType, IList->getInit(Index), Index); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 249 | llvm::APSInt elementIndex, |
| 250 | bool SubobjectIsDesignatorContext, |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 251 | unsigned &Index) { |
| 252 | // Check for the special-case of initializing an array with a string. |
| 253 | if (Index < IList->getNumInits()) { |
| 254 | if (StringLiteral *lit = |
| 255 | SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) { |
| 256 | SemaRef->CheckStringLiteralInit(lit, DeclType); |
| 257 | ++Index; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | } |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 261 | if (const VariableArrayType *VAT = |
| 262 | SemaRef->Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 263 | // Check for VLAs; in standard C it would be possible to check this |
| 264 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 265 | // them in all sorts of strange places). |
| 266 | SemaRef->Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 267 | diag::err_variable_object_no_init) |
| 268 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 46f8166 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 269 | hadError = true; |
| 270 | return; |
| 271 | } |
| 272 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 273 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 274 | llvm::APSInt maxElements(elementIndex.getBitWidth(), elementIndex.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 275 | bool maxElementsKnown = false; |
| 276 | if (const ConstantArrayType *CAT = |
| 277 | SemaRef->Context.getAsConstantArrayType(DeclType)) { |
| 278 | maxElements = CAT->getSize(); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 279 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 280 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 281 | maxElementsKnown = true; |
| 282 | } |
| 283 | |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 284 | QualType elementType = SemaRef->Context.getAsArrayType(DeclType) |
| 285 | ->getElementType(); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 286 | while (Index < IList->getNumInits()) { |
| 287 | Expr *Init = IList->getInit(Index); |
| 288 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 289 | // If we're not the subobject that matches up with the '{' for |
| 290 | // the designator, we shouldn't be handling the |
| 291 | // designator. Return immediately. |
| 292 | if (!SubobjectIsDesignatorContext) |
| 293 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 295 | // Handle this designated initializer. elementIndex will be |
| 296 | // updated to be the next array element we'll initialize. |
| 297 | if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(), |
| 298 | DeclType, 0, &elementIndex, Index)) { |
| 299 | hadError = true; |
| 300 | continue; |
| 301 | } |
| 302 | |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 303 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 304 | maxElements.extend(elementIndex.getBitWidth()); |
| 305 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 306 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 307 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 308 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 309 | // If the array is of incomplete type, keep track of the number of |
| 310 | // elements in the initializer. |
| 311 | if (!maxElementsKnown && elementIndex > maxElements) |
| 312 | maxElements = elementIndex; |
| 313 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 314 | continue; |
| 315 | } |
| 316 | |
| 317 | // If we know the maximum number of elements, and we've already |
| 318 | // hit it, stop consuming elements in the initializer list. |
| 319 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 320 | break; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 321 | |
| 322 | // Check this element. |
| 323 | CheckSubElementType(IList, elementType, IList->getInit(Index), Index); |
| 324 | ++elementIndex; |
| 325 | |
| 326 | // If the array is of incomplete type, keep track of the number of |
| 327 | // elements in the initializer. |
| 328 | if (!maxElementsKnown && elementIndex > maxElements) |
| 329 | maxElements = elementIndex; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 330 | } |
| 331 | if (DeclType->isIncompleteArrayType()) { |
| 332 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 333 | // be calculated here. |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 334 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 335 | if (maxElements == Zero) { |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 336 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 337 | // but is supported by GNU. |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 338 | SemaRef->Diag(IList->getLocStart(), |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 339 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 340 | } |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 342 | DeclType = SemaRef->Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 604dacf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 343 | ArrayType::Normal, 0); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | void InitListChecker::CheckStructUnionTypes(InitListExpr *IList, |
| 348 | QualType DeclType, |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 349 | RecordDecl::field_iterator Field, |
| 350 | bool SubobjectIsDesignatorContext, |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 351 | unsigned &Index) { |
| 352 | RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl(); |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 353 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 354 | // If the record is invalid, some of it's members are invalid. To avoid |
| 355 | // confusion, we forgo checking the intializer for the entire record. |
| 356 | if (structDecl->isInvalidDecl()) { |
| 357 | hadError = true; |
| 358 | return; |
| 359 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 360 | // If structDecl is a forward declaration, this loop won't do |
| 361 | // anything except look at designated initializers; That's okay, |
| 362 | // because an error should get printed out elsewhere. It might be |
| 363 | // worthwhile to skip over the rest of the initializer, though. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 364 | RecordDecl *RD = DeclType->getAsRecordType()->getDecl(); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 365 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 366 | while (Index < IList->getNumInits()) { |
| 367 | Expr *Init = IList->getInit(Index); |
| 368 | |
| 369 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 370 | // If we're not the subobject that matches up with the '{' for |
| 371 | // the designator, we shouldn't be handling the |
| 372 | // designator. Return immediately. |
| 373 | if (!SubobjectIsDesignatorContext) |
| 374 | return; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 376 | // Handle this designated initializer. Field will be updated to |
| 377 | // the next field that we'll be initializing. |
| 378 | if (CheckDesignatedInitializer(IList, DIE, DIE->designators_begin(), |
| 379 | DeclType, &Field, 0, Index)) |
| 380 | hadError = true; |
| 381 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 382 | continue; |
| 383 | } |
| 384 | |
| 385 | if (Field == FieldEnd) { |
| 386 | // We've run out of fields. We're done. |
| 387 | break; |
| 388 | } |
| 389 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 390 | // If we've hit the flexible array member at the end, we're done. |
| 391 | if (Field->getType()->isIncompleteArrayType()) |
| 392 | break; |
| 393 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 394 | if (!Field->getIdentifier()) { |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 395 | // Don't initialize unnamed fields, e.g. "int : 20;" |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 396 | ++Field; |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 397 | continue; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 398 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 400 | CheckSubElementType(IList, Field->getType(), IList->getInit(Index), Index); |
| 401 | if (DeclType->isUnionType()) // FIXME: designated initializers? |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 402 | break; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 403 | |
| 404 | ++Field; |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 405 | } |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 406 | |
Eli Friedman | 683cedf | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 407 | // FIXME: Implement flexible array initialization GCC extension (it's a |
| 408 | // really messy extension to implement, unfortunately...the necessary |
| 409 | // information isn't actually even here!) |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 410 | } |
Steve Naroff | c4d4a48 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 412 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 413 | /// |
| 414 | /// Determines whether the designated initializer @p DIE, which |
| 415 | /// resides at the given @p Index within the initializer list @p |
| 416 | /// IList, is well-formed for a current object of type @p DeclType |
| 417 | /// (C99 6.7.8). The actual subobject that this designator refers to |
| 418 | /// within the current subobject is returned in either |
| 419 | /// @p DesignatedField or @p DesignatedIndex (whichever is |
| 420 | /// appropriate). |
| 421 | /// |
| 422 | /// @param IList The initializer list in which this designated |
| 423 | /// initializer occurs. |
| 424 | /// |
| 425 | /// @param DIE The designated initializer and its initialization |
| 426 | /// expression. |
| 427 | /// |
| 428 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 429 | /// into which the designation in @p DIE should refer. |
| 430 | /// |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 431 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 432 | /// a field, this will be set to the field declaration corresponding |
| 433 | /// to the field named by the designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 434 | /// |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 435 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 436 | /// DIE is an array designator or GNU array-range designator, this |
| 437 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 438 | /// |
| 439 | /// @param Index Index into @p IList where the designated initializer |
| 440 | /// @p DIE occurs. |
| 441 | /// |
| 442 | /// @returns true if there was an error, false otherwise. |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 443 | bool |
| 444 | InitListChecker::CheckDesignatedInitializer(InitListExpr *IList, |
| 445 | DesignatedInitExpr *DIE, |
| 446 | DesignatedInitExpr::designators_iterator D, |
| 447 | QualType &CurrentObjectType, |
| 448 | RecordDecl::field_iterator *NextField, |
| 449 | llvm::APSInt *NextElementIndex, |
| 450 | unsigned &Index) { |
| 451 | bool IsFirstDesignator = (D == DIE->designators_begin()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 453 | if (D == DIE->designators_end()) { |
| 454 | // Check the actual initialization for the designated object type. |
| 455 | bool prevHadError = hadError; |
| 456 | CheckSubElementType(IList, CurrentObjectType, DIE->getInit(), Index); |
| 457 | return hadError && !prevHadError; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 460 | if (D->isFieldDesignator()) { |
| 461 | // C99 6.7.8p7: |
| 462 | // |
| 463 | // If a designator has the form |
| 464 | // |
| 465 | // . identifier |
| 466 | // |
| 467 | // then the current object (defined below) shall have |
| 468 | // structure or union type and the identifier shall be the |
| 469 | // name of a member of that type. |
| 470 | const RecordType *RT = CurrentObjectType->getAsRecordType(); |
| 471 | if (!RT) { |
| 472 | SourceLocation Loc = D->getDotLoc(); |
| 473 | if (Loc.isInvalid()) |
| 474 | Loc = D->getFieldLoc(); |
| 475 | SemaRef->Diag(Loc, diag::err_field_designator_non_aggr) |
| 476 | << SemaRef->getLangOptions().CPlusPlus << CurrentObjectType; |
| 477 | ++Index; |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | IdentifierInfo *FieldName = D->getFieldName(); |
| 482 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 483 | FieldDecl *DesignatedField = 0; |
| 484 | if (Lookup.first == Lookup.second) { |
| 485 | // Lookup did not find anything with this name. |
| 486 | SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 487 | << FieldName << CurrentObjectType; |
| 488 | } else if (isa<FieldDecl>(*Lookup.first)) { |
| 489 | // Name lookup found a field. |
| 490 | DesignatedField = cast<FieldDecl>(*Lookup.first); |
| 491 | // FIXME: Make sure this isn't a field in an anonymous |
| 492 | // struct/union. |
| 493 | } else { |
| 494 | // Name lookup found something, but it wasn't a field. |
| 495 | SemaRef->Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 496 | << FieldName; |
| 497 | SemaRef->Diag((*Lookup.first)->getLocation(), |
| 498 | diag::note_field_designator_found); |
| 499 | } |
| 500 | |
| 501 | if (!DesignatedField) { |
| 502 | ++Index; |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | // Update the designator with the field declaration. |
| 507 | D->setField(DesignatedField); |
| 508 | |
| 509 | // Recurse to check later designated subobjects. |
| 510 | QualType FieldType = DesignatedField->getType(); |
| 511 | if (CheckDesignatedInitializer(IList, DIE, ++D, FieldType, 0, 0, Index)) |
| 512 | return true; |
| 513 | |
| 514 | // Find the position of the next field to be initialized in this |
| 515 | // subobject. |
| 516 | RecordDecl::field_iterator Field(DeclContext::decl_iterator(DesignatedField), |
| 517 | RT->getDecl()->decls_end()); |
| 518 | ++Field; |
| 519 | |
| 520 | // If this the first designator, our caller will continue checking |
| 521 | // the rest of this struct/class/union subobject. |
| 522 | if (IsFirstDesignator) { |
| 523 | if (NextField) |
| 524 | *NextField = Field; |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | // Check the remaining fields within this class/struct/union subobject. |
| 529 | bool prevHadError = hadError; |
| 530 | CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index); |
| 531 | return hadError && !prevHadError; |
| 532 | } |
| 533 | |
| 534 | // C99 6.7.8p6: |
| 535 | // |
| 536 | // If a designator has the form |
| 537 | // |
| 538 | // [ constant-expression ] |
| 539 | // |
| 540 | // then the current object (defined below) shall have array |
| 541 | // type and the expression shall be an integer constant |
| 542 | // expression. If the array is of unknown size, any |
| 543 | // nonnegative value is valid. |
| 544 | // |
| 545 | // Additionally, cope with the GNU extension that permits |
| 546 | // designators of the form |
| 547 | // |
| 548 | // [ constant-expression ... constant-expression ] |
| 549 | const ArrayType *AT = SemaRef->Context.getAsArrayType(CurrentObjectType); |
| 550 | if (!AT) { |
| 551 | SemaRef->Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 552 | << CurrentObjectType; |
| 553 | ++Index; |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | Expr *IndexExpr = 0; |
| 558 | llvm::APSInt DesignatedIndex; |
| 559 | if (D->isArrayDesignator()) |
| 560 | IndexExpr = DIE->getArrayIndex(*D); |
| 561 | else { |
| 562 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
| 563 | IndexExpr = DIE->getArrayRangeEnd(*D); |
| 564 | } |
| 565 | |
| 566 | bool ConstExpr |
| 567 | = IndexExpr->isIntegerConstantExpr(DesignatedIndex, SemaRef->Context); |
| 568 | assert(ConstExpr && "Expression must be constant"); (void)ConstExpr; |
| 569 | |
| 570 | if (isa<ConstantArrayType>(AT)) { |
| 571 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 5a203a6 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 572 | DesignatedIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 573 | DesignatedIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 574 | if (DesignatedIndex >= MaxElements) { |
| 575 | SemaRef->Diag(IndexExpr->getSourceRange().getBegin(), |
| 576 | diag::err_array_designator_too_large) |
| 577 | << DesignatedIndex.toString(10) << MaxElements.toString(10) |
| 578 | << IndexExpr->getSourceRange(); |
| 579 | ++Index; |
| 580 | return true; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // Recurse to check later designated subobjects. |
| 585 | QualType ElementType = AT->getElementType(); |
| 586 | if (CheckDesignatedInitializer(IList, DIE, ++D, ElementType, 0, 0, Index)) |
| 587 | return true; |
| 588 | |
| 589 | // Move to the next index in the array that we'll be initializing. |
| 590 | ++DesignatedIndex; |
| 591 | |
| 592 | // If this the first designator, our caller will continue checking |
| 593 | // the rest of this array subobject. |
| 594 | if (IsFirstDesignator) { |
| 595 | if (NextElementIndex) |
| 596 | *NextElementIndex = DesignatedIndex; |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | // Check the remaining elements within this array subobject. |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 601 | bool prevHadError = hadError; |
Douglas Gregor | 710f6d4 | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 602 | CheckArrayType(IList, CurrentObjectType, DesignatedIndex, true, Index); |
| 603 | return hadError && !prevHadError; |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /// Check that the given Index expression is a valid array designator |
| 607 | /// value. This is essentailly just a wrapper around |
| 608 | /// Expr::isIntegerConstantExpr that also checks for negative values |
| 609 | /// and produces a reasonable diagnostic if there is a |
| 610 | /// failure. Returns true if there was an error, false otherwise. If |
| 611 | /// everything went okay, Value will receive the value of the constant |
| 612 | /// expression. |
| 613 | static bool |
| 614 | CheckArrayDesignatorExpr(Sema &Self, Expr *Index, llvm::APSInt &Value) { |
| 615 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 616 | |
| 617 | // Make sure this is an integer constant expression. |
| 618 | if (!Index->isIntegerConstantExpr(Value, Self.Context, &Loc)) |
| 619 | return Self.Diag(Loc, diag::err_array_designator_nonconstant) |
| 620 | << Index->getSourceRange(); |
| 621 | |
| 622 | // Make sure this constant expression is non-negative. |
Douglas Gregor | 6972270 | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 623 | llvm::APSInt Zero(llvm::APSInt::getNullValue(Value.getBitWidth()), |
| 624 | Value.isUnsigned()); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 625 | if (Value < Zero) |
| 626 | return Self.Diag(Loc, diag::err_array_designator_negative) |
| 627 | << Value.toString(10) << Index->getSourceRange(); |
| 628 | |
Douglas Gregor | e498e37 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 629 | Value.setIsUnsigned(true); |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | |
| 633 | Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 634 | SourceLocation Loc, |
| 635 | bool UsedColonSyntax, |
| 636 | OwningExprResult Init) { |
| 637 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 638 | |
| 639 | bool Invalid = false; |
| 640 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 641 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 642 | |
| 643 | // Build designators and check array designator expressions. |
| 644 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 645 | const Designator &D = Desig.getDesignator(Idx); |
| 646 | switch (D.getKind()) { |
| 647 | case Designator::FieldDesignator: |
| 648 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
| 649 | D.getFieldLoc())); |
| 650 | break; |
| 651 | |
| 652 | case Designator::ArrayDesignator: { |
| 653 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 654 | llvm::APSInt IndexValue; |
| 655 | if (CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
| 656 | Invalid = true; |
| 657 | else { |
| 658 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 659 | D.getLBracketLoc(), |
| 660 | D.getRBracketLoc())); |
| 661 | InitExpressions.push_back(Index); |
| 662 | } |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | case Designator::ArrayRangeDesignator: { |
| 667 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 668 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 669 | llvm::APSInt StartValue; |
| 670 | llvm::APSInt EndValue; |
| 671 | if (CheckArrayDesignatorExpr(*this, StartIndex, StartValue) || |
| 672 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue)) |
| 673 | Invalid = true; |
Douglas Gregor | ea0528d | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 674 | else { |
| 675 | // Make sure we're comparing values with the same bit width. |
| 676 | if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
| 677 | EndValue.extend(StartValue.getBitWidth()); |
| 678 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 679 | StartValue.extend(EndValue.getBitWidth()); |
| 680 | |
| 681 | if (EndValue < StartValue) { |
| 682 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
| 683 | << StartValue.toString(10) << EndValue.toString(10) |
| 684 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 685 | Invalid = true; |
| 686 | } else { |
| 687 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 688 | D.getLBracketLoc(), |
| 689 | D.getEllipsisLoc(), |
| 690 | D.getRBracketLoc())); |
| 691 | InitExpressions.push_back(StartIndex); |
| 692 | InitExpressions.push_back(EndIndex); |
| 693 | } |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 694 | } |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (Invalid || Init.isInvalid()) |
| 701 | return ExprError(); |
| 702 | |
| 703 | // Clear out the expressions within the designation. |
| 704 | Desig.ClearExprs(*this); |
| 705 | |
| 706 | DesignatedInitExpr *DIE |
| 707 | = DesignatedInitExpr::Create(Context, &Designators[0], Designators.size(), |
| 708 | &InitExpressions[0], InitExpressions.size(), |
| 709 | Loc, UsedColonSyntax, |
| 710 | static_cast<Expr *>(Init.release())); |
| 711 | return Owned(DIE); |
| 712 | } |