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