blob: 35dda28d4adfd55bd2befa7e107610149ae42ebe [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) {
Eli Friedman46f81662008-05-25 13:22:35 +000031 // FIXME: use a proper constant
32 int maxElements = 0x7FFFFFFF;
33 if (const ConstantArrayType *CAT = DeclType->getAsConstantArrayType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +000034 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
35 }
36 return maxElements;
37}
38
39int InitListChecker::numStructUnionElements(QualType DeclType) {
40 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Eli Friedman683cedf2008-05-19 19:16:24 +000041 if (structDecl->getKind() == Decl::Union)
42 return std::min(structDecl->getNumMembers(), 1);
Steve Naroffc4d4a482008-05-01 22:18:59 +000043 return structDecl->getNumMembers() - structDecl->hasFlexibleArrayMember();
44}
45
46void 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 Friedman683cedf2008-05-19 19:16:24 +000055 else if (T->isVectorType())
56 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroffc4d4a482008-05-01 22:18:59 +000057 else
58 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman683cedf2008-05-19 19:16:24 +000059
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 Naroffc4d4a482008-05-01 22:18:59 +000065 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 Friedmand8535af2008-05-19 20:00:43 +000080
Steve Naroffc4d4a482008-05-01 22:18:59 +000081 // Modify the parent InitListExpr to point to the implicit InitListExpr.
82 ParentIList->addInit(Index, ILE);
Steve Naroffc4d4a482008-05-01 22:18:59 +000083}
84
Steve Naroff56099522008-05-06 00:23:44 +000085void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Steve Naroffc4d4a482008-05-01 22:18:59 +000086 unsigned &Index) {
Eli Friedmand8535af2008-05-19 20:00:43 +000087 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Eli Friedmand8535af2008-05-19 20:00:43 +000088
Eli Friedman683cedf2008-05-19 19:16:24 +000089 CheckListElementTypes(IList, T, Index);
Steve Naroff56099522008-05-06 00:23:44 +000090 IList->setType(T);
Eli Friedman46f81662008-05-25 13:22:35 +000091 if (hadError)
92 return;
Eli Friedmand8535af2008-05-19 20:00:43 +000093
Eli Friedman46f81662008-05-25 13:22:35 +000094 if (Index < IList->getNumInits()) {
Eli Friedmand8535af2008-05-19 20:00:43 +000095 // We have leftover initializers
96 if (IList->getNumInits() > 0 &&
97 SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +000098 // Special-case
Eli Friedmand8535af2008-05-19 20:00:43 +000099 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
100 diag::err_excess_initializers_in_char_array_initializer,
101 IList->getInit(Index)->getSourceRange());
102 hadError = true;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000103 } else if (!T->isIncompleteType()) {
104 // Don't warn for incomplete types, since we'll get an error elsewhere
Eli Friedmand8535af2008-05-19 20:00:43 +0000105 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
106 diag::warn_excess_initializers,
107 IList->getInit(Index)->getSourceRange());
108 }
109 }
Eli Friedman455f7622008-05-19 20:20:43 +0000110
Eli Friedman46f81662008-05-25 13:22:35 +0000111 if (T->isScalarType())
Eli Friedman455f7622008-05-19 20:20:43 +0000112 SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
113 IList->getSourceRange());
Steve Naroffc4d4a482008-05-01 22:18:59 +0000114}
115
Eli Friedman683cedf2008-05-19 19:16:24 +0000116void InitListChecker::CheckListElementTypes(InitListExpr *IList,
117 QualType &DeclType,
118 unsigned &Index) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000119 if (DeclType->isScalarType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000120 CheckScalarType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000121 } else if (DeclType->isVectorType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000122 CheckVectorType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000123 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000124 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 Friedmanb9ea6bc2008-05-20 05:25:56 +0000130 } 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 Naroffc4d4a482008-05-01 22:18:59 +0000135 } 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 Friedman683cedf2008-05-19 19:16:24 +0000142void InitListChecker::CheckSubElementType(InitListExpr *IList,
143 QualType ElemType,
144 unsigned &Index) {
145 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000146 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
147 unsigned newIndex = 0;
148 CheckExplicitInitList(SubInitList, ElemType, newIndex);
149 Index++;
Eli Friedman683cedf2008-05-19 19:16:24 +0000150 } else if (StringLiteral *lit =
151 SemaRef->IsStringLiteralInit(expr, ElemType)) {
152 SemaRef->CheckStringLiteralInit(lit, ElemType);
153 Index++;
Eli Friedmand8535af2008-05-19 20:00:43 +0000154 } else if (ElemType->isScalarType()) {
155 CheckScalarType(IList, ElemType, Index);
Eli Friedman683cedf2008-05-19 19:16:24 +0000156 } else if (expr->getType()->getAsRecordType() &&
Eli Friedman46f81662008-05-25 13:22:35 +0000157 SemaRef->Context.typesAreCompatible(expr->getType(), ElemType)) {
Eli Friedman683cedf2008-05-19 19:16:24 +0000158 Index++;
159 // FIXME: Add checking
160 } else {
161 CheckImplicitInitList(IList, ElemType, Index);
162 Index++;
163 }
164}
165
Steve Naroffc4d4a482008-05-01 22:18:59 +0000166void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType,
167 unsigned &Index) {
168 if (Index < IList->getNumInits()) {
169 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000170 if (isa<InitListExpr>(expr)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000171 SemaRef->Diag(IList->getLocStart(),
172 diag::err_many_braces_around_scalar_init,
173 IList->getSourceRange());
174 hadError = true;
175 ++Index;
176 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000177 }
Eli Friedmand8535af2008-05-19 20:00:43 +0000178 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
179 if (SemaRef->CheckSingleInitializer(expr, DeclType))
Eli Friedman71de9eb2008-05-19 20:12:18 +0000180 hadError = true; // types weren't compatible.
Eli Friedmand8535af2008-05-19 20:00:43 +0000181 else if (savExpr != expr)
182 // The type was promoted, update initializer list.
183 IList->setInit(Index, expr);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000184 ++Index;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000185 } else {
186 SemaRef->Diag(IList->getLocStart(),
187 diag::err_empty_scalar_initializer,
188 IList->getSourceRange());
189 hadError = true;
190 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000191 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000192}
193
194void 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 Friedman683cedf2008-05-19 19:16:24 +0000205 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000206 }
207 }
208}
209
210void 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 Naroffc4d4a482008-05-01 22:18:59 +0000218 return;
219 }
220 }
Eli Friedman46f81662008-05-25 13:22:35 +0000221 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 Naroffc4d4a482008-05-01 22:18:59 +0000232 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 Friedman683cedf2008-05-19 19:16:24 +0000239 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000240 }
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
259void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
260 QualType DeclType,
Eli Friedman683cedf2008-05-19 19:16:24 +0000261 unsigned &Index) {
262 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000263
Eli Friedman683cedf2008-05-19 19:16:24 +0000264 // 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 Naroffc4d4a482008-05-01 22:18:59 +0000282 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000283 CheckSubElementType(IList, curField->getType(), Index);
284 if (DeclType->isUnionType())
285 break;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000286 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000287 // 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 Naroffc4d4a482008-05-01 22:18:59 +0000290}
291} // end namespace clang
292