diff mbox

C++ PATCH to compound literal/T{} semantics

Message ID 4C846FE5.4030508@redhat.com
State New
Headers show

Commit Message

Jason Merrill Sept. 6, 2010, 4:36 a.m. UTC
The C++ T{} expression uses the same code path as C99 compound literals, 
since both are applying a type to a brace-enclosed initializer list.  We 
switched to using a TARGET_EXPR for these a while back except at file 
scope.  A recent constexpr bug report caused me to realize that we ought 
to do the same there as well; before this patch the enclosed testcase 
creates a static temporary for the T{} expression, but it is never used 
because the expression is folded into the variable it initializes.

Tested x86_64-pc-linux-gnu, applied to trunk.
diff mbox

Patch

commit 1bd8ee20b2b525a124b728bdf63dd4be7ac69168
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Sep 6 00:07:43 2010 -0400

    	* semantics.c (finish_compound_literal): Always build a
    	TARGET_EXPR.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 0afa8f9..17c795f 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -2203,20 +2203,7 @@  finish_compound_literal (tree type, tree compound_literal)
   if (TREE_CODE (type) == ARRAY_TYPE)
     cp_complete_array_type (&type, compound_literal, false);
   compound_literal = digest_init (type, compound_literal);
-  if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
-      && initializer_constant_valid_p (compound_literal, type))
-    {
-      tree decl = create_temporary_var (type);
-      DECL_INITIAL (decl) = compound_literal;
-      TREE_STATIC (decl) = 1;
-      cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
-      decl = pushdecl_top_level (decl);
-      DECL_NAME (decl) = make_anon_name ();
-      SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
-      return decl;
-    }
-  else
-    return get_target_expr (compound_literal);
+  return get_target_expr (compound_literal);
 }
 
 /* Return the declaration for the function-name variable indicated by
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist43.C b/gcc/testsuite/g++.dg/cpp0x/initlist43.C
new file mode 100644
index 0000000..72a09bd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist43.C
@@ -0,0 +1,7 @@ 
+// Test that using T{} at file scope doesn't create a static temporary.
+// { dg-options -std=c++0x }
+// { dg-final { scan-assembler-not "local" } }
+
+struct A { };
+
+A a = A{};