diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-05-15 00:13:29 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-05-15 00:13:29 +0000 |
commit | 222cf0efbc174528022b6d51300247ae170a8b6d (patch) | |
tree | d15badb9890db68a222bb3bc0f9b8cba2853d19e /clang/lib/Sema/SemaInit.cpp | |
parent | 36d4d1541c49843e062a5c64d2fd65b9e9b98ac7 (diff) | |
download | bcm5719-llvm-222cf0efbc174528022b6d51300247ae170a8b6d.tar.gz bcm5719-llvm-222cf0efbc174528022b6d51300247ae170a8b6d.zip |
Recognize when the named return value optimization applies in a
"return" statement and mark the corresponding CXXConstructExpr as
elidable. Teach CodeGen that eliding a temporary is different from
eliding an object construction.
This is just a baby step toward NRVO.
llvm-svn: 103849
Diffstat (limited to 'clang/lib/Sema/SemaInit.cpp')
-rw-r--r-- | clang/lib/Sema/SemaInit.cpp | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 851fb9e8353..06d202d3e09 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -1984,6 +1984,26 @@ DeclaratorDecl *InitializedEntity::getDecl() const { return 0; } +bool InitializedEntity::allowsNRVO() const { + switch (getKind()) { + case EK_Result: + case EK_Exception: + return LocAndNRVO.NRVO; + + case EK_Variable: + case EK_Parameter: + case EK_Member: + case EK_New: + case EK_Temporary: + case EK_Base: + case EK_ArrayElement: + case EK_VectorElement: + break; + } + + return false; +} + //===----------------------------------------------------------------------===// // Initialization sequence //===----------------------------------------------------------------------===// @@ -3242,9 +3262,9 @@ static Sema::OwningExprResult CopyObject(Sema &S, // directly into the target of the omitted copy/move // // Note that the other three bullets are handled elsewhere. Copy - // elision for return statements and throw expressions are (FIXME: - // not yet) handled as part of constructor initialization, while - // copy elision for exception handlers is handled by the run-time. + // elision for return statements and throw expressions are handled as part + // of constructor initialization, while copy elision for exception handlers + // is handled by the run-time. bool Elidable = CurInitExpr->isTemporaryObject() && S.Context.hasSameUnqualifiedType(T, CurInitExpr->getType()); SourceLocation Loc; @@ -3737,7 +3757,7 @@ InitializationSequence::Perform(Sema &S, unsigned NumArgs = Args.size(); CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Step->Function.Function); - + // Build a call to the selected constructor. ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S); SourceLocation Loc = Kind.getLocation(); @@ -3774,11 +3794,21 @@ InitializationSequence::Perform(Sema &S, CXXConstructExpr::CK_VirtualBase : CXXConstructExpr::CK_NonVirtualBase; } - CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), - Constructor, - move_arg(ConstructorArgs), - ConstructorInitRequiresZeroInit, - ConstructKind); + + // If the entity allows NRVO, mark the construction as elidable + // unconditionally. + if (Entity.allowsNRVO()) + CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), + Constructor, /*Elidable=*/true, + move_arg(ConstructorArgs), + ConstructorInitRequiresZeroInit, + ConstructKind); + else + CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), + Constructor, + move_arg(ConstructorArgs), + ConstructorInitRequiresZeroInit, + ConstructKind); } if (CurInit.isInvalid()) return S.ExprError(); |