public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Battle of the comptypes (PR c++/35049)
@ 2008-02-05 17:53 Doug Gregor
  2008-02-06 15:51 ` H.J. Lu
                   ` (3 more replies)
  0 siblings, 4 replies; 28+ messages in thread
From: Doug Gregor @ 2008-02-05 17:53 UTC (permalink / raw)
  To: GCC Patches

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

PR c++/35049 is a 4.3 regression where we are failing to emit an error
when performing addition of vector types that don't have the same base
type. The bug seems to have been triggered by a recent change in the
canonical types system, but in fact it's been a ticking time bomb for
a while. The bug shows up on i386-apple-darwin9.1.0 and (for some
people) on i686-pc-linux-gnu

The C front end declares the function comptypes like this, in c-tree.h:

  extern int comptypes(tree, tree);

Parts of the C front end and the C/C++ commit bits use this comptypes.
However, this comptypes actually resides in c-typeck.c, which is not
linked into the C++ compiler.

The C++ front end declares comptypes like this, in cp-tree.h:

  extern int comptypes(tree, tree, int);

That int is the "strict" parameter, that tells the routine how to
compare the types, e.g., strict comparison, checking for base and
derived, derived and base, or redeclarations.

The fun comes in where the C/C++ common bits call comptypes with 2
arguments (as they should), but that ends up calling the C++
comptypes... with random data for the "strict" argument. So sometimes
we'll get strict comparisons (if that parameter is zero), sometimes
we'll get comparisons based on derived/base matching, etc. Most likely
this didn't matter before because the C bits don't deal with such C++
types often, but in the case of this bug we're thwarting the canonical
type system (which is only enabled with strict==COMPARE_STRICT) and
getting the wrong answer from comptypes.

This patch just renames "comptypes" in the C++ front end to
"cp_comptypes", and adds a 2-argument "comptypes" function to the C++
front end that accepts calls from the C/C++ common bits and forwards
on to cp_comptypes.

Tested i386-apple-darwin9.1.0; okay for mainline?

  - Doug

2008-02-05  Douglas Gregor  <doug.gregor@gmail.com>

	PR c++/35049
	* typeck.c (structural_comptypes): Call cp_comptypes.
	(comptypes): New; called from the C/C++ common bits to perform
	strict checks.
	(cp_comptypes): Renamed from comptypes, which is already used,
	with a different signature, by the C++ front end.
	(build_reinterpret_cast_1): Call cp_comptypes.
	(ptr_reasonably_similar): Ditto.
	* decl.c (decls_match): Ditto.
	* cvt.c (convert_to_reference): Ditto.
	* cp-tree.h (same_type_p): Ditto.
	(same_or_base_type_p): Ditto.
	(comptypes): Rename to cp_comptypes.
	* pt.c (canonical_type_parameter): Call cp_comptypes.

[-- Attachment #2: cp_comptypes.patch --]
[-- Type: application/octet-stream, Size: 8111 bytes --]

Index: typeck.c
===================================================================
--- typeck.c	(revision 132121)
+++ typeck.c	(working copy)
@@ -160,7 +160,7 @@ type_unknown_p (const_tree exp)
 
 \f
 /* Return the common type of two parameter lists.
-   We assume that comptypes has already been done and returned 1;
+   We assume that cp_comptypes has already been done and returned 1;
    if that isn't so, this may crash.
 
    As an optimization, free the space we allocate if the parameter
@@ -573,7 +573,7 @@ composite_pointer_type (tree t1, tree t2
 }
 
 /* Return the merged type of two types.
-   We assume that comptypes has already been done and returned 1;
+   We assume that cp_comptypes has already been done and returned 1;
    if that isn't so, this may crash.
 
    This just combines attributes and default arguments; any other
@@ -735,7 +735,7 @@ merge_types (tree t1, tree t2)
 }
 
 /* Return the common type of two types.
-   We assume that comptypes has already been done and returned 1;
+   We assume that cp_comptypes has already been done and returned 1;
    if that isn't so, this may crash.
 
    This is the type for the result of most arithmetic operations
@@ -926,7 +926,7 @@ comp_array_types (const_tree t1, const_t
   return true;
 }
 
-/* Subroutine in comptypes.  */
+/* Subroutine in cp_comptypes.  */
 
 static bool
 structural_comptypes (tree t1, tree t2, int strict)
@@ -1034,8 +1034,8 @@ structural_comptypes (tree t1, tree t2, 
       return false;
 
     case OFFSET_TYPE:
-      if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
-		      strict & ~COMPARE_REDECLARATION))
+      if (!cp_comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
+                         strict & ~COMPARE_REDECLARATION))
 	return false;
       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
 	return false;
