C++ PATCH: Name mangling fixes for ttp

Kriang Lerdsuwanakij lerdsuwa@scf.usc.edu
Sun Apr 11 18:02:00 GMT 1999


Hi

The following patches, one for gcc/cp/* and another for libiberty/*,
fix 2 name mangling issues.

1 When mangling template template argument, it is not mangled 
as a qualified name.  So the following case fails as
f<C> and f<N::C> both produce the same mangled name.

	template <class T> class C;
	namespace N {
		template <class T> class C;
	}
	template <template <class> class TT> void f() {}
	int main() {
		f<C>();
		f<N::C>();
	}

2 Name mangling of instantiation of function with the form:
	template <template <class> class TT> void f(D<TT>);
The output is not consistent with the rest of template uses.
It is also not properly demangled by cplus-dem.c.

By the way, what's the status of my other patch
(Bound ttp node type changes, achived at
http://egcs.cygnus.com/ml/egcs-patches/1999-03/msg-00000.html ) ?

Thanks

--Kriang

ChangeLog for gcc/cp/*:

1999-04-11  Kriang Lerdsuwanakij  <lerdsuwa@scf.usc.edu>

	* method.c (build_overload_nested_name): Handle TEMPLATE_DECL.
	(build_template_parm_names): When ARG is a TEMPLATE_DECL, also
	mangle its context.
	(process_overload_item): Remove an extra 'Z' when mangling
	unbound TEMPLATE_TEMPLATE_PARM.  Move parameters mangling of
	TEMPLATE_TEMPLATE_PARM from here 
	(build_template_parm_names): ...to here.
	* pt.c (mangle_class_name_for_template): Remove assert.
	* gxxint.texi: Document template template parameter
	name mangling.

*** gxxint.texi	1999/04/06 08:04:35	8.1
--- gxxint.texi	1999/04/11 12:33:14
*************** A class template instantiation is encode
*** 1655,1661 ****
  followed by the encoding of the template name, followed
  the number of template parameters, followed by encoding of the template
  parameters.  If a template parameter is a type, it is written
! as a @samp{Z} followed by the encoding of the type.
  
  A function template specialization (either an instantiation or an
  explicit specialization) is encoded by an @samp{H} followed by the
--- 1655,1663 ----
  followed by the encoding of the template name, followed
  the number of template parameters, followed by encoding of the template
  parameters.  If a template parameter is a type, it is written
! as a @samp{Z} followed by the encoding of the type.  If it is a
! template, it is encoded as @samp{z} followed by the parameter
! of the template template parameter and the template name.
  
  A function template specialization (either an instantiation or an
  explicit specialization) is encoded by an @samp{H} followed by the
*************** encoding of the template parameters, as 
*** 1664,1671 ****
  (not the specialization), another @samp{_}, and the return type.  (Like
  the argument types, the return type is the return type of the function
  template, not the specialization.)  Template parameters in the argument
! and return types are encoded by an @samp{X} for type parameters, or a
! @samp{Y} for constant parameters, an index indicating their position
  in the template parameter list declaration, and their template depth.
  
  @subsection Arrays
--- 1666,1674 ----
  (not the specialization), another @samp{_}, and the return type.  (Like
  the argument types, the return type is the return type of the function
  template, not the specialization.)  Template parameters in the argument
! and return types are encoded by an @samp{X} for type parameters, 
! @samp{zX} for template parameters, 
! or a @samp{Y} for constant parameters, an index indicating their position
  in the template parameter list declaration, and their template depth.
  
  @subsection Arrays
*************** Encodes a template type parameter, when 
*** 1822,1827 ****
--- 1825,1833 ----
  
  @item Y
  Encodes a template constant parameter, when part of a function type.
+ 
+ @item z
+ Used for template template parameters. 
  
  @item Z
  Used for template type parameters. 
*** method.c	1999/04/06 08:04:35	8.1
--- method.c	1999/04/11 11:32:31
*************** build_overload_nested_name (decl)
*** 516,522 ****
        OB_PUTCP (label);
        numeric_output_need_bar = 1;
      }
!   else if (TREE_CODE (decl) == NAMESPACE_DECL)
      build_overload_identifier (DECL_NAME (decl));
    else				/* TYPE_DECL */
      build_overload_identifier (decl);
--- 516,523 ----
        OB_PUTCP (label);
        numeric_output_need_bar = 1;
      }
!   else if (TREE_CODE (decl) == NAMESPACE_DECL
! 	   || TREE_CODE (decl) == TEMPLATE_DECL)
      build_overload_identifier (DECL_NAME (decl));
    else				/* TYPE_DECL */
      build_overload_identifier (decl);
