2008-05-02 06:18:59 +08:00
|
|
|
//===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for initializers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2008-08-11 11:27:53 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-12-12 00:49:14 +08:00
|
|
|
#include <algorithm> // for std::count_if
|
|
|
|
#include <functional> // for std::mem_fun
|
2008-05-02 06:18:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
InitListChecker::InitListChecker(Sema *S, InitListExpr *IL, QualType &T) {
|
|
|
|
hadError = false;
|
|
|
|
SemaRef = S;
|
2008-05-20 04:00:43 +08:00
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
unsigned newIndex = 0;
|
2008-05-20 04:00:43 +08:00
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
CheckExplicitInitList(IL, T, newIndex);
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int InitListChecker::numArrayElements(QualType DeclType) {
|
2008-05-25 21:22:35 +08:00
|
|
|
// FIXME: use a proper constant
|
|
|
|
int maxElements = 0x7FFFFFFF;
|
2008-08-04 15:31:14 +08:00
|
|
|
if (const ConstantArrayType *CAT =
|
|
|
|
SemaRef->Context.getAsConstantArrayType(DeclType)) {
|
2008-05-02 06:18:59 +08:00
|
|
|
maxElements = static_cast<int>(CAT->getSize().getZExtValue());
|
|
|
|
}
|
|
|
|
return maxElements;
|
|
|
|
}
|
|
|
|
|
|
|
|
int InitListChecker::numStructUnionElements(QualType DeclType) {
|
|
|
|
RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
|
2008-12-12 04:41:00 +08:00
|
|
|
const int InitializableMembers
|
2008-12-12 00:49:14 +08:00
|
|
|
= std::count_if(structDecl->field_begin(), structDecl->field_end(),
|
|
|
|
std::mem_fun(&FieldDecl::getDeclName));
|
2008-06-10 07:19:58 +08:00
|
|
|
if (structDecl->isUnion())
|
2008-05-25 22:03:31 +08:00
|
|
|
return std::min(InitializableMembers, 1);
|
|
|
|
return InitializableMembers - structDecl->hasFlexibleArrayMember();
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
|
|
|
|
QualType T, unsigned &Index) {
|
|
|
|
llvm::SmallVector<Expr*, 4> InitExprs;
|
|
|
|
int maxElements = 0;
|
|
|
|
|
|
|
|
if (T->isArrayType())
|
|
|
|
maxElements = numArrayElements(T);
|
|
|
|
else if (T->isStructureType() || T->isUnionType())
|
|
|
|
maxElements = numStructUnionElements(T);
|
2008-05-20 03:16:24 +08:00
|
|
|
else if (T->isVectorType())
|
|
|
|
maxElements = T->getAsVectorType()->getNumElements();
|
2008-05-02 06:18:59 +08:00
|
|
|
else
|
|
|
|
assert(0 && "CheckImplicitInitList(): Illegal type");
|
2008-05-20 03:16:24 +08:00
|
|
|
|
2008-05-25 21:49:22 +08:00
|
|
|
if (maxElements == 0) {
|
|
|
|
SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(),
|
|
|
|
diag::err_implicit_empty_initializer);
|
|
|
|
hadError = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
// Check the element types *before* we create the implicit init list;
|
|
|
|
// otherwise, we might end up taking the wrong number of elements
|
|
|
|
unsigned NewIndex = Index;
|
|
|
|
CheckListElementTypes(ParentIList, T, NewIndex);
|
|
|
|
|
2008-05-02 06:18:59 +08:00
|
|
|
for (int i = 0; i < maxElements; ++i) {
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (Index >= ParentIList->getNumInits())
|
|
|
|
break;
|
|
|
|
Expr* expr = ParentIList->getInit(Index);
|
|
|
|
|
|
|
|
// Add the expr to the new implicit init list and remove if from the old.
|
|
|
|
InitExprs.push_back(expr);
|
|
|
|
ParentIList->removeInit(Index);
|
|
|
|
}
|
|
|
|
// Synthesize an "implicit" InitListExpr (marked by the invalid source locs).
|
|
|
|
InitListExpr *ILE = new InitListExpr(SourceLocation(),
|
|
|
|
&InitExprs[0], InitExprs.size(),
|
2008-10-27 07:43:26 +08:00
|
|
|
SourceLocation(),
|
|
|
|
ParentIList->hadDesignators());
|
2008-05-02 06:18:59 +08:00
|
|
|
ILE->setType(T);
|
2008-05-20 04:00:43 +08:00
|
|
|
|
2008-05-02 06:18:59 +08:00
|
|
|
// Modify the parent InitListExpr to point to the implicit InitListExpr.
|
|
|
|
ParentIList->addInit(Index, ILE);
|
|
|
|
}
|
|
|
|
|
2008-05-06 08:23:44 +08:00
|
|
|
void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
|
2008-05-02 06:18:59 +08:00
|
|
|
unsigned &Index) {
|
2008-05-20 04:00:43 +08:00
|
|
|
assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
CheckListElementTypes(IList, T, Index);
|
2008-05-06 08:23:44 +08:00
|
|
|
IList->setType(T);
|
2008-05-25 21:22:35 +08:00
|
|
|
if (hadError)
|
|
|
|
return;
|
2008-05-20 04:00:43 +08:00
|
|
|
|
2008-05-25 21:22:35 +08:00
|
|
|
if (Index < IList->getNumInits()) {
|
2008-05-20 04:00:43 +08:00
|
|
|
// We have leftover initializers
|
|
|
|
if (IList->getNumInits() > 0 &&
|
|
|
|
SemaRef->IsStringLiteralInit(IList->getInit(Index), T)) {
|
2008-05-20 04:12:18 +08:00
|
|
|
// Special-case
|
2008-05-20 04:00:43 +08:00
|
|
|
SemaRef->Diag(IList->getInit(Index)->getLocStart(),
|
2008-11-19 13:27:50 +08:00
|
|
|
diag::err_excess_initializers_in_char_array_initializer)
|
|
|
|
<< IList->getInit(Index)->getSourceRange();
|
2008-05-20 04:00:43 +08:00
|
|
|
hadError = true;
|
2008-05-20 13:25:56 +08:00
|
|
|
} else if (!T->isIncompleteType()) {
|
|
|
|
// Don't warn for incomplete types, since we'll get an error elsewhere
|
2008-05-20 04:00:43 +08:00
|
|
|
SemaRef->Diag(IList->getInit(Index)->getLocStart(),
|
2008-11-19 13:27:50 +08:00
|
|
|
diag::warn_excess_initializers)
|
|
|
|
<< IList->getInit(Index)->getSourceRange();
|
2008-05-20 04:00:43 +08:00
|
|
|
}
|
|
|
|
}
|
2008-05-20 04:20:43 +08:00
|
|
|
|
2008-05-25 21:22:35 +08:00
|
|
|
if (T->isScalarType())
|
2008-11-19 13:27:50 +08:00
|
|
|
SemaRef->Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
|
|
|
|
<< IList->getSourceRange();
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
void InitListChecker::CheckListElementTypes(InitListExpr *IList,
|
|
|
|
QualType &DeclType,
|
|
|
|
unsigned &Index) {
|
2008-05-20 04:00:43 +08:00
|
|
|
if (DeclType->isScalarType()) {
|
2008-05-02 06:18:59 +08:00
|
|
|
CheckScalarType(IList, DeclType, Index);
|
2008-05-20 04:00:43 +08:00
|
|
|
} else if (DeclType->isVectorType()) {
|
2008-05-02 06:18:59 +08:00
|
|
|
CheckVectorType(IList, DeclType, Index);
|
2008-05-20 04:00:43 +08:00
|
|
|
} else if (DeclType->isAggregateType() || DeclType->isUnionType()) {
|
2008-05-02 06:18:59 +08:00
|
|
|
if (DeclType->isStructureType() || DeclType->isUnionType())
|
|
|
|
CheckStructUnionTypes(IList, DeclType, Index);
|
|
|
|
else if (DeclType->isArrayType())
|
|
|
|
CheckArrayType(IList, DeclType, Index);
|
|
|
|
else
|
|
|
|
assert(0 && "Aggregate that isn't a function or array?!");
|
2008-08-11 00:05:48 +08:00
|
|
|
} else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
|
|
|
|
// This type is invalid, issue a diagnostic.
|
2008-05-20 13:25:56 +08:00
|
|
|
Index++;
|
2008-11-20 14:38:18 +08:00
|
|
|
SemaRef->Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
|
2008-11-24 14:25:27 +08:00
|
|
|
<< DeclType;
|
2008-05-20 13:25:56 +08:00
|
|
|
hadError = true;
|
2008-05-02 06:18:59 +08:00
|
|
|
} else {
|
|
|
|
// In C, all types are either scalars or aggregates, but
|
|
|
|
// additional handling is needed here for C++ (and possibly others?).
|
|
|
|
assert(0 && "Unsupported initializer type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
void InitListChecker::CheckSubElementType(InitListExpr *IList,
|
|
|
|
QualType ElemType,
|
|
|
|
unsigned &Index) {
|
|
|
|
Expr* expr = IList->getInit(Index);
|
2008-05-20 04:00:43 +08:00
|
|
|
if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
|
|
|
|
unsigned newIndex = 0;
|
|
|
|
CheckExplicitInitList(SubInitList, ElemType, newIndex);
|
|
|
|
Index++;
|
2008-05-20 03:16:24 +08:00
|
|
|
} else if (StringLiteral *lit =
|
|
|
|
SemaRef->IsStringLiteralInit(expr, ElemType)) {
|
|
|
|
SemaRef->CheckStringLiteralInit(lit, ElemType);
|
|
|
|
Index++;
|
2008-05-20 04:00:43 +08:00
|
|
|
} else if (ElemType->isScalarType()) {
|
|
|
|
CheckScalarType(IList, ElemType, Index);
|
2008-05-20 03:16:24 +08:00
|
|
|
} else if (expr->getType()->getAsRecordType() &&
|
2008-06-09 11:52:40 +08:00
|
|
|
SemaRef->Context.typesAreCompatible(
|
|
|
|
expr->getType().getUnqualifiedType(),
|
|
|
|
ElemType.getUnqualifiedType())) {
|
2008-05-20 03:16:24 +08:00
|
|
|
Index++;
|
|
|
|
// FIXME: Add checking
|
|
|
|
} else {
|
|
|
|
CheckImplicitInitList(IList, ElemType, Index);
|
|
|
|
Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-02 06:18:59 +08:00
|
|
|
void InitListChecker::CheckScalarType(InitListExpr *IList, QualType &DeclType,
|
|
|
|
unsigned &Index) {
|
|
|
|
if (Index < IList->getNumInits()) {
|
|
|
|
Expr* expr = IList->getInit(Index);
|
2008-05-20 04:00:43 +08:00
|
|
|
if (isa<InitListExpr>(expr)) {
|
2008-05-20 04:12:18 +08:00
|
|
|
SemaRef->Diag(IList->getLocStart(),
|
2008-11-19 13:27:50 +08:00
|
|
|
diag::err_many_braces_around_scalar_init)
|
|
|
|
<< IList->getSourceRange();
|
2008-05-20 04:12:18 +08:00
|
|
|
hadError = true;
|
|
|
|
++Index;
|
|
|
|
return;
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
2008-05-20 04:00:43 +08:00
|
|
|
Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
|
2009-01-14 23:45:31 +08:00
|
|
|
if (SemaRef->CheckSingleInitializer(expr, DeclType, false))
|
2008-05-20 04:12:18 +08:00
|
|
|
hadError = true; // types weren't compatible.
|
2008-05-20 04:00:43 +08:00
|
|
|
else if (savExpr != expr)
|
|
|
|
// The type was promoted, update initializer list.
|
|
|
|
IList->setInit(Index, expr);
|
2008-05-02 06:18:59 +08:00
|
|
|
++Index;
|
2008-05-20 04:12:18 +08:00
|
|
|
} else {
|
2008-11-19 13:27:50 +08:00
|
|
|
SemaRef->Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
|
|
|
|
<< IList->getSourceRange();
|
2008-05-20 04:12:18 +08:00
|
|
|
hadError = true;
|
|
|
|
return;
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
|
|
|
|
unsigned &Index) {
|
|
|
|
if (Index < IList->getNumInits()) {
|
|
|
|
const VectorType *VT = DeclType->getAsVectorType();
|
|
|
|
int maxElements = VT->getNumElements();
|
|
|
|
QualType elementType = VT->getElementType();
|
|
|
|
|
|
|
|
for (int i = 0; i < maxElements; ++i) {
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (Index >= IList->getNumInits())
|
|
|
|
break;
|
2008-05-20 03:16:24 +08:00
|
|
|
CheckSubElementType(IList, elementType, Index);
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
|
|
|
|
unsigned &Index) {
|
|
|
|
// Check for the special-case of initializing an array with a string.
|
|
|
|
if (Index < IList->getNumInits()) {
|
|
|
|
if (StringLiteral *lit =
|
|
|
|
SemaRef->IsStringLiteralInit(IList->getInit(Index), DeclType)) {
|
|
|
|
SemaRef->CheckStringLiteralInit(lit, DeclType);
|
|
|
|
++Index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-08-04 15:31:14 +08:00
|
|
|
if (const VariableArrayType *VAT =
|
|
|
|
SemaRef->Context.getAsVariableArrayType(DeclType)) {
|
2008-05-25 21:22:35 +08:00
|
|
|
// Check for VLAs; in standard C it would be possible to check this
|
|
|
|
// earlier, but I don't know where clang accepts VLAs (gcc accepts
|
|
|
|
// them in all sorts of strange places).
|
|
|
|
SemaRef->Diag(VAT->getSizeExpr()->getLocStart(),
|
2008-11-19 13:27:50 +08:00
|
|
|
diag::err_variable_object_no_init)
|
|
|
|
<< VAT->getSizeExpr()->getSourceRange();
|
2008-05-25 21:22:35 +08:00
|
|
|
hadError = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-02 06:18:59 +08:00
|
|
|
int maxElements = numArrayElements(DeclType);
|
2008-08-04 15:31:14 +08:00
|
|
|
QualType elementType = SemaRef->Context.getAsArrayType(DeclType)
|
|
|
|
->getElementType();
|
2008-05-02 06:18:59 +08:00
|
|
|
int numElements = 0;
|
|
|
|
for (int i = 0; i < maxElements; ++i, ++numElements) {
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (Index >= IList->getNumInits())
|
|
|
|
break;
|
2008-05-20 03:16:24 +08:00
|
|
|
CheckSubElementType(IList, elementType, Index);
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
if (DeclType->isIncompleteArrayType()) {
|
|
|
|
// If this is an incomplete array type, the actual type needs to
|
2008-08-19 04:28:46 +08:00
|
|
|
// be calculated here.
|
2008-05-02 06:18:59 +08:00
|
|
|
if (numElements == 0) {
|
2008-08-19 04:28:46 +08:00
|
|
|
// Sizing an array implicitly to zero is not allowed by ISO C,
|
|
|
|
// but is supported by GNU.
|
2008-05-02 06:18:59 +08:00
|
|
|
SemaRef->Diag(IList->getLocStart(),
|
2008-08-19 04:28:46 +08:00
|
|
|
diag::ext_typecheck_zero_array_size);
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
2008-08-19 04:28:46 +08:00
|
|
|
|
|
|
|
llvm::APSInt ConstVal(32);
|
|
|
|
ConstVal = numElements;
|
|
|
|
DeclType = SemaRef->Context.getConstantArrayType(elementType, ConstVal,
|
|
|
|
ArrayType::Normal, 0);
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
|
|
|
|
QualType DeclType,
|
2008-05-20 03:16:24 +08:00
|
|
|
unsigned &Index) {
|
|
|
|
RecordDecl* structDecl = DeclType->getAsRecordType()->getDecl();
|
2008-05-02 06:18:59 +08:00
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
// If the record is invalid, some of it's members are invalid. To avoid
|
|
|
|
// confusion, we forgo checking the intializer for the entire record.
|
|
|
|
if (structDecl->isInvalidDecl()) {
|
|
|
|
hadError = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If structDecl is a forward declaration, this loop won't do anything;
|
|
|
|
// That's okay, because an error should get printed out elsewhere. It
|
|
|
|
// might be worthwhile to skip over the rest of the initializer, though.
|
2008-12-12 00:49:14 +08:00
|
|
|
RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
|
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
// If we've hit the flexible array member at the end, we're done.
|
|
|
|
if (Field->getType()->isIncompleteArrayType())
|
|
|
|
break;
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
if (Index >= IList->getNumInits())
|
|
|
|
break;
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
if (!Field->getIdentifier()) {
|
2008-05-20 03:16:24 +08:00
|
|
|
// Don't initialize unnamed fields, e.g. "int : 20;"
|
|
|
|
continue;
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
|
|
CheckSubElementType(IList, Field->getType(), Index);
|
2008-05-20 03:16:24 +08:00
|
|
|
if (DeclType->isUnionType())
|
|
|
|
break;
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
// FIXME: Implement flexible array initialization GCC extension (it's a
|
|
|
|
// really messy extension to implement, unfortunately...the necessary
|
|
|
|
// information isn't actually even here!)
|
2008-05-02 06:18:59 +08:00
|
|
|
}
|
|
|
|
} // end namespace clang
|
|
|
|
|