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