*************** build_template_parm_names (parmlist, arg
*** 946,964 ****
        else if (TREE_CODE (parm) == TEMPLATE_DECL)
  	{
  	  /* This parameter is a template.  */
  	  if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
! 	    /* Output parameter declaration, argument index and level.  */
  	    build_mangled_name_for_type (arg);
  	  else
  	    {
! 	      /* A TEMPLATE_DECL node, output the parameter declaration 
! 		 and template name */
! 
! 	      OB_PUTC ('z');
! 	      build_template_template_parm_names
! 		(DECL_INNERMOST_TEMPLATE_PARMS (parm));
! 	      icat (IDENTIFIER_LENGTH (DECL_NAME (arg)));
! 	      OB_PUTID (DECL_NAME (arg));
  	    }
  	}
        else
--- 947,964 ----
        else if (TREE_CODE (parm) == TEMPLATE_DECL)
  	{
  	  /* This parameter is a template.  */
+ 	  OB_PUTC ('z');
+ 	  build_template_template_parm_names
+ 	    (DECL_INNERMOST_TEMPLATE_PARMS (parm));
+ 
  	  if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
! 	    /* Output argument index and level.  */
  	    build_mangled_name_for_type (arg);
  	  else
  	    {
! 	      /* A TEMPLATE_DECL node.  */
! 	      numeric_output_need_bar = 0;
! 	      build_qualified_name (arg);
  	    }
  	}
        else
*************** process_overload_item (parmtype, extra_G
*** 1488,1495 ****
        break;
  
      case TEMPLATE_TEMPLATE_PARM:
-       /* Find and output the original template parameter 
-          declaration. */
        if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parmtype))
          {
  	  build_mangled_template_parm_index ("tzX",
--- 1488,1493 ----
*************** process_overload_item (parmtype, extra_G
*** 1501,1511 ****
          }
        else
          {
! 	  build_mangled_template_parm_index ("ZzX",
  					     TEMPLATE_TYPE_PARM_INDEX 
  					     (parmtype));
-           build_template_template_parm_names
-             (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_STUB_DECL (parmtype)));
          }
        break;
  
--- 1499,1507 ----
          }
        else
          {
! 	  build_mangled_template_parm_index ("zX",
  					     TEMPLATE_TYPE_PARM_INDEX 
  					     (parmtype));
          }
        break;
  
*** pt.c	1999/04/06 08:04:35	8.1
--- pt.c	1999/04/11 11:11:58
*************** mangle_class_name_for_template (name, pa
*** 3393,3399 ****
                tree context = DECL_CONTEXT (arg);
  	      if (context)
  		{
!                   my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422);
  		  cat(decl_as_string (DECL_CONTEXT (arg), 0));
  		  cat("::");
  		}
--- 3393,3399 ----
                tree context = DECL_CONTEXT (arg);
  	      if (context)
  		{
! 		  /* NAMESPACE_DECL or RECORD_TYPE */
  		  cat(decl_as_string (DECL_CONTEXT (arg), 0));
  		  cat("::");
  		}

