From: Anders Carlsson Date: Sat, 30 May 2009 20:36:53 +0000 (+0000) Subject: Add Sema::MaybeBindToTemporary which takes an expression and (if needed) wraps it... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=def11996fb558f58a8a7ae87aa95eb0bb96bfe4a;p=clang Add Sema::MaybeBindToTemporary which takes an expression and (if needed) wraps it in a CXXBindTemporaryExpr. Use this when creating CXXTemporaryObjectExprs. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72629 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 6214eb5a11..f630e1d6fb 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1494,7 +1494,12 @@ public: CXXConstructorDecl *Constructor, QualType DeclInitType, Expr **Exprs, unsigned NumExprs); - + + /// MaybeBindToTemporary - If the passed in expression has a record type with + /// a non-trivial destructor, this will return CXXBindTemporaryExpr. Otherwise + /// it simply returns the passed in expression. + OwningExprResult MaybeBindToTemporary(Expr *E); + /// InitializationKind - Represents which kind of C++ initialization /// [dcl.init] a routine is to perform. enum InitializationKind { diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 923aa1d742..b6ccb99d6b 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -207,10 +207,11 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, ExprTemporaries.push_back(Temp); exprs.release(); - return Owned(new (Context) CXXTemporaryObjectExpr(Context, Temp, - Constructor, Ty, - TyBeginLoc, Exprs, - NumExprs, RParenLoc)); + + Expr *E = new (Context) CXXTemporaryObjectExpr(Context, Temp, Constructor, + Ty, TyBeginLoc, Exprs, + NumExprs, RParenLoc); + return MaybeBindToTemporary(E); } // Fall through to value-initialize an object of class type that @@ -1529,6 +1530,22 @@ QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { return QualType(); } +Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { + const RecordType *RT = E->getType()->getAsRecordType(); + if (!RT) + return Owned(E); + + CXXRecordDecl *RD = cast(RT->getDecl()); + if (RD->hasTrivialDestructor()) + return Owned(E); + + CXXTemporary *Temp = CXXTemporary::Create(Context, + RD->getDestructor(Context)); + + // FIXME: Add the temporary to the temporaries vector. + return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); +} + Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { Expr *FullExpr = Arg.takeAs(); assert(FullExpr && "Null full expr!");