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]

[PATCH] factor common code in signed_or_unsigned


The attached patch factors common code to many signed_or_unsigned
language hooks. It also adds a default implementation that is used by
all non C/C++ frontends.

I am trying to remove this language hook. Removing it from C/C++
requires more work, but I believe that this patch is already a nice
cleanup.


Bootstrapped and regression tested with all languages enabled.


Cheers,
Rafael
Index: gcc/java/typeck.c
===================================================================
--- gcc/java/typeck.c	(revision 122787)
+++ gcc/java/typeck.c	(working copy)
@@ -195,31 +195,12 @@
   return 0;
 }
 
-/* Return a type the same as TYPE except unsigned or
-   signed according to UNSIGNEDP.  */
-
-tree
-java_signed_or_unsigned_type (int unsignedp, tree type)
-{
-  if (! INTEGRAL_TYPE_P (type))
-    return type;
-  if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
-    return unsignedp ? unsigned_int_type_node : int_type_node;
-  if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
-    return unsignedp ? unsigned_byte_type_node : byte_type_node;
-  if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
-    return unsignedp ? unsigned_short_type_node : short_type_node;
-  if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
-    return unsignedp ? unsigned_long_type_node : long_type_node;
-  return type;
-}
-
 /* Return a signed type the same as TYPE in other respects.  */
 
 tree
 java_signed_type (tree type)
 {
-  return java_signed_or_unsigned_type (0, type);
+  return get_signed_or_unsigned_type (0, type);
 }
 
 /* Return an unsigned type the same as TYPE in other respects.  */
@@ -227,7 +208,7 @@
 tree
 java_unsigned_type (tree type)
 {
-  return java_signed_or_unsigned_type (1, type);
+  return get_signed_or_unsigned_type (1, type);
 }
 
 /* Mark EXP saying that we need to be able to take the
Index: gcc/java/lang.c
===================================================================
--- gcc/java/lang.c	(revision 122787)
+++ gcc/java/lang.c	(working copy)
@@ -186,8 +186,6 @@
 #define LANG_HOOKS_SIGNED_TYPE java_signed_type
 #undef LANG_HOOKS_UNSIGNED_TYPE
 #define LANG_HOOKS_UNSIGNED_TYPE java_unsigned_type
-#undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
-#define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type
 
 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
Index: gcc/java/java-tree.h
===================================================================
--- gcc/java/java-tree.h	(revision 122787)
+++ gcc/java/java-tree.h	(working copy)
@@ -1101,7 +1101,6 @@
 extern tree java_type_for_size (unsigned int, int);
 extern tree java_unsigned_type (tree);
 extern tree java_signed_type (tree);
-extern tree java_signed_or_unsigned_type (int, tree);
 extern tree java_truthvalue_conversion (tree);
 extern void add_assume_compiled (const char *, int);
 extern void add_enable_assert (const char *, int);
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	(revision 122787)
+++ gcc/tree.h	(working copy)
@@ -3804,6 +3804,8 @@
 extern bool may_negate_without_overflow_p (tree);
 extern tree get_inner_array_type (tree);
 
+extern tree get_signed_or_unsigned_type (int unsignedp, tree type);
+
 /* From expmed.c.  Since rtl.h is included after tree.h, we can't
    put the prototype here.  Rtl.h does declare the prototype if
    tree.h had been included.  */
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	(revision 122787)
+++ gcc/fold-const.c	(working copy)
@@ -3033,7 +3033,7 @@
 
       /* Make sure shorter operand is extended the right way
 	 to match the longer operand.  */
-      primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
+      primarg1 = fold_convert (get_signed_or_unsigned_type
 			       (unsignedp1, TREE_TYPE (primarg1)), primarg1);
 
       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
Index: gcc/expr.c
===================================================================
--- gcc/expr.c	(revision 122787)
+++ gcc/expr.c	(working copy)
@@ -4383,7 +4383,7 @@
 	  if (TYPE_UNSIGNED (TREE_TYPE (exp))
 	      != SUBREG_PROMOTED_UNSIGNED_P (target))
 	    exp = fold_convert
