public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches List <gcc-patches@gcc.gnu.org>
Cc: Ville Voutilainen <ville.voutilainen@gmail.com>
Subject: C++ PATCH for P0135, C++17 guaranteed copy elision
Date: Wed, 05 Oct 2016 22:57:00 -0000	[thread overview]
Message-ID: <CADzB+2nUgkOKbdMM_XXL4e53HQ712TLmRszvwEJQmm591UobaQ@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 387 bytes --]

C++17 changes how we describe prvalues so that they express
initialization of an object to be named later, rather than objects
themselves.  This happens to match the front end's use of TARGET_EXPR
pretty closely, so I think we don't need to do much more than disable
the code that forces us to copy a TARGET_EXPR into another
TARGET_EXPR.

Tested x86_64-pc-linux-gnu, applying to trunk.

[-- Attachment #2: elision.diff --]
[-- Type: text/plain, Size: 1910 bytes --]

commit 054719b1c1f71236999ea4082cb3207c42cf883c
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Oct 5 12:59:30 2016 -0400

            Implement P0135R1, Guaranteed copy elision.
    
            * c-opts.c (set_std_cxx1z): Set flag_elide_constructors to 2.
            * cvt.c (ocp_convert): Don't re-copy a TARGET_EXPR in C++17.

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index c5a699d..977348f1 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -1579,6 +1579,7 @@ set_std_cxx1z (int iso)
   flag_isoc94 = 1;
   flag_isoc99 = 1;
   flag_isoc11 = 1;
+  flag_elide_constructors = 2;
   cxx_dialect = cxx1z;
   lang_hooks.name = "GNU C++14"; /* Pretend C++14 till standarization.  */
 }
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 2f5f15a..4de9745 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -693,8 +693,11 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
   if (error_operand_p (e))
     return error_mark_node;
 
-  if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
-    /* We need a new temporary; don't take this shortcut.  */;
+  if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP)
+      && !(flag_elide_constructors >= 2
+	   && TREE_CODE (e) == TARGET_EXPR))
+    /* We need a new temporary; don't take this shortcut.  But in C++17, don't
+       force a temporary if we already have one.  */;
   else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
     {
       if (same_type_p (type, TREE_TYPE (e)))
diff --git a/gcc/testsuite/g++.dg/cpp1z/elide1.C b/gcc/testsuite/g++.dg/cpp1z/elide1.C
new file mode 100644
index 0000000..a0538bb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/elide1.C
@@ -0,0 +1,16 @@
+// { dg-options -std=c++1z }
+
+struct A
+{
+  A();
+  A(const A&) = delete;
+};
+
+bool b;
+A a = A();
+A a1 = b ? A() : A();
+A a2 = (42, A());
+
+A f();
+A a3 = f();
+A a4 = b ? A() : f();

             reply	other threads:[~2016-10-05 22:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-05 22:57 Jason Merrill [this message]
2016-10-05 23:16 ` Pedro Alves
2016-10-05 23:22   ` Jason Merrill
2016-10-06 21:26     ` Jason Merrill
2016-10-08 16:27       ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADzB+2nUgkOKbdMM_XXL4e53HQ712TLmRszvwEJQmm591UobaQ@mail.gmail.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ville.voutilainen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).