]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: mangling cleanups
authorNathan Sidwell <nathan@acm.org>
Wed, 22 Jul 2020 14:52:31 +0000 (07:52 -0700)
committerNathan Sidwell <nathan@acm.org>
Wed, 22 Jul 2020 15:43:24 +0000 (08:43 -0700)
I noticed the mangler's handling of templates could be simplified.
We know template_info is non-null, which is sufficiently boolean --
no need for an explicit bool return.  also some of the internals of
template_args_equal had crept into find_substitution.  Let's not do
that.

gcc/cp/
* mangle.c (decl_is_template_id): Rename to ...
(maybe_template_info): ... here.  Return the template info,
rather than use a pointer.  Adjust all callers.
(find_substitution): Use template_args_equal, rather than
local check.

gcc/cp/mangle.c

index 43ff2e84db5850e831b89f796e6f90a1b2e400a0..9fd30011288600a87e486d5f2d3d55b0a71ae528 100644 (file)
@@ -170,7 +170,7 @@ integer_type_codes[itk_none] =
   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
 };
 
-static int decl_is_template_id (const tree, tree* const);
+static tree maybe_template_info (const tree);
 
 /* Functions for handling substitutions.  */
 
@@ -272,11 +272,10 @@ static tree mangle_special_for_type (const tree, const char *);
   write_number ((NUMBER), /*unsigned_p=*/1, 10)
 
 /* If DECL is a template instance (including the uninstantiated template
-   itself), return nonzero and, if TEMPLATE_INFO is non-NULL, set
-   *TEMPLATE_INFO to its template info.  Otherwise return zero.  */
+   itself), return its TEMPLATE_INFO.  Otherwise return NULL.  */
 
-static int
-decl_is_template_id (const tree decl, tree* const template_info)
+static tree
+maybe_template_info (const tree decl)
 {
   if (TREE_CODE (decl) == TYPE_DECL)
     {
@@ -285,33 +284,20 @@ decl_is_template_id (const tree decl, tree* const template_info)
       const tree type = TREE_TYPE (decl);
 
       if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
-       {
-         if (template_info != NULL)
-           /* For a templated TYPE_DECL, the template info is hanging
-              off the type.  */
-           *template_info = TYPE_TEMPLATE_INFO (type);
-         return 1;
-       }
+       return TYPE_TEMPLATE_INFO (type);
     }
   else
     {
-      /* Check if this is a primary template.  */
+      /* Check if the template is a primary template.  */
       if (DECL_LANG_SPECIFIC (decl) != NULL
          && VAR_OR_FUNCTION_DECL_P (decl)
          && DECL_TEMPLATE_INFO (decl)
-         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
-         && TREE_CODE (decl) != TEMPLATE_DECL)
-       {
-         if (template_info != NULL)
-           /* For most templated decls, the template info is hanging
-              off the decl.  */
-           *template_info = DECL_TEMPLATE_INFO (decl);
-         return 1;
-       }
+         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
+       return DECL_TEMPLATE_INFO (decl);
     }
 
   /* It's not a template id.  */
-  return 0;
+  return NULL_TREE;
 }
 
 /* Produce debugging output of current substitution candidates.  */
