blob: fb5329cfbb0aea1dae35da707f4b419b4638e8dd [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");
101 if (T->isScalarType())
102 SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init,
103 IList->getSourceRange());
104
Eli Friedman683cedf2008-05-19 19:16:24 +0000105 CheckListElementTypes(IList, T, Index);
Steve Naroff56099522008-05-06 00:23:44 +0000106 IList->setType(T);
Eli Friedmand8535af2008-05-19 20:00:43 +0000107
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 Naroffc4d4a482008-05-01 22:18:59 +0000123}
124
Eli Friedman683cedf2008-05-19 19:16:24 +0000125void InitListChecker::CheckListElementTypes(InitListExpr *IList,
126 QualType &DeclType,
127 unsigned &Index) {
Eli Friedmand8535af2008-05-19 20:00:43 +0000128 if (DeclType->isScalarType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000129 CheckScalarType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000130 } else if (DeclType->isVectorType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000131 CheckVectorType(IList, DeclType, Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000132 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
Steve Naroffc4d4a482008-05-01 22:18:59 +0000133 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 Friedman683cedf2008-05-19 19:16:24 +0000146void InitListChecker::CheckSubElementType(InitListExpr *IList,
147 QualType ElemType,
148 unsigned &Index) {
149 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000150 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
151 unsigned newIndex = 0;
152 CheckExplicitInitList(SubInitList, ElemType, newIndex);
153 Index++;
Eli Friedman683cedf2008-05-19 19:16:24 +0000154 } else if (StringLiteral *lit =
155 SemaRef->IsStringLiteralInit(expr, ElemType)) {
156 SemaRef->CheckStringLiteralInit(lit, ElemType);
157 Index++;
Eli Friedmand8535af2008-05-19 20:00:43 +0000158 } else if (ElemType->isScalarType()) {
159 CheckScalarType(IList, ElemType, Index);
Eli Friedman683cedf2008-05-19 19:16:24 +0000160 } 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 Naroffc4d4a482008-05-01 22:18:59 +0000171void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType,
172 unsigned &Index) {
173 if (Index < IList->getNumInits()) {
174 Expr* expr = IList->getInit(Index);
Eli Friedmand8535af2008-05-19 20:00:43 +0000175 if (isa<InitListExpr>(expr)) {
176 // FIXME: Print error about too many braces
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))
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 Naroffc4d4a482008-05-01 22:18:59 +0000184 ++Index;
185 }
186 // FIXME: Should an error be reported for empty initializer list + scalar?
187}
188
189void 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 Friedman683cedf2008-05-19 19:16:24 +0000200 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000201 }
202 }
203}
204
205void 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 Friedman683cedf2008-05-19 19:16:24 +0000213#if 0
Steve Naroffc4d4a482008-05-01 22:18:59 +0000214 if (IList->isExplicit() && Index < IList->getNumInits()) {
215 // We have leftover initializers; warn
Eli Friedmand8535af2008-05-19 20:00:43 +0000216
Steve Naroffc4d4a482008-05-01 22:18:59 +0000217 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000218#endif
Steve Naroffc4d4a482008-05-01 22:18:59 +0000219 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 Friedman683cedf2008-05-19 19:16:24 +0000229 CheckSubElementType(IList, elementType, Index);
Steve Naroffc4d4a482008-05-01 22:18:59 +0000230 }
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
249void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
250 QualType DeclType,
Eli Friedman683cedf2008-05-19 19:16:24 +0000251 unsigned &Index) {
252 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroffc4d4a482008-05-01 22:18:59 +0000253
Eli Friedman683cedf2008-05-19 19:16:24 +0000254 // 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 Naroffc4d4a482008-05-01 22:18:59 +0000272 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000273 CheckSubElementType(IList, curField->getType(), Index);
274 if (DeclType->isUnionType())
275 break;
Steve Naroffc4d4a482008-05-01 22:18:59 +0000276 }
Eli Friedman683cedf2008-05-19 19:16:24 +0000277 // 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 Naroffc4d4a482008-05-01 22:18:59 +0000280}
281} // end namespace clang
282