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.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// This file implements semantic analysis for initializers. The main entry
|
|
|
|
|
// point is Sema::CheckInitList(), but all of the work is performed
|
|
|
|
|
// within the InitListChecker class.
|
|
|
|
|
//
|
2009-02-25 06:48:58 +08:00
|
|
|
|
// This file also implements Sema::CheckInitializerTypes.
|
2008-05-02 06:18:59 +08:00
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
#include "SemaInit.h"
|
2008-05-02 06:18:59 +08:00
|
|
|
|
#include "Sema.h"
|
2009-01-22 08:58:24 +08:00
|
|
|
|
#include "clang/Parse/Designator.h"
|
2008-05-02 06:18:59 +08:00
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-05-28 00:10:08 +08:00
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2009-02-25 07:10:27 +08:00
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2009-12-10 07:02:17 +08:00
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-01-29 08:45:39 +08:00
|
|
|
|
#include <map>
|
2009-01-22 08:58:24 +08:00
|
|
|
|
using namespace clang;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Sema Initialization Checking
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2009-02-25 07:10:27 +08:00
|
|
|
|
static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
|
2009-02-27 07:26:43 +08:00
|
|
|
|
const ArrayType *AT = Context.getAsArrayType(DeclType);
|
|
|
|
|
if (!AT) return 0;
|
|
|
|
|
|
2009-05-30 02:22:49 +08:00
|
|
|
|
if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2009-02-27 07:26:43 +08:00
|
|
|
|
// See if this is a string literal or @encode.
|
|
|
|
|
Init = Init->IgnoreParens();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-27 07:26:43 +08:00
|
|
|
|
// Handle @encode, which is a narrow string.
|
|
|
|
|
if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
|
|
|
|
|
return Init;
|
|
|
|
|
|
|
|
|
|
// Otherwise we can only handle string literals.
|
|
|
|
|
StringLiteral *SL = dyn_cast<StringLiteral>(Init);
|
2009-02-27 07:42:47 +08:00
|
|
|
|
if (SL == 0) return 0;
|
2009-05-31 18:54:53 +08:00
|
|
|
|
|
|
|
|
|
QualType ElemTy = Context.getCanonicalType(AT->getElementType());
|
2009-02-27 07:26:43 +08:00
|
|
|
|
// char array can be initialized with a narrow string.
|
|
|
|
|
// Only allow char x[] = "foo"; not char x[] = L"foo";
|
|
|
|
|
if (!SL->isWide())
|
2009-05-31 18:54:53 +08:00
|
|
|
|
return ElemTy->isCharType() ? Init : 0;
|
2009-02-27 07:26:43 +08:00
|
|
|
|
|
2009-05-31 18:54:53 +08:00
|
|
|
|
// wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
|
|
|
|
|
// correction from DR343): "An array with element type compatible with a
|
|
|
|
|
// qualified or unqualified version of wchar_t may be initialized by a wide
|
|
|
|
|
// string literal, optionally enclosed in braces."
|
|
|
|
|
if (Context.typesAreCompatible(Context.getWCharType(),
|
|
|
|
|
ElemTy.getUnqualifiedType()))
|
2009-02-27 07:26:43 +08:00
|
|
|
|
return Init;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
static bool CheckSingleInitializer(Expr *&Init, QualType DeclType,
|
2009-02-25 06:46:58 +08:00
|
|
|
|
bool DirectInit, Sema &S) {
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// Get the type before calling CheckSingleAssignmentConstraints(), since
|
|
|
|
|
// it can promote the expression.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
QualType InitType = Init->getType();
|
|
|
|
|
|
2009-02-25 06:46:58 +08:00
|
|
|
|
if (S.getLangOptions().CPlusPlus) {
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// FIXME: I dislike this error message. A lot.
|
2009-09-16 03:12:21 +08:00
|
|
|
|
if (S.PerformImplicitConversion(Init, DeclType,
|
2009-12-16 11:45:30 +08:00
|
|
|
|
Sema::AA_Initializing, DirectInit)) {
|
2009-09-16 03:12:21 +08:00
|
|
|
|
ImplicitConversionSequence ICS;
|
|
|
|
|
OverloadCandidateSet CandidateSet;
|
|
|
|
|
if (S.IsUserDefinedConversion(Init, DeclType, ICS.UserDefined,
|
|
|
|
|
CandidateSet,
|
2009-12-10 07:02:17 +08:00
|
|
|
|
true, false, false) != OR_Ambiguous)
|
2009-09-16 03:12:21 +08:00
|
|
|
|
return S.Diag(Init->getSourceRange().getBegin(),
|
|
|
|
|
diag::err_typecheck_convert_incompatible)
|
2009-12-16 11:45:30 +08:00
|
|
|
|
<< DeclType << Init->getType() << Sema::AA_Initializing
|
2009-09-16 03:12:21 +08:00
|
|
|
|
<< Init->getSourceRange();
|
|
|
|
|
S.Diag(Init->getSourceRange().getBegin(),
|
|
|
|
|
diag::err_typecheck_convert_ambiguous)
|
|
|
|
|
<< DeclType << Init->getType() << Init->getSourceRange();
|
|
|
|
|
S.PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-02-25 06:27:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:46:58 +08:00
|
|
|
|
Sema::AssignConvertType ConvTy =
|
|
|
|
|
S.CheckSingleAssignmentConstraints(DeclType, Init);
|
|
|
|
|
return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType,
|
2009-12-16 11:45:30 +08:00
|
|
|
|
InitType, Init, Sema::AA_Initializing);
|
2009-02-25 06:27:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-25 07:10:27 +08:00
|
|
|
|
static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
|
|
|
|
|
// Get the length of the string as parsed.
|
|
|
|
|
uint64_t StrLength =
|
|
|
|
|
cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 07:10:27 +08:00
|
|
|
|
const ArrayType *AT = S.Context.getAsArrayType(DeclT);
|
2009-02-25 06:27:37 +08:00
|
|
|
|
if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
// C99 6.7.8p14. We have an array of character type with unknown size
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// being initialized to a string literal.
|
|
|
|
|
llvm::APSInt ConstVal(32);
|
2009-02-25 07:01:39 +08:00
|
|
|
|
ConstVal = StrLength;
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// Return a new array type (C99 6.7.8p22).
|
2009-10-16 08:14:28 +08:00
|
|
|
|
DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
|
|
|
|
|
ConstVal,
|
|
|
|
|
ArrayType::Normal, 0);
|
2009-02-25 07:01:39 +08:00
|
|
|
|
return;
|
2009-02-25 06:27:37 +08:00
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-05-30 02:22:49 +08:00
|
|
|
|
const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-05-30 02:22:49 +08:00
|
|
|
|
// C99 6.7.8p14. We have an array of character type with known size. However,
|
|
|
|
|
// the size may be smaller or larger than the string we are initializing.
|
|
|
|
|
// FIXME: Avoid truncation for 64-bit length strings.
|
|
|
|
|
if (StrLength-1 > CAT->getSize().getZExtValue())
|
|
|
|
|
S.Diag(Str->getSourceRange().getBegin(),
|
|
|
|
|
diag::warn_initializer_string_for_char_array_too_long)
|
|
|
|
|
<< Str->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-05-30 02:22:49 +08:00
|
|
|
|
// Set the type to the actual size that we are initializing. If we have
|
|
|
|
|
// something like:
|
|
|
|
|
// char x[1] = "foo";
|
|
|
|
|
// then this will set the string literal's type to char[1].
|
|
|
|
|
Str->setType(DeclT);
|
2009-02-25 06:27:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
|
2009-12-16 09:38:02 +08:00
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind) {
|
|
|
|
|
SourceLocation InitLoc = Kind.getLocation();
|
|
|
|
|
DeclarationName InitEntity = Entity.getName();
|
|
|
|
|
bool DirectInit = (Kind.getKind() == InitializationKind::IK_Direct);
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (DeclType->isDependentType() ||
|
2009-11-20 02:03:26 +08:00
|
|
|
|
Init->isTypeDependent() || Init->isValueDependent()) {
|
|
|
|
|
// We have either a dependent type or a type- or value-dependent
|
|
|
|
|
// initializer, so we don't perform any additional checking at
|
|
|
|
|
// this point.
|
|
|
|
|
|
|
|
|
|
// If the declaration is a non-dependent, incomplete array type
|
|
|
|
|
// that has an initializer, then its type will be completed once
|
2009-11-20 07:25:22 +08:00
|
|
|
|
// the initializer is instantiated.
|
2009-11-20 02:03:26 +08:00
|
|
|
|
if (!DeclType->isDependentType()) {
|
|
|
|
|
if (const IncompleteArrayType *ArrayT
|
|
|
|
|
= Context.getAsIncompleteArrayType(DeclType)) {
|
2009-11-20 07:25:22 +08:00
|
|
|
|
if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
|
|
|
|
|
if (!ILE->isTypeDependent()) {
|
|
|
|
|
// Compute the constant array type from the length of the
|
|
|
|
|
// initializer list.
|
|
|
|
|
// FIXME: This will be wrong if there are designated
|
|
|
|
|
// initializations. Good thing they don't exist in C++!
|
|
|
|
|
llvm::APInt NumElements(Context.getTypeSize(Context.getSizeType()),
|
|
|
|
|
ILE->getNumInits());
|
|
|
|
|
llvm::APInt Zero(Context.getTypeSize(Context.getSizeType()), 0);
|
|
|
|
|
if (NumElements == Zero) {
|
|
|
|
|
// Sizing an array implicitly to zero is not allowed by ISO C,
|
|
|
|
|
// but is supported by GNU.
|
|
|
|
|
Diag(ILE->getLocStart(), diag::ext_typecheck_zero_array_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeclType = Context.getConstantArrayType(ArrayT->getElementType(),
|
|
|
|
|
NumElements,
|
|
|
|
|
ArrayT->getSizeModifier(),
|
|
|
|
|
ArrayT->getIndexTypeCVRQualifiers());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make the array type-dependent by making it dependently-sized.
|
2009-11-20 02:03:26 +08:00
|
|
|
|
DeclType = Context.getDependentSizedArrayType(ArrayT->getElementType(),
|
|
|
|
|
/*NumElts=*/0,
|
|
|
|
|
ArrayT->getSizeModifier(),
|
|
|
|
|
ArrayT->getIndexTypeCVRQualifiers(),
|
|
|
|
|
SourceRange());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
return false;
|
2009-11-20 02:03:26 +08:00
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// C++ [dcl.init.ref]p1:
|
2009-03-17 07:22:08 +08:00
|
|
|
|
// A variable declared to be a T& or T&&, that is "reference to type T"
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// (8.3.2), shall be initialized by an object, or function, of
|
|
|
|
|
// type T or by an object that can be converted into a T.
|
2009-12-17 00:54:16 +08:00
|
|
|
|
if (DeclType->isReferenceType()) {
|
|
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, &Init, 1);
|
|
|
|
|
OwningExprResult CurInit = InitSeq.Perform(*this, Entity, Kind,
|
|
|
|
|
MultiExprArg(*this, (void**)&Init, 1),
|
|
|
|
|
&DeclType);
|
|
|
|
|
if (CurInit.isInvalid())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
Init = CurInit.takeAs<Expr>();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// C99 6.7.8p3: The type of the entity to be initialized shall be an array
|
|
|
|
|
// of unknown size ("[]") or an object type that is not a variable array type.
|
|
|
|
|
if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType))
|
|
|
|
|
return Diag(InitLoc, diag::err_variable_object_no_init)
|
|
|
|
|
<< VAT->getSizeExpr()->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
|
|
|
|
|
if (!InitList) {
|
|
|
|
|
// FIXME: Handle wide strings
|
2009-02-25 07:10:27 +08:00
|
|
|
|
if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
|
|
|
|
|
CheckStringInit(Str, DeclType, *this);
|
2009-02-25 07:01:39 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// C++ [dcl.init]p14:
|
|
|
|
|
// -- If the destination type is a (possibly cv-qualified) class
|
|
|
|
|
// type:
|
|
|
|
|
if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
|
2009-12-19 11:01:41 +08:00
|
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, &Init, 1);
|
|
|
|
|
OwningExprResult CurInit = InitSeq.Perform(*this, Entity, Kind,
|
|
|
|
|
MultiExprArg(*this, (void**)&Init, 1),
|
|
|
|
|
&DeclType);
|
|
|
|
|
if (CurInit.isInvalid())
|
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-12-19 11:01:41 +08:00
|
|
|
|
Init = CurInit.takeAs<Expr>();
|
|
|
|
|
return false;
|
2009-02-25 06:27:37 +08:00
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:27:37 +08:00
|
|
|
|
// C99 6.7.8p16.
|
|
|
|
|
if (DeclType->isArrayType())
|
|
|
|
|
return Diag(Init->getLocStart(), diag::err_array_init_list_required)
|
2009-06-26 12:45:06 +08:00
|
|
|
|
<< Init->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-02-25 06:46:58 +08:00
|
|
|
|
return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
bool hadError = CheckInitList(Entity, InitList, DeclType);
|
2009-02-25 06:27:37 +08:00
|
|
|
|
Init = InitList;
|
|
|
|
|
return hadError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Semantic checking for initializer lists.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2009-01-29 09:05:33 +08:00
|
|
|
|
/// @brief Semantic checking for initializer lists.
|
|
|
|
|
///
|
|
|
|
|
/// The InitListChecker class contains a set of routines that each
|
|
|
|
|
/// handle the initialization of a certain kind of entity, e.g.,
|
|
|
|
|
/// arrays, vectors, struct/union types, scalars, etc. The
|
|
|
|
|
/// InitListChecker itself performs a recursive walk of the subobject
|
|
|
|
|
/// structure of the type to be initialized, while stepping through
|
|
|
|
|
/// the initializer list one element at a time. The IList and Index
|
|
|
|
|
/// parameters to each of the Check* routines contain the active
|
|
|
|
|
/// (syntactic) initializer list and the index into that initializer
|
|
|
|
|
/// list that represents the current initializer. Each routine is
|
|
|
|
|
/// responsible for moving that Index forward as it consumes elements.
|
|
|
|
|
///
|
|
|
|
|
/// Each Check* routine also has a StructuredList/StructuredIndex
|
|
|
|
|
/// arguments, which contains the current the "structured" (semantic)
|
|
|
|
|
/// initializer list and the index into that initializer list where we
|
|
|
|
|
/// are copying initializers as we map them over to the semantic
|
|
|
|
|
/// list. Once we have completed our recursive walk of the subobject
|
|
|
|
|
/// structure, we will have constructed a full semantic initializer
|
|
|
|
|
/// list.
|
|
|
|
|
///
|
|
|
|
|
/// C99 designators cause changes in the initializer list traversal,
|
|
|
|
|
/// because they make the initialization "jump" into a specific
|
|
|
|
|
/// subobject and then continue the initialization from that
|
|
|
|
|
/// point. CheckDesignatedInitializer() recursively steps into the
|
|
|
|
|
/// designated subobject and manages backing out the recursion to
|
|
|
|
|
/// initialize the subobjects after the one designated.
|
2009-02-25 06:48:58 +08:00
|
|
|
|
namespace {
|
2009-01-29 08:45:39 +08:00
|
|
|
|
class InitListChecker {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
Sema &SemaRef;
|
2009-01-29 08:45:39 +08:00
|
|
|
|
bool hadError;
|
|
|
|
|
std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
|
|
|
|
|
InitListExpr *FullyStructuredList;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
|
void CheckImplicitInitList(InitListExpr *ParentIList, QualType T,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
unsigned &Index, InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject = false);
|
2009-01-29 08:45:39 +08:00
|
|
|
|
void CheckExplicitInitList(InitListExpr *IList, QualType &T,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
unsigned &Index, InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject = false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void CheckListElementTypes(InitListExpr *IList, QualType &DeclType,
|
|
|
|
|
bool SubobjectIsDesignatorContext,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
unsigned &Index,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject = false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void CheckSubElementType(InitListExpr *IList, QualType ElemType,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
unsigned &Index,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void CheckScalarType(InitListExpr *IList, QualType DeclType,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
unsigned &Index,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void CheckReferenceType(InitListExpr *IList, QualType DeclType,
|
2009-01-31 06:09:00 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex);
|
2009-01-29 08:45:39 +08:00
|
|
|
|
void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType,
|
|
|
|
|
RecordDecl::field_iterator Field,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
bool SubobjectIsDesignatorContext, unsigned &Index,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject = false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void CheckArrayType(InitListExpr *IList, QualType &DeclType,
|
|
|
|
|
llvm::APSInt elementIndex,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
bool SubobjectIsDesignatorContext, unsigned &Index,
|
2009-01-29 09:05:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE,
|
2009-04-15 12:56:10 +08:00
|
|
|
|
unsigned DesigIdx,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
QualType &CurrentObjectType,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
RecordDecl::field_iterator *NextField,
|
|
|
|
|
llvm::APSInt *NextElementIndex,
|
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
bool FinishSubobjectInit,
|
|
|
|
|
bool TopLevelObject);
|
2009-01-29 08:45:39 +08:00
|
|
|
|
InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
|
|
|
|
|
QualType CurrentObjectType,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned StructuredIndex,
|
|
|
|
|
SourceRange InitRange);
|
2009-01-29 09:05:33 +08:00
|
|
|
|
void UpdateStructuredListElement(InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex,
|
2009-01-29 08:45:39 +08:00
|
|
|
|
Expr *expr);
|
|
|
|
|
int numArrayElements(QualType DeclType);
|
|
|
|
|
int numStructUnionElements(QualType DeclType);
|
2009-01-31 06:09:00 +08:00
|
|
|
|
|
2009-12-22 08:05:34 +08:00
|
|
|
|
void FillInValueInitForField(unsigned Init, FieldDecl *Field,
|
|
|
|
|
const InitializedEntity &ParentEntity,
|
|
|
|
|
InitListExpr *ILE, bool &RequiresSecondPass);
|
2009-12-16 14:35:08 +08:00
|
|
|
|
void FillInValueInitializations(const InitializedEntity &Entity,
|
|
|
|
|
InitListExpr *ILE, bool &RequiresSecondPass);
|
2009-01-29 08:45:39 +08:00
|
|
|
|
public:
|
2009-12-16 14:35:08 +08:00
|
|
|
|
InitListChecker(Sema &S, const InitializedEntity &Entity,
|
|
|
|
|
InitListExpr *IL, QualType &T);
|
2009-01-29 08:45:39 +08:00
|
|
|
|
bool HadError() { return hadError; }
|
|
|
|
|
|
|
|
|
|
// @brief Retrieves the fully-structured initializer list used for
|
|
|
|
|
// semantic analysis and code generation.
|
|
|
|
|
InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
|
|
|
|
|
};
|
2009-02-25 06:48:58 +08:00
|
|
|
|
} // end anonymous namespace
|
2009-01-29 13:10:57 +08:00
|
|
|
|
|
2009-12-22 08:05:34 +08:00
|
|
|
|
void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
|
|
|
|
|
const InitializedEntity &ParentEntity,
|
|
|
|
|
InitListExpr *ILE,
|
|
|
|
|
bool &RequiresSecondPass) {
|
|
|
|
|
SourceLocation Loc = ILE->getSourceRange().getBegin();
|
|
|
|
|
unsigned NumInits = ILE->getNumInits();
|
|
|
|
|
InitializedEntity MemberEntity
|
|
|
|
|
= InitializedEntity::InitializeMember(Field, &ParentEntity);
|
|
|
|
|
if (Init >= NumInits || !ILE->getInit(Init)) {
|
|
|
|
|
// FIXME: We probably don't need to handle references
|
|
|
|
|
// specially here, since value-initialization of references is
|
|
|
|
|
// handled in InitializationSequence.
|
|
|
|
|
if (Field->getType()->isReferenceType()) {
|
|
|
|
|
// C++ [dcl.init.aggr]p9:
|
|
|
|
|
// If an incomplete or empty initializer-list leaves a
|
|
|
|
|
// member of reference type uninitialized, the program is
|
|
|
|
|
// ill-formed.
|
|
|
|
|
SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
|
|
|
|
|
<< Field->getType()
|
|
|
|
|
<< ILE->getSyntacticForm()->getSourceRange();
|
|
|
|
|
SemaRef.Diag(Field->getLocation(),
|
|
|
|
|
diag::note_uninit_reference_member);
|
|
|
|
|
hadError = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
|
|
|
|
|
true);
|
|
|
|
|
InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
|
|
|
|
|
if (!InitSeq) {
|
|
|
|
|
InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
|
|
|
|
|
hadError = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sema::OwningExprResult MemberInit
|
|
|
|
|
= InitSeq.Perform(SemaRef, MemberEntity, Kind,
|
|
|
|
|
Sema::MultiExprArg(SemaRef, 0, 0));
|
|
|
|
|
if (MemberInit.isInvalid()) {
|
|
|
|
|
hadError = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hadError) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
} else if (Init < NumInits) {
|
|
|
|
|
ILE->setInit(Init, MemberInit.takeAs<Expr>());
|
|
|
|
|
} else if (InitSeq.getKind()
|
|
|
|
|
== InitializationSequence::ConstructorInitialization) {
|
|
|
|
|
// Value-initialization requires a constructor call, so
|
|
|
|
|
// extend the initializer list to include the constructor
|
|
|
|
|
// call and make a note that we'll need to take another pass
|
|
|
|
|
// through the initializer list.
|
|
|
|
|
ILE->updateInit(Init, MemberInit.takeAs<Expr>());
|
|
|
|
|
RequiresSecondPass = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (InitListExpr *InnerILE
|
|
|
|
|
= dyn_cast<InitListExpr>(ILE->getInit(Init)))
|
|
|
|
|
FillInValueInitializations(MemberEntity, InnerILE,
|
|
|
|
|
RequiresSecondPass);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
/// Recursively replaces NULL values within the given initializer list
|
|
|
|
|
/// with expressions that perform value-initialization of the
|
|
|
|
|
/// appropriate type.
|
2009-12-16 14:35:08 +08:00
|
|
|
|
void
|
|
|
|
|
InitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
|
|
|
|
|
InitListExpr *ILE,
|
|
|
|
|
bool &RequiresSecondPass) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
assert((ILE->getType() != SemaRef.Context.VoidTy) &&
|
2009-01-31 06:09:00 +08:00
|
|
|
|
"Should not have void type");
|
2009-02-03 01:43:21 +08:00
|
|
|
|
SourceLocation Loc = ILE->getSourceRange().getBegin();
|
|
|
|
|
if (ILE->getSyntacticForm())
|
|
|
|
|
Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
|
if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
|
2009-12-22 08:05:34 +08:00
|
|
|
|
if (RType->getDecl()->isUnion() &&
|
|
|
|
|
ILE->getInitializedFieldInUnion())
|
|
|
|
|
FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
|
|
|
|
|
Entity, ILE, RequiresSecondPass);
|
|
|
|
|
else {
|
|
|
|
|
unsigned Init = 0;
|
|
|
|
|
for (RecordDecl::field_iterator
|
|
|
|
|
Field = RType->getDecl()->field_begin(),
|
|
|
|
|
FieldEnd = RType->getDecl()->field_end();
|
|
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
|
if (Field->isUnnamedBitfield())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (hadError)
|
2009-02-03 01:43:21 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2009-12-22 08:05:34 +08:00
|
|
|
|
FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
|
|
|
|
|
if (hadError)
|
2009-12-16 14:35:08 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2009-12-22 08:05:34 +08:00
|
|
|
|
++Init;
|
|
|
|
|
|
|
|
|
|
// Only look at the first initialization of a union.
|
|
|
|
|
if (RType->getDecl()->isUnion())
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-01-29 05:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
}
|
2009-01-29 05:54:33 +08:00
|
|
|
|
|
|
|
|
|
QualType ElementType;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
InitializedEntity ElementEntity = Entity;
|
2009-02-03 01:43:21 +08:00
|
|
|
|
unsigned NumInits = ILE->getNumInits();
|
|
|
|
|
unsigned NumElements = NumInits;
|
2009-02-25 06:50:46 +08:00
|
|
|
|
if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
|
2009-01-29 05:54:33 +08:00
|
|
|
|
ElementType = AType->getElementType();
|
2009-02-03 01:43:21 +08:00
|
|
|
|
if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
|
|
|
|
|
NumElements = CAType->getSize().getZExtValue();
|
2009-12-16 14:35:08 +08:00
|
|
|
|
ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
|
|
|
|
|
0, Entity);
|
2009-09-22 07:43:11 +08:00
|
|
|
|
} else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
|
2009-01-29 05:54:33 +08:00
|
|
|
|
ElementType = VType->getElementType();
|
2009-02-03 01:43:21 +08:00
|
|
|
|
NumElements = VType->getNumElements();
|
2009-12-16 14:35:08 +08:00
|
|
|
|
ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
|
|
|
|
|
0, Entity);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
} else
|
2009-01-29 05:54:33 +08:00
|
|
|
|
ElementType = ILE->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
|
2009-02-03 01:43:21 +08:00
|
|
|
|
for (unsigned Init = 0; Init != NumElements; ++Init) {
|
2009-12-17 02:50:27 +08:00
|
|
|
|
if (hadError)
|
|
|
|
|
return;
|
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
if (ElementEntity.getKind() == InitializedEntity::EK_ArrayOrVectorElement)
|
|
|
|
|
ElementEntity.setElementIndex(Init);
|
|
|
|
|
|
2009-02-03 01:43:21 +08:00
|
|
|
|
if (Init >= NumInits || !ILE->getInit(Init)) {
|
2009-12-16 14:35:08 +08:00
|
|
|
|
InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
|
|
|
|
|
true);
|
|
|
|
|
InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
|
|
|
|
|
if (!InitSeq) {
|
|
|
|
|
InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
|
2009-02-03 01:43:21 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
Sema::OwningExprResult ElementInit
|
|
|
|
|
= InitSeq.Perform(SemaRef, ElementEntity, Kind,
|
|
|
|
|
Sema::MultiExprArg(SemaRef, 0, 0));
|
|
|
|
|
if (ElementInit.isInvalid()) {
|
2009-12-17 02:50:27 +08:00
|
|
|
|
hadError = true;
|
2009-12-16 14:35:08 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hadError) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
} else if (Init < NumInits) {
|
|
|
|
|
ILE->setInit(Init, ElementInit.takeAs<Expr>());
|
|
|
|
|
} else if (InitSeq.getKind()
|
|
|
|
|
== InitializationSequence::ConstructorInitialization) {
|
|
|
|
|
// Value-initialization requires a constructor call, so
|
|
|
|
|
// extend the initializer list to include the constructor
|
|
|
|
|
// call and make a note that we'll need to take another pass
|
|
|
|
|
// through the initializer list.
|
|
|
|
|
ILE->updateInit(Init, ElementInit.takeAs<Expr>());
|
|
|
|
|
RequiresSecondPass = true;
|
|
|
|
|
}
|
2009-08-05 05:02:39 +08:00
|
|
|
|
} else if (InitListExpr *InnerILE
|
2009-12-16 14:35:08 +08:00
|
|
|
|
= dyn_cast<InitListExpr>(ILE->getInit(Init)))
|
|
|
|
|
FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 13:10:57 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
|
|
|
|
|
InitListExpr *IL, QualType &T)
|
2009-02-25 06:50:46 +08:00
|
|
|
|
: SemaRef(S) {
|
2008-05-02 06:18:59 +08:00
|
|
|
|
hadError = false;
|
2008-05-20 04:00:43 +08:00
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
|
unsigned newIndex = 0;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned newStructuredIndex = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
FullyStructuredList
|
2009-03-02 01:12:46 +08:00
|
|
|
|
= getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
|
2009-02-05 06:46:25 +08:00
|
|
|
|
CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex,
|
|
|
|
|
/*TopLevelObject=*/true);
|
2008-05-20 04:00:43 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
if (!hadError) {
|
|
|
|
|
bool RequiresSecondPass = false;
|
|
|
|
|
FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
|
2009-12-17 02:50:27 +08:00
|
|
|
|
if (RequiresSecondPass && !hadError)
|
2009-12-16 14:35:08 +08:00
|
|
|
|
FillInValueInitializations(Entity, FullyStructuredList,
|
|
|
|
|
RequiresSecondPass);
|
|
|
|
|
}
|
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 =
|
2009-02-25 06:50:46 +08:00
|
|
|
|
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) {
|
2009-07-30 05:53:49 +08:00
|
|
|
|
RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
|
2009-01-29 05:54:33 +08:00
|
|
|
|
int InitializableMembers = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
for (RecordDecl::field_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
|
Field = structDecl->field_begin(),
|
|
|
|
|
FieldEnd = structDecl->field_end();
|
2009-01-29 05:54:33 +08:00
|
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
|
if ((*Field)->getIdentifier() || !(*Field)->isBitField())
|
|
|
|
|
++InitializableMembers;
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
QualType T, unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject) {
|
2008-05-02 06:18:59 +08:00
|
|
|
|
int maxElements = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2008-05-02 06:18:59 +08:00
|
|
|
|
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())
|
2009-09-22 07:43:11 +08:00
|
|
|
|
maxElements = T->getAs<VectorType>()->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) {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
|
2008-05-25 21:49:22 +08:00
|
|
|
|
diag::err_implicit_empty_initializer);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++Index;
|
2008-05-25 21:49:22 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Build a structured initializer list corresponding to this subobject.
|
|
|
|
|
InitListExpr *StructuredSubobjectInitList
|
2009-09-09 23:08:12 +08:00
|
|
|
|
= getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
|
|
|
|
|
StructuredIndex,
|
2009-03-02 01:12:46 +08:00
|
|
|
|
SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
|
|
|
|
|
ParentIList->getSourceRange().getEnd()));
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned StructuredSubobjectInitIndex = 0;
|
|
|
|
|
|
|
|
|
|
// Check the element types and build the structural subobject.
|
2009-02-03 01:43:21 +08:00
|
|
|
|
unsigned StartIndex = Index;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
CheckListElementTypes(ParentIList, T, false, Index,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
StructuredSubobjectInitList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
StructuredSubobjectInitIndex,
|
|
|
|
|
TopLevelObject);
|
2009-02-03 01:43:21 +08:00
|
|
|
|
unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
|
2009-03-20 08:32:56 +08:00
|
|
|
|
StructuredSubobjectInitList->setType(T);
|
|
|
|
|
|
2009-03-02 01:12:46 +08:00
|
|
|
|
// Update the structured sub-object initializer so that it's ending
|
2009-02-03 01:43:21 +08:00
|
|
|
|
// range corresponds with the end of the last initializer it used.
|
|
|
|
|
if (EndIndex < ParentIList->getNumInits()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SourceLocation EndLoc
|
2009-02-03 01:43:21 +08:00
|
|
|
|
= ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
|
|
|
|
|
StructuredSubobjectInitList->setRBraceLoc(EndLoc);
|
|
|
|
|
}
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-06 08:23:44 +08:00
|
|
|
|
void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject) {
|
2008-05-20 04:00:43 +08:00
|
|
|
|
assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
|
2009-01-29 05:54:33 +08:00
|
|
|
|
SyntacticToSemantic[IList] = StructuredList;
|
|
|
|
|
StructuredList->setSyntacticForm(IList);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
CheckListElementTypes(IList, T, true, Index, StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
StructuredIndex, TopLevelObject);
|
2008-05-06 08:23:44 +08:00
|
|
|
|
IList->setType(T);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredList->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
|
2009-05-30 04:20:05 +08:00
|
|
|
|
if (StructuredIndex == 1 &&
|
|
|
|
|
IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
|
2009-02-19 06:23:55 +08:00
|
|
|
|
unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
|
2009-05-30 04:20:05 +08:00
|
|
|
|
if (SemaRef.getLangOptions().CPlusPlus) {
|
2009-02-19 06:23:55 +08:00
|
|
|
|
DK = diag::err_excess_initializers_in_char_array_initializer;
|
2009-05-30 04:20:05 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
}
|
2008-05-20 04:12:18 +08:00
|
|
|
|
// Special-case
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
|
2008-11-19 13:27:50 +08:00
|
|
|
|
<< IList->getInit(Index)->getSourceRange();
|
2008-05-20 13:25:56 +08:00
|
|
|
|
} else if (!T->isIncompleteType()) {
|
2009-01-31 06:26:29 +08:00
|
|
|
|
// Don't complain for incomplete types, since we'll get an error
|
|
|
|
|
// elsewhere
|
2009-02-05 06:46:25 +08:00
|
|
|
|
QualType CurrentObjectType = StructuredList->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
int initKind =
|
2009-02-05 06:46:25 +08:00
|
|
|
|
CurrentObjectType->isArrayType()? 0 :
|
|
|
|
|
CurrentObjectType->isVectorType()? 1 :
|
|
|
|
|
CurrentObjectType->isScalarType()? 2 :
|
|
|
|
|
CurrentObjectType->isUnionType()? 3 :
|
|
|
|
|
4;
|
2009-02-19 06:23:55 +08:00
|
|
|
|
|
|
|
|
|
unsigned DK = diag::warn_excess_initializers;
|
2009-05-30 04:20:05 +08:00
|
|
|
|
if (SemaRef.getLangOptions().CPlusPlus) {
|
|
|
|
|
DK = diag::err_excess_initializers;
|
2009-07-08 05:53:06 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
}
|
|
|
|
|
if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
|
|
|
|
|
DK = diag::err_excess_initializers;
|
2009-05-30 04:20:05 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
}
|
2009-02-19 06:23:55 +08:00
|
|
|
|
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
|
2009-02-05 06:46:25 +08:00
|
|
|
|
<< initKind << IList->getInit(Index)->getSourceRange();
|
2008-05-20 04:00:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-20 04:20:43 +08:00
|
|
|
|
|
2009-05-16 19:45:48 +08:00
|
|
|
|
if (T->isScalarType() && !TopLevelObject)
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
|
2009-04-02 07:51:29 +08:00
|
|
|
|
<< IList->getSourceRange()
|
2009-12-07 01:36:05 +08:00
|
|
|
|
<< CodeModificationHint::CreateRemoval(IList->getLocStart())
|
|
|
|
|
<< CodeModificationHint::CreateRemoval(IList->getLocEnd());
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
|
void InitListChecker::CheckListElementTypes(InitListExpr *IList,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
QualType &DeclType,
|
2009-01-23 07:26:18 +08:00
|
|
|
|
bool SubobjectIsDesignatorContext,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject) {
|
2008-05-20 04:00:43 +08:00
|
|
|
|
if (DeclType->isScalarType()) {
|
2009-01-29 08:39:20 +08:00
|
|
|
|
CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex);
|
2008-05-20 04:00:43 +08:00
|
|
|
|
} else if (DeclType->isVectorType()) {
|
2009-01-29 05:54:33 +08:00
|
|
|
|
CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex);
|
2009-01-31 01:31:00 +08:00
|
|
|
|
} else if (DeclType->isAggregateType()) {
|
|
|
|
|
if (DeclType->isRecordType()) {
|
2009-07-30 05:53:49 +08:00
|
|
|
|
RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
CheckStructUnionTypes(IList, DeclType, RD->field_begin(),
|
2009-01-29 05:54:33 +08:00
|
|
|
|
SubobjectIsDesignatorContext, Index,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
StructuredList, StructuredIndex,
|
|
|
|
|
TopLevelObject);
|
2009-01-23 07:26:18 +08:00
|
|
|
|
} else if (DeclType->isArrayType()) {
|
2009-01-24 00:54:12 +08:00
|
|
|
|
llvm::APSInt Zero(
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
|
2009-01-24 00:54:12 +08:00
|
|
|
|
false);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index,
|
|
|
|
|
StructuredList, StructuredIndex);
|
2009-08-05 05:02:39 +08:00
|
|
|
|
} else
|
2009-01-29 05:54:33 +08:00
|
|
|
|
assert(0 && "Aggregate that isn't a structure or array?!");
|
2008-08-11 00:05:48 +08:00
|
|
|
|
} else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
|
|
|
|
|
// This type is invalid, issue a diagnostic.
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++Index;
|
2009-02-25 06:50:46 +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;
|
2009-01-31 06:09:00 +08:00
|
|
|
|
} else if (DeclType->isRecordType()) {
|
|
|
|
|
// C++ [dcl.init]p14:
|
|
|
|
|
// [...] If the class is an aggregate (8.5.1), and the initializer
|
|
|
|
|
// is a brace-enclosed list, see 8.5.1.
|
|
|
|
|
//
|
|
|
|
|
// Note: 8.5.1 is handled below; here, we diagnose the case where
|
|
|
|
|
// we have an initializer list and a destination type that is not
|
|
|
|
|
// an aggregate.
|
|
|
|
|
// FIXME: In C++0x, this is yet another form of initialization.
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
|
2009-01-31 06:09:00 +08:00
|
|
|
|
<< DeclType << IList->getSourceRange();
|
|
|
|
|
hadError = true;
|
|
|
|
|
} else if (DeclType->isReferenceType()) {
|
|
|
|
|
CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex);
|
2008-05-02 06:18:59 +08:00
|
|
|
|
} else {
|
|
|
|
|
// In C, all types are either scalars or aggregates, but
|
2009-09-09 23:08:12 +08:00
|
|
|
|
// additional handling is needed here for C++ (and possibly others?).
|
2008-05-02 06:18:59 +08:00
|
|
|
|
assert(0 && "Unsupported initializer type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-20 03:16:24 +08:00
|
|
|
|
void InitListChecker::CheckSubElementType(InitListExpr *IList,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
QualType ElemType,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex) {
|
2009-01-29 08:39:20 +08:00
|
|
|
|
Expr *expr = IList->getInit(Index);
|
2008-05-20 04:00:43 +08:00
|
|
|
|
if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
|
|
|
|
|
unsigned newIndex = 0;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned newStructuredIndex = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
InitListExpr *newStructuredList
|
2009-01-29 05:54:33 +08:00
|
|
|
|
= getStructuredSubobjectInit(IList, Index, ElemType,
|
|
|
|
|
StructuredList, StructuredIndex,
|
|
|
|
|
SubInitList->getSourceRange());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
CheckExplicitInitList(SubInitList, ElemType, newIndex,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
newStructuredList, newStructuredIndex);
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
++Index;
|
2009-02-25 07:10:27 +08:00
|
|
|
|
} else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
|
|
|
|
|
CheckStringInit(Str, ElemType, SemaRef);
|
2009-02-25 06:41:04 +08:00
|
|
|
|
UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++Index;
|
2008-05-20 04:00:43 +08:00
|
|
|
|
} else if (ElemType->isScalarType()) {
|
2009-01-29 08:39:20 +08:00
|
|
|
|
CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex);
|
2009-01-31 06:09:00 +08:00
|
|
|
|
} else if (ElemType->isReferenceType()) {
|
|
|
|
|
CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex);
|
2008-05-20 03:16:24 +08:00
|
|
|
|
} else {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
if (SemaRef.getLangOptions().CPlusPlus) {
|
2009-01-31 06:09:00 +08:00
|
|
|
|
// C++ [dcl.init.aggr]p12:
|
|
|
|
|
// All implicit type conversions (clause 4) are considered when
|
|
|
|
|
// initializing the aggregate member with an ini- tializer from
|
|
|
|
|
// an initializer-list. If the initializer can initialize a
|
|
|
|
|
// member, the member is initialized. [...]
|
2009-09-09 23:08:12 +08:00
|
|
|
|
ImplicitConversionSequence ICS
|
2009-08-28 01:18:13 +08:00
|
|
|
|
= SemaRef.TryCopyInitialization(expr, ElemType,
|
|
|
|
|
/*SuppressUserConversions=*/false,
|
2009-08-28 01:37:39 +08:00
|
|
|
|
/*ForceRValue=*/false,
|
|
|
|
|
/*InOverloadResolution=*/false);
|
2009-08-28 01:18:13 +08:00
|
|
|
|
|
2009-01-31 06:09:00 +08:00
|
|
|
|
if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS,
|
2009-12-16 11:45:30 +08:00
|
|
|
|
Sema::AA_Initializing))
|
2009-01-31 06:09:00 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
|
|
|
|
|
++Index;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall through for subaggregate initialization
|
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
// C99 6.7.8p13:
|
2009-01-31 06:09:00 +08:00
|
|
|
|
//
|
|
|
|
|
// The initializer for a structure or union object that has
|
|
|
|
|
// automatic storage duration shall be either an initializer
|
|
|
|
|
// list as described below, or a single expression that has
|
|
|
|
|
// compatible structure or union type. In the latter case, the
|
|
|
|
|
// initial value of the object, including unnamed members, is
|
|
|
|
|
// that of the expression.
|
2009-06-13 18:38:46 +08:00
|
|
|
|
if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
|
2009-05-30 02:22:49 +08:00
|
|
|
|
SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
|
2009-01-31 06:09:00 +08:00
|
|
|
|
UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
|
|
|
|
|
++Index;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall through for subaggregate initialization
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// C++ [dcl.init.aggr]p12:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
//
|
2009-01-31 06:09:00 +08:00
|
|
|
|
// [...] Otherwise, if the member is itself a non-empty
|
|
|
|
|
// subaggregate, brace elision is assumed and the initializer is
|
|
|
|
|
// considered for the initialization of the first member of
|
|
|
|
|
// the subaggregate.
|
|
|
|
|
if (ElemType->isAggregateType() || ElemType->isVectorType()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
CheckImplicitInitList(IList, ElemType, Index, StructuredList,
|
2009-01-31 06:09:00 +08:00
|
|
|
|
StructuredIndex);
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
} else {
|
|
|
|
|
// We cannot initialize this element, so let
|
|
|
|
|
// PerformCopyInitialization produce the appropriate diagnostic.
|
2009-12-16 11:45:30 +08:00
|
|
|
|
SemaRef.PerformCopyInitialization(expr, ElemType, Sema::AA_Initializing);
|
2009-01-31 06:09:00 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
++Index;
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-20 03:16:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-31 06:09:00 +08:00
|
|
|
|
void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType,
|
2009-01-29 08:39:20 +08:00
|
|
|
|
unsigned &Index,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex) {
|
2008-05-02 06:18:59 +08:00
|
|
|
|
if (Index < IList->getNumInits()) {
|
2009-01-29 08:39:20 +08:00
|
|
|
|
Expr *expr = IList->getInit(Index);
|
2008-05-20 04:00:43 +08:00
|
|
|
|
if (isa<InitListExpr>(expr)) {
|
2009-02-25 06:50:46 +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;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++StructuredIndex;
|
2008-05-20 04:12:18 +08:00
|
|
|
|
return;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
} else if (isa<DesignatedInitExpr>(expr)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(expr->getSourceRange().getBegin(),
|
2009-01-22 08:58:24 +08:00
|
|
|
|
diag::err_designator_for_scalar_init)
|
|
|
|
|
<< DeclType << expr->getSourceRange();
|
|
|
|
|
hadError = true;
|
|
|
|
|
++Index;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++StructuredIndex;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
return;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2008-05-20 04:00:43 +08:00
|
|
|
|
Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
|
2009-02-25 06:50:46 +08:00
|
|
|
|
if (CheckSingleInitializer(expr, DeclType, false, SemaRef))
|
2008-05-20 04:12:18 +08:00
|
|
|
|
hadError = true; // types weren't compatible.
|
2009-01-22 08:58:24 +08:00
|
|
|
|
else if (savExpr != expr) {
|
2008-05-20 04:00:43 +08:00
|
|
|
|
// The type was promoted, update initializer list.
|
2009-01-29 08:39:20 +08:00
|
|
|
|
IList->setInit(Index, expr);
|
2009-01-22 08:58:24 +08:00
|
|
|
|
}
|
2009-01-29 05:54:33 +08:00
|
|
|
|
if (hadError)
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
else
|
|
|
|
|
UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
|
2008-05-02 06:18:59 +08:00
|
|
|
|
++Index;
|
2008-05-20 04:12:18 +08:00
|
|
|
|
} else {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
|
2008-11-19 13:27:50 +08:00
|
|
|
|
<< IList->getSourceRange();
|
2008-05-20 04:12:18 +08:00
|
|
|
|
hadError = true;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++Index;
|
|
|
|
|
++StructuredIndex;
|
2008-05-20 04:12:18 +08:00
|
|
|
|
return;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-31 06:09:00 +08:00
|
|
|
|
void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType,
|
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex) {
|
|
|
|
|
if (Index < IList->getNumInits()) {
|
|
|
|
|
Expr *expr = IList->getInit(Index);
|
|
|
|
|
if (isa<InitListExpr>(expr)) {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
|
2009-01-31 06:09:00 +08:00
|
|
|
|
<< DeclType << IList->getSourceRange();
|
|
|
|
|
hadError = true;
|
|
|
|
|
++Index;
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
}
|
2009-01-31 06:09:00 +08:00
|
|
|
|
|
|
|
|
|
Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer.
|
2009-08-28 01:30:43 +08:00
|
|
|
|
if (SemaRef.CheckReferenceInit(expr, DeclType,
|
2009-09-24 07:04:10 +08:00
|
|
|
|
/*FIXME:*/expr->getLocStart(),
|
2009-08-28 01:30:43 +08:00
|
|
|
|
/*SuppressUserConversions=*/false,
|
|
|
|
|
/*AllowExplicit=*/false,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
/*ForceRValue=*/false))
|
2009-01-31 06:09:00 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
else if (savExpr != expr) {
|
|
|
|
|
// The type was promoted, update initializer list.
|
|
|
|
|
IList->setInit(Index, expr);
|
|
|
|
|
}
|
|
|
|
|
if (hadError)
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
else
|
|
|
|
|
UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
|
|
|
|
|
++Index;
|
|
|
|
|
} else {
|
2009-05-16 15:39:55 +08:00
|
|
|
|
// FIXME: It would be wonderful if we could point at the actual member. In
|
|
|
|
|
// general, it would be useful to pass location information down the stack,
|
|
|
|
|
// so that we know the location (or decl) of the "current object" being
|
|
|
|
|
// initialized.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(IList->getLocStart(),
|
2009-01-31 06:09:00 +08:00
|
|
|
|
diag::err_init_reference_member_uninitialized)
|
|
|
|
|
<< DeclType
|
|
|
|
|
<< IList->getSourceRange();
|
|
|
|
|
hadError = true;
|
|
|
|
|
++Index;
|
|
|
|
|
++StructuredIndex;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex) {
|
2008-05-02 06:18:59 +08:00
|
|
|
|
if (Index < IList->getNumInits()) {
|
2009-09-22 07:43:11 +08:00
|
|
|
|
const VectorType *VT = DeclType->getAs<VectorType>();
|
2009-08-11 07:49:36 +08:00
|
|
|
|
unsigned maxElements = VT->getNumElements();
|
|
|
|
|
unsigned numEltsInit = 0;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
QualType elementType = VT->getElementType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-08-11 07:49:36 +08:00
|
|
|
|
if (!SemaRef.getLangOptions().OpenCL) {
|
|
|
|
|
for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
|
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
|
if (Index >= IList->getNumInits())
|
|
|
|
|
break;
|
|
|
|
|
CheckSubElementType(IList, elementType, Index,
|
|
|
|
|
StructuredList, StructuredIndex);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// OpenCL initializers allows vectors to be constructed from vectors.
|
|
|
|
|
for (unsigned i = 0; i < maxElements; ++i) {
|
|
|
|
|
// Don't attempt to go past the end of the init list
|
|
|
|
|
if (Index >= IList->getNumInits())
|
|
|
|
|
break;
|
|
|
|
|
QualType IType = IList->getInit(Index)->getType();
|
|
|
|
|
if (!IType->isVectorType()) {
|
|
|
|
|
CheckSubElementType(IList, elementType, Index,
|
|
|
|
|
StructuredList, StructuredIndex);
|
|
|
|
|
++numEltsInit;
|
|
|
|
|
} else {
|
2009-09-22 07:43:11 +08:00
|
|
|
|
const VectorType *IVT = IType->getAs<VectorType>();
|
2009-08-11 07:49:36 +08:00
|
|
|
|
unsigned numIElts = IVT->getNumElements();
|
|
|
|
|
QualType VecType = SemaRef.Context.getExtVectorType(elementType,
|
|
|
|
|
numIElts);
|
|
|
|
|
CheckSubElementType(IList, VecType, Index,
|
|
|
|
|
StructuredList, StructuredIndex);
|
|
|
|
|
numEltsInit += numIElts;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-08-11 07:49:36 +08:00
|
|
|
|
// OpenCL & AltiVec require all elements to be initialized.
|
|
|
|
|
if (numEltsInit != maxElements)
|
|
|
|
|
if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
|
|
|
|
|
SemaRef.Diag(IList->getSourceRange().getBegin(),
|
|
|
|
|
diag::err_vector_incorrect_num_initializers)
|
|
|
|
|
<< (numEltsInit < maxElements) << maxElements << numEltsInit;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType,
|
2009-01-23 07:26:18 +08:00
|
|
|
|
llvm::APSInt elementIndex,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
bool SubobjectIsDesignatorContext,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex) {
|
2008-05-02 06:18:59 +08:00
|
|
|
|
// Check for the special-case of initializing an array with a string.
|
|
|
|
|
if (Index < IList->getNumInits()) {
|
2009-02-25 07:10:27 +08:00
|
|
|
|
if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
|
|
|
|
|
SemaRef.Context)) {
|
|
|
|
|
CheckStringInit(Str, DeclType, SemaRef);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// We place the string literal directly into the resulting
|
|
|
|
|
// initializer list. This is the only place where the structure
|
|
|
|
|
// of the structured initializer list doesn't match exactly,
|
|
|
|
|
// because doing so would involve allocating one character
|
|
|
|
|
// constant for each string.
|
2009-02-25 06:41:04 +08:00
|
|
|
|
UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
|
2009-02-25 06:50:46 +08:00
|
|
|
|
StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
|
2008-05-02 06:18:59 +08:00
|
|
|
|
++Index;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-04 15:31:14 +08:00
|
|
|
|
if (const VariableArrayType *VAT =
|
2009-02-25 06:50:46 +08:00
|
|
|
|
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).
|
2009-02-25 06:50:46 +08:00
|
|
|
|
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;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++Index;
|
|
|
|
|
++StructuredIndex;
|
2008-05-25 21:22:35 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
|
// We might know the maximum number of elements in advance.
|
2009-01-29 05:54:33 +08:00
|
|
|
|
llvm::APSInt maxElements(elementIndex.getBitWidth(),
|
|
|
|
|
elementIndex.isUnsigned());
|
2009-01-22 08:58:24 +08:00
|
|
|
|
bool maxElementsKnown = false;
|
|
|
|
|
if (const ConstantArrayType *CAT =
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Context.getAsConstantArrayType(DeclType)) {
|
2009-01-22 08:58:24 +08:00
|
|
|
|
maxElements = CAT->getSize();
|
2009-01-24 00:54:12 +08:00
|
|
|
|
elementIndex.extOrTrunc(maxElements.getBitWidth());
|
2009-01-24 02:58:42 +08:00
|
|
|
|
elementIndex.setIsUnsigned(maxElements.isUnsigned());
|
2009-01-22 08:58:24 +08:00
|
|
|
|
maxElementsKnown = true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-25 06:50:46 +08:00
|
|
|
|
QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
|
2008-08-04 15:31:14 +08:00
|
|
|
|
->getElementType();
|
2009-01-22 08:58:24 +08:00
|
|
|
|
while (Index < IList->getNumInits()) {
|
|
|
|
|
Expr *Init = IList->getInit(Index);
|
|
|
|
|
if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// If we're not the subobject that matches up with the '{' for
|
|
|
|
|
// the designator, we shouldn't be handling the
|
|
|
|
|
// designator. Return immediately.
|
|
|
|
|
if (!SubobjectIsDesignatorContext)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Handle this designated initializer. elementIndex will be
|
|
|
|
|
// updated to be the next array element we'll initialize.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (CheckDesignatedInitializer(IList, DIE, 0,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
DeclType, 0, &elementIndex, Index,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
StructuredList, StructuredIndex, true,
|
|
|
|
|
false)) {
|
2009-01-22 08:58:24 +08:00
|
|
|
|
hadError = true;
|
2009-01-23 07:26:18 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-24 00:54:12 +08:00
|
|
|
|
if (elementIndex.getBitWidth() > maxElements.getBitWidth())
|
|
|
|
|
maxElements.extend(elementIndex.getBitWidth());
|
|
|
|
|
else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
|
|
|
|
|
elementIndex.extend(maxElements.getBitWidth());
|
2009-01-24 02:58:42 +08:00
|
|
|
|
elementIndex.setIsUnsigned(maxElements.isUnsigned());
|
2009-01-24 00:54:12 +08:00
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// If the array is of incomplete type, keep track of the number of
|
|
|
|
|
// elements in the initializer.
|
|
|
|
|
if (!maxElementsKnown && elementIndex > maxElements)
|
|
|
|
|
maxElements = elementIndex;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we know the maximum number of elements, and we've already
|
|
|
|
|
// hit it, stop consuming elements in the initializer list.
|
|
|
|
|
if (maxElementsKnown && elementIndex == maxElements)
|
2008-05-02 06:18:59 +08:00
|
|
|
|
break;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
|
|
|
|
// Check this element.
|
2009-01-29 08:39:20 +08:00
|
|
|
|
CheckSubElementType(IList, elementType, Index,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredList, StructuredIndex);
|
2009-01-22 08:58:24 +08:00
|
|
|
|
++elementIndex;
|
|
|
|
|
|
|
|
|
|
// If the array is of incomplete type, keep track of the number of
|
|
|
|
|
// elements in the initializer.
|
|
|
|
|
if (!maxElementsKnown && elementIndex > maxElements)
|
|
|
|
|
maxElements = elementIndex;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
2009-05-30 04:17:55 +08:00
|
|
|
|
if (!hadError && DeclType->isIncompleteArrayType()) {
|
2008-05-02 06:18:59 +08:00
|
|
|
|
// If this is an incomplete array type, the actual type needs to
|
2008-08-19 04:28:46 +08:00
|
|
|
|
// be calculated here.
|
2009-01-24 02:58:42 +08:00
|
|
|
|
llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
|
2009-01-22 08:58:24 +08:00
|
|
|
|
if (maxElements == Zero) {
|
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.
|
2009-02-25 06:50:46 +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
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
|
2008-08-19 04:28:46 +08:00
|
|
|
|
ArrayType::Normal, 0);
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
void InitListChecker::CheckStructUnionTypes(InitListExpr *IList,
|
|
|
|
|
QualType DeclType,
|
2009-01-23 07:26:18 +08:00
|
|
|
|
RecordDecl::field_iterator Field,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
bool SubobjectIsDesignatorContext,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
bool TopLevelObject) {
|
2009-07-30 05:53:49 +08:00
|
|
|
|
RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
|
2009-09-09 23:08:12 +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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
}
|
2009-01-30 01:44:32 +08:00
|
|
|
|
|
|
|
|
|
if (DeclType->isUnionType() && IList->getNumInits() == 0) {
|
|
|
|
|
// Value-initialize the first named member of the union.
|
2009-07-30 05:53:49 +08:00
|
|
|
|
RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
|
2009-06-30 10:36:12 +08:00
|
|
|
|
for (RecordDecl::field_iterator FieldEnd = RD->field_end();
|
2009-01-30 01:44:32 +08:00
|
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
|
if (Field->getDeclName()) {
|
|
|
|
|
StructuredList->setInitializedFieldInUnion(*Field);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
|
// If structDecl is a forward declaration, this loop won't do
|
|
|
|
|
// anything except look at designated initializers; That's okay,
|
|
|
|
|
// because an error should get printed out elsewhere. It might be
|
|
|
|
|
// worthwhile to skip over the rest of the initializer, though.
|
2009-07-30 05:53:49 +08:00
|
|
|
|
RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
|
2009-06-30 10:36:12 +08:00
|
|
|
|
RecordDecl::field_iterator FieldEnd = RD->field_end();
|
2009-02-13 03:00:39 +08:00
|
|
|
|
bool InitializedSomething = false;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
while (Index < IList->getNumInits()) {
|
|
|
|
|
Expr *Init = IList->getInit(Index);
|
|
|
|
|
|
|
|
|
|
if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// If we're not the subobject that matches up with the '{' for
|
|
|
|
|
// the designator, we shouldn't be handling the
|
|
|
|
|
// designator. Return immediately.
|
|
|
|
|
if (!SubobjectIsDesignatorContext)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Handle this designated initializer. Field will be updated to
|
|
|
|
|
// the next field that we'll be initializing.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (CheckDesignatedInitializer(IList, DIE, 0,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
DeclType, &Field, 0, Index,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
StructuredList, StructuredIndex,
|
|
|
|
|
true, TopLevelObject))
|
2009-01-22 08:58:24 +08:00
|
|
|
|
hadError = true;
|
|
|
|
|
|
2009-02-13 03:00:39 +08:00
|
|
|
|
InitializedSomething = true;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Field == FieldEnd) {
|
|
|
|
|
// We've run out of fields. We're done.
|
2008-12-12 00:49:14 +08:00
|
|
|
|
break;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
2009-02-13 03:00:39 +08:00
|
|
|
|
// We've already initialized a member of a union. We're done.
|
|
|
|
|
if (InitializedSomething && DeclType->isUnionType())
|
|
|
|
|
break;
|
|
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
|
// If we've hit the flexible array member at the end, we're done.
|
|
|
|
|
if (Field->getType()->isIncompleteArrayType())
|
2008-05-20 03:16:24 +08:00
|
|
|
|
break;
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
2009-01-30 00:53:55 +08:00
|
|
|
|
if (Field->isUnnamedBitfield()) {
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Don't initialize unnamed bitfields, e.g. "int : 20;"
|
2009-01-22 08:58:24 +08:00
|
|
|
|
++Field;
|
2008-05-20 03:16:24 +08:00
|
|
|
|
continue;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
2009-01-29 08:39:20 +08:00
|
|
|
|
CheckSubElementType(IList, Field->getType(), Index,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredList, StructuredIndex);
|
2009-02-13 03:00:39 +08:00
|
|
|
|
InitializedSomething = true;
|
2009-01-30 00:53:55 +08:00
|
|
|
|
|
|
|
|
|
if (DeclType->isUnionType()) {
|
|
|
|
|
// Initialize the first field within the union.
|
|
|
|
|
StructuredList->setInitializedFieldInUnion(*Field);
|
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
|
|
|
|
++Field;
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
2008-12-12 00:49:14 +08:00
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
|
2009-03-20 08:32:56 +08:00
|
|
|
|
Index >= IList->getNumInits())
|
2009-02-05 06:46:25 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Handle GNU flexible array initializers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (!TopLevelObject &&
|
2009-03-20 08:32:56 +08:00
|
|
|
|
(!isa<InitListExpr>(IList->getInit(Index)) ||
|
|
|
|
|
cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
|
2009-02-05 06:46:25 +08:00
|
|
|
|
diag::err_flexible_array_init_nonempty)
|
|
|
|
|
<< IList->getInit(Index)->getSourceRange().getBegin();
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
|
2009-02-05 06:46:25 +08:00
|
|
|
|
<< *Field;
|
|
|
|
|
hadError = true;
|
2009-03-20 08:32:56 +08:00
|
|
|
|
++Index;
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
|
2009-03-20 08:32:56 +08:00
|
|
|
|
diag::ext_flexible_array_init)
|
|
|
|
|
<< IList->getInit(Index)->getSourceRange().getBegin();
|
|
|
|
|
SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
|
|
|
|
|
<< *Field;
|
2009-02-05 06:46:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-20 08:32:56 +08:00
|
|
|
|
if (isa<InitListExpr>(IList->getInit(Index)))
|
|
|
|
|
CheckSubElementType(IList, Field->getType(), Index, StructuredList,
|
|
|
|
|
StructuredIndex);
|
|
|
|
|
else
|
|
|
|
|
CheckImplicitInitList(IList, Field->getType(), Index, StructuredList,
|
|
|
|
|
StructuredIndex);
|
2008-05-02 06:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-15 14:41:24 +08:00
|
|
|
|
/// \brief Expand a field designator that refers to a member of an
|
|
|
|
|
/// anonymous struct or union into a series of field designators that
|
|
|
|
|
/// refers to the field within the appropriate subobject.
|
|
|
|
|
///
|
|
|
|
|
/// Field/FieldIndex will be updated to point to the (new)
|
|
|
|
|
/// currently-designated field.
|
|
|
|
|
static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
DesignatedInitExpr *DIE,
|
|
|
|
|
unsigned DesigIdx,
|
2009-04-15 14:41:24 +08:00
|
|
|
|
FieldDecl *Field,
|
|
|
|
|
RecordDecl::field_iterator &FieldIter,
|
|
|
|
|
unsigned &FieldIndex) {
|
|
|
|
|
typedef DesignatedInitExpr::Designator Designator;
|
|
|
|
|
|
|
|
|
|
// Build the path from the current object to the member of the
|
|
|
|
|
// anonymous struct/union (backwards).
|
|
|
|
|
llvm::SmallVector<FieldDecl *, 4> Path;
|
|
|
|
|
SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-04-15 14:41:24 +08:00
|
|
|
|
// Build the replacement designators.
|
|
|
|
|
llvm::SmallVector<Designator, 4> Replacements;
|
|
|
|
|
for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
|
|
|
|
|
FI = Path.rbegin(), FIEnd = Path.rend();
|
|
|
|
|
FI != FIEnd; ++FI) {
|
|
|
|
|
if (FI + 1 == FIEnd)
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Replacements.push_back(Designator((IdentifierInfo *)0,
|
2009-04-15 14:41:24 +08:00
|
|
|
|
DIE->getDesignator(DesigIdx)->getDotLoc(),
|
|
|
|
|
DIE->getDesignator(DesigIdx)->getFieldLoc()));
|
|
|
|
|
else
|
|
|
|
|
Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
|
|
|
|
|
SourceLocation()));
|
|
|
|
|
Replacements.back().setField(*FI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expand the current designator into the set of replacement
|
|
|
|
|
// designators, so we have a full subobject path down to where the
|
|
|
|
|
// member of the anonymous struct/union is actually stored.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
DIE->ExpandDesignator(DesigIdx, &Replacements[0],
|
2009-04-15 14:41:24 +08:00
|
|
|
|
&Replacements[0] + Replacements.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-04-15 14:41:24 +08:00
|
|
|
|
// Update FieldIter/FieldIndex;
|
|
|
|
|
RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
|
2009-06-30 10:36:12 +08:00
|
|
|
|
FieldIter = Record->field_begin();
|
2009-04-15 14:41:24 +08:00
|
|
|
|
FieldIndex = 0;
|
2009-06-30 10:36:12 +08:00
|
|
|
|
for (RecordDecl::field_iterator FEnd = Record->field_end();
|
2009-04-15 14:41:24 +08:00
|
|
|
|
FieldIter != FEnd; ++FieldIter) {
|
|
|
|
|
if (FieldIter->isUnnamedBitfield())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (*FieldIter == Path.back())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
++FieldIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(false && "Unable to find anonymous struct/union field");
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
|
/// @brief Check the well-formedness of a C99 designated initializer.
|
|
|
|
|
///
|
|
|
|
|
/// Determines whether the designated initializer @p DIE, which
|
|
|
|
|
/// resides at the given @p Index within the initializer list @p
|
|
|
|
|
/// IList, is well-formed for a current object of type @p DeclType
|
|
|
|
|
/// (C99 6.7.8). The actual subobject that this designator refers to
|
2009-09-09 23:08:12 +08:00
|
|
|
|
/// within the current subobject is returned in either
|
2009-01-29 05:54:33 +08:00
|
|
|
|
/// @p NextField or @p NextElementIndex (whichever is appropriate).
|
2009-01-22 08:58:24 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param IList The initializer list in which this designated
|
|
|
|
|
/// initializer occurs.
|
|
|
|
|
///
|
2009-04-15 12:56:10 +08:00
|
|
|
|
/// @param DIE The designated initializer expression.
|
|
|
|
|
///
|
|
|
|
|
/// @param DesigIdx The index of the current designator.
|
2009-01-22 08:58:24 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param DeclType The type of the "current object" (C99 6.7.8p17),
|
|
|
|
|
/// into which the designation in @p DIE should refer.
|
|
|
|
|
///
|
2009-01-23 07:26:18 +08:00
|
|
|
|
/// @param NextField If non-NULL and the first designator in @p DIE is
|
|
|
|
|
/// a field, this will be set to the field declaration corresponding
|
|
|
|
|
/// to the field named by the designator.
|
2009-01-22 08:58:24 +08:00
|
|
|
|
///
|
2009-01-23 07:26:18 +08:00
|
|
|
|
/// @param NextElementIndex If non-NULL and the first designator in @p
|
|
|
|
|
/// DIE is an array designator or GNU array-range designator, this
|
|
|
|
|
/// will be set to the last index initialized by this designator.
|
2009-01-22 08:58:24 +08:00
|
|
|
|
///
|
|
|
|
|
/// @param Index Index into @p IList where the designated initializer
|
|
|
|
|
/// @p DIE occurs.
|
|
|
|
|
///
|
2009-01-29 05:54:33 +08:00
|
|
|
|
/// @param StructuredList The initializer list expression that
|
|
|
|
|
/// describes all of the subobject initializers in the order they'll
|
|
|
|
|
/// actually be initialized.
|
|
|
|
|
///
|
2009-01-22 08:58:24 +08:00
|
|
|
|
/// @returns true if there was an error, false otherwise.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
bool
|
2009-01-23 07:26:18 +08:00
|
|
|
|
InitListChecker::CheckDesignatedInitializer(InitListExpr *IList,
|
2009-09-09 23:08:12 +08:00
|
|
|
|
DesignatedInitExpr *DIE,
|
2009-04-15 12:56:10 +08:00
|
|
|
|
unsigned DesigIdx,
|
2009-01-23 07:26:18 +08:00
|
|
|
|
QualType &CurrentObjectType,
|
|
|
|
|
RecordDecl::field_iterator *NextField,
|
|
|
|
|
llvm::APSInt *NextElementIndex,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned &Index,
|
|
|
|
|
InitListExpr *StructuredList,
|
2009-01-29 07:36:17 +08:00
|
|
|
|
unsigned &StructuredIndex,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
bool FinishSubobjectInit,
|
|
|
|
|
bool TopLevelObject) {
|
2009-04-15 12:56:10 +08:00
|
|
|
|
if (DesigIdx == DIE->size()) {
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// Check the actual initialization for the designated object type.
|
|
|
|
|
bool prevHadError = hadError;
|
2009-01-29 08:39:20 +08:00
|
|
|
|
|
|
|
|
|
// Temporarily remove the designator expression from the
|
|
|
|
|
// initializer list that the child calls see, so that we don't try
|
|
|
|
|
// to re-process the designator.
|
|
|
|
|
unsigned OldIndex = Index;
|
|
|
|
|
IList->setInit(OldIndex, DIE->getInit());
|
|
|
|
|
|
|
|
|
|
CheckSubElementType(IList, CurrentObjectType, Index,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredList, StructuredIndex);
|
2009-01-29 08:39:20 +08:00
|
|
|
|
|
|
|
|
|
// Restore the designated initializer expression in the syntactic
|
|
|
|
|
// form of the initializer list.
|
|
|
|
|
if (IList->getInit(OldIndex) != DIE->getInit())
|
|
|
|
|
DIE->setInit(IList->getInit(OldIndex));
|
|
|
|
|
IList->setInit(OldIndex, DIE);
|
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
return hadError && !prevHadError;
|
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2009-04-15 12:56:10 +08:00
|
|
|
|
bool IsFirstDesignator = (DesigIdx == 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
assert((IsFirstDesignator || StructuredList) &&
|
2009-01-29 05:54:33 +08:00
|
|
|
|
"Need a non-designated initializer list to start from");
|
|
|
|
|
|
2009-04-15 12:56:10 +08:00
|
|
|
|
DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Determine the structural initializer list that corresponds to the
|
|
|
|
|
// current subobject.
|
|
|
|
|
StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
|
2009-09-09 23:08:12 +08:00
|
|
|
|
: getStructuredSubobjectInit(IList, Index, CurrentObjectType,
|
2009-03-02 01:12:46 +08:00
|
|
|
|
StructuredList, StructuredIndex,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
SourceRange(D->getStartLocation(),
|
|
|
|
|
DIE->getSourceRange().getEnd()));
|
|
|
|
|
assert(StructuredList && "Expected a structured initializer list");
|
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
if (D->isFieldDesignator()) {
|
|
|
|
|
// C99 6.7.8p7:
|
|
|
|
|
//
|
|
|
|
|
// If a designator has the form
|
|
|
|
|
//
|
|
|
|
|
// . identifier
|
|
|
|
|
//
|
|
|
|
|
// then the current object (defined below) shall have
|
|
|
|
|
// structure or union type and the identifier shall be the
|
2009-09-09 23:08:12 +08:00
|
|
|
|
// name of a member of that type.
|
2009-07-30 05:53:49 +08:00
|
|
|
|
const RecordType *RT = CurrentObjectType->getAs<RecordType>();
|
2009-01-23 07:26:18 +08:00
|
|
|
|
if (!RT) {
|
|
|
|
|
SourceLocation Loc = D->getDotLoc();
|
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
|
Loc = D->getFieldLoc();
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
|
|
|
|
|
<< SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
|
2009-01-23 07:26:18 +08:00
|
|
|
|
++Index;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Note: we perform a linear search of the fields here, despite
|
|
|
|
|
// the fact that we have a faster lookup method, because we always
|
|
|
|
|
// need to compute the field's index.
|
2009-04-15 14:41:24 +08:00
|
|
|
|
FieldDecl *KnownField = D->getField();
|
2009-01-23 07:26:18 +08:00
|
|
|
|
IdentifierInfo *FieldName = D->getFieldName();
|
2009-01-29 05:54:33 +08:00
|
|
|
|
unsigned FieldIndex = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
RecordDecl::field_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
|
Field = RT->getDecl()->field_begin(),
|
|
|
|
|
FieldEnd = RT->getDecl()->field_end();
|
2009-01-29 05:54:33 +08:00
|
|
|
|
for (; Field != FieldEnd; ++Field) {
|
|
|
|
|
if (Field->isUnnamedBitfield())
|
|
|
|
|
continue;
|
|
|
|
|
|
2009-04-15 14:41:24 +08:00
|
|
|
|
if (KnownField == *Field || Field->getIdentifier() == FieldName)
|
2009-01-29 05:54:33 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
++FieldIndex;
|
2009-01-23 07:26:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
if (Field == FieldEnd) {
|
2009-04-15 14:41:24 +08:00
|
|
|
|
// There was no normal field in the struct with the designated
|
|
|
|
|
// name. Perform another lookup for this name, which may find
|
|
|
|
|
// something that we can't designate (e.g., a member function),
|
|
|
|
|
// may find nothing, or may find a member of an anonymous
|
2009-09-09 23:08:12 +08:00
|
|
|
|
// struct/union.
|
2009-06-30 10:36:12 +08:00
|
|
|
|
DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
if (Lookup.first == Lookup.second) {
|
|
|
|
|
// Name lookup didn't find anything.
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
|
2009-01-29 05:54:33 +08:00
|
|
|
|
<< FieldName << CurrentObjectType;
|
2009-04-15 14:41:24 +08:00
|
|
|
|
++Index;
|
|
|
|
|
return true;
|
|
|
|
|
} else if (!KnownField && isa<FieldDecl>(*Lookup.first) &&
|
|
|
|
|
cast<RecordDecl>((*Lookup.first)->getDeclContext())
|
|
|
|
|
->isAnonymousStructOrUnion()) {
|
|
|
|
|
// Handle an field designator that refers to a member of an
|
|
|
|
|
// anonymous struct or union.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
|
2009-04-15 14:41:24 +08:00
|
|
|
|
cast<FieldDecl>(*Lookup.first),
|
|
|
|
|
Field, FieldIndex);
|
2009-04-17 01:49:48 +08:00
|
|
|
|
D = DIE->getDesignator(DesigIdx);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
} else {
|
|
|
|
|
// Name lookup found something, but it wasn't a field.
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
|
2009-01-29 05:54:33 +08:00
|
|
|
|
<< FieldName;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag((*Lookup.first)->getLocation(),
|
2009-01-29 05:54:33 +08:00
|
|
|
|
diag::note_field_designator_found);
|
2009-04-17 01:49:48 +08:00
|
|
|
|
++Index;
|
|
|
|
|
return true;
|
2009-04-15 14:41:24 +08:00
|
|
|
|
}
|
|
|
|
|
} else if (!KnownField &&
|
|
|
|
|
cast<RecordDecl>((*Field)->getDeclContext())
|
2009-01-29 05:54:33 +08:00
|
|
|
|
->isAnonymousStructOrUnion()) {
|
2009-04-15 14:41:24 +08:00
|
|
|
|
ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
|
|
|
|
|
Field, FieldIndex);
|
|
|
|
|
D = DIE->getDesignator(DesigIdx);
|
2009-01-23 07:26:18 +08:00
|
|
|
|
}
|
2009-01-29 05:54:33 +08:00
|
|
|
|
|
|
|
|
|
// All of the fields of a union are located at the same place in
|
|
|
|
|
// the initializer list.
|
2009-01-30 00:53:55 +08:00
|
|
|
|
if (RT->getDecl()->isUnion()) {
|
2009-01-29 05:54:33 +08:00
|
|
|
|
FieldIndex = 0;
|
2009-01-30 00:53:55 +08:00
|
|
|
|
StructuredList->setInitializedFieldInUnion(*Field);
|
|
|
|
|
}
|
2009-01-29 05:54:33 +08:00
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// Update the designator with the field declaration.
|
2009-01-29 05:54:33 +08:00
|
|
|
|
D->setField(*Field);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Make sure that our non-designated initializer list has space
|
|
|
|
|
// for a subobject corresponding to this field.
|
|
|
|
|
if (FieldIndex >= StructuredList->getNumInits())
|
2009-02-25 06:50:46 +08:00
|
|
|
|
StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
|
2009-01-29 05:54:33 +08:00
|
|
|
|
|
2009-02-05 06:46:25 +08:00
|
|
|
|
// This designator names a flexible array member.
|
|
|
|
|
if (Field->getType()->isIncompleteArrayType()) {
|
|
|
|
|
bool Invalid = false;
|
2009-04-15 12:56:10 +08:00
|
|
|
|
if ((DesigIdx + 1) != DIE->size()) {
|
2009-02-05 06:46:25 +08:00
|
|
|
|
// We can't designate an object within the flexible array
|
|
|
|
|
// member (because GCC doesn't allow it).
|
2009-09-09 23:08:12 +08:00
|
|
|
|
DesignatedInitExpr::Designator *NextD
|
2009-04-15 12:56:10 +08:00
|
|
|
|
= DIE->getDesignator(DesigIdx + 1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(NextD->getStartLocation(),
|
2009-02-05 06:46:25 +08:00
|
|
|
|
diag::err_designator_into_flexible_array_member)
|
2009-09-09 23:08:12 +08:00
|
|
|
|
<< SourceRange(NextD->getStartLocation(),
|
2009-02-05 06:46:25 +08:00
|
|
|
|
DIE->getSourceRange().getEnd());
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
|
2009-02-05 06:46:25 +08:00
|
|
|
|
<< *Field;
|
|
|
|
|
Invalid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
|
|
|
|
|
// The initializer is not an initializer list.
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
|
2009-02-05 06:46:25 +08:00
|
|
|
|
diag::err_flexible_array_init_needs_braces)
|
|
|
|
|
<< DIE->getInit()->getSourceRange();
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
|
2009-02-05 06:46:25 +08:00
|
|
|
|
<< *Field;
|
|
|
|
|
Invalid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle GNU flexible array initializers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (!Invalid && !TopLevelObject &&
|
2009-02-05 06:46:25 +08:00
|
|
|
|
cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(DIE->getSourceRange().getBegin(),
|
2009-02-05 06:46:25 +08:00
|
|
|
|
diag::err_flexible_array_init_nonempty)
|
|
|
|
|
<< DIE->getSourceRange().getBegin();
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
|
2009-02-05 06:46:25 +08:00
|
|
|
|
<< *Field;
|
|
|
|
|
Invalid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Invalid) {
|
|
|
|
|
++Index;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the array.
|
|
|
|
|
bool prevHadError = hadError;
|
|
|
|
|
unsigned newStructuredIndex = FieldIndex;
|
|
|
|
|
unsigned OldIndex = Index;
|
|
|
|
|
IList->setInit(Index, DIE->getInit());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
CheckSubElementType(IList, Field->getType(), Index,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
StructuredList, newStructuredIndex);
|
|
|
|
|
IList->setInit(OldIndex, DIE);
|
|
|
|
|
if (hadError && !prevHadError) {
|
|
|
|
|
++Field;
|
|
|
|
|
++FieldIndex;
|
|
|
|
|
if (NextField)
|
|
|
|
|
*NextField = Field;
|
|
|
|
|
StructuredIndex = FieldIndex;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Recurse to check later designated subobjects.
|
|
|
|
|
QualType FieldType = (*Field)->getType();
|
|
|
|
|
unsigned newStructuredIndex = FieldIndex;
|
2009-04-15 12:56:10 +08:00
|
|
|
|
if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0,
|
|
|
|
|
Index, StructuredList, newStructuredIndex,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
true, false))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-01-23 07:26:18 +08:00
|
|
|
|
|
|
|
|
|
// Find the position of the next field to be initialized in this
|
|
|
|
|
// subobject.
|
|
|
|
|
++Field;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++FieldIndex;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// If this the first designator, our caller will continue checking
|
|
|
|
|
// the rest of this struct/class/union subobject.
|
|
|
|
|
if (IsFirstDesignator) {
|
|
|
|
|
if (NextField)
|
|
|
|
|
*NextField = Field;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredIndex = FieldIndex;
|
2009-01-23 07:26:18 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2009-01-29 07:36:17 +08:00
|
|
|
|
if (!FinishSubobjectInit)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-04-15 14:41:24 +08:00
|
|
|
|
// We've already initialized something in the union; we're done.
|
|
|
|
|
if (RT->getDecl()->isUnion())
|
|
|
|
|
return hadError;
|
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// Check the remaining fields within this class/struct/union subobject.
|
|
|
|
|
bool prevHadError = hadError;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index,
|
|
|
|
|
StructuredList, FieldIndex);
|
2009-01-23 07:26:18 +08:00
|
|
|
|
return hadError && !prevHadError;
|
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// C99 6.7.8p6:
|
|
|
|
|
//
|
|
|
|
|
// If a designator has the form
|
|
|
|
|
//
|
|
|
|
|
// [ constant-expression ]
|
|
|
|
|
//
|
|
|
|
|
// then the current object (defined below) shall have array
|
|
|
|
|
// type and the expression shall be an integer constant
|
|
|
|
|
// expression. If the array is of unknown size, any
|
|
|
|
|
// nonnegative value is valid.
|
|
|
|
|
//
|
|
|
|
|
// Additionally, cope with the GNU extension that permits
|
|
|
|
|
// designators of the form
|
|
|
|
|
//
|
|
|
|
|
// [ constant-expression ... constant-expression ]
|
2009-02-25 06:50:46 +08:00
|
|
|
|
const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
|
2009-01-23 07:26:18 +08:00
|
|
|
|
if (!AT) {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
|
2009-01-23 07:26:18 +08:00
|
|
|
|
<< CurrentObjectType;
|
|
|
|
|
++Index;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
Expr *IndexExpr = 0;
|
2009-01-29 07:36:17 +08:00
|
|
|
|
llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
|
|
|
|
|
if (D->isArrayDesignator()) {
|
2009-01-23 07:26:18 +08:00
|
|
|
|
IndexExpr = DIE->getArrayIndex(*D);
|
2009-04-26 05:59:05 +08:00
|
|
|
|
DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
|
2009-01-29 07:36:17 +08:00
|
|
|
|
DesignatedEndIndex = DesignatedStartIndex;
|
|
|
|
|
} else {
|
2009-01-23 07:26:18 +08:00
|
|
|
|
assert(D->isArrayRangeDesignator() && "Need array-range designator");
|
2009-04-26 05:59:05 +08:00
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
|
DesignatedStartIndex =
|
2009-04-26 05:59:05 +08:00
|
|
|
|
DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
DesignatedEndIndex =
|
2009-04-26 05:59:05 +08:00
|
|
|
|
DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
|
2009-01-23 07:26:18 +08:00
|
|
|
|
IndexExpr = DIE->getArrayRangeEnd(*D);
|
2009-01-29 07:36:17 +08:00
|
|
|
|
|
2009-04-26 05:59:05 +08:00
|
|
|
|
if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
|
2009-01-30 03:42:23 +08:00
|
|
|
|
FullyStructuredList->sawArrayRangeDesignator();
|
2009-01-23 07:26:18 +08:00
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
if (isa<ConstantArrayType>(AT)) {
|
|
|
|
|
llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
|
2009-01-29 07:36:17 +08:00
|
|
|
|
DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
|
|
|
|
|
DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
|
|
|
|
|
DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
|
|
|
|
|
DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
|
|
|
|
|
if (DesignatedEndIndex >= MaxElements) {
|
2009-02-25 06:50:46 +08:00
|
|
|
|
SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
|
2009-01-23 07:26:18 +08:00
|
|
|
|
diag::err_array_designator_too_large)
|
2009-01-29 07:36:17 +08:00
|
|
|
|
<< DesignatedEndIndex.toString(10) << MaxElements.toString(10)
|
2009-01-23 07:26:18 +08:00
|
|
|
|
<< IndexExpr->getSourceRange();
|
|
|
|
|
++Index;
|
|
|
|
|
return true;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
}
|
2009-01-29 07:36:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
// Make sure the bit-widths and signedness match.
|
|
|
|
|
if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
|
|
|
|
|
DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
|
2009-04-26 05:59:05 +08:00
|
|
|
|
else if (DesignatedStartIndex.getBitWidth() <
|
|
|
|
|
DesignatedEndIndex.getBitWidth())
|
2009-01-29 07:36:17 +08:00
|
|
|
|
DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
|
|
|
|
|
DesignatedStartIndex.setIsUnsigned(true);
|
|
|
|
|
DesignatedEndIndex.setIsUnsigned(true);
|
2009-01-22 08:58:24 +08:00
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Make sure that our non-designated initializer list has space
|
|
|
|
|
// for a subobject corresponding to this array element.
|
2009-01-29 07:36:17 +08:00
|
|
|
|
if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
|
2009-09-09 23:08:12 +08:00
|
|
|
|
StructuredList->resizeInits(SemaRef.Context,
|
2009-01-29 07:36:17 +08:00
|
|
|
|
DesignatedEndIndex.getZExtValue() + 1);
|
|
|
|
|
|
|
|
|
|
// Repeatedly perform subobject initializations in the range
|
|
|
|
|
// [DesignatedStartIndex, DesignatedEndIndex].
|
|
|
|
|
|
|
|
|
|
// Move to the next designator
|
|
|
|
|
unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
|
|
|
|
|
unsigned OldIndex = Index;
|
|
|
|
|
while (DesignatedStartIndex <= DesignatedEndIndex) {
|
|
|
|
|
// Recurse to check later designated subobjects.
|
|
|
|
|
QualType ElementType = AT->getElementType();
|
|
|
|
|
Index = OldIndex;
|
2009-04-15 12:56:10 +08:00
|
|
|
|
if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0,
|
|
|
|
|
Index, StructuredList, ElementIndex,
|
2009-02-05 06:46:25 +08:00
|
|
|
|
(DesignatedStartIndex == DesignatedEndIndex),
|
|
|
|
|
false))
|
2009-01-29 07:36:17 +08:00
|
|
|
|
return true;
|
2009-01-23 07:26:18 +08:00
|
|
|
|
|
2009-01-29 07:36:17 +08:00
|
|
|
|
// Move to the next index in the array that we'll be initializing.
|
|
|
|
|
++DesignatedStartIndex;
|
|
|
|
|
ElementIndex = DesignatedStartIndex.getZExtValue();
|
|
|
|
|
}
|
2009-01-23 07:26:18 +08:00
|
|
|
|
|
|
|
|
|
// If this the first designator, our caller will continue checking
|
|
|
|
|
// the rest of this array subobject.
|
|
|
|
|
if (IsFirstDesignator) {
|
|
|
|
|
if (NextElementIndex)
|
2009-01-29 07:36:17 +08:00
|
|
|
|
*NextElementIndex = DesignatedStartIndex;
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredIndex = ElementIndex;
|
2009-01-23 07:26:18 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-01-29 07:36:17 +08:00
|
|
|
|
if (!FinishSubobjectInit)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-01-23 07:26:18 +08:00
|
|
|
|
// Check the remaining elements within this array subobject.
|
2009-01-22 08:58:24 +08:00
|
|
|
|
bool prevHadError = hadError;
|
2009-02-10 03:45:19 +08:00
|
|
|
|
CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index,
|
2009-01-29 05:54:33 +08:00
|
|
|
|
StructuredList, ElementIndex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
return hadError && !prevHadError;
|
2009-01-22 08:58:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Get the structured initializer list for a subobject of type
|
|
|
|
|
// @p CurrentObjectType.
|
|
|
|
|
InitListExpr *
|
|
|
|
|
InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
|
|
|
|
|
QualType CurrentObjectType,
|
|
|
|
|
InitListExpr *StructuredList,
|
|
|
|
|
unsigned StructuredIndex,
|
|
|
|
|
SourceRange InitRange) {
|
|
|
|
|
Expr *ExistingInit = 0;
|
|
|
|
|
if (!StructuredList)
|
|
|
|
|
ExistingInit = SyntacticToSemantic[IList];
|
|
|
|
|
else if (StructuredIndex < StructuredList->getNumInits())
|
|
|
|
|
ExistingInit = StructuredList->getInit(StructuredIndex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
|
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
|
|
if (ExistingInit) {
|
|
|
|
|
// We are creating an initializer list that initializes the
|
|
|
|
|
// subobjects of the current object, but there was already an
|
|
|
|
|
// initialization that completely initialized the current
|
|
|
|
|
// subobject, e.g., by a compound literal:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
//
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// struct X { int a, b; };
|
|
|
|
|
// struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
//
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Here, xs[0].a == 0 and xs[0].b == 3, since the second,
|
|
|
|
|
// designated initializer re-initializes the whole
|
|
|
|
|
// subobject [0], overwriting previous initializers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(InitRange.getBegin(),
|
2009-03-02 01:12:46 +08:00
|
|
|
|
diag::warn_subobject_initializer_overrides)
|
2009-01-29 05:54:33 +08:00
|
|
|
|
<< InitRange;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
|
2009-01-29 05:54:33 +08:00
|
|
|
|
diag::note_previous_initializer)
|
2009-01-29 07:43:32 +08:00
|
|
|
|
<< /*FIXME:has side effects=*/0
|
2009-01-29 05:54:33 +08:00
|
|
|
|
<< ExistingInit->getSourceRange();
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
InitListExpr *Result
|
|
|
|
|
= new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
|
2009-03-02 01:12:46 +08:00
|
|
|
|
InitRange.getEnd());
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
Result->setType(CurrentObjectType);
|
|
|
|
|
|
2009-03-21 07:58:33 +08:00
|
|
|
|
// Pre-allocate storage for the structured initializer list.
|
|
|
|
|
unsigned NumElements = 0;
|
2009-03-22 02:13:52 +08:00
|
|
|
|
unsigned NumInits = 0;
|
|
|
|
|
if (!StructuredList)
|
|
|
|
|
NumInits = IList->getNumInits();
|
|
|
|
|
else if (Index < IList->getNumInits()) {
|
|
|
|
|
if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
|
|
|
|
|
NumInits = SubList->getNumInits();
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
if (const ArrayType *AType
|
2009-03-21 07:58:33 +08:00
|
|
|
|
= SemaRef.Context.getAsArrayType(CurrentObjectType)) {
|
|
|
|
|
if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
|
|
|
|
|
NumElements = CAType->getSize().getZExtValue();
|
|
|
|
|
// Simple heuristic so that we don't allocate a very large
|
|
|
|
|
// initializer with many empty entries at the end.
|
2009-03-22 02:13:52 +08:00
|
|
|
|
if (NumInits && NumElements > NumInits)
|
2009-03-21 07:58:33 +08:00
|
|
|
|
NumElements = 0;
|
|
|
|
|
}
|
2009-09-22 07:43:11 +08:00
|
|
|
|
} else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
|
2009-03-21 07:58:33 +08:00
|
|
|
|
NumElements = VType->getNumElements();
|
2009-07-30 05:53:49 +08:00
|
|
|
|
else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
|
2009-03-21 07:58:33 +08:00
|
|
|
|
RecordDecl *RDecl = RType->getDecl();
|
|
|
|
|
if (RDecl->isUnion())
|
|
|
|
|
NumElements = 1;
|
|
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
|
NumElements = std::distance(RDecl->field_begin(),
|
2009-06-30 10:36:12 +08:00
|
|
|
|
RDecl->field_end());
|
2009-03-21 07:58:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-22 02:13:52 +08:00
|
|
|
|
if (NumElements < NumInits)
|
2009-03-21 07:58:33 +08:00
|
|
|
|
NumElements = IList->getNumInits();
|
|
|
|
|
|
|
|
|
|
Result->reserveInits(NumElements);
|
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
// Link this new initializer list into the structured initializer
|
|
|
|
|
// lists.
|
|
|
|
|
if (StructuredList)
|
|
|
|
|
StructuredList->updateInit(StructuredIndex, Result);
|
|
|
|
|
else {
|
|
|
|
|
Result->setSyntacticForm(IList);
|
|
|
|
|
SyntacticToSemantic[IList] = Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update the initializer at index @p StructuredIndex within the
|
|
|
|
|
/// structured initializer list to the value @p expr.
|
|
|
|
|
void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
|
|
|
|
|
unsigned &StructuredIndex,
|
|
|
|
|
Expr *expr) {
|
|
|
|
|
// No structured initializer list to update
|
|
|
|
|
if (!StructuredList)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
|
|
|
|
|
// This initializer overwrites a previous initializer. Warn.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(expr->getSourceRange().getBegin(),
|
2009-01-29 05:54:33 +08:00
|
|
|
|
diag::warn_initializer_overrides)
|
|
|
|
|
<< expr->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
|
2009-01-29 05:54:33 +08:00
|
|
|
|
diag::note_previous_initializer)
|
2009-01-29 07:43:32 +08:00
|
|
|
|
<< /*FIXME:has side effects=*/0
|
2009-01-29 05:54:33 +08:00
|
|
|
|
<< PrevInit->getSourceRange();
|
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
2009-01-29 05:54:33 +08:00
|
|
|
|
++StructuredIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
|
/// Check that the given Index expression is a valid array designator
|
|
|
|
|
/// value. This is essentailly just a wrapper around
|
2009-04-26 05:59:05 +08:00
|
|
|
|
/// VerifyIntegerConstantExpression that also checks for negative values
|
2009-01-22 08:58:24 +08:00
|
|
|
|
/// and produces a reasonable diagnostic if there is a
|
|
|
|
|
/// failure. Returns true if there was an error, false otherwise. If
|
|
|
|
|
/// everything went okay, Value will receive the value of the constant
|
|
|
|
|
/// expression.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
static bool
|
2009-04-26 05:59:05 +08:00
|
|
|
|
CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
|
2009-01-22 08:58:24 +08:00
|
|
|
|
SourceLocation Loc = Index->getSourceRange().getBegin();
|
|
|
|
|
|
|
|
|
|
// Make sure this is an integer constant expression.
|
2009-04-26 05:59:05 +08:00
|
|
|
|
if (S.VerifyIntegerConstantExpression(Index, &Value))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (Value.isSigned() && Value.isNegative())
|
|
|
|
|
return S.Diag(Loc, diag::err_array_designator_negative)
|
2009-01-22 08:58:24 +08:00
|
|
|
|
<< Value.toString(10) << Index->getSourceRange();
|
|
|
|
|
|
2009-01-24 05:04:18 +08:00
|
|
|
|
Value.setIsUnsigned(true);
|
2009-01-22 08:58:24 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
|
|
|
|
|
SourceLocation Loc,
|
2009-03-28 08:41:23 +08:00
|
|
|
|
bool GNUSyntax,
|
2009-01-22 08:58:24 +08:00
|
|
|
|
OwningExprResult Init) {
|
|
|
|
|
typedef DesignatedInitExpr::Designator ASTDesignator;
|
|
|
|
|
|
|
|
|
|
bool Invalid = false;
|
|
|
|
|
llvm::SmallVector<ASTDesignator, 32> Designators;
|
|
|
|
|
llvm::SmallVector<Expr *, 32> InitExpressions;
|
|
|
|
|
|
|
|
|
|
// Build designators and check array designator expressions.
|
|
|
|
|
for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
|
|
|
|
|
const Designator &D = Desig.getDesignator(Idx);
|
|
|
|
|
switch (D.getKind()) {
|
|
|
|
|
case Designator::FieldDesignator:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
|
2009-01-22 08:58:24 +08:00
|
|
|
|
D.getFieldLoc()));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Designator::ArrayDesignator: {
|
|
|
|
|
Expr *Index = static_cast<Expr *>(D.getArrayIndex());
|
|
|
|
|
llvm::APSInt IndexValue;
|
2009-05-22 07:17:49 +08:00
|
|
|
|
if (!Index->isTypeDependent() &&
|
|
|
|
|
!Index->isValueDependent() &&
|
|
|
|
|
CheckArrayDesignatorExpr(*this, Index, IndexValue))
|
2009-01-22 08:58:24 +08:00
|
|
|
|
Invalid = true;
|
|
|
|
|
else {
|
|
|
|
|
Designators.push_back(ASTDesignator(InitExpressions.size(),
|
2009-09-09 23:08:12 +08:00
|
|
|
|
D.getLBracketLoc(),
|
2009-01-22 08:58:24 +08:00
|
|
|
|
D.getRBracketLoc()));
|
|
|
|
|
InitExpressions.push_back(Index);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Designator::ArrayRangeDesignator: {
|
|
|
|
|
Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
|
|
|
|
|
Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
|
|
|
|
|
llvm::APSInt StartValue;
|
|
|
|
|
llvm::APSInt EndValue;
|
2009-05-22 07:17:49 +08:00
|
|
|
|
bool StartDependent = StartIndex->isTypeDependent() ||
|
|
|
|
|
StartIndex->isValueDependent();
|
|
|
|
|
bool EndDependent = EndIndex->isTypeDependent() ||
|
|
|
|
|
EndIndex->isValueDependent();
|
|
|
|
|
if ((!StartDependent &&
|
|
|
|
|
CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
|
|
|
|
|
(!EndDependent &&
|
|
|
|
|
CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
|
2009-01-22 08:58:24 +08:00
|
|
|
|
Invalid = true;
|
2009-01-24 06:22:29 +08:00
|
|
|
|
else {
|
|
|
|
|
// Make sure we're comparing values with the same bit width.
|
2009-05-22 07:17:49 +08:00
|
|
|
|
if (StartDependent || EndDependent) {
|
|
|
|
|
// Nothing to compute.
|
|
|
|
|
} else if (StartValue.getBitWidth() > EndValue.getBitWidth())
|
2009-01-24 06:22:29 +08:00
|
|
|
|
EndValue.extend(StartValue.getBitWidth());
|
|
|
|
|
else if (StartValue.getBitWidth() < EndValue.getBitWidth())
|
|
|
|
|
StartValue.extend(EndValue.getBitWidth());
|
|
|
|
|
|
2009-05-22 07:30:39 +08:00
|
|
|
|
if (!StartDependent && !EndDependent && EndValue < StartValue) {
|
2009-01-24 06:22:29 +08:00
|
|
|
|
Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
|
2009-09-09 23:08:12 +08:00
|
|
|
|
<< StartValue.toString(10) << EndValue.toString(10)
|
2009-01-24 06:22:29 +08:00
|
|
|
|
<< StartIndex->getSourceRange() << EndIndex->getSourceRange();
|
|
|
|
|
Invalid = true;
|
|
|
|
|
} else {
|
|
|
|
|
Designators.push_back(ASTDesignator(InitExpressions.size(),
|
2009-09-09 23:08:12 +08:00
|
|
|
|
D.getLBracketLoc(),
|
2009-01-24 06:22:29 +08:00
|
|
|
|
D.getEllipsisLoc(),
|
|
|
|
|
D.getRBracketLoc()));
|
|
|
|
|
InitExpressions.push_back(StartIndex);
|
|
|
|
|
InitExpressions.push_back(EndIndex);
|
|
|
|
|
}
|
2009-01-22 08:58:24 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Invalid || Init.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
|
|
// Clear out the expressions within the designation.
|
|
|
|
|
Desig.ClearExprs(*this);
|
|
|
|
|
|
|
|
|
|
DesignatedInitExpr *DIE
|
2009-05-21 17:52:38 +08:00
|
|
|
|
= DesignatedInitExpr::Create(Context,
|
|
|
|
|
Designators.data(), Designators.size(),
|
|
|
|
|
InitExpressions.data(), InitExpressions.size(),
|
2009-05-02 03:49:17 +08:00
|
|
|
|
Loc, GNUSyntax, Init.takeAs<Expr>());
|
2009-01-22 08:58:24 +08:00
|
|
|
|
return Owned(DIE);
|
|
|
|
|
}
|
2009-01-29 08:45:39 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
bool Sema::CheckInitList(const InitializedEntity &Entity,
|
|
|
|
|
InitListExpr *&InitList, QualType &DeclType) {
|
|
|
|
|
InitListChecker CheckInitList(*this, Entity, InitList, DeclType);
|
2009-01-29 08:45:39 +08:00
|
|
|
|
if (!CheckInitList.HadError())
|
|
|
|
|
InitList = CheckInitList.getFullyStructuredList();
|
|
|
|
|
|
|
|
|
|
return CheckInitList.HadError();
|
|
|
|
|
}
|
2009-02-03 01:43:21 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Initialization entity
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-10 07:08:42 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
|
|
|
|
|
const InitializedEntity &Parent)
|
|
|
|
|
: Kind(EK_ArrayOrVectorElement), Parent(&Parent), Index(Index)
|
|
|
|
|
{
|
|
|
|
|
if (isa<ArrayType>(Parent.TL.getType())) {
|
|
|
|
|
TL = cast<ArrayTypeLoc>(Parent.TL).getElementLoc();
|
|
|
|
|
return;
|
2009-02-03 01:43:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
// FIXME: should be able to get type location information for vectors, too.
|
2009-02-03 01:43:21 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
QualType T;
|
|
|
|
|
if (const ArrayType *AT = Context.getAsArrayType(Parent.TL.getType()))
|
|
|
|
|
T = AT->getElementType();
|
|
|
|
|
else
|
|
|
|
|
T = Parent.TL.getType()->getAs<VectorType>()->getElementType();
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
2009-12-16 14:35:08 +08:00
|
|
|
|
// FIXME: Once we've gone through the effort to create the fake
|
|
|
|
|
// TypeSourceInfo, should we cache it somewhere? (If not, we "leak" it).
|
|
|
|
|
TypeSourceInfo *DI = Context.CreateTypeSourceInfo(T);
|
|
|
|
|
DI->getTypeLoc().initialize(Parent.TL.getSourceRange().getBegin());
|
|
|
|
|
TL = DI->getTypeLoc();
|
|
|
|
|
}
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
|
|
|
|
void InitializedEntity::InitDeclLoc() {
|
|
|
|
|
assert((Kind == EK_Variable || Kind == EK_Parameter || Kind == EK_Member) &&
|
|
|
|
|
"InitDeclLoc cannot be used with non-declaration entities.");
|
|
|
|
|
|
|
|
|
|
if (TypeSourceInfo *DI = VariableOrMember->getTypeSourceInfo()) {
|
|
|
|
|
TL = DI->getTypeLoc();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: Once we've gone through the effort to create the fake
|
|
|
|
|
// TypeSourceInfo, should we cache it in the declaration?
|
|
|
|
|
// (If not, we "leak" it).
|
|
|
|
|
TypeSourceInfo *DI = VariableOrMember->getASTContext()
|
|
|
|
|
.CreateTypeSourceInfo(VariableOrMember->getType());
|
|
|
|
|
DI->getTypeLoc().initialize(VariableOrMember->getLocation());
|
|
|
|
|
TL = DI->getTypeLoc();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
|
|
|
|
|
CXXBaseSpecifier *Base)
|
|
|
|
|
{
|
|
|
|
|
InitializedEntity Result;
|
|
|
|
|
Result.Kind = EK_Base;
|
|
|
|
|
Result.Base = Base;
|
|
|
|
|
// FIXME: CXXBaseSpecifier should store a TypeLoc.
|
|
|
|
|
TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Base->getType());
|
|
|
|
|
DI->getTypeLoc().initialize(Base->getSourceRange().getBegin());
|
|
|
|
|
Result.TL = DI->getTypeLoc();
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 09:38:02 +08:00
|
|
|
|
DeclarationName InitializedEntity::getName() const {
|
|
|
|
|
switch (getKind()) {
|
|
|
|
|
case EK_Variable:
|
|
|
|
|
case EK_Parameter:
|
|
|
|
|
case EK_Member:
|
|
|
|
|
return VariableOrMember->getDeclName();
|
|
|
|
|
|
|
|
|
|
case EK_Result:
|
|
|
|
|
case EK_Exception:
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
case EK_New:
|
2009-12-16 09:38:02 +08:00
|
|
|
|
case EK_Temporary:
|
|
|
|
|
case EK_Base:
|
2009-12-16 14:35:08 +08:00
|
|
|
|
case EK_ArrayOrVectorElement:
|
2009-12-16 09:38:02 +08:00
|
|
|
|
return DeclarationName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Silence GCC warning
|
|
|
|
|
return DeclarationName();
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-19 11:01:41 +08:00
|
|
|
|
DeclaratorDecl *InitializedEntity::getDecl() const {
|
|
|
|
|
switch (getKind()) {
|
|
|
|
|
case EK_Variable:
|
|
|
|
|
case EK_Parameter:
|
|
|
|
|
case EK_Member:
|
|
|
|
|
return VariableOrMember;
|
|
|
|
|
|
|
|
|
|
case EK_Result:
|
|
|
|
|
case EK_Exception:
|
|
|
|
|
case EK_New:
|
|
|
|
|
case EK_Temporary:
|
|
|
|
|
case EK_Base:
|
|
|
|
|
case EK_ArrayOrVectorElement:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Silence GCC warning
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Initialization sequence
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
void InitializationSequence::Step::Destroy() {
|
|
|
|
|
switch (Kind) {
|
|
|
|
|
case SK_ResolveAddressOfOverloadedFunction:
|
|
|
|
|
case SK_CastDerivedToBaseRValue:
|
|
|
|
|
case SK_CastDerivedToBaseLValue:
|
|
|
|
|
case SK_BindReference:
|
|
|
|
|
case SK_BindReferenceToTemporary:
|
|
|
|
|
case SK_UserConversion:
|
|
|
|
|
case SK_QualificationConversionRValue:
|
|
|
|
|
case SK_QualificationConversionLValue:
|
2009-12-11 01:56:55 +08:00
|
|
|
|
case SK_ListInitialization:
|
2009-12-15 04:49:26 +08:00
|
|
|
|
case SK_ConstructorInitialization:
|
2009-12-15 08:01:57 +08:00
|
|
|
|
case SK_ZeroInitialization:
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
case SK_CAssignment:
|
2009-12-19 16:11:05 +08:00
|
|
|
|
case SK_StringInit:
|
2009-12-10 07:02:17 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SK_ConversionSequence:
|
|
|
|
|
delete ICS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitializationSequence::AddAddressOverloadResolutionStep(
|
|
|
|
|
FunctionDecl *Function) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_ResolveAddressOfOverloadedFunction;
|
|
|
|
|
S.Type = Function->getType();
|
|
|
|
|
S.Function = Function;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
|
|
|
|
|
bool IsLValue) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = IsLValue? SK_CastDerivedToBaseLValue : SK_CastDerivedToBaseRValue;
|
|
|
|
|
S.Type = BaseType;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitializationSequence::AddReferenceBindingStep(QualType T,
|
|
|
|
|
bool BindingTemporary) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 10:42:07 +08:00
|
|
|
|
void InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
|
|
|
|
|
QualType T) {
|
2009-12-10 07:02:17 +08:00
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_UserConversion;
|
2009-12-11 10:42:07 +08:00
|
|
|
|
S.Type = T;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
S.Function = Function;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitializationSequence::AddQualificationConversionStep(QualType Ty,
|
|
|
|
|
bool IsLValue) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = IsLValue? SK_QualificationConversionLValue
|
|
|
|
|
: SK_QualificationConversionRValue;
|
|
|
|
|
S.Type = Ty;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitializationSequence::AddConversionSequenceStep(
|
|
|
|
|
const ImplicitConversionSequence &ICS,
|
|
|
|
|
QualType T) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_ConversionSequence;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
S.ICS = new ImplicitConversionSequence(ICS);
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 01:56:55 +08:00
|
|
|
|
void InitializationSequence::AddListInitializationStep(QualType T) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_ListInitialization;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-15 04:49:26 +08:00
|
|
|
|
void
|
|
|
|
|
InitializationSequence::AddConstructorInitializationStep(
|
|
|
|
|
CXXConstructorDecl *Constructor,
|
|
|
|
|
QualType T) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_ConstructorInitialization;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
S.Function = Constructor;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-15 08:01:57 +08:00
|
|
|
|
void InitializationSequence::AddZeroInitializationStep(QualType T) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_ZeroInitialization;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
void InitializationSequence::AddCAssignmentStep(QualType T) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_CAssignment;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-19 16:11:05 +08:00
|
|
|
|
void InitializationSequence::AddStringInitStep(QualType T) {
|
|
|
|
|
Step S;
|
|
|
|
|
S.Kind = SK_StringInit;
|
|
|
|
|
S.Type = T;
|
|
|
|
|
Steps.push_back(S);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
void InitializationSequence::SetOverloadFailure(FailureKind Failure,
|
|
|
|
|
OverloadingResult Result) {
|
|
|
|
|
SequenceKind = FailedSequence;
|
|
|
|
|
this->Failure = Failure;
|
|
|
|
|
this->FailedOverloadResult = Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Attempt initialization
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
/// \brief Attempt list initialization (C++0x [dcl.init.list])
|
2009-12-11 01:56:55 +08:00
|
|
|
|
static void TryListInitialization(Sema &S,
|
2009-12-10 07:02:17 +08:00
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
InitListExpr *InitList,
|
|
|
|
|
InitializationSequence &Sequence) {
|
2009-12-11 01:56:55 +08:00
|
|
|
|
// FIXME: We only perform rudimentary checking of list
|
|
|
|
|
// initializations at this point, then assume that any list
|
|
|
|
|
// initialization of an array, aggregate, or scalar will be
|
|
|
|
|
// well-formed. We we actually "perform" list initialization, we'll
|
|
|
|
|
// do all of the necessary checking. C++0x initializer lists will
|
|
|
|
|
// force us to perform more checking here.
|
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::ListInitialization);
|
|
|
|
|
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
|
|
|
|
|
// C++ [dcl.init]p13:
|
|
|
|
|
// If T is a scalar type, then a declaration of the form
|
|
|
|
|
//
|
|
|
|
|
// T x = { a };
|
|
|
|
|
//
|
|
|
|
|
// is equivalent to
|
|
|
|
|
//
|
|
|
|
|
// T x = a;
|
|
|
|
|
if (DestType->isScalarType()) {
|
|
|
|
|
if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) {
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assume scalar initialization from a single value works.
|
|
|
|
|
} else if (DestType->isAggregateType()) {
|
|
|
|
|
// Assume aggregate initialization works.
|
|
|
|
|
} else if (DestType->isVectorType()) {
|
|
|
|
|
// Assume vector initialization works.
|
|
|
|
|
} else if (DestType->isReferenceType()) {
|
|
|
|
|
// FIXME: C++0x defines behavior for this.
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
|
|
|
|
|
return;
|
|
|
|
|
} else if (DestType->isRecordType()) {
|
|
|
|
|
// FIXME: C++0x defines behavior for this
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a general "list initialization" step.
|
|
|
|
|
Sequence.AddListInitializationStep(DestType);
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Try a reference initialization that involves calling a conversion
|
|
|
|
|
/// function.
|
|
|
|
|
///
|
|
|
|
|
/// FIXME: look intos DRs 656, 896
|
|
|
|
|
static OverloadingResult TryRefInitWithConversionFunction(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr *Initializer,
|
|
|
|
|
bool AllowRValues,
|
|
|
|
|
InitializationSequence &Sequence) {
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
|
|
|
|
|
QualType T1 = cv1T1.getUnqualifiedType();
|
|
|
|
|
QualType cv2T2 = Initializer->getType();
|
|
|
|
|
QualType T2 = cv2T2.getUnqualifiedType();
|
|
|
|
|
|
|
|
|
|
bool DerivedToBase;
|
|
|
|
|
assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
|
|
|
|
|
T1, T2, DerivedToBase) &&
|
|
|
|
|
"Must have incompatible references when binding via conversion");
|
2009-12-13 09:37:04 +08:00
|
|
|
|
(void)DerivedToBase;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
|
|
|
|
// Build the candidate set directly in the initialization sequence
|
|
|
|
|
// structure, so that it will persist if we fail.
|
|
|
|
|
OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
|
|
|
|
|
CandidateSet.clear();
|
|
|
|
|
|
|
|
|
|
// Determine whether we are allowed to call explicit constructors or
|
|
|
|
|
// explicit conversion operators.
|
|
|
|
|
bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
|
|
|
|
|
|
|
|
|
|
const RecordType *T1RecordType = 0;
|
|
|
|
|
if (AllowRValues && (T1RecordType = T1->getAs<RecordType>())) {
|
|
|
|
|
// The type we're converting to is a class type. Enumerate its constructors
|
|
|
|
|
// to see if there is a suitable conversion.
|
|
|
|
|
CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
|
|
|
|
|
|
|
|
|
|
DeclarationName ConstructorName
|
|
|
|
|
= S.Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
|
S.Context.getCanonicalType(T1).getUnqualifiedType());
|
|
|
|
|
DeclContext::lookup_iterator Con, ConEnd;
|
|
|
|
|
for (llvm::tie(Con, ConEnd) = T1RecordDecl->lookup(ConstructorName);
|
|
|
|
|
Con != ConEnd; ++Con) {
|
|
|
|
|
// Find the constructor (which may be a template).
|
|
|
|
|
CXXConstructorDecl *Constructor = 0;
|
|
|
|
|
FunctionTemplateDecl *ConstructorTmpl
|
|
|
|
|
= dyn_cast<FunctionTemplateDecl>(*Con);
|
|
|
|
|
if (ConstructorTmpl)
|
|
|
|
|
Constructor = cast<CXXConstructorDecl>(
|
|
|
|
|
ConstructorTmpl->getTemplatedDecl());
|
|
|
|
|
else
|
|
|
|
|
Constructor = cast<CXXConstructorDecl>(*Con);
|
|
|
|
|
|
|
|
|
|
if (!Constructor->isInvalidDecl() &&
|
|
|
|
|
Constructor->isConvertingConstructor(AllowExplicit)) {
|
|
|
|
|
if (ConstructorTmpl)
|
|
|
|
|
S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0,
|
|
|
|
|
&Initializer, 1, CandidateSet);
|
|
|
|
|
else
|
|
|
|
|
S.AddOverloadCandidate(Constructor, &Initializer, 1, CandidateSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (const RecordType *T2RecordType = T2->getAs<RecordType>()) {
|
|
|
|
|
// The type we're converting from is a class type, enumerate its conversion
|
|
|
|
|
// functions.
|
|
|
|
|
CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
|
|
|
|
|
|
|
|
|
|
// Determine the type we are converting to. If we are allowed to
|
|
|
|
|
// convert to an rvalue, take the type that the destination type
|
|
|
|
|
// refers to.
|
|
|
|
|
QualType ToType = AllowRValues? cv1T1 : DestType;
|
|
|
|
|
|
|
|
|
|
const UnresolvedSet *Conversions
|
|
|
|
|
= T2RecordDecl->getVisibleConversionFunctions();
|
|
|
|
|
for (UnresolvedSet::iterator I = Conversions->begin(),
|
|
|
|
|
E = Conversions->end();
|
|
|
|
|
I != E; ++I) {
|
|
|
|
|
NamedDecl *D = *I;
|
|
|
|
|
CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
|
|
|
|
|
if (isa<UsingShadowDecl>(D))
|
|
|
|
|
D = cast<UsingShadowDecl>(D)->getTargetDecl();
|
|
|
|
|
|
|
|
|
|
FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
|
|
|
|
|
CXXConversionDecl *Conv;
|
|
|
|
|
if (ConvTemplate)
|
|
|
|
|
Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
|
|
|
|
|
else
|
|
|
|
|
Conv = cast<CXXConversionDecl>(*I);
|
|
|
|
|
|
|
|
|
|
// If the conversion function doesn't return a reference type,
|
|
|
|
|
// it can't be considered for this conversion unless we're allowed to
|
|
|
|
|
// consider rvalues.
|
|
|
|
|
// FIXME: Do we need to make sure that we only consider conversion
|
|
|
|
|
// candidates with reference-compatible results? That might be needed to
|
|
|
|
|
// break recursion.
|
|
|
|
|
if ((AllowExplicit || !Conv->isExplicit()) &&
|
|
|
|
|
(AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
|
|
|
|
|
if (ConvTemplate)
|
|
|
|
|
S.AddTemplateConversionCandidate(ConvTemplate, ActingDC, Initializer,
|
|
|
|
|
ToType, CandidateSet);
|
|
|
|
|
else
|
|
|
|
|
S.AddConversionCandidate(Conv, ActingDC, Initializer, cv1T1,
|
|
|
|
|
CandidateSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SourceLocation DeclLoc = Initializer->getLocStart();
|
|
|
|
|
|
|
|
|
|
// Perform overload resolution. If it fails, return the failed result.
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
|
if (OverloadingResult Result
|
|
|
|
|
= S.BestViableFunction(CandidateSet, DeclLoc, Best))
|
|
|
|
|
return Result;
|
2009-12-11 10:42:07 +08:00
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
FunctionDecl *Function = Best->Function;
|
2009-12-11 10:42:07 +08:00
|
|
|
|
|
|
|
|
|
// Compute the returned type of the conversion.
|
2009-12-10 07:02:17 +08:00
|
|
|
|
if (isa<CXXConversionDecl>(Function))
|
|
|
|
|
T2 = Function->getResultType();
|
|
|
|
|
else
|
|
|
|
|
T2 = cv1T1;
|
2009-12-11 10:42:07 +08:00
|
|
|
|
|
|
|
|
|
// Add the user-defined conversion step.
|
|
|
|
|
Sequence.AddUserConversionStep(Function, T2.getNonReferenceType());
|
|
|
|
|
|
|
|
|
|
// Determine whether we need to perform derived-to-base or
|
|
|
|
|
// cv-qualification adjustments.
|
2009-12-10 07:02:17 +08:00
|
|
|
|
bool NewDerivedToBase = false;
|
|
|
|
|
Sema::ReferenceCompareResult NewRefRelationship
|
|
|
|
|
= S.CompareReferenceRelationship(DeclLoc, T1, T2.getNonReferenceType(),
|
|
|
|
|
NewDerivedToBase);
|
|
|
|
|
assert(NewRefRelationship != Sema::Ref_Incompatible &&
|
|
|
|
|
"Overload resolution picked a bad conversion function");
|
|
|
|
|
(void)NewRefRelationship;
|
|
|
|
|
if (NewDerivedToBase)
|
|
|
|
|
Sequence.AddDerivedToBaseCastStep(
|
|
|
|
|
S.Context.getQualifiedType(T1,
|
|
|
|
|
T2.getNonReferenceType().getQualifiers()),
|
|
|
|
|
/*isLValue=*/true);
|
|
|
|
|
|
|
|
|
|
if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
|
|
|
|
|
Sequence.AddQualificationConversionStep(cv1T1, T2->isReferenceType());
|
|
|
|
|
|
|
|
|
|
Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
|
|
|
|
|
return OR_Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Attempt reference initialization (C++0x [dcl.init.list])
|
|
|
|
|
static void TryReferenceInitialization(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr *Initializer,
|
|
|
|
|
InitializationSequence &Sequence) {
|
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::ReferenceBinding);
|
|
|
|
|
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
|
|
|
|
|
QualType T1 = cv1T1.getUnqualifiedType();
|
|
|
|
|
QualType cv2T2 = Initializer->getType();
|
|
|
|
|
QualType T2 = cv2T2.getUnqualifiedType();
|
|
|
|
|
SourceLocation DeclLoc = Initializer->getLocStart();
|
|
|
|
|
|
|
|
|
|
// If the initializer is the address of an overloaded function, try
|
|
|
|
|
// to resolve the overloaded function. If all goes well, T2 is the
|
|
|
|
|
// type of the resulting function.
|
|
|
|
|
if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) {
|
|
|
|
|
FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer,
|
|
|
|
|
T1,
|
|
|
|
|
false);
|
|
|
|
|
if (!Fn) {
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sequence.AddAddressOverloadResolutionStep(Fn);
|
|
|
|
|
cv2T2 = Fn->getType();
|
|
|
|
|
T2 = cv2T2.getUnqualifiedType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: Rvalue references
|
|
|
|
|
bool ForceRValue = false;
|
|
|
|
|
|
|
|
|
|
// Compute some basic properties of the types and the initializer.
|
|
|
|
|
bool isLValueRef = DestType->isLValueReferenceType();
|
|
|
|
|
bool isRValueRef = !isLValueRef;
|
|
|
|
|
bool DerivedToBase = false;
|
|
|
|
|
Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression :
|
|
|
|
|
Initializer->isLvalue(S.Context);
|
|
|
|
|
Sema::ReferenceCompareResult RefRelationship
|
|
|
|
|
= S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase);
|
|
|
|
|
|
|
|
|
|
// C++0x [dcl.init.ref]p5:
|
|
|
|
|
// A reference to type "cv1 T1" is initialized by an expression of type
|
|
|
|
|
// "cv2 T2" as follows:
|
|
|
|
|
//
|
|
|
|
|
// - If the reference is an lvalue reference and the initializer
|
|
|
|
|
// expression
|
|
|
|
|
OverloadingResult ConvOvlResult = OR_Success;
|
|
|
|
|
if (isLValueRef) {
|
|
|
|
|
if (InitLvalue == Expr::LV_Valid &&
|
|
|
|
|
RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
|
|
|
|
|
// - is an lvalue (but is not a bit-field), and "cv1 T1" is
|
|
|
|
|
// reference-compatible with "cv2 T2," or
|
|
|
|
|
//
|
|
|
|
|
// Per C++ [over.best.ics]p2, we ignore whether the lvalue is a
|
|
|
|
|
// bit-field when we're determining whether the reference initialization
|
|
|
|
|
// can occur. This property will be checked by PerformInitialization.
|
|
|
|
|
if (DerivedToBase)
|
|
|
|
|
Sequence.AddDerivedToBaseCastStep(
|
|
|
|
|
S.Context.getQualifiedType(T1, cv2T2.getQualifiers()),
|
|
|
|
|
/*isLValue=*/true);
|
|
|
|
|
if (cv1T1.getQualifiers() != cv2T2.getQualifiers())
|
|
|
|
|
Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/true);
|
|
|
|
|
Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - has a class type (i.e., T2 is a class type), where T1 is not
|
|
|
|
|
// reference-related to T2, and can be implicitly converted to an
|
|
|
|
|
// lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
|
|
|
|
|
// with "cv3 T3" (this conversion is selected by enumerating the
|
|
|
|
|
// applicable conversion functions (13.3.1.6) and choosing the best
|
|
|
|
|
// one through overload resolution (13.3)),
|
|
|
|
|
if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType()) {
|
|
|
|
|
ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
|
|
|
|
|
Initializer,
|
|
|
|
|
/*AllowRValues=*/false,
|
|
|
|
|
Sequence);
|
|
|
|
|
if (ConvOvlResult == OR_Success)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - Otherwise, the reference shall be an lvalue reference to a
|
|
|
|
|
// non-volatile const type (i.e., cv1 shall be const), or the reference
|
|
|
|
|
// shall be an rvalue reference and the initializer expression shall
|
|
|
|
|
// be an rvalue.
|
|
|
|
|
if (!((isLValueRef && cv1T1.getCVRQualifiers() == Qualifiers::Const) ||
|
|
|
|
|
(isRValueRef && InitLvalue != Expr::LV_Valid))) {
|
|
|
|
|
if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
|
|
|
|
|
Sequence.SetOverloadFailure(
|
|
|
|
|
InitializationSequence::FK_ReferenceInitOverloadFailed,
|
|
|
|
|
ConvOvlResult);
|
|
|
|
|
else if (isLValueRef)
|
|
|
|
|
Sequence.SetFailed(InitLvalue == Expr::LV_Valid
|
|
|
|
|
? (RefRelationship == Sema::Ref_Related
|
|
|
|
|
? InitializationSequence::FK_ReferenceInitDropsQualifiers
|
|
|
|
|
: InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
|
|
|
|
|
: InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
|
|
|
|
|
else
|
|
|
|
|
Sequence.SetFailed(
|
|
|
|
|
InitializationSequence::FK_RValueReferenceBindingToLValue);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - If T1 and T2 are class types and
|
|
|
|
|
if (T1->isRecordType() && T2->isRecordType()) {
|
|
|
|
|
// - the initializer expression is an rvalue and "cv1 T1" is
|
|
|
|
|
// reference-compatible with "cv2 T2", or
|
|
|
|
|
if (InitLvalue != Expr::LV_Valid &&
|
|
|
|
|
RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
|
|
|
|
|
if (DerivedToBase)
|
|
|
|
|
Sequence.AddDerivedToBaseCastStep(
|
|
|
|
|
S.Context.getQualifiedType(T1, cv2T2.getQualifiers()),
|
|
|
|
|
/*isLValue=*/false);
|
|
|
|
|
if (cv1T1.getQualifiers() != cv2T2.getQualifiers())
|
|
|
|
|
Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/false);
|
|
|
|
|
Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - T1 is not reference-related to T2 and the initializer expression
|
|
|
|
|
// can be implicitly converted to an rvalue of type "cv3 T3" (this
|
|
|
|
|
// conversion is selected by enumerating the applicable conversion
|
|
|
|
|
// functions (13.3.1.6) and choosing the best one through overload
|
|
|
|
|
// resolution (13.3)),
|
|
|
|
|
if (RefRelationship == Sema::Ref_Incompatible) {
|
|
|
|
|
ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
|
|
|
|
|
Kind, Initializer,
|
|
|
|
|
/*AllowRValues=*/true,
|
|
|
|
|
Sequence);
|
|
|
|
|
if (ConvOvlResult)
|
|
|
|
|
Sequence.SetOverloadFailure(
|
|
|
|
|
InitializationSequence::FK_ReferenceInitOverloadFailed,
|
|
|
|
|
ConvOvlResult);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - If the initializer expression is an rvalue, with T2 an array type,
|
|
|
|
|
// and "cv1 T1" is reference-compatible with "cv2 T2," the reference
|
|
|
|
|
// is bound to the object represented by the rvalue (see 3.10).
|
|
|
|
|
// FIXME: How can an array type be reference-compatible with anything?
|
|
|
|
|
// Don't we mean the element types of T1 and T2?
|
|
|
|
|
|
|
|
|
|
// - Otherwise, a temporary of type “cv1 T1” is created and initialized
|
|
|
|
|
// from the initializer expression using the rules for a non-reference
|
|
|
|
|
// copy initialization (8.5). The reference is then bound to the
|
|
|
|
|
// temporary. [...]
|
|
|
|
|
// Determine whether we are allowed to call explicit constructors or
|
|
|
|
|
// explicit conversion operators.
|
|
|
|
|
bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct);
|
|
|
|
|
ImplicitConversionSequence ICS
|
|
|
|
|
= S.TryImplicitConversion(Initializer, cv1T1,
|
|
|
|
|
/*SuppressUserConversions=*/false, AllowExplicit,
|
|
|
|
|
/*ForceRValue=*/false,
|
|
|
|
|
/*FIXME:InOverloadResolution=*/false,
|
|
|
|
|
/*UserCast=*/Kind.isExplicitCast());
|
|
|
|
|
|
|
|
|
|
if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) {
|
|
|
|
|
// FIXME: Use the conversion function set stored in ICS to turn
|
|
|
|
|
// this into an overloading ambiguity diagnostic. However, we need
|
|
|
|
|
// to keep that set as an OverloadCandidateSet rather than as some
|
|
|
|
|
// other kind of set.
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
|
|
|
|
|
Sequence.SetOverloadFailure(
|
|
|
|
|
InitializationSequence::FK_ReferenceInitOverloadFailed,
|
|
|
|
|
ConvOvlResult);
|
|
|
|
|
else
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
|
2009-12-10 07:02:17 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [...] If T1 is reference-related to T2, cv1 must be the
|
|
|
|
|
// same cv-qualification as, or greater cv-qualification
|
|
|
|
|
// than, cv2; otherwise, the program is ill-formed.
|
|
|
|
|
if (RefRelationship == Sema::Ref_Related &&
|
|
|
|
|
!cv1T1.isAtLeastAsQualifiedAs(cv2T2)) {
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Perform the actual conversion.
|
|
|
|
|
Sequence.AddConversionSequenceStep(ICS, cv1T1);
|
|
|
|
|
Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Attempt character array initialization from a string literal
|
|
|
|
|
/// (C++ [dcl.init.string], C99 6.7.8).
|
|
|
|
|
static void TryStringLiteralInitialization(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr *Initializer,
|
|
|
|
|
InitializationSequence &Sequence) {
|
2009-12-19 16:11:05 +08:00
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::StringInit);
|
|
|
|
|
Sequence.AddStringInitStep(Entity.getType().getType());
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Attempt initialization by constructor (C++ [dcl.init]), which
|
|
|
|
|
/// enumerates the constructors of the initialized entity and performs overload
|
|
|
|
|
/// resolution to select the best.
|
|
|
|
|
static void TryConstructorInitialization(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr **Args, unsigned NumArgs,
|
2009-12-15 08:01:57 +08:00
|
|
|
|
QualType DestType,
|
2009-12-10 07:02:17 +08:00
|
|
|
|
InitializationSequence &Sequence) {
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
if (Kind.getKind() == InitializationKind::IK_Copy)
|
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
|
|
|
|
|
else
|
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization);
|
2009-12-15 04:49:26 +08:00
|
|
|
|
|
|
|
|
|
// Build the candidate set directly in the initialization sequence
|
|
|
|
|
// structure, so that it will persist if we fail.
|
|
|
|
|
OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
|
|
|
|
|
CandidateSet.clear();
|
|
|
|
|
|
|
|
|
|
// Determine whether we are allowed to call explicit constructors or
|
|
|
|
|
// explicit conversion operators.
|
|
|
|
|
bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct ||
|
|
|
|
|
Kind.getKind() == InitializationKind::IK_Value ||
|
|
|
|
|
Kind.getKind() == InitializationKind::IK_Default);
|
|
|
|
|
|
|
|
|
|
// The type we're converting to is a class type. Enumerate its constructors
|
|
|
|
|
// to see if one is suitable.
|
|
|
|
|
const RecordType *DestRecordType = DestType->getAs<RecordType>();
|
|
|
|
|
assert(DestRecordType && "Constructor initialization requires record type");
|
|
|
|
|
CXXRecordDecl *DestRecordDecl
|
|
|
|
|
= cast<CXXRecordDecl>(DestRecordType->getDecl());
|
|
|
|
|
|
|
|
|
|
DeclarationName ConstructorName
|
|
|
|
|
= S.Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
|
S.Context.getCanonicalType(DestType).getUnqualifiedType());
|
|
|
|
|
DeclContext::lookup_iterator Con, ConEnd;
|
|
|
|
|
for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName);
|
|
|
|
|
Con != ConEnd; ++Con) {
|
|
|
|
|
// Find the constructor (which may be a template).
|
|
|
|
|
CXXConstructorDecl *Constructor = 0;
|
|
|
|
|
FunctionTemplateDecl *ConstructorTmpl
|
|
|
|
|
= dyn_cast<FunctionTemplateDecl>(*Con);
|
|
|
|
|
if (ConstructorTmpl)
|
|
|
|
|
Constructor = cast<CXXConstructorDecl>(
|
|
|
|
|
ConstructorTmpl->getTemplatedDecl());
|
|
|
|
|
else
|
|
|
|
|
Constructor = cast<CXXConstructorDecl>(*Con);
|
|
|
|
|
|
|
|
|
|
if (!Constructor->isInvalidDecl() &&
|
2009-12-16 09:38:02 +08:00
|
|
|
|
(AllowExplicit || !Constructor->isExplicit())) {
|
2009-12-15 04:49:26 +08:00
|
|
|
|
if (ConstructorTmpl)
|
|
|
|
|
S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0,
|
|
|
|
|
Args, NumArgs, CandidateSet);
|
|
|
|
|
else
|
|
|
|
|
S.AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SourceLocation DeclLoc = Kind.getLocation();
|
|
|
|
|
|
|
|
|
|
// Perform overload resolution. If it fails, return the failed result.
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
|
if (OverloadingResult Result
|
|
|
|
|
= S.BestViableFunction(CandidateSet, DeclLoc, Best)) {
|
|
|
|
|
Sequence.SetOverloadFailure(
|
|
|
|
|
InitializationSequence::FK_ConstructorOverloadFailed,
|
|
|
|
|
Result);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the constructor initialization step. Any cv-qualification conversion is
|
|
|
|
|
// subsumed by the initialization.
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
if (Kind.getKind() == InitializationKind::IK_Copy) {
|
|
|
|
|
Sequence.AddUserConversionStep(Best->Function, DestType);
|
|
|
|
|
} else {
|
|
|
|
|
Sequence.AddConstructorInitializationStep(
|
2009-12-15 04:49:26 +08:00
|
|
|
|
cast<CXXConstructorDecl>(Best->Function),
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
DestType);
|
|
|
|
|
}
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-15 08:01:57 +08:00
|
|
|
|
/// \brief Attempt value initialization (C++ [dcl.init]p7).
|
|
|
|
|
static void TryValueInitialization(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
InitializationSequence &Sequence) {
|
|
|
|
|
// C++ [dcl.init]p5:
|
|
|
|
|
//
|
|
|
|
|
// To value-initialize an object of type T means:
|
|
|
|
|
QualType T = Entity.getType().getType();
|
|
|
|
|
|
|
|
|
|
// -- if T is an array type, then each element is value-initialized;
|
|
|
|
|
while (const ArrayType *AT = S.Context.getAsArrayType(T))
|
|
|
|
|
T = AT->getElementType();
|
|
|
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
|
if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
|
|
|
// -- if T is a class type (clause 9) with a user-declared
|
|
|
|
|
// constructor (12.1), then the default constructor for T is
|
|
|
|
|
// called (and the initialization is ill-formed if T has no
|
|
|
|
|
// accessible default constructor);
|
|
|
|
|
//
|
|
|
|
|
// FIXME: we really want to refer to a single subobject of the array,
|
|
|
|
|
// but Entity doesn't have a way to capture that (yet).
|
|
|
|
|
if (ClassDecl->hasUserDeclaredConstructor())
|
|
|
|
|
return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
|
|
|
|
|
|
2009-12-17 02:50:27 +08:00
|
|
|
|
// -- if T is a (possibly cv-qualified) non-union class type
|
|
|
|
|
// without a user-provided constructor, then the object is
|
|
|
|
|
// zero-initialized and, if T’s implicitly-declared default
|
|
|
|
|
// constructor is non-trivial, that constructor is called.
|
|
|
|
|
if ((ClassDecl->getTagKind() == TagDecl::TK_class ||
|
|
|
|
|
ClassDecl->getTagKind() == TagDecl::TK_struct) &&
|
|
|
|
|
!ClassDecl->hasTrivialConstructor()) {
|
|
|
|
|
Sequence.AddZeroInitializationStep(Entity.getType().getType());
|
|
|
|
|
return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
|
|
|
|
|
}
|
2009-12-15 08:01:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sequence.AddZeroInitializationStep(Entity.getType().getType());
|
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::ZeroInitialization);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 09:38:02 +08:00
|
|
|
|
/// \brief Attempt default initialization (C++ [dcl.init]p6).
|
|
|
|
|
static void TryDefaultInitialization(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
InitializationSequence &Sequence) {
|
|
|
|
|
assert(Kind.getKind() == InitializationKind::IK_Default);
|
|
|
|
|
|
|
|
|
|
// C++ [dcl.init]p6:
|
|
|
|
|
// To default-initialize an object of type T means:
|
|
|
|
|
// - if T is an array type, each element is default-initialized;
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
while (const ArrayType *Array = S.Context.getAsArrayType(DestType))
|
|
|
|
|
DestType = Array->getElementType();
|
|
|
|
|
|
|
|
|
|
// - if T is a (possibly cv-qualified) class type (Clause 9), the default
|
|
|
|
|
// constructor for T is called (and the initialization is ill-formed if
|
|
|
|
|
// T has no accessible default constructor);
|
|
|
|
|
if (DestType->isRecordType()) {
|
|
|
|
|
// FIXME: If a program calls for the default initialization of an object of
|
|
|
|
|
// a const-qualified type T, T shall be a class type with a user-provided
|
|
|
|
|
// default constructor.
|
|
|
|
|
return TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType,
|
|
|
|
|
Sequence);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - otherwise, no initialization is performed.
|
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::NoInitialization);
|
|
|
|
|
|
|
|
|
|
// If a program calls for the default initialization of an object of
|
|
|
|
|
// a const-qualified type T, T shall be a class type with a user-provided
|
|
|
|
|
// default constructor.
|
|
|
|
|
if (DestType.isConstQualified())
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
/// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
|
|
|
|
|
/// which enumerates all conversion functions and performs overload resolution
|
|
|
|
|
/// to select the best.
|
|
|
|
|
static void TryUserDefinedConversion(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr *Initializer,
|
|
|
|
|
InitializationSequence &Sequence) {
|
2009-12-15 01:27:33 +08:00
|
|
|
|
Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
|
|
|
|
|
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
assert(!DestType->isReferenceType() && "References are handled elsewhere");
|
|
|
|
|
QualType SourceType = Initializer->getType();
|
|
|
|
|
assert((DestType->isRecordType() || SourceType->isRecordType()) &&
|
|
|
|
|
"Must have a class type to perform a user-defined conversion");
|
|
|
|
|
|
|
|
|
|
// Build the candidate set directly in the initialization sequence
|
|
|
|
|
// structure, so that it will persist if we fail.
|
|
|
|
|
OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
|
|
|
|
|
CandidateSet.clear();
|
|
|
|
|
|
|
|
|
|
// Determine whether we are allowed to call explicit constructors or
|
|
|
|
|
// explicit conversion operators.
|
|
|
|
|
bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
|
|
|
|
|
|
|
|
|
|
if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
|
|
|
|
|
// The type we're converting to is a class type. Enumerate its constructors
|
|
|
|
|
// to see if there is a suitable conversion.
|
|
|
|
|
CXXRecordDecl *DestRecordDecl
|
|
|
|
|
= cast<CXXRecordDecl>(DestRecordType->getDecl());
|
|
|
|
|
|
|
|
|
|
DeclarationName ConstructorName
|
|
|
|
|
= S.Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
|
S.Context.getCanonicalType(DestType).getUnqualifiedType());
|
|
|
|
|
DeclContext::lookup_iterator Con, ConEnd;
|
|
|
|
|
for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName);
|
|
|
|
|
Con != ConEnd; ++Con) {
|
|
|
|
|
// Find the constructor (which may be a template).
|
|
|
|
|
CXXConstructorDecl *Constructor = 0;
|
|
|
|
|
FunctionTemplateDecl *ConstructorTmpl
|
|
|
|
|
= dyn_cast<FunctionTemplateDecl>(*Con);
|
|
|
|
|
if (ConstructorTmpl)
|
|
|
|
|
Constructor = cast<CXXConstructorDecl>(
|
|
|
|
|
ConstructorTmpl->getTemplatedDecl());
|
|
|
|
|
else
|
|
|
|
|
Constructor = cast<CXXConstructorDecl>(*Con);
|
|
|
|
|
|
|
|
|
|
if (!Constructor->isInvalidDecl() &&
|
|
|
|
|
Constructor->isConvertingConstructor(AllowExplicit)) {
|
|
|
|
|
if (ConstructorTmpl)
|
|
|
|
|
S.AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0,
|
|
|
|
|
&Initializer, 1, CandidateSet);
|
|
|
|
|
else
|
|
|
|
|
S.AddOverloadCandidate(Constructor, &Initializer, 1, CandidateSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-19 16:11:05 +08:00
|
|
|
|
|
|
|
|
|
SourceLocation DeclLoc = Initializer->getLocStart();
|
|
|
|
|
|
2009-12-15 01:27:33 +08:00
|
|
|
|
if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
|
|
|
|
|
// The type we're converting from is a class type, enumerate its conversion
|
|
|
|
|
// functions.
|
2009-12-19 16:11:05 +08:00
|
|
|
|
|
2009-12-21 06:12:03 +08:00
|
|
|
|
// We can only enumerate the conversion functions for a complete type; if
|
|
|
|
|
// the type isn't complete, simply skip this step.
|
|
|
|
|
if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
|
|
|
|
|
CXXRecordDecl *SourceRecordDecl
|
|
|
|
|
= cast<CXXRecordDecl>(SourceRecordType->getDecl());
|
2009-12-15 01:27:33 +08:00
|
|
|
|
|
2009-12-21 06:12:03 +08:00
|
|
|
|
const UnresolvedSet *Conversions
|
|
|
|
|
= SourceRecordDecl->getVisibleConversionFunctions();
|
|
|
|
|
for (UnresolvedSet::iterator I = Conversions->begin(),
|
|
|
|
|
E = Conversions->end();
|
|
|
|
|
I != E; ++I) {
|
|
|
|
|
NamedDecl *D = *I;
|
|
|
|
|
CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
|
|
|
|
|
if (isa<UsingShadowDecl>(D))
|
|
|
|
|
D = cast<UsingShadowDecl>(D)->getTargetDecl();
|
|
|
|
|
|
|
|
|
|
FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
|
|
|
|
|
CXXConversionDecl *Conv;
|
2009-12-15 01:27:33 +08:00
|
|
|
|
if (ConvTemplate)
|
2009-12-21 06:12:03 +08:00
|
|
|
|
Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
|
2009-12-15 01:27:33 +08:00
|
|
|
|
else
|
2009-12-21 06:12:03 +08:00
|
|
|
|
Conv = cast<CXXConversionDecl>(*I);
|
|
|
|
|
|
|
|
|
|
if (AllowExplicit || !Conv->isExplicit()) {
|
|
|
|
|
if (ConvTemplate)
|
|
|
|
|
S.AddTemplateConversionCandidate(ConvTemplate, ActingDC,
|
|
|
|
|
Initializer, DestType,
|
|
|
|
|
CandidateSet);
|
|
|
|
|
else
|
|
|
|
|
S.AddConversionCandidate(Conv, ActingDC, Initializer, DestType,
|
|
|
|
|
CandidateSet);
|
|
|
|
|
}
|
2009-12-15 01:27:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Perform overload resolution. If it fails, return the failed result.
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
|
if (OverloadingResult Result
|
|
|
|
|
= S.BestViableFunction(CandidateSet, DeclLoc, Best)) {
|
|
|
|
|
Sequence.SetOverloadFailure(
|
|
|
|
|
InitializationSequence::FK_UserConversionOverloadFailed,
|
|
|
|
|
Result);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionDecl *Function = Best->Function;
|
|
|
|
|
|
|
|
|
|
if (isa<CXXConstructorDecl>(Function)) {
|
|
|
|
|
// Add the user-defined conversion step. Any cv-qualification conversion is
|
|
|
|
|
// subsumed by the initialization.
|
|
|
|
|
Sequence.AddUserConversionStep(Function, DestType);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the user-defined conversion step that calls the conversion function.
|
|
|
|
|
QualType ConvType = Function->getResultType().getNonReferenceType();
|
|
|
|
|
Sequence.AddUserConversionStep(Function, ConvType);
|
|
|
|
|
|
|
|
|
|
// If the conversion following the call to the conversion function is
|
|
|
|
|
// interesting, add it as a separate step.
|
|
|
|
|
if (Best->FinalConversion.First || Best->FinalConversion.Second ||
|
|
|
|
|
Best->FinalConversion.Third) {
|
|
|
|
|
ImplicitConversionSequence ICS;
|
|
|
|
|
ICS.ConversionKind = ImplicitConversionSequence::StandardConversion;
|
|
|
|
|
ICS.Standard = Best->FinalConversion;
|
|
|
|
|
Sequence.AddConversionSequenceStep(ICS, DestType);
|
|
|
|
|
}
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Attempt an implicit conversion (C++ [conv]) converting from one
|
|
|
|
|
/// non-class type to another.
|
|
|
|
|
static void TryImplicitConversion(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr *Initializer,
|
|
|
|
|
InitializationSequence &Sequence) {
|
|
|
|
|
ImplicitConversionSequence ICS
|
|
|
|
|
= S.TryImplicitConversion(Initializer, Entity.getType().getType(),
|
|
|
|
|
/*SuppressUserConversions=*/true,
|
|
|
|
|
/*AllowExplicit=*/false,
|
|
|
|
|
/*ForceRValue=*/false,
|
|
|
|
|
/*FIXME:InOverloadResolution=*/false,
|
|
|
|
|
/*UserCast=*/Kind.isExplicitCast());
|
|
|
|
|
|
|
|
|
|
if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) {
|
|
|
|
|
Sequence.SetFailed(InitializationSequence::FK_ConversionFailed);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sequence.AddConversionSequenceStep(ICS, Entity.getType().getType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializationSequence::InitializationSequence(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr **Args,
|
|
|
|
|
unsigned NumArgs) {
|
|
|
|
|
ASTContext &Context = S.Context;
|
|
|
|
|
|
|
|
|
|
// C++0x [dcl.init]p16:
|
|
|
|
|
// The semantics of initializers are as follows. The destination type is
|
|
|
|
|
// the type of the object or reference being initialized and the source
|
|
|
|
|
// type is the type of the initializer expression. The source type is not
|
|
|
|
|
// defined when the initializer is a braced-init-list or when it is a
|
|
|
|
|
// parenthesized list of expressions.
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
|
|
|
|
|
if (DestType->isDependentType() ||
|
|
|
|
|
Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
|
|
|
|
|
SequenceKind = DependentSequence;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QualType SourceType;
|
|
|
|
|
Expr *Initializer = 0;
|
2009-12-16 09:38:02 +08:00
|
|
|
|
if (NumArgs == 1) {
|
2009-12-10 07:02:17 +08:00
|
|
|
|
Initializer = Args[0];
|
|
|
|
|
if (!isa<InitListExpr>(Initializer))
|
|
|
|
|
SourceType = Initializer->getType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - If the initializer is a braced-init-list, the object is
|
|
|
|
|
// list-initialized (8.5.4).
|
|
|
|
|
if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
|
|
|
|
|
TryListInitialization(S, Entity, Kind, InitList, *this);
|
2009-12-11 01:56:55 +08:00
|
|
|
|
return;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - If the destination type is a reference type, see 8.5.3.
|
|
|
|
|
if (DestType->isReferenceType()) {
|
|
|
|
|
// C++0x [dcl.init.ref]p1:
|
|
|
|
|
// A variable declared to be a T& or T&&, that is, "reference to type T"
|
|
|
|
|
// (8.3.2), shall be initialized by an object, or function, of type T or
|
|
|
|
|
// by an object that can be converted into a T.
|
|
|
|
|
// (Therefore, multiple arguments are not permitted.)
|
|
|
|
|
if (NumArgs != 1)
|
|
|
|
|
SetFailed(FK_TooManyInitsForReference);
|
|
|
|
|
else
|
|
|
|
|
TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - If the destination type is an array of characters, an array of
|
|
|
|
|
// char16_t, an array of char32_t, or an array of wchar_t, and the
|
|
|
|
|
// initializer is a string literal, see 8.5.2.
|
|
|
|
|
if (Initializer && IsStringInit(Initializer, DestType, Context)) {
|
|
|
|
|
TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - If the initializer is (), the object is value-initialized.
|
2009-12-16 09:38:02 +08:00
|
|
|
|
if (Kind.getKind() == InitializationKind::IK_Value ||
|
|
|
|
|
(Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
|
2009-12-10 07:02:17 +08:00
|
|
|
|
TryValueInitialization(S, Entity, Kind, *this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 09:38:02 +08:00
|
|
|
|
// Handle default initialization.
|
|
|
|
|
if (Kind.getKind() == InitializationKind::IK_Default){
|
|
|
|
|
TryDefaultInitialization(S, Entity, Kind, *this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
// - Otherwise, if the destination type is an array, the program is
|
|
|
|
|
// ill-formed.
|
|
|
|
|
if (const ArrayType *AT = Context.getAsArrayType(DestType)) {
|
|
|
|
|
if (AT->getElementType()->isAnyCharacterType())
|
|
|
|
|
SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
|
|
|
|
|
else
|
|
|
|
|
SetFailed(FK_ArrayNeedsInitList);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-12-19 16:11:05 +08:00
|
|
|
|
|
|
|
|
|
// Handle initialization in C
|
|
|
|
|
if (!S.getLangOptions().CPlusPlus) {
|
|
|
|
|
setSequenceKind(CAssignment);
|
|
|
|
|
AddCAssignmentStep(DestType);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
|
|
|
|
// - If the destination type is a (possibly cv-qualified) class type:
|
|
|
|
|
if (DestType->isRecordType()) {
|
|
|
|
|
// - If the initialization is direct-initialization, or if it is
|
|
|
|
|
// copy-initialization where the cv-unqualified version of the
|
|
|
|
|
// source type is the same class as, or a derived class of, the
|
|
|
|
|
// class of the destination, constructors are considered. [...]
|
|
|
|
|
if (Kind.getKind() == InitializationKind::IK_Direct ||
|
|
|
|
|
(Kind.getKind() == InitializationKind::IK_Copy &&
|
|
|
|
|
(Context.hasSameUnqualifiedType(SourceType, DestType) ||
|
|
|
|
|
S.IsDerivedFrom(SourceType, DestType))))
|
2009-12-15 08:01:57 +08:00
|
|
|
|
TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
|
|
|
|
|
Entity.getType().getType(), *this);
|
2009-12-10 07:02:17 +08:00
|
|
|
|
// - Otherwise (i.e., for the remaining copy-initialization cases),
|
|
|
|
|
// user-defined conversion sequences that can convert from the source
|
|
|
|
|
// type to the destination type or (when a conversion function is
|
|
|
|
|
// used) to a derived class thereof are enumerated as described in
|
|
|
|
|
// 13.3.1.4, and the best one is chosen through overload resolution
|
|
|
|
|
// (13.3).
|
|
|
|
|
else
|
|
|
|
|
TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 09:38:02 +08:00
|
|
|
|
if (NumArgs > 1) {
|
|
|
|
|
SetFailed(FK_TooManyInitsForScalar);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assert(NumArgs == 1 && "Zero-argument case handled above");
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
// - Otherwise, if the source type is a (possibly cv-qualified) class
|
|
|
|
|
// type, conversion functions are considered.
|
2009-12-16 09:38:02 +08:00
|
|
|
|
if (!SourceType.isNull() && SourceType->isRecordType()) {
|
2009-12-10 07:02:17 +08:00
|
|
|
|
TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - Otherwise, the initial value of the object being initialized is the
|
2009-12-15 01:27:33 +08:00
|
|
|
|
// (possibly converted) value of the initializer expression. Standard
|
2009-12-10 07:02:17 +08:00
|
|
|
|
// conversions (Clause 4) will be used, if necessary, to convert the
|
|
|
|
|
// initializer expression to the cv-unqualified version of the
|
|
|
|
|
// destination type; no user-defined conversions are considered.
|
2009-12-16 09:38:02 +08:00
|
|
|
|
setSequenceKind(StandardConversion);
|
2009-12-10 07:02:17 +08:00
|
|
|
|
TryImplicitConversion(S, Entity, Kind, Initializer, *this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializationSequence::~InitializationSequence() {
|
|
|
|
|
for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(),
|
|
|
|
|
StepEnd = Steps.end();
|
|
|
|
|
Step != StepEnd; ++Step)
|
|
|
|
|
Step->Destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Perform initialization
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
static Sema::AssignmentAction
|
|
|
|
|
getAssignmentAction(const InitializedEntity &Entity) {
|
|
|
|
|
switch(Entity.getKind()) {
|
|
|
|
|
case InitializedEntity::EK_Variable:
|
|
|
|
|
case InitializedEntity::EK_New:
|
|
|
|
|
return Sema::AA_Initializing;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Parameter:
|
|
|
|
|
// FIXME: Can we tell when we're sending vs. passing?
|
|
|
|
|
return Sema::AA_Passing;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Result:
|
|
|
|
|
return Sema::AA_Returning;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Exception:
|
|
|
|
|
case InitializedEntity::EK_Base:
|
|
|
|
|
llvm_unreachable("No assignment action for C++-specific initialization");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Temporary:
|
|
|
|
|
// FIXME: Can we tell apart casting vs. converting?
|
|
|
|
|
return Sema::AA_Casting;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Member:
|
|
|
|
|
case InitializedEntity::EK_ArrayOrVectorElement:
|
|
|
|
|
return Sema::AA_Initializing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Sema::AA_Converting;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool shouldBindAsTemporary(const InitializedEntity &Entity,
|
|
|
|
|
bool IsCopy) {
|
|
|
|
|
switch (Entity.getKind()) {
|
|
|
|
|
case InitializedEntity::EK_Result:
|
|
|
|
|
case InitializedEntity::EK_Exception:
|
|
|
|
|
return !IsCopy;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_New:
|
|
|
|
|
case InitializedEntity::EK_Variable:
|
|
|
|
|
case InitializedEntity::EK_Base:
|
|
|
|
|
case InitializedEntity::EK_Member:
|
|
|
|
|
case InitializedEntity::EK_ArrayOrVectorElement:
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Parameter:
|
|
|
|
|
case InitializedEntity::EK_Temporary:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm_unreachable("missed an InitializedEntity kind?");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief If we need to perform an additional copy of the initialized object
|
|
|
|
|
/// for this kind of entity (e.g., the result of a function or an object being
|
|
|
|
|
/// thrown), make the copy.
|
|
|
|
|
static Sema::OwningExprResult CopyIfRequiredForEntity(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
2009-12-19 11:01:41 +08:00
|
|
|
|
const InitializationKind &Kind,
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
Sema::OwningExprResult CurInit) {
|
|
|
|
|
SourceLocation Loc;
|
|
|
|
|
|
|
|
|
|
switch (Entity.getKind()) {
|
|
|
|
|
case InitializedEntity::EK_Result:
|
|
|
|
|
if (Entity.getType().getType()->isReferenceType())
|
|
|
|
|
return move(CurInit);
|
|
|
|
|
Loc = Entity.getReturnLoc();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Exception:
|
|
|
|
|
Loc = Entity.getThrowLoc();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InitializedEntity::EK_Variable:
|
2009-12-19 11:01:41 +08:00
|
|
|
|
if (Entity.getType().getType()->isReferenceType() ||
|
|
|
|
|
Kind.getKind() != InitializationKind::IK_Copy)
|
|
|
|
|
return move(CurInit);
|
|
|
|
|
Loc = Entity.getDecl()->getLocation();
|
|
|
|
|
break;
|
|
|
|
|
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
case InitializedEntity::EK_Parameter:
|
2009-12-19 11:01:41 +08:00
|
|
|
|
// FIXME: Do we need this initialization for a parameter?
|
|
|
|
|
return move(CurInit);
|
|
|
|
|
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
case InitializedEntity::EK_New:
|
|
|
|
|
case InitializedEntity::EK_Temporary:
|
|
|
|
|
case InitializedEntity::EK_Base:
|
|
|
|
|
case InitializedEntity::EK_Member:
|
|
|
|
|
case InitializedEntity::EK_ArrayOrVectorElement:
|
|
|
|
|
// We don't need to copy for any of these initialized entities.
|
|
|
|
|
return move(CurInit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Expr *CurInitExpr = (Expr *)CurInit.get();
|
|
|
|
|
CXXRecordDecl *Class = 0;
|
|
|
|
|
if (const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>())
|
|
|
|
|
Class = cast<CXXRecordDecl>(Record->getDecl());
|
|
|
|
|
if (!Class)
|
|
|
|
|
return move(CurInit);
|
|
|
|
|
|
|
|
|
|
// Perform overload resolution using the class's copy constructors.
|
|
|
|
|
DeclarationName ConstructorName
|
|
|
|
|
= S.Context.DeclarationNames.getCXXConstructorName(
|
|
|
|
|
S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)));
|
|
|
|
|
DeclContext::lookup_iterator Con, ConEnd;
|
|
|
|
|
OverloadCandidateSet CandidateSet;
|
|
|
|
|
for (llvm::tie(Con, ConEnd) = Class->lookup(ConstructorName);
|
|
|
|
|
Con != ConEnd; ++Con) {
|
|
|
|
|
// Find the constructor (which may be a template).
|
|
|
|
|
CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(*Con);
|
|
|
|
|
if (!Constructor || Constructor->isInvalidDecl() ||
|
2009-12-22 08:34:07 +08:00
|
|
|
|
!Constructor->isCopyConstructor())
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
S.AddOverloadCandidate(Constructor, &CurInitExpr, 1, CandidateSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
|
switch (S.BestViableFunction(CandidateSet, Loc, Best)) {
|
|
|
|
|
case OR_Success:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OR_No_Viable_Function:
|
|
|
|
|
S.Diag(Loc, diag::err_temp_copy_no_viable)
|
2009-12-19 11:01:41 +08:00
|
|
|
|
<< (int)Entity.getKind() << CurInitExpr->getType()
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
<< CurInitExpr->getSourceRange();
|
|
|
|
|
S.PrintOverloadCandidates(CandidateSet, false);
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
case OR_Ambiguous:
|
|
|
|
|
S.Diag(Loc, diag::err_temp_copy_ambiguous)
|
2009-12-19 11:01:41 +08:00
|
|
|
|
<< (int)Entity.getKind() << CurInitExpr->getType()
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
<< CurInitExpr->getSourceRange();
|
|
|
|
|
S.PrintOverloadCandidates(CandidateSet, true);
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
case OR_Deleted:
|
|
|
|
|
S.Diag(Loc, diag::err_temp_copy_deleted)
|
2009-12-19 11:01:41 +08:00
|
|
|
|
<< (int)Entity.getKind() << CurInitExpr->getType()
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
<< CurInitExpr->getSourceRange();
|
|
|
|
|
S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
|
|
|
|
|
<< Best->Function->isDeleted();
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurInit.release();
|
|
|
|
|
return S.BuildCXXConstructExpr(Loc, CurInitExpr->getType(),
|
|
|
|
|
cast<CXXConstructorDecl>(Best->Function),
|
|
|
|
|
/*Elidable=*/true,
|
|
|
|
|
Sema::MultiExprArg(S,
|
|
|
|
|
(void**)&CurInitExpr, 1));
|
|
|
|
|
}
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
|
|
|
|
Action::OwningExprResult
|
|
|
|
|
InitializationSequence::Perform(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
2009-12-11 01:56:55 +08:00
|
|
|
|
Action::MultiExprArg Args,
|
|
|
|
|
QualType *ResultType) {
|
2009-12-10 07:02:17 +08:00
|
|
|
|
if (SequenceKind == FailedSequence) {
|
|
|
|
|
unsigned NumArgs = Args.size();
|
|
|
|
|
Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs);
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SequenceKind == DependentSequence) {
|
2009-12-11 01:56:55 +08:00
|
|
|
|
// If the declaration is a non-dependent, incomplete array type
|
|
|
|
|
// that has an initializer, then its type will be completed once
|
|
|
|
|
// the initializer is instantiated.
|
|
|
|
|
if (ResultType && !Entity.getType().getType()->isDependentType() &&
|
|
|
|
|
Args.size() == 1) {
|
|
|
|
|
QualType DeclType = Entity.getType().getType();
|
|
|
|
|
if (const IncompleteArrayType *ArrayT
|
|
|
|
|
= S.Context.getAsIncompleteArrayType(DeclType)) {
|
|
|
|
|
// FIXME: We don't currently have the ability to accurately
|
|
|
|
|
// compute the length of an initializer list without
|
|
|
|
|
// performing full type-checking of the initializer list
|
|
|
|
|
// (since we have to determine where braces are implicitly
|
|
|
|
|
// introduced and such). So, we fall back to making the array
|
|
|
|
|
// type a dependently-sized array type with no specified
|
|
|
|
|
// bound.
|
|
|
|
|
if (isa<InitListExpr>((Expr *)Args.get()[0])) {
|
|
|
|
|
SourceRange Brackets;
|
|
|
|
|
// Scavange the location of the brackets from the entity, if we can.
|
|
|
|
|
if (isa<IncompleteArrayTypeLoc>(Entity.getType())) {
|
|
|
|
|
IncompleteArrayTypeLoc ArrayLoc
|
|
|
|
|
= cast<IncompleteArrayTypeLoc>(Entity.getType());
|
|
|
|
|
Brackets = ArrayLoc.getBracketsRange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*ResultType
|
|
|
|
|
= S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
|
|
|
|
|
/*NumElts=*/0,
|
|
|
|
|
ArrayT->getSizeModifier(),
|
|
|
|
|
ArrayT->getIndexTypeCVRQualifiers(),
|
|
|
|
|
Brackets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
if (Kind.getKind() == InitializationKind::IK_Copy)
|
|
|
|
|
return Sema::OwningExprResult(S, Args.release()[0]);
|
|
|
|
|
|
|
|
|
|
unsigned NumArgs = Args.size();
|
|
|
|
|
return S.Owned(new (S.Context) ParenListExpr(S.Context,
|
|
|
|
|
SourceLocation(),
|
|
|
|
|
(Expr **)Args.release(),
|
|
|
|
|
NumArgs,
|
|
|
|
|
SourceLocation()));
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 09:38:02 +08:00
|
|
|
|
if (SequenceKind == NoInitialization)
|
|
|
|
|
return S.Owned((Expr *)0);
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
QualType DestType = Entity.getType().getType().getNonReferenceType();
|
2009-12-11 01:56:55 +08:00
|
|
|
|
if (ResultType)
|
|
|
|
|
*ResultType = Entity.getType().getType();
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
2009-12-16 09:38:02 +08:00
|
|
|
|
Sema::OwningExprResult CurInit = S.Owned((Expr *)0);
|
|
|
|
|
|
|
|
|
|
assert(!Steps.empty() && "Cannot have an empty initialization sequence");
|
|
|
|
|
|
|
|
|
|
// For initialization steps that start with a single initializer,
|
|
|
|
|
// grab the only argument out the Args and place it into the "current"
|
|
|
|
|
// initializer.
|
|
|
|
|
switch (Steps.front().Kind) {
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
case SK_ResolveAddressOfOverloadedFunction:
|
|
|
|
|
case SK_CastDerivedToBaseRValue:
|
|
|
|
|
case SK_CastDerivedToBaseLValue:
|
|
|
|
|
case SK_BindReference:
|
|
|
|
|
case SK_BindReferenceToTemporary:
|
|
|
|
|
case SK_UserConversion:
|
|
|
|
|
case SK_QualificationConversionLValue:
|
|
|
|
|
case SK_QualificationConversionRValue:
|
|
|
|
|
case SK_ConversionSequence:
|
|
|
|
|
case SK_ListInitialization:
|
|
|
|
|
case SK_CAssignment:
|
2009-12-19 16:11:05 +08:00
|
|
|
|
case SK_StringInit:
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
assert(Args.size() == 1);
|
|
|
|
|
CurInit = Sema::OwningExprResult(S, ((Expr **)(Args.get()))[0]->Retain());
|
|
|
|
|
if (CurInit.isInvalid())
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SK_ConstructorInitialization:
|
|
|
|
|
case SK_ZeroInitialization:
|
|
|
|
|
break;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Walk through the computed steps for the initialization sequence,
|
|
|
|
|
// performing the specified conversions along the way.
|
2009-12-17 02:50:27 +08:00
|
|
|
|
bool ConstructorInitRequiresZeroInit = false;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
for (step_iterator Step = step_begin(), StepEnd = step_end();
|
|
|
|
|
Step != StepEnd; ++Step) {
|
|
|
|
|
if (CurInit.isInvalid())
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
Expr *CurInitExpr = (Expr *)CurInit.get();
|
2009-12-16 09:38:02 +08:00
|
|
|
|
QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType();
|
2009-12-10 07:02:17 +08:00
|
|
|
|
|
|
|
|
|
switch (Step->Kind) {
|
|
|
|
|
case SK_ResolveAddressOfOverloadedFunction:
|
|
|
|
|
// Overload resolution determined which function invoke; update the
|
|
|
|
|
// initializer to reflect that choice.
|
|
|
|
|
CurInit = S.FixOverloadedFunctionReference(move(CurInit), Step->Function);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SK_CastDerivedToBaseRValue:
|
|
|
|
|
case SK_CastDerivedToBaseLValue: {
|
|
|
|
|
// We have a derived-to-base cast that produces either an rvalue or an
|
|
|
|
|
// lvalue. Perform that cast.
|
|
|
|
|
|
|
|
|
|
// Casts to inaccessible base classes are allowed with C-style casts.
|
|
|
|
|
bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
|
|
|
|
|
if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
|
|
|
|
|
CurInitExpr->getLocStart(),
|
|
|
|
|
CurInitExpr->getSourceRange(),
|
|
|
|
|
IgnoreBaseAccess))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
CurInit = S.Owned(new (S.Context) ImplicitCastExpr(Step->Type,
|
|
|
|
|
CastExpr::CK_DerivedToBase,
|
|
|
|
|
(Expr*)CurInit.release(),
|
|
|
|
|
Step->Kind == SK_CastDerivedToBaseLValue));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SK_BindReference:
|
|
|
|
|
if (FieldDecl *BitField = CurInitExpr->getBitField()) {
|
|
|
|
|
// References cannot bind to bit fields (C++ [dcl.init.ref]p5).
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
|
|
|
|
|
<< Entity.getType().getType().isVolatileQualified()
|
|
|
|
|
<< BitField->getDeclName()
|
|
|
|
|
<< CurInitExpr->getSourceRange();
|
|
|
|
|
S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reference binding does not have any corresponding ASTs.
|
|
|
|
|
|
|
|
|
|
// Check exception specifications
|
|
|
|
|
if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SK_BindReferenceToTemporary:
|
|
|
|
|
// Check exception specifications
|
|
|
|
|
if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
// FIXME: At present, we have no AST to describe when we need to make a
|
|
|
|
|
// temporary to bind a reference to. We should.
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SK_UserConversion: {
|
|
|
|
|
// We have a user-defined conversion that invokes either a constructor
|
|
|
|
|
// or a conversion function.
|
|
|
|
|
CastExpr::CastKind CastKind = CastExpr::CK_Unknown;
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
bool IsCopy = false;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
if (CXXConstructorDecl *Constructor
|
|
|
|
|
= dyn_cast<CXXConstructorDecl>(Step->Function)) {
|
|
|
|
|
// Build a call to the selected constructor.
|
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S);
|
|
|
|
|
SourceLocation Loc = CurInitExpr->getLocStart();
|
|
|
|
|
CurInit.release(); // Ownership transferred into MultiExprArg, below.
|
|
|
|
|
|
|
|
|
|
// Determine the arguments required to actually perform the constructor
|
|
|
|
|
// call.
|
|
|
|
|
if (S.CompleteConstructorCall(Constructor,
|
|
|
|
|
Sema::MultiExprArg(S,
|
|
|
|
|
(void **)&CurInitExpr,
|
|
|
|
|
1),
|
|
|
|
|
Loc, ConstructorArgs))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
// Build the an expression that constructs a temporary.
|
|
|
|
|
CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
|
|
|
|
|
move_arg(ConstructorArgs));
|
|
|
|
|
if (CurInit.isInvalid())
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
CastKind = CastExpr::CK_ConstructorConversion;
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
|
|
|
|
|
if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
|
|
|
|
|
S.IsDerivedFrom(SourceType, Class))
|
|
|
|
|
IsCopy = true;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
// Build a call to the conversion function.
|
|
|
|
|
CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Step->Function);
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
// FIXME: Should we move this initialization into a separate
|
|
|
|
|
// derived-to-base conversion? I believe the answer is "no", because
|
|
|
|
|
// we don't want to turn off access control here for c-style casts.
|
|
|
|
|
if (S.PerformObjectArgumentInitialization(CurInitExpr, Conversion))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
// Do a little dance to make sure that CurInit has the proper
|
|
|
|
|
// pointer.
|
|
|
|
|
CurInit.release();
|
|
|
|
|
|
|
|
|
|
// Build the actual call to the conversion function.
|
|
|
|
|
CurInit = S.Owned(S.BuildCXXMemberCallExpr(CurInitExpr, Conversion));
|
|
|
|
|
if (CurInit.isInvalid() || !CurInit.get())
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
CastKind = CastExpr::CK_UserDefinedConversion;
|
|
|
|
|
}
|
|
|
|
|
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
if (shouldBindAsTemporary(Entity, IsCopy))
|
|
|
|
|
CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
CurInitExpr = CurInit.takeAs<Expr>();
|
|
|
|
|
CurInit = S.Owned(new (S.Context) ImplicitCastExpr(CurInitExpr->getType(),
|
|
|
|
|
CastKind,
|
|
|
|
|
CurInitExpr,
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
false));
|
|
|
|
|
|
|
|
|
|
if (!IsCopy)
|
2009-12-19 11:01:41 +08:00
|
|
|
|
CurInit = CopyIfRequiredForEntity(S, Entity, Kind, move(CurInit));
|
2009-12-10 07:02:17 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SK_QualificationConversionLValue:
|
|
|
|
|
case SK_QualificationConversionRValue:
|
|
|
|
|
// Perform a qualification conversion; these can never go wrong.
|
|
|
|
|
S.ImpCastExprToType(CurInitExpr, Step->Type,
|
|
|
|
|
CastExpr::CK_NoOp,
|
|
|
|
|
Step->Kind == SK_QualificationConversionLValue);
|
|
|
|
|
CurInit.release();
|
|
|
|
|
CurInit = S.Owned(CurInitExpr);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SK_ConversionSequence:
|
2009-12-16 11:45:30 +08:00
|
|
|
|
if (S.PerformImplicitConversion(CurInitExpr, Step->Type, Sema::AA_Converting,
|
2009-12-10 07:02:17 +08:00
|
|
|
|
false, false, *Step->ICS))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
CurInit.release();
|
|
|
|
|
CurInit = S.Owned(CurInitExpr);
|
|
|
|
|
break;
|
2009-12-11 01:56:55 +08:00
|
|
|
|
|
|
|
|
|
case SK_ListInitialization: {
|
|
|
|
|
InitListExpr *InitList = cast<InitListExpr>(CurInitExpr);
|
|
|
|
|
QualType Ty = Step->Type;
|
2009-12-16 14:35:08 +08:00
|
|
|
|
if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty))
|
2009-12-11 01:56:55 +08:00
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
CurInit.release();
|
|
|
|
|
CurInit = S.Owned(InitList);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-12-15 04:49:26 +08:00
|
|
|
|
|
|
|
|
|
case SK_ConstructorInitialization: {
|
|
|
|
|
CXXConstructorDecl *Constructor
|
|
|
|
|
= cast<CXXConstructorDecl>(Step->Function);
|
|
|
|
|
|
|
|
|
|
// Build a call to the selected constructor.
|
|
|
|
|
ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S);
|
|
|
|
|
SourceLocation Loc = Kind.getLocation();
|
|
|
|
|
|
|
|
|
|
// Determine the arguments required to actually perform the constructor
|
|
|
|
|
// call.
|
|
|
|
|
if (S.CompleteConstructorCall(Constructor, move(Args),
|
|
|
|
|
Loc, ConstructorArgs))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
// Build the an expression that constructs a temporary.
|
2009-12-21 06:01:25 +08:00
|
|
|
|
CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType().getType(),
|
|
|
|
|
Constructor,
|
2009-12-17 02:50:27 +08:00
|
|
|
|
move_arg(ConstructorArgs),
|
|
|
|
|
ConstructorInitRequiresZeroInit);
|
2009-12-15 04:49:26 +08:00
|
|
|
|
if (CurInit.isInvalid())
|
|
|
|
|
return S.ExprError();
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
|
|
|
|
|
bool Elidable
|
|
|
|
|
= cast<CXXConstructExpr>((Expr *)CurInit.get())->isElidable();
|
|
|
|
|
if (shouldBindAsTemporary(Entity, Elidable))
|
|
|
|
|
CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
|
|
|
|
|
|
|
|
|
|
if (!Elidable)
|
2009-12-19 11:01:41 +08:00
|
|
|
|
CurInit = CopyIfRequiredForEntity(S, Entity, Kind, move(CurInit));
|
2009-12-15 04:49:26 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2009-12-15 08:01:57 +08:00
|
|
|
|
|
|
|
|
|
case SK_ZeroInitialization: {
|
2009-12-17 02:50:27 +08:00
|
|
|
|
step_iterator NextStep = Step;
|
|
|
|
|
++NextStep;
|
|
|
|
|
if (NextStep != StepEnd &&
|
|
|
|
|
NextStep->Kind == SK_ConstructorInitialization) {
|
|
|
|
|
// The need for zero-initialization is recorded directly into
|
|
|
|
|
// the call to the object's constructor within the next step.
|
|
|
|
|
ConstructorInitRequiresZeroInit = true;
|
|
|
|
|
} else if (Kind.getKind() == InitializationKind::IK_Value &&
|
|
|
|
|
S.getLangOptions().CPlusPlus &&
|
|
|
|
|
!Kind.isImplicitValueInit()) {
|
2009-12-15 08:01:57 +08:00
|
|
|
|
CurInit = S.Owned(new (S.Context) CXXZeroInitValueExpr(Step->Type,
|
|
|
|
|
Kind.getRange().getBegin(),
|
|
|
|
|
Kind.getRange().getEnd()));
|
2009-12-17 02:50:27 +08:00
|
|
|
|
} else {
|
2009-12-15 08:01:57 +08:00
|
|
|
|
CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type));
|
2009-12-17 02:50:27 +08:00
|
|
|
|
}
|
2009-12-15 08:01:57 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
|
|
|
|
|
case SK_CAssignment: {
|
|
|
|
|
QualType SourceType = CurInitExpr->getType();
|
|
|
|
|
Sema::AssignConvertType ConvTy =
|
|
|
|
|
S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr);
|
|
|
|
|
if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
|
|
|
|
|
Step->Type, SourceType,
|
|
|
|
|
CurInitExpr, getAssignmentAction(Entity)))
|
|
|
|
|
return S.ExprError();
|
|
|
|
|
|
|
|
|
|
CurInit.release();
|
|
|
|
|
CurInit = S.Owned(CurInitExpr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-12-19 16:11:05 +08:00
|
|
|
|
|
|
|
|
|
case SK_StringInit: {
|
|
|
|
|
QualType Ty = Step->Type;
|
|
|
|
|
CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, S);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return move(CurInit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Diagnose initialization failures
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
bool InitializationSequence::Diagnose(Sema &S,
|
|
|
|
|
const InitializedEntity &Entity,
|
|
|
|
|
const InitializationKind &Kind,
|
|
|
|
|
Expr **Args, unsigned NumArgs) {
|
|
|
|
|
if (SequenceKind != FailedSequence)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QualType DestType = Entity.getType().getType();
|
|
|
|
|
switch (Failure) {
|
|
|
|
|
case FK_TooManyInitsForReference:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
|
|
|
|
|
<< SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_ArrayNeedsInitList:
|
|
|
|
|
case FK_ArrayNeedsInitListOrStringLiteral:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list)
|
|
|
|
|
<< (Failure == FK_ArrayNeedsInitListOrStringLiteral);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_AddressOfOverloadFailed:
|
|
|
|
|
S.ResolveAddressOfOverloadedFunction(Args[0],
|
|
|
|
|
DestType.getNonReferenceType(),
|
|
|
|
|
true);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_ReferenceInitOverloadFailed:
|
2009-12-15 01:27:33 +08:00
|
|
|
|
case FK_UserConversionOverloadFailed:
|
2009-12-10 07:02:17 +08:00
|
|
|
|
switch (FailedOverloadResult) {
|
|
|
|
|
case OR_Ambiguous:
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
if (Failure == FK_UserConversionOverloadFailed)
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
|
|
|
|
|
<< Args[0]->getType() << DestType
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
else
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
|
|
|
|
|
<< DestType << Args[0]->getType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
|
2009-12-10 07:02:17 +08:00
|
|
|
|
S.PrintOverloadCandidates(FailedCandidateSet, true);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OR_No_Viable_Function:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
|
|
|
|
|
<< Args[0]->getType() << DestType.getNonReferenceType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
S.PrintOverloadCandidates(FailedCandidateSet, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OR_Deleted: {
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
|
|
|
|
|
<< Args[0]->getType() << DestType.getNonReferenceType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
|
OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet,
|
|
|
|
|
Kind.getLocation(),
|
|
|
|
|
Best);
|
|
|
|
|
if (Ovl == OR_Deleted) {
|
|
|
|
|
S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
|
|
|
|
|
<< Best->Function->isDeleted();
|
|
|
|
|
} else {
|
2009-12-12 13:05:38 +08:00
|
|
|
|
llvm_unreachable("Inconsistent overload resolution?");
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OR_Success:
|
2009-12-12 13:05:38 +08:00
|
|
|
|
llvm_unreachable("Conversion did not fail!");
|
2009-12-10 07:02:17 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_NonConstLValueReferenceBindingToTemporary:
|
|
|
|
|
case FK_NonConstLValueReferenceBindingToUnrelated:
|
|
|
|
|
S.Diag(Kind.getLocation(),
|
|
|
|
|
Failure == FK_NonConstLValueReferenceBindingToTemporary
|
|
|
|
|
? diag::err_lvalue_reference_bind_to_temporary
|
|
|
|
|
: diag::err_lvalue_reference_bind_to_unrelated)
|
|
|
|
|
<< DestType.getNonReferenceType()
|
|
|
|
|
<< Args[0]->getType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_RValueReferenceBindingToLValue:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_ReferenceInitDropsQualifiers:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
|
|
|
|
|
<< DestType.getNonReferenceType()
|
|
|
|
|
<< Args[0]->getType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_ReferenceInitFailed:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
|
|
|
|
|
<< DestType.getNonReferenceType()
|
|
|
|
|
<< (Args[0]->isLvalue(S.Context) == Expr::LV_Valid)
|
|
|
|
|
<< Args[0]->getType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_ConversionFailed:
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_init_conversion_failed)
|
|
|
|
|
<< (int)Entity.getKind()
|
2009-12-10 07:02:17 +08:00
|
|
|
|
<< DestType
|
|
|
|
|
<< (Args[0]->isLvalue(S.Context) == Expr::LV_Valid)
|
|
|
|
|
<< Args[0]->getType()
|
|
|
|
|
<< Args[0]->getSourceRange();
|
2009-12-11 01:56:55 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_TooManyInitsForScalar: {
|
2009-12-16 09:38:02 +08:00
|
|
|
|
SourceRange R;
|
|
|
|
|
|
|
|
|
|
if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
|
|
|
|
|
R = SourceRange(InitList->getInit(1)->getLocStart(),
|
|
|
|
|
InitList->getLocEnd());
|
|
|
|
|
else
|
|
|
|
|
R = SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
|
2009-12-11 01:56:55 +08:00
|
|
|
|
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_excess_initializers)
|
2009-12-16 09:38:02 +08:00
|
|
|
|
<< /*scalar=*/2 << R;
|
2009-12-11 01:56:55 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case FK_ReferenceBindingToInitList:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
|
|
|
|
|
<< DestType.getNonReferenceType() << Args[0]->getSourceRange();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FK_InitListBadDestinationType:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
|
|
|
|
|
<< (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
|
|
|
|
|
break;
|
2009-12-15 04:49:26 +08:00
|
|
|
|
|
|
|
|
|
case FK_ConstructorOverloadFailed: {
|
|
|
|
|
SourceRange ArgsRange;
|
|
|
|
|
if (NumArgs)
|
|
|
|
|
ArgsRange = SourceRange(Args[0]->getLocStart(),
|
|
|
|
|
Args[NumArgs - 1]->getLocEnd());
|
|
|
|
|
|
|
|
|
|
// FIXME: Using "DestType" for the entity we're printing is probably
|
|
|
|
|
// bad.
|
|
|
|
|
switch (FailedOverloadResult) {
|
|
|
|
|
case OR_Ambiguous:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
|
|
|
|
|
<< DestType << ArgsRange;
|
|
|
|
|
S.PrintOverloadCandidates(FailedCandidateSet, true);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OR_No_Viable_Function:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
|
|
|
|
|
<< DestType << ArgsRange;
|
|
|
|
|
S.PrintOverloadCandidates(FailedCandidateSet, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OR_Deleted: {
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
|
|
|
|
|
<< true << DestType << ArgsRange;
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
|
OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet,
|
|
|
|
|
Kind.getLocation(),
|
|
|
|
|
Best);
|
|
|
|
|
if (Ovl == OR_Deleted) {
|
|
|
|
|
S.Diag(Best->Function->getLocation(), diag::note_unavailable_here)
|
|
|
|
|
<< Best->Function->isDeleted();
|
|
|
|
|
} else {
|
|
|
|
|
llvm_unreachable("Inconsistent overload resolution?");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OR_Success:
|
|
|
|
|
llvm_unreachable("Conversion did not fail!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-12-16 09:38:02 +08:00
|
|
|
|
|
|
|
|
|
case FK_DefaultInitOfConst:
|
|
|
|
|
S.Diag(Kind.getLocation(), diag::err_default_init_const)
|
|
|
|
|
<< DestType;
|
|
|
|
|
break;
|
2009-12-10 07:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Initialization helper functions
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Sema::OwningExprResult
|
|
|
|
|
Sema::PerformCopyInitialization(const InitializedEntity &Entity,
|
|
|
|
|
SourceLocation EqualLoc,
|
|
|
|
|
OwningExprResult Init) {
|
|
|
|
|
if (Init.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
|
|
Expr *InitE = (Expr *)Init.get();
|
|
|
|
|
assert(InitE && "No initialization expression?");
|
|
|
|
|
|
|
|
|
|
if (EqualLoc.isInvalid())
|
|
|
|
|
EqualLoc = InitE->getLocStart();
|
|
|
|
|
|
|
|
|
|
InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
|
|
|
|
|
EqualLoc);
|
|
|
|
|
InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
|
|
|
|
|
Init.release();
|
|
|
|
|
return Seq.Perform(*this, Entity, Kind,
|
|
|
|
|
MultiExprArg(*this, (void**)&InitE, 1));
|
|
|
|
|
}
|