This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [3.4 PATCH] PR c++/20995: Type-unsafe trees for templates


On Sun, 24 Apr 2005, Mark Mitchell wrote:
> I agree that backporting fold_if_not_in_template along with all of its
> uses would be too risky.  But how about just backporting the function
> and using it from the two places you identified?  I expect that would
> work, and would be slightly preferable in that it would reduce
> divergence.  That change is preapproved, assuming it works OK.

Many thanks again.  For the record, here's the patch that I've just
committed to the gcc-3_4-branch to resolve this issue.  The new test
case is identical to that posted previously.  I'll also commit this
testcase to mainline to prevent this problem resurfacing in the future.

Tested against the gcc-3_4-branch on i686-pc-linux-gnu with a full
"make bootstrap", all default languages, and regression tested with
a top-level "make -k check" with no new failures.


2005-04-25  Roger Sayle  <roger@eyesopen.com>
	    Mark Mitchell  <mark@codesourcery.com>

	PR c++/20995
	Partial backport from mainline.
	2004-09-27  Mark Mitchell  <mark@codesourcery.com>
	* tree.c (fold_if_not_in_template): New function.
	* cp-tree.h (fold_if_not_in_template): Prototype here.
	* call.c (build_conditional_expr): Use fold_if_not_in_template.
	* typeck.c (build_binary_op): Likewise.

	* g++.dg/opt/pr20995-1.C: New test case.


Index: tree.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/tree.c,v
retrieving revision 1.360.4.11
diff -c -3 -p -r1.360.4.11 tree.c
*** tree.c	6 Apr 2005 15:35:30 -0000	1.360.4.11
--- tree.c	25 Apr 2005 03:09:52 -0000
***************
*** 1,6 ****
  /* Language-dependent node constructors for parse phase of GNU compiler.
     Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
     Hacked by Michael Tiemann (tiemann@cygnus.com)

  This file is part of GCC.
--- 1,6 ----
  /* Language-dependent node constructors for parse phase of GNU compiler.
     Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     Hacked by Michael Tiemann (tiemann@cygnus.com)

  This file is part of GCC.
*************** stabilize_init (tree init, tree *initp)
*** 2526,2531 ****
--- 2526,2544 ----
    return true;
  }

+ /* Like "fold", but should be used whenever we might be processing the
+    body of a template.  */
+
+ tree
+ fold_if_not_in_template (tree expr)
+ {
+   /* In the body of a template, there is never any need to call
+      "fold".  We will call fold later when actually instantiating the
+      template.  Integral constant expressions in templates will be
+      evaluated via fold_non_dependent_expr, as necessary.  */
+   return (processing_template_decl ? expr : fold (expr));
+ }
+

  #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
  /* Complain that some language-specific thing hanging off a tree
Index: cp-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.h,v
retrieving revision 1.946.4.18
diff -c -3 -p -r1.946.4.18 cp-tree.h
*** cp-tree.h	12 Feb 2005 16:29:11 -0000	1.946.4.18
--- cp-tree.h	25 Apr 2005 03:09:53 -0000
***************
*** 1,6 ****
  /* Definitions for C++ parsing and type checking.
     Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
!    2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
     Contributed by Michael Tiemann (tiemann@cygnus.com)

  This file is part of GCC.
--- 1,6 ----
  /* Definitions for C++ parsing and type checking.
     Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
!    2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     Contributed by Michael Tiemann (tiemann@cygnus.com)

  This file is part of GCC.
*************** extern int cp_cannot_inline_tree_fn (tre
*** 4194,4199 ****
--- 4194,4200 ----
  extern tree cp_add_pending_fn_decls (void*,tree);
  extern int cp_is_overload_p (tree);
  extern int cp_auto_var_in_fn_p (tree,tree);
+ extern tree fold_if_not_in_template (tree);
  extern tree cp_copy_res_decl_for_inlining (tree, tree, tree, void*,
  						   int*, tree);

Index: call.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/call.c,v
retrieving revision 1.452.2.24
diff -c -3 -p -r1.452.2.24 call.c
*** call.c	4 Apr 2005 05:45:35 -0000	1.452.2.24
--- call.c	25 Apr 2005 03:09:55 -0000
***************
*** 1,6 ****
  /* Functions related to invoking methods and overloaded functions.
     Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
     Contributed by Michael Tiemann (tiemann@cygnus.com) and
     modified by Brendan Kehoe (brendan@cygnus.com).

--- 1,6 ----
  /* Functions related to invoking methods and overloaded functions.
     Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     Contributed by Michael Tiemann (tiemann@cygnus.com) and
     modified by Brendan Kehoe (brendan@cygnus.com).

*************** build_conditional_expr (tree arg1, tree
*** 3278,3284 ****
      }

   valid_operands:
!   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.  */

--- 3278,3285 ----
      }

   valid_operands:
!   result = fold_if_not_in_template (build (COND_EXPR, result_type,
! 					   arg1, arg2, arg3));
    /* We can't use result_type below, as fold might have returned a
       throw_expr.  */

Index: typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.519.2.24
diff -c -3 -p -r1.519.2.24 typeck.c
*** typeck.c	17 Apr 2005 03:37:15 -0000	1.519.2.24
--- typeck.c	25 Apr 2005 03:09:56 -0000
***************
*** 1,6 ****
  /* Build expressions with type checking for C++ compiler.
     Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
     Hacked by Michael Tiemann (tiemann@cygnus.com)

  This file is part of GCC.
--- 1,6 ----
  /* Build expressions with type checking for C++ compiler.
     Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     Hacked by Michael Tiemann (tiemann@cygnus.com)

  This file is part of GCC.
*************** build_binary_op (enum tree_code code, tr
*** 3458,3464 ****
      tree result = build (resultcode, build_type, op0, op1);
      tree folded;

!     folded = fold (result);
      if (folded == result)
        TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
      if (final_type != 0)
--- 3458,3464 ----
      tree result = build (resultcode, build_type, op0, op1);
      tree folded;

!     folded = fold_if_not_in_template (result);
      if (folded == result)
        TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
      if (final_type != 0)


Roger
--


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]