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]

Re: C++ PATCH for PTRMEM_CSTs


>>>>> "Jason" == Jason Merrill <jason@cygnus.com> writes:

    Jason> Huh?

    Jason>   5.19 - Constant expressions [expr.const]

    Jason>   -6- A pointer to member constant expression shall be
    Jason> created using the unary & operator applied to a
    Jason> qualified-id operand (expr.unary.op), optionally preceded
    Jason> by a pointer to member cast (expr.static.cast).

Yup, that comment was badly worded.  I've checked in the following
patch which improves our pointer-to-member handling a bit more (fixing
the XFAIL on g++.pt/ptrmem6.C).  This patch only on the mainline.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

1999-05-22  Mark Mitchell  <mark@codesourcery.com>

	* cp-tree.h (cplus_expand_constant): Declare.
	* cvt.c (convert_to_pointer): Expand PTRMEM_CSTs when they're
	converted from one pointer-to-object type to another.
	* expr.c (cplus_expand_constant): Don't make it static.
	* typeck.c (build_component_ref): Don't crash when presented with
	a component which is a TEMPLATE_DECL.
	(build_ptrmemfunc): Tidy.  Clarify comment.  Make sure that even a
	cast from a pointer-to-member constant to its own type does not
	result in a valid non-type template argument.

