blob: 031fe27ddf7515779852a85367c8a6fb8bd5b1ec [file] [log] [blame]
Steve Naroff0cca7492008-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"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000018#include <algorithm> // for std::count_if
19#include <functional> // for std::mem_fun
Steve Naroff0cca7492008-05-01 22:18:59 +000020
21namespace clang {
22
23InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) {
24 hadError = false;
25 SemaRef = S;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +000026
Eli Friedmanb85f7072008-05-19 19:16:24 +000027 unsigned newIndex = 0;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +000028
Eli Friedmanb85f7072008-05-19 19:16:24 +000029 CheckExplicitInitList(IL, T, newIndex);
Steve Naroff0cca7492008-05-01 22:18:59 +000030}
31
32int InitListChecker::numArrayElements(QualType DeclType) {
Eli Friedman638e1442008-05-25 13:22:35 +000033 // FIXME: use a proper constant
34 int maxElements = 0x7FFFFFFF;
Chris Lattnerc63a1f22008-08-04 07:31:14 +000035 if (const ConstantArrayType *CAT =
36 SemaRef->Context.getAsConstantArrayType(DeclType)) {
Steve Naroff0cca7492008-05-01 22:18:59 +000037 maxElements = static_cast<int>(CAT->getSize().getZExtValue());
38 }
39 return maxElements;
40}
41
42int InitListChecker::numStructUnionElements(QualType DeclType) {
43 RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
Douglas Gregore267ff32008-12-11 20:41:00 +000044 const int InitializableMembers
Douglas Gregor44b43212008-12-11 16:49:14 +000045 = std::count_if(structDecl->field_begin(), structDecl->field_end(),
46 std::mem_fun(&FieldDecl::getDeclName));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000047 if (structDecl->isUnion())
Eli Friedmanf84eda32008-05-25 14:03:31 +000048 return std::min(InitializableMembers, 1);
49 return InitializableMembers - structDecl->hasFlexibleArrayMember();
Steve Naroff0cca7492008-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 Friedmanb85f7072008-05-19 19:16:24 +000061 else if (T->isVectorType())
62 maxElements = T->getAsVectorType()->getNumElements();
Steve Naroff0cca7492008-05-01 22:18:59 +000063 else
64 assert(0 && "CheckImplicitInitList(): Illegal type");
Eli Friedmanb85f7072008-05-19 19:16:24 +000065
Eli Friedman402256f2008-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 Friedmanb85f7072008-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 Naroff0cca7492008-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(),
Chris Lattner418f6c72008-10-26 23:43:26 +000091 SourceLocation(),
92 ParentIList->hadDesignators());
Steve Naroff0cca7492008-05-01 22:18:59 +000093 ILE->setType(T);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +000094
Steve Naroff0cca7492008-05-01 22:18:59 +000095 // Modify the parent InitListExpr to point to the implicit InitListExpr.
96 ParentIList->addInit(Index, ILE);
Steve Naroff0cca7492008-05-01 22:18:59 +000097}
98
Steve Naroffa647caa2008-05-06 00:23:44 +000099void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
Steve Naroff0cca7492008-05-01 22:18:59 +0000100 unsigned &Index) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000101 assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000102
Eli Friedmanb85f7072008-05-19 19:16:24 +0000103 CheckListElementTypes(IList, T, Index);
Steve Naroffa647caa2008-05-06 00:23:44 +0000104 IList->setType(T);
Eli Friedman638e1442008-05-25 13:22:35 +0000105 if (hadError)
106 return;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000107
Eli Friedman638e1442008-05-25 13:22:35 +0000108 if (Index < IList->getNumInits()) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000109 // We have leftover initializers
110 if (IList->getNumInits() > 0 &&
111 SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
Eli Friedmanbb504d32008-05-19 20:12:18 +0000112 // Special-case
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000113 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000114 diag::err_excess_initializers_in_char_array_initializer)
115 << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000116 hadError = true;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000117 } else if (!T->isIncompleteType()) {
118 // Don't warn for incomplete types, since we'll get an error elsewhere
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000119 SemaRef->Diag(IList->getInit(Index)->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000120 diag::warn_excess_initializers)
121 << IList->getInit(Index)->getSourceRange();
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000122 }
123 }
Eli Friedmancda25a92008-05-19 20:20:43 +0000124
Eli Friedman638e1442008-05-25 13:22:35 +0000125 if (T->isScalarType())
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000126 SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
127 << IList->getSourceRange();
Steve Naroff0cca7492008-05-01 22:18:59 +0000128}
129
Eli Friedmanb85f7072008-05-19 19:16:24 +0000130void InitListChecker::CheckListElementTypes(InitListExpr *IList,
131 QualType &DeclType,
132 unsigned &Index) {
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000133 if (DeclType->isScalarType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000134 CheckScalarType(IList, DeclType, Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000135 } else if (DeclType->isVectorType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000136 CheckVectorType(IList, DeclType, Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000137 } else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
Steve Naroff0cca7492008-05-01 22:18:59 +0000138 if (DeclType->isStructureType() || DeclType->isUnionType())
139 CheckStructUnionTypes(IList, DeclType, Index);
140 else if (DeclType->isArrayType())
141 CheckArrayType(IList, DeclType, Index);
142 else
143 assert(0 && "Aggregate that isn't a function or array?!");
Steve Naroff61353522008-08-10 16:05:48 +0000144 } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
145 // This type is invalid, issue a diagnostic.
Eli Friedmand8dc2102008-05-20 05:25:56 +0000146 Index++;
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000147 SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000148 << DeclType;
Eli Friedmand8dc2102008-05-20 05:25:56 +0000149 hadError = true;
Steve Naroff0cca7492008-05-01 22:18:59 +0000150 } else {
151 // In C, all types are either scalars or aggregates, but
152 // additional handling is needed here for C++ (and possibly others?).
153 assert(0 && "Unsupported initializer type");
154 }
155}
156
Eli Friedmanb85f7072008-05-19 19:16:24 +0000157void InitListChecker::CheckSubElementType(InitListExpr *IList,
158 QualType ElemType,
159 unsigned &Index) {
160 Expr* expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000161 if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
162 unsigned newIndex = 0;
163 CheckExplicitInitList(SubInitList, ElemType, newIndex);
164 Index++;
Eli Friedmanb85f7072008-05-19 19:16:24 +0000165 } else if (StringLiteral *lit =
166 SemaRef->IsStringLiteralInit(expr, ElemType)) {
167 SemaRef->CheckStringLiteralInit(lit, ElemType);
168 Index++;
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000169 } else if (ElemType->isScalarType()) {
170 CheckScalarType(IList, ElemType, Index);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000171 } else if (expr->getType()->getAsRecordType() &&
Eli Friedmanc92e5e42008-06-09 03:52:40 +0000172 SemaRef->Context.typesAreCompatible(
173 expr->getType().getUnqualifiedType(),
174 ElemType.getUnqualifiedType())) {
Eli Friedmanb85f7072008-05-19 19:16:24 +0000175 Index++;
176 // FIXME: Add checking
177 } else {
178 CheckImplicitInitList(IList, ElemType, Index);
179 Index++;
180 }
181}
182
Steve Naroff0cca7492008-05-01 22:18:59 +0000183void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType,
184 unsigned &Index) {
185 if (Index < IList->getNumInits()) {
186 Expr* expr = IList->getInit(Index);
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000187 if (isa<InitListExpr>(expr)) {
Eli Friedmanbb504d32008-05-19 20:12:18 +0000188 SemaRef->Diag(IList->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000189 diag::err_many_braces_around_scalar_init)
190 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000191 hadError = true;
192 ++Index;
193 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000194 }
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000195 Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
196 if (SemaRef->CheckSingleInitializer(expr, DeclType))
Eli Friedmanbb504d32008-05-19 20:12:18 +0000197 hadError = true; // types weren't compatible.
Eli Friedmanc9c0ea62008-05-19 20:00:43 +0000198 else if (savExpr != expr)
199 // The type was promoted, update initializer list.
200 IList->setInit(Index, expr);
Steve Naroff0cca7492008-05-01 22:18:59 +0000201 ++Index;
Eli Friedmanbb504d32008-05-19 20:12:18 +0000202 } else {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000203 SemaRef->Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
204 << IList->getSourceRange();
Eli Friedmanbb504d32008-05-19 20:12:18 +0000205 hadError = true;
206 return;
Steve Naroff0cca7492008-05-01 22:18:59 +0000207 }
Steve Naroff0cca7492008-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 Friedmanb85f7072008-05-19 19:16:24 +0000221 CheckSubElementType(IList, elementType, Index);
Steve Naroff0cca7492008-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 Naroff0cca7492008-05-01 22:18:59 +0000234 return;
235 }
236 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000237 if (const VariableArrayType *VAT =
238 SemaRef->Context.getAsVariableArrayType(DeclType)) {
Eli Friedman638e1442008-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(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000243 diag::err_variable_object_no_init)
244 << VAT->getSizeExpr()->getSourceRange();
Eli Friedman638e1442008-05-25 13:22:35 +0000245 hadError = true;
246 return;
247 }
248
Steve Naroff0cca7492008-05-01 22:18:59 +0000249 int maxElements = numArrayElements(DeclType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000250 QualType elementType = SemaRef->Context.getAsArrayType(DeclType)
251 ->getElementType();
Steve Naroff0cca7492008-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 Friedmanb85f7072008-05-19 19:16:24 +0000257 CheckSubElementType(IList, elementType, Index);
Steve Naroff0cca7492008-05-01 22:18:59 +0000258 }
259 if (DeclType->isIncompleteArrayType()) {
260 // If this is an incomplete array type, the actual type needs to
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000261 // be calculated here.
Steve Naroff0cca7492008-05-01 22:18:59 +0000262 if (numElements == 0) {
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000263 // Sizing an array implicitly to zero is not allowed by ISO C,
264 // but is supported by GNU.
Steve Naroff0cca7492008-05-01 22:18:59 +0000265 SemaRef->Diag(IList->getLocStart(),
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000266 diag::ext_typecheck_zero_array_size);
Steve Naroff0cca7492008-05-01 22:18:59 +0000267 }
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000268
269 llvm::APSInt ConstVal(32);
270 ConstVal = numElements;
271 DeclType = SemaRef->Context.getConstantArrayType(elementType, ConstVal,
272 ArrayType::Normal, 0);
Steve Naroff0cca7492008-05-01 22:18:59 +0000273 }
274}
275
276void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
277 QualType DeclType,
Eli Friedmanb85f7072008-05-19 19:16:24 +0000278 unsigned &Index) {
279 RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
Steve Naroff0cca7492008-05-01 22:18:59 +0000280
Eli Friedmanb85f7072008-05-19 19:16:24 +0000281 // If the record is invalid, some of it's members are invalid. To avoid
282 // confusion, we forgo checking the intializer for the entire record.
283 if (structDecl->isInvalidDecl()) {
284 hadError = true;
285 return;
286 }
287 // If structDecl is a forward declaration, this loop won't do anything;
288 // That's okay, because an error should get printed out elsewhere. It
289 // might be worthwhile to skip over the rest of the initializer, though.
Douglas Gregor44b43212008-12-11 16:49:14 +0000290 RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
291 for (RecordDecl::field_iterator Field = RD->field_begin(),
292 FieldEnd = RD->field_end();
293 Field != FieldEnd; ++Field) {
294 // If we've hit the flexible array member at the end, we're done.
295 if (Field->getType()->isIncompleteArrayType())
296 break;
297
Eli Friedmanb85f7072008-05-19 19:16:24 +0000298 // Don't attempt to go past the end of the init list
299 if (Index >= IList->getNumInits())
300 break;
Douglas Gregor44b43212008-12-11 16:49:14 +0000301
302 if (!Field->getIdentifier()) {
Eli Friedmanb85f7072008-05-19 19:16:24 +0000303 // Don't initialize unnamed fields, e.g. "int : 20;"
304 continue;
Steve Naroff0cca7492008-05-01 22:18:59 +0000305 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000306
307 CheckSubElementType(IList, Field->getType(), Index);
Eli Friedmanb85f7072008-05-19 19:16:24 +0000308 if (DeclType->isUnionType())
309 break;
Steve Naroff0cca7492008-05-01 22:18:59 +0000310 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000311
Eli Friedmanb85f7072008-05-19 19:16:24 +0000312 // FIXME: Implement flexible array initialization GCC extension (it's a
313 // really messy extension to implement, unfortunately...the necessary
314 // information isn't actually even here!)
Steve Naroff0cca7492008-05-01 22:18:59 +0000315}
316} // end namespace clang
317