init.c (decl_constant_value): Deal with COND_EXPR specially.
authorMark Mitchell <mark@codesourcery.com>
Fri, 29 Aug 2003 02:50:10 +0000 (02:50 +0000)
committerMark Mitchell <mmitchel@gcc.gnu.org>
Fri, 29 Aug 2003 02:50:10 +0000 (02:50 +0000)
* init.c (decl_constant_value): Deal with COND_EXPR specially.
* call.c (build_conditional_expr): Revert previous patch.

* g++.dg/expr/cond3.C: New test.

From-SVN: r70899

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/expr/cond3.C [new file with mode: 0644]

index 87857b249a70ca101c209d2db96e5e55fd7ed827..402b17720906eb6141a6cb2fbdd47abe51248108 100644 (file)
@@ -1,5 +1,8 @@
 2003-08-28  Mark Mitchell  <mark@codesourcery.com>
 
+       * init.c (decl_constant_value): Deal with COND_EXPR specially.
+       * call.c (build_conditional_expr): Revert previous patch.
+
        PR optimization/5079
        * call.c (build_conditional_expr): Use decl_constant_value to
        simplify the arguments.
index 739ce71f590d5c1e7edb8d1eb96715920ba45ae2..a74fd63d556d993d2f1996041051a98fb682a85f 100644 (file)
@@ -3358,8 +3358,6 @@ build_conditional_expr (tree arg1, tree arg2, tree arg3)
     }
 
  valid_operands:
-  arg2 = decl_constant_value (arg2);
-  arg3 = decl_constant_value (arg3);
   result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
   /* We can't use result_type below, as fold might have returned a
      throw_expr.  */
index 032959c7152316c571db9d55c44a5c1fc2fbaf9d..ace82cdc39f96fefc8cb138ecd3e62785671e60a 100644 (file)
@@ -1587,6 +1587,24 @@ build_offset_ref (tree type, tree name, bool address_p)
 tree
 decl_constant_value (tree decl)
 {
+  /* When we build a COND_EXPR, we don't know whether it will be used
+     as an lvalue or as an rvalue.  If it is an lvalue, it's not safe
+     to replace the second and third operands with their
+     initializers.  So, we do that here.  */
+  if (TREE_CODE (decl) == COND_EXPR)
+    {
+      tree d1;
+      tree d2;
+
+      d1 = decl_constant_value (TREE_OPERAND (decl, 1));
+      d2 = decl_constant_value (TREE_OPERAND (decl, 2));
+
+      if (d1 != TREE_OPERAND (decl, 1) || d2 != TREE_OPERAND (decl, 2))
+       return build (COND_EXPR,
+                     TREE_TYPE (decl),
+                     TREE_OPERAND (decl, 0), d1, d2);
+    }
+
   if (TREE_READONLY_DECL_P (decl)
       && ! TREE_THIS_VOLATILE (decl)
       && DECL_INITIAL (decl)
index 3c76266f983f48dd4a9ef1916048053356840a99..ea39a5d6cacfb50b2fffe3eb60e15dae47576eb3 100644 (file)
@@ -1,3 +1,7 @@
+2003-08-28  Mark Mitchell  <mark@codesourcery.com>
+
+       * g++.dg/expr/cond3.C: New test.
+
 2003-08-28  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
        * gcc.dg/builtins-1.c: Add new builtin cases.
diff --git a/gcc/testsuite/g++.dg/expr/cond3.C b/gcc/testsuite/g++.dg/expr/cond3.C
new file mode 100644 (file)
index 0000000..50a4d9a
--- /dev/null
@@ -0,0 +1,6 @@
+const int i = 7;
+const int j = 3;
+
+void f(bool b) {
+  &(b ? i : j);
+}