-	      (lang_hooks.types.signed_or_unsigned_type
+	      (get_signed_or_unsigned_type
 	       (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)), exp);
 
 	  exp = fold_convert (lang_hooks.types.type_for_mode
Index: gcc/ada/trans.c
===================================================================
--- gcc/ada/trans.c	(revision 122787)
+++ gcc/ada/trans.c	(working copy)
@@ -876,7 +876,7 @@
 	  /* 'Length or 'Range_Length.  */
 	  {
 	    tree gnu_compute_type
-	      = gnat_signed_or_unsigned_type (0,
+	      = get_signed_or_unsigned_type (0,
 					      get_base_type (gnu_result_type));
 
 	    gnu_result
Index: gcc/ada/utils.c
===================================================================
--- gcc/ada/utils.c	(revision 122787)
+++ gcc/ada/utils.c	(working copy)
@@ -2307,17 +2307,6 @@
   return type;
 }
 
-/* Return a type the same as TYPE except unsigned or signed according to
-   UNSIGNEDP.  */
-
-tree
-gnat_signed_or_unsigned_type (int unsignedp, tree type)
-{
-  if (!INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
-    return type;
-  else
-    return gnat_type_for_size (TYPE_PRECISION (type), unsignedp);
-}
 
 /* EXP is an expression for the size of an object.  If this size contains
    discriminant references, replace them with the maximum (if MAX_P) or
Index: gcc/ada/misc.c
===================================================================
--- gcc/ada/misc.c	(revision 122787)
+++ gcc/ada/misc.c	(working copy)
@@ -161,8 +161,6 @@
 #define LANG_HOOKS_SIGNED_TYPE		gnat_signed_type
 #undef  LANG_HOOKS_UNSIGNED_TYPE
 #define LANG_HOOKS_UNSIGNED_TYPE	gnat_unsigned_type
-#undef  LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
-#define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE gnat_signed_or_unsigned_type
 #undef  LANG_HOOKS_ATTRIBUTE_TABLE
 #define LANG_HOOKS_ATTRIBUTE_TABLE	gnat_internal_attribute_table
 #undef  LANG_HOOKS_BUILTIN_FUNCTION
Index: gcc/ada/gigi.h
===================================================================
--- gcc/ada/gigi.h	(revision 122787)
+++ gcc/ada/gigi.h	(working copy)
@@ -454,10 +454,6 @@
 /* Return the signed version of a TYPE_NODE, a scalar type.  */
 extern tree gnat_signed_type (tree type_node);
 
-/* Return a type the same as TYPE except unsigned or signed according to
-   UNSIGNEDP.  */
-extern tree gnat_signed_or_unsigned_type (int unsignedp, tree type);
-
 /* Create an expression whose value is that of EXPR,
    converted to type TYPE.  The TREE_TYPE of the value
    is always TYPE.  This function implements all reasonable
Index: gcc/fortran/trans-types.c
===================================================================
--- gcc/fortran/trans-types.c	(revision 122787)
+++ gcc/fortran/trans-types.c	(working copy)
@@ -1870,24 +1870,12 @@
   return NULL_TREE;
 }
 
-/* Return a type the same as TYPE except unsigned or
-   signed according to UNSIGNEDP.  */
-
-tree
-gfc_signed_or_unsigned_type (int unsignedp, tree type)
-{
-  if (TREE_CODE (type) != INTEGER_TYPE || TYPE_UNSIGNED (type) == unsignedp)
-    return type;
-  else
-    return gfc_type_for_size (TYPE_PRECISION (type), unsignedp);
-}
-
 /* Return an unsigned type the same as TYPE in other respects.  */
 
 tree
 gfc_unsigned_type (tree type)
 {
-  return gfc_signed_or_unsigned_type (1, type);
+  return get_signed_or_unsigned_type (1, type);
 }
 
 /* Return a signed type the same as TYPE in other respects.  */
@@ -1895,7 +1883,7 @@
 tree
 gfc_signed_type (tree type)
 {
-  return gfc_signed_or_unsigned_type (0, type);
+  return get_signed_or_unsigned_type (0, type);
 }
 
 #include "gt-fortran-trans-types.h"
Index: gcc/fortran/trans-types.h
===================================================================
--- gcc/fortran/trans-types.h	(revision 122787)
+++ gcc/fortran/trans-types.h	(working copy)
@@ -76,7 +76,6 @@
 tree gfc_type_for_mode (enum machine_mode, int);
 tree gfc_unsigned_type (tree);
 tree gfc_signed_type (tree);
-tree gfc_signed_or_unsigned_type (int, tree);
 
 tree gfc_get_element_type (tree);
 tree gfc_get_array_type_bounds (tree, int, tree *, tree *, int);
Index: gcc/fortran/f95-lang.c
===================================================================
--- gcc/fortran/f95-lang.c	(revision 122787)
+++ gcc/fortran/f95-lang.c	(working copy)
@@ -115,7 +115,6 @@
 #undef LANG_HOOKS_TYPE_FOR_SIZE
 #undef LANG_HOOKS_UNSIGNED_TYPE
 #undef LANG_HOOKS_SIGNED_TYPE
-#undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
 #undef LANG_HOOKS_CLEAR_BINDING_STACK
 #undef LANG_HOOKS_GET_ALIAS_SET
@@ -141,7 +140,6 @@
 #define LANG_HOOKS_TYPE_FOR_SIZE           gfc_type_for_size
 #define LANG_HOOKS_UNSIGNED_TYPE           gfc_unsigned_type
 #define LANG_HOOKS_SIGNED_TYPE             gfc_signed_type
-#define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE gfc_signed_or_unsigned_type
 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION gfc_expand_function
 #define LANG_HOOKS_CLEAR_BINDING_STACK     gfc_clear_binding_stack
 #define LANG_HOOKS_GET_ALIAS_SET	   gfc_get_alias_set
Index: gcc/langhooks.c
===================================================================
--- gcc/langhooks.c	(revision 122787)
+++ gcc/langhooks.c	(working copy)
@@ -621,3 +621,18 @@
   lang_hooks.decls.pushdecl (decl);
   return decl;
 }
+
+tree
+get_signed_or_unsigned_type (int unsignedp, tree type)
+{
+  if (!INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
+    return type;
+
+  return lang_hooks.types.signed_or_unsigned_type(unsignedp, type);
+}
+
+tree
+lhd_signed_or_unsigned_type (int unsignedp, tree type)
+{
+  return lang_hooks.types.type_for_size (TYPE_PRECISION (type), unsignedp);
+}
Index: gcc/langhooks.h
===================================================================
--- gcc/langhooks.h	(revision 122787)
+++ gcc/langhooks.h	(working copy)
@@ -461,5 +461,6 @@
 				  int function_code, enum built_in_class cl,
 				  const char *library_name,
 				  tree attrs);
+extern tree lhd_signed_or_unsigned_type (int unsignedp, tree type);
 
 #endif /* GCC_LANG_HOOKS_H */
Index: gcc/treelang/treetree.c
===================================================================
--- gcc/treelang/treetree.c	(revision 122787)
+++ gcc/treelang/treetree.c	(working copy)
@@ -129,7 +129,6 @@
 static tree tree_lang_type_for_mode (enum machine_mode mode, int unsignedp);
 static tree tree_lang_unsigned_type (tree type_node);
 static tree tree_lang_signed_type (tree type_node);
-static tree tree_lang_signed_or_unsigned_type (int unsignedp, tree type);
 
 /* Functions to keep track of the current scope.  */
 static void pushlevel (int ignore);
@@ -156,8 +155,6 @@
 #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type
 #undef LANG_HOOKS_UNSIGNED_TYPE
 #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type
-#undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
-#define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type
 #undef LANG_HOOKS_TYPE_FOR_MODE
 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
 #undef LANG_HOOKS_TYPE_FOR_SIZE
@@ -884,17 +881,6 @@
   return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0);
 }
 
-/* Return a type the same as TYPE except unsigned or signed according to
-   UNSIGNEDP.  */
-
-static tree
-tree_lang_signed_or_unsigned_type (int unsignedp, tree type)
-{
-  if (! INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
-    return type;
-  else
-    return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp);
-}
 
 /* These functions and variables deal with binding contours.  We only
    need these functions for the list of PARM_DECLs, but we leave the
Index: gcc/langhooks-def.h
===================================================================
--- gcc/langhooks-def.h	(revision 122787)
+++ gcc/langhooks-def.h	(working copy)
@@ -130,6 +130,7 @@
 #define LANG_HOOKS_TREE_SIZE		lhd_tree_size
 #define LANG_HOOKS_TYPES_COMPATIBLE_P	lhd_types_compatible_p
 #define LANG_HOOKS_BUILTIN_FUNCTION	lhd_builtin_function
+#define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE	lhd_signed_or_unsigned_type
 #define LANG_HOOKS_EXPR_TO_DECL		lhd_expr_to_decl
 #define LANG_HOOKS_TO_TARGET_CHARSET	lhd_to_target_charset
 #define LANG_HOOKS_INIT_TS		lhd_do_nothing

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