Index: testsuite/g++.old-deja/g++.pt/crash42.C
===================================================================
RCS file: crash42.C
diff -N crash42.C
--- /dev/null	Sat Dec  5 20:30:03 1998
+++ crash42.C	Sat May 22 01:15:46 1999
@@ -0,0 +1,14 @@
+// Build don't link:
+// Origin: Walter Brisken <walterfb@puppsr14.princeton.edu>
+
+template <class T> class list {};
+
+class newtype
+{
+};
+
+void crash()
+{
+  newtype* n;
+  n->list.size (); // ERROR - invalid use of template
+}
Index: testsuite/g++.old-deja/g++.pt/ptrmem6.C
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/testsuite/g++.old-deja/g++.pt/ptrmem6.C,v
retrieving revision 1.1
diff -u -p -r1.1 ptrmem6.C
--- ptrmem6.C	1999/03/23 00:01:36	1.1
+++ ptrmem6.C	1999/05/22 08:15:46
@@ -23,7 +23,7 @@ int main() {
   h<&A::i>();
   g<&B::f>(); // ERROR - 
   h<&B::j>(); // ERROR - 
-  g<(void (A::*)()) &A::f>(); // ERROR - XFAIL *-*-*
+  g<(void (A::*)()) &A::f>(); // ERROR -
   h<(int A::*) &A::i>(); // ERROR - 
   g<(void (A::*)()) &B::f>(); // ERROR - 
   h<(int A::*) &B::j>(); // ERROR - 
Index: cp/cp-tree.h
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/cp-tree.h,v
retrieving revision 1.234
diff -u -p -r1.234 cp-tree.h
--- cp-tree.h	1999/05/21 15:55:46	1.234
+++ cp-tree.h	1999/05/22 08:15:49
@@ -3007,6 +3007,7 @@ extern void init_cplus_expand			PROTO((v
 extern void fixup_result_decl			PROTO((tree, struct rtx_def *));
 extern int extract_init				PROTO((tree, tree));
 extern void do_case				PROTO((tree, tree));
+extern tree cplus_expand_constant               PROTO((tree));
 
 /* friend.c */
 extern int is_friend				PROTO((tree, tree));
Index: cp/cvt.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/cvt.c,v
retrieving revision 1.58
diff -u -p -r1.58 cvt.c
--- cvt.c	1999/05/10 12:12:48	1.58
+++ cvt.c	1999/05/22 08:15:50
@@ -179,19 +179,29 @@ cp_convert_to_pointer (type, expr)
 
       if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
 	{
-	  tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
-	  tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
-	  tree binfo = get_binfo (b2, b1, 1);
-	  enum tree_code code = PLUS_EXPR;
+	  tree b1; 
+	  tree b2;
+	  tree binfo;
+	  enum tree_code code;
 
+	  b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
+	  b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
+	  binfo = get_binfo (b2, b1, 1);
+
 	  if (binfo == NULL_TREE)
 	    {
 	      binfo = get_binfo (b1, b2, 1);
 	      code = MINUS_EXPR;
 	    }
+	  else
+	    code = PLUS_EXPR;
 
 	  if (binfo == error_mark_node)
 	    return error_mark_node;
+
+	  if (TREE_CODE (expr) == PTRMEM_CST)
+	    expr = cplus_expand_constant (expr);
+
 	  if (binfo && ! TREE_VIA_VIRTUAL (binfo))
 	    expr = size_binop (code, expr, BINFO_OFFSET (binfo));
 	}
Index: cp/expr.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/expr.c,v
retrieving revision 1.23
diff -u -p -r1.23 expr.c
--- expr.c	1999/05/20 10:44:36	1.23
+++ expr.c	1999/05/22 08:15:50
@@ -39,7 +39,7 @@ static rtx cplus_expand_expr PROTO((tree
 /* Hook used by output_constant to expand language-specific
    constants.  */
 
-static tree
+tree
 cplus_expand_constant (cst)
      tree cst;
 {
Index: cp/typeck.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/typeck.c,v
retrieving revision 1.166
diff -u -p -r1.166 typeck.c
--- typeck.c	1999/05/21 15:42:55	1.166
+++ typeck.c	1999/05/22 08:16:00
@@ -2138,6 +2138,11 @@ build_component_ref (datum, component, b
       cp_error ("invalid use of type decl `%#D' as expression", component);
       return error_mark_node;
     }
+  else if (TREE_CODE (component) == TEMPLATE_DECL)
+    {
+      cp_error ("invalid use of template `%#D' as expression", component);
+      return error_mark_node;
+    }
   else
     {
       tree name = component;
@@ -6516,7 +6521,9 @@ build_ptrmemfunc (type, pfn, force)
      int force;
 {
   tree fn;
-  
+  tree pfn_type = TREE_TYPE (pfn);
+  tree to_type = build_ptrmemfunc_type (type);
+
   /* Handle multiple conversions of pointer to member functions.  */
   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
     {
@@ -6526,27 +6533,21 @@ build_ptrmemfunc (type, pfn, force)
       tree npfn = NULL_TREE;
       tree ndelta, ndelta2;
       tree e1, e2, e3, n;
-      tree pfn_type;
 
-      /* Is is already the right type? */
-      if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
-	return pfn;
-
-      pfn_type = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn));
-      if (!force
-	  && comp_target_types (type, pfn_type, 1) != 1)
-	cp_error ("conversion to `%T' from `%T'", type, pfn_type);
+      if (!force 
+	  && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
+	cp_error ("conversion to `%T' from `%T'", 
+		  to_type, pfn_type);
 
       if (TREE_CODE (pfn) == PTRMEM_CST)
 	{
 	  /* We could just build the resulting CONSTRUCTOR now, but we
 	     don't, relying on the general machinery below, together
 	     with constant-folding, to do the right thing.  We don't
-	     want to return a PTRMEM_CST here, even though we could,
-	     because a pointer-to-member constant ceases to be a
-	     constant (from the point of view of the language) when it
-	     is cast to another type.  */
-
+	     want to return a PTRMEM_CST here, since a
+	     pointer-to-member constant is no longer a valid template
+	     argument once it is cast to any type, including its
+	     original type.  */
 	  expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
 	  if (npfn)
 	    /* This constant points to a non-virtual function.
@@ -6554,6 +6555,12 @@ build_ptrmemfunc (type, pfn, force)
 	       matter since we won't use it anyhow.  */
 	    ndelta2 = integer_zero_node;
 	}
+      else if (same_type_p (to_type, pfn_type))
+	/* We don't have to do any conversion.  Note that we do this
+	   after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
+	   to its own type, will not be considered a legal non-type
+	   template argument.  */
+	return pfn;
       else
 	{
 	  ndelta = cp_convert (ptrdiff_type_node, 
@@ -6565,15 +6572,15 @@ build_ptrmemfunc (type, pfn, force)
 	  idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
 	}
 
-      n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type)),
-				TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
+      n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
+				TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
 				force);
       delta = build_binary_op (PLUS_EXPR, ndelta, n);
       delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
       e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
 	  
       /* If it's a virtual function, this is what we want.  */
-      e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
+      e2 = build_ptrmemfunc1 (to_type, delta, idx,
 			      NULL_TREE, delta2);
 
       pfn = PFN_FROM_PTRMEMFUNC (pfn);
@@ -6582,7 +6589,7 @@ build_ptrmemfunc (type, pfn, force)
 
       /* But if it's a non-virtual function, or NULL, we use this
 	 instead.  */
-      e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta,
+      e3 = build_ptrmemfunc1 (to_type, delta,
 			      idx, npfn, NULL_TREE);
       return build_conditional_expr (e1, e2, e3);
     }
@@ -6591,7 +6598,7 @@ build_ptrmemfunc (type, pfn, force)
   if (integer_zerop (pfn))
     {
       pfn = build_c_cast (type, integer_zero_node);
-      return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
+      return build_ptrmemfunc1 (to_type,
 				integer_zero_node, integer_zero_node,
 				pfn, NULL_TREE);
     }
@@ -6601,7 +6608,7 @@ build_ptrmemfunc (type, pfn, force)
 
   fn = TREE_OPERAND (pfn, 0);
   my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
-  return make_ptrmem_cst (build_ptrmemfunc_type (type), fn);
+  return make_ptrmem_cst (to_type, fn);
 }
 
 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
@@ -6621,8 +6628,7 @@ expand_ptrmemfunc_cst (cst, delta, idx, 
   my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
   
   *delta 
-    = get_delta_difference (TYPE_METHOD_BASETYPE 
-			    (TREE_TYPE (fn)),
+    = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (fn)),
 			    TYPE_PTRMEMFUNC_OBJECT_TYPE (type),
 			    /*force=*/0);
   if (!DECL_VIRTUAL_P (fn))


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