mangle.c revision 96263
166200Simp/* Name mangling for the 3.0 C++ ABI. 252506Simp Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. 352506Simp Written by Alex Samuel <sameul@codesourcery.com> 452506Simp 552506Simp This file is part of GNU CC. 652506Simp 752506Simp GNU CC is free software; you can redistribute it and/or modify it 852506Simp under the terms of the GNU General Public License as published by 952506Simp the Free Software Foundation; either version 2, or (at your option) 1052506Simp any later version. 1152506Simp 1252506Simp GNU CC is distributed in the hope that it will be useful, but 1352506Simp WITHOUT ANY WARRANTY; without even the implied warranty of 1452506Simp MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1552506Simp General Public License for more details. 1652506Simp 1752506Simp You should have received a copy of the GNU General Public License 1852506Simp along with GNU CC; see the file COPYING. If not, write to the Free 1952506Simp Software Foundation, 59 Temple Place - Suite 330, Boston, MA 2052506Simp 02111-1307, USA. */ 2152506Simp 2252506Simp/* This file implements mangling of C++ names according to the IA64 2352506Simp C++ ABI specification. A mangled name encodes a function or 2452506Simp variable's name, scope, type, and/or template arguments into a text 2552506Simp identifier. This identifier is used as the function's or 2652506Simp variable's linkage name, to preserve compatibility between C++'s 2752506Simp language features (templates, scoping, and overloading) and C 2852506Simp linkers. 2952506Simp 3052506Simp Additionally, g++ uses mangled names internally. To support this, 3152506Simp mangling of types is allowed, even though the mangled name of a 3252506Simp type should not appear by itself as an exported name. Ditto for 3352506Simp uninstantiated templates. 3452506Simp 3552506Simp The primary entry point for this module is mangle_decl, which 3652506Simp returns an identifier containing the mangled name for a decl. 3752506Simp Additional entry points are provided to build mangled names of 3852506Simp particular constructs when the appropriate decl for that construct 3952506Simp is not available. These are: 4052506Simp 4152506Simp mangle_typeinfo_for_type: typeinfo data 4252506Simp mangle_typeinfo_string_for_type: typeinfo type name 4352506Simp mangle_vtbl_for_type: virtual table data 4452506Simp mangle_vtt_for_type: VTT data 4552506Simp mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data 4652506Simp mangle_thunk: thunk function or entry 4752506Simp 4852506Simp*/ 4955500Simp 5059193Simp#include "config.h" 5155500Simp#include "system.h" 5255500Simp#include "tree.h" 5355500Simp#include "cp-tree.h" 5452506Simp#include "obstack.h" 5555500Simp#include "toplev.h" 5652506Simp#include "varray.h" 5755500Simp 5867333Simp/* Debugging support. */ 5967333Simp 6052506Simp/* Define DEBUG_MANGLE to enable very verbose trace messages. */ 6152506Simp#ifndef DEBUG_MANGLE 6255500Simp#define DEBUG_MANGLE 0 6367333Simp#endif 6467333Simp 6552506Simp/* Macros for tracing the write_* functions. */ 6652506Simp#if DEBUG_MANGLE 6752506Simp# define MANGLE_TRACE(FN, INPUT) \ 6852506Simp fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT)) 6952506Simp# define MANGLE_TRACE_TREE(FN, NODE) \ 7052506Simp fprintf (stderr, " %-24s: %-24s (%p)\n", \ 7152506Simp (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE)) 7252506Simp#else 7354250Simp# define MANGLE_TRACE(FN, INPUT) 7452506Simp# define MANGLE_TRACE_TREE(FN, NODE) 7552506Simp#endif 7652506Simp 7752506Simp/* Non-zero if NODE is a class template-id. We can't rely on 7852506Simp CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser 7952506Simp that hard to distinguish A<T> from A, where A<T> is the type as 8052506Simp instantiated outside of the template, and A is the type used 8152506Simp without parameters inside the template. */ 8252506Simp#define CLASSTYPE_TEMPLATE_ID_P(NODE) \ 8352506Simp (TYPE_LANG_SPECIFIC (NODE) != NULL \ 8452506Simp && CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \ 8552506Simp && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE)))) 8652506Simp 8752506Simp/* Things we only need one of. This module is not reentrant. */ 8852506Simpstatic struct globals 8952506Simp{ 9052506Simp /* The name in which we're building the mangled name. */ 9152506Simp struct obstack name_obstack; 9252506Simp 9352506Simp /* An array of the current substitution candidates, in the order 9452506Simp we've seen them. */ 9552506Simp varray_type substitutions; 9652506Simp} G; 9759193Simp 9859193Simp/* Indices into subst_identifiers. These are identifiers used in 9952506Simp special substitution rules. */ 10064850Simptypedef enum 10152506Simp{ 10265917Simp SUBID_ALLOCATOR, 10361788Simp SUBID_BASIC_STRING, 10452506Simp SUBID_CHAR_TRAITS, 10552506Simp SUBID_BASIC_ISTREAM, 10670715Sjon SUBID_BASIC_OSTREAM, 10752506Simp SUBID_BASIC_IOSTREAM, 10852506Simp SUBID_MAX 10952506Simp} 11052506Simpsubstitution_identifier_index_t; 11152506Simp 11255500Simp/* For quick substitution checks, look up these common identifiers 11355500Simp once only. */ 11452506Simpstatic tree subst_identifiers[SUBID_MAX]; 11555500Simp 11652506Simp/* Single-letter codes for builtin integer types, defined in 11752506Simp <builtin-type>. These are indexed by integer_type_kind values. */ 11855500Simpstatic char 11952506Simpinteger_type_codes[itk_none] = 12052506Simp{ 12152506Simp 'c', /* itk_char */ 12252506Simp 'a', /* itk_signed_char */ 12352506Simp 'h', /* itk_unsigned_char */ 12452506Simp 's', /* itk_short */ 12552506Simp 't', /* itk_unsigned_short */ 12670715Sjon 'i', /* itk_int */ 12770715Sjon 'j', /* itk_unsigned_int */ 12852506Simp 'l', /* itk_long */ 12970715Sjon 'm', /* itk_unsigned_long */ 13070715Sjon 'x', /* itk_long_long */ 13170715Sjon 'y' /* itk_unsigned_long_long */ 13252506Simp}; 13370715Sjon 13452506Simpstatic int decl_is_template_id PARAMS ((tree, tree*)); 13561788Simp 13652506Simp/* Functions for handling substitutions. */ 13752506Simp 13852506Simpstatic inline tree canonicalize_for_substitution PARAMS ((tree)); 13952506Simpstatic void add_substitution PARAMS ((tree)); 14055500Simpstatic inline int is_std_substitution PARAMS ((tree, substitution_identifier_index_t)); 14152506Simpstatic inline int is_std_substitution_char PARAMS ((tree, substitution_identifier_index_t)); 14252506Simpstatic int find_substitution PARAMS ((tree)); 14352506Simp 14452506Simp/* Functions for emitting mangled representations of things. */ 14552506Simp 14652506Simpstatic void write_mangled_name PARAMS ((tree)); 14764927Simpstatic void write_encoding PARAMS ((tree)); 14852506Simpstatic void write_name PARAMS ((tree, int)); 14965098Simpstatic void write_unscoped_name PARAMS ((tree)); 15064850Simpstatic void write_unscoped_template_name PARAMS ((tree)); 15164850Simpstatic void write_nested_name PARAMS ((tree)); 15265098Simpstatic void write_prefix PARAMS ((tree)); 15352506Simpstatic void write_template_prefix PARAMS ((tree)); 15452506Simpstatic void write_unqualified_name PARAMS ((tree)); 15552506Simpstatic void write_source_name PARAMS ((tree)); 15661788Simpstatic int hwint_to_ascii PARAMS ((unsigned HOST_WIDE_INT, unsigned int, char *, unsigned)); 15761788Simpstatic void write_number PARAMS ((unsigned HOST_WIDE_INT, int, 15861788Simp unsigned int)); 15961788Simpstatic void write_integer_cst PARAMS ((tree)); 16061788Simpstatic void write_identifier PARAMS ((const char *)); 16161788Simpstatic void write_special_name_constructor PARAMS ((tree)); 16261788Simpstatic void write_special_name_destructor PARAMS ((tree)); 16361788Simpstatic void write_type PARAMS ((tree)); 16461788Simpstatic int write_CV_qualifiers_for_type PARAMS ((tree)); 16561788Simpstatic void write_builtin_type PARAMS ((tree)); 16661788Simpstatic void write_function_type PARAMS ((tree)); 16767897Sdwmalonestatic void write_bare_function_type PARAMS ((tree, int, tree)); 16867897Sdwmalonestatic void write_method_parms PARAMS ((tree, int, tree)); 16961788Simpstatic void write_class_enum_type PARAMS ((tree)); 17065917Simpstatic void write_template_args PARAMS ((tree)); 17166847Simpstatic void write_expression PARAMS ((tree)); 17267187Simpstatic void write_template_arg_literal PARAMS ((tree)); 17367167Simpstatic void write_template_arg PARAMS ((tree)); 17467167Simpstatic void write_template_template_arg PARAMS ((tree)); 17567167Simpstatic void write_array_type PARAMS ((tree)); 17667167Simpstatic void write_pointer_to_member_type PARAMS ((tree)); 17767167Simpstatic void write_template_param PARAMS ((tree)); 17867167Simpstatic void write_template_template_param PARAMS ((tree)); 17967167Simpstatic void write_substitution PARAMS ((int)); 18067167Simpstatic int discriminator_for_local_entity PARAMS ((tree)); 18165917Simpstatic int discriminator_for_string_literal PARAMS ((tree, tree)); 18267333Simpstatic void write_discriminator PARAMS ((int)); 18367333Simpstatic void write_local_name PARAMS ((tree, tree, tree)); 18452506Simpstatic void dump_substitution_candidates PARAMS ((void)); 18552506Simpstatic const char *mangle_decl_string PARAMS ((tree)); 18655500Simp 18767167Simp/* Control functions. */ 18867167Simp 18967167Simpstatic inline void start_mangling PARAMS ((void)); 19052506Simpstatic inline const char *finish_mangling PARAMS ((void)); 19152506Simpstatic tree mangle_special_for_type PARAMS ((tree, const char *)); 19252506Simp 19352506Simp/* Foreign language functions. */ 19467167Simp 19567167Simpstatic void write_java_integer_type_codes PARAMS ((tree)); 19652506Simp 19752506Simp/* Append a single character to the end of the mangled 19861788Simp representation. */ 19952506Simp#define write_char(CHAR) \ 20052506Simp obstack_1grow (&G.name_obstack, (CHAR)) 20159193Simp 20259193Simp/* Append a sized buffer to the end of the mangled representation. */ 20352506Simp#define write_chars(CHAR, LEN) \ 20464850Simp obstack_grow (&G.name_obstack, (CHAR), (LEN)) 20552506Simp 20652506Simp/* Append a NUL-terminated string to the end of the mangled 20752506Simp representation. */ 20852506Simp#define write_string(STRING) \ 20952506Simp obstack_grow (&G.name_obstack, (STRING), strlen (STRING)) 21052506Simp 21152506Simp/* Return the position at which the next character will be appended to 21252506Simp the mangled representation. */ 21352506Simp#define mangled_position() \ 21461788Simp obstack_object_size (&G.name_obstack) 21561788Simp 21664927Simp/* Non-zero if NODE1 and NODE2 are both TREE_LIST nodes and have the 21764927Simp same purpose (context, which may be a type) and value (template 21852506Simp decl). See write_template_prefix for more information on what this 21959193Simp is used for. */ 22052506Simp#define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \ 22152506Simp (TREE_CODE (NODE1) == TREE_LIST \ 22266200Simp && TREE_CODE (NODE2) == TREE_LIST \ 22366200Simp && ((TYPE_P (TREE_PURPOSE (NODE1)) \ 22466200Simp && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\ 22566200Simp || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \ 22666200Simp && TREE_VALUE (NODE1) == TREE_VALUE (NODE2)) 22766200Simp 22866200Simp/* Write out a signed quantity in base 10. */ 22966200Simp#define write_signed_number(NUMBER) \ 23066200Simp write_number ((NUMBER), /*unsigned_p=*/0, 10) 23166200Simp 23266200Simp/* Write out an unsigned quantity in base 10. */ 23366200Simp#define write_unsigned_number(NUMBER) \ 23466200Simp write_number ((NUMBER), /*unsigned_p=*/1, 10) 23566200Simp 23670715Sjon/* If DECL is a template instance, return non-zero and, if 23766200Simp TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info. 23866200Simp Otherwise return zero. */ 23966200Simp 24066200Simpstatic int 24166200Simpdecl_is_template_id (decl, template_info) 24266200Simp tree decl; 24366200Simp tree* template_info; 24466200Simp{ 24566200Simp if (TREE_CODE (decl) == TYPE_DECL) 24666200Simp { 24766200Simp /* TYPE_DECLs are handled specially. Look at its type to decide 24866200Simp if this is a template instantiation. */ 24970715Sjon tree type = TREE_TYPE (decl); 25066200Simp 25166200Simp if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type)) 25266200Simp { 25366200Simp if (template_info != NULL) 25466200Simp /* For a templated TYPE_DECL, the template info is hanging 25566200Simp off the type. */ 25666200Simp *template_info = CLASSTYPE_TEMPLATE_INFO (type); 25766200Simp return 1; 25866200Simp } 25966200Simp } 26066200Simp else 26170715Sjon { 26266200Simp /* Check if this is a primary template. */ 26366200Simp if (DECL_LANG_SPECIFIC (decl) != NULL 26470715Sjon && DECL_USE_TEMPLATE (decl) 26566200Simp && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)) 26666200Simp && TREE_CODE (decl) != TEMPLATE_DECL) 26766200Simp { 26866200Simp if (template_info != NULL) 26966200Simp /* For most templated decls, the template info is hanging 27066200Simp off the decl. */ 27166200Simp *template_info = DECL_TEMPLATE_INFO (decl); 27266200Simp return 1; 27366200Simp } 27466200Simp } 27570715Sjon 27659193Simp /* It's not a template id. */ 27752506Simp return 0; 27864850Simp} 27952506Simp 28052506Simp/* Produce debugging output of current substitution candidates. */ 28152506Simp 28252506Simpstatic void 28352506Simpdump_substitution_candidates () 28452506Simp{ 28552506Simp unsigned i; 28652506Simp 28752506Simp fprintf (stderr, " ++ substitutions "); 28852506Simp for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i) 28952506Simp { 29059193Simp tree el = VARRAY_TREE (G.substitutions, i); 29152506Simp const char *name = "???"; 29259193Simp 29359193Simp if (i > 0) 29452506Simp fprintf (stderr, " "); 29552506Simp if (DECL_P (el)) 29652506Simp name = IDENTIFIER_POINTER (DECL_NAME (el)); 29752506Simp else if (TREE_CODE (el) == TREE_LIST) 29852506Simp name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el))); 29952506Simp else if (TYPE_NAME (el)) 30052506Simp name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el))); 30170715Sjon fprintf (stderr, " S%d_ = ", i - 1); 30252506Simp if (TYPE_P (el) && 30365917Simp (CP_TYPE_RESTRICT_P (el) 30467187Simp || CP_TYPE_VOLATILE_P (el) 30567242Simp || CP_TYPE_CONST_P (el))) 30667242Simp fprintf (stderr, "CV-"); 30767242Simp fprintf (stderr, "%s (%s at %p)\n", 30867242Simp name, tree_code_name[TREE_CODE (el)], (void *) el); 30967424Simp } 31067424Simp} 31165917Simp 31270715Sjon/* Both decls and types can be substitution candidates, but sometimes 31370715Sjon they refer to the same thing. For instance, a TYPE_DECL and 31470715Sjon RECORD_TYPE for the same class refer to the same thing, and should 31570715Sjon be treated accordinginly in substitutions. This function returns a 31667242Simp canonicalized tree node representing NODE that is used when adding 31752506Simp and substitution candidates and finding matches. */ 31867167Simp 31967167Simpstatic inline tree 32067187Simpcanonicalize_for_substitution (node) 32167187Simp tree node; 32267187Simp{ 32367187Simp /* For a TYPE_DECL, use the type instead. */ 32467424Simp if (TREE_CODE (node) == TYPE_DECL) 32567424Simp node = TREE_TYPE (node); 32667424Simp if (TYPE_P (node)) 32767424Simp node = canonical_type_variant (node); 32867424Simp 32967187Simp return node; 33067242Simp} 33167424Simp 33270715Sjon/* Add NODE as a substitution candidate. NODE must not already be on 33367424Simp the list of candidates. */ 33467187Simp 33567187Simpstatic void 33667242Simpadd_substitution (node) 33767242Simp tree node; 33867242Simp{ 33970715Sjon tree c; 34070715Sjon 34170715Sjon if (DEBUG_MANGLE) 34270715Sjon fprintf (stderr, " ++ add_substitution (%s at %10p)\n", 34367187Simp tree_code_name[TREE_CODE (node)], (void *) node); 34467187Simp 34567187Simp /* Get the canonicalized substitution candidate for NODE. */ 34667187Simp c = canonicalize_for_substitution (node); 34767187Simp if (DEBUG_MANGLE && c != node) 34867187Simp fprintf (stderr, " ++ using candidate (%s at %10p)\n", 34967399Simp tree_code_name[TREE_CODE (node)], (void *) node); 35067424Simp node = c; 35167187Simp 35267187Simp#if ENABLE_CHECKING 35367242Simp /* Make sure NODE isn't already a candidate. */ 35467242Simp { 35570715Sjon int i; 35670715Sjon for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; ) 35770715Sjon { 35870715Sjon tree candidate = VARRAY_TREE (G.substitutions, i); 35967187Simp if ((DECL_P (node) 36067187Simp && node == candidate) 36167187Simp || (TYPE_P (node) 36267167Simp && TYPE_P (candidate) 36367187Simp && same_type_p (node, candidate))) 36467187Simp abort (); 36567424Simp } 36667424Simp } 36767333Simp#endif /* ENABLE_CHECKING */ 36867333Simp 36967333Simp /* Put the decl onto the varray of substitution candidates. */ 37067333Simp VARRAY_PUSH_TREE (G.substitutions, node); 37167187Simp 37267242Simp if (DEBUG_MANGLE) 37367187Simp dump_substitution_candidates (); 37470715Sjon} 37567187Simp 37667187Simp/* Helper function for find_substitution. Returns non-zero if NODE, 37767187Simp which may be a decl or a CLASS_TYPE, is a template-id with template 37867187Simp name of substitution_index[INDEX] in the ::std namespace. */ 37967242Simp 38067242Simpstatic inline int 38167187Simpis_std_substitution (node, index) 38267187Simp tree node; 38367187Simp substitution_identifier_index_t index; 38467167Simp{ 38552506Simp tree type = NULL; 38652506Simp tree decl = NULL; 38752506Simp 38852506Simp if (DECL_P (node)) 38955720Simp { 39052506Simp type = TREE_TYPE (node); 39152506Simp decl = node; 39252506Simp } 39355720Simp else if (CLASS_TYPE_P (node)) 39470746Simp { 39567333Simp type = node; 39667333Simp decl = TYPE_NAME (node); 39767333Simp } 39867333Simp else 39952506Simp /* These are not the droids you're looking for. */ 40052506Simp return 0; 40152506Simp 40252506Simp return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)) 40352506Simp && TYPE_LANG_SPECIFIC (type) 40452506Simp && CLASSTYPE_TEMPLATE_INFO (type) 40555720Simp && (DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) 40655720Simp == subst_identifiers[index])); 40752506Simp} 40852506Simp 40952506Simp/* Helper function for find_substitution. Returns non-zero if NODE, 41052506Simp which may be a decl or a CLASS_TYPE, is the template-id 41152506Simp ::std::identifier<char>, where identifier is 41252506Simp substitution_index[INDEX]. */ 41352506Simp 41452506Simpstatic inline int 41552506Simpis_std_substitution_char (node, index) 41652506Simp tree node; 41752506Simp substitution_identifier_index_t index; 41852506Simp{ 41952506Simp tree args; 42052506Simp /* Check NODE's name is ::std::identifier. */ 42152506Simp if (!is_std_substitution (node, index)) 42252506Simp return 0; 42352506Simp /* Figure out its template args. */ 42452506Simp if (DECL_P (node)) 42552506Simp args = DECL_TI_ARGS (node); 42652506Simp else if (CLASS_TYPE_P (node)) 42752506Simp args = CLASSTYPE_TI_ARGS (node); 42852506Simp else 42952506Simp /* Oops, not a template. */ 43052506Simp return 0; 43152506Simp /* NODE's template arg list should be <char>. */ 43252506Simp return 43370715Sjon TREE_VEC_LENGTH (args) == 1 43452506Simp && TREE_VEC_ELT (args, 0) == char_type_node; 43570715Sjon} 43652506Simp 43752506Simp/* Check whether a substitution should be used to represent NODE in 43852506Simp the mangling. 43952506Simp 44052506Simp First, check standard special-case substitutions. 44155720Simp 44255720Simp <substitution> ::= St 44370715Sjon # ::std 44470715Sjon 44552506Simp ::= Sa 44670746Simp # ::std::allocator 44770746Simp 44870746Simp ::= Sb 44961788Simp # ::std::basic_string 45061788Simp 45170715Sjon ::= Ss 45270748Simp # ::std::basic_string<char, 45355720Simp ::std::char_traits<char>, 45455720Simp ::std::allocator<char> > 45555720Simp 45652506Simp ::= Si 45752506Simp # ::std::basic_istream<char, ::std::char_traits<char> > 45852506Simp 45952506Simp ::= So 46052506Simp # ::std::basic_ostream<char, ::std::char_traits<char> > 46152506Simp 46252506Simp ::= Sd 46370715Sjon # ::std::basic_iostream<char, ::std::char_traits<char> > 46467167Simp 46567167Simp Then examine the stack of currently available substitution 46667167Simp candidates for entities appearing earlier in the same mangling 46767167Simp 46870715Sjon If a substitution is found, write its mangled representation and 46952506Simp return non-zero. If none is found, just return zero. */ 47052506Simp 47152506Simpstatic int 47252506Simpfind_substitution (node) 47352506Simp tree node; 47452506Simp{ 47552506Simp int i; 47652506Simp int size = VARRAY_ACTIVE_SIZE (G.substitutions); 47752506Simp tree decl; 47852506Simp tree type; 47952506Simp 48052506Simp if (DEBUG_MANGLE) 48152506Simp fprintf (stderr, " ++ find_substitution (%s at %p)\n", 48252506Simp tree_code_name[TREE_CODE (node)], (void *) node); 48352506Simp 48452506Simp /* Obtain the canonicalized substitution representation for NODE. 48552506Simp This is what we'll compare against. */ 48652506Simp node = canonicalize_for_substitution (node); 48752506Simp 48852506Simp /* Check for builtin substitutions. */ 48952506Simp 49052506Simp decl = TYPE_P (node) ? TYPE_NAME (node) : node; 49152506Simp type = TYPE_P (node) ? node : TREE_TYPE (node); 49252506Simp 49352506Simp /* Check for std::allocator. */ 49452506Simp if (decl 49552506Simp && is_std_substitution (decl, SUBID_ALLOCATOR) 49652506Simp && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl))) 49752506Simp { 49852506Simp write_string ("Sa"); 49952506Simp return 1; 50052506Simp } 50152506Simp 50252506Simp /* Check for std::basic_string. */ 50370715Sjon if (decl && is_std_substitution (decl, SUBID_BASIC_STRING)) 50455500Simp { 50555500Simp if (TYPE_P (node)) 50670715Sjon { 50755500Simp /* If this is a type (i.e. a fully-qualified template-id), 50855500Simp check for 50955500Simp std::basic_string <char, 51055500Simp std::char_traits<char>, 51155500Simp std::allocator<char> > . */ 51255500Simp if (cp_type_quals (type) == TYPE_UNQUALIFIED 51370715Sjon && CLASSTYPE_USE_TEMPLATE (type)) 51455500Simp { 51555500Simp tree args = CLASSTYPE_TI_ARGS (type); 51655500Simp if (TREE_VEC_LENGTH (args) == 3 51752506Simp && same_type_p (TREE_VEC_ELT (args, 0), char_type_node) 51852506Simp && is_std_substitution_char (TREE_VEC_ELT (args, 1), 51952506Simp SUBID_CHAR_TRAITS) 52052506Simp && is_std_substitution_char (TREE_VEC_ELT (args, 2), 52152506Simp SUBID_ALLOCATOR)) 52252506Simp { 52352506Simp write_string ("Ss"); 52452506Simp return 1; 52552506Simp } 52652506Simp } 52752506Simp } 52852506Simp else 52955720Simp /* Substitute for the template name only if this isn't a type. */ 53065098Simp { 53152506Simp write_string ("Sb"); 53252506Simp return 1; 53352506Simp } 53452506Simp } 53552506Simp 53652506Simp /* Check for basic_{i,o,io}stream. */ 53755720Simp if (TYPE_P (node) 53852506Simp && cp_type_quals (type) == TYPE_UNQUALIFIED 53952506Simp && CLASS_TYPE_P (type) 54055720Simp && CLASSTYPE_USE_TEMPLATE (type) 54152506Simp && CLASSTYPE_TEMPLATE_INFO (type) != NULL) 54252506Simp { 54361788Simp /* First, check for the template 54452506Simp args <char, std::char_traits<char> > . */ 54552506Simp tree args = CLASSTYPE_TI_ARGS (type); 54652506Simp if (TREE_VEC_LENGTH (args) == 2 54752506Simp && same_type_p (TREE_VEC_ELT (args, 0), char_type_node) 54852506Simp && is_std_substitution_char (TREE_VEC_ELT (args, 1), 54952506Simp SUBID_CHAR_TRAITS)) 55052506Simp { 55152506Simp /* Got them. Is this basic_istream? */ 55270715Sjon tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); 55370715Sjon if (name == subst_identifiers[SUBID_BASIC_ISTREAM]) 55470715Sjon { 55570715Sjon write_string ("Si"); 55670715Sjon return 1; 55770715Sjon } 55870715Sjon /* Or basic_ostream? */ 55970715Sjon else if (name == subst_identifiers[SUBID_BASIC_OSTREAM]) 56070715Sjon { 56170715Sjon write_string ("So"); 56270715Sjon return 1; 56370715Sjon } 56470715Sjon /* Or basic_iostream? */ 56570715Sjon else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM]) 56670715Sjon { 56770715Sjon write_string ("Sd"); 56870715Sjon return 1; 56970715Sjon } 57070715Sjon } 57170715Sjon } 57270715Sjon 57352506Simp /* Check for namespace std. */ 57452506Simp if (decl && DECL_NAMESPACE_STD_P (decl)) 57552506Simp { 57652506Simp write_string ("St"); 57752506Simp return 1; 57852506Simp } 57952506Simp 58052506Simp /* Now check the list of available substitutions for this mangling 58152506Simp operation. */ 58252506Simp for (i = 0; i < size; ++i) 58352506Simp { 58452506Simp tree candidate = VARRAY_TREE (G.substitutions, i); 58552506Simp /* NODE is a matched to a candidate if it's the same decl node or 58652506Simp if it's the same type. */ 58752506Simp if (decl == candidate 58852506Simp || (TYPE_P (candidate) && type && TYPE_P (type) 58952506Simp && same_type_p (type, candidate)) 59070715Sjon || NESTED_TEMPLATE_MATCH (node, candidate)) 59155720Simp { 59255720Simp write_substitution (i); 59352506Simp return 1; 59452506Simp } 59552506Simp } 59652506Simp 59752506Simp /* No substitution found. */ 59852506Simp return 0; 59952506Simp} 60055720Simp 60155720Simp 60252506Simp/* <mangled-name> ::= _Z <encoding> */ 60352506Simp 60455720Simpstatic inline void 60555720Simpwrite_mangled_name (decl) 60652506Simp tree decl; 60755720Simp{ 60855720Simp MANGLE_TRACE_TREE ("mangled-name", decl); 60952506Simp 61052506Simp if (DECL_LANG_SPECIFIC (decl) 61152506Simp && DECL_EXTERN_C_FUNCTION_P (decl) 61267167Simp && ! DECL_OVERLOADED_OPERATOR_P (decl)) 61367167Simp /* The standard notes: 61452506Simp "The <encoding> of an extern "C" function is treated like 61552506Simp global-scope data, i.e. as its <source-name> without a type." 61652506Simp We cannot write overloaded operators that way though, 61752506Simp because it contains characters invalid in assembler. */ 61852506Simp write_source_name (DECL_NAME (decl)); 61952506Simp else 62052506Simp /* C++ name; needs to be mangled. */ 62152506Simp { 62252506Simp write_string ("_Z"); 62352506Simp write_encoding (decl); 62452506Simp } 62552506Simp} 62652506Simp 62752506Simp/* <encoding> ::= <function name> <bare-function-type> 62852506Simp ::= <data name> */ 62952506Simp 63052506Simpstatic void 63152506Simpwrite_encoding (decl) 63252506Simp tree decl; 63352506Simp{ 63452506Simp MANGLE_TRACE_TREE ("encoding", decl); 63552506Simp 63652506Simp if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl)) 63752506Simp { 63852506Simp /* For overloaded operators write just the mangled name 63952506Simp without arguments. */ 64052506Simp if (DECL_OVERLOADED_OPERATOR_P (decl)) 64152506Simp write_name (decl, /*ignore_local_scope=*/0); 64270715Sjon else 64355720Simp write_source_name (DECL_NAME (decl)); 64452506Simp return; 64555720Simp } 64652506Simp 64752506Simp write_name (decl, /*ignore_local_scope=*/0); 64852506Simp if (TREE_CODE (decl) == FUNCTION_DECL) 64952506Simp { 65052506Simp tree fn_type; 65152506Simp 65252506Simp if (decl_is_template_id (decl, NULL)) 65352506Simp fn_type = get_mostly_instantiated_function_type (decl, NULL, NULL); 65452506Simp else 65552506Simp fn_type = TREE_TYPE (decl); 65652506Simp 65752506Simp write_bare_function_type (fn_type, 65852506Simp (!DECL_CONSTRUCTOR_P (decl) 65955720Simp && !DECL_DESTRUCTOR_P (decl) 66052506Simp && !DECL_CONV_FN_P (decl) 66152506Simp && decl_is_template_id (decl, NULL)), 66252506Simp decl); 66352506Simp } 66452506Simp} 66552506Simp 66652506Simp/* <name> ::= <unscoped-name> 66752506Simp ::= <unscoped-template-name> <template-args> 66866058Simp ::= <nested-name> 66966058Simp ::= <local-name> 67066058Simp 67166058Simp If IGNORE_LOCAL_SCOPE is non-zero, this production of <name> is 67266058Simp called from <local-name>, which mangles the enclosing scope 67366058Simp elsewhere and then uses this function to mangle just the part 67466058Simp underneath the function scope. So don't use the <local-name> 67566058Simp production, to avoid an infinite recursion. */ 67666058Simp 67766058Simpstatic void 67866058Simpwrite_name (decl, ignore_local_scope) 67966058Simp tree decl; 68066058Simp int ignore_local_scope; 68166058Simp{ 68266058Simp tree context; 68366058Simp 68466058Simp MANGLE_TRACE_TREE ("name", decl); 68566058Simp 68666058Simp if (TREE_CODE (decl) == TYPE_DECL) 68766058Simp { 68866058Simp /* In case this is a typedef, fish out the corresponding 68966058Simp TYPE_DECL for the main variant. */ 69053873Simp decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))); 69153873Simp context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl))); 69253873Simp } 69353873Simp else 69453873Simp context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl); 69552506Simp 69652506Simp /* A decl in :: or ::std scope is treated specially. The former is 69752506Simp mangled using <unscoped-name> or <unscoped-template-name>, the 69859193Simp latter with a special substitution. Also, a name that is 69952506Simp directly in a local function scope is also mangled with 70052506Simp <unscoped-name> rather than a full <nested-name>. */ 70152506Simp if (context == NULL 70252506Simp || context == global_namespace 70352506Simp || DECL_NAMESPACE_STD_P (context) 70452506Simp || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL)) 70567333Simp { 70652506Simp tree template_info; 70752506Simp /* Is this a template instance? */ 70852506Simp if (decl_is_template_id (decl, &template_info)) 70959193Simp { 71059193Simp /* Yes: use <unscoped-template-name>. */ 71159193Simp write_unscoped_template_name (TI_TEMPLATE (template_info)); 71264850Simp write_template_args (TI_ARGS (template_info)); 71361788Simp } 71459193Simp else 71561788Simp /* Everything else gets an <unqualified-name>. */ 71659193Simp write_unscoped_name (decl); 71759193Simp } 71859193Simp else 71953873Simp { 72053873Simp /* Handle local names, unless we asked not to (that is, invoked 72153873Simp under <local-name>, to handle only the part of the name under 72253873Simp the local scope). */ 72353873Simp if (!ignore_local_scope) 72453873Simp { 72553873Simp /* Scan up the list of scope context, looking for a 72653873Simp function. If we find one, this entity is in local 72753873Simp function scope. local_entity tracks context one scope 72853873Simp level down, so it will contain the element that's 72953873Simp directly in that function's scope, either decl or one of 73053873Simp its enclosing scopes. */ 73153873Simp tree local_entity = decl; 73253873Simp while (context != NULL && context != global_namespace) 73353873Simp { 73453873Simp /* Make sure we're always dealing with decls. */ 73553873Simp if (context != NULL && TYPE_P (context)) 73653873Simp context = TYPE_NAME (context); 73753873Simp /* Is this a function? */ 73853873Simp if (TREE_CODE (context) == FUNCTION_DECL) 73953873Simp { 74053873Simp /* Yes, we have local scope. Use the <local-name> 74153873Simp production for the innermost function scope. */ 74253873Simp write_local_name (context, local_entity, decl); 74353873Simp return; 74453873Simp } 74553873Simp /* Up one scope level. */ 74653873Simp local_entity = context; 74753873Simp context = CP_DECL_CONTEXT (context); 74853873Simp } 74953873Simp 75053873Simp /* No local scope found? Fall through to <nested-name>. */ 75166847Simp } 75253873Simp 75353873Simp /* Other decls get a <nested-name> to encode their scope. */ 75453873Simp write_nested_name (decl); 75553873Simp } 75653873Simp} 75753873Simp 75853873Simp/* <unscoped-name> ::= <unqualified-name> 75953873Simp ::= St <unqualified-name> # ::std:: */ 76053873Simp 76153873Simpstatic void 76253873Simpwrite_unscoped_name (decl) 76353873Simp tree decl; 76453873Simp{ 76570715Sjon tree context = CP_DECL_CONTEXT (decl); 76653873Simp 76767269Simp MANGLE_TRACE_TREE ("unscoped-name", decl); 76867269Simp 76953873Simp /* Is DECL in ::std? */ 77053873Simp if (DECL_NAMESPACE_STD_P (context)) 77153873Simp { 77253873Simp write_string ("St"); 77353873Simp write_unqualified_name (decl); 77453873Simp } 77553873Simp /* If not, it should be either in the global namespace, or directly 77653873Simp in a local function scope. */ 77753873Simp else if (context == global_namespace 77853873Simp || context == NULL 77953873Simp || TREE_CODE (context) == FUNCTION_DECL) 78066847Simp write_unqualified_name (decl); 78153873Simp else 78253873Simp abort (); 78353873Simp} 78453873Simp 78553873Simp/* <unscoped-template-name> ::= <unscoped-name> 78653873Simp ::= <substitution> */ 78753873Simp 78853873Simpstatic void 78953873Simpwrite_unscoped_template_name (decl) 79053873Simp tree decl; 79153873Simp{ 79253873Simp MANGLE_TRACE_TREE ("unscoped-template-name", decl); 79353873Simp 79453873Simp if (find_substitution (decl)) 79553873Simp return; 79653873Simp write_unscoped_name (decl); 79753873Simp add_substitution (decl); 79853873Simp} 79953873Simp 80053873Simp/* Write the nested name, including CV-qualifiers, of DECL. 80153873Simp 80253873Simp <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E 80353873Simp ::= N [<CV-qualifiers>] <template-prefix> <template-args> E 80453873Simp 80553873Simp <CV-qualifiers> ::= [r] [V] [K] */ 80666847Simp 80753873Simpstatic void 80853873Simpwrite_nested_name (decl) 80953873Simp tree decl; 81053873Simp{ 81153873Simp tree template_info; 81253873Simp 81370715Sjon MANGLE_TRACE_TREE ("nested-name", decl); 81453873Simp 81553873Simp write_char ('N'); 81653873Simp 81753873Simp /* Write CV-qualifiers, if this is a member function. */ 81853873Simp if (TREE_CODE (decl) == FUNCTION_DECL 81953873Simp && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 82053873Simp { 82153873Simp if (DECL_VOLATILE_MEMFUNC_P (decl)) 82253873Simp write_char ('V'); 82353873Simp if (DECL_CONST_MEMFUNC_P (decl)) 82453873Simp write_char ('K'); 82566847Simp } 82653873Simp 82753873Simp /* Is this a template instance? */ 82853873Simp if (decl_is_template_id (decl, &template_info)) 82953873Simp { 83059193Simp /* Yes, use <template-prefix>. */ 83159193Simp write_template_prefix (decl); 83259193Simp write_template_args (TI_ARGS (template_info)); 83359193Simp } 83459193Simp else 83559193Simp { 83659193Simp /* No, just use <prefix> */ 83759193Simp write_prefix (DECL_CONTEXT (decl)); 83859193Simp write_unqualified_name (decl); 83959193Simp } 84070748Simp write_char ('E'); 84170715Sjon} 84259193Simp 84359193Simp/* <prefix> ::= <prefix> <unqualified-name>> 84470748Simp ::= <template-prefix> <template-args> 84559193Simp ::= # empty 84659193Simp ::= <substitution> */ 84766058Simp 84866058Simpstatic void 84966058Simpwrite_prefix (node) 85066847Simp tree node; 85166779Simp{ 85266779Simp tree decl; 85366779Simp /* Non-NULL if NODE represents a template-id. */ 85466058Simp tree template_info = NULL; 85566779Simp 85666779Simp MANGLE_TRACE_TREE ("prefix", node); 85766779Simp 85866779Simp if (node == NULL 85966779Simp || node == global_namespace) 86066779Simp return; 86166779Simp 86266779Simp if (find_substitution (node)) 86366779Simp return; 86466779Simp 86566779Simp if (DECL_P (node)) 86666779Simp /* Node is a decl. */ 86766847Simp { 86866847Simp /* If this is a function decl, that means we've hit function 86966847Simp scope, so this prefix must be for a local name. In this 87066847Simp case, we're under the <local-name> production, which encodes 87166847Simp the enclosing function scope elsewhere. So don't continue 87266779Simp here. */ 87366779Simp if (TREE_CODE (node) == FUNCTION_DECL) 87466779Simp return; 87566779Simp 87666779Simp decl = node; 87766779Simp decl_is_template_id (decl, &template_info); 87866779Simp } 87966779Simp else 88066779Simp /* Node is a type. */ 88166779Simp { 88267167Simp decl = TYPE_NAME (node); 88367167Simp if (CLASSTYPE_TEMPLATE_ID_P (node)) 88467167Simp template_info = CLASSTYPE_TEMPLATE_INFO (node); 88566779Simp } 88666779Simp 88766058Simp if (template_info != NULL) 88866058Simp /* Templated. */ 88966779Simp { 89066779Simp write_template_prefix (decl); 89166779Simp write_template_args (TI_ARGS (template_info)); 89267167Simp } 89367167Simp else 89467167Simp /* Not templated. */ 89567167Simp { 89667167Simp write_prefix (CP_DECL_CONTEXT (decl)); 89767167Simp write_unqualified_name (decl); 89866779Simp } 89966058Simp 90067242Simp add_substitution (node); 90167242Simp} 90267242Simp 90367242Simp/* <template-prefix> ::= <prefix> <template component> 90470715Sjon ::= <substitution> */ 90567242Simp 90670715Sjonstatic void 90770715Sjonwrite_template_prefix (node) 90867242Simp tree node; 90970715Sjon{ 91070715Sjon tree decl = DECL_P (node) ? node : TYPE_NAME (node); 91170715Sjon tree type = DECL_P (node) ? TREE_TYPE (node) : node; 91270715Sjon tree context = CP_DECL_CONTEXT (decl); 91367242Simp tree template_info; 91470715Sjon tree template; 91570715Sjon tree substitution; 91670715Sjon 91770715Sjon MANGLE_TRACE_TREE ("template-prefix", node); 91870715Sjon 91967269Simp /* Find the template decl. */ 92067242Simp if (decl_is_template_id (decl, &template_info)) 92167242Simp template = TI_TEMPLATE (template_info); 92267242Simp else if (CLASSTYPE_TEMPLATE_ID_P (type)) 92367242Simp template = CLASSTYPE_TI_TEMPLATE (type); 92467242Simp else 92567242Simp /* Oops, not a template. */ 92667242Simp abort (); 92767242Simp 92870715Sjon /* For a member template, though, the template name for the 92970715Sjon innermost name must have all the outer template levels 93070715Sjon instantiated. For instance, consider 93170715Sjon 93270715Sjon template<typename T> struct Outer { 93370715Sjon template<typename U> struct Inner {}; 93470715Sjon }; 93570715Sjon 93670715Sjon The template name for `Inner' in `Outer<int>::Inner<float>' is 93770715Sjon `Outer<int>::Inner<U>'. In g++, we don't instantiate the template 93870715Sjon levels separately, so there's no TEMPLATE_DECL available for this 93970715Sjon (there's only `Outer<T>::Inner<U>'). 94070715Sjon 94167242Simp In order to get the substitutions right, we create a special 94267242Simp TREE_LIST to represent the substitution candidate for a nested 94367242Simp template. The TREE_PURPOSE is the template's context, fully 94467242Simp instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner 94567242Simp template. 94667242Simp 94767242Simp So, for the example above, `Outer<int>::Inner' is represented as a 94867269Simp substitution candidate by a TREE_LIST whose purpose is `Outer<int>' 94970715Sjon and whose value is `Outer<T>::Inner<U>'. */ 95070715Sjon if (TYPE_P (context)) 95170715Sjon substitution = build_tree_list (context, template); 95270715Sjon else 95370715Sjon substitution = template; 95470715Sjon 95570715Sjon if (find_substitution (substitution)) 95670715Sjon return; 95770715Sjon 95870715Sjon write_prefix (context); 95970715Sjon write_unqualified_name (decl); 96070715Sjon 96170715Sjon add_substitution (substitution); 96267242Simp} 96367242Simp 96467242Simp/* We don't need to handle thunks, vtables, or VTTs here. Those are 96567242Simp mangled through special entry points. 96667242Simp 96767242Simp <unqualified-name> ::= <operator-name> 96867242Simp ::= <special-name> 96967269Simp ::= <source-name> */ 97070715Sjon 97170715Sjonstatic void 97270715Sjonwrite_unqualified_name (decl) 97370715Sjon tree decl; 97470715Sjon{ 97570715Sjon MANGLE_TRACE_TREE ("unqualified-name", decl); 97670715Sjon 97770715Sjon if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl)) 97870715Sjon write_special_name_constructor (decl); 97970715Sjon else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl)) 98070715Sjon write_special_name_destructor (decl); 98170715Sjon else if (DECL_CONV_FN_P (decl)) 98270715Sjon { 98367242Simp /* Conversion operator. Handle it right here. 98467242Simp <operator> ::= cv <type> */ 98567242Simp write_string ("cv"); 98667333Simp write_type (TREE_TYPE (DECL_NAME (decl))); 98767333Simp } 98867333Simp else if (DECL_OVERLOADED_OPERATOR_P (decl)) 98967333Simp { 99067333Simp operator_name_info_t *oni; 99167333Simp if (DECL_ASSIGNMENT_OPERATOR_P (decl)) 99267333Simp oni = assignment_operator_name_info; 99367333Simp else 99467333Simp oni = operator_name_info; 99570715Sjon 99670715Sjon write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name); 99770715Sjon } 99870715Sjon else 99970715Sjon write_source_name (DECL_NAME (decl)); 100070715Sjon} 100170715Sjon 100270715Sjon/* Non-termial <source-name>. IDENTIFIER is an IDENTIFIER_NODE. 100370715Sjon 100470715Sjon <source-name> ::= </length/ number> <identifier> */ 100570715Sjon 100670715Sjonstatic void 100770715Sjonwrite_source_name (identifier) 100870715Sjon tree identifier; 100970715Sjon{ 101070715Sjon MANGLE_TRACE_TREE ("source-name", identifier); 101170715Sjon 101270715Sjon /* Never write the whole template-id name including the template 101370715Sjon arguments; we only want the template name. */ 101470715Sjon if (IDENTIFIER_TEMPLATE (identifier)) 101570715Sjon identifier = IDENTIFIER_TEMPLATE (identifier); 101670715Sjon 101770715Sjon write_unsigned_number (IDENTIFIER_LENGTH (identifier)); 101870715Sjon write_identifier (IDENTIFIER_POINTER (identifier)); 101970715Sjon} 102070715Sjon 102170715Sjon/* Convert NUMBER to ascii using base BASE and generating at least 102270715Sjon MIN_DIGITS characters. BUFFER points to the _end_ of the buffer 102370715Sjon into which to store the characters. Returns the number of 102470715Sjon characters generated (these will be layed out in advance of where 102570715Sjon BUFFER points). */ 102670715Sjon 102770715Sjonstatic int 102870715Sjonhwint_to_ascii (number, base, buffer, min_digits) 102970761Simp unsigned HOST_WIDE_INT number; 103070715Sjon unsigned int base; 103170715Sjon char *buffer; 103270715Sjon unsigned min_digits; 103370715Sjon{ 103470715Sjon static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 103570715Sjon unsigned digits = 0; 103670715Sjon 103770715Sjon while (number) 103870715Sjon { 103970715Sjon unsigned HOST_WIDE_INT d = number / base; 104070715Sjon 104170715Sjon *--buffer = base_digits[number - d * base]; 104270715Sjon digits++; 104370715Sjon number = d; 104470715Sjon } 104570715Sjon while (digits < min_digits) 104670715Sjon { 104770715Sjon *--buffer = base_digits[0]; 104870715Sjon digits++; 104970715Sjon } 105070715Sjon return digits; 105170715Sjon} 105270715Sjon 105370715Sjon/* Non-terminal <number>. 105470715Sjon 105570715Sjon <number> ::= [n] </decimal integer/> */ 105670715Sjon 105770715Sjonstatic void 105870715Sjonwrite_number (number, unsigned_p, base) 105970715Sjon unsigned HOST_WIDE_INT number; 106070715Sjon int unsigned_p; 106170715Sjon unsigned int base; 106270715Sjon{ 106370715Sjon char buffer[sizeof (HOST_WIDE_INT) * 8]; 106470715Sjon unsigned count = 0; 106570715Sjon 106670715Sjon if (!unsigned_p && (HOST_WIDE_INT) number < 0) 106770715Sjon { 106870715Sjon write_char ('n'); 106970715Sjon number = -((HOST_WIDE_INT) number); 107070715Sjon } 107170715Sjon count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1); 107270715Sjon write_chars (buffer + sizeof (buffer) - count, count); 107370715Sjon} 107452506Simp 107552506Simp/* Write out an integral CST in decimal. Most numbers are small, and 107652506Simp representable in a HOST_WIDE_INT. Occasionally we'll have numbers 107759193Simp bigger than that, which we must deal with. */ 107861788Simp 107952506Simpstatic inline void 108052506Simpwrite_integer_cst (cst) 108152506Simp tree cst; 108252506Simp{ 108352506Simp int sign = tree_int_cst_sgn (cst); 108452506Simp 108566779Simp if (TREE_INT_CST_HIGH (cst) + (sign < 0)) 108667333Simp { 108767242Simp /* A bignum. We do this in chunks, each of which fits in a 108867242Simp HOST_WIDE_INT. */ 108967242Simp char buffer[sizeof (HOST_WIDE_INT) * 8 * 2]; 109067242Simp unsigned HOST_WIDE_INT chunk; 109170715Sjon unsigned chunk_digits; 109270715Sjon char *ptr = buffer + sizeof (buffer); 109352506Simp unsigned count = 0; 109452506Simp tree n, base, type; 109552506Simp int done; 109666058Simp 109752506Simp /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is 109859193Simp representable. */ 109959193Simp chunk = 1000000000; 110059193Simp chunk_digits = 9; 110159193Simp 110259193Simp if (sizeof (HOST_WIDE_INT) >= 8) 110359193Simp { 110459193Simp /* It is at least 64 bits, so 10^18 is representable. */ 110552506Simp chunk_digits = 18; 110652506Simp chunk *= chunk; 110752506Simp } 110852506Simp 110952506Simp type = signed_or_unsigned_type (1, TREE_TYPE (cst)); 111052506Simp base = build_int_2 (chunk, 0); 111164850Simp n = build_int_2 (TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst)); 111252506Simp TREE_TYPE (n) = TREE_TYPE (base) = type; 111352506Simp 111452506Simp if (sign < 0) 111552506Simp { 111653873Simp write_char ('n'); 111752506Simp n = fold (build1 (NEGATE_EXPR, type, n)); 111852506Simp } 111953873Simp do 112064927Simp { 112170715Sjon tree d = fold (build (FLOOR_DIV_EXPR, type, n, base)); 1122 tree tmp = fold (build (MULT_EXPR, type, d, base)); 1123 unsigned c; 1124 1125 done = integer_zerop (d); 1126 tmp = fold (build (MINUS_EXPR, type, n, tmp)); 1127 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr, 1128 done ? 1 : chunk_digits); 1129 ptr -= c; 1130 count += c; 1131 n = d; 1132 } 1133 while (!done); 1134 write_chars (ptr, count); 1135 } 1136 else 1137 { 1138 /* A small num. */ 1139 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst); 1140 1141 if (sign < 0) 1142 { 1143 write_char ('n'); 1144 low = -low; 1145 } 1146 write_unsigned_number (low); 1147 } 1148} 1149 1150/* Non-terminal <identifier>. 1151 1152 <identifier> ::= </unqualified source code identifier> */ 1153 1154static void 1155write_identifier (identifier) 1156 const char *identifier; 1157{ 1158 MANGLE_TRACE ("identifier", identifier); 1159 write_string (identifier); 1160} 1161 1162/* Handle constructor productions of non-terminal <special-name>. 1163 CTOR is a constructor FUNCTION_DECL. 1164 1165 <special-name> ::= C1 # complete object constructor 1166 ::= C2 # base object constructor 1167 ::= C3 # complete object allocating constructor 1168 1169 Currently, allocating constructors are never used. 1170 1171 We also need to provide mangled names for the maybe-in-charge 1172 constructor, so we treat it here too. mangle_decl_string will 1173 append *INTERNAL* to that, to make sure we never emit it. */ 1174 1175static void 1176write_special_name_constructor (ctor) 1177 tree ctor; 1178{ 1179 if (DECL_COMPLETE_CONSTRUCTOR_P (ctor) 1180 /* Even though we don't ever emit a definition of the 1181 old-style destructor, we still have to consider entities 1182 (like static variables) nested inside it. */ 1183 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor)) 1184 write_string ("C1"); 1185 else if (DECL_BASE_CONSTRUCTOR_P (ctor)) 1186 write_string ("C2"); 1187 else 1188 abort (); 1189} 1190 1191/* Handle destructor productions of non-terminal <special-name>. 1192 DTOR is a destructor FUNCTION_DECL. 1193 1194 <special-name> ::= D0 # deleting (in-charge) destructor 1195 ::= D1 # complete object (in-charge) destructor 1196 ::= D2 # base object (not-in-charge) destructor 1197 1198 We also need to provide mangled names for the maybe-incharge 1199 destructor, so we treat it here too. mangle_decl_string will 1200 append *INTERNAL* to that, to make sure we never emit it. */ 1201 1202static void 1203write_special_name_destructor (dtor) 1204 tree dtor; 1205{ 1206 if (DECL_DELETING_DESTRUCTOR_P (dtor)) 1207 write_string ("D0"); 1208 else if (DECL_COMPLETE_DESTRUCTOR_P (dtor) 1209 /* Even though we don't ever emit a definition of the 1210 old-style destructor, we still have to consider entities 1211 (like static variables) nested inside it. */ 1212 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor)) 1213 write_string ("D1"); 1214 else if (DECL_BASE_DESTRUCTOR_P (dtor)) 1215 write_string ("D2"); 1216 else 1217 abort (); 1218} 1219 1220/* Return the discriminator for ENTITY appearing inside 1221 FUNCTION. The discriminator is the lexical ordinal of VAR among 1222 entities with the same name in the same FUNCTION. */ 1223 1224static int 1225discriminator_for_local_entity (entity) 1226 tree entity; 1227{ 1228 tree *type; 1229 int discriminator; 1230 1231 /* Assume this is the only local entity with this name. */ 1232 discriminator = 0; 1233 1234 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity)) 1235 discriminator = DECL_DISCRIMINATOR (entity); 1236 else if (TREE_CODE (entity) == TYPE_DECL) 1237 { 1238 /* Scan the list of local classes. */ 1239 entity = TREE_TYPE (entity); 1240 for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type) 1241 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity) 1242 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity)) 1243 ++discriminator; 1244 } 1245 1246 return discriminator; 1247} 1248 1249/* Return the discriminator for STRING, a string literal used inside 1250 FUNCTION. The disciminator is the lexical ordinal of STRING among 1251 string literals used in FUNCTION. */ 1252 1253static int 1254discriminator_for_string_literal (function, string) 1255 tree function ATTRIBUTE_UNUSED; 1256 tree string ATTRIBUTE_UNUSED; 1257{ 1258 /* For now, we don't discriminate amongst string literals. */ 1259 return 0; 1260} 1261 1262/* <discriminator> := _ <number> 1263 1264 The discriminator is used only for the second and later occurrences 1265 of the same name within a single function. In this case <number> is 1266 n - 2, if this is the nth occurrence, in lexical order. */ 1267 1268static void 1269write_discriminator (discriminator) 1270 int discriminator; 1271{ 1272 /* If discriminator is zero, don't write anything. Otherwise... */ 1273 if (discriminator > 0) 1274 { 1275 write_char ('_'); 1276 write_unsigned_number (discriminator - 1); 1277 } 1278} 1279 1280/* Mangle the name of a function-scope entity. FUNCTION is the 1281 FUNCTION_DECL for the enclosing function. ENTITY is the decl for 1282 the entity itself. LOCAL_ENTITY is the entity that's directly 1283 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope 1284 of ENTITY. 1285 1286 <local-name> := Z <function encoding> E <entity name> [<discriminator>] 1287 := Z <function encoding> E s [<discriminator>] */ 1288 1289static void 1290write_local_name (function, local_entity, entity) 1291 tree function; 1292 tree local_entity; 1293 tree entity; 1294{ 1295 MANGLE_TRACE_TREE ("local-name", entity); 1296 1297 write_char ('Z'); 1298 write_encoding (function); 1299 write_char ('E'); 1300 if (TREE_CODE (entity) == STRING_CST) 1301 { 1302 write_char ('s'); 1303 write_discriminator (discriminator_for_string_literal (function, 1304 entity)); 1305 } 1306 else 1307 { 1308 /* Now the <entity name>. Let write_name know its being called 1309 from <local-name>, so it doesn't try to process the enclosing 1310 function scope again. */ 1311 write_name (entity, /*ignore_local_scope=*/1); 1312 write_discriminator (discriminator_for_local_entity (local_entity)); 1313 } 1314} 1315 1316/* Non-terminals <type> and <CV-qualifier>. 1317 1318 <type> ::= <builtin-type> 1319 ::= <function-type> 1320 ::= <class-enum-type> 1321 ::= <array-type> 1322 ::= <pointer-to-member-type> 1323 ::= <template-param> 1324 ::= <substitution> 1325 ::= <CV-qualifier> 1326 ::= P <type> # pointer-to 1327 ::= R <type> # reference-to 1328 ::= C <type> # complex pair (C 2000) 1329 ::= G <type> # imaginary (C 2000) [not supported] 1330 ::= U <source-name> <type> # vendor extended type qualifier 1331 1332 TYPE is a type node. */ 1333 1334static void 1335write_type (type) 1336 tree type; 1337{ 1338 /* This gets set to non-zero if TYPE turns out to be a (possibly 1339 CV-qualified) builtin type. */ 1340 int is_builtin_type = 0; 1341 1342 MANGLE_TRACE_TREE ("type", type); 1343 1344 if (type == error_mark_node) 1345 return; 1346 1347 if (find_substitution (type)) 1348 return; 1349 1350 if (write_CV_qualifiers_for_type (type) > 0) 1351 /* If TYPE was CV-qualified, we just wrote the qualifiers; now 1352 mangle the unqualified type. The recursive call is needed here 1353 since both the qualified and uqualified types are substitution 1354 candidates. */ 1355 write_type (TYPE_MAIN_VARIANT (type)); 1356 else if (TREE_CODE (type) == ARRAY_TYPE) 1357 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here 1358 so that the cv-qualification of the element type is available 1359 in write_array_type. */ 1360 write_array_type (type); 1361 else 1362 { 1363 /* See through any typedefs. */ 1364 type = TYPE_MAIN_VARIANT (type); 1365 1366 switch (TREE_CODE (type)) 1367 { 1368 case VOID_TYPE: 1369 case BOOLEAN_TYPE: 1370 case INTEGER_TYPE: /* Includes wchar_t. */ 1371 case REAL_TYPE: 1372 /* If this is a typedef, TYPE may not be one of 1373 the standard builtin type nodes, but an alias of one. Use 1374 TYPE_MAIN_VARIANT to get to the underlying builtin type. */ 1375 write_builtin_type (TYPE_MAIN_VARIANT (type)); 1376 ++is_builtin_type; 1377 break; 1378 1379 case COMPLEX_TYPE: 1380 write_char ('C'); 1381 write_type (TREE_TYPE (type)); 1382 break; 1383 1384 case FUNCTION_TYPE: 1385 case METHOD_TYPE: 1386 write_function_type (type); 1387 break; 1388 1389 case UNION_TYPE: 1390 case RECORD_TYPE: 1391 case ENUMERAL_TYPE: 1392 /* A pointer-to-member function is represented as a special 1393 RECORD_TYPE, so check for this first. */ 1394 if (TYPE_PTRMEMFUNC_P (type)) 1395 write_pointer_to_member_type (type); 1396 else 1397 write_class_enum_type (type); 1398 break; 1399 1400 case TYPENAME_TYPE: 1401 case UNBOUND_CLASS_TEMPLATE: 1402 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like 1403 ordinary nested names. */ 1404 write_nested_name (TYPE_STUB_DECL (type)); 1405 break; 1406 1407 case POINTER_TYPE: 1408 /* A pointer-to-member variable is represented by a POINTER_TYPE 1409 to an OFFSET_TYPE, so check for this first. */ 1410 if (TYPE_PTRMEM_P (type)) 1411 write_pointer_to_member_type (type); 1412 else 1413 { 1414 write_char ('P'); 1415 write_type (TREE_TYPE (type)); 1416 } 1417 break; 1418 1419 case REFERENCE_TYPE: 1420 write_char ('R'); 1421 write_type (TREE_TYPE (type)); 1422 break; 1423 1424 case TEMPLATE_TYPE_PARM: 1425 case TEMPLATE_PARM_INDEX: 1426 write_template_param (type); 1427 break; 1428 1429 case TEMPLATE_TEMPLATE_PARM: 1430 write_template_template_param (type); 1431 break; 1432 1433 case BOUND_TEMPLATE_TEMPLATE_PARM: 1434 write_template_template_param (type); 1435 write_template_args 1436 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type))); 1437 break; 1438 1439 case OFFSET_TYPE: 1440 write_pointer_to_member_type (build_pointer_type (type)); 1441 break; 1442 1443 case VECTOR_TYPE: 1444 write_string ("U8__vector"); 1445 write_type (TREE_TYPE (type)); 1446 break; 1447 1448 default: 1449 abort (); 1450 } 1451 } 1452 1453 /* Types other than builtin types are substitution candidates. */ 1454 if (!is_builtin_type) 1455 add_substitution (type); 1456} 1457 1458/* Non-terminal <CV-qualifiers> for type nodes. Returns the number of 1459 CV-qualifiers written for TYPE. 1460 1461 <CV-qualifiers> ::= [r] [V] [K] */ 1462 1463static int 1464write_CV_qualifiers_for_type (type) 1465 tree type; 1466{ 1467 int num_qualifiers = 0; 1468 1469 /* The order is specified by: 1470 1471 "In cases where multiple order-insensitive qualifiers are 1472 present, they should be ordered 'K' (closest to the base type), 1473 'V', 'r', and 'U' (farthest from the base type) ..." 1474 1475 Note that we do not use cp_type_quals below; given "const 1476 int[3]", the "const" is emitted with the "int", not with the 1477 array. */ 1478 1479 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT) 1480 { 1481 write_char ('r'); 1482 ++num_qualifiers; 1483 } 1484 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE) 1485 { 1486 write_char ('V'); 1487 ++num_qualifiers; 1488 } 1489 if (TYPE_QUALS (type) & TYPE_QUAL_CONST) 1490 { 1491 write_char ('K'); 1492 ++num_qualifiers; 1493 } 1494 1495 return num_qualifiers; 1496} 1497 1498/* Non-terminal <builtin-type>. 1499 1500 <builtin-type> ::= v # void 1501 ::= b # bool 1502 ::= w # wchar_t 1503 ::= c # char 1504 ::= a # signed char 1505 ::= h # unsigned char 1506 ::= s # short 1507 ::= t # unsigned short 1508 ::= i # int 1509 ::= j # unsigned int 1510 ::= l # long 1511 ::= m # unsigned long 1512 ::= x # long long, __int64 1513 ::= y # unsigned long long, __int64 1514 ::= n # __int128 1515 ::= o # unsigned __int128 1516 ::= f # float 1517 ::= d # double 1518 ::= e # long double, __float80 1519 ::= g # __float128 [not supported] 1520 ::= u <source-name> # vendor extended type */ 1521 1522static void 1523write_builtin_type (type) 1524 tree type; 1525{ 1526 switch (TREE_CODE (type)) 1527 { 1528 case VOID_TYPE: 1529 write_char ('v'); 1530 break; 1531 1532 case BOOLEAN_TYPE: 1533 write_char ('b'); 1534 break; 1535 1536 case INTEGER_TYPE: 1537 /* If this is size_t, get the underlying int type. */ 1538 if (TYPE_IS_SIZETYPE (type)) 1539 type = TYPE_DOMAIN (type); 1540 1541 /* TYPE may still be wchar_t, since that isn't in 1542 integer_type_nodes. */ 1543 if (type == wchar_type_node) 1544 write_char ('w'); 1545 else if (TYPE_FOR_JAVA (type)) 1546 write_java_integer_type_codes (type); 1547 else 1548 { 1549 size_t itk; 1550 /* Assume TYPE is one of the shared integer type nodes. Find 1551 it in the array of these nodes. */ 1552 iagain: 1553 for (itk = 0; itk < itk_none; ++itk) 1554 if (type == integer_types[itk]) 1555 { 1556 /* Print the corresponding single-letter code. */ 1557 write_char (integer_type_codes[itk]); 1558 break; 1559 } 1560 1561 if (itk == itk_none) 1562 { 1563 tree t = type_for_mode (TYPE_MODE (type), TREE_UNSIGNED (type)); 1564 if (type == t) 1565 { 1566 if (TYPE_PRECISION (type) == 128) 1567 write_char (TREE_UNSIGNED (type) ? 'o' : 'n'); 1568 else 1569 /* Couldn't find this type. */ 1570 abort (); 1571 } 1572 else 1573 { 1574 type = t; 1575 goto iagain; 1576 } 1577 } 1578 } 1579 break; 1580 1581 case REAL_TYPE: 1582 if (type == float_type_node 1583 || type == java_float_type_node) 1584 write_char ('f'); 1585 else if (type == double_type_node 1586 || type == java_double_type_node) 1587 write_char ('d'); 1588 else if (type == long_double_type_node) 1589 write_char ('e'); 1590 else 1591 abort (); 1592 break; 1593 1594 default: 1595 abort (); 1596 } 1597} 1598 1599/* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or 1600 METHOD_TYPE. The return type is mangled before the parameter 1601 types. 1602 1603 <function-type> ::= F [Y] <bare-function-type> E */ 1604 1605static void 1606write_function_type (type) 1607 tree type; 1608{ 1609 MANGLE_TRACE_TREE ("function-type", type); 1610 1611 /* For a pointer to member function, the function type may have 1612 cv-qualifiers, indicating the quals for the artificial 'this' 1613 parameter. */ 1614 if (TREE_CODE (type) == METHOD_TYPE) 1615 { 1616 /* The first parameter must be a POINTER_TYPE pointing to the 1617 `this' parameter. */ 1618 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))); 1619 write_CV_qualifiers_for_type (this_type); 1620 } 1621 1622 write_char ('F'); 1623 /* We don't track whether or not a type is `extern "C"'. Note that 1624 you can have an `extern "C"' function that does not have 1625 `extern "C"' type, and vice versa: 1626 1627 extern "C" typedef void function_t(); 1628 function_t f; // f has C++ linkage, but its type is 1629 // `extern "C"' 1630 1631 typedef void function_t(); 1632 extern "C" function_t f; // Vice versa. 1633 1634 See [dcl.link]. */ 1635 write_bare_function_type (type, /*include_return_type_p=*/1, 1636 /*decl=*/NULL); 1637 write_char ('E'); 1638} 1639 1640/* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or 1641 METHOD_TYPE. If INCLUDE_RETURN_TYPE is non-zero, the return value 1642 is mangled before the parameter types. If non-NULL, DECL is 1643 FUNCTION_DECL for the function whose type is being emitted. 1644 1645 <bare-function-type> ::= </signature/ type>+ */ 1646 1647static void 1648write_bare_function_type (type, include_return_type_p, decl) 1649 tree type; 1650 int include_return_type_p; 1651 tree decl; 1652{ 1653 MANGLE_TRACE_TREE ("bare-function-type", type); 1654 1655 /* Mangle the return type, if requested. */ 1656 if (include_return_type_p) 1657 write_type (TREE_TYPE (type)); 1658 1659 /* Now mangle the types of the arguments. */ 1660 write_method_parms (TYPE_ARG_TYPES (type), 1661 TREE_CODE (type) == METHOD_TYPE, 1662 decl); 1663} 1664 1665/* Write the mangled representation of a method parameter list of 1666 types given in PARM_TYPES. If METHOD_P is non-zero, the function is 1667 considered a non-static method, and the this parameter is omitted. 1668 If non-NULL, DECL is the FUNCTION_DECL for the function whose 1669 parameters are being emitted. */ 1670 1671static void 1672write_method_parms (parm_types, method_p, decl) 1673 tree decl; 1674 tree parm_types; 1675 int method_p; 1676{ 1677 tree first_parm_type; 1678 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE; 1679 1680 /* Assume this parameter type list is variable-length. If it ends 1681 with a void type, then it's not. */ 1682 int varargs_p = 1; 1683 1684 /* If this is a member function, skip the first arg, which is the 1685 this pointer. 1686 "Member functions do not encode the type of their implicit this 1687 parameter." 1688 1689 Similarly, there's no need to mangle artificial parameters, like 1690 the VTT parameters for constructors and destructors. */ 1691 if (method_p) 1692 { 1693 parm_types = TREE_CHAIN (parm_types); 1694 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE; 1695 1696 while (parm_decl && DECL_ARTIFICIAL (parm_decl)) 1697 { 1698 parm_types = TREE_CHAIN (parm_types); 1699 parm_decl = TREE_CHAIN (parm_decl); 1700 } 1701 } 1702 1703 for (first_parm_type = parm_types; 1704 parm_types; 1705 parm_types = TREE_CHAIN (parm_types)) 1706 { 1707 tree parm = TREE_VALUE (parm_types); 1708 if (parm == void_type_node) 1709 { 1710 /* "Empty parameter lists, whether declared as () or 1711 conventionally as (void), are encoded with a void parameter 1712 (v)." */ 1713 if (parm_types == first_parm_type) 1714 write_type (parm); 1715 /* If the parm list is terminated with a void type, it's 1716 fixed-length. */ 1717 varargs_p = 0; 1718 /* A void type better be the last one. */ 1719 my_friendly_assert (TREE_CHAIN (parm_types) == NULL, 20000523); 1720 } 1721 else 1722 write_type (parm); 1723 } 1724 1725 if (varargs_p) 1726 /* <builtin-type> ::= z # ellipsis */ 1727 write_char ('z'); 1728} 1729 1730/* <class-enum-type> ::= <name> */ 1731 1732static void 1733write_class_enum_type (type) 1734 tree type; 1735{ 1736 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0); 1737} 1738 1739/* Non-terminal <template-args>. ARGS is a TREE_VEC of template 1740 arguments. 1741 1742 <template-args> ::= I <template-arg>+ E */ 1743 1744static void 1745write_template_args (args) 1746 tree args; 1747{ 1748 int i; 1749 int length = TREE_VEC_LENGTH (args); 1750 1751 MANGLE_TRACE_TREE ("template-args", args); 1752 1753 my_friendly_assert (length > 0, 20000422); 1754 1755 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC) 1756 { 1757 /* We have nested template args. We want the innermost template 1758 argument list. */ 1759 args = TREE_VEC_ELT (args, length - 1); 1760 length = TREE_VEC_LENGTH (args); 1761 } 1762 1763 write_char ('I'); 1764 for (i = 0; i < length; ++i) 1765 write_template_arg (TREE_VEC_ELT (args, i)); 1766 write_char ('E'); 1767} 1768 1769/* <expression> ::= <unary operator-name> <expression> 1770 ::= <binary operator-name> <expression> <expression> 1771 ::= <expr-primary> 1772 1773 <expr-primary> ::= <template-param> 1774 ::= L <type> <value number> E # literal 1775 ::= L <mangled-name> E # external name */ 1776 1777static void 1778write_expression (expr) 1779 tree expr; 1780{ 1781 enum tree_code code; 1782 1783 code = TREE_CODE (expr); 1784 1785 /* Handle pointers-to-members by making them look like expression 1786 nodes. */ 1787 if (code == PTRMEM_CST) 1788 { 1789 expr = build_nt (ADDR_EXPR, 1790 build_nt (SCOPE_REF, 1791 PTRMEM_CST_CLASS (expr), 1792 PTRMEM_CST_MEMBER (expr))); 1793 code = TREE_CODE (expr); 1794 } 1795 1796 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument 1797 is converted (via qualification conversions) to another 1798 type. */ 1799 while (TREE_CODE (expr) == NOP_EXPR 1800 || TREE_CODE (expr) == NON_LVALUE_EXPR) 1801 { 1802 expr = TREE_OPERAND (expr, 0); 1803 code = TREE_CODE (expr); 1804 } 1805 1806 /* Handle template parameters. */ 1807 if (code == TEMPLATE_TYPE_PARM 1808 || code == TEMPLATE_TEMPLATE_PARM 1809 || code == BOUND_TEMPLATE_TEMPLATE_PARM 1810 || code == TEMPLATE_PARM_INDEX) 1811 write_template_param (expr); 1812 /* Handle literals. */ 1813 else if (TREE_CODE_CLASS (code) == 'c') 1814 write_template_arg_literal (expr); 1815 else if (DECL_P (expr)) 1816 { 1817 write_char ('L'); 1818 write_mangled_name (expr); 1819 write_char ('E'); 1820 } 1821 else 1822 { 1823 int i; 1824 1825 /* When we bind a variable or function to a non-type template 1826 argument with reference type, we create an ADDR_EXPR to show 1827 the fact that the entity's address has been taken. But, we 1828 don't actually want to output a mangling code for the `&'. */ 1829 if (TREE_CODE (expr) == ADDR_EXPR 1830 && TREE_TYPE (expr) 1831 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE) 1832 { 1833 expr = TREE_OPERAND (expr, 0); 1834 if (DECL_P (expr)) 1835 { 1836 write_expression (expr); 1837 return; 1838 } 1839 1840 code = TREE_CODE (expr); 1841 } 1842 1843 /* If it wasn't any of those, recursively expand the expression. */ 1844 write_string (operator_name_info[(int) code].mangled_name); 1845 1846 switch (code) 1847 { 1848 case CAST_EXPR: 1849 write_type (TREE_TYPE (expr)); 1850 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0))); 1851 break; 1852 1853 case STATIC_CAST_EXPR: 1854 case CONST_CAST_EXPR: 1855 write_type (TREE_TYPE (expr)); 1856 write_expression (TREE_OPERAND (expr, 0)); 1857 break; 1858 1859 /* Handle pointers-to-members specially. */ 1860 case SCOPE_REF: 1861 write_type (TREE_OPERAND (expr, 0)); 1862 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE) 1863 write_source_name (TREE_OPERAND (expr, 1)); 1864 else 1865 write_encoding (TREE_OPERAND (expr, 1)); 1866 break; 1867 1868 default: 1869 for (i = 0; i < TREE_CODE_LENGTH (code); ++i) 1870 write_expression (TREE_OPERAND (expr, i)); 1871 } 1872 } 1873} 1874 1875/* Literal subcase of non-terminal <template-arg>. 1876 1877 "Literal arguments, e.g. "A<42L>", are encoded with their type 1878 and value. Negative integer values are preceded with "n"; for 1879 example, "A<-42L>" becomes "1AILln42EE". The bool value false is 1880 encoded as 0, true as 1. If floating-point arguments are accepted 1881 as an extension, their values should be encoded using a 1882 fixed-length lowercase hexadecimal string corresponding to the 1883 internal representation (IEEE on IA-64), high-order bytes first, 1884 without leading zeroes. For example: "Lfbff000000E" is -1.0f." */ 1885 1886static void 1887write_template_arg_literal (value) 1888 tree value; 1889{ 1890 tree type = TREE_TYPE (value); 1891 write_char ('L'); 1892 write_type (type); 1893 1894 if (TREE_CODE (value) == CONST_DECL) 1895 write_integer_cst (DECL_INITIAL (value)); 1896 else if (TREE_CODE (value) == INTEGER_CST) 1897 { 1898 if (same_type_p (type, boolean_type_node)) 1899 { 1900 if (value == boolean_false_node || integer_zerop (value)) 1901 write_unsigned_number (0); 1902 else if (value == boolean_true_node) 1903 write_unsigned_number (1); 1904 else 1905 abort (); 1906 } 1907 else 1908 write_integer_cst (value); 1909 } 1910 else if (TREE_CODE (value) == REAL_CST) 1911 { 1912#ifdef CROSS_COMPILE 1913 static int explained; 1914 1915 if (!explained) 1916 { 1917 sorry ("real-valued template parameters when cross-compiling"); 1918 explained = 1; 1919 } 1920#else 1921 size_t i; 1922 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i) 1923 write_number (((unsigned char *) 1924 &TREE_REAL_CST (value))[i], 1925 /*unsigned_p=*/1, 1926 16); 1927#endif 1928 } 1929 else 1930 abort (); 1931 1932 write_char ('E'); 1933} 1934 1935/* Non-terminal <tempalate-arg>. 1936 1937 <template-arg> ::= <type> # type 1938 ::= L <type> </value/ number> E # literal 1939 ::= LZ <name> E # external name 1940 ::= X <expression> E # expression */ 1941 1942static void 1943write_template_arg (node) 1944 tree node; 1945{ 1946 enum tree_code code = TREE_CODE (node); 1947 1948 MANGLE_TRACE_TREE ("template-arg", node); 1949 1950 /* A template template paramter's argument list contains TREE_LIST 1951 nodes of which the value field is the the actual argument. */ 1952 if (code == TREE_LIST) 1953 { 1954 node = TREE_VALUE (node); 1955 /* If it's a decl, deal with its type instead. */ 1956 if (DECL_P (node)) 1957 { 1958 node = TREE_TYPE (node); 1959 code = TREE_CODE (node); 1960 } 1961 } 1962 1963 if (TYPE_P (node)) 1964 write_type (node); 1965 else if (code == TEMPLATE_DECL) 1966 /* A template appearing as a template arg is a template template arg. */ 1967 write_template_template_arg (node); 1968 else if (DECL_P (node)) 1969 { 1970 write_char ('L'); 1971 write_char ('Z'); 1972 write_encoding (node); 1973 write_char ('E'); 1974 } 1975 else if (TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST) 1976 write_template_arg_literal (node); 1977 else 1978 { 1979 /* Template arguments may be expressions. */ 1980 write_char ('X'); 1981 write_expression (node); 1982 write_char ('E'); 1983 } 1984} 1985 1986/* <template-template-arg> 1987 ::= <name> 1988 ::= <substitution> */ 1989 1990void 1991write_template_template_arg (tree decl) 1992{ 1993 MANGLE_TRACE_TREE ("template-template-arg", decl); 1994 1995 if (find_substitution (decl)) 1996 return; 1997 write_name (decl, /*ignore_local_scope=*/0); 1998 add_substitution (decl); 1999} 2000 2001 2002/* Non-terminal <array-type>. TYPE is an ARRAY_TYPE. 2003 2004 <array-type> ::= A [</dimension/ number>] _ </element/ type> 2005 ::= A <expression> _ </element/ type> 2006 2007 "Array types encode the dimension (number of elements) and the 2008 element type. For variable length arrays, the dimension (but not 2009 the '_' separator) is omitted." */ 2010 2011static void 2012write_array_type (type) 2013 tree type; 2014{ 2015 write_char ('A'); 2016 if (TYPE_DOMAIN (type)) 2017 { 2018 tree index_type; 2019 tree max; 2020 2021 index_type = TYPE_DOMAIN (type); 2022 /* The INDEX_TYPE gives the upper and lower bounds of the 2023 array. */ 2024 max = TYPE_MAX_VALUE (index_type); 2025 if (TREE_CODE (max) == INTEGER_CST) 2026 { 2027 /* The ABI specifies that we should mangle the number of 2028 elements in the array, not the largest allowed index. */ 2029 max = size_binop (PLUS_EXPR, max, size_one_node); 2030 write_unsigned_number (tree_low_cst (max, 1)); 2031 } 2032 else 2033 write_expression (TREE_OPERAND (max, 0)); 2034 } 2035 write_char ('_'); 2036 write_type (TREE_TYPE (type)); 2037} 2038 2039/* Non-terminal <pointer-to-member-type> for pointer-to-member 2040 variables. TYPE is a pointer-to-member POINTER_TYPE. 2041 2042 <pointer-to-member-type> ::= M </class/ type> </member/ type> */ 2043 2044static void 2045write_pointer_to_member_type (type) 2046 tree type; 2047{ 2048 write_char ('M'); 2049 write_type (TYPE_PTRMEM_CLASS_TYPE (type)); 2050 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type)); 2051} 2052 2053/* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM, 2054 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a 2055 TEMPLATE_PARM_INDEX. 2056 2057 <template-param> ::= T </parameter/ number> _ */ 2058 2059static void 2060write_template_param (parm) 2061 tree parm; 2062{ 2063 int parm_index; 2064 2065 MANGLE_TRACE_TREE ("template-parm", parm); 2066 2067 switch (TREE_CODE (parm)) 2068 { 2069 case TEMPLATE_TYPE_PARM: 2070 case TEMPLATE_TEMPLATE_PARM: 2071 case BOUND_TEMPLATE_TEMPLATE_PARM: 2072 parm_index = TEMPLATE_TYPE_IDX (parm); 2073 break; 2074 2075 case TEMPLATE_PARM_INDEX: 2076 parm_index = TEMPLATE_PARM_IDX (parm); 2077 break; 2078 2079 default: 2080 abort (); 2081 } 2082 2083 write_char ('T'); 2084 /* NUMBER as it appears in the mangling is (-1)-indexed, with the 2085 earliest template param denoted by `_'. */ 2086 if (parm_index > 0) 2087 write_unsigned_number (parm_index - 1); 2088 write_char ('_'); 2089} 2090 2091/* <template-template-param> 2092 ::= <template-param> 2093 ::= <substitution> */ 2094 2095static void 2096write_template_template_param (parm) 2097 tree parm; 2098{ 2099 tree template = NULL_TREE; 2100 2101 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the 2102 template template parameter. The substitution candidate here is 2103 only the template. */ 2104 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 2105 { 2106 template 2107 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)); 2108 if (find_substitution (template)) 2109 return; 2110 } 2111 2112 /* <template-param> encodes only the template parameter position, 2113 not its template arguments, which is fine here. */ 2114 write_template_param (parm); 2115 if (template) 2116 add_substitution (template); 2117} 2118 2119/* Non-terminal <substitution>. 2120 2121 <substitution> ::= S <seq-id> _ 2122 ::= S_ */ 2123 2124static void 2125write_substitution (seq_id) 2126 int seq_id; 2127{ 2128 MANGLE_TRACE ("substitution", ""); 2129 2130 write_char ('S'); 2131 if (seq_id > 0) 2132 write_number (seq_id - 1, /*unsigned=*/1, 36); 2133 write_char ('_'); 2134} 2135 2136/* Start mangling a new name or type. */ 2137 2138static inline void 2139start_mangling () 2140{ 2141 obstack_free (&G.name_obstack, obstack_base (&G.name_obstack)); 2142} 2143 2144/* Done with mangling. Return the generated mangled name. */ 2145 2146static inline const char * 2147finish_mangling () 2148{ 2149 /* Clear all the substitutions. */ 2150 VARRAY_POP_ALL (G.substitutions); 2151 2152 /* Null-terminate the string. */ 2153 write_char ('\0'); 2154 2155 return (const char *) obstack_base (&G.name_obstack); 2156} 2157 2158/* Initialize data structures for mangling. */ 2159 2160void 2161init_mangle () 2162{ 2163 gcc_obstack_init (&G.name_obstack); 2164 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions"); 2165 2166 /* Cache these identifiers for quick comparison when checking for 2167 standard substitutions. */ 2168 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator"); 2169 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string"); 2170 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits"); 2171 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream"); 2172 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream"); 2173 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream"); 2174} 2175 2176/* Generate the mangled name of DECL. */ 2177 2178static const char * 2179mangle_decl_string (decl) 2180 tree decl; 2181{ 2182 const char *result; 2183 2184 start_mangling (); 2185 2186 if (TREE_CODE (decl) == TYPE_DECL) 2187 write_type (TREE_TYPE (decl)); 2188 else if (/* The names of `extern "C"' functions are not mangled. */ 2189 (DECL_EXTERN_C_FUNCTION_P (decl) 2190 /* But overloaded operator names *are* mangled. */ 2191 && !DECL_OVERLOADED_OPERATOR_P (decl)) 2192 /* The names of global variables aren't mangled either. */ 2193 || (TREE_CODE (decl) == VAR_DECL 2194 && CP_DECL_CONTEXT (decl) == global_namespace) 2195 /* And neither are `extern "C"' variables. */ 2196 || (TREE_CODE (decl) == VAR_DECL 2197 && DECL_EXTERN_C_P (decl))) 2198 write_string (IDENTIFIER_POINTER (DECL_NAME (decl))); 2199 else 2200 { 2201 write_mangled_name (decl); 2202 if (DECL_LANG_SPECIFIC (decl) 2203 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl) 2204 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))) 2205 /* We need a distinct mangled name for these entities, but 2206 we should never actually output it. So, we append some 2207 characters the assembler won't like. */ 2208 write_string (" *INTERNAL* "); 2209 } 2210 2211 result = finish_mangling (); 2212 if (DEBUG_MANGLE) 2213 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result); 2214 return result; 2215} 2216 2217/* Create an identifier for the external mangled name of DECL. */ 2218 2219void 2220mangle_decl (decl) 2221 tree decl; 2222{ 2223 tree id = get_identifier (mangle_decl_string (decl)); 2224 2225 SET_DECL_ASSEMBLER_NAME (decl, id); 2226} 2227 2228/* Generate the mangled representation of TYPE. */ 2229 2230const char * 2231mangle_type_string (type) 2232 tree type; 2233{ 2234 const char *result; 2235 2236 start_mangling (); 2237 write_type (type); 2238 result = finish_mangling (); 2239 if (DEBUG_MANGLE) 2240 fprintf (stderr, "mangle_type_string = '%s'\n\n", result); 2241 return result; 2242} 2243 2244/* Create an identifier for the mangled representation of TYPE. */ 2245 2246tree 2247mangle_type (type) 2248 tree type; 2249{ 2250 return get_identifier (mangle_type_string (type)); 2251} 2252 2253/* Create an identifier for the mangled name of a special component 2254 for belonging to TYPE. CODE is the ABI-specified code for this 2255 component. */ 2256 2257static tree 2258mangle_special_for_type (type, code) 2259 tree type; 2260 const char *code; 2261{ 2262 const char *result; 2263 2264 /* We don't have an actual decl here for the special component, so 2265 we can't just process the <encoded-name>. Instead, fake it. */ 2266 start_mangling (); 2267 2268 /* Start the mangling. */ 2269 write_string ("_Z"); 2270 write_string (code); 2271 2272 /* Add the type. */ 2273 write_type (type); 2274 result = finish_mangling (); 2275 2276 if (DEBUG_MANGLE) 2277 fprintf (stderr, "mangle_special_for_type = %s\n\n", result); 2278 2279 return get_identifier (result); 2280} 2281 2282/* Create an identifier for the mangled representation of the typeinfo 2283 structure for TYPE. */ 2284 2285tree 2286mangle_typeinfo_for_type (type) 2287 tree type; 2288{ 2289 return mangle_special_for_type (type, "TI"); 2290} 2291 2292/* Create an identifier for the mangled name of the NTBS containing 2293 the mangled name of TYPE. */ 2294 2295tree 2296mangle_typeinfo_string_for_type (type) 2297 tree type; 2298{ 2299 return mangle_special_for_type (type, "TS"); 2300} 2301 2302/* Create an identifier for the mangled name of the vtable for TYPE. */ 2303 2304tree 2305mangle_vtbl_for_type (type) 2306 tree type; 2307{ 2308 return mangle_special_for_type (type, "TV"); 2309} 2310 2311/* Returns an identifier for the mangled name of the VTT for TYPE. */ 2312 2313tree 2314mangle_vtt_for_type (type) 2315 tree type; 2316{ 2317 return mangle_special_for_type (type, "TT"); 2318} 2319 2320/* Return an identifier for a construction vtable group. TYPE is 2321 the most derived class in the hierarchy; BINFO is the base 2322 subobject for which this construction vtable group will be used. 2323 2324 This mangling isn't part of the ABI specification; in the ABI 2325 specification, the vtable group is dumped in the same COMDAT as the 2326 main vtable, and is referenced only from that vtable, so it doesn't 2327 need an external name. For binary formats without COMDAT sections, 2328 though, we need external names for the vtable groups. 2329 2330 We use the production 2331 2332 <special-name> ::= CT <type> <offset number> _ <base type> */ 2333 2334tree 2335mangle_ctor_vtbl_for_type (type, binfo) 2336 tree type; 2337 tree binfo; 2338{ 2339 const char *result; 2340 2341 start_mangling (); 2342 2343 write_string ("_Z"); 2344 write_string ("TC"); 2345 write_type (type); 2346 write_integer_cst (BINFO_OFFSET (binfo)); 2347 write_char ('_'); 2348 write_type (BINFO_TYPE (binfo)); 2349 2350 result = finish_mangling (); 2351 if (DEBUG_MANGLE) 2352 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result); 2353 return get_identifier (result); 2354} 2355 2356/* Return an identifier for the mangled name of a thunk to FN_DECL. 2357 OFFSET is the initial adjustment to this used to find the vptr. If 2358 VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the 2359 vtbl offset in bytes. 2360 2361 <special-name> ::= Th <offset number> _ <base encoding> 2362 ::= Tv <offset number> _ <vcall offset number> _ 2363 <base encoding> 2364*/ 2365 2366tree 2367mangle_thunk (fn_decl, offset, vcall_offset) 2368 tree fn_decl; 2369 tree offset; 2370 tree vcall_offset; 2371{ 2372 const char *result; 2373 2374 start_mangling (); 2375 2376 write_string ("_Z"); 2377 /* The <special-name> for virtual thunks is Tv, for non-virtual 2378 thunks Th. */ 2379 write_char ('T'); 2380 if (vcall_offset != 0) 2381 write_char ('v'); 2382 else 2383 write_char ('h'); 2384 2385 /* For either flavor, write the offset to this. */ 2386 write_integer_cst (offset); 2387 write_char ('_'); 2388 2389 /* For a virtual thunk, add the vcall offset. */ 2390 if (vcall_offset) 2391 { 2392 /* Virtual thunk. Write the vcall offset and base type name. */ 2393 write_integer_cst (vcall_offset); 2394 write_char ('_'); 2395 } 2396 2397 /* Scoped name. */ 2398 write_encoding (fn_decl); 2399 2400 result = finish_mangling (); 2401 if (DEBUG_MANGLE) 2402 fprintf (stderr, "mangle_thunk = %s\n\n", result); 2403 return get_identifier (result); 2404} 2405 2406/* Return an identifier for the mangled unqualified name for a 2407 conversion operator to TYPE. This mangling is not specified by the 2408 ABI spec; it is only used internally. */ 2409 2410tree 2411mangle_conv_op_name_for_type (type) 2412 tree type; 2413{ 2414 tree identifier; 2415 2416 /* Build the mangling for TYPE. */ 2417 const char *mangled_type = mangle_type_string (type); 2418 /* Allocate a temporary buffer for the complete name. */ 2419 char *op_name = concat ("operator ", mangled_type, NULL); 2420 /* Find or create an identifier. */ 2421 identifier = get_identifier (op_name); 2422 /* Done with the temporary buffer. */ 2423 free (op_name); 2424 /* Set bits on the identifier so we know later it's a conversion. */ 2425 IDENTIFIER_OPNAME_P (identifier) = 1; 2426 IDENTIFIER_TYPENAME_P (identifier) = 1; 2427 /* Hang TYPE off the identifier so it can be found easily later when 2428 performing conversions. */ 2429 TREE_TYPE (identifier) = type; 2430 2431 return identifier; 2432} 2433 2434/* Return an identifier for the name of an initialization guard 2435 variable for indicated VARIABLE. */ 2436 2437tree 2438mangle_guard_variable (variable) 2439 tree variable; 2440{ 2441 start_mangling (); 2442 write_string ("_ZGV"); 2443 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0) 2444 /* The name of a guard variable for a reference temporary should refer 2445 to the reference, not the temporary. */ 2446 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4); 2447 else 2448 write_name (variable, /*ignore_local_scope=*/0); 2449 return get_identifier (finish_mangling ()); 2450} 2451 2452/* Return an identifier for the name of a temporary variable used to 2453 initialize a static reference. This isn't part of the ABI, but we might 2454 as well call them something readable. */ 2455 2456tree 2457mangle_ref_init_variable (variable) 2458 tree variable; 2459{ 2460 start_mangling (); 2461 write_string ("_ZGR"); 2462 write_name (variable, /*ignore_local_scope=*/0); 2463 return get_identifier (finish_mangling ()); 2464} 2465 2466 2467/* Foreign language type mangling section. */ 2468 2469/* How to write the type codes for the integer Java type. */ 2470 2471static void 2472write_java_integer_type_codes (type) 2473 tree type; 2474{ 2475 if (type == java_int_type_node) 2476 write_char ('i'); 2477 else if (type == java_short_type_node) 2478 write_char ('s'); 2479 else if (type == java_byte_type_node) 2480 write_char ('c'); 2481 else if (type == java_char_type_node) 2482 write_char ('w'); 2483 else if (type == java_long_type_node) 2484 write_char ('x'); 2485 else if (type == java_boolean_type_node) 2486 write_char ('b'); 2487 else 2488 abort (); 2489} 2490 2491