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