This is the mail archive of the gcc@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]

[patch] duplicate convert_and_check in the c++ front end


The attached patch duplicates convert_and_check into the c++ front
end. I would like to include it to be able to remove the convert
callback latter on. Duplicating convert_and_check makes it is easy to
use cxx_convert in the c++ front end and c_convert in the c front end.

Thanks,
Rafael

2006-04-03 Rafael Ãvila de EspÃndola  <rafael.espindola@gmail.com>

                * gcc/cp/call.c (constant_fits_type_p): New function
                   (cxx_convert_and_check): New function
                   (convert_like_real): Use cxx_convert_and_check
instead of convert_and_check
Index: gcc/cp/call.c
===================================================================
--- gcc/cp/call.c	(revision 112530)
+++ gcc/cp/call.c	(working copy)
@@ -4141,7 +4141,57 @@
   return expr;
 }
 
+/* Nonzero if constant C has a value that is permissible
+   for type TYPE (an INTEGER_TYPE).  */
 
+static int
+constant_fits_type_p (tree c, tree type)
+{
+  if (TREE_CODE (c) == INTEGER_CST)
+    return int_fits_type_p (c, type);
+
+  c = convert (type, c);
+  return !TREE_OVERFLOW (c);
+}
+
+/* Convert EXPR to TYPE, warning about conversion problems with constants.
+   Invoke this function on every expression that is converted implicitly,
+   i.e. because of language rules and not because of an explicit cast.  */
+
+static tree
+cxx_convert_and_check (tree type, tree expr)
+{
+  tree t = convert (type, expr);
+  if (TREE_CODE (t) == INTEGER_CST)
+    {
+      if (TREE_OVERFLOW (t))
+	{
+	  TREE_OVERFLOW (t) = 0;
+
+	  /* Do not diagnose overflow in a constant expression merely
+	     because a conversion overflowed.  */
+	  TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
+
+	  /* No warning for converting 0x80000000 to int.  */
+	  if (!(TYPE_UNSIGNED (type) < TYPE_UNSIGNED (TREE_TYPE (expr))
+		&& TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
+		&& TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
+	    /* If EXPR fits in the unsigned version of TYPE,
+	       don't warn unless pedantic.  */
+	    if ((pedantic
+		 || TYPE_UNSIGNED (type)
+		 || !constant_fits_type_p (expr,
+					   c_common_unsigned_type (type))
+		 )
+		&& skip_evaluation == 0)
+	      warning (0, "overflow in implicit constant conversion");
+	}
+      else
+	unsigned_conversion_warning (t, expr);
+    }
+  return t;
+}
+
 /* Perform the conversions in CONVS on the expression EXPR.  FN and
    ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
    indicates the `this' argument of a method.  INNER is nonzero when
@@ -4413,7 +4463,7 @@
     }
 
   if (issue_conversion_warnings)
-    expr = convert_and_check (totype, expr);
+    expr = cxx_convert_and_check (totype, expr);
   else
     expr = convert (totype, expr);
 




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