blob: 20fa50731d0a3f7b276abe992667f34e9ea49d1d [file] [log] [blame]
Steve Naroffc4d4a482008-05-01 22:18:59 +00001//===--- 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
19namespace clang {
20
21InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) {
22 hadError = false;
23 SemaRef = S;
Eli Friedmand8535af2008-05-19 20:00:43 +000024
Eli Friedman683cedf2008-05-19 19:16:24 +000025 unsigned newIndex = 0;
Eli Friedmand8535af2008-05-19 20:00:43 +000026
Eli Friedman683cedf2008-05-19 19:16:24 +000027 CheckExplicitInitList(IL, T, newIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +000028}
29
30int 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
52int InitListChecker::numStructUnionElements(QualType DeclType) {
53 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Eli Friedman683cedf2008-05-19 19:16:24 +000054 if (structDecl->getKind() == Decl::Union)
55 return std::min(structDecl->getNumMembers(), 1);
Steve Naroffc4d4a482008-05-01 22:18:59 +000056 return structDecl->getNumMembers() - structDecl->hasFlexibleArrayMember();
57}
58
59void 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 Friedman683cedf2008-05-19 19:16:24 +000068 else if (T->isVectorType())
69 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroffc4d4a482008-05-01 22:18:59 +000070 else
71 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman683cedf2008-05-19 19:16:24 +000072
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 Naroffc4d4a482008-05-01 22:18:59 +000078 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 Friedmand8535af2008-05-19 20:00:43 +000093
Steve Naroffc4d4a482008-05-01 22:18:59 +000094 // Modify the parent InitListExpr to point to the implicit InitListExpr.
95 ParentIList->addInit(Index, ILE);
Steve Naroffc4d4a482008-05-01 22:18:59 +000096}
97
Steve Naroff56099522008-05-06 00:23:44 +000098void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Steve Naroffc4d4a482008-05-01 22:18:59 +000099 unsigned &Index) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000100 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Eli Friedmand8535af2008-05-19 20:00:43 +0000101
Eli Friedman683cedf2008-05-19 19:16:24 +0000102 CheckListElementTypes(IList, T, Index);
Steve Naroff56099522008-05-06 00:23:44 +0000103 IList->setType(T);
Eli Friedmand8535af2008-05-19 20:00:43 +0000104
105 if (!hadError && (Index < IList->getNumInits())) {
106 // We have leftover initializers
107 if (IList->getNumInits() > 0 &&
108 SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000109 // Special-case
Eli Friedmand8535af2008-05-19 20:00:43 +0000110 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
111 diag::err_excess_initializers_in_char_array_initializer,
112 IList->getInit(Index)->getSourceRange());
113 hadError = true;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000114 } else if (!T->isIncompleteType()) {
115 // Don't warn for incomplete types, since we'll get an error elsewhere
Eli Friedmand8535af2008-05-19 20:00:43 +0000116 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
117 diag::warn_excess_initializers,
118 IList->getInit(Index)->getSourceRange());
119 }
120 }
Eli Friedman455f7622008-05-19 20:20:43 +0000121
122 if (!hadError && T->isScalarType())
123 SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
124 IList->getSourceRange());
Steve Naroffc4d4a482008-05-01 22:18:59 +0000125}
126
Eli Friedman683cedf2008-05-19 19:16:24 +0000127void InitListChecker::CheckListElementTypes(InitListExpr *IList,
128 QualType &DeclType,
129 unsigned &Index) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000130 if (DeclType->isScalarType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000131 CheckScalarType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000132 } else if (DeclType->isVectorType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000133 CheckVectorType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000134 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000135 if (DeclType->isStructureType() || DeclType->isUnionType())
136 CheckStructUnionTypes(IList, DeclType, Index);
137 else if (DeclType->isArrayType())
138 CheckArrayType(IList, DeclType, Index);
139 else
140 assert(0 && "Aggregate that isn't a function or array?!");
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000141 } else if (DeclType->isVoidType()) {
142 // This is clearly invalid, so not much we can do here. Don't bother
143 // with a diagnostic; we'll give an error elsewhere.
144 Index++;
145 hadError = true;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000146 } else {
147 // In C, all types are either scalars or aggregates, but
148 // additional handling is needed here for C++ (and possibly others?).
149 assert(0 && "Unsupported initializer type");
150 }
151}
152
Eli Friedman683cedf2008-05-19 19:16:24 +0000153void InitListChecker::CheckSubElementType(InitListExpr *IList,
154 QualType ElemType,
155 unsigned &Index) {
156 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000157 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
158 unsigned newIndex = 0;
159 CheckExplicitInitList(SubInitList, ElemType, newIndex);
160 Index++;
Eli Friedman683cedf2008-05-19 19:16:24 +0000161 } else if (StringLiteral *lit =
162 SemaRef->IsStringLiteralInit(expr, ElemType)) {
163 SemaRef->CheckStringLiteralInit(lit, ElemType);
164 Index++;
Eli Friedmand8535af2008-05-19 20:00:43 +0000165 } else if (ElemType->isScalarType()) {
166 CheckScalarType(IList, ElemType, Index);
Eli Friedman683cedf2008-05-19 19:16:24 +0000167 } else if (expr->getType()->getAsRecordType() &&
168 SemaRef->Context.typesAreCompatible(
169 IList->getInit(Index)->getType(), ElemType)) {
170 Index++;
171 // FIXME: Add checking
172 } else {
173 CheckImplicitInitList(IList, ElemType, Index);
174 Index++;
175 }
176}
177
Steve Naroffc4d4a482008-05-01 22:18:59 +0000178void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType,
179 unsigned &Index) {
180 if (Index < IList->getNumInits()) {
181 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000182 if (isa<InitListExpr>(expr)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000183 SemaRef->Diag(IList->getLocStart(),
184 diag::err_many_braces_around_scalar_init,
185 IList->getSourceRange());
186 hadError = true;
187 ++Index;
188 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000189 }
Eli Friedmand8535af2008-05-19 20:00:43 +0000190 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
191 if (SemaRef->CheckSingleInitializer(expr, DeclType))
Eli Friedman71de9eb2008-05-19 20:12:18 +0000192 hadError = true; // types weren't compatible.
Eli Friedmand8535af2008-05-19 20:00:43 +0000193 else if (savExpr != expr)
194 // The type was promoted, update initializer list.
195 IList->setInit(Index, expr);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000196 ++Index;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000197 } else {
198 SemaRef->Diag(IList->getLocStart(),
199 diag::err_empty_scalar_initializer,
200 IList->getSourceRange());
201 hadError = true;
202 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000203 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000204}
205
206void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
207 unsigned &Index) {
208 if (Index < IList->getNumInits()) {
209 const VectorType *VT = DeclType->getAsVectorType();
210 int maxElements = VT->getNumElements();
211 QualType elementType = VT->getElementType();
212
213 for (int i = 0; i < maxElements; ++i) {
214 // Don't attempt to go past the end of the init list
215 if (Index >= IList->getNumInits())
216 break;
Eli Friedman683cedf2008-05-19 19:16:24 +0000217 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000218 }
219 }
220}
221
222void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
223 unsigned &Index) {
224 // Check for the special-case of initializing an array with a string.
225 if (Index < IList->getNumInits()) {
226 if (StringLiteral *lit =
227 SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) {
228 SemaRef->CheckStringLiteralInit(lit, DeclType);
229 ++Index;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000230 return;
231 }
232 }
233 int maxElements = numArrayElements(DeclType);
234 QualType elementType = DeclType->getAsArrayType()->getElementType();
235 int numElements = 0;
236 for (int i = 0; i < maxElements; ++i, ++numElements) {
237 // Don't attempt to go past the end of the init list
238 if (Index >= IList->getNumInits())
239 break;
Eli Friedman683cedf2008-05-19 19:16:24 +0000240 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000241 }
242 if (DeclType->isIncompleteArrayType()) {
243 // If this is an incomplete array type, the actual type needs to
244 // be calculated here
245 if (numElements == 0) {
246 // Sizing an array implicitly to zero is not allowed
247 // (It could in theory be allowed, but it doesn't really matter.)
248 SemaRef->Diag(IList->getLocStart(),
249 diag::err_at_least_one_initializer_needed_to_size_array);
250 hadError = true;
251 } else {
252 llvm::APSInt ConstVal(32);
253 ConstVal = numElements;
254 DeclType = SemaRef->Context.getConstantArrayType(elementType, ConstVal,
255 ArrayType::Normal, 0);
256 }
257 }
258}
259
260void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
261 QualType DeclType,
Eli Friedman683cedf2008-05-19 19:16:24 +0000262 unsigned &Index) {
263 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000264
Eli Friedman683cedf2008-05-19 19:16:24 +0000265 // If the record is invalid, some of it's members are invalid. To avoid
266 // confusion, we forgo checking the intializer for the entire record.
267 if (structDecl->isInvalidDecl()) {
268 hadError = true;
269 return;
270 }
271 // If structDecl is a forward declaration, this loop won't do anything;
272 // That's okay, because an error should get printed out elsewhere. It
273 // might be worthwhile to skip over the rest of the initializer, though.
274 int numMembers = numStructUnionElements(DeclType);
275 for (int i = 0; i < numMembers; i++) {
276 // Don't attempt to go past the end of the init list
277 if (Index >= IList->getNumInits())
278 break;
279 FieldDecl * curField = structDecl->getMember(i);
280 if (!curField->getIdentifier()) {
281 // Don't initialize unnamed fields, e.g. "int : 20;"
282 continue;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000283 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000284 CheckSubElementType(IList, curField->getType(), Index);
285 if (DeclType->isUnionType())
286 break;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000287 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000288 // FIXME: Implement flexible array initialization GCC extension (it's a
289 // really messy extension to implement, unfortunately...the necessary
290 // information isn't actually even here!)
Steve Naroffc4d4a482008-05-01 22:18:59 +0000291}
292} // end namespace clang
293