=================================
ChangeLog for libiberty/*:
1999-04-11  Kriang Lerdsuwanakij  <lerdsuwa@scf.usc.edu>

	* cplus-dem.c (demangle_template_name): New function.
	(demangle_template): Use it.

*** cplus-dem.c	1999/04/06 08:04:20	8.1
--- cplus-dem.c	1999/04/12 00:07:04
*************** demangle_template_template_parm PARAMS (
*** 278,283 ****
--- 278,287 ----
  					 const char **, string *));
  
  static int
+ demangle_template_name PARAMS ((struct work_stuff *work, const char **, 
+ 				string *));
+ 
+ static int
  demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
  			   string *, int, int));
  
*************** demangle_template_value_parm (work, mang
*** 1526,1531 ****
--- 1530,1587 ----
    return success;
  }
  
+ static int
+ demangle_template_name (work, mangled, tname)
+      struct work_stuff *work;
+      const char **mangled;
+      string *tname;
+ {
+   int success = 1;
+ 
+   if (**mangled == 'z')
+     {
+       int idx;
+       (*mangled)++;
+       (*mangled)++;
+ 
+       idx = consume_count_with_underscores (mangled);
+       if (idx == -1
+ 	  || (work->tmpl_argvec && idx >= work->ntmpl_args)
+ 	  || consume_count_with_underscores (mangled) == -1)
+ 	    return (0);
+ 
+       if (work->tmpl_argvec)
+ 	{
+ 	  string_append (tname, work->tmpl_argvec[idx]);
+ 	}
+       else
+ 	{
+ 	  char buf[10];
+ 	  sprintf(buf, "T%d", idx);
+ 	  string_append (tname, buf);
+ 	}
+     }
+   else if (**mangled == 'Q' || **mangled == 'K')
+     {
+       success = demangle_qualified (work, mangled, tname, 0, 1);
+       if (!success)
+ 	return (0);
+     }
+   else
+     {
+       int r;
+       if ((r = consume_count (mangled)) == 0
+ 	  || (int) strlen (*mangled) < r)
+ 	{
+ 	  return (0);
+ 	}
+       string_appendn (tname, *mangled, r);
+       *mangled += r;
+     }
+ 
+   return (1);
+ }
+ 
  /* Demangle the template name in MANGLED.  The full name of the
     template (e.g., S<int>) is placed in TNAME.  The name without the
     template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
*************** demangle_template (work, mangled, tname,
*** 1557,1601 ****
        if (remember)
  	bindex = register_Btype (work);
        start = *mangled;
-       /* get template name */
-       if (**mangled == 'z')
- 	{
- 	  int idx;
- 	  (*mangled)++;
- 	  (*mangled)++;
  
! 	  idx = consume_count_with_underscores (mangled);
! 	  if (idx == -1
! 	      || (work->tmpl_argvec && idx >= work->ntmpl_args)
! 	      || consume_count_with_underscores (mangled) == -1)
! 	    return (0);
  
! 	  if (work->tmpl_argvec)
! 	    {
! 	      string_append (tname, work->tmpl_argvec[idx]);
! 	      if (trawname)
! 		string_append (trawname, work->tmpl_argvec[idx]);
! 	    }
! 	  else
! 	    {
! 	      char buf[10];
! 	      sprintf(buf, "T%d", idx);
! 	      string_append (tname, buf);
! 	      if (trawname)
! 		string_append (trawname, buf);
! 	    }
! 	}
!       else
  	{
! 	  if ((r = consume_count (mangled)) == 0
! 	      || (int) strlen (*mangled) < r)
! 	    {
! 	      return (0);
! 	    }
! 	  string_appendn (tname, *mangled, r);
! 	  if (trawname)
! 	    string_appendn (trawname, *mangled, r);
! 	  *mangled += r;
  	}
      }
    string_append (tname, "<");
--- 1613,1631 ----
        if (remember)
  	bindex = register_Btype (work);
        start = *mangled;
  
!       /* Name can be either an unqualified name or a template template 
! 	 parameter. */
!       success = demangle_template_name (work, mangled, tname);
!       if (!success)
! 	return (0);
  
!       if (trawname)
  	{
! 	  *mangled = start;
! 	  success = demangle_template_name (work, mangled, trawname);
! 	  if (!success)
! 	    return (0);
  	}
      }
    string_append (tname, "<");
*************** demangle_template (work, mangled, tname,
*** 1646,1671 ****
        /* z for template parameters */
        else if (**mangled == 'z')
  	{
- 	  int r2;
  	  (*mangled)++;
! 	  success = demangle_template_template_parm (work, mangled, tname);
  
  	  if (success
! 	      && (r2 = consume_count (mangled)) > 0
! 	      && (int) strlen (*mangled) >= r2)
  	    {
! 	      string_append (tname, " ");
! 	      string_appendn (tname, *mangled, r2);
  	      if (!is_type)
  		{
  		  /* Save the template argument. */
! 		  int len = r2;
  		  work->tmpl_argvec[i] = xmalloc (len + 1);
! 		  memcpy (work->tmpl_argvec[i], *mangled, len);
  		  work->tmpl_argvec[i][len] = '\0';
  		}
- 	      *mangled += r2;
  	    }
  	  if (!success)
  	    {
  	      break;
--- 1676,1701 ----
        /* z for template parameters */
        else if (**mangled == 'z')
  	{
  	  (*mangled)++;
! 	  string_init (&temp);
! 	  success = demangle_template_template_parm (work, mangled, &temp);
! 	  string_clear(&temp);
  
  	  if (success
! 	      && (success = demangle_template_name (work, mangled, &temp)))
  	    {
! 	      string_appends (tname, &temp);
! 
  	      if (!is_type)
  		{
  		  /* Save the template argument. */
! 		  int len = temp.p - temp.b;
  		  work->tmpl_argvec[i] = xmalloc (len + 1);
! 		  memcpy (work->tmpl_argvec[i], temp.b, len);
  		  work->tmpl_argvec[i][len] = '\0';
  		}
  	    }
+ 	  string_delete(&temp);
  	  if (!success)
  	    {
  	      break;




More information about the Gcc-patches mailing list