190075Sobrien/* Name mangling for the 3.0 C++ ABI.
2169689Skan   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3169689Skan   Free Software Foundation, Inc.
4169689Skan   Written by Alex Samuel <samuel@codesourcery.com>
590075Sobrien
6132718Skan   This file is part of GCC.
790075Sobrien
8132718Skan   GCC is free software; you can redistribute it and/or modify it
990075Sobrien   under the terms of the GNU General Public License as published by
1090075Sobrien   the Free Software Foundation; either version 2, or (at your option)
1190075Sobrien   any later version.
1290075Sobrien
13132718Skan   GCC is distributed in the hope that it will be useful, but
1490075Sobrien   WITHOUT ANY WARRANTY; without even the implied warranty of
1590075Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1690075Sobrien   General Public License for more details.
1790075Sobrien
1890075Sobrien   You should have received a copy of the GNU General Public License
19132718Skan   along with GCC; see the file COPYING.  If not, write to the Free
20169689Skan   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169689Skan   02110-1301, USA.  */
2290075Sobrien
2390075Sobrien/* This file implements mangling of C++ names according to the IA64
2490075Sobrien   C++ ABI specification.  A mangled name encodes a function or
2590075Sobrien   variable's name, scope, type, and/or template arguments into a text
2690075Sobrien   identifier.  This identifier is used as the function's or
2790075Sobrien   variable's linkage name, to preserve compatibility between C++'s
2890075Sobrien   language features (templates, scoping, and overloading) and C
2990075Sobrien   linkers.
3090075Sobrien
3190075Sobrien   Additionally, g++ uses mangled names internally.  To support this,
3290075Sobrien   mangling of types is allowed, even though the mangled name of a
3390075Sobrien   type should not appear by itself as an exported name.  Ditto for
3490075Sobrien   uninstantiated templates.
3590075Sobrien
3690075Sobrien   The primary entry point for this module is mangle_decl, which
3790075Sobrien   returns an identifier containing the mangled name for a decl.
3890075Sobrien   Additional entry points are provided to build mangled names of
3990075Sobrien   particular constructs when the appropriate decl for that construct
4090075Sobrien   is not available.  These are:
4190075Sobrien
42169689Skan     mangle_typeinfo_for_type:		typeinfo data
43169689Skan     mangle_typeinfo_string_for_type:	typeinfo type name
44169689Skan     mangle_vtbl_for_type:		virtual table data
45169689Skan     mangle_vtt_for_type:		VTT data
46169689Skan     mangle_ctor_vtbl_for_type:		`C-in-B' constructor virtual table data
47169689Skan     mangle_thunk:			thunk function or entry  */
4890075Sobrien
4990075Sobrien#include "config.h"
5090075Sobrien#include "system.h"
51132718Skan#include "coretypes.h"
52132718Skan#include "tm.h"
5390075Sobrien#include "tree.h"
54117395Skan#include "tm_p.h"
5590075Sobrien#include "cp-tree.h"
56117395Skan#include "real.h"
5790075Sobrien#include "obstack.h"
5890075Sobrien#include "toplev.h"
5990075Sobrien#include "varray.h"
60132718Skan#include "flags.h"
61146895Skan#include "target.h"
6290075Sobrien
6390075Sobrien/* Debugging support.  */
6490075Sobrien
6590075Sobrien/* Define DEBUG_MANGLE to enable very verbose trace messages.  */
6690075Sobrien#ifndef DEBUG_MANGLE
6790075Sobrien#define DEBUG_MANGLE 0
6890075Sobrien#endif
6990075Sobrien
7090075Sobrien/* Macros for tracing the write_* functions.  */
7190075Sobrien#if DEBUG_MANGLE
7290075Sobrien# define MANGLE_TRACE(FN, INPUT) \
7390075Sobrien  fprintf (stderr, "  %-24s: %-24s\n", (FN), (INPUT))
7490075Sobrien# define MANGLE_TRACE_TREE(FN, NODE) \
7590075Sobrien  fprintf (stderr, "  %-24s: %-24s (%p)\n", \
76169689Skan	   (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
7790075Sobrien#else
7890075Sobrien# define MANGLE_TRACE(FN, INPUT)
7990075Sobrien# define MANGLE_TRACE_TREE(FN, NODE)
8090075Sobrien#endif
8190075Sobrien
82117395Skan/* Nonzero if NODE is a class template-id.  We can't rely on
8390075Sobrien   CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
8490075Sobrien   that hard to distinguish A<T> from A, where A<T> is the type as
8590075Sobrien   instantiated outside of the template, and A is the type used
8690075Sobrien   without parameters inside the template.  */
87117395Skan#define CLASSTYPE_TEMPLATE_ID_P(NODE)					\
88117395Skan  (TYPE_LANG_SPECIFIC (NODE) != NULL					\
89117395Skan   && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM			\
90117395Skan       || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL			\
91117395Skan	   && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
9290075Sobrien
9390075Sobrien/* Things we only need one of.  This module is not reentrant.  */
94169689Skantypedef struct globals GTY(())
9590075Sobrien{
9690075Sobrien  /* An array of the current substitution candidates, in the order
9790075Sobrien     we've seen them.  */
98169689Skan  VEC(tree,gc) *substitutions;
99107590Sobrien
100107590Sobrien  /* The entity that is being mangled.  */
101169689Skan  tree GTY ((skip)) entity;
102107590Sobrien
103107590Sobrien  /* True if the mangling will be different in a future version of the
104107590Sobrien     ABI.  */
105107590Sobrien  bool need_abi_warning;
106169689Skan} globals;
10790075Sobrien
108169689Skanstatic GTY (()) globals G;
109169689Skan
110169689Skan/* The obstack on which we build mangled names.  */
111169689Skanstatic struct obstack *mangle_obstack;
112169689Skan
113169689Skan/* The obstack on which we build mangled names that are not going to
114169689Skan   be IDENTIFIER_NODEs.  */
115169689Skanstatic struct obstack name_obstack;
116169689Skan
117169689Skan/* The first object on the name_obstack; we use this to free memory
118169689Skan   allocated on the name_obstack.  */
119169689Skanstatic void *name_base;
120169689Skan
121169689Skan/* An incomplete mangled name.  There will be no NUL terminator.  If
122169689Skan   there is no incomplete mangled name, this variable is NULL.  */
123169689Skanstatic char *partially_mangled_name;
124169689Skan
125169689Skan/* The number of characters in the PARTIALLY_MANGLED_NAME.  */
126169689Skanstatic size_t partially_mangled_name_len;
127169689Skan
12890075Sobrien/* Indices into subst_identifiers.  These are identifiers used in
12990075Sobrien   special substitution rules.  */
13090075Sobrientypedef enum
13190075Sobrien{
13290075Sobrien  SUBID_ALLOCATOR,
13390075Sobrien  SUBID_BASIC_STRING,
13490075Sobrien  SUBID_CHAR_TRAITS,
13590075Sobrien  SUBID_BASIC_ISTREAM,
13690075Sobrien  SUBID_BASIC_OSTREAM,
13790075Sobrien  SUBID_BASIC_IOSTREAM,
13890075Sobrien  SUBID_MAX
13990075Sobrien}
14090075Sobriensubstitution_identifier_index_t;
14190075Sobrien
14290075Sobrien/* For quick substitution checks, look up these common identifiers
14390075Sobrien   once only.  */
144132718Skanstatic GTY(()) tree subst_identifiers[SUBID_MAX];
14590075Sobrien
14690075Sobrien/* Single-letter codes for builtin integer types, defined in
14790075Sobrien   <builtin-type>.  These are indexed by integer_type_kind values.  */
148117395Skanstatic const char
14990075Sobrieninteger_type_codes[itk_none] =
15090075Sobrien{
15190075Sobrien  'c',  /* itk_char */
15290075Sobrien  'a',  /* itk_signed_char */
15390075Sobrien  'h',  /* itk_unsigned_char */
15490075Sobrien  's',  /* itk_short */
15590075Sobrien  't',  /* itk_unsigned_short */
15690075Sobrien  'i',  /* itk_int */
15790075Sobrien  'j',  /* itk_unsigned_int */
15890075Sobrien  'l',  /* itk_long */
15990075Sobrien  'm',  /* itk_unsigned_long */
16090075Sobrien  'x',  /* itk_long_long */
16190075Sobrien  'y'   /* itk_unsigned_long_long */
16290075Sobrien};
16390075Sobrien
164132718Skanstatic int decl_is_template_id (const tree, tree* const);
16590075Sobrien
16690075Sobrien/* Functions for handling substitutions.  */
16790075Sobrien
168132718Skanstatic inline tree canonicalize_for_substitution (tree);
169132718Skanstatic void add_substitution (tree);
170132718Skanstatic inline int is_std_substitution (const tree,
171132718Skan				       const substitution_identifier_index_t);
172132718Skanstatic inline int is_std_substitution_char (const tree,
173132718Skan					    const substitution_identifier_index_t);
174132718Skanstatic int find_substitution (tree);
175132718Skanstatic void mangle_call_offset (const tree, const tree);
17690075Sobrien
17790075Sobrien/* Functions for emitting mangled representations of things.  */
17890075Sobrien
179132718Skanstatic void write_mangled_name (const tree, bool);
180132718Skanstatic void write_encoding (const tree);
181132718Skanstatic void write_name (tree, const int);
182132718Skanstatic void write_unscoped_name (const tree);
183132718Skanstatic void write_unscoped_template_name (const tree);
184132718Skanstatic void write_nested_name (const tree);
185132718Skanstatic void write_prefix (const tree);
186132718Skanstatic void write_template_prefix (const tree);
187132718Skanstatic void write_unqualified_name (const tree);
188132718Skanstatic void write_conversion_operator_name (const tree);
189132718Skanstatic void write_source_name (tree);
190132718Skanstatic int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
191132718Skan			   const unsigned int);
192132718Skanstatic void write_number (unsigned HOST_WIDE_INT, const int,
193132718Skan			  const unsigned int);
194132718Skanstatic void write_integer_cst (const tree);
195132718Skanstatic void write_real_cst (const tree);
196132718Skanstatic void write_identifier (const char *);
197132718Skanstatic void write_special_name_constructor (const tree);
198132718Skanstatic void write_special_name_destructor (const tree);
199132718Skanstatic void write_type (tree);
200132718Skanstatic int write_CV_qualifiers_for_type (const tree);
201132718Skanstatic void write_builtin_type (tree);
202132718Skanstatic void write_function_type (const tree);
203132718Skanstatic void write_bare_function_type (const tree, const int, const tree);
204132718Skanstatic void write_method_parms (tree, const int, const tree);
205132718Skanstatic void write_class_enum_type (const tree);
206132718Skanstatic void write_template_args (tree);
207132718Skanstatic void write_expression (tree);
208132718Skanstatic void write_template_arg_literal (const tree);
209132718Skanstatic void write_template_arg (tree);
210132718Skanstatic void write_template_template_arg (const tree);
211132718Skanstatic void write_array_type (const tree);
212132718Skanstatic void write_pointer_to_member_type (const tree);
213132718Skanstatic void write_template_param (const tree);
214132718Skanstatic void write_template_template_param (const tree);
215132718Skanstatic void write_substitution (const int);
216132718Skanstatic int discriminator_for_local_entity (tree);
217132718Skanstatic int discriminator_for_string_literal (tree, tree);
218132718Skanstatic void write_discriminator (const int);
219132718Skanstatic void write_local_name (const tree, const tree, const tree);
220132718Skanstatic void dump_substitution_candidates (void);
221132718Skanstatic const char *mangle_decl_string (const tree);
22290075Sobrien
22390075Sobrien/* Control functions.  */
22490075Sobrien
225169689Skanstatic inline void start_mangling (const tree, bool);
226132718Skanstatic inline const char *finish_mangling (const bool);
227132718Skanstatic tree mangle_special_for_type (const tree, const char *);
22890075Sobrien
229117395Skan/* Foreign language functions.  */
23090075Sobrien
231132718Skanstatic void write_java_integer_type_codes (const tree);
23290075Sobrien
23390075Sobrien/* Append a single character to the end of the mangled
23490075Sobrien   representation.  */
235169689Skan#define write_char(CHAR)						\
236169689Skan  obstack_1grow (mangle_obstack, (CHAR))
23790075Sobrien
238117395Skan/* Append a sized buffer to the end of the mangled representation.  */
239169689Skan#define write_chars(CHAR, LEN)						\
240169689Skan  obstack_grow (mangle_obstack, (CHAR), (LEN))
24190075Sobrien
24290075Sobrien/* Append a NUL-terminated string to the end of the mangled
24390075Sobrien   representation.  */
244169689Skan#define write_string(STRING)						\
245169689Skan  obstack_grow (mangle_obstack, (STRING), strlen (STRING))
24690075Sobrien
247117395Skan/* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
24890075Sobrien   same purpose (context, which may be a type) and value (template
24990075Sobrien   decl).  See write_template_prefix for more information on what this
25090075Sobrien   is used for.  */
251169689Skan#define NESTED_TEMPLATE_MATCH(NODE1, NODE2)				\
252169689Skan  (TREE_CODE (NODE1) == TREE_LIST					\
253169689Skan   && TREE_CODE (NODE2) == TREE_LIST					\
254169689Skan   && ((TYPE_P (TREE_PURPOSE (NODE1))					\
255169689Skan	&& same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))	\
256169689Skan       || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))			\
25790075Sobrien   && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
25890075Sobrien
25990075Sobrien/* Write out an unsigned quantity in base 10.  */
260169689Skan#define write_unsigned_number(NUMBER)					\
26190075Sobrien  write_number ((NUMBER), /*unsigned_p=*/1, 10)
26290075Sobrien
263169689Skan/* Save the current (incomplete) mangled name and release the obstack
264169689Skan   storage holding it.  This function should be used during mangling
265169689Skan   when making a call that could result in a call to get_identifier,
266169689Skan   as such a call will clobber the same obstack being used for
267169689Skan   mangling.  This function may not be called twice without an
268169689Skan   intervening call to restore_partially_mangled_name.  */
269169689Skan
270169689Skanstatic void
271169689Skansave_partially_mangled_name (void)
272169689Skan{
273169689Skan  if (mangle_obstack == &ident_hash->stack)
274169689Skan    {
275169689Skan      gcc_assert (!partially_mangled_name);
276169689Skan      partially_mangled_name_len = obstack_object_size (mangle_obstack);
277169689Skan      partially_mangled_name = XNEWVEC (char, partially_mangled_name_len);
278169689Skan      memcpy (partially_mangled_name, obstack_base (mangle_obstack),
279169689Skan	      partially_mangled_name_len);
280169689Skan      obstack_free (mangle_obstack, obstack_finish (mangle_obstack));
281169689Skan    }
282169689Skan}
283169689Skan
284169689Skan/* Restore the incomplete mangled name saved with
285169689Skan   save_partially_mangled_name.  */
286169689Skan
287169689Skanstatic void
288169689Skanrestore_partially_mangled_name (void)
289169689Skan{
290169689Skan  if (partially_mangled_name)
291169689Skan    {
292169689Skan      obstack_grow (mangle_obstack, partially_mangled_name,
293169689Skan		    partially_mangled_name_len);
294169689Skan      free (partially_mangled_name);
295169689Skan      partially_mangled_name = NULL;
296169689Skan    }
297169689Skan}
298169689Skan
299117395Skan/* If DECL is a template instance, return nonzero and, if
30090075Sobrien   TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
30190075Sobrien   Otherwise return zero.  */
30290075Sobrien
30390075Sobrienstatic int
304132718Skandecl_is_template_id (const tree decl, tree* const template_info)
30590075Sobrien{
30690075Sobrien  if (TREE_CODE (decl) == TYPE_DECL)
30790075Sobrien    {
30890075Sobrien      /* TYPE_DECLs are handled specially.  Look at its type to decide
30990075Sobrien	 if this is a template instantiation.  */
310132718Skan      const tree type = TREE_TYPE (decl);
31190075Sobrien
31290075Sobrien      if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
31390075Sobrien	{
31490075Sobrien	  if (template_info != NULL)
31590075Sobrien	    /* For a templated TYPE_DECL, the template info is hanging
31690075Sobrien	       off the type.  */
317117395Skan	    *template_info = TYPE_TEMPLATE_INFO (type);
31890075Sobrien	  return 1;
31990075Sobrien	}
320169689Skan    }
32190075Sobrien  else
32290075Sobrien    {
32390075Sobrien      /* Check if this is a primary template.  */
32490075Sobrien      if (DECL_LANG_SPECIFIC (decl) != NULL
32590075Sobrien	  && DECL_USE_TEMPLATE (decl)
32690075Sobrien	  && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
32790075Sobrien	  && TREE_CODE (decl) != TEMPLATE_DECL)
32890075Sobrien	{
32990075Sobrien	  if (template_info != NULL)
33090075Sobrien	    /* For most templated decls, the template info is hanging
33190075Sobrien	       off the decl.  */
33290075Sobrien	    *template_info = DECL_TEMPLATE_INFO (decl);
33390075Sobrien	  return 1;
33490075Sobrien	}
33590075Sobrien    }
33690075Sobrien
33790075Sobrien  /* It's not a template id.  */
33890075Sobrien  return 0;
33990075Sobrien}
34090075Sobrien
34190075Sobrien/* Produce debugging output of current substitution candidates.  */
34290075Sobrien
34390075Sobrienstatic void
344132718Skandump_substitution_candidates (void)
34590075Sobrien{
34690075Sobrien  unsigned i;
347169689Skan  tree el;
34890075Sobrien
34990075Sobrien  fprintf (stderr, "  ++ substitutions  ");
350169689Skan  for (i = 0; VEC_iterate (tree, G.substitutions, i, el); ++i)
35190075Sobrien    {
35290075Sobrien      const char *name = "???";
35390075Sobrien
35490075Sobrien      if (i > 0)
35590075Sobrien	fprintf (stderr, "                    ");
35690075Sobrien      if (DECL_P (el))
35790075Sobrien	name = IDENTIFIER_POINTER (DECL_NAME (el));
35890075Sobrien      else if (TREE_CODE (el) == TREE_LIST)
35990075Sobrien	name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
36090075Sobrien      else if (TYPE_NAME (el))
36190075Sobrien	name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
36290075Sobrien      fprintf (stderr, " S%d_ = ", i - 1);
363169689Skan      if (TYPE_P (el) &&
364169689Skan	  (CP_TYPE_RESTRICT_P (el)
365169689Skan	   || CP_TYPE_VOLATILE_P (el)
36690075Sobrien	   || CP_TYPE_CONST_P (el)))
36790075Sobrien	fprintf (stderr, "CV-");
368169689Skan      fprintf (stderr, "%s (%s at %p)\n",
36990075Sobrien	       name, tree_code_name[TREE_CODE (el)], (void *) el);
37090075Sobrien    }
37190075Sobrien}
37290075Sobrien
37390075Sobrien/* Both decls and types can be substitution candidates, but sometimes
37490075Sobrien   they refer to the same thing.  For instance, a TYPE_DECL and
37590075Sobrien   RECORD_TYPE for the same class refer to the same thing, and should
376132718Skan   be treated accordingly in substitutions.  This function returns a
37790075Sobrien   canonicalized tree node representing NODE that is used when adding
37890075Sobrien   and substitution candidates and finding matches.  */
37990075Sobrien
38090075Sobrienstatic inline tree
381132718Skancanonicalize_for_substitution (tree node)
38290075Sobrien{
38390075Sobrien  /* For a TYPE_DECL, use the type instead.  */
38490075Sobrien  if (TREE_CODE (node) == TYPE_DECL)
38590075Sobrien    node = TREE_TYPE (node);
38690075Sobrien  if (TYPE_P (node))
38790075Sobrien    node = canonical_type_variant (node);
38890075Sobrien
38990075Sobrien  return node;
39090075Sobrien}
39190075Sobrien
39290075Sobrien/* Add NODE as a substitution candidate.  NODE must not already be on
39390075Sobrien   the list of candidates.  */
39490075Sobrien
39590075Sobrienstatic void
396132718Skanadd_substitution (tree node)
39790075Sobrien{
39890075Sobrien  tree c;
39990075Sobrien
40090075Sobrien  if (DEBUG_MANGLE)
401169689Skan    fprintf (stderr, "  ++ add_substitution (%s at %10p)\n",
40290075Sobrien	     tree_code_name[TREE_CODE (node)], (void *) node);
40390075Sobrien
40490075Sobrien  /* Get the canonicalized substitution candidate for NODE.  */
40590075Sobrien  c = canonicalize_for_substitution (node);
40690075Sobrien  if (DEBUG_MANGLE && c != node)
40790075Sobrien    fprintf (stderr, "  ++ using candidate (%s at %10p)\n",
40890075Sobrien	     tree_code_name[TREE_CODE (node)], (void *) node);
40990075Sobrien  node = c;
41090075Sobrien
41190075Sobrien#if ENABLE_CHECKING
41290075Sobrien  /* Make sure NODE isn't already a candidate.  */
41390075Sobrien  {
41490075Sobrien    int i;
415169689Skan    tree candidate;
416169689Skan
417169689Skan    for (i = 0; VEC_iterate (tree, G.substitutions, i, candidate); i++)
41890075Sobrien      {
419169689Skan	gcc_assert (!(DECL_P (node) && node == candidate));
420169689Skan	gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
421169689Skan		      && same_type_p (node, candidate)));
42290075Sobrien      }
42390075Sobrien  }
42490075Sobrien#endif /* ENABLE_CHECKING */
42590075Sobrien
42690075Sobrien  /* Put the decl onto the varray of substitution candidates.  */
427169689Skan  VEC_safe_push (tree, gc, G.substitutions, node);
42890075Sobrien
42990075Sobrien  if (DEBUG_MANGLE)
43090075Sobrien    dump_substitution_candidates ();
43190075Sobrien}
43290075Sobrien
433117395Skan/* Helper function for find_substitution.  Returns nonzero if NODE,
43490075Sobrien   which may be a decl or a CLASS_TYPE, is a template-id with template
43590075Sobrien   name of substitution_index[INDEX] in the ::std namespace.  */
43690075Sobrien
437169689Skanstatic inline int
438132718Skanis_std_substitution (const tree node,
439132718Skan		     const substitution_identifier_index_t index)
44090075Sobrien{
44190075Sobrien  tree type = NULL;
44290075Sobrien  tree decl = NULL;
44390075Sobrien
44490075Sobrien  if (DECL_P (node))
44590075Sobrien    {
44690075Sobrien      type = TREE_TYPE (node);
44790075Sobrien      decl = node;
44890075Sobrien    }
44990075Sobrien  else if (CLASS_TYPE_P (node))
45090075Sobrien    {
45190075Sobrien      type = node;
45290075Sobrien      decl = TYPE_NAME (node);
45390075Sobrien    }
454169689Skan  else
45590075Sobrien    /* These are not the droids you're looking for.  */
45690075Sobrien    return 0;
45790075Sobrien
45890075Sobrien  return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
459169689Skan	  && TYPE_LANG_SPECIFIC (type)
460117395Skan	  && TYPE_TEMPLATE_INFO (type)
461169689Skan	  && (DECL_NAME (TYPE_TI_TEMPLATE (type))
46290075Sobrien	      == subst_identifiers[index]));
46390075Sobrien}
46490075Sobrien
465117395Skan/* Helper function for find_substitution.  Returns nonzero if NODE,
46690075Sobrien   which may be a decl or a CLASS_TYPE, is the template-id
46790075Sobrien   ::std::identifier<char>, where identifier is
46890075Sobrien   substitution_index[INDEX].  */
46990075Sobrien
47090075Sobrienstatic inline int
471132718Skanis_std_substitution_char (const tree node,
472132718Skan			  const substitution_identifier_index_t index)
47390075Sobrien{
47490075Sobrien  tree args;
47590075Sobrien  /* Check NODE's name is ::std::identifier.  */
47690075Sobrien  if (!is_std_substitution (node, index))
47790075Sobrien    return 0;
47890075Sobrien  /* Figure out its template args.  */
47990075Sobrien  if (DECL_P (node))
480169689Skan    args = DECL_TI_ARGS (node);
48190075Sobrien  else if (CLASS_TYPE_P (node))
48290075Sobrien    args = CLASSTYPE_TI_ARGS (node);
48390075Sobrien  else
48490075Sobrien    /* Oops, not a template.  */
48590075Sobrien    return 0;
48690075Sobrien  /* NODE's template arg list should be <char>.  */
487169689Skan  return
48890075Sobrien    TREE_VEC_LENGTH (args) == 1
48990075Sobrien    && TREE_VEC_ELT (args, 0) == char_type_node;
49090075Sobrien}
49190075Sobrien
49290075Sobrien/* Check whether a substitution should be used to represent NODE in
49390075Sobrien   the mangling.
49490075Sobrien
49590075Sobrien   First, check standard special-case substitutions.
49690075Sobrien
497169689Skan     <substitution> ::= St
498169689Skan	 # ::std
49990075Sobrien
500169689Skan		    ::= Sa
50190075Sobrien	 # ::std::allocator
50290075Sobrien
503169689Skan		    ::= Sb
504169689Skan	 # ::std::basic_string
50590075Sobrien
506169689Skan		    ::= Ss
507169689Skan	 # ::std::basic_string<char,
50890075Sobrien			       ::std::char_traits<char>,
50990075Sobrien			       ::std::allocator<char> >
51090075Sobrien
511169689Skan		    ::= Si
512169689Skan	 # ::std::basic_istream<char, ::std::char_traits<char> >
51390075Sobrien
514169689Skan		    ::= So
515169689Skan	 # ::std::basic_ostream<char, ::std::char_traits<char> >
51690075Sobrien
517169689Skan		    ::= Sd
518169689Skan	 # ::std::basic_iostream<char, ::std::char_traits<char> >
51990075Sobrien
52090075Sobrien   Then examine the stack of currently available substitution
52190075Sobrien   candidates for entities appearing earlier in the same mangling
52290075Sobrien
52390075Sobrien   If a substitution is found, write its mangled representation and
524117395Skan   return nonzero.  If none is found, just return zero.  */
52590075Sobrien
52690075Sobrienstatic int
527132718Skanfind_substitution (tree node)
52890075Sobrien{
52990075Sobrien  int i;
530169689Skan  const int size = VEC_length (tree, G.substitutions);
53190075Sobrien  tree decl;
53290075Sobrien  tree type;
53390075Sobrien
53490075Sobrien  if (DEBUG_MANGLE)
53590075Sobrien    fprintf (stderr, "  ++ find_substitution (%s at %p)\n",
53690075Sobrien	     tree_code_name[TREE_CODE (node)], (void *) node);
53790075Sobrien
53890075Sobrien  /* Obtain the canonicalized substitution representation for NODE.
53990075Sobrien     This is what we'll compare against.  */
54090075Sobrien  node = canonicalize_for_substitution (node);
54190075Sobrien
54290075Sobrien  /* Check for builtin substitutions.  */
54390075Sobrien
54490075Sobrien  decl = TYPE_P (node) ? TYPE_NAME (node) : node;
54590075Sobrien  type = TYPE_P (node) ? node : TREE_TYPE (node);
54690075Sobrien
54790075Sobrien  /* Check for std::allocator.  */
548169689Skan  if (decl
54990075Sobrien      && is_std_substitution (decl, SUBID_ALLOCATOR)
55090075Sobrien      && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
55190075Sobrien    {
55290075Sobrien      write_string ("Sa");
55390075Sobrien      return 1;
55490075Sobrien    }
55590075Sobrien
55690075Sobrien  /* Check for std::basic_string.  */
55790075Sobrien  if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
55890075Sobrien    {
55990075Sobrien      if (TYPE_P (node))
56090075Sobrien	{
561169689Skan	  /* If this is a type (i.e. a fully-qualified template-id),
562169689Skan	     check for
563169689Skan		 std::basic_string <char,
564169689Skan				    std::char_traits<char>,
56590075Sobrien				    std::allocator<char> > .  */
56690075Sobrien	  if (cp_type_quals (type) == TYPE_UNQUALIFIED
56790075Sobrien	      && CLASSTYPE_USE_TEMPLATE (type))
56890075Sobrien	    {
56990075Sobrien	      tree args = CLASSTYPE_TI_ARGS (type);
57090075Sobrien	      if (TREE_VEC_LENGTH (args) == 3
57190075Sobrien		  && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
57290075Sobrien		  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
57390075Sobrien					       SUBID_CHAR_TRAITS)
57490075Sobrien		  && is_std_substitution_char (TREE_VEC_ELT (args, 2),
57590075Sobrien					       SUBID_ALLOCATOR))
57690075Sobrien		{
57790075Sobrien		  write_string ("Ss");
57890075Sobrien		  return 1;
57990075Sobrien		}
58090075Sobrien	    }
58190075Sobrien	}
58290075Sobrien      else
58390075Sobrien	/* Substitute for the template name only if this isn't a type.  */
58490075Sobrien	{
58590075Sobrien	  write_string ("Sb");
58690075Sobrien	  return 1;
58790075Sobrien	}
58890075Sobrien    }
58990075Sobrien
59090075Sobrien  /* Check for basic_{i,o,io}stream.  */
59190075Sobrien  if (TYPE_P (node)
59290075Sobrien      && cp_type_quals (type) == TYPE_UNQUALIFIED
59390075Sobrien      && CLASS_TYPE_P (type)
59490075Sobrien      && CLASSTYPE_USE_TEMPLATE (type)
59590075Sobrien      && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
59690075Sobrien    {
597169689Skan      /* First, check for the template
59890075Sobrien	 args <char, std::char_traits<char> > .  */
59990075Sobrien      tree args = CLASSTYPE_TI_ARGS (type);
60090075Sobrien      if (TREE_VEC_LENGTH (args) == 2
601132718Skan	  && TYPE_P (TREE_VEC_ELT (args, 0))
60290075Sobrien	  && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
60390075Sobrien	  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
60490075Sobrien				       SUBID_CHAR_TRAITS))
60590075Sobrien	{
60690075Sobrien	  /* Got them.  Is this basic_istream?  */
607169689Skan	  if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
60890075Sobrien	    {
60990075Sobrien	      write_string ("Si");
61090075Sobrien	      return 1;
61190075Sobrien	    }
61290075Sobrien	  /* Or basic_ostream?  */
613169689Skan	  else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
61490075Sobrien	    {
61590075Sobrien	      write_string ("So");
61690075Sobrien	      return 1;
61790075Sobrien	    }
61890075Sobrien	  /* Or basic_iostream?  */
619169689Skan	  else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
62090075Sobrien	    {
62190075Sobrien	      write_string ("Sd");
62290075Sobrien	      return 1;
62390075Sobrien	    }
62490075Sobrien	}
62590075Sobrien    }
62690075Sobrien
62790075Sobrien  /* Check for namespace std.  */
62890075Sobrien  if (decl && DECL_NAMESPACE_STD_P (decl))
62990075Sobrien    {
63090075Sobrien      write_string ("St");
63190075Sobrien      return 1;
63290075Sobrien    }
63390075Sobrien
63490075Sobrien  /* Now check the list of available substitutions for this mangling
635117395Skan     operation.  */
63690075Sobrien  for (i = 0; i < size; ++i)
63790075Sobrien    {
638169689Skan      tree candidate = VEC_index (tree, G.substitutions, i);
63990075Sobrien      /* NODE is a matched to a candidate if it's the same decl node or
64090075Sobrien	 if it's the same type.  */
64190075Sobrien      if (decl == candidate
64290075Sobrien	  || (TYPE_P (candidate) && type && TYPE_P (type)
64390075Sobrien	      && same_type_p (type, candidate))
64490075Sobrien	  || NESTED_TEMPLATE_MATCH (node, candidate))
64590075Sobrien	{
64690075Sobrien	  write_substitution (i);
64790075Sobrien	  return 1;
64890075Sobrien	}
64990075Sobrien    }
65090075Sobrien
65190075Sobrien  /* No substitution found.  */
65290075Sobrien  return 0;
65390075Sobrien}
65490075Sobrien
65590075Sobrien
656132718Skan/* TOP_LEVEL is true, if this is being called at outermost level of
657132718Skan  mangling. It should be false when mangling a decl appearing in an
658132718Skan  expression within some other mangling.
659169689Skan
660132718Skan  <mangled-name>      ::= _Z <encoding>  */
66190075Sobrien
662132718Skanstatic void
663132718Skanwrite_mangled_name (const tree decl, bool top_level)
66490075Sobrien{
66590075Sobrien  MANGLE_TRACE_TREE ("mangled-name", decl);
66690075Sobrien
667132718Skan  if (/* The names of `extern "C"' functions are not mangled.  */
668132718Skan      DECL_EXTERN_C_FUNCTION_P (decl)
669132718Skan      /* But overloaded operator names *are* mangled.  */
670132718Skan      && !DECL_OVERLOADED_OPERATOR_P (decl))
671132718Skan    {
672132718Skan    unmangled_name:;
673169689Skan
674132718Skan      if (top_level)
675132718Skan	write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
676132718Skan      else
677132718Skan	{
678132718Skan	  /* The standard notes: "The <encoding> of an extern "C"
679169689Skan	     function is treated like global-scope data, i.e. as its
680169689Skan	     <source-name> without a type."  We cannot write
681169689Skan	     overloaded operators that way though, because it contains
682169689Skan	     characters invalid in assembler.  */
683132718Skan	  if (abi_version_at_least (2))
684132718Skan	    write_string ("_Z");
685132718Skan	  else
686132718Skan	    G.need_abi_warning = true;
687132718Skan	  write_source_name (DECL_NAME (decl));
688132718Skan	}
689132718Skan    }
690132718Skan  else if (TREE_CODE (decl) == VAR_DECL
691259705Spfg	   /* The names of non-static global variables aren't mangled.  */
692259705Spfg	   && DECL_EXTERNAL_LINKAGE_P (decl)
693132718Skan	   && (CP_DECL_CONTEXT (decl) == global_namespace
694132718Skan	       /* And neither are `extern "C"' variables.  */
695132718Skan	       || DECL_EXTERN_C_P (decl)))
696132718Skan    {
697132718Skan      if (top_level || abi_version_at_least (2))
698132718Skan	goto unmangled_name;
699132718Skan      else
700132718Skan	{
701132718Skan	  G.need_abi_warning = true;
702132718Skan	  goto mangled_name;
703132718Skan	}
704132718Skan    }
70590075Sobrien  else
70690075Sobrien    {
707132718Skan    mangled_name:;
70890075Sobrien      write_string ("_Z");
70990075Sobrien      write_encoding (decl);
710132718Skan      if (DECL_LANG_SPECIFIC (decl)
711132718Skan	  && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
712132718Skan	      || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
713132718Skan	/* We need a distinct mangled name for these entities, but
714132718Skan	   we should never actually output it.  So, we append some
715132718Skan	   characters the assembler won't like.  */
716132718Skan	write_string (" *INTERNAL* ");
71790075Sobrien    }
71890075Sobrien}
71990075Sobrien
72090075Sobrien/*   <encoding>		::= <function name> <bare-function-type>
72190075Sobrien			::= <data name>  */
72290075Sobrien
72390075Sobrienstatic void
724132718Skanwrite_encoding (const tree decl)
72590075Sobrien{
72690075Sobrien  MANGLE_TRACE_TREE ("encoding", decl);
72790075Sobrien
72890075Sobrien  if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
72990075Sobrien    {
73090075Sobrien      /* For overloaded operators write just the mangled name
73190075Sobrien	 without arguments.  */
73290075Sobrien      if (DECL_OVERLOADED_OPERATOR_P (decl))
73390075Sobrien	write_name (decl, /*ignore_local_scope=*/0);
73490075Sobrien      else
73590075Sobrien	write_source_name (DECL_NAME (decl));
73690075Sobrien      return;
73790075Sobrien    }
73890075Sobrien
73990075Sobrien  write_name (decl, /*ignore_local_scope=*/0);
74090075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL)
74190075Sobrien    {
74290075Sobrien      tree fn_type;
743132718Skan      tree d;
74490075Sobrien
74590075Sobrien      if (decl_is_template_id (decl, NULL))
746132718Skan	{
747169689Skan	  save_partially_mangled_name ();
748132718Skan	  fn_type = get_mostly_instantiated_function_type (decl);
749169689Skan	  restore_partially_mangled_name ();
750169689Skan	  /* FN_TYPE will not have parameter types for in-charge or
751169689Skan	     VTT parameters.  Therefore, we pass NULL_TREE to
752169689Skan	     write_bare_function_type -- otherwise, it will get
753169689Skan	     confused about which artificial parameters to skip.  */
754132718Skan	  d = NULL_TREE;
755132718Skan	}
75690075Sobrien      else
757132718Skan	{
758132718Skan	  fn_type = TREE_TYPE (decl);
759132718Skan	  d = decl;
760132718Skan	}
76190075Sobrien
762169689Skan      write_bare_function_type (fn_type,
76390075Sobrien				(!DECL_CONSTRUCTOR_P (decl)
76490075Sobrien				 && !DECL_DESTRUCTOR_P (decl)
76590075Sobrien				 && !DECL_CONV_FN_P (decl)
76690075Sobrien				 && decl_is_template_id (decl, NULL)),
767132718Skan				d);
76890075Sobrien    }
76990075Sobrien}
77090075Sobrien
77190075Sobrien/* <name> ::= <unscoped-name>
772169689Skan	  ::= <unscoped-template-name> <template-args>
77390075Sobrien	  ::= <nested-name>
774169689Skan	  ::= <local-name>
77590075Sobrien
776117395Skan   If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
77790075Sobrien   called from <local-name>, which mangles the enclosing scope
77890075Sobrien   elsewhere and then uses this function to mangle just the part
77990075Sobrien   underneath the function scope.  So don't use the <local-name>
78090075Sobrien   production, to avoid an infinite recursion.  */
78190075Sobrien
78290075Sobrienstatic void
783132718Skanwrite_name (tree decl, const int ignore_local_scope)
78490075Sobrien{
78590075Sobrien  tree context;
78690075Sobrien
78790075Sobrien  MANGLE_TRACE_TREE ("name", decl);
78890075Sobrien
78990075Sobrien  if (TREE_CODE (decl) == TYPE_DECL)
79090075Sobrien    {
79190075Sobrien      /* In case this is a typedef, fish out the corresponding
79290075Sobrien	 TYPE_DECL for the main variant.  */
79390075Sobrien      decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
79490075Sobrien      context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
79590075Sobrien    }
79690075Sobrien  else
79790075Sobrien    context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
79890075Sobrien
79990075Sobrien  /* A decl in :: or ::std scope is treated specially.  The former is
80090075Sobrien     mangled using <unscoped-name> or <unscoped-template-name>, the
80190075Sobrien     latter with a special substitution.  Also, a name that is
80290075Sobrien     directly in a local function scope is also mangled with
80390075Sobrien     <unscoped-name> rather than a full <nested-name>.  */
804169689Skan  if (context == NULL
805169689Skan      || context == global_namespace
80690075Sobrien      || DECL_NAMESPACE_STD_P (context)
80790075Sobrien      || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
80890075Sobrien    {
80990075Sobrien      tree template_info;
81090075Sobrien      /* Is this a template instance?  */
81190075Sobrien      if (decl_is_template_id (decl, &template_info))
81290075Sobrien	{
81390075Sobrien	  /* Yes: use <unscoped-template-name>.  */
81490075Sobrien	  write_unscoped_template_name (TI_TEMPLATE (template_info));
81590075Sobrien	  write_template_args (TI_ARGS (template_info));
81690075Sobrien	}
81790075Sobrien      else
81890075Sobrien	/* Everything else gets an <unqualified-name>.  */
81990075Sobrien	write_unscoped_name (decl);
82090075Sobrien    }
82190075Sobrien  else
82290075Sobrien    {
82390075Sobrien      /* Handle local names, unless we asked not to (that is, invoked
824169689Skan	 under <local-name>, to handle only the part of the name under
825169689Skan	 the local scope).  */
82690075Sobrien      if (!ignore_local_scope)
827169689Skan	{
82890075Sobrien	  /* Scan up the list of scope context, looking for a
82990075Sobrien	     function.  If we find one, this entity is in local
83090075Sobrien	     function scope.  local_entity tracks context one scope
83190075Sobrien	     level down, so it will contain the element that's
83290075Sobrien	     directly in that function's scope, either decl or one of
83390075Sobrien	     its enclosing scopes.  */
83490075Sobrien	  tree local_entity = decl;
83590075Sobrien	  while (context != NULL && context != global_namespace)
83690075Sobrien	    {
83790075Sobrien	      /* Make sure we're always dealing with decls.  */
83890075Sobrien	      if (context != NULL && TYPE_P (context))
83990075Sobrien		context = TYPE_NAME (context);
84090075Sobrien	      /* Is this a function?  */
84190075Sobrien	      if (TREE_CODE (context) == FUNCTION_DECL)
84290075Sobrien		{
84390075Sobrien		  /* Yes, we have local scope.  Use the <local-name>
84490075Sobrien		     production for the innermost function scope.  */
84590075Sobrien		  write_local_name (context, local_entity, decl);
84690075Sobrien		  return;
84790075Sobrien		}
84890075Sobrien	      /* Up one scope level.  */
84990075Sobrien	      local_entity = context;
85090075Sobrien	      context = CP_DECL_CONTEXT (context);
85190075Sobrien	    }
85290075Sobrien
85390075Sobrien	  /* No local scope found?  Fall through to <nested-name>.  */
85490075Sobrien	}
85590075Sobrien
85690075Sobrien      /* Other decls get a <nested-name> to encode their scope.  */
85790075Sobrien      write_nested_name (decl);
85890075Sobrien    }
85990075Sobrien}
86090075Sobrien
86190075Sobrien/* <unscoped-name> ::= <unqualified-name>
862169689Skan		   ::= St <unqualified-name>   # ::std::  */
86390075Sobrien
86490075Sobrienstatic void
865132718Skanwrite_unscoped_name (const tree decl)
86690075Sobrien{
86790075Sobrien  tree context = CP_DECL_CONTEXT (decl);
86890075Sobrien
86990075Sobrien  MANGLE_TRACE_TREE ("unscoped-name", decl);
87090075Sobrien
87190075Sobrien  /* Is DECL in ::std?  */
87290075Sobrien  if (DECL_NAMESPACE_STD_P (context))
87390075Sobrien    {
87490075Sobrien      write_string ("St");
87590075Sobrien      write_unqualified_name (decl);
87690075Sobrien    }
877169689Skan  else
878169689Skan    {
879169689Skan      /* If not, it should be either in the global namespace, or directly
880169689Skan	 in a local function scope.  */
881169689Skan      gcc_assert (context == global_namespace
882169689Skan		  || context == NULL
883169689Skan		  || TREE_CODE (context) == FUNCTION_DECL);
884169689Skan
885169689Skan      write_unqualified_name (decl);
886169689Skan    }
88790075Sobrien}
88890075Sobrien
88990075Sobrien/* <unscoped-template-name> ::= <unscoped-name>
890169689Skan			    ::= <substitution>  */
89190075Sobrien
89290075Sobrienstatic void
893132718Skanwrite_unscoped_template_name (const tree decl)
89490075Sobrien{
89590075Sobrien  MANGLE_TRACE_TREE ("unscoped-template-name", decl);
89690075Sobrien
89790075Sobrien  if (find_substitution (decl))
89890075Sobrien    return;
89990075Sobrien  write_unscoped_name (decl);
90090075Sobrien  add_substitution (decl);
90190075Sobrien}
90290075Sobrien
90390075Sobrien/* Write the nested name, including CV-qualifiers, of DECL.
90490075Sobrien
905169689Skan   <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
906169689Skan		 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
90790075Sobrien
90890075Sobrien   <CV-qualifiers> ::= [r] [V] [K]  */
90990075Sobrien
91090075Sobrienstatic void
911132718Skanwrite_nested_name (const tree decl)
91290075Sobrien{
91390075Sobrien  tree template_info;
91490075Sobrien
91590075Sobrien  MANGLE_TRACE_TREE ("nested-name", decl);
91690075Sobrien
91790075Sobrien  write_char ('N');
918169689Skan
91990075Sobrien  /* Write CV-qualifiers, if this is a member function.  */
920169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL
92190075Sobrien      && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
92290075Sobrien    {
92390075Sobrien      if (DECL_VOLATILE_MEMFUNC_P (decl))
92490075Sobrien	write_char ('V');
92590075Sobrien      if (DECL_CONST_MEMFUNC_P (decl))
92690075Sobrien	write_char ('K');
92790075Sobrien    }
92890075Sobrien
92990075Sobrien  /* Is this a template instance?  */
93090075Sobrien  if (decl_is_template_id (decl, &template_info))
93190075Sobrien    {
93290075Sobrien      /* Yes, use <template-prefix>.  */
93390075Sobrien      write_template_prefix (decl);
93490075Sobrien      write_template_args (TI_ARGS (template_info));
93590075Sobrien    }
93690075Sobrien  else
93790075Sobrien    {
93890075Sobrien      /* No, just use <prefix>  */
93990075Sobrien      write_prefix (DECL_CONTEXT (decl));
94090075Sobrien      write_unqualified_name (decl);
94190075Sobrien    }
94290075Sobrien  write_char ('E');
94390075Sobrien}
94490075Sobrien
945117395Skan/* <prefix> ::= <prefix> <unqualified-name>
946169689Skan	    ::= <template-param>
947169689Skan	    ::= <template-prefix> <template-args>
94890075Sobrien	    ::= # empty
94990075Sobrien	    ::= <substitution>  */
95090075Sobrien
95190075Sobrienstatic void
952132718Skanwrite_prefix (const tree node)
95390075Sobrien{
95490075Sobrien  tree decl;
95590075Sobrien  /* Non-NULL if NODE represents a template-id.  */
95690075Sobrien  tree template_info = NULL;
95790075Sobrien
95890075Sobrien  MANGLE_TRACE_TREE ("prefix", node);
95990075Sobrien
96090075Sobrien  if (node == NULL
96190075Sobrien      || node == global_namespace)
96290075Sobrien    return;
96390075Sobrien
96490075Sobrien  if (find_substitution (node))
96590075Sobrien    return;
96690075Sobrien
96790075Sobrien  if (DECL_P (node))
96890075Sobrien    {
96990075Sobrien      /* If this is a function decl, that means we've hit function
97090075Sobrien	 scope, so this prefix must be for a local name.  In this
97190075Sobrien	 case, we're under the <local-name> production, which encodes
97290075Sobrien	 the enclosing function scope elsewhere.  So don't continue
97390075Sobrien	 here.  */
97490075Sobrien      if (TREE_CODE (node) == FUNCTION_DECL)
97590075Sobrien	return;
97690075Sobrien
97790075Sobrien      decl = node;
97890075Sobrien      decl_is_template_id (decl, &template_info);
97990075Sobrien    }
98090075Sobrien  else
98190075Sobrien    {
982117395Skan      /* Node is a type.  */
98390075Sobrien      decl = TYPE_NAME (node);
98490075Sobrien      if (CLASSTYPE_TEMPLATE_ID_P (node))
985117395Skan	template_info = TYPE_TEMPLATE_INFO (node);
98690075Sobrien    }
98790075Sobrien
988107590Sobrien  /* In G++ 3.2, the name of the template parameter was used.  */
989169689Skan  if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
990117395Skan      && !abi_version_at_least (2))
991107590Sobrien    G.need_abi_warning = true;
992107590Sobrien
993117395Skan  if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
994117395Skan      && abi_version_at_least (2))
995117395Skan    write_template_param (node);
996117395Skan  else if (template_info != NULL)
99790075Sobrien    /* Templated.  */
99890075Sobrien    {
99990075Sobrien      write_template_prefix (decl);
100090075Sobrien      write_template_args (TI_ARGS (template_info));
100190075Sobrien    }
100290075Sobrien  else
100390075Sobrien    /* Not templated.  */
100490075Sobrien    {
100590075Sobrien      write_prefix (CP_DECL_CONTEXT (decl));
100690075Sobrien      write_unqualified_name (decl);
100790075Sobrien    }
100890075Sobrien
100990075Sobrien  add_substitution (node);
101090075Sobrien}
101190075Sobrien
101290075Sobrien/* <template-prefix> ::= <prefix> <template component>
1013169689Skan		     ::= <template-param>
1014169689Skan		     ::= <substitution>  */
101590075Sobrien
101690075Sobrienstatic void
1017132718Skanwrite_template_prefix (const tree node)
101890075Sobrien{
101990075Sobrien  tree decl = DECL_P (node) ? node : TYPE_NAME (node);
102090075Sobrien  tree type = DECL_P (node) ? TREE_TYPE (node) : node;
102190075Sobrien  tree context = CP_DECL_CONTEXT (decl);
102290075Sobrien  tree template_info;
102390075Sobrien  tree template;
102490075Sobrien  tree substitution;
102590075Sobrien
102690075Sobrien  MANGLE_TRACE_TREE ("template-prefix", node);
102790075Sobrien
102890075Sobrien  /* Find the template decl.  */
102990075Sobrien  if (decl_is_template_id (decl, &template_info))
103090075Sobrien    template = TI_TEMPLATE (template_info);
103190075Sobrien  else
1032169689Skan    {
1033169689Skan      gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
103490075Sobrien
1035169689Skan      template = TYPE_TI_TEMPLATE (type);
1036169689Skan    }
1037169689Skan
103890075Sobrien  /* For a member template, though, the template name for the
103990075Sobrien     innermost name must have all the outer template levels
104090075Sobrien     instantiated.  For instance, consider
104190075Sobrien
104290075Sobrien       template<typename T> struct Outer {
104390075Sobrien	 template<typename U> struct Inner {};
104490075Sobrien       };
104590075Sobrien
104690075Sobrien     The template name for `Inner' in `Outer<int>::Inner<float>' is
104790075Sobrien     `Outer<int>::Inner<U>'.  In g++, we don't instantiate the template
104890075Sobrien     levels separately, so there's no TEMPLATE_DECL available for this
104990075Sobrien     (there's only `Outer<T>::Inner<U>').
105090075Sobrien
105190075Sobrien     In order to get the substitutions right, we create a special
105290075Sobrien     TREE_LIST to represent the substitution candidate for a nested
105390075Sobrien     template.  The TREE_PURPOSE is the template's context, fully
105490075Sobrien     instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
105590075Sobrien     template.
105690075Sobrien
105790075Sobrien     So, for the example above, `Outer<int>::Inner' is represented as a
105890075Sobrien     substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
105990075Sobrien     and whose value is `Outer<T>::Inner<U>'.  */
106090075Sobrien  if (TYPE_P (context))
106190075Sobrien    substitution = build_tree_list (context, template);
106290075Sobrien  else
106390075Sobrien    substitution = template;
106490075Sobrien
106590075Sobrien  if (find_substitution (substitution))
106690075Sobrien    return;
106790075Sobrien
1068107590Sobrien  /* In G++ 3.2, the name of the template template parameter was used.  */
1069117395Skan  if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1070117395Skan      && !abi_version_at_least (2))
1071107590Sobrien    G.need_abi_warning = true;
1072107590Sobrien
1073117395Skan  if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1074117395Skan      && abi_version_at_least (2))
1075117395Skan    write_template_param (TREE_TYPE (template));
1076117395Skan  else
1077117395Skan    {
1078117395Skan      write_prefix (context);
1079117395Skan      write_unqualified_name (decl);
1080117395Skan    }
108190075Sobrien
108290075Sobrien  add_substitution (substitution);
108390075Sobrien}
108490075Sobrien
108590075Sobrien/* We don't need to handle thunks, vtables, or VTTs here.  Those are
1086169689Skan   mangled through special entry points.
108790075Sobrien
108890075Sobrien    <unqualified-name>  ::= <operator-name>
1089169689Skan			::= <special-name>
1090259705Spfg			::= <source-name>
1091259705Spfg			::= <local-source-name>
109290075Sobrien
1093259705Spfg    <local-source-name>	::= L <source-name> <discriminator> */
1094259705Spfg
109590075Sobrienstatic void
1096132718Skanwrite_unqualified_name (const tree decl)
109790075Sobrien{
109890075Sobrien  MANGLE_TRACE_TREE ("unqualified-name", decl);
109990075Sobrien
110090075Sobrien  if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
110190075Sobrien    write_special_name_constructor (decl);
110290075Sobrien  else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
110390075Sobrien    write_special_name_destructor (decl);
1104132718Skan  else if (DECL_NAME (decl) == NULL_TREE)
1105132718Skan    write_source_name (DECL_ASSEMBLER_NAME (decl));
1106169689Skan  else if (DECL_CONV_FN_P (decl))
110790075Sobrien    {
1108169689Skan      /* Conversion operator. Handle it right here.
1109169689Skan	   <operator> ::= cv <type>  */
1110102780Skan      tree type;
1111102780Skan      if (decl_is_template_id (decl, NULL))
1112102780Skan	{
1113169689Skan	  tree fn_type;
1114169689Skan	  save_partially_mangled_name ();
1115169689Skan	  fn_type = get_mostly_instantiated_function_type (decl);
1116169689Skan	  restore_partially_mangled_name ();
1117102780Skan	  type = TREE_TYPE (fn_type);
1118102780Skan	}
1119102780Skan      else
1120117395Skan	type = DECL_CONV_FN_TYPE (decl);
1121117395Skan      write_conversion_operator_name (type);
112290075Sobrien    }
112390075Sobrien  else if (DECL_OVERLOADED_OPERATOR_P (decl))
112490075Sobrien    {
112590075Sobrien      operator_name_info_t *oni;
112690075Sobrien      if (DECL_ASSIGNMENT_OPERATOR_P (decl))
112790075Sobrien	oni = assignment_operator_name_info;
112890075Sobrien      else
112990075Sobrien	oni = operator_name_info;
1130169689Skan
113190075Sobrien      write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
113290075Sobrien    }
1133259705Spfg  else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1134259705Spfg	   && DECL_NAMESPACE_SCOPE_P (decl)
1135259705Spfg	   && decl_linkage (decl) == lk_internal)
1136259705Spfg    {
1137259705Spfg      MANGLE_TRACE_TREE ("local-source-name", decl);
1138259705Spfg      write_char ('L');
1139259705Spfg      write_source_name (DECL_NAME (decl));
1140259705Spfg      /* The default discriminator is 1, and that's all we ever use,
1141259705Spfg	 so there's no code to output one here.  */
1142259705Spfg    }
114390075Sobrien  else
114490075Sobrien    write_source_name (DECL_NAME (decl));
114590075Sobrien}
114690075Sobrien
1147117395Skan/* Write the unqualified-name for a conversion operator to TYPE.  */
1148117395Skan
1149117395Skanstatic void
1150132718Skanwrite_conversion_operator_name (const tree type)
1151117395Skan{
1152117395Skan  write_string ("cv");
1153117395Skan  write_type (type);
1154117395Skan}
1155117395Skan
1156169689Skan/* Non-terminal <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.
115790075Sobrien
115890075Sobrien     <source-name> ::= </length/ number> <identifier>  */
115990075Sobrien
116090075Sobrienstatic void
1161132718Skanwrite_source_name (tree identifier)
116290075Sobrien{
116390075Sobrien  MANGLE_TRACE_TREE ("source-name", identifier);
116490075Sobrien
116590075Sobrien  /* Never write the whole template-id name including the template
116690075Sobrien     arguments; we only want the template name.  */
116790075Sobrien  if (IDENTIFIER_TEMPLATE (identifier))
116890075Sobrien    identifier = IDENTIFIER_TEMPLATE (identifier);
116990075Sobrien
117090075Sobrien  write_unsigned_number (IDENTIFIER_LENGTH (identifier));
117190075Sobrien  write_identifier (IDENTIFIER_POINTER (identifier));
117290075Sobrien}
117390075Sobrien
117490075Sobrien/* Convert NUMBER to ascii using base BASE and generating at least
117590075Sobrien   MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
117690075Sobrien   into which to store the characters. Returns the number of
117790075Sobrien   characters generated (these will be layed out in advance of where
117890075Sobrien   BUFFER points).  */
117990075Sobrien
118090075Sobrienstatic int
1181132718Skanhwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1182132718Skan		char *buffer, const unsigned int min_digits)
118390075Sobrien{
118490075Sobrien  static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
118590075Sobrien  unsigned digits = 0;
1186169689Skan
118790075Sobrien  while (number)
118890075Sobrien    {
118990075Sobrien      unsigned HOST_WIDE_INT d = number / base;
1190169689Skan
119190075Sobrien      *--buffer = base_digits[number - d * base];
119290075Sobrien      digits++;
119390075Sobrien      number = d;
119490075Sobrien    }
119590075Sobrien  while (digits < min_digits)
119690075Sobrien    {
119790075Sobrien      *--buffer = base_digits[0];
119890075Sobrien      digits++;
119990075Sobrien    }
120090075Sobrien  return digits;
120190075Sobrien}
120290075Sobrien
120390075Sobrien/* Non-terminal <number>.
120490075Sobrien
120590075Sobrien     <number> ::= [n] </decimal integer/>  */
120690075Sobrien
120790075Sobrienstatic void
1208132718Skanwrite_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1209132718Skan	      const unsigned int base)
121090075Sobrien{
121190075Sobrien  char buffer[sizeof (HOST_WIDE_INT) * 8];
121290075Sobrien  unsigned count = 0;
121390075Sobrien
121490075Sobrien  if (!unsigned_p && (HOST_WIDE_INT) number < 0)
121590075Sobrien    {
121690075Sobrien      write_char ('n');
121790075Sobrien      number = -((HOST_WIDE_INT) number);
121890075Sobrien    }
121990075Sobrien  count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
122090075Sobrien  write_chars (buffer + sizeof (buffer) - count, count);
122190075Sobrien}
122290075Sobrien
122390075Sobrien/* Write out an integral CST in decimal. Most numbers are small, and
122490075Sobrien   representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1225117395Skan   bigger than that, which we must deal with.  */
122690075Sobrien
122790075Sobrienstatic inline void
1228132718Skanwrite_integer_cst (const tree cst)
122990075Sobrien{
123090075Sobrien  int sign = tree_int_cst_sgn (cst);
123190075Sobrien
123290075Sobrien  if (TREE_INT_CST_HIGH (cst) + (sign < 0))
123390075Sobrien    {
123490075Sobrien      /* A bignum. We do this in chunks, each of which fits in a
1235117395Skan	 HOST_WIDE_INT.  */
123690075Sobrien      char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
123790075Sobrien      unsigned HOST_WIDE_INT chunk;
123890075Sobrien      unsigned chunk_digits;
123990075Sobrien      char *ptr = buffer + sizeof (buffer);
124090075Sobrien      unsigned count = 0;
124190075Sobrien      tree n, base, type;
124290075Sobrien      int done;
124390075Sobrien
124490075Sobrien      /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1245117395Skan	 representable.  */
124690075Sobrien      chunk = 1000000000;
124790075Sobrien      chunk_digits = 9;
1248169689Skan
124990075Sobrien      if (sizeof (HOST_WIDE_INT) >= 8)
125090075Sobrien	{
1251117395Skan	  /* It is at least 64 bits, so 10^18 is representable.  */
125290075Sobrien	  chunk_digits = 18;
125390075Sobrien	  chunk *= chunk;
125490075Sobrien	}
1255169689Skan
1256117395Skan      type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1257169689Skan      base = build_int_cstu (type, chunk);
1258169689Skan      n = build_int_cst_wide (type,
1259169689Skan			      TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
126090075Sobrien
126190075Sobrien      if (sign < 0)
126290075Sobrien	{
126390075Sobrien	  write_char ('n');
1264169689Skan	  n = fold_build1 (NEGATE_EXPR, type, n);
126590075Sobrien	}
126690075Sobrien      do
126790075Sobrien	{
1268169689Skan	  tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1269169689Skan	  tree tmp = fold_build2 (MULT_EXPR, type, d, base);
127090075Sobrien	  unsigned c;
1271132718Skan
127290075Sobrien	  done = integer_zerop (d);
1273169689Skan	  tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
127490075Sobrien	  c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1275169689Skan			      done ? 1 : chunk_digits);
127690075Sobrien	  ptr -= c;
127790075Sobrien	  count += c;
127890075Sobrien	  n = d;
127990075Sobrien	}
128090075Sobrien      while (!done);
128190075Sobrien      write_chars (ptr, count);
128290075Sobrien    }
1283169689Skan  else
128490075Sobrien    {
128590075Sobrien      /* A small num.  */
128690075Sobrien      unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1287169689Skan
128890075Sobrien      if (sign < 0)
128990075Sobrien	{
129090075Sobrien	  write_char ('n');
129190075Sobrien	  low = -low;
129290075Sobrien	}
129390075Sobrien      write_unsigned_number (low);
129490075Sobrien    }
129590075Sobrien}
129690075Sobrien
1297169689Skan/* Write out a floating-point literal.
1298169689Skan
1299117395Skan    "Floating-point literals are encoded using the bit pattern of the
1300117395Skan    target processor's internal representation of that number, as a
1301117395Skan    fixed-length lowercase hexadecimal string, high-order bytes first
1302117395Skan    (even if the target processor would store low-order bytes first).
1303117395Skan    The "n" prefix is not used for floating-point literals; the sign
1304117395Skan    bit is encoded with the rest of the number.
1305117395Skan
1306117395Skan    Here are some examples, assuming the IEEE standard representation
1307117395Skan    for floating point numbers.  (Spaces are for readability, not
1308117395Skan    part of the encoding.)
1309117395Skan
1310169689Skan	1.0f			Lf 3f80 0000 E
1311169689Skan       -1.0f			Lf bf80 0000 E
1312169689Skan	1.17549435e-38f		Lf 0080 0000 E
1313169689Skan	1.40129846e-45f		Lf 0000 0001 E
1314169689Skan	0.0f			Lf 0000 0000 E"
1315117395Skan
1316117395Skan   Caller is responsible for the Lx and the E.  */
1317117395Skanstatic void
1318132718Skanwrite_real_cst (const tree value)
1319117395Skan{
1320117395Skan  if (abi_version_at_least (2))
1321117395Skan    {
1322117395Skan      long target_real[4];  /* largest supported float */
1323117395Skan      char buffer[9];       /* eight hex digits in a 32-bit number */
1324117395Skan      int i, limit, dir;
1325117395Skan
1326117395Skan      tree type = TREE_TYPE (value);
1327117395Skan      int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1328117395Skan
1329117395Skan      real_to_target (target_real, &TREE_REAL_CST (value),
1330117395Skan		      TYPE_MODE (type));
1331117395Skan
1332117395Skan      /* The value in target_real is in the target word order,
1333169689Skan	 so we must write it out backward if that happens to be
1334117395Skan	 little-endian.  write_number cannot be used, it will
1335117395Skan	 produce uppercase.  */
1336117395Skan      if (FLOAT_WORDS_BIG_ENDIAN)
1337117395Skan	i = 0, limit = words, dir = 1;
1338117395Skan      else
1339117395Skan	i = words - 1, limit = -1, dir = -1;
1340117395Skan
1341117395Skan      for (; i != limit; i += dir)
1342117395Skan	{
1343259948Spfg	  sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1344117395Skan	  write_chars (buffer, 8);
1345117395Skan	}
1346117395Skan    }
1347117395Skan  else
1348117395Skan    {
1349117395Skan      /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1350117395Skan	 literally.  Note that compatibility with 3.2 is impossible,
1351117395Skan	 because the old floating-point emulator used a different
1352117395Skan	 format for REAL_VALUE_TYPE.  */
1353117395Skan      size_t i;
1354117395Skan      for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1355169689Skan	write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1356117395Skan		      /*unsigned_p*/ 1,
1357117395Skan		      /*base*/ 16);
1358117395Skan      G.need_abi_warning = 1;
1359117395Skan    }
1360117395Skan}
1361117395Skan
136290075Sobrien/* Non-terminal <identifier>.
136390075Sobrien
136490075Sobrien     <identifier> ::= </unqualified source code identifier>  */
136590075Sobrien
136690075Sobrienstatic void
1367132718Skanwrite_identifier (const char *identifier)
136890075Sobrien{
136990075Sobrien  MANGLE_TRACE ("identifier", identifier);
137090075Sobrien  write_string (identifier);
137190075Sobrien}
137290075Sobrien
137390075Sobrien/* Handle constructor productions of non-terminal <special-name>.
1374169689Skan   CTOR is a constructor FUNCTION_DECL.
137590075Sobrien
137690075Sobrien     <special-name> ::= C1   # complete object constructor
1377169689Skan		    ::= C2   # base object constructor
1378169689Skan		    ::= C3   # complete object allocating constructor
137990075Sobrien
1380169689Skan   Currently, allocating constructors are never used.
138190075Sobrien
138290075Sobrien   We also need to provide mangled names for the maybe-in-charge
138390075Sobrien   constructor, so we treat it here too.  mangle_decl_string will
138490075Sobrien   append *INTERNAL* to that, to make sure we never emit it.  */
138590075Sobrien
138690075Sobrienstatic void
1387132718Skanwrite_special_name_constructor (const tree ctor)
138890075Sobrien{
1389169689Skan  if (DECL_BASE_CONSTRUCTOR_P (ctor))
139090075Sobrien    write_string ("C2");
139190075Sobrien  else
1392169689Skan    {
1393169689Skan      gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1394169689Skan		  /* Even though we don't ever emit a definition of
1395169689Skan		     the old-style destructor, we still have to
1396169689Skan		     consider entities (like static variables) nested
1397169689Skan		     inside it.  */
1398169689Skan		  || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1399169689Skan      write_string ("C1");
1400169689Skan    }
140190075Sobrien}
140290075Sobrien
140390075Sobrien/* Handle destructor productions of non-terminal <special-name>.
1404169689Skan   DTOR is a destructor FUNCTION_DECL.
140590075Sobrien
140690075Sobrien     <special-name> ::= D0 # deleting (in-charge) destructor
1407169689Skan		    ::= D1 # complete object (in-charge) destructor
1408169689Skan		    ::= D2 # base object (not-in-charge) destructor
140990075Sobrien
141090075Sobrien   We also need to provide mangled names for the maybe-incharge
141190075Sobrien   destructor, so we treat it here too.  mangle_decl_string will
141290075Sobrien   append *INTERNAL* to that, to make sure we never emit it.  */
141390075Sobrien
141490075Sobrienstatic void
1415132718Skanwrite_special_name_destructor (const tree dtor)
141690075Sobrien{
141790075Sobrien  if (DECL_DELETING_DESTRUCTOR_P (dtor))
141890075Sobrien    write_string ("D0");
141990075Sobrien  else if (DECL_BASE_DESTRUCTOR_P (dtor))
142090075Sobrien    write_string ("D2");
142190075Sobrien  else
1422169689Skan    {
1423169689Skan      gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1424169689Skan		  /* Even though we don't ever emit a definition of
1425169689Skan		     the old-style destructor, we still have to
1426169689Skan		     consider entities (like static variables) nested
1427169689Skan		     inside it.  */
1428169689Skan		  || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1429169689Skan      write_string ("D1");
1430169689Skan    }
143190075Sobrien}
143290075Sobrien
143390075Sobrien/* Return the discriminator for ENTITY appearing inside
143490075Sobrien   FUNCTION.  The discriminator is the lexical ordinal of VAR among
143590075Sobrien   entities with the same name in the same FUNCTION.  */
143690075Sobrien
143790075Sobrienstatic int
1438132718Skandiscriminator_for_local_entity (tree entity)
143990075Sobrien{
144090075Sobrien  /* Assume this is the only local entity with this name.  */
1441132718Skan  int discriminator = 0;
144290075Sobrien
144390075Sobrien  if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
144490075Sobrien    discriminator = DECL_DISCRIMINATOR (entity);
144590075Sobrien  else if (TREE_CODE (entity) == TYPE_DECL)
144690075Sobrien    {
1447169689Skan      int ix;
1448169689Skan
144990075Sobrien      /* Scan the list of local classes.  */
145090075Sobrien      entity = TREE_TYPE (entity);
1451169689Skan      for (ix = 0; ; ix++)
1452169689Skan	{
1453169689Skan	  tree type = VEC_index (tree, local_classes, ix);
1454169689Skan	  if (type == entity)
1455169689Skan	    break;
1456169689Skan	  if (TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (entity)
1457169689Skan	      && TYPE_CONTEXT (type) == TYPE_CONTEXT (entity))
1458169689Skan	    ++discriminator;
1459169689Skan	}
1460169689Skan    }
146190075Sobrien
146290075Sobrien  return discriminator;
146390075Sobrien}
146490075Sobrien
146590075Sobrien/* Return the discriminator for STRING, a string literal used inside
1466132718Skan   FUNCTION.  The discriminator is the lexical ordinal of STRING among
146790075Sobrien   string literals used in FUNCTION.  */
146890075Sobrien
146990075Sobrienstatic int
1470132718Skandiscriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1471132718Skan				  tree string ATTRIBUTE_UNUSED)
147290075Sobrien{
147390075Sobrien  /* For now, we don't discriminate amongst string literals.  */
147490075Sobrien  return 0;
147590075Sobrien}
147690075Sobrien
1477169689Skan/*   <discriminator> := _ <number>
147890075Sobrien
147990075Sobrien   The discriminator is used only for the second and later occurrences
148090075Sobrien   of the same name within a single function. In this case <number> is
148190075Sobrien   n - 2, if this is the nth occurrence, in lexical order.  */
148290075Sobrien
148390075Sobrienstatic void
1484132718Skanwrite_discriminator (const int discriminator)
148590075Sobrien{
1486117395Skan  /* If discriminator is zero, don't write anything.  Otherwise...  */
148790075Sobrien  if (discriminator > 0)
148890075Sobrien    {
148990075Sobrien      write_char ('_');
149090075Sobrien      write_unsigned_number (discriminator - 1);
149190075Sobrien    }
149290075Sobrien}
149390075Sobrien
149490075Sobrien/* Mangle the name of a function-scope entity.  FUNCTION is the
149590075Sobrien   FUNCTION_DECL for the enclosing function.  ENTITY is the decl for
149690075Sobrien   the entity itself.  LOCAL_ENTITY is the entity that's directly
149790075Sobrien   scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
149890075Sobrien   of ENTITY.
149990075Sobrien
150090075Sobrien     <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1501169689Skan		  := Z <function encoding> E s [<discriminator>]  */
150290075Sobrien
150390075Sobrienstatic void
1504132718Skanwrite_local_name (const tree function, const tree local_entity,
1505132718Skan		  const tree entity)
150690075Sobrien{
150790075Sobrien  MANGLE_TRACE_TREE ("local-name", entity);
150890075Sobrien
150990075Sobrien  write_char ('Z');
151090075Sobrien  write_encoding (function);
151190075Sobrien  write_char ('E');
151290075Sobrien  if (TREE_CODE (entity) == STRING_CST)
151390075Sobrien    {
151490075Sobrien      write_char ('s');
1515169689Skan      write_discriminator (discriminator_for_string_literal (function,
151690075Sobrien							     entity));
151790075Sobrien    }
151890075Sobrien  else
151990075Sobrien    {
152090075Sobrien      /* Now the <entity name>.  Let write_name know its being called
152190075Sobrien	 from <local-name>, so it doesn't try to process the enclosing
152290075Sobrien	 function scope again.  */
152390075Sobrien      write_name (entity, /*ignore_local_scope=*/1);
152490075Sobrien      write_discriminator (discriminator_for_local_entity (local_entity));
152590075Sobrien    }
152690075Sobrien}
152790075Sobrien
1528169689Skan/* Non-terminals <type> and <CV-qualifier>.
152990075Sobrien
153090075Sobrien     <type> ::= <builtin-type>
1531169689Skan	    ::= <function-type>
1532169689Skan	    ::= <class-enum-type>
1533169689Skan	    ::= <array-type>
1534169689Skan	    ::= <pointer-to-member-type>
1535169689Skan	    ::= <template-param>
1536169689Skan	    ::= <substitution>
1537169689Skan	    ::= <CV-qualifier>
1538169689Skan	    ::= P <type>    # pointer-to
1539169689Skan	    ::= R <type>    # reference-to
1540169689Skan	    ::= C <type>    # complex pair (C 2000)
1541169689Skan	    ::= G <type>    # imaginary (C 2000)     [not supported]
1542169689Skan	    ::= U <source-name> <type>   # vendor extended type qualifier
154390075Sobrien
154490075Sobrien   TYPE is a type node.  */
154590075Sobrien
1546169689Skanstatic void
1547132718Skanwrite_type (tree type)
154890075Sobrien{
1549117395Skan  /* This gets set to nonzero if TYPE turns out to be a (possibly
155090075Sobrien     CV-qualified) builtin type.  */
155190075Sobrien  int is_builtin_type = 0;
155290075Sobrien
155390075Sobrien  MANGLE_TRACE_TREE ("type", type);
155490075Sobrien
155590075Sobrien  if (type == error_mark_node)
155690075Sobrien    return;
155790075Sobrien
155890075Sobrien  if (find_substitution (type))
155990075Sobrien    return;
1560169689Skan
156190075Sobrien  if (write_CV_qualifiers_for_type (type) > 0)
156290075Sobrien    /* If TYPE was CV-qualified, we just wrote the qualifiers; now
156390075Sobrien       mangle the unqualified type.  The recursive call is needed here
1564132718Skan       since both the qualified and unqualified types are substitution
156590075Sobrien       candidates.  */
156690075Sobrien    write_type (TYPE_MAIN_VARIANT (type));
156796263Sobrien  else if (TREE_CODE (type) == ARRAY_TYPE)
156896263Sobrien    /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
156996263Sobrien       so that the cv-qualification of the element type is available
157096263Sobrien       in write_array_type.  */
157196263Sobrien    write_array_type (type);
157290075Sobrien  else
157390075Sobrien    {
157490075Sobrien      /* See through any typedefs.  */
157590075Sobrien      type = TYPE_MAIN_VARIANT (type);
157690075Sobrien
1577132718Skan      if (TYPE_PTRMEM_P (type))
1578132718Skan	write_pointer_to_member_type (type);
1579132718Skan      else switch (TREE_CODE (type))
158090075Sobrien	{
158190075Sobrien	case VOID_TYPE:
158290075Sobrien	case BOOLEAN_TYPE:
158390075Sobrien	case INTEGER_TYPE:  /* Includes wchar_t.  */
158490075Sobrien	case REAL_TYPE:
1585146895Skan	{
1586146895Skan	  /* Handle any target-specific fundamental types.  */
1587146895Skan	  const char *target_mangling
1588146895Skan	    = targetm.mangle_fundamental_type (type);
1589146895Skan
1590146895Skan	  if (target_mangling)
1591146895Skan	    {
1592146895Skan	      write_string (target_mangling);
1593146895Skan	      return;
1594146895Skan	    }
1595146895Skan
159690075Sobrien	  /* If this is a typedef, TYPE may not be one of
159790075Sobrien	     the standard builtin type nodes, but an alias of one.  Use
159890075Sobrien	     TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
159990075Sobrien	  write_builtin_type (TYPE_MAIN_VARIANT (type));
160090075Sobrien	  ++is_builtin_type;
160190075Sobrien	  break;
1602146895Skan	}
160390075Sobrien
160490075Sobrien	case COMPLEX_TYPE:
160590075Sobrien	  write_char ('C');
160690075Sobrien	  write_type (TREE_TYPE (type));
160790075Sobrien	  break;
160890075Sobrien
160990075Sobrien	case FUNCTION_TYPE:
161090075Sobrien	case METHOD_TYPE:
161190075Sobrien	  write_function_type (type);
161290075Sobrien	  break;
161390075Sobrien
161490075Sobrien	case UNION_TYPE:
161590075Sobrien	case RECORD_TYPE:
161690075Sobrien	case ENUMERAL_TYPE:
161790075Sobrien	  /* A pointer-to-member function is represented as a special
161890075Sobrien	     RECORD_TYPE, so check for this first.  */
161990075Sobrien	  if (TYPE_PTRMEMFUNC_P (type))
162090075Sobrien	    write_pointer_to_member_type (type);
162190075Sobrien	  else
162290075Sobrien	    write_class_enum_type (type);
162390075Sobrien	  break;
162490075Sobrien
162590075Sobrien	case TYPENAME_TYPE:
162690075Sobrien	case UNBOUND_CLASS_TEMPLATE:
162790075Sobrien	  /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
162890075Sobrien	     ordinary nested names.  */
162990075Sobrien	  write_nested_name (TYPE_STUB_DECL (type));
163090075Sobrien	  break;
163190075Sobrien
163290075Sobrien	case POINTER_TYPE:
1633132718Skan	  write_char ('P');
1634132718Skan	  write_type (TREE_TYPE (type));
163590075Sobrien	  break;
163690075Sobrien
163790075Sobrien	case REFERENCE_TYPE:
163890075Sobrien	  write_char ('R');
163990075Sobrien	  write_type (TREE_TYPE (type));
164090075Sobrien	  break;
164190075Sobrien
164290075Sobrien	case TEMPLATE_TYPE_PARM:
164390075Sobrien	case TEMPLATE_PARM_INDEX:
164490075Sobrien	  write_template_param (type);
164590075Sobrien	  break;
164690075Sobrien
164790075Sobrien	case TEMPLATE_TEMPLATE_PARM:
164890075Sobrien	  write_template_template_param (type);
164990075Sobrien	  break;
165090075Sobrien
165190075Sobrien	case BOUND_TEMPLATE_TEMPLATE_PARM:
165290075Sobrien	  write_template_template_param (type);
1653169689Skan	  write_template_args
165490075Sobrien	    (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
165590075Sobrien	  break;
165690075Sobrien
165790075Sobrien	case VECTOR_TYPE:
165890075Sobrien	  write_string ("U8__vector");
165990075Sobrien	  write_type (TREE_TYPE (type));
166090075Sobrien	  break;
166190075Sobrien
166290075Sobrien	default:
1663169689Skan	  gcc_unreachable ();
166490075Sobrien	}
166590075Sobrien    }
166690075Sobrien
166790075Sobrien  /* Types other than builtin types are substitution candidates.  */
166890075Sobrien  if (!is_builtin_type)
166990075Sobrien    add_substitution (type);
167090075Sobrien}
167190075Sobrien
167290075Sobrien/* Non-terminal <CV-qualifiers> for type nodes.  Returns the number of
167390075Sobrien   CV-qualifiers written for TYPE.
167490075Sobrien
167590075Sobrien     <CV-qualifiers> ::= [r] [V] [K]  */
167690075Sobrien
167790075Sobrienstatic int
1678132718Skanwrite_CV_qualifiers_for_type (const tree type)
167990075Sobrien{
168090075Sobrien  int num_qualifiers = 0;
168190075Sobrien
168290075Sobrien  /* The order is specified by:
168390075Sobrien
168490075Sobrien       "In cases where multiple order-insensitive qualifiers are
168590075Sobrien       present, they should be ordered 'K' (closest to the base type),
1686169689Skan       'V', 'r', and 'U' (farthest from the base type) ..."
168790075Sobrien
168896263Sobrien     Note that we do not use cp_type_quals below; given "const
168996263Sobrien     int[3]", the "const" is emitted with the "int", not with the
169096263Sobrien     array.  */
169196263Sobrien
169296263Sobrien  if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
169390075Sobrien    {
169490075Sobrien      write_char ('r');
169590075Sobrien      ++num_qualifiers;
169690075Sobrien    }
169796263Sobrien  if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
169890075Sobrien    {
169990075Sobrien      write_char ('V');
170090075Sobrien      ++num_qualifiers;
170190075Sobrien    }
170296263Sobrien  if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
170390075Sobrien    {
170490075Sobrien      write_char ('K');
170590075Sobrien      ++num_qualifiers;
170690075Sobrien    }
170790075Sobrien
170890075Sobrien  return num_qualifiers;
170990075Sobrien}
171090075Sobrien
1711169689Skan/* Non-terminal <builtin-type>.
171290075Sobrien
1713169689Skan     <builtin-type> ::= v   # void
1714169689Skan		    ::= b   # bool
1715169689Skan		    ::= w   # wchar_t
1716169689Skan		    ::= c   # char
1717169689Skan		    ::= a   # signed char
1718169689Skan		    ::= h   # unsigned char
1719169689Skan		    ::= s   # short
1720169689Skan		    ::= t   # unsigned short
1721169689Skan		    ::= i   # int
1722169689Skan		    ::= j   # unsigned int
1723169689Skan		    ::= l   # long
1724169689Skan		    ::= m   # unsigned long
1725169689Skan		    ::= x   # long long, __int64
1726169689Skan		    ::= y   # unsigned long long, __int64
1727169689Skan		    ::= n   # __int128
1728169689Skan		    ::= o   # unsigned __int128
1729169689Skan		    ::= f   # float
1730169689Skan		    ::= d   # double
1731169689Skan		    ::= e   # long double, __float80
1732169689Skan		    ::= g   # __float128          [not supported]
1733169689Skan		    ::= u <source-name>  # vendor extended type */
173490075Sobrien
1735169689Skanstatic void
1736132718Skanwrite_builtin_type (tree type)
173790075Sobrien{
173890075Sobrien  switch (TREE_CODE (type))
173990075Sobrien    {
174090075Sobrien    case VOID_TYPE:
174190075Sobrien      write_char ('v');
174290075Sobrien      break;
174390075Sobrien
174490075Sobrien    case BOOLEAN_TYPE:
174590075Sobrien      write_char ('b');
174690075Sobrien      break;
174790075Sobrien
174890075Sobrien    case INTEGER_TYPE:
174990075Sobrien      /* If this is size_t, get the underlying int type.  */
175090075Sobrien      if (TYPE_IS_SIZETYPE (type))
175190075Sobrien	type = TYPE_DOMAIN (type);
175290075Sobrien
175390075Sobrien      /* TYPE may still be wchar_t, since that isn't in
175490075Sobrien	 integer_type_nodes.  */
175590075Sobrien      if (type == wchar_type_node)
175690075Sobrien	write_char ('w');
175790075Sobrien      else if (TYPE_FOR_JAVA (type))
175890075Sobrien	write_java_integer_type_codes (type);
175990075Sobrien      else
176090075Sobrien	{
176190075Sobrien	  size_t itk;
176290075Sobrien	  /* Assume TYPE is one of the shared integer type nodes.  Find
176390075Sobrien	     it in the array of these nodes.  */
176490075Sobrien	iagain:
176590075Sobrien	  for (itk = 0; itk < itk_none; ++itk)
176690075Sobrien	    if (type == integer_types[itk])
176790075Sobrien	      {
176890075Sobrien		/* Print the corresponding single-letter code.  */
176990075Sobrien		write_char (integer_type_codes[itk]);
177090075Sobrien		break;
177190075Sobrien	      }
177296263Sobrien
177390075Sobrien	  if (itk == itk_none)
177490075Sobrien	    {
1775117395Skan	      tree t = c_common_type_for_mode (TYPE_MODE (type),
1776169689Skan					       TYPE_UNSIGNED (type));
1777169689Skan	      if (type != t)
177896263Sobrien		{
1779169689Skan		  type = t;
1780169689Skan		  goto iagain;
178196263Sobrien		}
1782169689Skan
1783169689Skan	      if (TYPE_PRECISION (type) == 128)
1784169689Skan		write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
178596263Sobrien	      else
178696263Sobrien		{
1787169689Skan		  /* Allow for cases where TYPE is not one of the shared
1788169689Skan		     integer type nodes and write a "vendor extended builtin
1789169689Skan		     type" with a name the form intN or uintN, respectively.
1790169689Skan		     Situations like this can happen if you have an
1791169689Skan		     __attribute__((__mode__(__SI__))) type and use exotic
1792169689Skan		     switches like '-mint8' on AVR.  Of course, this is
1793169689Skan		     undefined by the C++ ABI (and '-mint8' is not even
1794169689Skan		     Standard C conforming), but when using such special
1795169689Skan		     options you're pretty much in nowhere land anyway.  */
1796169689Skan		  const char *prefix;
1797169689Skan		  char prec[11];	/* up to ten digits for an unsigned */
1798169689Skan
1799169689Skan		  prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1800169689Skan		  sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1801169689Skan		  write_char ('u');	/* "vendor extended builtin type" */
1802169689Skan		  write_unsigned_number (strlen (prefix) + strlen (prec));
1803169689Skan		  write_string (prefix);
1804169689Skan		  write_string (prec);
180596263Sobrien		}
180690075Sobrien	    }
180790075Sobrien	}
180890075Sobrien      break;
180990075Sobrien
181090075Sobrien    case REAL_TYPE:
181190075Sobrien      if (type == float_type_node
181290075Sobrien	  || type == java_float_type_node)
181390075Sobrien	write_char ('f');
181490075Sobrien      else if (type == double_type_node
181590075Sobrien	       || type == java_double_type_node)
181690075Sobrien	write_char ('d');
181790075Sobrien      else if (type == long_double_type_node)
181890075Sobrien	write_char ('e');
181990075Sobrien      else
1820169689Skan	gcc_unreachable ();
182190075Sobrien      break;
182290075Sobrien
182390075Sobrien    default:
1824169689Skan      gcc_unreachable ();
182590075Sobrien    }
182690075Sobrien}
182790075Sobrien
182890075Sobrien/* Non-terminal <function-type>.  NODE is a FUNCTION_TYPE or
182990075Sobrien   METHOD_TYPE.  The return type is mangled before the parameter
183090075Sobrien   types.
183190075Sobrien
183290075Sobrien     <function-type> ::= F [Y] <bare-function-type> E   */
183390075Sobrien
183490075Sobrienstatic void
1835132718Skanwrite_function_type (const tree type)
183690075Sobrien{
183790075Sobrien  MANGLE_TRACE_TREE ("function-type", type);
183890075Sobrien
183996263Sobrien  /* For a pointer to member function, the function type may have
184096263Sobrien     cv-qualifiers, indicating the quals for the artificial 'this'
184196263Sobrien     parameter.  */
184296263Sobrien  if (TREE_CODE (type) == METHOD_TYPE)
184396263Sobrien    {
184496263Sobrien      /* The first parameter must be a POINTER_TYPE pointing to the
184596263Sobrien	 `this' parameter.  */
184696263Sobrien      tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
184796263Sobrien      write_CV_qualifiers_for_type (this_type);
184896263Sobrien    }
184996263Sobrien
185090075Sobrien  write_char ('F');
185190075Sobrien  /* We don't track whether or not a type is `extern "C"'.  Note that
185290075Sobrien     you can have an `extern "C"' function that does not have
185390075Sobrien     `extern "C"' type, and vice versa:
185490075Sobrien
185590075Sobrien       extern "C" typedef void function_t();
185690075Sobrien       function_t f; // f has C++ linkage, but its type is
1857169689Skan		     // `extern "C"'
185890075Sobrien
185990075Sobrien       typedef void function_t();
186090075Sobrien       extern "C" function_t f; // Vice versa.
186190075Sobrien
186290075Sobrien     See [dcl.link].  */
1863169689Skan  write_bare_function_type (type, /*include_return_type_p=*/1,
186490075Sobrien			    /*decl=*/NULL);
186590075Sobrien  write_char ('E');
186690075Sobrien}
186790075Sobrien
186890075Sobrien/* Non-terminal <bare-function-type>.  TYPE is a FUNCTION_TYPE or
1869117395Skan   METHOD_TYPE.  If INCLUDE_RETURN_TYPE is nonzero, the return value
187090075Sobrien   is mangled before the parameter types.  If non-NULL, DECL is
187190075Sobrien   FUNCTION_DECL for the function whose type is being emitted.
187290075Sobrien
1873169689Skan   If DECL is a member of a Java type, then a literal 'J'
1874169689Skan   is output and the return type is mangled as if INCLUDE_RETURN_TYPE
1875169689Skan   were nonzero.
187690075Sobrien
1877169689Skan     <bare-function-type> ::= [J]</signature/ type>+  */
1878169689Skan
187990075Sobrienstatic void
1880132718Skanwrite_bare_function_type (const tree type, const int include_return_type_p,
1881132718Skan			  const tree decl)
188290075Sobrien{
1883169689Skan  int java_method_p;
1884169689Skan
188590075Sobrien  MANGLE_TRACE_TREE ("bare-function-type", type);
188690075Sobrien
1887169689Skan  /* Detect Java methods and emit special encoding.  */
1888169689Skan  if (decl != NULL
1889169689Skan      && DECL_FUNCTION_MEMBER_P (decl)
1890169689Skan      && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
1891169689Skan      && !DECL_CONSTRUCTOR_P (decl)
1892169689Skan      && !DECL_DESTRUCTOR_P (decl)
1893169689Skan      && !DECL_CONV_FN_P (decl))
1894169689Skan    {
1895169689Skan      java_method_p = 1;
1896169689Skan      write_char ('J');
1897169689Skan    }
1898169689Skan  else
1899169689Skan    {
1900169689Skan      java_method_p = 0;
1901169689Skan    }
1902169689Skan
190390075Sobrien  /* Mangle the return type, if requested.  */
1904169689Skan  if (include_return_type_p || java_method_p)
190590075Sobrien    write_type (TREE_TYPE (type));
190690075Sobrien
190790075Sobrien  /* Now mangle the types of the arguments.  */
1908169689Skan  write_method_parms (TYPE_ARG_TYPES (type),
190990075Sobrien		      TREE_CODE (type) == METHOD_TYPE,
191090075Sobrien		      decl);
191190075Sobrien}
191290075Sobrien
191390075Sobrien/* Write the mangled representation of a method parameter list of
1914117395Skan   types given in PARM_TYPES.  If METHOD_P is nonzero, the function is
191590075Sobrien   considered a non-static method, and the this parameter is omitted.
191690075Sobrien   If non-NULL, DECL is the FUNCTION_DECL for the function whose
191790075Sobrien   parameters are being emitted.  */
191890075Sobrien
191990075Sobrienstatic void
1920132718Skanwrite_method_parms (tree parm_types, const int method_p, const tree decl)
192190075Sobrien{
192290075Sobrien  tree first_parm_type;
192390075Sobrien  tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
192490075Sobrien
192590075Sobrien  /* Assume this parameter type list is variable-length.  If it ends
192690075Sobrien     with a void type, then it's not.  */
192790075Sobrien  int varargs_p = 1;
192890075Sobrien
192990075Sobrien  /* If this is a member function, skip the first arg, which is the
1930169689Skan     this pointer.
193190075Sobrien       "Member functions do not encode the type of their implicit this
1932169689Skan       parameter."
1933169689Skan
193490075Sobrien     Similarly, there's no need to mangle artificial parameters, like
193590075Sobrien     the VTT parameters for constructors and destructors.  */
193690075Sobrien  if (method_p)
193790075Sobrien    {
193890075Sobrien      parm_types = TREE_CHAIN (parm_types);
193990075Sobrien      parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
194090075Sobrien
194190075Sobrien      while (parm_decl && DECL_ARTIFICIAL (parm_decl))
194290075Sobrien	{
194390075Sobrien	  parm_types = TREE_CHAIN (parm_types);
194490075Sobrien	  parm_decl = TREE_CHAIN (parm_decl);
194590075Sobrien	}
194690075Sobrien    }
194790075Sobrien
1948169689Skan  for (first_parm_type = parm_types;
1949169689Skan       parm_types;
195090075Sobrien       parm_types = TREE_CHAIN (parm_types))
195190075Sobrien    {
195290075Sobrien      tree parm = TREE_VALUE (parm_types);
195390075Sobrien      if (parm == void_type_node)
195490075Sobrien	{
195590075Sobrien	  /* "Empty parameter lists, whether declared as () or
195690075Sobrien	     conventionally as (void), are encoded with a void parameter
195790075Sobrien	     (v)."  */
195890075Sobrien	  if (parm_types == first_parm_type)
195990075Sobrien	    write_type (parm);
196090075Sobrien	  /* If the parm list is terminated with a void type, it's
196190075Sobrien	     fixed-length.  */
196290075Sobrien	  varargs_p = 0;
196390075Sobrien	  /* A void type better be the last one.  */
1964169689Skan	  gcc_assert (TREE_CHAIN (parm_types) == NULL);
196590075Sobrien	}
196690075Sobrien      else
196790075Sobrien	write_type (parm);
196890075Sobrien    }
196990075Sobrien
197090075Sobrien  if (varargs_p)
197190075Sobrien    /* <builtin-type> ::= z  # ellipsis  */
197290075Sobrien    write_char ('z');
197390075Sobrien}
197490075Sobrien
197590075Sobrien/* <class-enum-type> ::= <name>  */
197690075Sobrien
1977169689Skanstatic void
1978132718Skanwrite_class_enum_type (const tree type)
197990075Sobrien{
198090075Sobrien  write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
198190075Sobrien}
198290075Sobrien
198390075Sobrien/* Non-terminal <template-args>.  ARGS is a TREE_VEC of template
198490075Sobrien   arguments.
198590075Sobrien
198690075Sobrien     <template-args> ::= I <template-arg>+ E  */
198790075Sobrien
198890075Sobrienstatic void
1989132718Skanwrite_template_args (tree args)
199090075Sobrien{
1991132718Skan  int i;
1992132718Skan  int length = TREE_VEC_LENGTH (args);
1993169689Skan
199490075Sobrien  MANGLE_TRACE_TREE ("template-args", args);
199590075Sobrien
1996117395Skan  write_char ('I');
199790075Sobrien
1998169689Skan  gcc_assert (length > 0);
1999117395Skan
2000132718Skan  if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2001117395Skan    {
2002132718Skan      /* We have nested template args.  We want the innermost template
2003132718Skan	 argument list.  */
2004132718Skan      args = TREE_VEC_ELT (args, length - 1);
2005132718Skan      length = TREE_VEC_LENGTH (args);
2006117395Skan    }
2007132718Skan  for (i = 0; i < length; ++i)
2008132718Skan    write_template_arg (TREE_VEC_ELT (args, i));
2009169689Skan
201090075Sobrien  write_char ('E');
201190075Sobrien}
201290075Sobrien
201390075Sobrien/* <expression> ::= <unary operator-name> <expression>
201490075Sobrien		::= <binary operator-name> <expression> <expression>
201590075Sobrien		::= <expr-primary>
201690075Sobrien
201790075Sobrien   <expr-primary> ::= <template-param>
2018169689Skan		  ::= L <type> <value number> E		# literal
2019169689Skan		  ::= L <mangled-name> E		# external name
2020169689Skan		  ::= sr <type> <unqualified-name>
2021169689Skan		  ::= sr <type> <unqualified-name> <template-args> */
202290075Sobrien
202390075Sobrienstatic void
2024132718Skanwrite_expression (tree expr)
202590075Sobrien{
202690075Sobrien  enum tree_code code;
202790075Sobrien
202890075Sobrien  code = TREE_CODE (expr);
202990075Sobrien
203096263Sobrien  /* Skip NOP_EXPRs.  They can occur when (say) a pointer argument
203196263Sobrien     is converted (via qualification conversions) to another
203296263Sobrien     type.  */
203396263Sobrien  while (TREE_CODE (expr) == NOP_EXPR
203496263Sobrien	 || TREE_CODE (expr) == NON_LVALUE_EXPR)
203596263Sobrien    {
203696263Sobrien      expr = TREE_OPERAND (expr, 0);
203796263Sobrien      code = TREE_CODE (expr);
203896263Sobrien    }
203996263Sobrien
2040169689Skan  if (code == BASELINK)
2041169689Skan    {
2042169689Skan      expr = BASELINK_FUNCTIONS (expr);
2043169689Skan      code = TREE_CODE (expr);
2044169689Skan    }
2045169689Skan
2046169689Skan  /* Handle pointers-to-members by making them look like expression
2047169689Skan     nodes.  */
2048169689Skan  if (code == PTRMEM_CST)
2049169689Skan    {
2050169689Skan      expr = build_nt (ADDR_EXPR,
2051169689Skan		       build_qualified_name (/*type=*/NULL_TREE,
2052169689Skan					     PTRMEM_CST_CLASS (expr),
2053169689Skan					     PTRMEM_CST_MEMBER (expr),
2054169689Skan					     /*template_p=*/false));
2055169689Skan      code = TREE_CODE (expr);
2056169689Skan    }
2057169689Skan
2058117395Skan  /* Handle template parameters.  */
2059169689Skan  if (code == TEMPLATE_TYPE_PARM
206090075Sobrien      || code == TEMPLATE_TEMPLATE_PARM
206190075Sobrien      || code == BOUND_TEMPLATE_TEMPLATE_PARM
206290075Sobrien      || code == TEMPLATE_PARM_INDEX)
206390075Sobrien    write_template_param (expr);
206490075Sobrien  /* Handle literals.  */
2065169689Skan  else if (TREE_CODE_CLASS (code) == tcc_constant
2066117395Skan	   || (abi_version_at_least (2) && code == CONST_DECL))
206790075Sobrien    write_template_arg_literal (expr);
206890075Sobrien  else if (DECL_P (expr))
206990075Sobrien    {
2070107590Sobrien      /* G++ 3.2 incorrectly mangled non-type template arguments of
2071107590Sobrien	 enumeration type using their names.  */
2072107590Sobrien      if (code == CONST_DECL)
2073107590Sobrien	G.need_abi_warning = 1;
207490075Sobrien      write_char ('L');
2075132718Skan      write_mangled_name (expr, false);
207690075Sobrien      write_char ('E');
207790075Sobrien    }
2078169689Skan  else if (TREE_CODE (expr) == SIZEOF_EXPR
2079102780Skan	   && TYPE_P (TREE_OPERAND (expr, 0)))
2080102780Skan    {
2081102780Skan      write_string ("st");
2082102780Skan      write_type (TREE_OPERAND (expr, 0));
2083102780Skan    }
2084117395Skan  else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2085117395Skan    {
2086117395Skan      tree scope = TREE_OPERAND (expr, 0);
2087117395Skan      tree member = TREE_OPERAND (expr, 1);
2088117395Skan
2089117395Skan      /* If the MEMBER is a real declaration, then the qualifying
2090117395Skan	 scope was not dependent.  Ideally, we would not have a
2091117395Skan	 SCOPE_REF in those cases, but sometimes we do.  If the second
2092117395Skan	 argument is a DECL, then the name must not have been
2093117395Skan	 dependent.  */
2094117395Skan      if (DECL_P (member))
2095117395Skan	write_expression (member);
2096117395Skan      else
2097117395Skan	{
2098117395Skan	  tree template_args;
2099117395Skan
2100117395Skan	  write_string ("sr");
2101117395Skan	  write_type (scope);
2102117395Skan	  /* If MEMBER is a template-id, separate the template
2103117395Skan	     from the arguments.  */
2104117395Skan	  if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2105117395Skan	    {
2106117395Skan	      template_args = TREE_OPERAND (member, 1);
2107117395Skan	      member = TREE_OPERAND (member, 0);
2108117395Skan	    }
2109117395Skan	  else
2110117395Skan	    template_args = NULL_TREE;
2111117395Skan	  /* Write out the name of the MEMBER.  */
2112117395Skan	  if (IDENTIFIER_TYPENAME_P (member))
2113117395Skan	    write_conversion_operator_name (TREE_TYPE (member));
2114117395Skan	  else if (IDENTIFIER_OPNAME_P (member))
2115117395Skan	    {
2116117395Skan	      int i;
2117117395Skan	      const char *mangled_name = NULL;
2118117395Skan
2119117395Skan	      /* Unfortunately, there is no easy way to go from the
2120117395Skan		 name of the operator back to the corresponding tree
2121117395Skan		 code.  */
2122117395Skan	      for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
2123117395Skan		if (operator_name_info[i].identifier == member)
2124117395Skan		  {
2125117395Skan		    /* The ABI says that we prefer binary operator
2126117395Skan		       names to unary operator names.  */
2127117395Skan		    if (operator_name_info[i].arity == 2)
2128117395Skan		      {
2129117395Skan			mangled_name = operator_name_info[i].mangled_name;
2130117395Skan			break;
2131117395Skan		      }
2132117395Skan		    else if (!mangled_name)
2133117395Skan		      mangled_name = operator_name_info[i].mangled_name;
2134117395Skan		  }
2135117395Skan		else if (assignment_operator_name_info[i].identifier
2136117395Skan			 == member)
2137117395Skan		  {
2138169689Skan		    mangled_name
2139117395Skan		      = assignment_operator_name_info[i].mangled_name;
2140117395Skan		    break;
2141117395Skan		  }
2142117395Skan	      write_string (mangled_name);
2143117395Skan	    }
2144117395Skan	  else
2145117395Skan	    write_source_name (member);
2146117395Skan	  /* Write out the template arguments.  */
2147117395Skan	  if (template_args)
2148117395Skan	    write_template_args (template_args);
2149117395Skan	}
2150117395Skan    }
215190075Sobrien  else
215290075Sobrien    {
215390075Sobrien      int i;
215490075Sobrien
215590075Sobrien      /* When we bind a variable or function to a non-type template
215690075Sobrien	 argument with reference type, we create an ADDR_EXPR to show
215790075Sobrien	 the fact that the entity's address has been taken.  But, we
215890075Sobrien	 don't actually want to output a mangling code for the `&'.  */
215990075Sobrien      if (TREE_CODE (expr) == ADDR_EXPR
216090075Sobrien	  && TREE_TYPE (expr)
216190075Sobrien	  && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
216290075Sobrien	{
216390075Sobrien	  expr = TREE_OPERAND (expr, 0);
216490075Sobrien	  if (DECL_P (expr))
216590075Sobrien	    {
216690075Sobrien	      write_expression (expr);
216790075Sobrien	      return;
216890075Sobrien	    }
216990075Sobrien
217090075Sobrien	  code = TREE_CODE (expr);
217190075Sobrien	}
2172117395Skan
217390075Sobrien      /* If it wasn't any of those, recursively expand the expression.  */
217490075Sobrien      write_string (operator_name_info[(int) code].mangled_name);
217590075Sobrien
217690075Sobrien      switch (code)
217790075Sobrien	{
2178169689Skan	case CALL_EXPR:
2179169689Skan	  sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
2180169689Skan	  break;
2181117395Skan
218290075Sobrien	case CAST_EXPR:
218390075Sobrien	  write_type (TREE_TYPE (expr));
2184169689Skan	  /* There is no way to mangle a zero-operand cast like
2185169689Skan	     "T()".  */
2186169689Skan	  if (!TREE_OPERAND (expr, 0))
2187169689Skan	    sorry ("zero-operand casts cannot be mangled due to a defect "
2188169689Skan		   "in the C++ ABI");
2189169689Skan	  else
2190169689Skan	    write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
219190075Sobrien	  break;
219290075Sobrien
219390075Sobrien	case STATIC_CAST_EXPR:
219490075Sobrien	case CONST_CAST_EXPR:
219590075Sobrien	  write_type (TREE_TYPE (expr));
219690075Sobrien	  write_expression (TREE_OPERAND (expr, 0));
219790075Sobrien	  break;
219890075Sobrien
2199169689Skan
220090075Sobrien	/* Handle pointers-to-members specially.  */
220190075Sobrien	case SCOPE_REF:
220290075Sobrien	  write_type (TREE_OPERAND (expr, 0));
220390075Sobrien	  if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
220490075Sobrien	    write_source_name (TREE_OPERAND (expr, 1));
2205132718Skan	  else if (TREE_CODE (TREE_OPERAND (expr, 1)) == TEMPLATE_ID_EXPR)
2206132718Skan	    {
2207132718Skan	      tree template_id;
2208132718Skan	      tree name;
2209132718Skan
2210132718Skan	      template_id = TREE_OPERAND (expr, 1);
2211132718Skan	      name = TREE_OPERAND (template_id, 0);
2212132718Skan	      /* FIXME: What about operators?  */
2213169689Skan	      gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2214132718Skan	      write_source_name (TREE_OPERAND (template_id, 0));
2215132718Skan	      write_template_args (TREE_OPERAND (template_id, 1));
2216132718Skan	    }
221790075Sobrien	  else
2218107590Sobrien	    {
2219107590Sobrien	      /* G++ 3.2 incorrectly put out both the "sr" code and
2220107590Sobrien		 the nested name of the qualified name.  */
2221107590Sobrien	      G.need_abi_warning = 1;
2222107590Sobrien	      write_encoding (TREE_OPERAND (expr, 1));
2223107590Sobrien	    }
222490075Sobrien	  break;
222590075Sobrien
222690075Sobrien	default:
222790075Sobrien	  for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
2228132718Skan	    {
2229132718Skan	      tree operand = TREE_OPERAND (expr, i);
2230169689Skan	      /* As a GNU extension, the middle operand of a
2231132718Skan		 conditional may be omitted.  Since expression
2232132718Skan		 manglings are supposed to represent the input token
2233132718Skan		 stream, there's no good way to mangle such an
2234132718Skan		 expression without extending the C++ ABI.  */
2235132718Skan	      if (code == COND_EXPR && i == 1 && !operand)
2236132718Skan		{
2237169689Skan		  error ("omitted middle operand to %<?:%> operand "
2238132718Skan			 "cannot be mangled");
2239132718Skan		  continue;
2240132718Skan		}
2241132718Skan	      write_expression (operand);
2242132718Skan	    }
224390075Sobrien	}
224490075Sobrien    }
224590075Sobrien}
224690075Sobrien
2247169689Skan/* Literal subcase of non-terminal <template-arg>.
224890075Sobrien
224990075Sobrien     "Literal arguments, e.g. "A<42L>", are encoded with their type
225090075Sobrien     and value. Negative integer values are preceded with "n"; for
225190075Sobrien     example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2252117395Skan     encoded as 0, true as 1."  */
225390075Sobrien
225490075Sobrienstatic void
2255132718Skanwrite_template_arg_literal (const tree value)
225690075Sobrien{
225790075Sobrien  write_char ('L');
2258169689Skan  write_type (TREE_TYPE (value));
225990075Sobrien
2260169689Skan  switch (TREE_CODE (value))
226190075Sobrien    {
2262169689Skan    case CONST_DECL:
2263169689Skan      write_integer_cst (DECL_INITIAL (value));
2264169689Skan      break;
2265169689Skan
2266169689Skan    case INTEGER_CST:
2267169689Skan      gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2268169689Skan		  || integer_zerop (value) || integer_onep (value));
2269169689Skan      write_integer_cst (value);
2270169689Skan      break;
2271169689Skan
2272169689Skan    case REAL_CST:
2273169689Skan      write_real_cst (value);
2274169689Skan      break;
2275169689Skan
2276169689Skan    default:
2277169689Skan      gcc_unreachable ();
227890075Sobrien    }
227990075Sobrien
228090075Sobrien  write_char ('E');
228190075Sobrien}
228290075Sobrien
2283169689Skan/* Non-terminal <template-arg>.
228490075Sobrien
2285169689Skan     <template-arg> ::= <type>				# type
2286169689Skan		    ::= L <type> </value/ number> E	# literal
2287169689Skan		    ::= LZ <name> E			# external name
2288169689Skan		    ::= X <expression> E		# expression  */
228990075Sobrien
229090075Sobrienstatic void
2291132718Skanwrite_template_arg (tree node)
229290075Sobrien{
229390075Sobrien  enum tree_code code = TREE_CODE (node);
229490075Sobrien
229590075Sobrien  MANGLE_TRACE_TREE ("template-arg", node);
229690075Sobrien
2297132718Skan  /* A template template parameter's argument list contains TREE_LIST
2298169689Skan     nodes of which the value field is the actual argument.  */
229990075Sobrien  if (code == TREE_LIST)
230090075Sobrien    {
230190075Sobrien      node = TREE_VALUE (node);
230290075Sobrien      /* If it's a decl, deal with its type instead.  */
230390075Sobrien      if (DECL_P (node))
230490075Sobrien	{
230590075Sobrien	  node = TREE_TYPE (node);
230690075Sobrien	  code = TREE_CODE (node);
230790075Sobrien	}
230890075Sobrien    }
2309169689Skan
2310132718Skan  if (TREE_CODE (node) == NOP_EXPR
2311132718Skan      && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2312132718Skan    {
2313132718Skan      /* Template parameters can be of reference type. To maintain
2314132718Skan	 internal consistency, such arguments use a conversion from
2315132718Skan	 address of object to reference type.  */
2316169689Skan      gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
2317132718Skan      if (abi_version_at_least (2))
2318132718Skan	node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2319132718Skan      else
2320132718Skan	G.need_abi_warning = 1;
2321132718Skan    }
232290075Sobrien
232390075Sobrien  if (TYPE_P (node))
232490075Sobrien    write_type (node);
232590075Sobrien  else if (code == TEMPLATE_DECL)
232690075Sobrien    /* A template appearing as a template arg is a template template arg.  */
232790075Sobrien    write_template_template_arg (node);
2328169689Skan  else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
2329117395Skan	   || (abi_version_at_least (2) && code == CONST_DECL))
2330117395Skan    write_template_arg_literal (node);
233190075Sobrien  else if (DECL_P (node))
233290075Sobrien    {
2333169689Skan      /* Until ABI version 2, non-type template arguments of
2334169689Skan	 enumeration type were mangled using their names.  */
2335169689Skan      if (code == CONST_DECL && !abi_version_at_least (2))
2336107590Sobrien	G.need_abi_warning = 1;
233790075Sobrien      write_char ('L');
2338169689Skan      /* Until ABI version 3, the underscore before the mangled name
2339169689Skan	 was incorrectly omitted.  */
2340169689Skan      if (!abi_version_at_least (3))
2341169689Skan	{
2342169689Skan	  G.need_abi_warning = 1;
2343169689Skan	  write_char ('Z');
2344169689Skan	}
2345169689Skan      else
2346169689Skan	write_string ("_Z");
234790075Sobrien      write_encoding (node);
234890075Sobrien      write_char ('E');
234990075Sobrien    }
235090075Sobrien  else
235190075Sobrien    {
235290075Sobrien      /* Template arguments may be expressions.  */
235390075Sobrien      write_char ('X');
235490075Sobrien      write_expression (node);
235590075Sobrien      write_char ('E');
235690075Sobrien    }
235790075Sobrien}
235890075Sobrien
235990075Sobrien/*  <template-template-arg>
236090075Sobrien			::= <name>
236190075Sobrien			::= <substitution>  */
236290075Sobrien
2363132718Skanstatic void
2364132718Skanwrite_template_template_arg (const tree decl)
236590075Sobrien{
236690075Sobrien  MANGLE_TRACE_TREE ("template-template-arg", decl);
236790075Sobrien
236890075Sobrien  if (find_substitution (decl))
236990075Sobrien    return;
237090075Sobrien  write_name (decl, /*ignore_local_scope=*/0);
237190075Sobrien  add_substitution (decl);
237290075Sobrien}
237390075Sobrien
237490075Sobrien
2375169689Skan/* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.
237690075Sobrien
2377169689Skan     <array-type> ::= A [</dimension/ number>] _ </element/ type>
2378169689Skan		  ::= A <expression> _ </element/ type>
237990075Sobrien
238090075Sobrien     "Array types encode the dimension (number of elements) and the
238190075Sobrien     element type. For variable length arrays, the dimension (but not
238290075Sobrien     the '_' separator) is omitted."  */
238390075Sobrien
2384132718Skanstatic void
2385132718Skanwrite_array_type (const tree type)
238690075Sobrien{
238790075Sobrien  write_char ('A');
238890075Sobrien  if (TYPE_DOMAIN (type))
238990075Sobrien    {
239090075Sobrien      tree index_type;
239190075Sobrien      tree max;
239290075Sobrien
239390075Sobrien      index_type = TYPE_DOMAIN (type);
239490075Sobrien      /* The INDEX_TYPE gives the upper and lower bounds of the
239590075Sobrien	 array.  */
239690075Sobrien      max = TYPE_MAX_VALUE (index_type);
239790075Sobrien      if (TREE_CODE (max) == INTEGER_CST)
239890075Sobrien	{
239990075Sobrien	  /* The ABI specifies that we should mangle the number of
240090075Sobrien	     elements in the array, not the largest allowed index.  */
240190075Sobrien	  max = size_binop (PLUS_EXPR, max, size_one_node);
240290075Sobrien	  write_unsigned_number (tree_low_cst (max, 1));
240390075Sobrien	}
240490075Sobrien      else
2405132718Skan	{
2406132718Skan	  max = TREE_OPERAND (max, 0);
2407132718Skan	  if (!abi_version_at_least (2))
2408132718Skan	    {
2409132718Skan	      /* value_dependent_expression_p presumes nothing is
2410169689Skan		 dependent when PROCESSING_TEMPLATE_DECL is zero.  */
2411132718Skan	      ++processing_template_decl;
2412132718Skan	      if (!value_dependent_expression_p (max))
2413132718Skan		G.need_abi_warning = 1;
2414132718Skan	      --processing_template_decl;
2415132718Skan	    }
2416132718Skan	  write_expression (max);
2417132718Skan	}
2418169689Skan
241990075Sobrien    }
242090075Sobrien  write_char ('_');
242190075Sobrien  write_type (TREE_TYPE (type));
242290075Sobrien}
242390075Sobrien
242490075Sobrien/* Non-terminal <pointer-to-member-type> for pointer-to-member
242590075Sobrien   variables.  TYPE is a pointer-to-member POINTER_TYPE.
242690075Sobrien
242790075Sobrien     <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
242890075Sobrien
242990075Sobrienstatic void
2430132718Skanwrite_pointer_to_member_type (const tree type)
243190075Sobrien{
243290075Sobrien  write_char ('M');
243396263Sobrien  write_type (TYPE_PTRMEM_CLASS_TYPE (type));
243490075Sobrien  write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
243590075Sobrien}
243690075Sobrien
243790075Sobrien/* Non-terminal <template-param>.  PARM is a TEMPLATE_TYPE_PARM,
243890075Sobrien   TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
243990075Sobrien   TEMPLATE_PARM_INDEX.
244090075Sobrien
2441132718Skan     <template-param> ::= T </parameter/ number> _  */
244290075Sobrien
244390075Sobrienstatic void
2444132718Skanwrite_template_param (const tree parm)
244590075Sobrien{
244690075Sobrien  int parm_index;
2447117395Skan  int parm_level;
2448117395Skan  tree parm_type = NULL_TREE;
244990075Sobrien
245090075Sobrien  MANGLE_TRACE_TREE ("template-parm", parm);
245190075Sobrien
245290075Sobrien  switch (TREE_CODE (parm))
245390075Sobrien    {
245490075Sobrien    case TEMPLATE_TYPE_PARM:
245590075Sobrien    case TEMPLATE_TEMPLATE_PARM:
245690075Sobrien    case BOUND_TEMPLATE_TEMPLATE_PARM:
245790075Sobrien      parm_index = TEMPLATE_TYPE_IDX (parm);
2458117395Skan      parm_level = TEMPLATE_TYPE_LEVEL (parm);
245990075Sobrien      break;
246090075Sobrien
246190075Sobrien    case TEMPLATE_PARM_INDEX:
246290075Sobrien      parm_index = TEMPLATE_PARM_IDX (parm);
2463117395Skan      parm_level = TEMPLATE_PARM_LEVEL (parm);
2464117395Skan      parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
246590075Sobrien      break;
246690075Sobrien
246790075Sobrien    default:
2468169689Skan      gcc_unreachable ();
246990075Sobrien    }
247090075Sobrien
247190075Sobrien  write_char ('T');
247290075Sobrien  /* NUMBER as it appears in the mangling is (-1)-indexed, with the
247390075Sobrien     earliest template param denoted by `_'.  */
247490075Sobrien  if (parm_index > 0)
247590075Sobrien    write_unsigned_number (parm_index - 1);
247690075Sobrien  write_char ('_');
247790075Sobrien}
247890075Sobrien
247990075Sobrien/*  <template-template-param>
2480169689Skan			::= <template-param>
248190075Sobrien			::= <substitution>  */
248290075Sobrien
248390075Sobrienstatic void
2484132718Skanwrite_template_template_param (const tree parm)
248590075Sobrien{
248690075Sobrien  tree template = NULL_TREE;
248790075Sobrien
248890075Sobrien  /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
248990075Sobrien     template template parameter.  The substitution candidate here is
249090075Sobrien     only the template.  */
249190075Sobrien  if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
249290075Sobrien    {
2493169689Skan      template
249490075Sobrien	= TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
249590075Sobrien      if (find_substitution (template))
249690075Sobrien	return;
249790075Sobrien    }
249890075Sobrien
249990075Sobrien  /* <template-param> encodes only the template parameter position,
250090075Sobrien     not its template arguments, which is fine here.  */
250190075Sobrien  write_template_param (parm);
250290075Sobrien  if (template)
250390075Sobrien    add_substitution (template);
250490075Sobrien}
250590075Sobrien
2506169689Skan/* Non-terminal <substitution>.
250790075Sobrien
250890075Sobrien      <substitution> ::= S <seq-id> _
2509169689Skan		     ::= S_  */
251090075Sobrien
251190075Sobrienstatic void
2512132718Skanwrite_substitution (const int seq_id)
251390075Sobrien{
251490075Sobrien  MANGLE_TRACE ("substitution", "");
251590075Sobrien
251690075Sobrien  write_char ('S');
251790075Sobrien  if (seq_id > 0)
251890075Sobrien    write_number (seq_id - 1, /*unsigned=*/1, 36);
251990075Sobrien  write_char ('_');
252090075Sobrien}
252190075Sobrien
2522117395Skan/* Start mangling ENTITY.  */
252390075Sobrien
252490075Sobrienstatic inline void
2525169689Skanstart_mangling (const tree entity, const bool ident_p)
252690075Sobrien{
2527107590Sobrien  G.entity = entity;
2528107590Sobrien  G.need_abi_warning = false;
2529169689Skan  if (!ident_p)
2530169689Skan    {
2531169689Skan      obstack_free (&name_obstack, name_base);
2532169689Skan      mangle_obstack = &name_obstack;
2533169689Skan      name_base = obstack_alloc (&name_obstack, 0);
2534169689Skan    }
2535169689Skan  else
2536169689Skan    mangle_obstack = &ident_hash->stack;
253790075Sobrien}
253890075Sobrien
2539117395Skan/* Done with mangling.  Return the generated mangled name.  If WARN is
2540117395Skan   true, and the name of G.entity will be mangled differently in a
2541117395Skan   future version of the ABI, issue a warning.  */
254290075Sobrien
254390075Sobrienstatic inline const char *
2544132718Skanfinish_mangling (const bool warn)
254590075Sobrien{
2546107590Sobrien  if (warn_abi && warn && G.need_abi_warning)
2547169689Skan    warning (OPT_Wabi, "the mangled name of %qD will change in a future "
2548107590Sobrien	     "version of GCC",
2549107590Sobrien	     G.entity);
2550107590Sobrien
255190075Sobrien  /* Clear all the substitutions.  */
2552169689Skan  VEC_truncate (tree, G.substitutions, 0);
255390075Sobrien
255490075Sobrien  /* Null-terminate the string.  */
255590075Sobrien  write_char ('\0');
255690075Sobrien
2557169689Skan  return (const char *) obstack_finish (mangle_obstack);
255890075Sobrien}
255990075Sobrien
256090075Sobrien/* Initialize data structures for mangling.  */
256190075Sobrien
256290075Sobrienvoid
2563132718Skaninit_mangle (void)
256490075Sobrien{
2565169689Skan  gcc_obstack_init (&name_obstack);
2566169689Skan  name_base = obstack_alloc (&name_obstack, 0);
2567169689Skan  G.substitutions = NULL;
256890075Sobrien
256990075Sobrien  /* Cache these identifiers for quick comparison when checking for
257090075Sobrien     standard substitutions.  */
257190075Sobrien  subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
257290075Sobrien  subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
257390075Sobrien  subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
257490075Sobrien  subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
257590075Sobrien  subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
257690075Sobrien  subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
257790075Sobrien}
257890075Sobrien
257990075Sobrien/* Generate the mangled name of DECL.  */
258090075Sobrien
258190075Sobrienstatic const char *
2582132718Skanmangle_decl_string (const tree decl)
258390075Sobrien{
258490075Sobrien  const char *result;
258590075Sobrien
2586169689Skan  start_mangling (decl, /*ident_p=*/true);
258790075Sobrien
258890075Sobrien  if (TREE_CODE (decl) == TYPE_DECL)
258990075Sobrien    write_type (TREE_TYPE (decl));
259090075Sobrien  else
2591132718Skan    write_mangled_name (decl, true);
2592169689Skan
2593107590Sobrien  result = finish_mangling (/*warn=*/true);
259490075Sobrien  if (DEBUG_MANGLE)
259590075Sobrien    fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
259690075Sobrien  return result;
259790075Sobrien}
259890075Sobrien
2599169689Skan/* Like get_identifier, except that NAME is assumed to have been
2600169689Skan   allocated on the obstack used by the identifier hash table.  */
2601169689Skan
2602169689Skanstatic inline tree
2603169689Skanget_identifier_nocopy (const char *name)
2604169689Skan{
2605169689Skan  hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
2606169689Skan				strlen (name), HT_ALLOCED);
2607169689Skan  return HT_IDENT_TO_GCC_IDENT (ht_node);
2608169689Skan}
2609169689Skan
261090075Sobrien/* Create an identifier for the external mangled name of DECL.  */
261190075Sobrien
261290075Sobrienvoid
2613132718Skanmangle_decl (const tree decl)
261490075Sobrien{
2615169689Skan  SET_DECL_ASSEMBLER_NAME (decl,
2616169689Skan			   get_identifier_nocopy (mangle_decl_string (decl)));
261790075Sobrien}
261890075Sobrien
261990075Sobrien/* Generate the mangled representation of TYPE.  */
262090075Sobrien
262190075Sobrienconst char *
2622132718Skanmangle_type_string (const tree type)
262390075Sobrien{
262490075Sobrien  const char *result;
262590075Sobrien
2626169689Skan  start_mangling (type, /*ident_p=*/false);
262790075Sobrien  write_type (type);
2628107590Sobrien  result = finish_mangling (/*warn=*/false);
262990075Sobrien  if (DEBUG_MANGLE)
263090075Sobrien    fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
263190075Sobrien  return result;
263290075Sobrien}
263390075Sobrien
263490075Sobrien/* Create an identifier for the mangled name of a special component
263590075Sobrien   for belonging to TYPE.  CODE is the ABI-specified code for this
263690075Sobrien   component.  */
263790075Sobrien
263890075Sobrienstatic tree
2639132718Skanmangle_special_for_type (const tree type, const char *code)
264090075Sobrien{
264190075Sobrien  const char *result;
264290075Sobrien
264390075Sobrien  /* We don't have an actual decl here for the special component, so
264490075Sobrien     we can't just process the <encoded-name>.  Instead, fake it.  */
2645169689Skan  start_mangling (type, /*ident_p=*/true);
264690075Sobrien
264790075Sobrien  /* Start the mangling.  */
264890075Sobrien  write_string ("_Z");
264990075Sobrien  write_string (code);
265090075Sobrien
265190075Sobrien  /* Add the type.  */
265290075Sobrien  write_type (type);
2653107590Sobrien  result = finish_mangling (/*warn=*/false);
265490075Sobrien
265590075Sobrien  if (DEBUG_MANGLE)
265690075Sobrien    fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
265790075Sobrien
2658169689Skan  return get_identifier_nocopy (result);
265990075Sobrien}
266090075Sobrien
266190075Sobrien/* Create an identifier for the mangled representation of the typeinfo
266290075Sobrien   structure for TYPE.  */
266390075Sobrien
266490075Sobrientree
2665132718Skanmangle_typeinfo_for_type (const tree type)
266690075Sobrien{
266790075Sobrien  return mangle_special_for_type (type, "TI");
266890075Sobrien}
266990075Sobrien
267090075Sobrien/* Create an identifier for the mangled name of the NTBS containing
267190075Sobrien   the mangled name of TYPE.  */
267290075Sobrien
267390075Sobrientree
2674132718Skanmangle_typeinfo_string_for_type (const tree type)
267590075Sobrien{
267690075Sobrien  return mangle_special_for_type (type, "TS");
267790075Sobrien}
267890075Sobrien
267990075Sobrien/* Create an identifier for the mangled name of the vtable for TYPE.  */
268090075Sobrien
268190075Sobrientree
2682132718Skanmangle_vtbl_for_type (const tree type)
268390075Sobrien{
268490075Sobrien  return mangle_special_for_type (type, "TV");
268590075Sobrien}
268690075Sobrien
268790075Sobrien/* Returns an identifier for the mangled name of the VTT for TYPE.  */
268890075Sobrien
268990075Sobrientree
2690132718Skanmangle_vtt_for_type (const tree type)
269190075Sobrien{
269290075Sobrien  return mangle_special_for_type (type, "TT");
269390075Sobrien}
269490075Sobrien
269590075Sobrien/* Return an identifier for a construction vtable group.  TYPE is
269690075Sobrien   the most derived class in the hierarchy; BINFO is the base
2697169689Skan   subobject for which this construction vtable group will be used.
269890075Sobrien
269990075Sobrien   This mangling isn't part of the ABI specification; in the ABI
270090075Sobrien   specification, the vtable group is dumped in the same COMDAT as the
270190075Sobrien   main vtable, and is referenced only from that vtable, so it doesn't
270290075Sobrien   need an external name.  For binary formats without COMDAT sections,
2703169689Skan   though, we need external names for the vtable groups.
270490075Sobrien
270590075Sobrien   We use the production
270690075Sobrien
270790075Sobrien    <special-name> ::= CT <type> <offset number> _ <base type>  */
270890075Sobrien
270990075Sobrientree
2710132718Skanmangle_ctor_vtbl_for_type (const tree type, const tree binfo)
271190075Sobrien{
271290075Sobrien  const char *result;
271390075Sobrien
2714169689Skan  start_mangling (type, /*ident_p=*/true);
271590075Sobrien
271690075Sobrien  write_string ("_Z");
271790075Sobrien  write_string ("TC");
271890075Sobrien  write_type (type);
271990075Sobrien  write_integer_cst (BINFO_OFFSET (binfo));
272090075Sobrien  write_char ('_');
272190075Sobrien  write_type (BINFO_TYPE (binfo));
272290075Sobrien
2723107590Sobrien  result = finish_mangling (/*warn=*/false);
272490075Sobrien  if (DEBUG_MANGLE)
272590075Sobrien    fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2726169689Skan  return get_identifier_nocopy (result);
272790075Sobrien}
272890075Sobrien
2729132718Skan/* Mangle a this pointer or result pointer adjustment.
2730169689Skan
2731132718Skan   <call-offset> ::= h <fixed offset number> _
2732132718Skan		 ::= v <fixed offset number> _ <virtual offset number> _ */
2733169689Skan
2734132718Skanstatic void
2735132718Skanmangle_call_offset (const tree fixed_offset, const tree virtual_offset)
2736132718Skan{
2737132718Skan  write_char (virtual_offset ? 'v' : 'h');
273890075Sobrien
2739132718Skan  /* For either flavor, write the fixed offset.  */
2740132718Skan  write_integer_cst (fixed_offset);
2741132718Skan  write_char ('_');
2742132718Skan
2743132718Skan  /* For a virtual thunk, add the virtual offset.  */
2744132718Skan  if (virtual_offset)
2745132718Skan    {
2746132718Skan      write_integer_cst (virtual_offset);
2747132718Skan      write_char ('_');
2748132718Skan    }
2749132718Skan}
2750132718Skan
2751132718Skan/* Return an identifier for the mangled name of a this-adjusting or
2752132718Skan   covariant thunk to FN_DECL.  FIXED_OFFSET is the initial adjustment
2753132718Skan   to this used to find the vptr.  If VIRTUAL_OFFSET is non-NULL, this
2754132718Skan   is a virtual thunk, and it is the vtbl offset in
2755132718Skan   bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
2756132718Skan   zero for a covariant thunk. Note, that FN_DECL might be a covariant
2757132718Skan   thunk itself. A covariant thunk name always includes the adjustment
2758132718Skan   for the this pointer, even if there is none.
2759132718Skan
2760132718Skan   <special-name> ::= T <call-offset> <base encoding>
2761169689Skan		  ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2762169689Skan					<base encoding>  */
276390075Sobrien
276490075Sobrientree
2765132718Skanmangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2766132718Skan	      tree virtual_offset)
276790075Sobrien{
276890075Sobrien  const char *result;
276990075Sobrien
2770169689Skan  start_mangling (fn_decl, /*ident_p=*/true);
2771169689Skan
277290075Sobrien  write_string ("_Z");
277390075Sobrien  write_char ('T');
2774169689Skan
2775132718Skan  if (!this_adjusting)
2776132718Skan    {
2777132718Skan      /* Covariant thunk with no this adjustment */
2778132718Skan      write_char ('c');
2779132718Skan      mangle_call_offset (integer_zero_node, NULL_TREE);
2780132718Skan      mangle_call_offset (fixed_offset, virtual_offset);
2781132718Skan    }
2782132718Skan  else if (!DECL_THUNK_P (fn_decl))
2783132718Skan    /* Plain this adjusting thunk.  */
2784132718Skan    mangle_call_offset (fixed_offset, virtual_offset);
278590075Sobrien  else
278690075Sobrien    {
2787132718Skan      /* This adjusting thunk to covariant thunk.  */
2788132718Skan      write_char ('c');
2789132718Skan      mangle_call_offset (fixed_offset, virtual_offset);
2790132718Skan      fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2791132718Skan      virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2792132718Skan      if (virtual_offset)
2793132718Skan	virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2794132718Skan      mangle_call_offset (fixed_offset, virtual_offset);
2795132718Skan      fn_decl = THUNK_TARGET (fn_decl);
279690075Sobrien    }
279790075Sobrien
279890075Sobrien  /* Scoped name.  */
279990075Sobrien  write_encoding (fn_decl);
280090075Sobrien
2801107590Sobrien  result = finish_mangling (/*warn=*/false);
280290075Sobrien  if (DEBUG_MANGLE)
280390075Sobrien    fprintf (stderr, "mangle_thunk = %s\n\n", result);
2804169689Skan  return get_identifier_nocopy (result);
280590075Sobrien}
280690075Sobrien
2807117395Skan/* This hash table maps TYPEs to the IDENTIFIER for a conversion
2808132718Skan   operator to TYPE.  The nodes are IDENTIFIERs whose TREE_TYPE is the
2809132718Skan   TYPE.  */
2810117395Skan
2811117395Skanstatic GTY ((param_is (union tree_node))) htab_t conv_type_names;
2812117395Skan
2813117395Skan/* Hash a node (VAL1) in the table.  */
2814117395Skan
2815117395Skanstatic hashval_t
2816117395Skanhash_type (const void *val)
2817117395Skan{
2818132718Skan  return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
2819117395Skan}
2820117395Skan
2821117395Skan/* Compare VAL1 (a node in the table) with VAL2 (a TYPE).  */
2822117395Skan
2823117395Skanstatic int
2824117395Skancompare_type (const void *val1, const void *val2)
2825117395Skan{
2826132718Skan  return TREE_TYPE ((tree) val1) == (tree) val2;
2827117395Skan}
2828117395Skan
282990075Sobrien/* Return an identifier for the mangled unqualified name for a
283090075Sobrien   conversion operator to TYPE.  This mangling is not specified by the
283190075Sobrien   ABI spec; it is only used internally.  */
2832132718Skan
283390075Sobrientree
2834117395Skanmangle_conv_op_name_for_type (const tree type)
283590075Sobrien{
2836117395Skan  void **slot;
283790075Sobrien  tree identifier;
2838132718Skan
2839169689Skan  if (type == error_mark_node)
2840169689Skan    return error_mark_node;
2841169689Skan
2842169689Skan  if (conv_type_names == NULL)
2843117395Skan    conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
284490075Sobrien
2845169689Skan  slot = htab_find_slot_with_hash (conv_type_names, type,
2846132718Skan				   (hashval_t) TYPE_UID (type), INSERT);
2847132718Skan  identifier = (tree)*slot;
2848132718Skan  if (!identifier)
2849132718Skan    {
2850132718Skan      char buffer[64];
2851169689Skan
2852132718Skan       /* Create a unique name corresponding to TYPE.  */
2853132718Skan      sprintf (buffer, "operator %lu",
2854132718Skan	       (unsigned long) htab_elements (conv_type_names));
2855132718Skan      identifier = get_identifier (buffer);
2856132718Skan      *slot = identifier;
2857117395Skan
2858132718Skan      /* Hang TYPE off the identifier so it can be found easily later
2859132718Skan	 when performing conversions.  */
2860132718Skan      TREE_TYPE (identifier) = type;
2861132718Skan
2862132718Skan      /* Set bits on the identifier so we know later it's a conversion.  */
2863132718Skan      IDENTIFIER_OPNAME_P (identifier) = 1;
2864132718Skan      IDENTIFIER_TYPENAME_P (identifier) = 1;
2865132718Skan    }
2866169689Skan
286790075Sobrien  return identifier;
286890075Sobrien}
286990075Sobrien
287090075Sobrien/* Return an identifier for the name of an initialization guard
287190075Sobrien   variable for indicated VARIABLE.  */
287290075Sobrien
287390075Sobrientree
2874132718Skanmangle_guard_variable (const tree variable)
287590075Sobrien{
2876169689Skan  start_mangling (variable, /*ident_p=*/true);
287790075Sobrien  write_string ("_ZGV");
287890075Sobrien  if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
287990075Sobrien    /* The name of a guard variable for a reference temporary should refer
288090075Sobrien       to the reference, not the temporary.  */
288190075Sobrien    write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
288290075Sobrien  else
288390075Sobrien    write_name (variable, /*ignore_local_scope=*/0);
2884169689Skan  return get_identifier_nocopy (finish_mangling (/*warn=*/false));
288590075Sobrien}
288690075Sobrien
288790075Sobrien/* Return an identifier for the name of a temporary variable used to
288890075Sobrien   initialize a static reference.  This isn't part of the ABI, but we might
288990075Sobrien   as well call them something readable.  */
289090075Sobrien
289190075Sobrientree
2892132718Skanmangle_ref_init_variable (const tree variable)
289390075Sobrien{
2894169689Skan  start_mangling (variable, /*ident_p=*/true);
289590075Sobrien  write_string ("_ZGR");
289690075Sobrien  write_name (variable, /*ignore_local_scope=*/0);
2897169689Skan  return get_identifier_nocopy (finish_mangling (/*warn=*/false));
289890075Sobrien}
289990075Sobrien
290090075Sobrien
290190075Sobrien/* Foreign language type mangling section.  */
290290075Sobrien
290390075Sobrien/* How to write the type codes for the integer Java type.  */
290490075Sobrien
290590075Sobrienstatic void
2906132718Skanwrite_java_integer_type_codes (const tree type)
290790075Sobrien{
290890075Sobrien  if (type == java_int_type_node)
290990075Sobrien    write_char ('i');
291090075Sobrien  else if (type == java_short_type_node)
291190075Sobrien    write_char ('s');
291290075Sobrien  else if (type == java_byte_type_node)
291390075Sobrien    write_char ('c');
291490075Sobrien  else if (type == java_char_type_node)
291590075Sobrien    write_char ('w');
291690075Sobrien  else if (type == java_long_type_node)
291790075Sobrien    write_char ('x');
291890075Sobrien  else if (type == java_boolean_type_node)
291990075Sobrien    write_char ('b');
292090075Sobrien  else
2921169689Skan    gcc_unreachable ();
292290075Sobrien}
292390075Sobrien
2924117395Skan#include "gt-cp-mangle.h"
2925