@@ -1123,11 +1123,22 @@ structural_comptypes (tree t1, tree t2, 
   return targetm.comp_type_attributes (t1, t2);
 }
 
+extern int comptypes (tree, tree);
+
+/* Type comparison function that matches the signature of comptypes
+   from c-tree.h, which is used by the C front end and some of the
+   C/C++ common bits.  */
+int
+comptypes (tree t1, tree t2)
+{
+  return cp_comptypes (t1, t2, COMPARE_STRICT);
+}
+
 /* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
    is a bitwise-or of the COMPARE_* flags.  */
 
 bool
-comptypes (tree t1, tree t2, int strict)
+cp_comptypes (tree t1, tree t2, int strict)
 {
   if (strict == COMPARE_STRICT)
     {
@@ -1224,7 +1235,7 @@ comp_cv_qual_signature (tree type1, tree
     return 0;
 }
 \f
-/* Subroutines of `comptypes'.  */
+/* Subroutines of `cp_comptypes'.  */
 
 /* Return true if two parameter type lists PARMS1 and PARMS2 are
    equivalent in the sense that functions with those parameter types
@@ -5249,8 +5260,8 @@ build_reinterpret_cast_1 (tree type, tre
 	 "B" are related class types; the reinterpret_cast does not
 	 adjust the pointer.  */
       if (TYPE_PTR_P (intype)
-	  && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
-			 COMPARE_BASE | COMPARE_DERIVED)))
+	  && (cp_comptypes (TREE_TYPE (intype), TREE_TYPE (type),
+                            COMPARE_BASE | COMPARE_DERIVED)))
 	warning (0, "casting %qT to %qT does not dereference pointer",
 		 intype, type);
 
@@ -6913,9 +6924,9 @@ ptr_reasonably_similar (const_tree to, c
 	return 0;
 
       if (TREE_CODE (from) == OFFSET_TYPE
-	  && comptypes (TYPE_OFFSET_BASETYPE (to),
-			TYPE_OFFSET_BASETYPE (from),
-			COMPARE_BASE | COMPARE_DERIVED))
+	  && cp_comptypes (TYPE_OFFSET_BASETYPE (to),
+                           TYPE_OFFSET_BASETYPE (from),
+                           COMPARE_BASE | COMPARE_DERIVED))
 	continue;
 
       if (TREE_CODE (to) == VECTOR_TYPE
@@ -6930,7 +6941,7 @@ ptr_reasonably_similar (const_tree to, c
 	return 1;
 
       if (TREE_CODE (to) != POINTER_TYPE)
-	return comptypes
+	return cp_comptypes
 	  (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
 	   COMPARE_BASE | COMPARE_DERIVED);
     }
Index: decl.c
===================================================================
--- decl.c	(revision 132121)
+++ decl.c	(working copy)
@@ -1015,9 +1015,9 @@ decls_match (tree newdecl, tree olddecl)
       else if (TREE_TYPE (newdecl) == NULL_TREE)
 	types_match = 0;
       else
-	types_match = comptypes (TREE_TYPE (newdecl),
-				 TREE_TYPE (olddecl),
-				 COMPARE_REDECLARATION);
+	types_match = cp_comptypes (TREE_TYPE (newdecl),
+                                    TREE_TYPE (olddecl),
+                                    COMPARE_REDECLARATION);
     }
 
   return types_match;
Index: cvt.c
===================================================================
--- cvt.c	(revision 132121)
+++ cvt.c	(working copy)
@@ -465,8 +465,8 @@ convert_to_reference (tree reftype, tree
       /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
 	 meant.  */
       if (TREE_CODE (intype) == POINTER_TYPE
-	  && (comptypes (TREE_TYPE (intype), type,
-			 COMPARE_BASE | COMPARE_DERIVED)))
+	  && (cp_comptypes (TREE_TYPE (intype), type,
+                            COMPARE_BASE | COMPARE_DERIVED)))
 	warning (0, "casting %qT to %qT does not dereference pointer",
 		 intype, reftype);
 