@@ -628,9 +614,7 @@ find_substitution (tree node)
            {
              tree args = CLASSTYPE_TI_ARGS (type);
              if (TREE_VEC_LENGTH (args) == 3
-                 && (TREE_CODE (TREE_VEC_ELT (args, 0))
-                     == TREE_CODE (char_type_node))
-                 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
+                 && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
                  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
                                               SUBID_CHAR_TRAITS)
                  && is_std_substitution_char (TREE_VEC_ELT (args, 2),
@@ -654,8 +638,7 @@ find_substitution (tree node)
         args <char, std::char_traits<char> > .  */
       tree args = CLASSTYPE_TI_ARGS (type);
       if (TREE_VEC_LENGTH (args) == 2
-         && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_CODE (char_type_node)
-         && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
+         && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
          && is_std_substitution_char (TREE_VEC_ELT (args, 1),
                                       SUBID_CHAR_TRAITS))
        {
@@ -800,7 +783,7 @@ mangle_return_type_p (tree decl)
   return (!DECL_CONSTRUCTOR_P (decl)
          && !DECL_DESTRUCTOR_P (decl)
          && !DECL_CONV_FN_P (decl)
-         && decl_is_template_id (decl, NULL));
+         && maybe_template_info (decl));
 }
 
 /*   <encoding>                ::= <function name> <bare-function-type>
@@ -827,9 +810,8 @@ write_encoding (const tree decl)
     {
       tree fn_type;
       tree d;
-      bool tmpl = decl_is_template_id (decl, NULL);
 
-      if (tmpl)
+      if (maybe_template_info (decl))
        {
          fn_type = get_mostly_instantiated_function_type (decl);
          /* FN_TYPE will not have parameter types for in-charge or
@@ -933,13 +915,12 @@ write_name (tree decl, const int ignore_local_scope)
              || (abi_version_at_least (7)
                  && TREE_CODE (context) == PARM_DECL))))
     {
-      tree template_info;
       /* Is this a template instance?  */
-      if (decl_is_template_id (decl, &template_info))
+      if (tree info = maybe_template_info (decl))
        {
          /* Yes: use <unscoped-template-name>.  */
-         write_unscoped_template_name (TI_TEMPLATE (template_info));
-         write_template_args (TI_ARGS (template_info));
+         write_unscoped_template_name (TI_TEMPLATE (info));
+         write_template_args (TI_ARGS (info));
        }
       else
        /* Everything else gets an <unqualified-name>.  */
@@ -1041,8 +1022,6 @@ write_unscoped_template_name (const tree decl)
 static void
 write_nested_name (const tree decl)
 {
-  tree template_info;
-
   MANGLE_TRACE_TREE ("nested-name", decl);
 
   write_char ('N');
@@ -1065,11 +1044,11 @@ write_nested_name (const tree decl)
     }
 
   /* Is this a template instance?  */
-  if (decl_is_template_id (decl, &template_info))
+  if (tree info = maybe_template_info (decl))
     {
       /* Yes, use <template-prefix>.  */
       write_template_prefix (decl);
-      write_template_args (TI_ARGS (template_info));
+      write_template_args (TI_ARGS (info));
     }
   else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
           && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
@@ -1106,8 +1085,6 @@ static void
 write_prefix (const tree node)
 {
   tree decl;
-  /* Non-NULL if NODE represents a template-id.  */
-  tree template_info = NULL;
 
   if (node == NULL
       || node == global_namespace)
@@ -1124,6 +1101,7 @@ write_prefix (const tree node)
   if (find_substitution (node))
     return;
 
+  tree template_info = NULL_TREE;
   if (DECL_P (node))
     {
       /* If this is a function or parm decl, that means we've hit function
@@ -1136,19 +1114,20 @@ write_prefix (const tree node)
        return;
 
       decl = node;
-      decl_is_template_id (decl, &template_info);
+      template_info = maybe_template_info (decl);
     }
   else
     {
       /* Node is a type.  */
       decl = TYPE_NAME (node);
+      /* The DECL might not point at the node.  */
       if (CLASSTYPE_TEMPLATE_ID_P (node))
        template_info = TYPE_TEMPLATE_INFO (node);
     }
 
   if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
     write_template_param (node);
-  else if (template_info != NULL)
+  else if (template_info)
     /* Templated.  */
     {
       write_template_prefix (decl);
@@ -1195,15 +1174,14 @@ write_template_prefix (const tree node)
   tree decl = DECL_P (node) ? node : TYPE_NAME (node);
   tree type = DECL_P (node) ? TREE_TYPE (node) : node;
   tree context = decl_mangling_context (decl);
-  tree template_info;
   tree templ;
   tree substitution;
 
   MANGLE_TRACE_TREE ("template-prefix", node);
 
   /* Find the template decl.  */
-  if (decl_is_template_id (decl, &template_info))
-    templ = TI_TEMPLATE (template_info);
+  if (tree info = maybe_template_info (decl))
+    templ = TI_TEMPLATE (info);
   else if (TREE_CODE (type) == TYPENAME_TYPE)
     /* For a typename type, all we have is the name.  */
     templ = DECL_NAME (decl);
@@ -1369,7 +1347,7 @@ write_unqualified_name (tree decl)
          /* Conversion operator. Handle it right here.
             <operator> ::= cv <type>  */
          tree type;
-         if (decl_is_template_id (decl, NULL))
+         if (maybe_template_info (decl))
            {
              tree fn_type;
              fn_type = get_mostly_instantiated_function_type (decl);
@@ -1413,8 +1391,7 @@ write_unqualified_name (tree decl)
       if (TREE_CODE (decl) == TYPE_DECL
           && TYPE_UNNAMED_P (type))
         write_unnamed_type_name (type);
-      else if (TREE_CODE (decl) == TYPE_DECL
-               && LAMBDA_TYPE_P (type))
+      else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
         write_closure_type_name (type);
       else
         write_source_name (DECL_NAME (decl));