Str->setType(DeclT);
}
-bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
- const InitializedEntity &Entity,
- const InitializationKind &Kind) {
- SourceLocation InitLoc = Kind.getLocation();
- DeclarationName InitEntity = Entity.getName();
- bool DirectInit = (Kind.getKind() == InitializationKind::IK_Direct);
-
- if (DeclType->isDependentType() ||
- 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
- // the initializer is instantiated.
- if (!DeclType->isDependentType()) {
- if (const IncompleteArrayType *ArrayT
- = Context.getAsIncompleteArrayType(DeclType)) {
- 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.
- DeclType = Context.getDependentSizedArrayType(ArrayT->getElementType(),
- /*NumElts=*/0,
- ArrayT->getSizeModifier(),
- ArrayT->getIndexTypeCVRQualifiers(),
- SourceRange());
- }
- }
-
- return false;
- }
-
- // C++ [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.
- 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;
- }
-
- // 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();
-
- InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
- if (!InitList) {
- // FIXME: Handle wide strings
- if (Expr *Str = IsStringInit(Init, DeclType, Context)) {
- CheckStringInit(Str, DeclType, *this);
- return false;
- }
-
- // C++ [dcl.init]p14:
- // -- If the destination type is a (possibly cv-qualified) class
- // type:
- if (getLangOptions().CPlusPlus && DeclType->isRecordType()) {
- 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;
- }
-
- // C99 6.7.8p16.
- if (DeclType->isArrayType())
- return Diag(Init->getLocStart(), diag::err_array_init_list_required)
- << Init->getSourceRange();
-
- return CheckSingleInitializer(Init, DeclType, DirectInit, *this);
- }
-
- bool hadError = CheckInitList(Entity, InitList, DeclType);
- Init = InitList;
- return hadError;
-}
-
//===----------------------------------------------------------------------===//
// Semantic checking for initializer lists.
//===----------------------------------------------------------------------===//