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 6214eb5..f630e1d 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -1494,7 +1494,12 @@
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 923aa1d..b6ccb99 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -207,10 +207,11 @@
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 @@
return QualType();
}
+Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) {
+ const RecordType *RT = E->getType()->getAsRecordType();
+ if (!RT)
+ return Owned(E);
+
+ CXXRecordDecl *RD = cast<CXXRecordDecl>(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<Expr>();
assert(FullExpr && "Null full expr!");