blob: cb7f5324e398be5ed65e1b6069ffb1487478ee38 [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"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroffc4d4a482008-05-01 22:18:59 +000019
20namespace clang {
21
22InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) {
23 hadError = false;
24 SemaRef = S;
Eli Friedmand8535af2008-05-19 20:00:43 +000025
Eli Friedman683cedf2008-05-19 19:16:24 +000026 unsigned newIndex = 0;
Eli Friedmand8535af2008-05-19 20:00:43 +000027
Eli Friedman683cedf2008-05-19 19:16:24 +000028 CheckExplicitInitList(IL, T, newIndex);
Steve Naroffc4d4a482008-05-01 22:18:59 +000029}
30
31int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman46f81662008-05-25 13:22:35 +000032 // FIXME: use a proper constant
33 int maxElements = 0x7FFFFFFF;
Chris Lattnera1923f62008-08-04 07:31:14 +000034 if (const ConstantArrayType *CAT =
35 SemaRef->Context.getAsConstantArrayType(DeclType)) {
Steve Naroffc4d4a482008-05-01 22:18:59 +000036 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
37 }
38 return maxElements;
39}
40
41int InitListChecker::numStructUnionElements(QualType DeclType) {
42 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Eli Friedman9f5250b2008-05-25 14:03:31 +000043 int InitializableMembers = 0;
44 for (int i = 0; i < structDecl->getNumMembers(); i++)
45 if (structDecl->getMember(i)->getIdentifier())
46 ++InitializableMembers;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000047 if (structDecl->isUnion())
Eli Friedman9f5250b2008-05-25 14:03:31 +000048 return std::min(InitializableMembers, 1);
49 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroffc4d4a482008-05-01 22:18:59 +000050}
51
52void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
53 QualType T, unsigned &Index) {
54 llvm::SmallVector<Expr*, 4> InitExprs;
55 int maxElements = 0;
56
57 if (T->isArrayType())
58 maxElements = numArrayElements(T);
59 else if (T->isStructureType() || T->isUnionType())
60 maxElements = numStructUnionElements(T);
Eli Friedman683cedf2008-05-19 19:16:24 +000061 else if (T->isVectorType())
62 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroffc4d4a482008-05-01 22:18:59 +000063 else
64 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedman683cedf2008-05-19 19:16:24 +000065
Eli Friedmanf8df28c2008-05-25 13:49:22 +000066 if (maxElements == 0) {
67 SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(),
68 diag::err_implicit_empty_initializer);
69 hadError = true;
70 return;
71 }
72
Eli Friedman683cedf2008-05-19 19:16:24 +000073 // 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 Friedman46f81662008-05-25 13:22:35 +0000104 if (hadError)
105 return;
Eli Friedmand8535af2008-05-19 20:00:43 +0000106
Eli Friedman46f81662008-05-25 13:22:35 +0000107 if (Index < IList->getNumInits()) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000108 // We have leftover initializers
109 if (IList->getNumInits() > 0 &&
110 SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000111 // Special-case
Eli Friedmand8535af2008-05-19 20:00:43 +0000112 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
113 diag::err_excess_initializers_in_char_array_initializer,
114 IList->getInit(Index)->getSourceRange());
115 hadError = true;
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000116 } else if (!T->isIncompleteType()) {
117 // Don't warn for incomplete types, since we'll get an error elsewhere
Eli Friedmand8535af2008-05-19 20:00:43 +0000118 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
119 diag::warn_excess_initializers,
120 IList->getInit(Index)->getSourceRange());
121 }
122 }
Eli Friedman455f7622008-05-19 20:20:43 +0000123
Eli Friedman46f81662008-05-25 13:22:35 +0000124 if (T->isScalarType())
Eli Friedman455f7622008-05-19 20:20:43 +0000125 SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
126 IList->getSourceRange());
Steve Naroffc4d4a482008-05-01 22:18:59 +0000127}
128
Eli Friedman683cedf2008-05-19 19:16:24 +0000129void InitListChecker::CheckListElementTypes(InitListExpr *IList,
130 QualType &DeclType,
131 unsigned &Index) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000132 if (DeclType->isScalarType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000133 CheckScalarType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000134 } else if (DeclType->isVectorType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000135 CheckVectorType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000136 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000137 if (DeclType->isStructureType() || DeclType->isUnionType())
138 CheckStructUnionTypes(IList, DeclType, Index);
139 else if (DeclType->isArrayType())
140 CheckArrayType(IList, DeclType, Index);
141 else
142 assert(0 && "Aggregate that isn't a function or array?!");
Steve Naroffff5b3a82008-08-10 16:05:48 +0000143 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
144 // This type is invalid, issue a diagnostic.
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000145 Index++;
Steve Naroffff5b3a82008-08-10 16:05:48 +0000146 SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type,
147 DeclType.getAsString());
Eli Friedmanb9ea6bc2008-05-20 05:25:56 +0000148 hadError = true;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000149 } else {
150 // In C, all types are either scalars or aggregates, but
151 // additional handling is needed here for C++ (and possibly others?).
152 assert(0 && "Unsupported initializer type");
153 }
154}
155
Eli Friedman683cedf2008-05-19 19:16:24 +0000156void InitListChecker::CheckSubElementType(InitListExpr *IList,
157 QualType ElemType,
158 unsigned &Index) {
159 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000160 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
161 unsigned newIndex = 0;
162 CheckExplicitInitList(SubInitList, ElemType, newIndex);
163 Index++;
Eli Friedman683cedf2008-05-19 19:16:24 +0000164 } else if (StringLiteral *lit =
165 SemaRef->IsStringLiteralInit(expr, ElemType)) {
166 SemaRef->CheckStringLiteralInit(lit, ElemType);
167 Index++;
Eli Friedmand8535af2008-05-19 20:00:43 +0000168 } else if (ElemType->isScalarType()) {
169 CheckScalarType(IList, ElemType, Index);
Eli Friedman683cedf2008-05-19 19:16:24 +0000170 } else if (expr->getType()->getAsRecordType() &&
Eli Friedman2ccfb512008-06-09 03:52:40 +0000171 SemaRef->Context.typesAreCompatible(
172 expr->getType().getUnqualifiedType(),
173 ElemType.getUnqualifiedType())) {
Eli Friedman683cedf2008-05-19 19:16:24 +0000174 Index++;
175 // FIXME: Add checking
176 } else {
177 CheckImplicitInitList(IList, ElemType, Index);
178 Index++;
179 }
180}
181
Steve Naroffc4d4a482008-05-01 22:18:59 +0000182void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType,
183 unsigned &Index) {
184 if (Index < IList->getNumInits()) {
185 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000186 if (isa<InitListExpr>(expr)) {
Eli Friedman71de9eb2008-05-19 20:12:18 +0000187 SemaRef->Diag(IList->getLocStart(),
188 diag::err_many_braces_around_scalar_init,
189 IList->getSourceRange());
190 hadError = true;
191 ++Index;
192 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000193 }
Eli Friedmand8535af2008-05-19 20:00:43 +0000194 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
195 if (SemaRef->CheckSingleInitializer(expr, DeclType))
Eli Friedman71de9eb2008-05-19 20:12:18 +0000196 hadError = true; // types weren't compatible.
Eli Friedmand8535af2008-05-19 20:00:43 +0000197 else if (savExpr != expr)
198 // The type was promoted, update initializer list.
199 IList->setInit(Index, expr);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000200 ++Index;
Eli Friedman71de9eb2008-05-19 20:12:18 +0000201 } else {
202 SemaRef->Diag(IList->getLocStart(),
203 diag::err_empty_scalar_initializer,
204 IList->getSourceRange());
205 hadError = true;
206 return;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000207 }
Steve Naroffc4d4a482008-05-01 22:18:59 +0000208}
209
210void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
211 unsigned &Index) {
212 if (Index < IList->getNumInits()) {
213 const VectorType *VT = DeclType->getAsVectorType();
214 int maxElements = VT->getNumElements();
215 QualType elementType = VT->getElementType();
216
217 for (int i = 0; i < maxElements; ++i) {
218 // Don't attempt to go past the end of the init list
219 if (Index >= IList->getNumInits())
220 break;
Eli Friedman683cedf2008-05-19 19:16:24 +0000221 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000222 }
223 }
224}
225
226void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
227 unsigned &Index) {
228 // Check for the special-case of initializing an array with a string.
229 if (Index < IList->getNumInits()) {
230 if (StringLiteral *lit =
231 SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) {
232 SemaRef->CheckStringLiteralInit(lit, DeclType);
233 ++Index;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000234 return;
235 }
236 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000237 if (const VariableArrayType *VAT =
238 SemaRef->Context.getAsVariableArrayType(DeclType)) {
Eli Friedman46f81662008-05-25 13:22:35 +0000239 // Check for VLAs; in standard C it would be possible to check this
240 // earlier, but I don't know where clang accepts VLAs (gcc accepts
241 // them in all sorts of strange places).
242 SemaRef->Diag(VAT->getSizeExpr()->getLocStart(),
243 diag::err_variable_object_no_init,
244 VAT->getSizeExpr()->getSourceRange());
245 hadError = true;
246 return;
247 }
248
Steve Naroffc4d4a482008-05-01 22:18:59 +0000249 int maxElements = numArrayElements(DeclType);
Chris Lattnera1923f62008-08-04 07:31:14 +0000250 QualType elementType = SemaRef->Context.getAsArrayType(DeclType)
251 ->getElementType();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000252 int numElements = 0;
253 for (int i = 0; i < maxElements; ++i, ++numElements) {
254 // Don't attempt to go past the end of the init list
255 if (Index >= IList->getNumInits())
256 break;
Eli Friedman683cedf2008-05-19 19:16:24 +0000257 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000258 }
259 if (DeclType->isIncompleteArrayType()) {
260 // If this is an incomplete array type, the actual type needs to
261 // be calculated here
262 if (numElements == 0) {
263 // Sizing an array implicitly to zero is not allowed
264 // (It could in theory be allowed, but it doesn't really matter.)
265 SemaRef->Diag(IList->getLocStart(),
266 diag::err_at_least_one_initializer_needed_to_size_array);
267 hadError = true;
268 } else {
269 llvm::APSInt ConstVal(32);
270 ConstVal = numElements;
271 DeclType = SemaRef->Context.getConstantArrayType(elementType, ConstVal,
272 ArrayType::Normal, 0);
273 }
274 }
275}
276
277void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
278 QualType DeclType,
Eli Friedman683cedf2008-05-19 19:16:24 +0000279 unsigned &Index) {
280 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000281
Eli Friedman683cedf2008-05-19 19:16:24 +0000282 // If the record is invalid, some of it's members are invalid. To avoid
283 // confusion, we forgo checking the intializer for the entire record.
284 if (structDecl->isInvalidDecl()) {
285 hadError = true;
286 return;
287 }
288 // If structDecl is a forward declaration, this loop won't do anything;
289 // That's okay, because an error should get printed out elsewhere. It
290 // might be worthwhile to skip over the rest of the initializer, though.
Eli Friedman171a4632008-08-09 23:45:45 +0000291 int numMembers = DeclType->getAsRecordType()->getDecl()->getNumMembers() -
292 structDecl->hasFlexibleArrayMember();
Eli Friedman683cedf2008-05-19 19:16:24 +0000293 for (int i = 0; i < numMembers; i++) {
294 // Don't attempt to go past the end of the init list
295 if (Index >= IList->getNumInits())
296 break;
297 FieldDecl * curField = structDecl->getMember(i);
298 if (!curField->getIdentifier()) {
299 // Don't initialize unnamed fields, e.g. "int : 20;"
300 continue;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000301 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000302 CheckSubElementType(IList, curField->getType(), Index);
303 if (DeclType->isUnionType())
304 break;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000305 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000306 // FIXME: Implement flexible array initialization GCC extension (it's a
307 // really messy extension to implement, unfortunately...the necessary
308 // information isn't actually even here!)
Steve Naroffc4d4a482008-05-01 22:18:59 +0000309}
310} // end namespace clang
311