diff options
Diffstat (limited to 'lib/Sema')
-rw-r--r-- | lib/Sema/SemaExprCXX.cpp | 49 | ||||
-rw-r--r-- | lib/Sema/SemaInit.cpp | 18 | ||||
-rw-r--r-- | lib/Sema/TreeTransform.h | 118 |
3 files changed, 86 insertions, 99 deletions
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 8e376c29f3..de9b599907 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -481,34 +481,43 @@ ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); } -/// ActOnCXXTypeConstructExpr - Parse construction of a specified type. -/// Can be interpreted either as function-style casting ("int(x)") -/// or class type construction ("ClassType(x,y,z)") -/// or creation of a value-initialized type ("int()"). ExprResult -Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, ParsedType TypeRep, +Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, SourceLocation LParenLoc, MultiExprArg exprs, SourceLocation *CommaLocs, SourceLocation RParenLoc) { if (!TypeRep) return ExprError(); - + TypeSourceInfo *TInfo; QualType Ty = GetTypeFromParser(TypeRep, &TInfo); if (!TInfo) TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); + + return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); +} + +/// ActOnCXXTypeConstructExpr - Parse construction of a specified type. +/// Can be interpreted either as function-style casting ("int(x)") +/// or class type construction ("ClassType(x,y,z)") +/// or creation of a value-initialized type ("int()"). +ExprResult +Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, + SourceLocation LParenLoc, + MultiExprArg exprs, + SourceLocation RParenLoc) { + QualType Ty = TInfo->getType(); unsigned NumExprs = exprs.size(); Expr **Exprs = (Expr**)exprs.get(); - SourceLocation TyBeginLoc = TypeRange.getBegin(); + SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); if (Ty->isDependentType() || CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { exprs.release(); - return Owned(CXXUnresolvedConstructExpr::Create(Context, - TypeRange.getBegin(), Ty, + return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, LParenLoc, Exprs, NumExprs, RParenLoc)); @@ -536,29 +545,29 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, ParsedType TypeRep, if (NumExprs == 1) { CastKind Kind = CK_Unknown; CXXCastPath BasePath; - if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, BasePath, + if (CheckCastTypes(TInfo->getTypeLoc().getSourceRange(), Ty, Exprs[0], + Kind, BasePath, /*FunctionalStyle=*/true)) return ExprError(); exprs.release(); return Owned(CXXFunctionalCastExpr::Create(Context, - Ty.getNonLValueExprType(Context), + Ty.getNonLValueExprType(Context), TInfo, TyBeginLoc, Kind, Exprs[0], &BasePath, RParenLoc)); } if (Ty->isRecordType()) { - InitializedEntity Entity = InitializedEntity::InitializeTemporary(Ty); + InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); InitializationKind Kind - = NumExprs ? InitializationKind::CreateDirect(TypeRange.getBegin(), + = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, LParenLoc, RParenLoc) - : InitializationKind::CreateValue(TypeRange.getBegin(), + : InitializationKind::CreateValue(TyBeginLoc, LParenLoc, RParenLoc); InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); - ExprResult Result = InitSeq.Perform(*this, Entity, Kind, - move(exprs)); + ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); // FIXME: Improve AST representation? return move(Result); @@ -569,18 +578,22 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, ParsedType TypeRep, // be a class with a suitably declared constructor. // if (NumExprs > 1) - return ExprError(Diag(CommaLocs[0], + return ExprError(Diag(PP.getLocForEndOfToken(Exprs[0]->getLocEnd()), diag::err_builtin_func_cast_more_than_one_arg) << FullRange); assert(NumExprs == 0 && "Expected 0 expressions"); + // FIXME: Why doesn't this go through the new-initialization code? + // C++ [expr.type.conv]p2: // The expression T(), where T is a simple-type-specifier for a non-array // complete object type or the (possibly cv-qualified) void type, creates an // rvalue of the specified type, which is value-initialized. // exprs.release(); - return Owned(new (Context) CXXScalarValueInitExpr(Ty, TyBeginLoc, RParenLoc)); + return Owned(new (Context) CXXScalarValueInitExpr( + TInfo->getType().getNonLValueExprType(Context), + TInfo, RParenLoc)); } diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index a28fd7fe12..64f5ac06b4 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -3843,10 +3843,14 @@ InitializationSequence::Perform(Sema &S, unsigned NumExprs = ConstructorArgs.size(); Expr **Exprs = (Expr **)ConstructorArgs.take(); S.MarkDeclarationReferenced(Loc, Constructor); + + TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); + if (!TSInfo) + TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); + CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, - Entity.getType(), - Loc, + TSInfo, Exprs, NumExprs, Kind.getParenRange().getEnd(), @@ -3901,8 +3905,14 @@ InitializationSequence::Perform(Sema &S, } else if (Kind.getKind() == InitializationKind::IK_Value && S.getLangOptions().CPlusPlus && !Kind.isImplicitValueInit()) { - CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr(Step->Type, - Kind.getRange().getBegin(), + TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); + if (!TSInfo) + TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, + Kind.getRange().getBegin()); + + CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( + TSInfo->getType().getNonLValueExprType(S.Context), + TSInfo, Kind.getRange().getEnd())); } else { CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 0300143743..16114f2ca3 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -1492,16 +1492,12 @@ public: /// /// By default, performs semantic analysis to build the new expression. /// Subclasses may override this routine to provide different behavior. - ExprResult RebuildCXXFunctionalCastExpr(SourceRange TypeRange, - TypeSourceInfo *TInfo, - SourceLocation LParenLoc, - Expr *Sub, - SourceLocation RParenLoc) { - return getSema().ActOnCXXTypeConstructExpr(TypeRange, - ParsedType::make(TInfo->getType()), - LParenLoc, + ExprResult RebuildCXXFunctionalCastExpr(TypeSourceInfo *TInfo, + SourceLocation LParenLoc, + Expr *Sub, + SourceLocation RParenLoc) { + return getSema().BuildCXXTypeConstructExpr(TInfo, LParenLoc, MultiExprArg(&Sub, 1), - /*CommaLocs=*/0, RParenLoc); } @@ -1565,14 +1561,12 @@ public: /// /// By default, performs semantic analysis to build the new expression. /// Subclasses may override this routine to provide different behavior. - ExprResult RebuildCXXScalarValueInitExpr(SourceLocation TypeStartLoc, - SourceLocation LParenLoc, - QualType T, - SourceLocation RParenLoc) { - return getSema().ActOnCXXTypeConstructExpr(SourceRange(TypeStartLoc), - ParsedType::make(T), LParenLoc, + ExprResult RebuildCXXScalarValueInitExpr(TypeSourceInfo *TSInfo, + SourceLocation LParenLoc, + SourceLocation RParenLoc) { + return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc, MultiExprArg(getSema(), 0, 0), - 0, RParenLoc); + RParenLoc); } /// \brief Build a new C++ "new" expression. @@ -1685,17 +1679,13 @@ public: /// /// By default, performs semantic analysis to build the new expression. /// Subclasses may override this routine to provide different behavior. - ExprResult RebuildCXXTemporaryObjectExpr(SourceLocation TypeBeginLoc, - QualType T, - SourceLocation LParenLoc, - MultiExprArg Args, - SourceLocation *Commas, - SourceLocation RParenLoc) { - return getSema().ActOnCXXTypeConstructExpr(SourceRange(TypeBeginLoc), - ParsedType::make(T), + ExprResult RebuildCXXTemporaryObjectExpr(TypeSourceInfo *TSInfo, + SourceLocation LParenLoc, + MultiExprArg Args, + SourceLocation RParenLoc) { + return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc, move(Args), - Commas, RParenLoc); } @@ -1703,18 +1693,13 @@ public: /// /// By default, performs semantic analysis to build the new expression. /// Subclasses may override this routine to provide different behavior. - ExprResult RebuildCXXUnresolvedConstructExpr(SourceLocation TypeBeginLoc, - QualType T, - SourceLocation LParenLoc, - MultiExprArg Args, - SourceLocation *Commas, - SourceLocation RParenLoc) { - return getSema().ActOnCXXTypeConstructExpr(SourceRange(TypeBeginLoc, - /*FIXME*/LParenLoc), - ParsedType::make(T), + ExprResult RebuildCXXUnresolvedConstructExpr(TypeSourceInfo *TSInfo, + SourceLocation LParenLoc, + MultiExprArg Args, + SourceLocation RParenLoc) { + return getSema().BuildCXXTypeConstructExpr(TSInfo, LParenLoc, move(Args), - Commas, RParenLoc); } @@ -5113,10 +5098,7 @@ TreeTransform<Derived>::TransformCXXFunctionalCastExpr( SubExpr.get() == E->getSubExpr()) return SemaRef.Owned(E->Retain()); - // FIXME: The end of the type's source range is wrong - return getDerived().RebuildCXXFunctionalCastExpr( - /*FIXME:*/SourceRange(E->getTypeBeginLoc()), - NewT, + return getDerived().RebuildCXXFunctionalCastExpr(NewT, /*FIXME:*/E->getSubExpr()->getLocStart(), SubExpr.get(), E->getRParenLoc()); @@ -5222,20 +5204,18 @@ TreeTransform<Derived>::TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E) { template<typename Derived> ExprResult -TreeTransform<Derived>::TransformCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { - TemporaryBase Rebase(*this, E->getTypeBeginLoc(), DeclarationName()); - - QualType T = getDerived().TransformType(E->getType()); - if (T.isNull()) +TreeTransform<Derived>::TransformCXXScalarValueInitExpr( + CXXScalarValueInitExpr *E) { + TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo()); + if (!T) return ExprError(); - + if (!getDerived().AlwaysRebuild() && - T == E->getType()) + T == E->getTypeSourceInfo()) return SemaRef.Owned(E->Retain()); - return getDerived().RebuildCXXScalarValueInitExpr(E->getTypeBeginLoc(), - /*FIXME:*/E->getTypeBeginLoc(), - T, + return getDerived().RebuildCXXScalarValueInitExpr(T, + /*FIXME:*/T->getTypeLoc().getEndLoc(), E->getRParenLoc()); } @@ -5709,10 +5689,9 @@ TreeTransform<Derived>::TransformCXXExprWithTemporaries( template<typename Derived> ExprResult TreeTransform<Derived>::TransformCXXTemporaryObjectExpr( - CXXTemporaryObjectExpr *E) { - TemporaryBase Rebase(*this, E->getTypeBeginLoc(), DeclarationName()); - QualType T = getDerived().TransformType(E->getType()); - if (T.isNull()) + CXXTemporaryObjectExpr *E) { + TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo()); + if (!T) return ExprError(); CXXConstructorDecl *Constructor @@ -5742,26 +5721,17 @@ TreeTransform<Derived>::TransformCXXTemporaryObjectExpr( } if (!getDerived().AlwaysRebuild() && - T == E->getType() && + T == E->getTypeSourceInfo() && Constructor == E->getConstructor() && !ArgumentChanged) { // FIXME: Instantiation-specific - SemaRef.MarkDeclarationReferenced(E->getTypeBeginLoc(), Constructor); + SemaRef.MarkDeclarationReferenced(E->getLocStart(), Constructor); return SemaRef.MaybeBindToTemporary(E->Retain()); } - - // FIXME: Bogus location information - SourceLocation CommaLoc; - if (Args.size() > 1) { - Expr *First = (Expr *)Args[0]; - CommaLoc - = SemaRef.PP.getLocForEndOfToken(First->getSourceRange().getEnd()); - } - return getDerived().RebuildCXXTemporaryObjectExpr(E->getTypeBeginLoc(), - T, - /*FIXME:*/E->getTypeBeginLoc(), + + return getDerived().RebuildCXXTemporaryObjectExpr(T, + /*FIXME:*/T->getTypeLoc().getEndLoc(), move_arg(Args), - &CommaLoc, E->getLocEnd()); } @@ -5769,14 +5739,12 @@ template<typename Derived> ExprResult TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr( CXXUnresolvedConstructExpr *E) { - TemporaryBase Rebase(*this, E->getTypeBeginLoc(), DeclarationName()); - QualType T = getDerived().TransformType(E->getTypeAsWritten()); - if (T.isNull()) + TypeSourceInfo *T = getDerived().TransformType(E->getTypeSourceInfo()); + if (!T) return ExprError(); bool ArgumentChanged = false; ASTOwningVector<Expr*> Args(SemaRef); - llvm::SmallVector<SourceLocation, 8> FakeCommaLocs; for (CXXUnresolvedConstructExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end(); Arg != ArgEnd; ++Arg) { @@ -5785,22 +5753,18 @@ TreeTransform<Derived>::TransformCXXUnresolvedConstructExpr( return ExprError(); ArgumentChanged = ArgumentChanged || TransArg.get() != *Arg; - FakeCommaLocs.push_back( - SemaRef.PP.getLocForEndOfToken((*Arg)->getLocEnd())); Args.push_back(TransArg.get()); } if (!getDerived().AlwaysRebuild() && - T == E->getTypeAsWritten() && + T == E->getTypeSourceInfo() && !ArgumentChanged) return SemaRef.Owned(E->Retain()); // FIXME: we're faking the locations of the commas - return getDerived().RebuildCXXUnresolvedConstructExpr(E->getTypeBeginLoc(), - T, + return getDerived().RebuildCXXUnresolvedConstructExpr(T, E->getLParenLoc(), move_arg(Args), - FakeCommaLocs.data(), E->getRParenLoc()); } |