@@ -604,7 +604,7 @@ ocp_convert (tree type, tree expr, int c
 	/* The call to fold will not always remove the NOP_EXPR as
 	   might be expected, since if one of the types is a typedef;
 	   the comparison in fold is just equality of pointers, not a
-	   call to comptypes.  We don't call fold in this case because
+	   call to cp_comptypes.  We don't call fold in this case because
 	   that can result in infinite recursion; fold will call
 	   convert, which will call ocp_convert, etc.  */
 	return e;
Index: cp-tree.h
===================================================================
--- cp-tree.h	(revision 132121)
+++ cp-tree.h	(working copy)
@@ -281,7 +281,7 @@ typedef struct ptrmem_cst * ptrmem_cst_t
 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, in the usual
    sense of `same'.  */
 #define same_type_p(TYPE1, TYPE2) \
-  comptypes ((TYPE1), (TYPE2), COMPARE_STRICT)
+  cp_comptypes ((TYPE1), (TYPE2), COMPARE_STRICT)
 
 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
    top-level qualifiers.  */
@@ -3744,7 +3744,7 @@ enum overload_flags { NO_SPECIAL = 0, DT
 #define WANT_VECTOR    32 /* vector types */
 #define WANT_ARITH	(WANT_INT | WANT_FLOAT | WANT_VECTOR)
 
-/* Used with comptypes, and related functions, to guide type
+/* Used with cp_comptypes, and related functions, to guide type
    comparison.  */
 
 #define COMPARE_STRICT	      0 /* Just check if the types are the
@@ -3780,7 +3780,7 @@ enum overload_flags { NO_SPECIAL = 0, DT
    is derived from TYPE1, or if TYPE2 is a pointer (reference) to a
    class derived from the type pointed to (referred to) by TYPE1.  */
 #define same_or_base_type_p(TYPE1, TYPE2) \
-  comptypes ((TYPE1), (TYPE2), COMPARE_BASE)
+  cp_comptypes ((TYPE1), (TYPE2), COMPARE_BASE)
 
 /* These macros are used to access a TEMPLATE_PARM_INDEX.  */
 #define TEMPLATE_PARM_INDEX_CAST(NODE) \
@@ -4764,7 +4764,7 @@ extern tree complete_type			(tree);
 extern tree complete_type_or_else		(tree, tree);
 extern int type_unknown_p			(const_tree);
 extern bool comp_except_specs			(const_tree, const_tree, bool);
-extern bool comptypes				(tree, tree, int);
+extern bool cp_comptypes			(tree, tree, int);
 extern bool compparms				(const_tree, const_tree);
 extern int comp_cv_qualification		(const_tree, const_tree);
 extern int comp_cv_qual_signature		(tree, tree);
Index: pt.c
===================================================================
--- pt.c	(revision 132121)
+++ pt.c	(working copy)
@@ -2905,7 +2905,7 @@ canonical_type_parameter (tree type)
     VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
 
   list = VEC_index (tree, canonical_template_parms, idx);
-  while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
+  while (list && !cp_comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
     list = TREE_CHAIN (list);
 
   if (list)

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2008-02-11 12:23 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-05 17:53 [C++ PATCH] Battle of the comptypes (PR c++/35049) Doug Gregor
2008-02-06 15:51 ` H.J. Lu
2008-02-06 15:59   ` Doug Gregor
2008-02-06 16:48     ` H.J. Lu
2008-02-06 22:51 ` Andreas Tobler
2008-02-06 22:55   ` Manuel López-Ibáñez
2008-02-06 23:16     ` Andrew Pinski
2008-02-07  8:59 ` Paolo Bonzini
2008-02-07 13:46   ` [C/C++/ObjC/ObjC++ " Paolo Bonzini
2008-02-07 13:52     ` H.J. Lu
2008-02-07 14:59       ` Paolo Bonzini
2008-02-07 15:05         ` H.J. Lu
2008-02-07 15:07           ` Doug Gregor
2008-02-07 15:07           ` Jakub Jelinek
2008-02-07 15:47             ` Richard Guenther
2008-02-07 15:57               ` Doug Gregor
2008-02-07 16:22                 ` Paolo Bonzini
2008-02-11  4:58     ` Mark Mitchell
2008-02-11 12:23       ` Joseph S. Myers
2008-02-11 13:33         ` Paolo Bonzini
2008-02-07 20:49   ` [C++ " Joseph S. Myers
2008-02-08 10:42     ` Paolo Bonzini
2008-02-08 15:49       ` Joseph S. Myers
2008-02-08 15:59         ` Doug Gregor
2008-02-08 16:37           ` Paolo Bonzini
2008-02-08 20:02           ` Joseph S. Myers
2008-02-07 18:54 ` Andreas Krebbel
2008-02-07 19:06   ` Doug Gregor

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).