1 1.1 mrg /* Name mangling for the 3.0 -*- C++ -*- ABI. 2 1.1 mrg Copyright (C) 2000-2022 Free Software Foundation, Inc. 3 1.1 mrg Written by Alex Samuel <samuel (at) codesourcery.com> 4 1.1 mrg 5 1.1 mrg This file is part of GCC. 6 1.1 mrg 7 1.1 mrg GCC is free software; you can redistribute it and/or modify it 8 1.1 mrg under the terms of the GNU General Public License as published by 9 1.1 mrg the Free Software Foundation; either version 3, or (at your option) 10 1.1 mrg any later version. 11 1.1 mrg 12 1.1 mrg GCC is distributed in the hope that it will be useful, but 13 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 1.1 mrg General Public License for more details. 16 1.1 mrg 17 1.1 mrg You should have received a copy of the GNU General Public License 18 1.1 mrg along with GCC; see the file COPYING3. If not see 19 1.1 mrg <http://www.gnu.org/licenses/>. */ 20 1.1 mrg 21 1.1 mrg /* This file implements mangling of C++ names according to the IA64 22 1.1 mrg C++ ABI specification. A mangled name encodes a function or 23 1.1 mrg variable's name, scope, type, and/or template arguments into a text 24 1.1 mrg identifier. This identifier is used as the function's or 25 1.1 mrg variable's linkage name, to preserve compatibility between C++'s 26 1.1 mrg language features (templates, scoping, and overloading) and C 27 1.1 mrg linkers. 28 1.1 mrg 29 1.1 mrg Additionally, g++ uses mangled names internally. To support this, 30 1.1 mrg mangling of types is allowed, even though the mangled name of a 31 1.1 mrg type should not appear by itself as an exported name. Ditto for 32 1.1 mrg uninstantiated templates. 33 1.1 mrg 34 1.1 mrg The primary entry point for this module is mangle_decl, which 35 1.1 mrg returns an identifier containing the mangled name for a decl. 36 1.1 mrg Additional entry points are provided to build mangled names of 37 1.1 mrg particular constructs when the appropriate decl for that construct 38 1.1 mrg is not available. These are: 39 1.1 mrg 40 1.1 mrg mangle_typeinfo_for_type: typeinfo data 41 1.1 mrg mangle_typeinfo_string_for_type: typeinfo type name 42 1.1 mrg mangle_vtbl_for_type: virtual table data 43 1.1 mrg mangle_vtt_for_type: VTT data 44 1.1 mrg mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data 45 1.1 mrg mangle_thunk: thunk function or entry */ 46 1.1 mrg 47 1.1 mrg #include "config.h" 48 1.1 mrg #include "system.h" 49 1.1 mrg #include "coretypes.h" 50 1.1 mrg #include "target.h" 51 1.1 mrg #include "vtable-verify.h" 52 1.1 mrg #include "cp-tree.h" 53 1.1 mrg #include "stringpool.h" 54 1.1 mrg #include "cgraph.h" 55 1.1 mrg #include "stor-layout.h" 56 1.1 mrg #include "flags.h" 57 1.1 mrg #include "attribs.h" 58 1.1 mrg 59 1.1 mrg /* Debugging support. */ 60 1.1 mrg 61 1.1 mrg /* Define DEBUG_MANGLE to enable very verbose trace messages. */ 62 1.1 mrg #ifndef DEBUG_MANGLE 63 1.1 mrg #define DEBUG_MANGLE 0 64 1.1 mrg #endif 65 1.1 mrg 66 1.1 mrg /* Macros for tracing the write_* functions. */ 67 1.1 mrg #if DEBUG_MANGLE 68 1.1 mrg # define MANGLE_TRACE(FN, INPUT) \ 69 1.1 mrg fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT)) 70 1.1 mrg # define MANGLE_TRACE_TREE(FN, NODE) \ 71 1.1 mrg fprintf (stderr, " %-24s: %-24s (%p)\n", \ 72 1.1 mrg (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE)) 73 1.1 mrg #else 74 1.1 mrg # define MANGLE_TRACE(FN, INPUT) 75 1.1 mrg # define MANGLE_TRACE_TREE(FN, NODE) 76 1.1 mrg #endif 77 1.1 mrg 78 1.1 mrg /* Nonzero if NODE is a class template-id. We can't rely on 79 1.1 mrg CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser 80 1.1 mrg that hard to distinguish A<T> from A, where A<T> is the type as 81 1.1 mrg instantiated outside of the template, and A is the type used 82 1.1 mrg without parameters inside the template. */ 83 1.1 mrg #define CLASSTYPE_TEMPLATE_ID_P(NODE) \ 84 1.1 mrg (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \ 85 1.1 mrg || (CLASS_TYPE_P (NODE) \ 86 1.1 mrg && CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \ 87 1.1 mrg && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE)))) 88 1.1 mrg 89 1.1 mrg /* For deciding whether to set G.need_abi_warning, we need to consider both 90 1.1 mrg warn_abi_version and flag_abi_compat_version. */ 91 1.1 mrg #define abi_warn_or_compat_version_crosses(N) \ 92 1.1 mrg (abi_version_crosses (N) || abi_compat_version_crosses (N)) 93 1.1 mrg 94 1.1 mrg /* And sometimes we can simplify the code path if we don't need to worry about 95 1.1 mrg previous ABIs. */ 96 1.1 mrg #define abi_flag_at_least(flag,N) (flag == 0 || flag >= N) 97 1.1 mrg #define any_abi_below(N) \ 98 1.1 mrg (!abi_version_at_least (N) \ 99 1.1 mrg || !abi_flag_at_least (warn_abi_version, (N)) \ 100 1.1 mrg || !abi_flag_at_least (flag_abi_compat_version, (N))) 101 1.1 mrg 102 1.1 mrg /* Things we only need one of. This module is not reentrant. */ 103 1.1 mrg struct GTY(()) globals { 104 1.1 mrg /* An array of the current substitution candidates, in the order 105 1.1 mrg we've seen them. Contains NULLS, which correspond to module 106 1.1 mrg substitutions. */ 107 1.1 mrg vec<tree, va_gc> *substitutions; 108 1.1 mrg 109 1.1 mrg /* The entity that is being mangled. */ 110 1.1 mrg tree GTY ((skip)) entity; 111 1.1 mrg 112 1.1 mrg /* How many parameter scopes we are inside. */ 113 1.1 mrg int parm_depth; 114 1.1 mrg 115 1.1 mrg /* True if the mangling will be different in a future version of the 116 1.1 mrg ABI. */ 117 1.1 mrg bool need_abi_warning; 118 1.1 mrg 119 1.1 mrg /* True if the mangling will be different in C++17 mode. */ 120 1.1 mrg bool need_cxx17_warning; 121 1.1 mrg 122 1.1 mrg /* True if we mangled a module name. */ 123 1.1 mrg bool mod; 124 1.1 mrg }; 125 1.1 mrg 126 1.1 mrg static GTY (()) globals G; 127 1.1 mrg 128 1.1 mrg /* The obstack on which we build mangled names. */ 129 1.1 mrg static struct obstack *mangle_obstack; 130 1.1 mrg 131 1.1 mrg /* The obstack on which we build mangled names that are not going to 132 1.1 mrg be IDENTIFIER_NODEs. */ 133 1.1 mrg static struct obstack name_obstack; 134 1.1 mrg 135 1.1 mrg /* The first object on the name_obstack; we use this to free memory 136 1.1 mrg allocated on the name_obstack. */ 137 1.1 mrg static void *name_base; 138 1.1 mrg 139 1.1 mrg /* Indices into subst_identifiers. These are identifiers used in 140 1.1 mrg special substitution rules. */ 141 1.1 mrg typedef enum 142 1.1 mrg { 143 1.1 mrg SUBID_ALLOCATOR, 144 1.1 mrg SUBID_BASIC_STRING, 145 1.1 mrg SUBID_CHAR_TRAITS, 146 1.1 mrg SUBID_BASIC_ISTREAM, 147 1.1 mrg SUBID_BASIC_OSTREAM, 148 1.1 mrg SUBID_BASIC_IOSTREAM, 149 1.1 mrg SUBID_MAX 150 1.1 mrg } 151 1.1 mrg substitution_identifier_index_t; 152 1.1 mrg 153 1.1 mrg /* For quick substitution checks, look up these common identifiers 154 1.1 mrg once only. */ 155 1.1 mrg static GTY(()) tree subst_identifiers[SUBID_MAX]; 156 1.1 mrg 157 1.1 mrg /* Single-letter codes for builtin integer types, defined in 158 1.1 mrg <builtin-type>. These are indexed by integer_type_kind values. */ 159 1.1 mrg static const char 160 1.1 mrg integer_type_codes[itk_none] = 161 1.1 mrg { 162 1.1 mrg 'c', /* itk_char */ 163 1.1 mrg 'a', /* itk_signed_char */ 164 1.1 mrg 'h', /* itk_unsigned_char */ 165 1.1 mrg 's', /* itk_short */ 166 1.1 mrg 't', /* itk_unsigned_short */ 167 1.1 mrg 'i', /* itk_int */ 168 1.1 mrg 'j', /* itk_unsigned_int */ 169 1.1 mrg 'l', /* itk_long */ 170 1.1 mrg 'm', /* itk_unsigned_long */ 171 1.1 mrg 'x', /* itk_long_long */ 172 1.1 mrg 'y', /* itk_unsigned_long_long */ 173 1.1 mrg /* __intN types are handled separately */ 174 1.1 mrg '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' 175 1.1 mrg }; 176 1.1 mrg 177 1.1 mrg static tree maybe_template_info (const tree); 178 1.1 mrg 179 1.1 mrg /* Functions for handling substitutions. */ 180 1.1 mrg 181 1.1 mrg static inline tree canonicalize_for_substitution (tree); 182 1.1 mrg static void add_substitution (tree); 183 1.1 mrg static inline bool is_std_substitution (const tree, 184 1.1 mrg const substitution_identifier_index_t); 185 1.1 mrg static inline bool is_std_substitution_char (const tree, 186 1.1 mrg const substitution_identifier_index_t); 187 1.1 mrg static int find_substitution (tree); 188 1.1 mrg static void mangle_call_offset (const tree, const tree); 189 1.1 mrg 190 1.1 mrg /* Functions for emitting mangled representations of things. */ 191 1.1 mrg 192 1.1 mrg static void write_mangled_name (const tree, bool); 193 1.1 mrg static void write_encoding (const tree); 194 1.1 mrg static void write_name (tree, const int); 195 1.1 mrg static void write_abi_tags (tree); 196 1.1 mrg static void write_unscoped_name (const tree); 197 1.1 mrg static void write_unscoped_template_name (const tree); 198 1.1 mrg static void write_nested_name (const tree); 199 1.1 mrg static void write_prefix (const tree); 200 1.1 mrg static void write_template_prefix (const tree); 201 1.1 mrg static void write_unqualified_name (tree); 202 1.1 mrg static void write_conversion_operator_name (const tree); 203 1.1 mrg static void write_source_name (tree); 204 1.1 mrg static void write_literal_operator_name (tree); 205 1.1 mrg static void write_unnamed_type_name (const tree); 206 1.1 mrg static void write_closure_type_name (const tree); 207 1.1 mrg static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *, 208 1.1 mrg const unsigned int); 209 1.1 mrg static void write_number (unsigned HOST_WIDE_INT, const int, 210 1.1 mrg const unsigned int); 211 1.1 mrg static void write_compact_number (int num); 212 1.1 mrg static void write_integer_cst (const tree); 213 1.1 mrg static void write_real_cst (const tree); 214 1.1 mrg static void write_identifier (const char *); 215 1.1 mrg static void write_special_name_constructor (const tree); 216 1.1 mrg static void write_special_name_destructor (const tree); 217 1.1 mrg static void write_type (tree); 218 1.1 mrg static int write_CV_qualifiers_for_type (const tree); 219 1.1 mrg static void write_builtin_type (tree); 220 1.1 mrg static void write_function_type (const tree); 221 1.1 mrg static void write_bare_function_type (const tree, const int, const tree); 222 1.1 mrg static void write_method_parms (tree, const int, const tree); 223 1.1 mrg static void write_class_enum_type (const tree); 224 1.1 mrg static void write_template_args (tree); 225 1.1 mrg static void write_expression (tree); 226 1.1 mrg static void write_template_arg_literal (const tree); 227 1.1 mrg static void write_template_arg (tree); 228 1.1 mrg static void write_template_template_arg (const tree); 229 1.1 mrg static void write_array_type (const tree); 230 1.1 mrg static void write_pointer_to_member_type (const tree); 231 1.1 mrg static void write_template_param (const tree); 232 1.1 mrg static void write_template_template_param (const tree); 233 1.1 mrg static void write_substitution (const int); 234 1.1 mrg static int discriminator_for_local_entity (tree); 235 1.1 mrg static int discriminator_for_string_literal (tree, tree); 236 1.1 mrg static void write_discriminator (const int); 237 1.1 mrg static void write_local_name (tree, const tree, const tree); 238 1.1 mrg static void dump_substitution_candidates (void); 239 1.1 mrg static tree mangle_decl_string (const tree); 240 1.1 mrg static void maybe_check_abi_tags (tree, tree = NULL_TREE, int = 10); 241 1.1 mrg static bool equal_abi_tags (tree, tree); 242 1.1 mrg 243 1.1 mrg /* Control functions. */ 244 1.1 mrg 245 1.1 mrg static inline void start_mangling (const tree); 246 1.1 mrg static tree mangle_special_for_type (const tree, const char *); 247 1.1 mrg 248 1.1 mrg /* Append a single character to the end of the mangled 249 1.1 mrg representation. */ 250 1.1 mrg #define write_char(CHAR) \ 251 1.1 mrg obstack_1grow (mangle_obstack, (CHAR)) 252 1.1 mrg 253 1.1 mrg /* Append a sized buffer to the end of the mangled representation. */ 254 1.1 mrg #define write_chars(CHAR, LEN) \ 255 1.1 mrg obstack_grow (mangle_obstack, (CHAR), (LEN)) 256 1.1 mrg 257 1.1 mrg /* Append a NUL-terminated string to the end of the mangled 258 1.1 mrg representation. */ 259 1.1 mrg #define write_string(STRING) \ 260 1.1 mrg obstack_grow (mangle_obstack, (STRING), strlen (STRING)) 261 1.1 mrg 262 1.1 mrg /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the 263 1.1 mrg same purpose (context, which may be a type) and value (template 264 1.1 mrg decl). See write_template_prefix for more information on what this 265 1.1 mrg is used for. */ 266 1.1 mrg #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \ 267 1.1 mrg (TREE_CODE (NODE1) == TREE_LIST \ 268 1.1 mrg && TREE_CODE (NODE2) == TREE_LIST \ 269 1.1 mrg && ((TYPE_P (TREE_PURPOSE (NODE1)) \ 270 1.1 mrg && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \ 271 1.1 mrg || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \ 272 1.1 mrg && TREE_VALUE (NODE1) == TREE_VALUE (NODE2)) 273 1.1 mrg 274 1.1 mrg /* Write out an unsigned quantity in base 10. */ 275 1.1 mrg #define write_unsigned_number(NUMBER) \ 276 1.1 mrg write_number ((NUMBER), /*unsigned_p=*/1, 10) 277 1.1 mrg 278 1.1 mrg /* If DECL is a template instance (including the uninstantiated template 279 1.1 mrg itself), return its TEMPLATE_INFO. Otherwise return NULL. */ 280 1.1 mrg 281 1.1 mrg static tree 282 1.1 mrg maybe_template_info (const tree decl) 283 1.1 mrg { 284 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL) 285 1.1 mrg { 286 1.1 mrg /* TYPE_DECLs are handled specially. Look at its type to decide 287 1.1 mrg if this is a template instantiation. */ 288 1.1 mrg const tree type = TREE_TYPE (decl); 289 1.1 mrg 290 1.1 mrg if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type)) 291 1.1 mrg return TYPE_TEMPLATE_INFO (type); 292 1.1 mrg } 293 1.1 mrg else 294 1.1 mrg { 295 1.1 mrg /* Check if the template is a primary template. */ 296 1.1 mrg if (DECL_LANG_SPECIFIC (decl) != NULL 297 1.1 mrg && VAR_OR_FUNCTION_DECL_P (decl) 298 1.1 mrg && DECL_TEMPLATE_INFO (decl) 299 1.1 mrg && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) 300 1.1 mrg return DECL_TEMPLATE_INFO (decl); 301 1.1 mrg } 302 1.1 mrg 303 1.1 mrg /* It's not a template id. */ 304 1.1 mrg return NULL_TREE; 305 1.1 mrg } 306 1.1 mrg 307 1.1 mrg /* Produce debugging output of current substitution candidates. */ 308 1.1 mrg 309 1.1 mrg static void 310 1.1 mrg dump_substitution_candidates (void) 311 1.1 mrg { 312 1.1 mrg unsigned i; 313 1.1 mrg tree el; 314 1.1 mrg 315 1.1 mrg fprintf (stderr, " ++ substitutions "); 316 1.1 mrg FOR_EACH_VEC_ELT (*G.substitutions, i, el) 317 1.1 mrg { 318 1.1 mrg const char *name = "???"; 319 1.1 mrg 320 1.1 mrg if (i > 0) 321 1.1 mrg fprintf (stderr, " "); 322 1.1 mrg if (!el) 323 1.1 mrg name = "module"; 324 1.1 mrg else if (DECL_P (el)) 325 1.1 mrg name = IDENTIFIER_POINTER (DECL_NAME (el)); 326 1.1 mrg else if (TREE_CODE (el) == TREE_LIST) 327 1.1 mrg name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el))); 328 1.1 mrg else if (TYPE_NAME (el)) 329 1.1 mrg name = TYPE_NAME_STRING (el); 330 1.1 mrg fprintf (stderr, " S%d_ = ", i - 1); 331 1.1 mrg if (el) 332 1.1 mrg { 333 1.1 mrg if (TYPE_P (el) && 334 1.1 mrg (CP_TYPE_RESTRICT_P (el) 335 1.1 mrg || CP_TYPE_VOLATILE_P (el) 336 1.1 mrg || CP_TYPE_CONST_P (el))) 337 1.1 mrg fprintf (stderr, "CV-"); 338 1.1 mrg fprintf (stderr, "%s (%s at %p)", 339 1.1 mrg name, get_tree_code_name (TREE_CODE (el)), (void *) el); 340 1.1 mrg } 341 1.1 mrg fprintf (stderr, "\n"); 342 1.1 mrg } 343 1.1 mrg } 344 1.1 mrg 345 1.1 mrg /* <exception-spec> ::= 346 1.1 mrg Do -- non-throwing exception specification 347 1.1 mrg DO <expression> E -- computed (instantiation-dependent) noexcept 348 1.1 mrg Dw <type>* E -- throw (types) */ 349 1.1 mrg 350 1.1 mrg static void 351 1.1 mrg write_exception_spec (tree spec) 352 1.1 mrg { 353 1.1 mrg 354 1.1 mrg if (!spec || spec == noexcept_false_spec) 355 1.1 mrg /* Nothing. */ 356 1.1 mrg return; 357 1.1 mrg 358 1.1 mrg if (!flag_noexcept_type) 359 1.1 mrg { 360 1.1 mrg G.need_cxx17_warning = true; 361 1.1 mrg return; 362 1.1 mrg } 363 1.1 mrg 364 1.1 mrg if (spec == noexcept_true_spec || spec == empty_except_spec) 365 1.1 mrg write_string ("Do"); 366 1.1 mrg else if (tree expr = TREE_PURPOSE (spec)) 367 1.1 mrg { 368 1.1 mrg /* noexcept (expr) */ 369 1.1 mrg gcc_assert (uses_template_parms (expr)); 370 1.1 mrg write_string ("DO"); 371 1.1 mrg write_expression (expr); 372 1.1 mrg write_char ('E'); 373 1.1 mrg } 374 1.1 mrg else 375 1.1 mrg { 376 1.1 mrg /* throw (type-list) */ 377 1.1 mrg write_string ("Dw"); 378 1.1 mrg for (tree t = spec; t; t = TREE_CHAIN (t)) 379 1.1 mrg write_type (TREE_VALUE (t)); 380 1.1 mrg write_char ('E'); 381 1.1 mrg } 382 1.1 mrg } 383 1.1 mrg 384 1.1 mrg /* Both decls and types can be substitution candidates, but sometimes 385 1.1 mrg they refer to the same thing. For instance, a TYPE_DECL and 386 1.1 mrg RECORD_TYPE for the same class refer to the same thing, and should 387 1.1 mrg be treated accordingly in substitutions. This function returns a 388 1.1 mrg canonicalized tree node representing NODE that is used when adding 389 1.1 mrg and substitution candidates and finding matches. */ 390 1.1 mrg 391 1.1 mrg static inline tree 392 1.1 mrg canonicalize_for_substitution (tree node) 393 1.1 mrg { 394 1.1 mrg /* For a TYPE_DECL, use the type instead. */ 395 1.1 mrg if (TREE_CODE (node) == TYPE_DECL) 396 1.1 mrg node = TREE_TYPE (node); 397 1.1 mrg if (TYPE_P (node) 398 1.1 mrg && TYPE_CANONICAL (node) != node 399 1.1 mrg && TYPE_MAIN_VARIANT (node) != node) 400 1.1 mrg { 401 1.1 mrg tree orig = node; 402 1.1 mrg /* Here we want to strip the topmost typedef only. 403 1.1 mrg We need to do that so is_std_substitution can do proper 404 1.1 mrg name matching. */ 405 1.1 mrg if (TREE_CODE (node) == FUNCTION_TYPE) 406 1.1 mrg /* Use build_qualified_type and TYPE_QUALS here to preserve 407 1.1 mrg the old buggy mangling of attribute noreturn with abi<5. */ 408 1.1 mrg node = build_qualified_type (TYPE_MAIN_VARIANT (node), 409 1.1 mrg TYPE_QUALS (node)); 410 1.1 mrg else 411 1.1 mrg node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node), 412 1.1 mrg cp_type_quals (node)); 413 1.1 mrg if (FUNC_OR_METHOD_TYPE_P (node)) 414 1.1 mrg { 415 1.1 mrg node = build_ref_qualified_type (node, type_memfn_rqual (orig)); 416 1.1 mrg tree r = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig)); 417 1.1 mrg if (flag_noexcept_type) 418 1.1 mrg node = build_exception_variant (node, r); 419 1.1 mrg else 420 1.1 mrg /* Set the warning flag if appropriate. */ 421 1.1 mrg write_exception_spec (r); 422 1.1 mrg } 423 1.1 mrg } 424 1.1 mrg return node; 425 1.1 mrg } 426 1.1 mrg 427 1.1 mrg /* Add NODE as a substitution candidate. NODE must not already be on 428 1.1 mrg the list of candidates. */ 429 1.1 mrg 430 1.1 mrg static void 431 1.1 mrg add_substitution (tree node) 432 1.1 mrg { 433 1.1 mrg tree c; 434 1.1 mrg 435 1.1 mrg if (DEBUG_MANGLE) 436 1.1 mrg fprintf (stderr, " ++ add_substitution (%s at %10p)\n", 437 1.1 mrg get_tree_code_name (TREE_CODE (node)), (void *) node); 438 1.1 mrg 439 1.1 mrg /* Get the canonicalized substitution candidate for NODE. */ 440 1.1 mrg c = canonicalize_for_substitution (node); 441 1.1 mrg if (DEBUG_MANGLE && c != node) 442 1.1 mrg fprintf (stderr, " ++ using candidate (%s at %10p)\n", 443 1.1 mrg get_tree_code_name (TREE_CODE (node)), (void *) node); 444 1.1 mrg node = c; 445 1.1 mrg 446 1.1 mrg /* Make sure NODE isn't already a candidate. */ 447 1.1 mrg if (flag_checking) 448 1.1 mrg { 449 1.1 mrg int i; 450 1.1 mrg tree candidate; 451 1.1 mrg 452 1.1 mrg FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate) 453 1.1 mrg if (candidate) 454 1.1 mrg { 455 1.1 mrg gcc_assert (!(DECL_P (node) && node == candidate)); 456 1.1 mrg gcc_assert (!(TYPE_P (node) && TYPE_P (candidate) 457 1.1 mrg && same_type_p (node, candidate))); 458 1.1 mrg } 459 1.1 mrg } 460 1.1 mrg 461 1.1 mrg /* Put the decl onto the varray of substitution candidates. */ 462 1.1 mrg vec_safe_push (G.substitutions, node); 463 1.1 mrg 464 1.1 mrg if (DEBUG_MANGLE) 465 1.1 mrg dump_substitution_candidates (); 466 1.1 mrg } 467 1.1 mrg 468 1.1 mrg /* Helper function for find_substitution. Returns nonzero if NODE, 469 1.1 mrg which may be a decl or a CLASS_TYPE, is a template-id with template 470 1.1 mrg name of substitution_index[INDEX] in the ::std namespace, with 471 1.1 mrg global module attachment. */ 472 1.1 mrg 473 1.1 mrg static bool 474 1.1 mrg is_std_substitution (const tree node, 475 1.1 mrg const substitution_identifier_index_t index) 476 1.1 mrg { 477 1.1 mrg tree type = NULL; 478 1.1 mrg tree decl = NULL; 479 1.1 mrg 480 1.1 mrg if (DECL_P (node)) 481 1.1 mrg { 482 1.1 mrg type = TREE_TYPE (node); 483 1.1 mrg decl = node; 484 1.1 mrg } 485 1.1 mrg else if (CLASS_TYPE_P (node)) 486 1.1 mrg { 487 1.1 mrg type = node; 488 1.1 mrg decl = TYPE_NAME (node); 489 1.1 mrg } 490 1.1 mrg else 491 1.1 mrg /* These are not the droids you're looking for. */ 492 1.1 mrg return false; 493 1.1 mrg 494 1.1 mrg if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))) 495 1.1 mrg return false; 496 1.1 mrg 497 1.1 mrg if (!(TYPE_LANG_SPECIFIC (type) && TYPE_TEMPLATE_INFO (type))) 498 1.1 mrg return false; 499 1.1 mrg 500 1.1 mrg tree tmpl = TYPE_TI_TEMPLATE (type); 501 1.1 mrg if (DECL_NAME (tmpl) != subst_identifiers[index]) 502 1.1 mrg return false; 503 1.1 mrg 504 1.1 mrg if (modules_p () && get_originating_module (tmpl, true) >= 0) 505 1.1 mrg return false; 506 1.1 mrg 507 1.1 mrg return true; 508 1.1 mrg } 509 1.1 mrg 510 1.1 mrg /* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T, 511 1.1 mrg which can be a decl or type. */ 512 1.1 mrg 513 1.1 mrg static tree 514 1.1 mrg get_abi_tags (tree t) 515 1.1 mrg { 516 1.1 mrg if (!t || TREE_CODE (t) == NAMESPACE_DECL) 517 1.1 mrg return NULL_TREE; 518 1.1 mrg 519 1.1 mrg if (DECL_P (t) && DECL_DECLARES_TYPE_P (t)) 520 1.1 mrg t = TREE_TYPE (t); 521 1.1 mrg 522 1.1 mrg tree attrs; 523 1.1 mrg if (TYPE_P (t)) 524 1.1 mrg attrs = TYPE_ATTRIBUTES (t); 525 1.1 mrg else 526 1.1 mrg attrs = DECL_ATTRIBUTES (t); 527 1.1 mrg 528 1.1 mrg tree tags = lookup_attribute ("abi_tag", attrs); 529 1.1 mrg if (tags) 530 1.1 mrg tags = TREE_VALUE (tags); 531 1.1 mrg return tags; 532 1.1 mrg } 533 1.1 mrg 534 1.1 mrg /* Helper function for find_substitution. Returns nonzero if NODE, 535 1.1 mrg which may be a decl or a CLASS_TYPE, is the template-id 536 1.1 mrg ::std::identifier<char>, where identifier is 537 1.1 mrg substitution_index[INDEX]. */ 538 1.1 mrg 539 1.1 mrg static bool 540 1.1 mrg is_std_substitution_char (const tree node, 541 1.1 mrg const substitution_identifier_index_t index) 542 1.1 mrg { 543 1.1 mrg tree args; 544 1.1 mrg /* Check NODE's name is ::std::identifier. */ 545 1.1 mrg if (!is_std_substitution (node, index)) 546 1.1 mrg return 0; 547 1.1 mrg /* Figure out its template args. */ 548 1.1 mrg if (DECL_P (node)) 549 1.1 mrg args = DECL_TI_ARGS (node); 550 1.1 mrg else if (CLASS_TYPE_P (node)) 551 1.1 mrg args = CLASSTYPE_TI_ARGS (node); 552 1.1 mrg else 553 1.1 mrg /* Oops, not a template. */ 554 1.1 mrg return 0; 555 1.1 mrg /* NODE's template arg list should be <char>. */ 556 1.1 mrg return 557 1.1 mrg TREE_VEC_LENGTH (args) == 1 558 1.1 mrg && TREE_VEC_ELT (args, 0) == char_type_node; 559 1.1 mrg } 560 1.1 mrg 561 1.1 mrg /* Check whether a substitution should be used to represent NODE in 562 1.1 mrg the mangling. 563 1.1 mrg 564 1.1 mrg First, check standard special-case substitutions. 565 1.1 mrg 566 1.1 mrg <substitution> ::= St 567 1.1 mrg # ::std 568 1.1 mrg 569 1.1 mrg ::= Sa 570 1.1 mrg # ::std::allocator 571 1.1 mrg 572 1.1 mrg ::= Sb 573 1.1 mrg # ::std::basic_string 574 1.1 mrg 575 1.1 mrg ::= Ss 576 1.1 mrg # ::std::basic_string<char, 577 1.1 mrg ::std::char_traits<char>, 578 1.1 mrg ::std::allocator<char> > 579 1.1 mrg 580 1.1 mrg ::= Si 581 1.1 mrg # ::std::basic_istream<char, ::std::char_traits<char> > 582 1.1 mrg 583 1.1 mrg ::= So 584 1.1 mrg # ::std::basic_ostream<char, ::std::char_traits<char> > 585 1.1 mrg 586 1.1 mrg ::= Sd 587 1.1 mrg # ::std::basic_iostream<char, ::std::char_traits<char> > 588 1.1 mrg 589 1.1 mrg Then examine the stack of currently available substitution 590 1.1 mrg candidates for entities appearing earlier in the same mangling 591 1.1 mrg 592 1.1 mrg If a substitution is found, write its mangled representation and 593 1.1 mrg return nonzero. If none is found, just return zero. */ 594 1.1 mrg 595 1.1 mrg static int 596 1.1 mrg find_substitution (tree node) 597 1.1 mrg { 598 1.1 mrg int i; 599 1.1 mrg const int size = vec_safe_length (G.substitutions); 600 1.1 mrg tree decl; 601 1.1 mrg tree type; 602 1.1 mrg const char *abbr = NULL; 603 1.1 mrg 604 1.1 mrg if (DEBUG_MANGLE) 605 1.1 mrg fprintf (stderr, " ++ find_substitution (%s at %p)\n", 606 1.1 mrg get_tree_code_name (TREE_CODE (node)), (void *) node); 607 1.1 mrg 608 1.1 mrg /* Obtain the canonicalized substitution representation for NODE. 609 1.1 mrg This is what we'll compare against. */ 610 1.1 mrg node = canonicalize_for_substitution (node); 611 1.1 mrg 612 1.1 mrg /* Check for builtin substitutions. */ 613 1.1 mrg 614 1.1 mrg decl = TYPE_P (node) ? TYPE_NAME (node) : node; 615 1.1 mrg type = TYPE_P (node) ? node : TREE_TYPE (node); 616 1.1 mrg 617 1.1 mrg /* Check for std::allocator. */ 618 1.1 mrg if (decl 619 1.1 mrg && is_std_substitution (decl, SUBID_ALLOCATOR) 620 1.1 mrg && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl))) 621 1.1 mrg abbr = "Sa"; 622 1.1 mrg 623 1.1 mrg /* Check for std::basic_string. */ 624 1.1 mrg else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING)) 625 1.1 mrg { 626 1.1 mrg if (TYPE_P (node)) 627 1.1 mrg { 628 1.1 mrg /* If this is a type (i.e. a fully-qualified template-id), 629 1.1 mrg check for 630 1.1 mrg std::basic_string <char, 631 1.1 mrg std::char_traits<char>, 632 1.1 mrg std::allocator<char> > . */ 633 1.1 mrg if (cp_type_quals (type) == TYPE_UNQUALIFIED 634 1.1 mrg && CLASSTYPE_USE_TEMPLATE (type)) 635 1.1 mrg { 636 1.1 mrg tree args = CLASSTYPE_TI_ARGS (type); 637 1.1 mrg if (TREE_VEC_LENGTH (args) == 3 638 1.1 mrg && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node) 639 1.1 mrg && is_std_substitution_char (TREE_VEC_ELT (args, 1), 640 1.1 mrg SUBID_CHAR_TRAITS) 641 1.1 mrg && is_std_substitution_char (TREE_VEC_ELT (args, 2), 642 1.1 mrg SUBID_ALLOCATOR)) 643 1.1 mrg abbr = "Ss"; 644 1.1 mrg } 645 1.1 mrg } 646 1.1 mrg else 647 1.1 mrg /* Substitute for the template name only if this isn't a type. */ 648 1.1 mrg abbr = "Sb"; 649 1.1 mrg } 650 1.1 mrg 651 1.1 mrg /* Check for basic_{i,o,io}stream. */ 652 1.1 mrg else if (TYPE_P (node) 653 1.1 mrg && cp_type_quals (type) == TYPE_UNQUALIFIED 654 1.1 mrg && CLASS_TYPE_P (type) 655 1.1 mrg && CLASSTYPE_USE_TEMPLATE (type) 656 1.1 mrg && CLASSTYPE_TEMPLATE_INFO (type) != NULL) 657 1.1 mrg { 658 1.1 mrg /* First, check for the template 659 1.1 mrg args <char, std::char_traits<char> > . */ 660 1.1 mrg tree args = CLASSTYPE_TI_ARGS (type); 661 1.1 mrg if (TREE_VEC_LENGTH (args) == 2 662 1.1 mrg && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node) 663 1.1 mrg && is_std_substitution_char (TREE_VEC_ELT (args, 1), 664 1.1 mrg SUBID_CHAR_TRAITS)) 665 1.1 mrg { 666 1.1 mrg /* Got them. Is this basic_istream? */ 667 1.1 mrg if (is_std_substitution (decl, SUBID_BASIC_ISTREAM)) 668 1.1 mrg abbr = "Si"; 669 1.1 mrg /* Or basic_ostream? */ 670 1.1 mrg else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM)) 671 1.1 mrg abbr = "So"; 672 1.1 mrg /* Or basic_iostream? */ 673 1.1 mrg else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM)) 674 1.1 mrg abbr = "Sd"; 675 1.1 mrg } 676 1.1 mrg } 677 1.1 mrg 678 1.1 mrg /* Check for namespace std. */ 679 1.1 mrg else if (decl && DECL_NAMESPACE_STD_P (decl)) 680 1.1 mrg { 681 1.1 mrg write_string ("St"); 682 1.1 mrg return 1; 683 1.1 mrg } 684 1.1 mrg 685 1.1 mrg tree tags = NULL_TREE; 686 1.1 mrg if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node)) 687 1.1 mrg tags = get_abi_tags (type); 688 1.1 mrg /* Now check the list of available substitutions for this mangling 689 1.1 mrg operation. */ 690 1.1 mrg if (!abbr || tags) 691 1.1 mrg for (i = 0; i < size; ++i) 692 1.1 mrg if (tree candidate = (*G.substitutions)[i]) 693 1.1 mrg { 694 1.1 mrg /* NODE is a matched to a candidate if it's the same decl node or 695 1.1 mrg if it's the same type. */ 696 1.1 mrg if (decl == candidate 697 1.1 mrg || (TYPE_P (candidate) && type && TYPE_P (node) 698 1.1 mrg && same_type_p (type, candidate)) 699 1.1 mrg || NESTED_TEMPLATE_MATCH (node, candidate)) 700 1.1 mrg { 701 1.1 mrg write_substitution (i); 702 1.1 mrg return 1; 703 1.1 mrg } 704 1.1 mrg } 705 1.1 mrg 706 1.1 mrg if (!abbr) 707 1.1 mrg /* No substitution found. */ 708 1.1 mrg return 0; 709 1.1 mrg 710 1.1 mrg write_string (abbr); 711 1.1 mrg if (tags) 712 1.1 mrg { 713 1.1 mrg /* If there are ABI tags on the abbreviation, it becomes 714 1.1 mrg a substitution candidate. */ 715 1.1 mrg write_abi_tags (tags); 716 1.1 mrg add_substitution (node); 717 1.1 mrg } 718 1.1 mrg return 1; 719 1.1 mrg } 720 1.1 mrg 721 1.1 mrg /* Returns whether DECL's symbol name should be the plain unqualified-id 722 1.1 mrg rather than a more complicated mangled name. */ 723 1.1 mrg 724 1.1 mrg static bool 725 1.1 mrg unmangled_name_p (const tree decl) 726 1.1 mrg { 727 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 728 1.1 mrg { 729 1.1 mrg /* The names of `extern "C"' functions are not mangled. */ 730 1.1 mrg return (DECL_EXTERN_C_FUNCTION_P (decl) 731 1.1 mrg /* But overloaded operator names *are* mangled. */ 732 1.1 mrg && !DECL_OVERLOADED_OPERATOR_P (decl)); 733 1.1 mrg } 734 1.1 mrg else if (VAR_P (decl)) 735 1.1 mrg { 736 1.1 mrg /* static variables are mangled. */ 737 1.1 mrg if (!DECL_EXTERNAL_LINKAGE_P (decl)) 738 1.1 mrg return false; 739 1.1 mrg 740 1.1 mrg /* extern "C" declarations aren't mangled. */ 741 1.1 mrg if (DECL_EXTERN_C_P (decl)) 742 1.1 mrg return true; 743 1.1 mrg 744 1.1 mrg /* Other variables at non-global scope are mangled. */ 745 1.1 mrg if (CP_DECL_CONTEXT (decl) != global_namespace) 746 1.1 mrg return false; 747 1.1 mrg 748 1.1 mrg /* Variable template instantiations are mangled. */ 749 1.1 mrg if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) 750 1.1 mrg && variable_template_p (DECL_TI_TEMPLATE (decl))) 751 1.1 mrg return false; 752 1.1 mrg 753 1.1 mrg /* Declarations with ABI tags are mangled. */ 754 1.1 mrg if (get_abi_tags (decl)) 755 1.1 mrg return false; 756 1.1 mrg 757 1.1 mrg // Declarations attached to a named module are mangled 758 1.1 mrg if (modules_p () && get_originating_module (decl, true) >= 0) 759 1.1 mrg return false; 760 1.1 mrg 761 1.1 mrg /* The names of non-static global variables aren't mangled. */ 762 1.1 mrg return true; 763 1.1 mrg } 764 1.1 mrg 765 1.1 mrg return false; 766 1.1 mrg } 767 1.1 mrg 768 1.1 mrg /* TOP_LEVEL is true, if this is being called at outermost level of 769 1.1 mrg mangling. It should be false when mangling a decl appearing in an 770 1.1 mrg expression within some other mangling. 771 1.1 mrg 772 1.1 mrg <mangled-name> ::= _Z <encoding> */ 773 1.1 mrg 774 1.1 mrg static void 775 1.1 mrg write_mangled_name (const tree decl, bool top_level) 776 1.1 mrg { 777 1.1 mrg MANGLE_TRACE_TREE ("mangled-name", decl); 778 1.1 mrg 779 1.1 mrg check_abi_tags (decl); 780 1.1 mrg 781 1.1 mrg if (unmangled_name_p (decl)) 782 1.1 mrg { 783 1.1 mrg if (top_level) 784 1.1 mrg write_string (IDENTIFIER_POINTER (DECL_NAME (decl))); 785 1.1 mrg else 786 1.1 mrg { 787 1.1 mrg /* The standard notes: "The <encoding> of an extern "C" 788 1.1 mrg function is treated like global-scope data, i.e. as its 789 1.1 mrg <source-name> without a type." We cannot write 790 1.1 mrg overloaded operators that way though, because it contains 791 1.1 mrg characters invalid in assembler. */ 792 1.1 mrg write_string ("_Z"); 793 1.1 mrg write_source_name (DECL_NAME (decl)); 794 1.1 mrg } 795 1.1 mrg } 796 1.1 mrg else 797 1.1 mrg { 798 1.1 mrg write_string ("_Z"); 799 1.1 mrg write_encoding (decl); 800 1.1 mrg } 801 1.1 mrg } 802 1.1 mrg 803 1.1 mrg /* Returns true if the return type of DECL is part of its signature, and 804 1.1 mrg therefore its mangling. */ 805 1.1 mrg 806 1.1 mrg bool 807 1.1 mrg mangle_return_type_p (tree decl) 808 1.1 mrg { 809 1.1 mrg return (!DECL_CONSTRUCTOR_P (decl) 810 1.1 mrg && !DECL_DESTRUCTOR_P (decl) 811 1.1 mrg && !DECL_CONV_FN_P (decl) 812 1.1 mrg && maybe_template_info (decl)); 813 1.1 mrg } 814 1.1 mrg 815 1.1 mrg /* <encoding> ::= <function name> <bare-function-type> 816 1.1 mrg ::= <data name> */ 817 1.1 mrg 818 1.1 mrg static void 819 1.1 mrg write_encoding (const tree decl) 820 1.1 mrg { 821 1.1 mrg MANGLE_TRACE_TREE ("encoding", decl); 822 1.1 mrg 823 1.1 mrg if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl)) 824 1.1 mrg { 825 1.1 mrg /* For overloaded operators write just the mangled name 826 1.1 mrg without arguments. */ 827 1.1 mrg if (DECL_OVERLOADED_OPERATOR_P (decl)) 828 1.1 mrg write_name (decl, /*ignore_local_scope=*/0); 829 1.1 mrg else 830 1.1 mrg write_source_name (DECL_NAME (decl)); 831 1.1 mrg return; 832 1.1 mrg } 833 1.1 mrg 834 1.1 mrg write_name (decl, /*ignore_local_scope=*/0); 835 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 836 1.1 mrg { 837 1.1 mrg tree fn_type; 838 1.1 mrg tree d; 839 1.1 mrg 840 1.1 mrg if (maybe_template_info (decl)) 841 1.1 mrg { 842 1.1 mrg fn_type = get_mostly_instantiated_function_type (decl); 843 1.1 mrg /* FN_TYPE will not have parameter types for in-charge or 844 1.1 mrg VTT parameters. Therefore, we pass NULL_TREE to 845 1.1 mrg write_bare_function_type -- otherwise, it will get 846 1.1 mrg confused about which artificial parameters to skip. */ 847 1.1 mrg d = NULL_TREE; 848 1.1 mrg } 849 1.1 mrg else 850 1.1 mrg { 851 1.1 mrg fn_type = TREE_TYPE (decl); 852 1.1 mrg d = decl; 853 1.1 mrg } 854 1.1 mrg 855 1.1 mrg write_bare_function_type (fn_type, 856 1.1 mrg mangle_return_type_p (decl), 857 1.1 mrg d); 858 1.1 mrg 859 1.1 mrg /* If this is a coroutine helper, then append an appropriate string to 860 1.1 mrg identify which. */ 861 1.1 mrg if (tree ramp = DECL_RAMP_FN (decl)) 862 1.1 mrg { 863 1.1 mrg if (DECL_ACTOR_FN (ramp) == decl) 864 1.1 mrg write_string (JOIN_STR "actor"); 865 1.1 mrg else if (DECL_DESTROY_FN (ramp) == decl) 866 1.1 mrg write_string (JOIN_STR "destroy"); 867 1.1 mrg else 868 1.1 mrg gcc_unreachable (); 869 1.1 mrg } 870 1.1 mrg } 871 1.1 mrg } 872 1.1 mrg 873 1.1 mrg /* Interface to substitution and identifier mangling, used by the 874 1.1 mrg module name mangler. */ 875 1.1 mrg 876 1.1 mrg void 877 1.1 mrg mangle_module_substitution (int v) 878 1.1 mrg { 879 1.1 mrg write_substitution (v - 1); 880 1.1 mrg } 881 1.1 mrg 882 1.1 mrg int 883 1.1 mrg mangle_module_component (tree comp, bool partition_p) 884 1.1 mrg { 885 1.1 mrg write_char ('W'); 886 1.1 mrg if (partition_p) 887 1.1 mrg write_char ('P'); 888 1.1 mrg write_source_name (comp); 889 1.1 mrg 890 1.1 mrg // Module substitutions use the same number-space as entity 891 1.1 mrg // substitutions, but are orthogonal. 892 1.1 mrg vec_safe_push (G.substitutions, NULL_TREE); 893 1.1 mrg return G.substitutions->length (); 894 1.1 mrg } 895 1.1 mrg 896 1.1 mrg /* If the outermost non-namespace context (including DECL itself) is 897 1.1 mrg a module-linkage decl, mangle the module information. For module 898 1.1 mrg global initializers we need to include the partition part. 899 1.1 mrg 900 1.1 mrg <module-name> ::= <module-sub> 901 1.1 mrg || <subst> 902 1.1 mrg || <module-name> <module-sub> 903 1.1 mrg <module-sub> :: W [P] <unqualified-name> 904 1.1 mrg */ 905 1.1 mrg 906 1.1 mrg static void 907 1.1 mrg write_module (int m, bool include_partition) 908 1.1 mrg { 909 1.1 mrg G.mod = true; 910 1.1 mrg mangle_module (m, include_partition); 911 1.1 mrg } 912 1.1 mrg 913 1.1 mrg static void 914 1.1 mrg maybe_write_module (tree decl) 915 1.1 mrg { 916 1.1 mrg if (!DECL_NAMESPACE_SCOPE_P (decl)) 917 1.1 mrg return; 918 1.1 mrg 919 1.1 mrg if (TREE_CODE (decl) == NAMESPACE_DECL && DECL_NAME (decl)) 920 1.1 mrg return; 921 1.1 mrg 922 1.1 mrg int m = get_originating_module (decl, true); 923 1.1 mrg if (m >= 0) 924 1.1 mrg write_module (m, false); 925 1.1 mrg } 926 1.1 mrg 927 1.1 mrg /* Lambdas can have a bit more context for mangling, specifically VAR_DECL 928 1.1 mrg or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */ 929 1.1 mrg 930 1.1 mrg static tree 931 1.1 mrg decl_mangling_context (tree decl) 932 1.1 mrg { 933 1.1 mrg tree tcontext = targetm.cxx.decl_mangling_context (decl); 934 1.1 mrg 935 1.1 mrg if (tcontext != NULL_TREE) 936 1.1 mrg return tcontext; 937 1.1 mrg 938 1.1 mrg if (TREE_CODE (decl) == TEMPLATE_DECL 939 1.1 mrg && DECL_TEMPLATE_RESULT (decl)) 940 1.1 mrg decl = DECL_TEMPLATE_RESULT (decl); 941 1.1 mrg 942 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL 943 1.1 mrg && LAMBDA_TYPE_P (TREE_TYPE (decl))) 944 1.1 mrg { 945 1.1 mrg tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)); 946 1.1 mrg if (extra) 947 1.1 mrg return extra; 948 1.1 mrg } 949 1.1 mrg else if (template_type_parameter_p (decl)) 950 1.1 mrg /* template type parms have no mangling context. */ 951 1.1 mrg return NULL_TREE; 952 1.1 mrg 953 1.1 mrg tcontext = CP_DECL_CONTEXT (decl); 954 1.1 mrg 955 1.1 mrg /* Ignore the artificial declare reduction functions. */ 956 1.1 mrg if (tcontext 957 1.1 mrg && TREE_CODE (tcontext) == FUNCTION_DECL 958 1.1 mrg && DECL_OMP_DECLARE_REDUCTION_P (tcontext)) 959 1.1 mrg return decl_mangling_context (tcontext); 960 1.1 mrg 961 1.1 mrg return tcontext; 962 1.1 mrg } 963 1.1 mrg 964 1.1 mrg /* <name> ::= <unscoped-name> 965 1.1 mrg ::= <unscoped-template-name> <template-args> 966 1.1 mrg ::= <nested-name> 967 1.1 mrg ::= <local-name> 968 1.1 mrg 969 1.1 mrg If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is 970 1.1 mrg called from <local-name>, which mangles the enclosing scope 971 1.1 mrg elsewhere and then uses this function to mangle just the part 972 1.1 mrg underneath the function scope. So don't use the <local-name> 973 1.1 mrg production, to avoid an infinite recursion. */ 974 1.1 mrg 975 1.1 mrg static void 976 1.1 mrg write_name (tree decl, const int ignore_local_scope) 977 1.1 mrg { 978 1.1 mrg tree context; 979 1.1 mrg 980 1.1 mrg MANGLE_TRACE_TREE ("name", decl); 981 1.1 mrg 982 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL) 983 1.1 mrg { 984 1.1 mrg /* In case this is a typedef, fish out the corresponding 985 1.1 mrg TYPE_DECL for the main variant. */ 986 1.1 mrg decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))); 987 1.1 mrg } 988 1.1 mrg 989 1.1 mrg context = decl_mangling_context (decl); 990 1.1 mrg 991 1.1 mrg gcc_assert (context != NULL_TREE); 992 1.1 mrg 993 1.1 mrg if (abi_warn_or_compat_version_crosses (7) 994 1.1 mrg && ignore_local_scope 995 1.1 mrg && TREE_CODE (context) == PARM_DECL) 996 1.1 mrg G.need_abi_warning = 1; 997 1.1 mrg 998 1.1 mrg /* A decl in :: or ::std scope is treated specially. The former is 999 1.1 mrg mangled using <unscoped-name> or <unscoped-template-name>, the 1000 1.1 mrg latter with a special substitution. Also, a name that is 1001 1.1 mrg directly in a local function scope is also mangled with 1002 1.1 mrg <unscoped-name> rather than a full <nested-name>. */ 1003 1.1 mrg if (context == global_namespace 1004 1.1 mrg || DECL_NAMESPACE_STD_P (context) 1005 1.1 mrg || (ignore_local_scope 1006 1.1 mrg && (TREE_CODE (context) == FUNCTION_DECL 1007 1.1 mrg || (abi_version_at_least (7) 1008 1.1 mrg && TREE_CODE (context) == PARM_DECL)))) 1009 1.1 mrg { 1010 1.1 mrg /* Is this a template instance? */ 1011 1.1 mrg if (tree info = maybe_template_info (decl)) 1012 1.1 mrg { 1013 1.1 mrg /* Yes: use <unscoped-template-name>. */ 1014 1.1 mrg write_unscoped_template_name (TI_TEMPLATE (info)); 1015 1.1 mrg write_template_args (TI_ARGS (info)); 1016 1.1 mrg } 1017 1.1 mrg else 1018 1.1 mrg /* Everything else gets an <unqualified-name>. */ 1019 1.1 mrg write_unscoped_name (decl); 1020 1.1 mrg } 1021 1.1 mrg else 1022 1.1 mrg { 1023 1.1 mrg /* Handle local names, unless we asked not to (that is, invoked 1024 1.1 mrg under <local-name>, to handle only the part of the name under 1025 1.1 mrg the local scope). */ 1026 1.1 mrg if (!ignore_local_scope) 1027 1.1 mrg { 1028 1.1 mrg /* Scan up the list of scope context, looking for a 1029 1.1 mrg function. If we find one, this entity is in local 1030 1.1 mrg function scope. local_entity tracks context one scope 1031 1.1 mrg level down, so it will contain the element that's 1032 1.1 mrg directly in that function's scope, either decl or one of 1033 1.1 mrg its enclosing scopes. */ 1034 1.1 mrg tree local_entity = decl; 1035 1.1 mrg while (context != global_namespace) 1036 1.1 mrg { 1037 1.1 mrg /* Make sure we're always dealing with decls. */ 1038 1.1 mrg if (TYPE_P (context)) 1039 1.1 mrg context = TYPE_NAME (context); 1040 1.1 mrg /* Is this a function? */ 1041 1.1 mrg if (TREE_CODE (context) == FUNCTION_DECL 1042 1.1 mrg || TREE_CODE (context) == PARM_DECL) 1043 1.1 mrg { 1044 1.1 mrg /* Yes, we have local scope. Use the <local-name> 1045 1.1 mrg production for the innermost function scope. */ 1046 1.1 mrg write_local_name (context, local_entity, decl); 1047 1.1 mrg return; 1048 1.1 mrg } 1049 1.1 mrg /* Up one scope level. */ 1050 1.1 mrg local_entity = context; 1051 1.1 mrg context = decl_mangling_context (context); 1052 1.1 mrg } 1053 1.1 mrg 1054 1.1 mrg /* No local scope found? Fall through to <nested-name>. */ 1055 1.1 mrg } 1056 1.1 mrg 1057 1.1 mrg /* Other decls get a <nested-name> to encode their scope. */ 1058 1.1 mrg write_nested_name (decl); 1059 1.1 mrg } 1060 1.1 mrg } 1061 1.1 mrg 1062 1.1 mrg /* <unscoped-name> ::= <unqualified-name> 1063 1.1 mrg ::= St <unqualified-name> # ::std:: */ 1064 1.1 mrg 1065 1.1 mrg static void 1066 1.1 mrg write_unscoped_name (const tree decl) 1067 1.1 mrg { 1068 1.1 mrg tree context = decl_mangling_context (decl); 1069 1.1 mrg 1070 1.1 mrg MANGLE_TRACE_TREE ("unscoped-name", decl); 1071 1.1 mrg 1072 1.1 mrg /* Is DECL in ::std? */ 1073 1.1 mrg if (DECL_NAMESPACE_STD_P (context)) 1074 1.1 mrg { 1075 1.1 mrg write_string ("St"); 1076 1.1 mrg write_unqualified_name (decl); 1077 1.1 mrg } 1078 1.1 mrg else 1079 1.1 mrg { 1080 1.1 mrg /* If not, it should be either in the global namespace, or directly 1081 1.1 mrg in a local function scope. A lambda can also be mangled in the 1082 1.1 mrg scope of a default argument. */ 1083 1.1 mrg gcc_assert (context == global_namespace 1084 1.1 mrg || TREE_CODE (context) == PARM_DECL 1085 1.1 mrg || TREE_CODE (context) == FUNCTION_DECL); 1086 1.1 mrg 1087 1.1 mrg write_unqualified_name (decl); 1088 1.1 mrg } 1089 1.1 mrg } 1090 1.1 mrg 1091 1.1 mrg /* <unscoped-template-name> ::= <unscoped-name> 1092 1.1 mrg ::= <substitution> */ 1093 1.1 mrg 1094 1.1 mrg static void 1095 1.1 mrg write_unscoped_template_name (const tree decl) 1096 1.1 mrg { 1097 1.1 mrg MANGLE_TRACE_TREE ("unscoped-template-name", decl); 1098 1.1 mrg 1099 1.1 mrg if (find_substitution (decl)) 1100 1.1 mrg return; 1101 1.1 mrg write_unscoped_name (decl); 1102 1.1 mrg add_substitution (decl); 1103 1.1 mrg } 1104 1.1 mrg 1105 1.1 mrg /* Write the nested name, including CV-qualifiers, of DECL. 1106 1.1 mrg 1107 1.1 mrg <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E 1108 1.1 mrg ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E 1109 1.1 mrg 1110 1.1 mrg <ref-qualifier> ::= R # & ref-qualifier 1111 1.1 mrg ::= O # && ref-qualifier 1112 1.1 mrg <CV-qualifiers> ::= [r] [V] [K] */ 1113 1.1 mrg 1114 1.1 mrg static void 1115 1.1 mrg write_nested_name (const tree decl) 1116 1.1 mrg { 1117 1.1 mrg MANGLE_TRACE_TREE ("nested-name", decl); 1118 1.1 mrg 1119 1.1 mrg write_char ('N'); 1120 1.1 mrg 1121 1.1 mrg /* Write CV-qualifiers, if this is a member function. */ 1122 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL 1123 1.1 mrg && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1124 1.1 mrg { 1125 1.1 mrg if (DECL_VOLATILE_MEMFUNC_P (decl)) 1126 1.1 mrg write_char ('V'); 1127 1.1 mrg if (DECL_CONST_MEMFUNC_P (decl)) 1128 1.1 mrg write_char ('K'); 1129 1.1 mrg if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl))) 1130 1.1 mrg { 1131 1.1 mrg if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl))) 1132 1.1 mrg write_char ('O'); 1133 1.1 mrg else 1134 1.1 mrg write_char ('R'); 1135 1.1 mrg } 1136 1.1 mrg } 1137 1.1 mrg 1138 1.1 mrg /* Is this a template instance? */ 1139 1.1 mrg if (tree info = maybe_template_info (decl)) 1140 1.1 mrg { 1141 1.1 mrg /* Yes, use <template-prefix>. */ 1142 1.1 mrg write_template_prefix (decl); 1143 1.1 mrg write_template_args (TI_ARGS (info)); 1144 1.1 mrg } 1145 1.1 mrg else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL) 1146 1.1 mrg && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE) 1147 1.1 mrg { 1148 1.1 mrg tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl)); 1149 1.1 mrg if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 1150 1.1 mrg { 1151 1.1 mrg write_template_prefix (decl); 1152 1.1 mrg write_template_args (TREE_OPERAND (name, 1)); 1153 1.1 mrg } 1154 1.1 mrg else 1155 1.1 mrg { 1156 1.1 mrg write_prefix (decl_mangling_context (decl)); 1157 1.1 mrg write_unqualified_name (decl); 1158 1.1 mrg } 1159 1.1 mrg } 1160 1.1 mrg else 1161 1.1 mrg { 1162 1.1 mrg /* No, just use <prefix> */ 1163 1.1 mrg write_prefix (decl_mangling_context (decl)); 1164 1.1 mrg write_unqualified_name (decl); 1165 1.1 mrg } 1166 1.1 mrg write_char ('E'); 1167 1.1 mrg } 1168 1.1 mrg 1169 1.1 mrg /* <prefix> ::= <prefix> <unqualified-name> 1170 1.1 mrg ::= <template-param> 1171 1.1 mrg ::= <template-prefix> <template-args> 1172 1.1 mrg ::= <decltype> 1173 1.1 mrg ::= # empty 1174 1.1 mrg ::= <substitution> */ 1175 1.1 mrg 1176 1.1 mrg static void 1177 1.1 mrg write_prefix (const tree node) 1178 1.1 mrg { 1179 1.1 mrg tree decl; 1180 1.1 mrg 1181 1.1 mrg if (node == NULL 1182 1.1 mrg || node == global_namespace) 1183 1.1 mrg return; 1184 1.1 mrg 1185 1.1 mrg MANGLE_TRACE_TREE ("prefix", node); 1186 1.1 mrg 1187 1.1 mrg if (TREE_CODE (node) == DECLTYPE_TYPE) 1188 1.1 mrg { 1189 1.1 mrg write_type (node); 1190 1.1 mrg return; 1191 1.1 mrg } 1192 1.1 mrg 1193 1.1 mrg if (find_substitution (node)) 1194 1.1 mrg return; 1195 1.1 mrg 1196 1.1 mrg tree template_info = NULL_TREE; 1197 1.1 mrg if (DECL_P (node)) 1198 1.1 mrg { 1199 1.1 mrg /* If this is a function or parm decl, that means we've hit function 1200 1.1 mrg scope, so this prefix must be for a local name. In this 1201 1.1 mrg case, we're under the <local-name> production, which encodes 1202 1.1 mrg the enclosing function scope elsewhere. So don't continue 1203 1.1 mrg here. */ 1204 1.1 mrg if (TREE_CODE (node) == FUNCTION_DECL 1205 1.1 mrg || TREE_CODE (node) == PARM_DECL) 1206 1.1 mrg return; 1207 1.1 mrg 1208 1.1 mrg decl = node; 1209 1.1 mrg template_info = maybe_template_info (decl); 1210 1.1 mrg } 1211 1.1 mrg else 1212 1.1 mrg { 1213 1.1 mrg /* Node is a type. */ 1214 1.1 mrg decl = TYPE_NAME (node); 1215 1.1 mrg /* The DECL might not point at the node. */ 1216 1.1 mrg if (CLASSTYPE_TEMPLATE_ID_P (node)) 1217 1.1 mrg template_info = TYPE_TEMPLATE_INFO (node); 1218 1.1 mrg } 1219 1.1 mrg 1220 1.1 mrg if (TREE_CODE (node) == TEMPLATE_TYPE_PARM) 1221 1.1 mrg write_template_param (node); 1222 1.1 mrg else if (template_info) 1223 1.1 mrg /* Templated. */ 1224 1.1 mrg { 1225 1.1 mrg write_template_prefix (decl); 1226 1.1 mrg write_template_args (TI_ARGS (template_info)); 1227 1.1 mrg } 1228 1.1 mrg else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE) 1229 1.1 mrg { 1230 1.1 mrg tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl)); 1231 1.1 mrg if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 1232 1.1 mrg { 1233 1.1 mrg write_template_prefix (decl); 1234 1.1 mrg write_template_args (TREE_OPERAND (name, 1)); 1235 1.1 mrg } 1236 1.1 mrg else 1237 1.1 mrg { 1238 1.1 mrg write_prefix (decl_mangling_context (decl)); 1239 1.1 mrg write_unqualified_name (decl); 1240 1.1 mrg } 1241 1.1 mrg } 1242 1.1 mrg else 1243 1.1 mrg /* Not templated. */ 1244 1.1 mrg { 1245 1.1 mrg write_prefix (decl_mangling_context (decl)); 1246 1.1 mrg write_unqualified_name (decl); 1247 1.1 mrg if (VAR_P (decl) 1248 1.1 mrg || TREE_CODE (decl) == FIELD_DECL) 1249 1.1 mrg { 1250 1.1 mrg /* <data-member-prefix> := <member source-name> M */ 1251 1.1 mrg write_char ('M'); 1252 1.1 mrg return; 1253 1.1 mrg } 1254 1.1 mrg } 1255 1.1 mrg 1256 1.1 mrg add_substitution (node); 1257 1.1 mrg } 1258 1.1 mrg 1259 1.1 mrg /* <template-prefix> ::= <prefix> <template component> 1260 1.1 mrg ::= <template-param> 1261 1.1 mrg ::= <substitution> */ 1262 1.1 mrg 1263 1.1 mrg static void 1264 1.1 mrg write_template_prefix (const tree node) 1265 1.1 mrg { 1266 1.1 mrg tree decl = DECL_P (node) ? node : TYPE_NAME (node); 1267 1.1 mrg tree type = DECL_P (node) ? TREE_TYPE (node) : node; 1268 1.1 mrg tree context = decl_mangling_context (decl); 1269 1.1 mrg tree templ; 1270 1.1 mrg tree substitution; 1271 1.1 mrg 1272 1.1 mrg MANGLE_TRACE_TREE ("template-prefix", node); 1273 1.1 mrg 1274 1.1 mrg /* Find the template decl. */ 1275 1.1 mrg if (tree info = maybe_template_info (decl)) 1276 1.1 mrg templ = TI_TEMPLATE (info); 1277 1.1 mrg else if (TREE_CODE (type) == TYPENAME_TYPE) 1278 1.1 mrg /* For a typename type, all we have is the name. */ 1279 1.1 mrg templ = DECL_NAME (decl); 1280 1.1 mrg else 1281 1.1 mrg { 1282 1.1 mrg gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type)); 1283 1.1 mrg 1284 1.1 mrg templ = TYPE_TI_TEMPLATE (type); 1285 1.1 mrg } 1286 1.1 mrg 1287 1.1 mrg /* For a member template, though, the template name for the 1288 1.1 mrg innermost name must have all the outer template levels 1289 1.1 mrg instantiated. For instance, consider 1290 1.1 mrg 1291 1.1 mrg template<typename T> struct Outer { 1292 1.1 mrg template<typename U> struct Inner {}; 1293 1.1 mrg }; 1294 1.1 mrg 1295 1.1 mrg The template name for `Inner' in `Outer<int>::Inner<float>' is 1296 1.1 mrg `Outer<int>::Inner<U>'. In g++, we don't instantiate the template 1297 1.1 mrg levels separately, so there's no TEMPLATE_DECL available for this 1298 1.1 mrg (there's only `Outer<T>::Inner<U>'). 1299 1.1 mrg 1300 1.1 mrg In order to get the substitutions right, we create a special 1301 1.1 mrg TREE_LIST to represent the substitution candidate for a nested 1302 1.1 mrg template. The TREE_PURPOSE is the template's context, fully 1303 1.1 mrg instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner 1304 1.1 mrg template. 1305 1.1 mrg 1306 1.1 mrg So, for the example above, `Outer<int>::Inner' is represented as a 1307 1.1 mrg substitution candidate by a TREE_LIST whose purpose is `Outer<int>' 1308 1.1 mrg and whose value is `Outer<T>::Inner<U>'. */ 1309 1.1 mrg if (context && TYPE_P (context)) 1310 1.1 mrg substitution = build_tree_list (context, templ); 1311 1.1 mrg else 1312 1.1 mrg substitution = templ; 1313 1.1 mrg 1314 1.1 mrg if (find_substitution (substitution)) 1315 1.1 mrg return; 1316 1.1 mrg 1317 1.1 mrg if (TREE_TYPE (templ) 1318 1.1 mrg && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM) 1319 1.1 mrg write_template_param (TREE_TYPE (templ)); 1320 1.1 mrg else 1321 1.1 mrg { 1322 1.1 mrg write_prefix (context); 1323 1.1 mrg write_unqualified_name (decl); 1324 1.1 mrg } 1325 1.1 mrg 1326 1.1 mrg add_substitution (substitution); 1327 1.1 mrg } 1328 1.1 mrg 1329 1.1 mrg /* As the list of identifiers for the structured binding declaration 1330 1.1 mrg DECL is likely gone, try to recover the DC <source-name>+ E portion 1331 1.1 mrg from its mangled name. Return pointer to the DC and set len to 1332 1.1 mrg the length up to and including the terminating E. On failure 1333 1.1 mrg return NULL. */ 1334 1.1 mrg 1335 1.1 mrg static const char * 1336 1.1 mrg find_decomp_unqualified_name (tree decl, size_t *len) 1337 1.1 mrg { 1338 1.1 mrg const char *p = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 1339 1.1 mrg const char *end = p + IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl)); 1340 1.1 mrg bool nested = false; 1341 1.1 mrg if (!startswith (p, "_Z")) 1342 1.1 mrg return NULL; 1343 1.1 mrg p += 2; 1344 1.1 mrg if (startswith (p, "St")) 1345 1.1 mrg p += 2; 1346 1.1 mrg else if (*p == 'N') 1347 1.1 mrg { 1348 1.1 mrg nested = true; 1349 1.1 mrg ++p; 1350 1.1 mrg while (ISDIGIT (p[0])) 1351 1.1 mrg { 1352 1.1 mrg char *e; 1353 1.1 mrg long num = strtol (p, &e, 10); 1354 1.1 mrg if (num >= 1 && num < end - e) 1355 1.1 mrg p = e + num; 1356 1.1 mrg else 1357 1.1 mrg break; 1358 1.1 mrg } 1359 1.1 mrg } 1360 1.1 mrg if (!startswith (p, "DC")) 1361 1.1 mrg return NULL; 1362 1.1 mrg if (nested) 1363 1.1 mrg { 1364 1.1 mrg if (end[-1] != 'E') 1365 1.1 mrg return NULL; 1366 1.1 mrg --end; 1367 1.1 mrg } 1368 1.1 mrg if (end[-1] != 'E') 1369 1.1 mrg return NULL; 1370 1.1 mrg *len = end - p; 1371 1.1 mrg return p; 1372 1.1 mrg } 1373 1.1 mrg 1374 1.1 mrg /* "For the purposes of mangling, the name of an anonymous union is considered 1375 1.1 mrg to be the name of the first named data member found by a pre-order, 1376 1.1 mrg depth-first, declaration-order walk of the data members of the anonymous 1377 1.1 mrg union. If there is no such data member (i.e., if all of the data members in 1378 1.1 mrg the union are unnamed), then there is no way for a program to refer to the 1379 1.1 mrg anonymous union, and there is therefore no need to mangle its name." */ 1380 1.1 mrg 1381 1.1 mrg static tree 1382 1.1 mrg anon_aggr_naming_decl (tree type) 1383 1.1 mrg { 1384 1.1 mrg tree field = next_initializable_field (TYPE_FIELDS (type)); 1385 1.1 mrg for (; field; field = next_initializable_field (DECL_CHAIN (field))) 1386 1.1 mrg { 1387 1.1 mrg if (DECL_NAME (field)) 1388 1.1 mrg return field; 1389 1.1 mrg if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 1390 1.1 mrg if (tree sub = anon_aggr_naming_decl (TREE_TYPE (field))) 1391 1.1 mrg return sub; 1392 1.1 mrg } 1393 1.1 mrg return NULL_TREE; 1394 1.1 mrg } 1395 1.1 mrg 1396 1.1 mrg /* We don't need to handle thunks, vtables, or VTTs here. Those are 1397 1.1 mrg mangled through special entry points. 1398 1.1 mrg 1399 1.1 mrg <unqualified-name> ::= [<module-name>] <operator-name> 1400 1.1 mrg ::= <special-name> 1401 1.1 mrg ::= [<module-name>] <source-name> 1402 1.1 mrg ::= [<module-name>] <unnamed-type-name> 1403 1.1 mrg ::= <local-source-name> 1404 1.1 mrg 1405 1.1 mrg <local-source-name> ::= L <source-name> <discriminator> */ 1406 1.1 mrg 1407 1.1 mrg static void 1408 1.1 mrg write_unqualified_id (tree identifier) 1409 1.1 mrg { 1410 1.1 mrg if (IDENTIFIER_CONV_OP_P (identifier)) 1411 1.1 mrg write_conversion_operator_name (TREE_TYPE (identifier)); 1412 1.1 mrg else if (IDENTIFIER_OVL_OP_P (identifier)) 1413 1.1 mrg { 1414 1.1 mrg const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier); 1415 1.1 mrg write_string (ovl_op->mangled_name); 1416 1.1 mrg } 1417 1.1 mrg else if (UDLIT_OPER_P (identifier)) 1418 1.1 mrg write_literal_operator_name (identifier); 1419 1.1 mrg else 1420 1.1 mrg write_source_name (identifier); 1421 1.1 mrg } 1422 1.1 mrg 1423 1.1 mrg static void 1424 1.1 mrg write_unqualified_name (tree decl) 1425 1.1 mrg { 1426 1.1 mrg MANGLE_TRACE_TREE ("unqualified-name", decl); 1427 1.1 mrg 1428 1.1 mrg if (modules_p ()) 1429 1.1 mrg maybe_write_module (decl); 1430 1.1 mrg 1431 1.1 mrg if (identifier_p (decl)) 1432 1.1 mrg { 1433 1.1 mrg write_unqualified_id (decl); 1434 1.1 mrg return; 1435 1.1 mrg } 1436 1.1 mrg 1437 1.1 mrg bool found = false; 1438 1.1 mrg 1439 1.1 mrg if (DECL_NAME (decl) == NULL_TREE 1440 1.1 mrg && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) 1441 1.1 mrg decl = anon_aggr_naming_decl (TREE_TYPE (decl)); 1442 1.1 mrg else if (DECL_NAME (decl) == NULL_TREE) 1443 1.1 mrg { 1444 1.1 mrg found = true; 1445 1.1 mrg gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); 1446 1.1 mrg const char *decomp_str = NULL; 1447 1.1 mrg size_t decomp_len = 0; 1448 1.1 mrg if (VAR_P (decl) 1449 1.1 mrg && DECL_DECOMPOSITION_P (decl) 1450 1.1 mrg && DECL_NAME (decl) == NULL_TREE 1451 1.1 mrg && DECL_NAMESPACE_SCOPE_P (decl)) 1452 1.1 mrg decomp_str = find_decomp_unqualified_name (decl, &decomp_len); 1453 1.1 mrg if (decomp_str) 1454 1.1 mrg write_chars (decomp_str, decomp_len); 1455 1.1 mrg else 1456 1.1 mrg write_source_name (DECL_ASSEMBLER_NAME (decl)); 1457 1.1 mrg } 1458 1.1 mrg else if (DECL_DECLARES_FUNCTION_P (decl)) 1459 1.1 mrg { 1460 1.1 mrg found = true; 1461 1.1 mrg if (DECL_CONSTRUCTOR_P (decl)) 1462 1.1 mrg write_special_name_constructor (decl); 1463 1.1 mrg else if (DECL_DESTRUCTOR_P (decl)) 1464 1.1 mrg write_special_name_destructor (decl); 1465 1.1 mrg else if (DECL_CONV_FN_P (decl)) 1466 1.1 mrg { 1467 1.1 mrg /* Conversion operator. Handle it right here. 1468 1.1 mrg <operator> ::= cv <type> */ 1469 1.1 mrg tree type; 1470 1.1 mrg if (maybe_template_info (decl)) 1471 1.1 mrg { 1472 1.1 mrg tree fn_type; 1473 1.1 mrg fn_type = get_mostly_instantiated_function_type (decl); 1474 1.1 mrg type = TREE_TYPE (fn_type); 1475 1.1 mrg } 1476 1.1 mrg else if (FNDECL_USED_AUTO (decl)) 1477 1.1 mrg type = DECL_SAVED_AUTO_RETURN_TYPE (decl); 1478 1.1 mrg else 1479 1.1 mrg type = DECL_CONV_FN_TYPE (decl); 1480 1.1 mrg write_conversion_operator_name (type); 1481 1.1 mrg } 1482 1.1 mrg else if (DECL_OVERLOADED_OPERATOR_P (decl)) 1483 1.1 mrg { 1484 1.1 mrg tree t; 1485 1.1 mrg if (!(t = DECL_RAMP_FN (decl))) 1486 1.1 mrg t = decl; 1487 1.1 mrg const char *mangled_name 1488 1.1 mrg = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)] 1489 1.1 mrg [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name); 1490 1.1 mrg write_string (mangled_name); 1491 1.1 mrg } 1492 1.1 mrg else if (UDLIT_OPER_P (DECL_NAME (decl))) 1493 1.1 mrg write_literal_operator_name (DECL_NAME (decl)); 1494 1.1 mrg else 1495 1.1 mrg found = false; 1496 1.1 mrg } 1497 1.1 mrg 1498 1.1 mrg if (found) 1499 1.1 mrg /* OK */; 1500 1.1 mrg else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl) 1501 1.1 mrg && DECL_NAMESPACE_SCOPE_P (decl) 1502 1.1 mrg && decl_linkage (decl) == lk_internal) 1503 1.1 mrg { 1504 1.1 mrg MANGLE_TRACE_TREE ("local-source-name", decl); 1505 1.1 mrg write_char ('L'); 1506 1.1 mrg write_source_name (DECL_NAME (decl)); 1507 1.1 mrg /* The default discriminator is 1, and that's all we ever use, 1508 1.1 mrg so there's no code to output one here. */ 1509 1.1 mrg } 1510 1.1 mrg else 1511 1.1 mrg { 1512 1.1 mrg tree type = TREE_TYPE (decl); 1513 1.1 mrg 1514 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL 1515 1.1 mrg && TYPE_UNNAMED_P (type)) 1516 1.1 mrg write_unnamed_type_name (type); 1517 1.1 mrg else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type)) 1518 1.1 mrg write_closure_type_name (type); 1519 1.1 mrg else 1520 1.1 mrg write_source_name (DECL_NAME (decl)); 1521 1.1 mrg } 1522 1.1 mrg 1523 1.1 mrg /* We use the ABI tags from the primary class template, ignoring tags on any 1524 1.1 mrg specializations. This is necessary because C++ doesn't require a 1525 1.1 mrg specialization to be declared before it is used unless the use requires a 1526 1.1 mrg complete type, but we need to get the tags right on incomplete types as 1527 1.1 mrg well. */ 1528 1.1 mrg if (tree tmpl = most_general_template (decl)) 1529 1.1 mrg { 1530 1.1 mrg tree res = DECL_TEMPLATE_RESULT (tmpl); 1531 1.1 mrg if (res == NULL_TREE) 1532 1.1 mrg /* UNBOUND_CLASS_TEMPLATE. */; 1533 1.1 mrg else if (DECL_DECLARES_TYPE_P (decl)) 1534 1.1 mrg decl = res; 1535 1.1 mrg else if (any_abi_below (11)) 1536 1.1 mrg { 1537 1.1 mrg /* ABI v10 implicit tags on the template. */ 1538 1.1 mrg tree mtags = missing_abi_tags (res); 1539 1.1 mrg /* Explicit tags on the template. */ 1540 1.1 mrg tree ttags = get_abi_tags (res); 1541 1.1 mrg /* Tags on the instantiation. */ 1542 1.1 mrg tree dtags = get_abi_tags (decl); 1543 1.1 mrg 1544 1.1 mrg if (mtags && abi_warn_or_compat_version_crosses (10)) 1545 1.1 mrg G.need_abi_warning = 1; 1546 1.1 mrg 1547 1.1 mrg /* Add the v10 tags to the explicit tags now. */ 1548 1.1 mrg mtags = chainon (mtags, ttags); 1549 1.1 mrg 1550 1.1 mrg if (!G.need_abi_warning 1551 1.1 mrg && abi_warn_or_compat_version_crosses (11) 1552 1.1 mrg && !equal_abi_tags (dtags, mtags)) 1553 1.1 mrg G.need_abi_warning = 1; 1554 1.1 mrg 1555 1.1 mrg if (!abi_version_at_least (10)) 1556 1.1 mrg /* In abi <10, we only got the explicit tags. */ 1557 1.1 mrg decl = res; 1558 1.1 mrg else if (flag_abi_version == 10) 1559 1.1 mrg { 1560 1.1 mrg /* In ABI 10, we want explict and implicit tags. */ 1561 1.1 mrg write_abi_tags (mtags); 1562 1.1 mrg return; 1563 1.1 mrg } 1564 1.1 mrg } 1565 1.1 mrg } 1566 1.1 mrg 1567 1.1 mrg tree tags = get_abi_tags (decl); 1568 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl) 1569 1.1 mrg && any_abi_below (11)) 1570 1.1 mrg if (tree mtags = missing_abi_tags (decl)) 1571 1.1 mrg { 1572 1.1 mrg if (abi_warn_or_compat_version_crosses (11)) 1573 1.1 mrg G.need_abi_warning = true; 1574 1.1 mrg if (!abi_version_at_least (11)) 1575 1.1 mrg tags = chainon (mtags, tags); 1576 1.1 mrg } 1577 1.1 mrg write_abi_tags (tags); 1578 1.1 mrg } 1579 1.1 mrg 1580 1.1 mrg /* Write the unqualified-name for a conversion operator to TYPE. */ 1581 1.1 mrg 1582 1.1 mrg static void 1583 1.1 mrg write_conversion_operator_name (const tree type) 1584 1.1 mrg { 1585 1.1 mrg write_string ("cv"); 1586 1.1 mrg write_type (type); 1587 1.1 mrg } 1588 1.1 mrg 1589 1.1 mrg /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE. 1590 1.1 mrg 1591 1.1 mrg <source-name> ::= </length/ number> <identifier> */ 1592 1.1 mrg 1593 1.1 mrg static void 1594 1.1 mrg write_source_name (tree identifier) 1595 1.1 mrg { 1596 1.1 mrg MANGLE_TRACE_TREE ("source-name", identifier); 1597 1.1 mrg 1598 1.1 mrg write_unsigned_number (IDENTIFIER_LENGTH (identifier)); 1599 1.1 mrg write_identifier (IDENTIFIER_POINTER (identifier)); 1600 1.1 mrg } 1601 1.1 mrg 1602 1.1 mrg /* Compare two TREE_STRINGs like strcmp. */ 1603 1.1 mrg 1604 1.1 mrg int 1605 1.1 mrg tree_string_cmp (const void *p1, const void *p2) 1606 1.1 mrg { 1607 1.1 mrg if (p1 == p2) 1608 1.1 mrg return 0; 1609 1.1 mrg tree s1 = *(const tree*)p1; 1610 1.1 mrg tree s2 = *(const tree*)p2; 1611 1.1 mrg return strcmp (TREE_STRING_POINTER (s1), 1612 1.1 mrg TREE_STRING_POINTER (s2)); 1613 1.1 mrg } 1614 1.1 mrg 1615 1.1 mrg /* Return the TREE_LIST of TAGS as a sorted VEC. */ 1616 1.1 mrg 1617 1.1 mrg static vec<tree, va_gc> * 1618 1.1 mrg sorted_abi_tags (tree tags) 1619 1.1 mrg { 1620 1.1 mrg vec<tree, va_gc> * vec = make_tree_vector(); 1621 1.1 mrg 1622 1.1 mrg for (tree t = tags; t; t = TREE_CHAIN (t)) 1623 1.1 mrg { 1624 1.1 mrg if (ABI_TAG_IMPLICIT (t)) 1625 1.1 mrg continue; 1626 1.1 mrg tree str = TREE_VALUE (t); 1627 1.1 mrg vec_safe_push (vec, str); 1628 1.1 mrg } 1629 1.1 mrg 1630 1.1 mrg vec->qsort (tree_string_cmp); 1631 1.1 mrg 1632 1.1 mrg return vec; 1633 1.1 mrg } 1634 1.1 mrg 1635 1.1 mrg /* ID is the name of a function or type with abi_tags attribute TAGS. 1636 1.1 mrg Write out the name, suitably decorated. */ 1637 1.1 mrg 1638 1.1 mrg static void 1639 1.1 mrg write_abi_tags (tree tags) 1640 1.1 mrg { 1641 1.1 mrg if (tags == NULL_TREE) 1642 1.1 mrg return; 1643 1.1 mrg 1644 1.1 mrg vec<tree, va_gc> * vec = sorted_abi_tags (tags); 1645 1.1 mrg 1646 1.1 mrg unsigned i; tree str; 1647 1.1 mrg FOR_EACH_VEC_ELT (*vec, i, str) 1648 1.1 mrg { 1649 1.1 mrg write_string ("B"); 1650 1.1 mrg write_unsigned_number (TREE_STRING_LENGTH (str) - 1); 1651 1.1 mrg write_identifier (TREE_STRING_POINTER (str)); 1652 1.1 mrg } 1653 1.1 mrg 1654 1.1 mrg release_tree_vector (vec); 1655 1.1 mrg } 1656 1.1 mrg 1657 1.1 mrg /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */ 1658 1.1 mrg 1659 1.1 mrg static bool 1660 1.1 mrg equal_abi_tags (tree t1, tree t2) 1661 1.1 mrg { 1662 1.1 mrg releasing_vec v1 = sorted_abi_tags (t1); 1663 1.1 mrg releasing_vec v2 = sorted_abi_tags (t2); 1664 1.1 mrg 1665 1.1 mrg unsigned len1 = v1->length(); 1666 1.1 mrg if (len1 != v2->length()) 1667 1.1 mrg return false; 1668 1.1 mrg for (unsigned i = 0; i < len1; ++i) 1669 1.1 mrg if (tree_string_cmp (v1[i], v2[i]) != 0) 1670 1.1 mrg return false; 1671 1.1 mrg return true; 1672 1.1 mrg } 1673 1.1 mrg 1674 1.1 mrg /* Write a user-defined literal operator. 1675 1.1 mrg ::= li <source-name> # "" <source-name> 1676 1.1 mrg IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */ 1677 1.1 mrg 1678 1.1 mrg static void 1679 1.1 mrg write_literal_operator_name (tree identifier) 1680 1.1 mrg { 1681 1.1 mrg const char* suffix = UDLIT_OP_SUFFIX (identifier); 1682 1.1 mrg write_identifier (UDLIT_OP_MANGLED_PREFIX); 1683 1.1 mrg write_unsigned_number (strlen (suffix)); 1684 1.1 mrg write_identifier (suffix); 1685 1.1 mrg } 1686 1.1 mrg 1687 1.1 mrg /* Encode 0 as _, and 1+ as n-1_. */ 1688 1.1 mrg 1689 1.1 mrg static void 1690 1.1 mrg write_compact_number (int num) 1691 1.1 mrg { 1692 1.1 mrg gcc_checking_assert (num >= 0); 1693 1.1 mrg if (num > 0) 1694 1.1 mrg write_unsigned_number (num - 1); 1695 1.1 mrg write_char ('_'); 1696 1.1 mrg } 1697 1.1 mrg 1698 1.1 mrg /* Return how many unnamed types precede TYPE in its enclosing class. */ 1699 1.1 mrg 1700 1.1 mrg static int 1701 1.1 mrg nested_anon_class_index (tree type) 1702 1.1 mrg { 1703 1.1 mrg int index = 0; 1704 1.1 mrg tree member = TYPE_FIELDS (TYPE_CONTEXT (type)); 1705 1.1 mrg for (; member; member = DECL_CHAIN (member)) 1706 1.1 mrg if (DECL_IMPLICIT_TYPEDEF_P (member)) 1707 1.1 mrg { 1708 1.1 mrg tree memtype = TREE_TYPE (member); 1709 1.1 mrg if (memtype == type) 1710 1.1 mrg return index; 1711 1.1 mrg else if (TYPE_UNNAMED_P (memtype)) 1712 1.1 mrg ++index; 1713 1.1 mrg } 1714 1.1 mrg 1715 1.1 mrg if (seen_error ()) 1716 1.1 mrg return -1; 1717 1.1 mrg 1718 1.1 mrg gcc_unreachable (); 1719 1.1 mrg } 1720 1.1 mrg 1721 1.1 mrg /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */ 1722 1.1 mrg 1723 1.1 mrg static void 1724 1.1 mrg write_unnamed_type_name (const tree type) 1725 1.1 mrg { 1726 1.1 mrg int discriminator; 1727 1.1 mrg MANGLE_TRACE_TREE ("unnamed-type-name", type); 1728 1.1 mrg 1729 1.1 mrg if (TYPE_FUNCTION_SCOPE_P (type)) 1730 1.1 mrg discriminator = discriminator_for_local_entity (TYPE_NAME (type)); 1731 1.1 mrg else if (TYPE_CLASS_SCOPE_P (type)) 1732 1.1 mrg discriminator = nested_anon_class_index (type); 1733 1.1 mrg else 1734 1.1 mrg { 1735 1.1 mrg gcc_assert (no_linkage_check (type, /*relaxed_p=*/true)); 1736 1.1 mrg /* Just use the old mangling at namespace scope. */ 1737 1.1 mrg write_source_name (TYPE_IDENTIFIER (type)); 1738 1.1 mrg return; 1739 1.1 mrg } 1740 1.1 mrg 1741 1.1 mrg write_string ("Ut"); 1742 1.1 mrg write_compact_number (discriminator); 1743 1.1 mrg } 1744 1.1 mrg 1745 1.1 mrg /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ 1746 1.1 mrg <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */ 1747 1.1 mrg 1748 1.1 mrg static void 1749 1.1 mrg write_closure_type_name (const tree type) 1750 1.1 mrg { 1751 1.1 mrg tree fn = lambda_function (type); 1752 1.1 mrg tree lambda = CLASSTYPE_LAMBDA_EXPR (type); 1753 1.1 mrg tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn)); 1754 1.1 mrg 1755 1.1 mrg MANGLE_TRACE_TREE ("closure-type-name", type); 1756 1.1 mrg 1757 1.1 mrg write_string ("Ul"); 1758 1.1 mrg write_method_parms (parms, /*method_p=*/1, fn); 1759 1.1 mrg write_char ('E'); 1760 1.1 mrg write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda)); 1761 1.1 mrg } 1762 1.1 mrg 1763 1.1 mrg /* Convert NUMBER to ascii using base BASE and generating at least 1764 1.1 mrg MIN_DIGITS characters. BUFFER points to the _end_ of the buffer 1765 1.1 mrg into which to store the characters. Returns the number of 1766 1.1 mrg characters generated (these will be laid out in advance of where 1767 1.1 mrg BUFFER points). */ 1768 1.1 mrg 1769 1.1 mrg static int 1770 1.1 mrg hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base, 1771 1.1 mrg char *buffer, const unsigned int min_digits) 1772 1.1 mrg { 1773 1.1 mrg static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 1774 1.1 mrg unsigned digits = 0; 1775 1.1 mrg 1776 1.1 mrg while (number) 1777 1.1 mrg { 1778 1.1 mrg unsigned HOST_WIDE_INT d = number / base; 1779 1.1 mrg 1780 1.1 mrg *--buffer = base_digits[number - d * base]; 1781 1.1 mrg digits++; 1782 1.1 mrg number = d; 1783 1.1 mrg } 1784 1.1 mrg while (digits < min_digits) 1785 1.1 mrg { 1786 1.1 mrg *--buffer = base_digits[0]; 1787 1.1 mrg digits++; 1788 1.1 mrg } 1789 1.1 mrg return digits; 1790 1.1 mrg } 1791 1.1 mrg 1792 1.1 mrg /* Non-terminal <number>. 1793 1.1 mrg 1794 1.1 mrg <number> ::= [n] </decimal integer/> */ 1795 1.1 mrg 1796 1.1 mrg static void 1797 1.1 mrg write_number (unsigned HOST_WIDE_INT number, const int unsigned_p, 1798 1.1 mrg const unsigned int base) 1799 1.1 mrg { 1800 1.1 mrg char buffer[sizeof (HOST_WIDE_INT) * 8]; 1801 1.1 mrg unsigned count = 0; 1802 1.1 mrg 1803 1.1 mrg if (!unsigned_p && (HOST_WIDE_INT) number < 0) 1804 1.1 mrg { 1805 1.1 mrg write_char ('n'); 1806 1.1 mrg number = -((HOST_WIDE_INT) number); 1807 1.1 mrg } 1808 1.1 mrg count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1); 1809 1.1 mrg write_chars (buffer + sizeof (buffer) - count, count); 1810 1.1 mrg } 1811 1.1 mrg 1812 1.1 mrg /* Write out an integral CST in decimal. Most numbers are small, and 1813 1.1 mrg representable in a HOST_WIDE_INT. Occasionally we'll have numbers 1814 1.1 mrg bigger than that, which we must deal with. */ 1815 1.1 mrg 1816 1.1 mrg static inline void 1817 1.1 mrg write_integer_cst (const tree cst) 1818 1.1 mrg { 1819 1.1 mrg int sign = tree_int_cst_sgn (cst); 1820 1.1 mrg widest_int abs_value = wi::abs (wi::to_widest (cst)); 1821 1.1 mrg if (!wi::fits_uhwi_p (abs_value)) 1822 1.1 mrg { 1823 1.1 mrg /* A bignum. We do this in chunks, each of which fits in a 1824 1.1 mrg HOST_WIDE_INT. */ 1825 1.1 mrg char buffer[sizeof (HOST_WIDE_INT) * 8 * 2]; 1826 1.1 mrg unsigned HOST_WIDE_INT chunk; 1827 1.1 mrg unsigned chunk_digits; 1828 1.1 mrg char *ptr = buffer + sizeof (buffer); 1829 1.1 mrg unsigned count = 0; 1830 1.1 mrg tree n, base, type; 1831 1.1 mrg int done; 1832 1.1 mrg 1833 1.1 mrg /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is 1834 1.1 mrg representable. */ 1835 1.1 mrg chunk = 1000000000; 1836 1.1 mrg chunk_digits = 9; 1837 1.1 mrg 1838 1.1 mrg if (sizeof (HOST_WIDE_INT) >= 8) 1839 1.1 mrg { 1840 1.1 mrg /* It is at least 64 bits, so 10^18 is representable. */ 1841 1.1 mrg chunk_digits = 18; 1842 1.1 mrg chunk *= chunk; 1843 1.1 mrg } 1844 1.1 mrg 1845 1.1 mrg type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst)); 1846 1.1 mrg base = build_int_cstu (type, chunk); 1847 1.1 mrg n = wide_int_to_tree (type, wi::to_wide (cst)); 1848 1.1 mrg 1849 1.1 mrg if (sign < 0) 1850 1.1 mrg { 1851 1.1 mrg write_char ('n'); 1852 1.1 mrg n = fold_build1_loc (input_location, NEGATE_EXPR, type, n); 1853 1.1 mrg } 1854 1.1 mrg do 1855 1.1 mrg { 1856 1.1 mrg tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base); 1857 1.1 mrg tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base); 1858 1.1 mrg unsigned c; 1859 1.1 mrg 1860 1.1 mrg done = integer_zerop (d); 1861 1.1 mrg tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp); 1862 1.1 mrg c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr, 1863 1.1 mrg done ? 1 : chunk_digits); 1864 1.1 mrg ptr -= c; 1865 1.1 mrg count += c; 1866 1.1 mrg n = d; 1867 1.1 mrg } 1868 1.1 mrg while (!done); 1869 1.1 mrg write_chars (ptr, count); 1870 1.1 mrg } 1871 1.1 mrg else 1872 1.1 mrg { 1873 1.1 mrg /* A small num. */ 1874 1.1 mrg if (sign < 0) 1875 1.1 mrg write_char ('n'); 1876 1.1 mrg write_unsigned_number (abs_value.to_uhwi ()); 1877 1.1 mrg } 1878 1.1 mrg } 1879 1.1 mrg 1880 1.1 mrg /* Write out a floating-point literal. 1881 1.1 mrg 1882 1.1 mrg "Floating-point literals are encoded using the bit pattern of the 1883 1.1 mrg target processor's internal representation of that number, as a 1884 1.1 mrg fixed-length lowercase hexadecimal string, high-order bytes first 1885 1.1 mrg (even if the target processor would store low-order bytes first). 1886 1.1 mrg The "n" prefix is not used for floating-point literals; the sign 1887 1.1 mrg bit is encoded with the rest of the number. 1888 1.1 mrg 1889 1.1 mrg Here are some examples, assuming the IEEE standard representation 1890 1.1 mrg for floating point numbers. (Spaces are for readability, not 1891 1.1 mrg part of the encoding.) 1892 1.1 mrg 1893 1.1 mrg 1.0f Lf 3f80 0000 E 1894 1.1 mrg -1.0f Lf bf80 0000 E 1895 1.1 mrg 1.17549435e-38f Lf 0080 0000 E 1896 1.1 mrg 1.40129846e-45f Lf 0000 0001 E 1897 1.1 mrg 0.0f Lf 0000 0000 E" 1898 1.1 mrg 1899 1.1 mrg Caller is responsible for the Lx and the E. */ 1900 1.1 mrg static void 1901 1.1 mrg write_real_cst (const tree value) 1902 1.1 mrg { 1903 1.1 mrg long target_real[4]; /* largest supported float */ 1904 1.1 mrg /* Buffer for eight hex digits in a 32-bit number but big enough 1905 1.1 mrg even for 64-bit long to avoid warnings. */ 1906 1.1 mrg char buffer[17]; 1907 1.1 mrg int i, limit, dir; 1908 1.1 mrg 1909 1.1 mrg tree type = TREE_TYPE (value); 1910 1.1 mrg int words = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type)) / 32; 1911 1.1 mrg 1912 1.1 mrg real_to_target (target_real, &TREE_REAL_CST (value), 1913 1.1 mrg TYPE_MODE (type)); 1914 1.1 mrg 1915 1.1 mrg /* The value in target_real is in the target word order, 1916 1.1 mrg so we must write it out backward if that happens to be 1917 1.1 mrg little-endian. write_number cannot be used, it will 1918 1.1 mrg produce uppercase. */ 1919 1.1 mrg if (FLOAT_WORDS_BIG_ENDIAN) 1920 1.1 mrg i = 0, limit = words, dir = 1; 1921 1.1 mrg else 1922 1.1 mrg i = words - 1, limit = -1, dir = -1; 1923 1.1 mrg 1924 1.1 mrg for (; i != limit; i += dir) 1925 1.1 mrg { 1926 1.1 mrg sprintf (buffer, "%08lx", (unsigned long) target_real[i]); 1927 1.1 mrg write_chars (buffer, 8); 1928 1.1 mrg } 1929 1.1 mrg } 1930 1.1 mrg 1931 1.1 mrg /* Non-terminal <identifier>. 1932 1.1 mrg 1933 1.1 mrg <identifier> ::= </unqualified source code identifier> */ 1934 1.1 mrg 1935 1.1 mrg static void 1936 1.1 mrg write_identifier (const char *identifier) 1937 1.1 mrg { 1938 1.1 mrg MANGLE_TRACE ("identifier", identifier); 1939 1.1 mrg write_string (identifier); 1940 1.1 mrg } 1941 1.1 mrg 1942 1.1 mrg /* Handle constructor productions of non-terminal <special-name>. 1943 1.1 mrg CTOR is a constructor FUNCTION_DECL. 1944 1.1 mrg 1945 1.1 mrg <special-name> ::= C1 # complete object constructor 1946 1.1 mrg ::= C2 # base object constructor 1947 1.1 mrg ::= C3 # complete object allocating constructor 1948 1.1 mrg 1949 1.1 mrg Currently, allocating constructors are never used. */ 1950 1.1 mrg 1951 1.1 mrg static void 1952 1.1 mrg write_special_name_constructor (const tree ctor) 1953 1.1 mrg { 1954 1.1 mrg write_char ('C'); 1955 1.1 mrg bool new_inh = (flag_new_inheriting_ctors 1956 1.1 mrg && DECL_INHERITED_CTOR (ctor)); 1957 1.1 mrg if (new_inh) 1958 1.1 mrg write_char ('I'); 1959 1.1 mrg if (DECL_BASE_CONSTRUCTOR_P (ctor)) 1960 1.1 mrg write_char ('2'); 1961 1.1 mrg /* This is the old-style "[unified]" constructor. 1962 1.1 mrg In some cases, we may emit this function and call 1963 1.1 mrg it from the clones in order to share code and save space. */ 1964 1.1 mrg else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor)) 1965 1.1 mrg write_char ('4'); 1966 1.1 mrg else 1967 1.1 mrg { 1968 1.1 mrg gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)); 1969 1.1 mrg write_char ('1'); 1970 1.1 mrg } 1971 1.1 mrg if (new_inh) 1972 1.1 mrg write_type (DECL_INHERITED_CTOR_BASE (ctor)); 1973 1.1 mrg } 1974 1.1 mrg 1975 1.1 mrg /* Handle destructor productions of non-terminal <special-name>. 1976 1.1 mrg DTOR is a destructor FUNCTION_DECL. 1977 1.1 mrg 1978 1.1 mrg <special-name> ::= D0 # deleting (in-charge) destructor 1979 1.1 mrg ::= D1 # complete object (in-charge) destructor 1980 1.1 mrg ::= D2 # base object (not-in-charge) destructor */ 1981 1.1 mrg 1982 1.1 mrg static void 1983 1.1 mrg write_special_name_destructor (const tree dtor) 1984 1.1 mrg { 1985 1.1 mrg if (DECL_DELETING_DESTRUCTOR_P (dtor)) 1986 1.1 mrg write_string ("D0"); 1987 1.1 mrg else if (DECL_BASE_DESTRUCTOR_P (dtor)) 1988 1.1 mrg write_string ("D2"); 1989 1.1 mrg else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor)) 1990 1.1 mrg /* This is the old-style "[unified]" destructor. 1991 1.1 mrg In some cases, we may emit this function and call 1992 1.1 mrg it from the clones in order to share code and save space. */ 1993 1.1 mrg write_string ("D4"); 1994 1.1 mrg else 1995 1.1 mrg { 1996 1.1 mrg gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)); 1997 1.1 mrg write_string ("D1"); 1998 1.1 mrg } 1999 1.1 mrg } 2000 1.1 mrg 2001 1.1 mrg /* Return the discriminator for ENTITY appearing inside 2002 1.1 mrg FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among 2003 1.1 mrg entities with the same name and kind in the same FUNCTION. */ 2004 1.1 mrg 2005 1.1 mrg static int 2006 1.1 mrg discriminator_for_local_entity (tree entity) 2007 1.1 mrg { 2008 1.1 mrg if (!DECL_LANG_SPECIFIC (entity)) 2009 1.1 mrg { 2010 1.1 mrg /* Some decls, like __FUNCTION__, don't need a discriminator. */ 2011 1.1 mrg gcc_checking_assert (DECL_ARTIFICIAL (entity)); 2012 1.1 mrg return 0; 2013 1.1 mrg } 2014 1.1 mrg else if (tree disc = DECL_DISCRIMINATOR (entity)) 2015 1.1 mrg return TREE_INT_CST_LOW (disc); 2016 1.1 mrg else 2017 1.1 mrg /* The first entity with a particular name doesn't get 2018 1.1 mrg DECL_DISCRIMINATOR set up. */ 2019 1.1 mrg return 0; 2020 1.1 mrg } 2021 1.1 mrg 2022 1.1 mrg /* Return the discriminator for STRING, a string literal used inside 2023 1.1 mrg FUNCTION. The discriminator is the lexical ordinal of STRING among 2024 1.1 mrg string literals used in FUNCTION. */ 2025 1.1 mrg 2026 1.1 mrg static int 2027 1.1 mrg discriminator_for_string_literal (tree /*function*/, 2028 1.1 mrg tree /*string*/) 2029 1.1 mrg { 2030 1.1 mrg /* For now, we don't discriminate amongst string literals. */ 2031 1.1 mrg return 0; 2032 1.1 mrg } 2033 1.1 mrg 2034 1.1 mrg /* <discriminator> := _ <number> # when number < 10 2035 1.1 mrg := __ <number> _ # when number >= 10 2036 1.1 mrg 2037 1.1 mrg The discriminator is used only for the second and later occurrences 2038 1.1 mrg of the same name within a single function. In this case <number> is 2039 1.1 mrg n - 2, if this is the nth occurrence, in lexical order. */ 2040 1.1 mrg 2041 1.1 mrg static void 2042 1.1 mrg write_discriminator (const int discriminator) 2043 1.1 mrg { 2044 1.1 mrg /* If discriminator is zero, don't write anything. Otherwise... */ 2045 1.1 mrg if (discriminator > 0) 2046 1.1 mrg { 2047 1.1 mrg write_char ('_'); 2048 1.1 mrg if (discriminator - 1 >= 10) 2049 1.1 mrg { 2050 1.1 mrg if (abi_warn_or_compat_version_crosses (11)) 2051 1.1 mrg G.need_abi_warning = 1; 2052 1.1 mrg if (abi_version_at_least (11)) 2053 1.1 mrg write_char ('_'); 2054 1.1 mrg } 2055 1.1 mrg write_unsigned_number (discriminator - 1); 2056 1.1 mrg if (abi_version_at_least (11) && discriminator - 1 >= 10) 2057 1.1 mrg write_char ('_'); 2058 1.1 mrg } 2059 1.1 mrg } 2060 1.1 mrg 2061 1.1 mrg /* Mangle the name of a function-scope entity. FUNCTION is the 2062 1.1 mrg FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in 2063 1.1 mrg default argument scope. ENTITY is the decl for the entity itself. 2064 1.1 mrg LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL, 2065 1.1 mrg either ENTITY itself or an enclosing scope of ENTITY. 2066 1.1 mrg 2067 1.1 mrg <local-name> := Z <function encoding> E <entity name> [<discriminator>] 2068 1.1 mrg := Z <function encoding> E s [<discriminator>] 2069 1.1 mrg := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */ 2070 1.1 mrg 2071 1.1 mrg static void 2072 1.1 mrg write_local_name (tree function, const tree local_entity, 2073 1.1 mrg const tree entity) 2074 1.1 mrg { 2075 1.1 mrg tree parm = NULL_TREE; 2076 1.1 mrg 2077 1.1 mrg MANGLE_TRACE_TREE ("local-name", entity); 2078 1.1 mrg 2079 1.1 mrg if (TREE_CODE (function) == PARM_DECL) 2080 1.1 mrg { 2081 1.1 mrg parm = function; 2082 1.1 mrg function = DECL_CONTEXT (parm); 2083 1.1 mrg } 2084 1.1 mrg 2085 1.1 mrg write_char ('Z'); 2086 1.1 mrg write_encoding (function); 2087 1.1 mrg write_char ('E'); 2088 1.1 mrg 2089 1.1 mrg /* For this purpose, parameters are numbered from right-to-left. */ 2090 1.1 mrg if (parm) 2091 1.1 mrg { 2092 1.1 mrg int i = list_length (parm); 2093 1.1 mrg write_char ('d'); 2094 1.1 mrg write_compact_number (i - 1); 2095 1.1 mrg } 2096 1.1 mrg 2097 1.1 mrg if (TREE_CODE (entity) == STRING_CST) 2098 1.1 mrg { 2099 1.1 mrg write_char ('s'); 2100 1.1 mrg write_discriminator (discriminator_for_string_literal (function, 2101 1.1 mrg entity)); 2102 1.1 mrg } 2103 1.1 mrg else 2104 1.1 mrg { 2105 1.1 mrg /* Now the <entity name>. Let write_name know its being called 2106 1.1 mrg from <local-name>, so it doesn't try to process the enclosing 2107 1.1 mrg function scope again. */ 2108 1.1 mrg write_name (entity, /*ignore_local_scope=*/1); 2109 1.1 mrg if (DECL_DISCRIMINATOR_P (local_entity) 2110 1.1 mrg && !(TREE_CODE (local_entity) == TYPE_DECL 2111 1.1 mrg && TYPE_ANON_P (TREE_TYPE (local_entity)))) 2112 1.1 mrg write_discriminator (discriminator_for_local_entity (local_entity)); 2113 1.1 mrg } 2114 1.1 mrg } 2115 1.1 mrg 2116 1.1 mrg /* Non-terminals <type> and <CV-qualifier>. 2117 1.1 mrg 2118 1.1 mrg <type> ::= <builtin-type> 2119 1.1 mrg ::= <function-type> 2120 1.1 mrg ::= <class-enum-type> 2121 1.1 mrg ::= <array-type> 2122 1.1 mrg ::= <pointer-to-member-type> 2123 1.1 mrg ::= <template-param> 2124 1.1 mrg ::= <substitution> 2125 1.1 mrg ::= <CV-qualifier> 2126 1.1 mrg ::= P <type> # pointer-to 2127 1.1 mrg ::= R <type> # reference-to 2128 1.1 mrg ::= C <type> # complex pair (C 2000) 2129 1.1 mrg ::= G <type> # imaginary (C 2000) [not supported] 2130 1.1 mrg ::= U <source-name> <type> # vendor extended type qualifier 2131 1.1 mrg 2132 1.1 mrg C++0x extensions 2133 1.1 mrg 2134 1.1 mrg <type> ::= RR <type> # rvalue reference-to 2135 1.1 mrg <type> ::= Dt <expression> # decltype of an id-expression or 2136 1.1 mrg # class member access 2137 1.1 mrg <type> ::= DT <expression> # decltype of an expression 2138 1.1 mrg <type> ::= Dn # decltype of nullptr 2139 1.1 mrg 2140 1.1 mrg TYPE is a type node. */ 2141 1.1 mrg 2142 1.1 mrg static void 2143 1.1 mrg write_type (tree type) 2144 1.1 mrg { 2145 1.1 mrg /* This gets set to nonzero if TYPE turns out to be a (possibly 2146 1.1 mrg CV-qualified) builtin type. */ 2147 1.1 mrg int is_builtin_type = 0; 2148 1.1 mrg 2149 1.1 mrg MANGLE_TRACE_TREE ("type", type); 2150 1.1 mrg 2151 1.1 mrg if (type == error_mark_node) 2152 1.1 mrg return; 2153 1.1 mrg 2154 1.1 mrg type = canonicalize_for_substitution (type); 2155 1.1 mrg if (find_substitution (type)) 2156 1.1 mrg return; 2157 1.1 mrg 2158 1.1 mrg 2159 1.1 mrg if (write_CV_qualifiers_for_type (type) > 0) 2160 1.1 mrg /* If TYPE was CV-qualified, we just wrote the qualifiers; now 2161 1.1 mrg mangle the unqualified type. The recursive call is needed here 2162 1.1 mrg since both the qualified and unqualified types are substitution 2163 1.1 mrg candidates. */ 2164 1.1 mrg { 2165 1.1 mrg tree t = TYPE_MAIN_VARIANT (type); 2166 1.1 mrg if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t)) 2167 1.1 mrg { 2168 1.1 mrg tree attrs = NULL_TREE; 2169 1.1 mrg if (tx_safe_fn_type_p (type)) 2170 1.1 mrg attrs = tree_cons (get_identifier ("transaction_safe"), 2171 1.1 mrg NULL_TREE, attrs); 2172 1.1 mrg t = cp_build_type_attribute_variant (t, attrs); 2173 1.1 mrg } 2174 1.1 mrg gcc_assert (t != type); 2175 1.1 mrg if (FUNC_OR_METHOD_TYPE_P (t)) 2176 1.1 mrg { 2177 1.1 mrg t = build_ref_qualified_type (t, type_memfn_rqual (type)); 2178 1.1 mrg if (flag_noexcept_type) 2179 1.1 mrg { 2180 1.1 mrg tree r = TYPE_RAISES_EXCEPTIONS (type); 2181 1.1 mrg t = build_exception_variant (t, r); 2182 1.1 mrg } 2183 1.1 mrg if (abi_version_at_least (8) 2184 1.1 mrg || type == TYPE_MAIN_VARIANT (type)) 2185 1.1 mrg /* Avoid adding the unqualified function type as a substitution. */ 2186 1.1 mrg write_function_type (t); 2187 1.1 mrg else 2188 1.1 mrg write_type (t); 2189 1.1 mrg if (abi_warn_or_compat_version_crosses (8)) 2190 1.1 mrg G.need_abi_warning = 1; 2191 1.1 mrg } 2192 1.1 mrg else 2193 1.1 mrg write_type (t); 2194 1.1 mrg } 2195 1.1 mrg else if (TREE_CODE (type) == ARRAY_TYPE) 2196 1.1 mrg /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here 2197 1.1 mrg so that the cv-qualification of the element type is available 2198 1.1 mrg in write_array_type. */ 2199 1.1 mrg write_array_type (type); 2200 1.1 mrg else 2201 1.1 mrg { 2202 1.1 mrg tree type_orig = type; 2203 1.1 mrg 2204 1.1 mrg /* See through any typedefs. */ 2205 1.1 mrg type = TYPE_MAIN_VARIANT (type); 2206 1.1 mrg if (FUNC_OR_METHOD_TYPE_P (type)) 2207 1.1 mrg type = cxx_copy_lang_qualifiers (type, type_orig); 2208 1.1 mrg 2209 1.1 mrg /* According to the C++ ABI, some library classes are passed the 2210 1.1 mrg same as the scalar type of their single member and use the same 2211 1.1 mrg mangling. */ 2212 1.1 mrg if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type)) 2213 1.1 mrg type = TREE_TYPE (first_field (type)); 2214 1.1 mrg 2215 1.1 mrg if (TYPE_PTRDATAMEM_P (type)) 2216 1.1 mrg write_pointer_to_member_type (type); 2217 1.1 mrg else 2218 1.1 mrg { 2219 1.1 mrg /* Handle any target-specific fundamental types. */ 2220 1.1 mrg const char *target_mangling 2221 1.1 mrg = targetm.mangle_type (type_orig); 2222 1.1 mrg 2223 1.1 mrg if (target_mangling) 2224 1.1 mrg { 2225 1.1 mrg write_string (target_mangling); 2226 1.1 mrg /* Add substitutions for types other than fundamental 2227 1.1 mrg types. */ 2228 1.1 mrg if (!VOID_TYPE_P (type) 2229 1.1 mrg && TREE_CODE (type) != INTEGER_TYPE 2230 1.1 mrg && TREE_CODE (type) != REAL_TYPE 2231 1.1 mrg && TREE_CODE (type) != BOOLEAN_TYPE) 2232 1.1 mrg add_substitution (type); 2233 1.1 mrg return; 2234 1.1 mrg } 2235 1.1 mrg 2236 1.1 mrg switch (TREE_CODE (type)) 2237 1.1 mrg { 2238 1.1 mrg case VOID_TYPE: 2239 1.1 mrg case BOOLEAN_TYPE: 2240 1.1 mrg case INTEGER_TYPE: /* Includes wchar_t. */ 2241 1.1 mrg case REAL_TYPE: 2242 1.1 mrg case FIXED_POINT_TYPE: 2243 1.1 mrg { 2244 1.1 mrg /* If this is a typedef, TYPE may not be one of 2245 1.1 mrg the standard builtin type nodes, but an alias of one. Use 2246 1.1 mrg TYPE_MAIN_VARIANT to get to the underlying builtin type. */ 2247 1.1 mrg write_builtin_type (TYPE_MAIN_VARIANT (type)); 2248 1.1 mrg ++is_builtin_type; 2249 1.1 mrg } 2250 1.1 mrg break; 2251 1.1 mrg 2252 1.1 mrg case COMPLEX_TYPE: 2253 1.1 mrg write_char ('C'); 2254 1.1 mrg write_type (TREE_TYPE (type)); 2255 1.1 mrg break; 2256 1.1 mrg 2257 1.1 mrg case FUNCTION_TYPE: 2258 1.1 mrg case METHOD_TYPE: 2259 1.1 mrg write_function_type (type); 2260 1.1 mrg break; 2261 1.1 mrg 2262 1.1 mrg case UNION_TYPE: 2263 1.1 mrg case RECORD_TYPE: 2264 1.1 mrg case ENUMERAL_TYPE: 2265 1.1 mrg /* A pointer-to-member function is represented as a special 2266 1.1 mrg RECORD_TYPE, so check for this first. */ 2267 1.1 mrg if (TYPE_PTRMEMFUNC_P (type)) 2268 1.1 mrg write_pointer_to_member_type (type); 2269 1.1 mrg else 2270 1.1 mrg write_class_enum_type (type); 2271 1.1 mrg break; 2272 1.1 mrg 2273 1.1 mrg case TYPENAME_TYPE: 2274 1.1 mrg case UNBOUND_CLASS_TEMPLATE: 2275 1.1 mrg /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like 2276 1.1 mrg ordinary nested names. */ 2277 1.1 mrg write_nested_name (TYPE_STUB_DECL (type)); 2278 1.1 mrg break; 2279 1.1 mrg 2280 1.1 mrg case POINTER_TYPE: 2281 1.1 mrg case REFERENCE_TYPE: 2282 1.1 mrg if (TYPE_PTR_P (type)) 2283 1.1 mrg write_char ('P'); 2284 1.1 mrg else if (TYPE_REF_IS_RVALUE (type)) 2285 1.1 mrg write_char ('O'); 2286 1.1 mrg else 2287 1.1 mrg write_char ('R'); 2288 1.1 mrg { 2289 1.1 mrg tree target = TREE_TYPE (type); 2290 1.1 mrg /* Attribute const/noreturn are not reflected in mangling. 2291 1.1 mrg We strip them here rather than at a lower level because 2292 1.1 mrg a typedef or template argument can have function type 2293 1.1 mrg with function-cv-quals (that use the same representation), 2294 1.1 mrg but you can't have a pointer/reference to such a type. */ 2295 1.1 mrg if (TREE_CODE (target) == FUNCTION_TYPE) 2296 1.1 mrg { 2297 1.1 mrg if (abi_warn_or_compat_version_crosses (5) 2298 1.1 mrg && TYPE_QUALS (target) != TYPE_UNQUALIFIED) 2299 1.1 mrg G.need_abi_warning = 1; 2300 1.1 mrg if (abi_version_at_least (5)) 2301 1.1 mrg target = build_qualified_type (target, TYPE_UNQUALIFIED); 2302 1.1 mrg } 2303 1.1 mrg write_type (target); 2304 1.1 mrg } 2305 1.1 mrg break; 2306 1.1 mrg 2307 1.1 mrg case TEMPLATE_TYPE_PARM: 2308 1.1 mrg if (is_auto (type)) 2309 1.1 mrg { 2310 1.1 mrg if (AUTO_IS_DECLTYPE (type)) 2311 1.1 mrg write_identifier ("Dc"); 2312 1.1 mrg else 2313 1.1 mrg write_identifier ("Da"); 2314 1.1 mrg ++is_builtin_type; 2315 1.1 mrg break; 2316 1.1 mrg } 2317 1.1 mrg /* fall through. */ 2318 1.1 mrg case TEMPLATE_PARM_INDEX: 2319 1.1 mrg write_template_param (type); 2320 1.1 mrg break; 2321 1.1 mrg 2322 1.1 mrg case TEMPLATE_TEMPLATE_PARM: 2323 1.1 mrg write_template_template_param (type); 2324 1.1 mrg break; 2325 1.1 mrg 2326 1.1 mrg case BOUND_TEMPLATE_TEMPLATE_PARM: 2327 1.1 mrg write_template_template_param (type); 2328 1.1 mrg write_template_args 2329 1.1 mrg (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type))); 2330 1.1 mrg break; 2331 1.1 mrg 2332 1.1 mrg case VECTOR_TYPE: 2333 1.1 mrg if (abi_version_at_least (4)) 2334 1.1 mrg { 2335 1.1 mrg write_string ("Dv"); 2336 1.1 mrg /* Non-constant vector size would be encoded with 2337 1.1 mrg _ expression, but we don't support that yet. */ 2338 1.1 mrg write_unsigned_number (TYPE_VECTOR_SUBPARTS (type) 2339 1.1 mrg .to_constant ()); 2340 1.1 mrg write_char ('_'); 2341 1.1 mrg } 2342 1.1 mrg else 2343 1.1 mrg write_string ("U8__vector"); 2344 1.1 mrg if (abi_warn_or_compat_version_crosses (4)) 2345 1.1 mrg G.need_abi_warning = 1; 2346 1.1 mrg write_type (TREE_TYPE (type)); 2347 1.1 mrg break; 2348 1.1 mrg 2349 1.1 mrg case TYPE_PACK_EXPANSION: 2350 1.1 mrg write_string ("Dp"); 2351 1.1 mrg write_type (PACK_EXPANSION_PATTERN (type)); 2352 1.1 mrg break; 2353 1.1 mrg 2354 1.1 mrg case DECLTYPE_TYPE: 2355 1.1 mrg /* These shouldn't make it into mangling. */ 2356 1.1 mrg gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type) 2357 1.1 mrg && !DECLTYPE_FOR_LAMBDA_PROXY (type)); 2358 1.1 mrg 2359 1.1 mrg /* In ABI <5, we stripped decltype of a plain decl. */ 2360 1.1 mrg if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)) 2361 1.1 mrg { 2362 1.1 mrg tree expr = DECLTYPE_TYPE_EXPR (type); 2363 1.1 mrg tree etype = NULL_TREE; 2364 1.1 mrg switch (TREE_CODE (expr)) 2365 1.1 mrg { 2366 1.1 mrg case VAR_DECL: 2367 1.1 mrg case PARM_DECL: 2368 1.1 mrg case RESULT_DECL: 2369 1.1 mrg case FUNCTION_DECL: 2370 1.1 mrg case CONST_DECL: 2371 1.1 mrg case TEMPLATE_PARM_INDEX: 2372 1.1 mrg etype = TREE_TYPE (expr); 2373 1.1 mrg break; 2374 1.1 mrg 2375 1.1 mrg default: 2376 1.1 mrg break; 2377 1.1 mrg } 2378 1.1 mrg 2379 1.1 mrg if (etype && !type_uses_auto (etype)) 2380 1.1 mrg { 2381 1.1 mrg if (abi_warn_or_compat_version_crosses (5)) 2382 1.1 mrg G.need_abi_warning = 1; 2383 1.1 mrg if (!abi_version_at_least (5)) 2384 1.1 mrg { 2385 1.1 mrg write_type (etype); 2386 1.1 mrg return; 2387 1.1 mrg } 2388 1.1 mrg } 2389 1.1 mrg } 2390 1.1 mrg 2391 1.1 mrg write_char ('D'); 2392 1.1 mrg if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)) 2393 1.1 mrg write_char ('t'); 2394 1.1 mrg else 2395 1.1 mrg write_char ('T'); 2396 1.1 mrg ++cp_unevaluated_operand; 2397 1.1 mrg write_expression (DECLTYPE_TYPE_EXPR (type)); 2398 1.1 mrg --cp_unevaluated_operand; 2399 1.1 mrg write_char ('E'); 2400 1.1 mrg break; 2401 1.1 mrg 2402 1.1 mrg case NULLPTR_TYPE: 2403 1.1 mrg write_string ("Dn"); 2404 1.1 mrg if (abi_version_at_least (7)) 2405 1.1 mrg ++is_builtin_type; 2406 1.1 mrg if (abi_warn_or_compat_version_crosses (7)) 2407 1.1 mrg G.need_abi_warning = 1; 2408 1.1 mrg break; 2409 1.1 mrg 2410 1.1 mrg case TYPEOF_TYPE: 2411 1.1 mrg sorry ("mangling %<typeof%>, use %<decltype%> instead"); 2412 1.1 mrg break; 2413 1.1 mrg 2414 1.1 mrg case UNDERLYING_TYPE: 2415 1.1 mrg sorry ("mangling %<__underlying_type%>"); 2416 1.1 mrg break; 2417 1.1 mrg 2418 1.1 mrg case LANG_TYPE: 2419 1.1 mrg /* fall through. */ 2420 1.1 mrg 2421 1.1 mrg default: 2422 1.1 mrg gcc_unreachable (); 2423 1.1 mrg } 2424 1.1 mrg } 2425 1.1 mrg } 2426 1.1 mrg 2427 1.1 mrg /* Types other than builtin types are substitution candidates. */ 2428 1.1 mrg if (!is_builtin_type) 2429 1.1 mrg add_substitution (type); 2430 1.1 mrg } 2431 1.1 mrg 2432 1.1 mrg /* qsort callback for sorting a vector of attribute entries. */ 2433 1.1 mrg 2434 1.1 mrg static int 2435 1.1 mrg attr_strcmp (const void *p1, const void *p2) 2436 1.1 mrg { 2437 1.1 mrg tree a1 = *(const tree*)p1; 2438 1.1 mrg tree a2 = *(const tree*)p2; 2439 1.1 mrg 2440 1.1 mrg const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1)); 2441 1.1 mrg const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2)); 2442 1.1 mrg 2443 1.1 mrg return strcmp (as1->name, as2->name); 2444 1.1 mrg } 2445 1.1 mrg 2446 1.1 mrg /* Return true if we should mangle a type attribute with name NAME. */ 2447 1.1 mrg 2448 1.1 mrg static bool 2449 1.1 mrg mangle_type_attribute_p (tree name) 2450 1.1 mrg { 2451 1.1 mrg const attribute_spec *as = lookup_attribute_spec (name); 2452 1.1 mrg if (!as || !as->affects_type_identity) 2453 1.1 mrg return false; 2454 1.1 mrg 2455 1.1 mrg /* Skip internal-only attributes, which are distinguished from others 2456 1.1 mrg by having a space. At present, all internal-only attributes that 2457 1.1 mrg affect type identity are target-specific and are handled by 2458 1.1 mrg targetm.mangle_type instead. 2459 1.1 mrg 2460 1.1 mrg Another reason to do this is that a space isn't a valid identifier 2461 1.1 mrg character for most file formats. */ 2462 1.1 mrg if (strchr (IDENTIFIER_POINTER (name), ' ')) 2463 1.1 mrg return false; 2464 1.1 mrg 2465 1.1 mrg /* The following attributes are mangled specially. */ 2466 1.1 mrg if (is_attribute_p ("transaction_safe", name)) 2467 1.1 mrg return false; 2468 1.1 mrg if (is_attribute_p ("abi_tag", name)) 2469 1.1 mrg return false; 2470 1.1 mrg 2471 1.1 mrg return true; 2472 1.1 mrg } 2473 1.1 mrg 2474 1.1 mrg /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of 2475 1.1 mrg CV-qualifiers written for TYPE. 2476 1.1 mrg 2477 1.1 mrg <CV-qualifiers> ::= [r] [V] [K] */ 2478 1.1 mrg 2479 1.1 mrg static int 2480 1.1 mrg write_CV_qualifiers_for_type (const tree type) 2481 1.1 mrg { 2482 1.1 mrg int num_qualifiers = 0; 2483 1.1 mrg 2484 1.1 mrg /* The order is specified by: 2485 1.1 mrg 2486 1.1 mrg "In cases where multiple order-insensitive qualifiers are 2487 1.1 mrg present, they should be ordered 'K' (closest to the base type), 2488 1.1 mrg 'V', 'r', and 'U' (farthest from the base type) ..." */ 2489 1.1 mrg 2490 1.1 mrg /* Mangle attributes that affect type identity as extended qualifiers. 2491 1.1 mrg 2492 1.1 mrg We don't do this with classes and enums because their attributes 2493 1.1 mrg are part of their definitions, not something added on. */ 2494 1.1 mrg 2495 1.1 mrg if (!OVERLOAD_TYPE_P (type)) 2496 1.1 mrg { 2497 1.1 mrg auto_vec<tree> vec; 2498 1.1 mrg for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a)) 2499 1.1 mrg if (mangle_type_attribute_p (get_attribute_name (a))) 2500 1.1 mrg vec.safe_push (a); 2501 1.1 mrg if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ()) 2502 1.1 mrg G.need_abi_warning = true; 2503 1.1 mrg if (abi_version_at_least (10)) 2504 1.1 mrg { 2505 1.1 mrg vec.qsort (attr_strcmp); 2506 1.1 mrg while (!vec.is_empty()) 2507 1.1 mrg { 2508 1.1 mrg tree a = vec.pop(); 2509 1.1 mrg const attribute_spec *as 2510 1.1 mrg = lookup_attribute_spec (get_attribute_name (a)); 2511 1.1 mrg 2512 1.1 mrg write_char ('U'); 2513 1.1 mrg write_unsigned_number (strlen (as->name)); 2514 1.1 mrg write_string (as->name); 2515 1.1 mrg if (TREE_VALUE (a)) 2516 1.1 mrg { 2517 1.1 mrg write_char ('I'); 2518 1.1 mrg for (tree args = TREE_VALUE (a); args; 2519 1.1 mrg args = TREE_CHAIN (args)) 2520 1.1 mrg { 2521 1.1 mrg tree arg = TREE_VALUE (args); 2522 1.1 mrg write_template_arg (arg); 2523 1.1 mrg } 2524 1.1 mrg write_char ('E'); 2525 1.1 mrg } 2526 1.1 mrg 2527 1.1 mrg ++num_qualifiers; 2528 1.1 mrg } 2529 1.1 mrg } 2530 1.1 mrg } 2531 1.1 mrg 2532 1.1 mrg /* Note that we do not use cp_type_quals below; given "const 2533 1.1 mrg int[3]", the "const" is emitted with the "int", not with the 2534 1.1 mrg array. */ 2535 1.1 mrg cp_cv_quals quals = TYPE_QUALS (type); 2536 1.1 mrg 2537 1.1 mrg if (quals & TYPE_QUAL_RESTRICT) 2538 1.1 mrg { 2539 1.1 mrg write_char ('r'); 2540 1.1 mrg ++num_qualifiers; 2541 1.1 mrg } 2542 1.1 mrg if (quals & TYPE_QUAL_VOLATILE) 2543 1.1 mrg { 2544 1.1 mrg write_char ('V'); 2545 1.1 mrg ++num_qualifiers; 2546 1.1 mrg } 2547 1.1 mrg if (quals & TYPE_QUAL_CONST) 2548 1.1 mrg { 2549 1.1 mrg write_char ('K'); 2550 1.1 mrg ++num_qualifiers; 2551 1.1 mrg } 2552 1.1 mrg 2553 1.1 mrg return num_qualifiers; 2554 1.1 mrg } 2555 1.1 mrg 2556 1.1 mrg /* Non-terminal <builtin-type>. 2557 1.1 mrg 2558 1.1 mrg <builtin-type> ::= v # void 2559 1.1 mrg ::= b # bool 2560 1.1 mrg ::= w # wchar_t 2561 1.1 mrg ::= c # char 2562 1.1 mrg ::= a # signed char 2563 1.1 mrg ::= h # unsigned char 2564 1.1 mrg ::= s # short 2565 1.1 mrg ::= t # unsigned short 2566 1.1 mrg ::= i # int 2567 1.1 mrg ::= j # unsigned int 2568 1.1 mrg ::= l # long 2569 1.1 mrg ::= m # unsigned long 2570 1.1 mrg ::= x # long long, __int64 2571 1.1 mrg ::= y # unsigned long long, __int64 2572 1.1 mrg ::= n # __int128 2573 1.1 mrg ::= o # unsigned __int128 2574 1.1 mrg ::= f # float 2575 1.1 mrg ::= d # double 2576 1.1 mrg ::= e # long double, __float80 2577 1.1 mrg ::= g # __float128 [not supported] 2578 1.1 mrg ::= u <source-name> # vendor extended type */ 2579 1.1 mrg 2580 1.1 mrg static void 2581 1.1 mrg write_builtin_type (tree type) 2582 1.1 mrg { 2583 1.1 mrg if (TYPE_CANONICAL (type)) 2584 1.1 mrg type = TYPE_CANONICAL (type); 2585 1.1 mrg 2586 1.1 mrg switch (TREE_CODE (type)) 2587 1.1 mrg { 2588 1.1 mrg case VOID_TYPE: 2589 1.1 mrg write_char ('v'); 2590 1.1 mrg break; 2591 1.1 mrg 2592 1.1 mrg case BOOLEAN_TYPE: 2593 1.1 mrg write_char ('b'); 2594 1.1 mrg break; 2595 1.1 mrg 2596 1.1 mrg case INTEGER_TYPE: 2597 1.1 mrg /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that 2598 1.1 mrg isn't in integer_type_nodes. */ 2599 1.1 mrg if (type == wchar_type_node) 2600 1.1 mrg write_char ('w'); 2601 1.1 mrg else if (type == char8_type_node) 2602 1.1 mrg write_string ("Du"); 2603 1.1 mrg else if (type == char16_type_node) 2604 1.1 mrg write_string ("Ds"); 2605 1.1 mrg else if (type == char32_type_node) 2606 1.1 mrg write_string ("Di"); 2607 1.1 mrg else 2608 1.1 mrg { 2609 1.1 mrg size_t itk; 2610 1.1 mrg /* Assume TYPE is one of the shared integer type nodes. Find 2611 1.1 mrg it in the array of these nodes. */ 2612 1.1 mrg iagain: 2613 1.1 mrg for (itk = 0; itk < itk_none; ++itk) 2614 1.1 mrg if (integer_types[itk] != NULL_TREE 2615 1.1 mrg && integer_type_codes[itk] != '\0' 2616 1.1 mrg && type == integer_types[itk]) 2617 1.1 mrg { 2618 1.1 mrg /* Print the corresponding single-letter code. */ 2619 1.1 mrg write_char (integer_type_codes[itk]); 2620 1.1 mrg break; 2621 1.1 mrg } 2622 1.1 mrg 2623 1.1 mrg if (itk == itk_none) 2624 1.1 mrg { 2625 1.1 mrg tree t = c_common_type_for_mode (TYPE_MODE (type), 2626 1.1 mrg TYPE_UNSIGNED (type)); 2627 1.1 mrg if (type != t) 2628 1.1 mrg { 2629 1.1 mrg type = t; 2630 1.1 mrg goto iagain; 2631 1.1 mrg } 2632 1.1 mrg 2633 1.1 mrg if (TYPE_PRECISION (type) == 128) 2634 1.1 mrg write_char (TYPE_UNSIGNED (type) ? 'o' : 'n'); 2635 1.1 mrg else 2636 1.1 mrg { 2637 1.1 mrg /* Allow for cases where TYPE is not one of the shared 2638 1.1 mrg integer type nodes and write a "vendor extended builtin 2639 1.1 mrg type" with a name the form intN or uintN, respectively. 2640 1.1 mrg Situations like this can happen if you have an 2641 1.1 mrg __attribute__((__mode__(__SI__))) type and use exotic 2642 1.1 mrg switches like '-mint8' on AVR. Of course, this is 2643 1.1 mrg undefined by the C++ ABI (and '-mint8' is not even 2644 1.1 mrg Standard C conforming), but when using such special 2645 1.1 mrg options you're pretty much in nowhere land anyway. */ 2646 1.1 mrg const char *prefix; 2647 1.1 mrg char prec[11]; /* up to ten digits for an unsigned */ 2648 1.1 mrg 2649 1.1 mrg prefix = TYPE_UNSIGNED (type) ? "uint" : "int"; 2650 1.1 mrg sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type)); 2651 1.1 mrg write_char ('u'); /* "vendor extended builtin type" */ 2652 1.1 mrg write_unsigned_number (strlen (prefix) + strlen (prec)); 2653 1.1 mrg write_string (prefix); 2654 1.1 mrg write_string (prec); 2655 1.1 mrg } 2656 1.1 mrg } 2657 1.1 mrg } 2658 1.1 mrg break; 2659 1.1 mrg 2660 1.1 mrg case REAL_TYPE: 2661 1.1 mrg if (type == float_type_node) 2662 1.1 mrg write_char ('f'); 2663 1.1 mrg else if (type == double_type_node) 2664 1.1 mrg write_char ('d'); 2665 1.1 mrg else if (type == long_double_type_node) 2666 1.1 mrg write_char ('e'); 2667 1.1 mrg else if (type == dfloat32_type_node || type == fallback_dfloat32_type) 2668 1.1 mrg write_string ("Df"); 2669 1.1 mrg else if (type == dfloat64_type_node || type == fallback_dfloat64_type) 2670 1.1 mrg write_string ("Dd"); 2671 1.1 mrg else if (type == dfloat128_type_node || type == fallback_dfloat128_type) 2672 1.1 mrg write_string ("De"); 2673 1.1 mrg else 2674 1.1 mrg gcc_unreachable (); 2675 1.1 mrg break; 2676 1.1 mrg 2677 1.1 mrg case FIXED_POINT_TYPE: 2678 1.1 mrg write_string ("DF"); 2679 1.1 mrg if (GET_MODE_IBIT (TYPE_MODE (type)) > 0) 2680 1.1 mrg write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type))); 2681 1.1 mrg if (type == fract_type_node 2682 1.1 mrg || type == sat_fract_type_node 2683 1.1 mrg || type == accum_type_node 2684 1.1 mrg || type == sat_accum_type_node) 2685 1.1 mrg write_char ('i'); 2686 1.1 mrg else if (type == unsigned_fract_type_node 2687 1.1 mrg || type == sat_unsigned_fract_type_node 2688 1.1 mrg || type == unsigned_accum_type_node 2689 1.1 mrg || type == sat_unsigned_accum_type_node) 2690 1.1 mrg write_char ('j'); 2691 1.1 mrg else if (type == short_fract_type_node 2692 1.1 mrg || type == sat_short_fract_type_node 2693 1.1 mrg || type == short_accum_type_node 2694 1.1 mrg || type == sat_short_accum_type_node) 2695 1.1 mrg write_char ('s'); 2696 1.1 mrg else if (type == unsigned_short_fract_type_node 2697 1.1 mrg || type == sat_unsigned_short_fract_type_node 2698 1.1 mrg || type == unsigned_short_accum_type_node 2699 1.1 mrg || type == sat_unsigned_short_accum_type_node) 2700 1.1 mrg write_char ('t'); 2701 1.1 mrg else if (type == long_fract_type_node 2702 1.1 mrg || type == sat_long_fract_type_node 2703 1.1 mrg || type == long_accum_type_node 2704 1.1 mrg || type == sat_long_accum_type_node) 2705 1.1 mrg write_char ('l'); 2706 1.1 mrg else if (type == unsigned_long_fract_type_node 2707 1.1 mrg || type == sat_unsigned_long_fract_type_node 2708 1.1 mrg || type == unsigned_long_accum_type_node 2709 1.1 mrg || type == sat_unsigned_long_accum_type_node) 2710 1.1 mrg write_char ('m'); 2711 1.1 mrg else if (type == long_long_fract_type_node 2712 1.1 mrg || type == sat_long_long_fract_type_node 2713 1.1 mrg || type == long_long_accum_type_node 2714 1.1 mrg || type == sat_long_long_accum_type_node) 2715 1.1 mrg write_char ('x'); 2716 1.1 mrg else if (type == unsigned_long_long_fract_type_node 2717 1.1 mrg || type == sat_unsigned_long_long_fract_type_node 2718 1.1 mrg || type == unsigned_long_long_accum_type_node 2719 1.1 mrg || type == sat_unsigned_long_long_accum_type_node) 2720 1.1 mrg write_char ('y'); 2721 1.1 mrg else 2722 1.1 mrg sorry ("mangling unknown fixed point type"); 2723 1.1 mrg write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type))); 2724 1.1 mrg if (TYPE_SATURATING (type)) 2725 1.1 mrg write_char ('s'); 2726 1.1 mrg else 2727 1.1 mrg write_char ('n'); 2728 1.1 mrg break; 2729 1.1 mrg 2730 1.1 mrg default: 2731 1.1 mrg gcc_unreachable (); 2732 1.1 mrg } 2733 1.1 mrg } 2734 1.1 mrg 2735 1.1 mrg /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or 2736 1.1 mrg METHOD_TYPE. The return type is mangled before the parameter 2737 1.1 mrg types. 2738 1.1 mrg 2739 1.1 mrg <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */ 2740 1.1 mrg 2741 1.1 mrg static void 2742 1.1 mrg write_function_type (const tree type) 2743 1.1 mrg { 2744 1.1 mrg MANGLE_TRACE_TREE ("function-type", type); 2745 1.1 mrg 2746 1.1 mrg /* For a pointer to member function, the function type may have 2747 1.1 mrg cv-qualifiers, indicating the quals for the artificial 'this' 2748 1.1 mrg parameter. */ 2749 1.1 mrg if (TREE_CODE (type) == METHOD_TYPE) 2750 1.1 mrg { 2751 1.1 mrg /* The first parameter must be a POINTER_TYPE pointing to the 2752 1.1 mrg `this' parameter. */ 2753 1.1 mrg tree this_type = class_of_this_parm (type); 2754 1.1 mrg write_CV_qualifiers_for_type (this_type); 2755 1.1 mrg } 2756 1.1 mrg 2757 1.1 mrg write_exception_spec (TYPE_RAISES_EXCEPTIONS (type)); 2758 1.1 mrg 2759 1.1 mrg if (tx_safe_fn_type_p (type)) 2760 1.1 mrg write_string ("Dx"); 2761 1.1 mrg 2762 1.1 mrg write_char ('F'); 2763 1.1 mrg /* We don't track whether or not a type is `extern "C"'. Note that 2764 1.1 mrg you can have an `extern "C"' function that does not have 2765 1.1 mrg `extern "C"' type, and vice versa: 2766 1.1 mrg 2767 1.1 mrg extern "C" typedef void function_t(); 2768 1.1 mrg function_t f; // f has C++ linkage, but its type is 2769 1.1 mrg // `extern "C"' 2770 1.1 mrg 2771 1.1 mrg typedef void function_t(); 2772 1.1 mrg extern "C" function_t f; // Vice versa. 2773 1.1 mrg 2774 1.1 mrg See [dcl.link]. */ 2775 1.1 mrg write_bare_function_type (type, /*include_return_type_p=*/1, 2776 1.1 mrg /*decl=*/NULL); 2777 1.1 mrg if (FUNCTION_REF_QUALIFIED (type)) 2778 1.1 mrg { 2779 1.1 mrg if (FUNCTION_RVALUE_QUALIFIED (type)) 2780 1.1 mrg write_char ('O'); 2781 1.1 mrg else 2782 1.1 mrg write_char ('R'); 2783 1.1 mrg } 2784 1.1 mrg write_char ('E'); 2785 1.1 mrg } 2786 1.1 mrg 2787 1.1 mrg /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or 2788 1.1 mrg METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value 2789 1.1 mrg is mangled before the parameter types. If non-NULL, DECL is 2790 1.1 mrg FUNCTION_DECL for the function whose type is being emitted. */ 2791 1.1 mrg 2792 1.1 mrg static void 2793 1.1 mrg write_bare_function_type (const tree type, const int include_return_type_p, 2794 1.1 mrg const tree decl) 2795 1.1 mrg { 2796 1.1 mrg MANGLE_TRACE_TREE ("bare-function-type", type); 2797 1.1 mrg 2798 1.1 mrg /* Mangle the return type, if requested. */ 2799 1.1 mrg if (include_return_type_p) 2800 1.1 mrg write_type (TREE_TYPE (type)); 2801 1.1 mrg 2802 1.1 mrg /* Now mangle the types of the arguments. */ 2803 1.1 mrg ++G.parm_depth; 2804 1.1 mrg write_method_parms (TYPE_ARG_TYPES (type), 2805 1.1 mrg TREE_CODE (type) == METHOD_TYPE, 2806 1.1 mrg decl); 2807 1.1 mrg --G.parm_depth; 2808 1.1 mrg } 2809 1.1 mrg 2810 1.1 mrg /* Write the mangled representation of a method parameter list of 2811 1.1 mrg types given in PARM_TYPES. If METHOD_P is nonzero, the function is 2812 1.1 mrg considered a non-static method, and the this parameter is omitted. 2813 1.1 mrg If non-NULL, DECL is the FUNCTION_DECL for the function whose 2814 1.1 mrg parameters are being emitted. */ 2815 1.1 mrg 2816 1.1 mrg static void 2817 1.1 mrg write_method_parms (tree parm_types, const int method_p, const tree decl) 2818 1.1 mrg { 2819 1.1 mrg tree first_parm_type; 2820 1.1 mrg tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE; 2821 1.1 mrg 2822 1.1 mrg /* Assume this parameter type list is variable-length. If it ends 2823 1.1 mrg with a void type, then it's not. */ 2824 1.1 mrg int varargs_p = 1; 2825 1.1 mrg 2826 1.1 mrg /* If this is a member function, skip the first arg, which is the 2827 1.1 mrg this pointer. 2828 1.1 mrg "Member functions do not encode the type of their implicit this 2829 1.1 mrg parameter." 2830 1.1 mrg 2831 1.1 mrg Similarly, there's no need to mangle artificial parameters, like 2832 1.1 mrg the VTT parameters for constructors and destructors. */ 2833 1.1 mrg if (method_p) 2834 1.1 mrg { 2835 1.1 mrg parm_types = TREE_CHAIN (parm_types); 2836 1.1 mrg parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE; 2837 1.1 mrg 2838 1.1 mrg while (parm_decl && DECL_ARTIFICIAL (parm_decl)) 2839 1.1 mrg { 2840 1.1 mrg parm_types = TREE_CHAIN (parm_types); 2841 1.1 mrg parm_decl = DECL_CHAIN (parm_decl); 2842 1.1 mrg } 2843 1.1 mrg 2844 1.1 mrg if (decl && ctor_omit_inherited_parms (decl)) 2845 1.1 mrg /* Bring back parameters omitted from an inherited ctor. */ 2846 1.1 mrg parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl)); 2847 1.1 mrg } 2848 1.1 mrg 2849 1.1 mrg for (first_parm_type = parm_types; 2850 1.1 mrg parm_types; 2851 1.1 mrg parm_types = TREE_CHAIN (parm_types)) 2852 1.1 mrg { 2853 1.1 mrg tree parm = TREE_VALUE (parm_types); 2854 1.1 mrg if (parm == void_type_node) 2855 1.1 mrg { 2856 1.1 mrg /* "Empty parameter lists, whether declared as () or 2857 1.1 mrg conventionally as (void), are encoded with a void parameter 2858 1.1 mrg (v)." */ 2859 1.1 mrg if (parm_types == first_parm_type) 2860 1.1 mrg write_type (parm); 2861 1.1 mrg /* If the parm list is terminated with a void type, it's 2862 1.1 mrg fixed-length. */ 2863 1.1 mrg varargs_p = 0; 2864 1.1 mrg /* A void type better be the last one. */ 2865 1.1 mrg gcc_assert (TREE_CHAIN (parm_types) == NULL); 2866 1.1 mrg } 2867 1.1 mrg else 2868 1.1 mrg write_type (parm); 2869 1.1 mrg } 2870 1.1 mrg 2871 1.1 mrg if (varargs_p) 2872 1.1 mrg /* <builtin-type> ::= z # ellipsis */ 2873 1.1 mrg write_char ('z'); 2874 1.1 mrg } 2875 1.1 mrg 2876 1.1 mrg /* <class-enum-type> ::= <name> */ 2877 1.1 mrg 2878 1.1 mrg static void 2879 1.1 mrg write_class_enum_type (const tree type) 2880 1.1 mrg { 2881 1.1 mrg write_name (TYPE_NAME (type), /*ignore_local_scope=*/0); 2882 1.1 mrg } 2883 1.1 mrg 2884 1.1 mrg /* Non-terminal <template-args>. ARGS is a TREE_VEC of template 2885 1.1 mrg arguments. 2886 1.1 mrg 2887 1.1 mrg <template-args> ::= I <template-arg>* E */ 2888 1.1 mrg 2889 1.1 mrg static void 2890 1.1 mrg write_template_args (tree args) 2891 1.1 mrg { 2892 1.1 mrg int i; 2893 1.1 mrg int length = 0; 2894 1.1 mrg 2895 1.1 mrg MANGLE_TRACE_TREE ("template-args", args); 2896 1.1 mrg 2897 1.1 mrg write_char ('I'); 2898 1.1 mrg 2899 1.1 mrg if (args) 2900 1.1 mrg length = TREE_VEC_LENGTH (args); 2901 1.1 mrg 2902 1.1 mrg if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC) 2903 1.1 mrg { 2904 1.1 mrg /* We have nested template args. We want the innermost template 2905 1.1 mrg argument list. */ 2906 1.1 mrg args = TREE_VEC_ELT (args, length - 1); 2907 1.1 mrg length = TREE_VEC_LENGTH (args); 2908 1.1 mrg } 2909 1.1 mrg for (i = 0; i < length; ++i) 2910 1.1 mrg write_template_arg (TREE_VEC_ELT (args, i)); 2911 1.1 mrg 2912 1.1 mrg write_char ('E'); 2913 1.1 mrg } 2914 1.1 mrg 2915 1.1 mrg /* Write out the 2916 1.1 mrg <unqualified-name> 2917 1.1 mrg <unqualified-name> <template-args> 2918 1.1 mrg part of SCOPE_REF or COMPONENT_REF mangling. */ 2919 1.1 mrg 2920 1.1 mrg static void 2921 1.1 mrg write_member_name (tree member) 2922 1.1 mrg { 2923 1.1 mrg if (identifier_p (member)) 2924 1.1 mrg { 2925 1.1 mrg if (IDENTIFIER_ANY_OP_P (member)) 2926 1.1 mrg { 2927 1.1 mrg if (abi_version_at_least (11)) 2928 1.1 mrg write_string ("on"); 2929 1.1 mrg if (abi_warn_or_compat_version_crosses (11)) 2930 1.1 mrg G.need_abi_warning = 1; 2931 1.1 mrg } 2932 1.1 mrg write_unqualified_id (member); 2933 1.1 mrg } 2934 1.1 mrg else if (DECL_P (member)) 2935 1.1 mrg { 2936 1.1 mrg gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member)); 2937 1.1 mrg write_unqualified_name (member); 2938 1.1 mrg } 2939 1.1 mrg else if (TREE_CODE (member) == TEMPLATE_ID_EXPR) 2940 1.1 mrg { 2941 1.1 mrg tree name = TREE_OPERAND (member, 0); 2942 1.1 mrg name = OVL_FIRST (name); 2943 1.1 mrg write_member_name (name); 2944 1.1 mrg write_template_args (TREE_OPERAND (member, 1)); 2945 1.1 mrg } 2946 1.1 mrg else 2947 1.1 mrg write_expression (member); 2948 1.1 mrg } 2949 1.1 mrg 2950 1.1 mrg /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for 2951 1.1 mrg converting to BASE, or just the conversion of EXPR if BASE is null. 2952 1.1 mrg 2953 1.1 mrg "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base 2954 1.1 mrg path Min(P) is defined as follows: let C_i be the last element for which the 2955 1.1 mrg conversion to C_0 is unambiguous; if that element is C_n, the minimized path 2956 1.1 mrg is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) -> 2957 1.1 mrg C_0." 2958 1.1 mrg 2959 1.1 mrg We mangle the conversion to C_i if it's different from C_n. */ 2960 1.1 mrg 2961 1.1 mrg static bool 2962 1.1 mrg write_base_ref (tree expr, tree base = NULL_TREE) 2963 1.1 mrg { 2964 1.1 mrg if (TREE_CODE (expr) != COMPONENT_REF) 2965 1.1 mrg return false; 2966 1.1 mrg 2967 1.1 mrg tree field = TREE_OPERAND (expr, 1); 2968 1.1 mrg 2969 1.1 mrg if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field)) 2970 1.1 mrg return false; 2971 1.1 mrg 2972 1.1 mrg tree object = TREE_OPERAND (expr, 0); 2973 1.1 mrg 2974 1.1 mrg tree binfo = NULL_TREE; 2975 1.1 mrg if (base) 2976 1.1 mrg { 2977 1.1 mrg tree cur = TREE_TYPE (object); 2978 1.1 mrg binfo = lookup_base (cur, base, ba_unique, NULL, tf_none); 2979 1.1 mrg } 2980 1.1 mrg else 2981 1.1 mrg /* We're at the end of the base conversion chain, so it can't be 2982 1.1 mrg ambiguous. */ 2983 1.1 mrg base = TREE_TYPE (field); 2984 1.1 mrg 2985 1.1 mrg if (binfo == error_mark_node) 2986 1.1 mrg { 2987 1.1 mrg /* cur->base is ambiguous, so make the conversion to 2988 1.1 mrg last explicit, expressed as a cast (last&)object. */ 2989 1.1 mrg tree last = TREE_TYPE (expr); 2990 1.1 mrg write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name); 2991 1.1 mrg write_type (build_reference_type (last)); 2992 1.1 mrg write_expression (object); 2993 1.1 mrg } 2994 1.1 mrg else if (write_base_ref (object, base)) 2995 1.1 mrg /* cur->base is unambiguous, but we had another base conversion 2996 1.1 mrg underneath and wrote it out. */; 2997 1.1 mrg else 2998 1.1 mrg /* No more base conversions, just write out the object. */ 2999 1.1 mrg write_expression (object); 3000 1.1 mrg 3001 1.1 mrg return true; 3002 1.1 mrg } 3003 1.1 mrg 3004 1.1 mrg /* The number of elements spanned by a RANGE_EXPR. */ 3005 1.1 mrg 3006 1.1 mrg unsigned HOST_WIDE_INT 3007 1.1 mrg range_expr_nelts (tree expr) 3008 1.1 mrg { 3009 1.1 mrg tree lo = TREE_OPERAND (expr, 0); 3010 1.1 mrg tree hi = TREE_OPERAND (expr, 1); 3011 1.1 mrg return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1; 3012 1.1 mrg } 3013 1.1 mrg 3014 1.1 mrg /* <expression> ::= <unary operator-name> <expression> 3015 1.1 mrg ::= <binary operator-name> <expression> <expression> 3016 1.1 mrg ::= <expr-primary> 3017 1.1 mrg 3018 1.1 mrg <expr-primary> ::= <template-param> 3019 1.1 mrg ::= L <type> <value number> E # literal 3020 1.1 mrg ::= L <mangled-name> E # external name 3021 1.1 mrg ::= st <type> # sizeof 3022 1.1 mrg ::= sr <type> <unqualified-name> # dependent name 3023 1.1 mrg ::= sr <type> <unqualified-name> <template-args> */ 3024 1.1 mrg 3025 1.1 mrg static void 3026 1.1 mrg write_expression (tree expr) 3027 1.1 mrg { 3028 1.1 mrg enum tree_code code = TREE_CODE (expr); 3029 1.1 mrg 3030 1.1 mrg if (TREE_CODE (expr) == TARGET_EXPR) 3031 1.1 mrg { 3032 1.1 mrg expr = TARGET_EXPR_INITIAL (expr); 3033 1.1 mrg code = TREE_CODE (expr); 3034 1.1 mrg } 3035 1.1 mrg 3036 1.1 mrg /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer 3037 1.1 mrg argument is converted (via qualification conversions) to another type. */ 3038 1.1 mrg while (CONVERT_EXPR_CODE_P (code) 3039 1.1 mrg || code == IMPLICIT_CONV_EXPR 3040 1.1 mrg || location_wrapper_p (expr) 3041 1.1 mrg /* Parentheses aren't mangled. */ 3042 1.1 mrg || code == PAREN_EXPR 3043 1.1 mrg || code == NON_LVALUE_EXPR 3044 1.1 mrg || (code == VIEW_CONVERT_EXPR 3045 1.1 mrg && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX)) 3046 1.1 mrg { 3047 1.1 mrg expr = TREE_OPERAND (expr, 0); 3048 1.1 mrg code = TREE_CODE (expr); 3049 1.1 mrg } 3050 1.1 mrg 3051 1.1 mrg if (code == BASELINK 3052 1.1 mrg && (!type_unknown_p (expr) 3053 1.1 mrg || !BASELINK_QUALIFIED_P (expr))) 3054 1.1 mrg { 3055 1.1 mrg expr = BASELINK_FUNCTIONS (expr); 3056 1.1 mrg code = TREE_CODE (expr); 3057 1.1 mrg } 3058 1.1 mrg 3059 1.1 mrg /* Handle pointers-to-members by making them look like expression 3060 1.1 mrg nodes. */ 3061 1.1 mrg if (code == PTRMEM_CST) 3062 1.1 mrg { 3063 1.1 mrg expr = build_nt (ADDR_EXPR, 3064 1.1 mrg build_qualified_name (/*type=*/NULL_TREE, 3065 1.1 mrg PTRMEM_CST_CLASS (expr), 3066 1.1 mrg PTRMEM_CST_MEMBER (expr), 3067 1.1 mrg /*template_p=*/false)); 3068 1.1 mrg code = TREE_CODE (expr); 3069 1.1 mrg } 3070 1.1 mrg 3071 1.1 mrg /* Handle template parameters. */ 3072 1.1 mrg if (code == TEMPLATE_TYPE_PARM 3073 1.1 mrg || code == TEMPLATE_TEMPLATE_PARM 3074 1.1 mrg || code == BOUND_TEMPLATE_TEMPLATE_PARM 3075 1.1 mrg || code == TEMPLATE_PARM_INDEX) 3076 1.1 mrg write_template_param (expr); 3077 1.1 mrg /* Handle literals. */ 3078 1.1 mrg else if (TREE_CODE_CLASS (code) == tcc_constant 3079 1.1 mrg || code == CONST_DECL) 3080 1.1 mrg write_template_arg_literal (expr); 3081 1.1 mrg else if (code == PARM_DECL && DECL_ARTIFICIAL (expr)) 3082 1.1 mrg { 3083 1.1 mrg gcc_assert (id_equal (DECL_NAME (expr), "this")); 3084 1.1 mrg write_string ("fpT"); 3085 1.1 mrg } 3086 1.1 mrg else if (code == PARM_DECL) 3087 1.1 mrg { 3088 1.1 mrg /* A function parameter used in a late-specified return type. */ 3089 1.1 mrg int index = DECL_PARM_INDEX (expr); 3090 1.1 mrg int level = DECL_PARM_LEVEL (expr); 3091 1.1 mrg int delta = G.parm_depth - level + 1; 3092 1.1 mrg gcc_assert (index >= 1); 3093 1.1 mrg write_char ('f'); 3094 1.1 mrg if (delta != 0) 3095 1.1 mrg { 3096 1.1 mrg if (abi_version_at_least (5)) 3097 1.1 mrg { 3098 1.1 mrg /* Let L be the number of function prototype scopes from the 3099 1.1 mrg innermost one (in which the parameter reference occurs) up 3100 1.1 mrg to (and including) the one containing the declaration of 3101 1.1 mrg the referenced parameter. If the parameter declaration 3102 1.1 mrg clause of the innermost function prototype scope has been 3103 1.1 mrg completely seen, it is not counted (in that case -- which 3104 1.1 mrg is perhaps the most common -- L can be zero). */ 3105 1.1 mrg write_char ('L'); 3106 1.1 mrg write_unsigned_number (delta - 1); 3107 1.1 mrg } 3108 1.1 mrg if (abi_warn_or_compat_version_crosses (5)) 3109 1.1 mrg G.need_abi_warning = true; 3110 1.1 mrg } 3111 1.1 mrg write_char ('p'); 3112 1.1 mrg write_compact_number (index - 1); 3113 1.1 mrg } 3114 1.1 mrg else if (DECL_P (expr)) 3115 1.1 mrg { 3116 1.1 mrg write_char ('L'); 3117 1.1 mrg write_mangled_name (expr, false); 3118 1.1 mrg write_char ('E'); 3119 1.1 mrg } 3120 1.1 mrg else if (TREE_CODE (expr) == SIZEOF_EXPR) 3121 1.1 mrg { 3122 1.1 mrg tree op = TREE_OPERAND (expr, 0); 3123 1.1 mrg 3124 1.1 mrg if (PACK_EXPANSION_P (op)) 3125 1.1 mrg { 3126 1.1 mrg if (abi_warn_or_compat_version_crosses (11)) 3127 1.1 mrg G.need_abi_warning = true; 3128 1.1 mrg if (abi_version_at_least (11)) 3129 1.1 mrg { 3130 1.1 mrg /* sZ rather than szDp. */ 3131 1.1 mrg write_string ("sZ"); 3132 1.1 mrg write_expression (PACK_EXPANSION_PATTERN (op)); 3133 1.1 mrg return; 3134 1.1 mrg } 3135 1.1 mrg } 3136 1.1 mrg 3137 1.1 mrg if (SIZEOF_EXPR_TYPE_P (expr)) 3138 1.1 mrg { 3139 1.1 mrg write_string ("st"); 3140 1.1 mrg write_type (TREE_TYPE (op)); 3141 1.1 mrg } 3142 1.1 mrg else if (ARGUMENT_PACK_P (op)) 3143 1.1 mrg { 3144 1.1 mrg tree args = ARGUMENT_PACK_ARGS (op); 3145 1.1 mrg int length = TREE_VEC_LENGTH (args); 3146 1.1 mrg if (abi_warn_or_compat_version_crosses (10)) 3147 1.1 mrg G.need_abi_warning = true; 3148 1.1 mrg if (abi_version_at_least (10)) 3149 1.1 mrg { 3150 1.1 mrg /* sP <template-arg>* E # sizeof...(T), size of a captured 3151 1.1 mrg template parameter pack from an alias template */ 3152 1.1 mrg write_string ("sP"); 3153 1.1 mrg for (int i = 0; i < length; ++i) 3154 1.1 mrg write_template_arg (TREE_VEC_ELT (args, i)); 3155 1.1 mrg write_char ('E'); 3156 1.1 mrg } 3157 1.1 mrg else 3158 1.1 mrg { 3159 1.1 mrg /* In GCC 5 we represented this sizeof wrong, with the effect 3160 1.1 mrg that we mangled it as the last element of the pack. */ 3161 1.1 mrg tree arg = TREE_VEC_ELT (args, length-1); 3162 1.1 mrg if (TYPE_P (op)) 3163 1.1 mrg { 3164 1.1 mrg write_string ("st"); 3165 1.1 mrg write_type (arg); 3166 1.1 mrg } 3167 1.1 mrg else 3168 1.1 mrg { 3169 1.1 mrg write_string ("sz"); 3170 1.1 mrg write_expression (arg); 3171 1.1 mrg } 3172 1.1 mrg } 3173 1.1 mrg } 3174 1.1 mrg else if (TYPE_P (TREE_OPERAND (expr, 0))) 3175 1.1 mrg { 3176 1.1 mrg write_string ("st"); 3177 1.1 mrg write_type (TREE_OPERAND (expr, 0)); 3178 1.1 mrg } 3179 1.1 mrg else 3180 1.1 mrg goto normal_expr; 3181 1.1 mrg } 3182 1.1 mrg else if (TREE_CODE (expr) == ALIGNOF_EXPR) 3183 1.1 mrg { 3184 1.1 mrg if (!ALIGNOF_EXPR_STD_P (expr)) 3185 1.1 mrg { 3186 1.1 mrg if (abi_warn_or_compat_version_crosses (16)) 3187 1.1 mrg G.need_abi_warning = true; 3188 1.1 mrg if (abi_version_at_least (16)) 3189 1.1 mrg { 3190 1.1 mrg /* We used to mangle __alignof__ like alignof. */ 3191 1.1 mrg write_string ("u11__alignof__"); 3192 1.1 mrg write_template_arg (TREE_OPERAND (expr, 0)); 3193 1.1 mrg write_char ('E'); 3194 1.1 mrg return; 3195 1.1 mrg } 3196 1.1 mrg } 3197 1.1 mrg if (TYPE_P (TREE_OPERAND (expr, 0))) 3198 1.1 mrg { 3199 1.1 mrg write_string ("at"); 3200 1.1 mrg write_type (TREE_OPERAND (expr, 0)); 3201 1.1 mrg } 3202 1.1 mrg else 3203 1.1 mrg goto normal_expr; 3204 1.1 mrg } 3205 1.1 mrg else if (code == SCOPE_REF 3206 1.1 mrg || code == BASELINK) 3207 1.1 mrg { 3208 1.1 mrg tree scope, member; 3209 1.1 mrg if (code == SCOPE_REF) 3210 1.1 mrg { 3211 1.1 mrg scope = TREE_OPERAND (expr, 0); 3212 1.1 mrg member = TREE_OPERAND (expr, 1); 3213 1.1 mrg if (BASELINK_P (member)) 3214 1.1 mrg member = BASELINK_FUNCTIONS (member); 3215 1.1 mrg } 3216 1.1 mrg else 3217 1.1 mrg { 3218 1.1 mrg scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr)); 3219 1.1 mrg member = BASELINK_FUNCTIONS (expr); 3220 1.1 mrg } 3221 1.1 mrg 3222 1.1 mrg /* If the MEMBER is a real declaration, then the qualifying 3223 1.1 mrg scope was not dependent. Ideally, we would not have a 3224 1.1 mrg SCOPE_REF in those cases, but sometimes we do. If the second 3225 1.1 mrg argument is a DECL, then the name must not have been 3226 1.1 mrg dependent. */ 3227 1.1 mrg if (DECL_P (member)) 3228 1.1 mrg write_expression (member); 3229 1.1 mrg else 3230 1.1 mrg { 3231 1.1 mrg gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr)); 3232 1.1 mrg write_string ("sr"); 3233 1.1 mrg write_type (scope); 3234 1.1 mrg write_member_name (member); 3235 1.1 mrg } 3236 1.1 mrg } 3237 1.1 mrg else if (INDIRECT_REF_P (expr) 3238 1.1 mrg && TREE_TYPE (TREE_OPERAND (expr, 0)) 3239 1.1 mrg && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0)))) 3240 1.1 mrg { 3241 1.1 mrg write_expression (TREE_OPERAND (expr, 0)); 3242 1.1 mrg } 3243 1.1 mrg else if (identifier_p (expr)) 3244 1.1 mrg { 3245 1.1 mrg /* An operator name appearing as a dependent name needs to be 3246 1.1 mrg specially marked to disambiguate between a use of the operator 3247 1.1 mrg name and a use of the operator in an expression. */ 3248 1.1 mrg if (IDENTIFIER_ANY_OP_P (expr)) 3249 1.1 mrg write_string ("on"); 3250 1.1 mrg write_unqualified_id (expr); 3251 1.1 mrg } 3252 1.1 mrg else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) 3253 1.1 mrg { 3254 1.1 mrg tree fn = TREE_OPERAND (expr, 0); 3255 1.1 mrg fn = OVL_NAME (fn); 3256 1.1 mrg if (IDENTIFIER_ANY_OP_P (fn)) 3257 1.1 mrg write_string ("on"); 3258 1.1 mrg write_unqualified_id (fn); 3259 1.1 mrg write_template_args (TREE_OPERAND (expr, 1)); 3260 1.1 mrg } 3261 1.1 mrg else if (TREE_CODE (expr) == MODOP_EXPR) 3262 1.1 mrg { 3263 1.1 mrg enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1)); 3264 1.1 mrg const char *name = OVL_OP_INFO (true, subop)->mangled_name; 3265 1.1 mrg 3266 1.1 mrg write_string (name); 3267 1.1 mrg write_expression (TREE_OPERAND (expr, 0)); 3268 1.1 mrg write_expression (TREE_OPERAND (expr, 2)); 3269 1.1 mrg } 3270 1.1 mrg else if (code == NEW_EXPR || code == VEC_NEW_EXPR) 3271 1.1 mrg { 3272 1.1 mrg /* ::= [gs] nw <expression>* _ <type> E 3273 1.1 mrg ::= [gs] nw <expression>* _ <type> <initializer> 3274 1.1 mrg ::= [gs] na <expression>* _ <type> E 3275 1.1 mrg ::= [gs] na <expression>* _ <type> <initializer> 3276 1.1 mrg <initializer> ::= pi <expression>* E */ 3277 1.1 mrg tree placement = TREE_OPERAND (expr, 0); 3278 1.1 mrg tree type = TREE_OPERAND (expr, 1); 3279 1.1 mrg tree nelts = TREE_OPERAND (expr, 2); 3280 1.1 mrg tree init = TREE_OPERAND (expr, 3); 3281 1.1 mrg tree t; 3282 1.1 mrg 3283 1.1 mrg gcc_assert (code == NEW_EXPR); 3284 1.1 mrg if (TREE_OPERAND (expr, 2)) 3285 1.1 mrg code = VEC_NEW_EXPR; 3286 1.1 mrg 3287 1.1 mrg if (NEW_EXPR_USE_GLOBAL (expr)) 3288 1.1 mrg write_string ("gs"); 3289 1.1 mrg 3290 1.1 mrg write_string (OVL_OP_INFO (false, code)->mangled_name); 3291 1.1 mrg 3292 1.1 mrg for (t = placement; t; t = TREE_CHAIN (t)) 3293 1.1 mrg write_expression (TREE_VALUE (t)); 3294 1.1 mrg 3295 1.1 mrg write_char ('_'); 3296 1.1 mrg 3297 1.1 mrg if (nelts) 3298 1.1 mrg { 3299 1.1 mrg tree domain; 3300 1.1 mrg ++processing_template_decl; 3301 1.1 mrg domain = compute_array_index_type (NULL_TREE, nelts, 3302 1.1 mrg tf_warning_or_error); 3303 1.1 mrg type = build_cplus_array_type (type, domain); 3304 1.1 mrg --processing_template_decl; 3305 1.1 mrg } 3306 1.1 mrg write_type (type); 3307 1.1 mrg 3308 1.1 mrg if (init && TREE_CODE (init) == TREE_LIST 3309 1.1 mrg && DIRECT_LIST_INIT_P (TREE_VALUE (init))) 3310 1.1 mrg write_expression (TREE_VALUE (init)); 3311 1.1 mrg else 3312 1.1 mrg { 3313 1.1 mrg if (init) 3314 1.1 mrg write_string ("pi"); 3315 1.1 mrg if (init && init != void_node) 3316 1.1 mrg for (t = init; t; t = TREE_CHAIN (t)) 3317 1.1 mrg write_expression (TREE_VALUE (t)); 3318 1.1 mrg write_char ('E'); 3319 1.1 mrg } 3320 1.1 mrg } 3321 1.1 mrg else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR) 3322 1.1 mrg { 3323 1.1 mrg gcc_assert (code == DELETE_EXPR); 3324 1.1 mrg if (DELETE_EXPR_USE_VEC (expr)) 3325 1.1 mrg code = VEC_DELETE_EXPR; 3326 1.1 mrg 3327 1.1 mrg if (DELETE_EXPR_USE_GLOBAL (expr)) 3328 1.1 mrg write_string ("gs"); 3329 1.1 mrg 3330 1.1 mrg write_string (OVL_OP_INFO (false, code)->mangled_name); 3331 1.1 mrg 3332 1.1 mrg write_expression (TREE_OPERAND (expr, 0)); 3333 1.1 mrg } 3334 1.1 mrg else if (code == THROW_EXPR) 3335 1.1 mrg { 3336 1.1 mrg tree op = TREE_OPERAND (expr, 0); 3337 1.1 mrg if (op) 3338 1.1 mrg { 3339 1.1 mrg write_string ("tw"); 3340 1.1 mrg write_expression (op); 3341 1.1 mrg } 3342 1.1 mrg else 3343 1.1 mrg write_string ("tr"); 3344 1.1 mrg } 3345 1.1 mrg else if (code == CONSTRUCTOR) 3346 1.1 mrg { 3347 1.1 mrg bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr); 3348 1.1 mrg tree etype = TREE_TYPE (expr); 3349 1.1 mrg 3350 1.1 mrg if (braced_init) 3351 1.1 mrg write_string ("il"); 3352 1.1 mrg else 3353 1.1 mrg { 3354 1.1 mrg write_string ("tl"); 3355 1.1 mrg write_type (etype); 3356 1.1 mrg } 3357 1.1 mrg 3358 1.1 mrg /* If this is an undigested initializer, mangle it as written. 3359 1.1 mrg COMPOUND_LITERAL_P doesn't actually distinguish between digested and 3360 1.1 mrg undigested braced casts, but it should work to use it to distinguish 3361 1.1 mrg between braced casts in a template signature (undigested) and template 3362 1.1 mrg parm object values (digested), and all CONSTRUCTORS that get here 3363 1.1 mrg should be one of those two cases. */ 3364 1.1 mrg bool undigested = braced_init || COMPOUND_LITERAL_P (expr); 3365 1.1 mrg if (undigested || !zero_init_expr_p (expr)) 3366 1.1 mrg { 3367 1.1 mrg /* Convert braced initializer lists to STRING_CSTs so that 3368 1.1 mrg A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while 3369 1.1 mrg still using the latter mangling for strings that 3370 1.1 mrg originated as braced initializer lists. */ 3371 1.1 mrg expr = braced_lists_to_strings (etype, expr); 3372 1.1 mrg 3373 1.1 mrg if (TREE_CODE (expr) == CONSTRUCTOR) 3374 1.1 mrg { 3375 1.1 mrg vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr); 3376 1.1 mrg unsigned last_nonzero = UINT_MAX; 3377 1.1 mrg constructor_elt *ce; 3378 1.1 mrg 3379 1.1 mrg if (!undigested) 3380 1.1 mrg for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) 3381 1.1 mrg if ((TREE_CODE (etype) == UNION_TYPE 3382 1.1 mrg && ce->index != first_field (etype)) 3383 1.1 mrg || !zero_init_expr_p (ce->value)) 3384 1.1 mrg last_nonzero = i; 3385 1.1 mrg 3386 1.1 mrg if (undigested || last_nonzero != UINT_MAX) 3387 1.1 mrg for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) 3388 1.1 mrg { 3389 1.1 mrg if (i > last_nonzero) 3390 1.1 mrg break; 3391 1.1 mrg if (!undigested && TREE_CODE (etype) == UNION_TYPE) 3392 1.1 mrg { 3393 1.1 mrg /* Express the active member as a designator. */ 3394 1.1 mrg write_string ("di"); 3395 1.1 mrg write_unqualified_name (ce->index); 3396 1.1 mrg } 3397 1.1 mrg unsigned reps = 1; 3398 1.1 mrg if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR) 3399 1.1 mrg reps = range_expr_nelts (ce->index); 3400 1.1 mrg for (unsigned j = 0; j < reps; ++j) 3401 1.1 mrg write_expression (ce->value); 3402 1.1 mrg } 3403 1.1 mrg } 3404 1.1 mrg else 3405 1.1 mrg { 3406 1.1 mrg gcc_assert (TREE_CODE (expr) == STRING_CST); 3407 1.1 mrg write_expression (expr); 3408 1.1 mrg } 3409 1.1 mrg } 3410 1.1 mrg write_char ('E'); 3411 1.1 mrg } 3412 1.1 mrg else if (code == LAMBDA_EXPR) 3413 1.1 mrg { 3414 1.1 mrg /* [temp.over.link] Two lambda-expressions are never considered 3415 1.1 mrg equivalent. 3416 1.1 mrg 3417 1.1 mrg So just use the closure type mangling. */ 3418 1.1 mrg write_string ("tl"); 3419 1.1 mrg write_type (LAMBDA_EXPR_CLOSURE (expr)); 3420 1.1 mrg write_char ('E'); 3421 1.1 mrg } 3422 1.1 mrg else if (dependent_name (expr)) 3423 1.1 mrg { 3424 1.1 mrg tree name = dependent_name (expr); 3425 1.1 mrg if (IDENTIFIER_ANY_OP_P (name)) 3426 1.1 mrg { 3427 1.1 mrg if (abi_version_at_least (16)) 3428 1.1 mrg write_string ("on"); 3429 1.1 mrg if (abi_warn_or_compat_version_crosses (16)) 3430 1.1 mrg G.need_abi_warning = 1; 3431 1.1 mrg } 3432 1.1 mrg write_unqualified_id (name); 3433 1.1 mrg } 3434 1.1 mrg else 3435 1.1 mrg { 3436 1.1 mrg normal_expr: 3437 1.1 mrg int i, len; 3438 1.1 mrg const char *name; 3439 1.1 mrg 3440 1.1 mrg /* When we bind a variable or function to a non-type template 3441 1.1 mrg argument with reference type, we create an ADDR_EXPR to show 3442 1.1 mrg the fact that the entity's address has been taken. But, we 3443 1.1 mrg don't actually want to output a mangling code for the `&'. */ 3444 1.1 mrg if (TREE_CODE (expr) == ADDR_EXPR 3445 1.1 mrg && TREE_TYPE (expr) 3446 1.1 mrg && TYPE_REF_P (TREE_TYPE (expr))) 3447 1.1 mrg { 3448 1.1 mrg expr = TREE_OPERAND (expr, 0); 3449 1.1 mrg if (DECL_P (expr)) 3450 1.1 mrg { 3451 1.1 mrg write_expression (expr); 3452 1.1 mrg return; 3453 1.1 mrg } 3454 1.1 mrg 3455 1.1 mrg code = TREE_CODE (expr); 3456 1.1 mrg } 3457 1.1 mrg 3458 1.1 mrg if (code == COMPONENT_REF) 3459 1.1 mrg { 3460 1.1 mrg tree ob = TREE_OPERAND (expr, 0); 3461 1.1 mrg 3462 1.1 mrg if (TREE_CODE (ob) == ARROW_EXPR) 3463 1.1 mrg { 3464 1.1 mrg write_string (OVL_OP_INFO (false, code)->mangled_name); 3465 1.1 mrg ob = TREE_OPERAND (ob, 0); 3466 1.1 mrg write_expression (ob); 3467 1.1 mrg } 3468 1.1 mrg else if (write_base_ref (expr)) 3469 1.1 mrg return; 3470 1.1 mrg else if (!is_dummy_object (ob)) 3471 1.1 mrg { 3472 1.1 mrg write_string ("dt"); 3473 1.1 mrg write_expression (ob); 3474 1.1 mrg } 3475 1.1 mrg /* else, for a non-static data member with no associated object (in 3476 1.1 mrg unevaluated context), use the unresolved-name mangling. */ 3477 1.1 mrg 3478 1.1 mrg write_member_name (TREE_OPERAND (expr, 1)); 3479 1.1 mrg return; 3480 1.1 mrg } 3481 1.1 mrg 3482 1.1 mrg /* If it wasn't any of those, recursively expand the expression. */ 3483 1.1 mrg name = OVL_OP_INFO (false, code)->mangled_name; 3484 1.1 mrg 3485 1.1 mrg /* We used to mangle const_cast and static_cast like a C cast. */ 3486 1.1 mrg if (code == CONST_CAST_EXPR 3487 1.1 mrg || code == STATIC_CAST_EXPR) 3488 1.1 mrg { 3489 1.1 mrg if (abi_warn_or_compat_version_crosses (6)) 3490 1.1 mrg G.need_abi_warning = 1; 3491 1.1 mrg if (!abi_version_at_least (6)) 3492 1.1 mrg name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name; 3493 1.1 mrg } 3494 1.1 mrg 3495 1.1 mrg if (name == NULL) 3496 1.1 mrg { 3497 1.1 mrg switch (code) 3498 1.1 mrg { 3499 1.1 mrg case TRAIT_EXPR: 3500 1.1 mrg error ("use of built-in trait %qE in function signature; " 3501 1.1 mrg "use library traits instead", expr); 3502 1.1 mrg break; 3503 1.1 mrg 3504 1.1 mrg default: 3505 1.1 mrg sorry ("mangling %C", code); 3506 1.1 mrg break; 3507 1.1 mrg } 3508 1.1 mrg return; 3509 1.1 mrg } 3510 1.1 mrg else 3511 1.1 mrg write_string (name); 3512 1.1 mrg 3513 1.1 mrg switch (code) 3514 1.1 mrg { 3515 1.1 mrg case CALL_EXPR: 3516 1.1 mrg { 3517 1.1 mrg tree fn = CALL_EXPR_FN (expr); 3518 1.1 mrg 3519 1.1 mrg if (TREE_CODE (fn) == ADDR_EXPR) 3520 1.1 mrg fn = TREE_OPERAND (fn, 0); 3521 1.1 mrg 3522 1.1 mrg /* Mangle a dependent name as the name, not whatever happens to 3523 1.1 mrg be the first function in the overload set. */ 3524 1.1 mrg if (OVL_P (fn) 3525 1.1 mrg && type_dependent_expression_p_push (expr)) 3526 1.1 mrg fn = OVL_NAME (fn); 3527 1.1 mrg 3528 1.1 mrg write_expression (fn); 3529 1.1 mrg } 3530 1.1 mrg 3531 1.1 mrg for (i = 0; i < call_expr_nargs (expr); ++i) 3532 1.1 mrg write_expression (CALL_EXPR_ARG (expr, i)); 3533 1.1 mrg write_char ('E'); 3534 1.1 mrg break; 3535 1.1 mrg 3536 1.1 mrg case CAST_EXPR: 3537 1.1 mrg write_type (TREE_TYPE (expr)); 3538 1.1 mrg if (list_length (TREE_OPERAND (expr, 0)) == 1) 3539 1.1 mrg write_expression (TREE_VALUE (TREE_OPERAND (expr, 0))); 3540 1.1 mrg else 3541 1.1 mrg { 3542 1.1 mrg tree args = TREE_OPERAND (expr, 0); 3543 1.1 mrg write_char ('_'); 3544 1.1 mrg for (; args; args = TREE_CHAIN (args)) 3545 1.1 mrg write_expression (TREE_VALUE (args)); 3546 1.1 mrg write_char ('E'); 3547 1.1 mrg } 3548 1.1 mrg break; 3549 1.1 mrg 3550 1.1 mrg case DYNAMIC_CAST_EXPR: 3551 1.1 mrg case REINTERPRET_CAST_EXPR: 3552 1.1 mrg case STATIC_CAST_EXPR: 3553 1.1 mrg case CONST_CAST_EXPR: 3554 1.1 mrg write_type (TREE_TYPE (expr)); 3555 1.1 mrg write_expression (TREE_OPERAND (expr, 0)); 3556 1.1 mrg break; 3557 1.1 mrg 3558 1.1 mrg case PREINCREMENT_EXPR: 3559 1.1 mrg case PREDECREMENT_EXPR: 3560 1.1 mrg if (abi_version_at_least (6)) 3561 1.1 mrg write_char ('_'); 3562 1.1 mrg if (abi_warn_or_compat_version_crosses (6)) 3563 1.1 mrg G.need_abi_warning = 1; 3564 1.1 mrg /* Fall through. */ 3565 1.1 mrg 3566 1.1 mrg default: 3567 1.1 mrg /* In the middle-end, some expressions have more operands than 3568 1.1 mrg they do in templates (and mangling). */ 3569 1.1 mrg len = cp_tree_operand_length (expr); 3570 1.1 mrg 3571 1.1 mrg for (i = 0; i < len; ++i) 3572 1.1 mrg { 3573 1.1 mrg tree operand = TREE_OPERAND (expr, i); 3574 1.1 mrg /* As a GNU extension, the middle operand of a 3575 1.1 mrg conditional may be omitted. Since expression 3576 1.1 mrg manglings are supposed to represent the input token 3577 1.1 mrg stream, there's no good way to mangle such an 3578 1.1 mrg expression without extending the C++ ABI. */ 3579 1.1 mrg if (code == COND_EXPR && i == 1 && !operand) 3580 1.1 mrg { 3581 1.1 mrg error ("omitted middle operand to %<?:%> operand " 3582 1.1 mrg "cannot be mangled"); 3583 1.1 mrg continue; 3584 1.1 mrg } 3585 1.1 mrg else if (FOLD_EXPR_P (expr)) 3586 1.1 mrg { 3587 1.1 mrg /* The first 'operand' of a fold-expression is the operator 3588 1.1 mrg that it folds over. */ 3589 1.1 mrg if (i == 0) 3590 1.1 mrg { 3591 1.1 mrg int fcode = TREE_INT_CST_LOW (operand); 3592 1.1 mrg write_string (OVL_OP_INFO (false, fcode)->mangled_name); 3593 1.1 mrg continue; 3594 1.1 mrg } 3595 1.1 mrg else if (code == BINARY_LEFT_FOLD_EXPR) 3596 1.1 mrg { 3597 1.1 mrg /* The order of operands of the binary left and right 3598 1.1 mrg folds is the same, but we want to mangle them in 3599 1.1 mrg lexical order, i.e. non-pack first. */ 3600 1.1 mrg if (i == 1) 3601 1.1 mrg operand = FOLD_EXPR_INIT (expr); 3602 1.1 mrg else 3603 1.1 mrg operand = FOLD_EXPR_PACK (expr); 3604 1.1 mrg } 3605 1.1 mrg if (PACK_EXPANSION_P (operand)) 3606 1.1 mrg operand = PACK_EXPANSION_PATTERN (operand); 3607 1.1 mrg } 3608 1.1 mrg write_expression (operand); 3609 1.1 mrg } 3610 1.1 mrg } 3611 1.1 mrg } 3612 1.1 mrg } 3613 1.1 mrg 3614 1.1 mrg /* Literal subcase of non-terminal <template-arg>. 3615 1.1 mrg 3616 1.1 mrg "Literal arguments, e.g. "A<42L>", are encoded with their type 3617 1.1 mrg and value. Negative integer values are preceded with "n"; for 3618 1.1 mrg example, "A<-42L>" becomes "1AILln42EE". The bool value false is 3619 1.1 mrg encoded as 0, true as 1." */ 3620 1.1 mrg 3621 1.1 mrg static void 3622 1.1 mrg write_template_arg_literal (const tree value) 3623 1.1 mrg { 3624 1.1 mrg if (TREE_CODE (value) == STRING_CST) 3625 1.1 mrg /* Temporarily mangle strings as braced initializer lists. */ 3626 1.1 mrg write_string ("tl"); 3627 1.1 mrg else 3628 1.1 mrg write_char ('L'); 3629 1.1 mrg 3630 1.1 mrg tree valtype = TREE_TYPE (value); 3631 1.1 mrg write_type (valtype); 3632 1.1 mrg 3633 1.1 mrg /* Write a null member pointer value as (type)0, regardless of its 3634 1.1 mrg real representation. */ 3635 1.1 mrg if (null_member_pointer_value_p (value)) 3636 1.1 mrg write_integer_cst (integer_zero_node); 3637 1.1 mrg else 3638 1.1 mrg switch (TREE_CODE (value)) 3639 1.1 mrg { 3640 1.1 mrg case CONST_DECL: 3641 1.1 mrg write_integer_cst (DECL_INITIAL (value)); 3642 1.1 mrg break; 3643 1.1 mrg 3644 1.1 mrg case INTEGER_CST: 3645 1.1 mrg gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node) 3646 1.1 mrg || integer_zerop (value) || integer_onep (value)); 3647 1.1 mrg if (!(abi_version_at_least (14) 3648 1.1 mrg && NULLPTR_TYPE_P (TREE_TYPE (value)))) 3649 1.1 mrg write_integer_cst (value); 3650 1.1 mrg break; 3651 1.1 mrg 3652 1.1 mrg case REAL_CST: 3653 1.1 mrg write_real_cst (value); 3654 1.1 mrg break; 3655 1.1 mrg 3656 1.1 mrg case COMPLEX_CST: 3657 1.1 mrg if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST 3658 1.1 mrg && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST) 3659 1.1 mrg { 3660 1.1 mrg write_integer_cst (TREE_REALPART (value)); 3661 1.1 mrg write_char ('_'); 3662 1.1 mrg write_integer_cst (TREE_IMAGPART (value)); 3663 1.1 mrg } 3664 1.1 mrg else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST 3665 1.1 mrg && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST) 3666 1.1 mrg { 3667 1.1 mrg write_real_cst (TREE_REALPART (value)); 3668 1.1 mrg write_char ('_'); 3669 1.1 mrg write_real_cst (TREE_IMAGPART (value)); 3670 1.1 mrg } 3671 1.1 mrg else 3672 1.1 mrg gcc_unreachable (); 3673 1.1 mrg break; 3674 1.1 mrg 3675 1.1 mrg case STRING_CST: 3676 1.1 mrg { 3677 1.1 mrg /* Mangle strings the same as braced initializer lists. */ 3678 1.1 mrg unsigned n = TREE_STRING_LENGTH (value); 3679 1.1 mrg const char *str = TREE_STRING_POINTER (value); 3680 1.1 mrg 3681 1.1 mrg /* Count the number of trailing nuls and subtract them from 3682 1.1 mrg STRSIZE because they don't need to be mangled. */ 3683 1.1 mrg for (const char *p = str + n - 1; ; --p) 3684 1.1 mrg { 3685 1.1 mrg if (*p || p == str) 3686 1.1 mrg { 3687 1.1 mrg n -= str + n - !!*p - p; 3688 1.1 mrg break; 3689 1.1 mrg } 3690 1.1 mrg } 3691 1.1 mrg tree eltype = TREE_TYPE (valtype); 3692 1.1 mrg for (const char *p = str; n--; ++p) 3693 1.1 mrg { 3694 1.1 mrg write_char ('L'); 3695 1.1 mrg write_type (eltype); 3696 1.1 mrg write_unsigned_number (*(const unsigned char*)p); 3697 1.1 mrg write_string ("E"); 3698 1.1 mrg } 3699 1.1 mrg break; 3700 1.1 mrg } 3701 1.1 mrg 3702 1.1 mrg default: 3703 1.1 mrg gcc_unreachable (); 3704 1.1 mrg } 3705 1.1 mrg 3706 1.1 mrg write_char ('E'); 3707 1.1 mrg } 3708 1.1 mrg 3709 1.1 mrg /* Non-terminal <template-arg>. 3710 1.1 mrg 3711 1.1 mrg <template-arg> ::= <type> # type 3712 1.1 mrg ::= L <type> </value/ number> E # literal 3713 1.1 mrg ::= LZ <name> E # external name 3714 1.1 mrg ::= X <expression> E # expression */ 3715 1.1 mrg 3716 1.1 mrg static void 3717 1.1 mrg write_template_arg (tree node) 3718 1.1 mrg { 3719 1.1 mrg enum tree_code code = TREE_CODE (node); 3720 1.1 mrg 3721 1.1 mrg MANGLE_TRACE_TREE ("template-arg", node); 3722 1.1 mrg 3723 1.1 mrg /* A template template parameter's argument list contains TREE_LIST 3724 1.1 mrg nodes of which the value field is the actual argument. */ 3725 1.1 mrg if (code == TREE_LIST) 3726 1.1 mrg { 3727 1.1 mrg node = TREE_VALUE (node); 3728 1.1 mrg /* If it's a decl, deal with its type instead. */ 3729 1.1 mrg if (DECL_P (node)) 3730 1.1 mrg { 3731 1.1 mrg node = TREE_TYPE (node); 3732 1.1 mrg code = TREE_CODE (node); 3733 1.1 mrg } 3734 1.1 mrg } 3735 1.1 mrg 3736 1.1 mrg if (template_parm_object_p (node)) 3737 1.1 mrg /* We want to mangle the argument, not the var we stored it in. */ 3738 1.1 mrg node = tparm_object_argument (node); 3739 1.1 mrg 3740 1.1 mrg /* Strip a conversion added by convert_nontype_argument. */ 3741 1.1 mrg if (TREE_CODE (node) == IMPLICIT_CONV_EXPR) 3742 1.1 mrg node = TREE_OPERAND (node, 0); 3743 1.1 mrg if (REFERENCE_REF_P (node)) 3744 1.1 mrg node = TREE_OPERAND (node, 0); 3745 1.1 mrg if (TREE_CODE (node) == NOP_EXPR 3746 1.1 mrg && TYPE_REF_P (TREE_TYPE (node))) 3747 1.1 mrg { 3748 1.1 mrg /* Template parameters can be of reference type. To maintain 3749 1.1 mrg internal consistency, such arguments use a conversion from 3750 1.1 mrg address of object to reference type. */ 3751 1.1 mrg gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR); 3752 1.1 mrg node = TREE_OPERAND (TREE_OPERAND (node, 0), 0); 3753 1.1 mrg } 3754 1.1 mrg 3755 1.1 mrg if (TREE_CODE (node) == BASELINK 3756 1.1 mrg && !type_unknown_p (node)) 3757 1.1 mrg { 3758 1.1 mrg if (abi_version_at_least (6)) 3759 1.1 mrg node = BASELINK_FUNCTIONS (node); 3760 1.1 mrg if (abi_warn_or_compat_version_crosses (6)) 3761 1.1 mrg /* We wrongly wrapped a class-scope function in X/E. */ 3762 1.1 mrg G.need_abi_warning = 1; 3763 1.1 mrg } 3764 1.1 mrg 3765 1.1 mrg if (ARGUMENT_PACK_P (node)) 3766 1.1 mrg { 3767 1.1 mrg /* Expand the template argument pack. */ 3768 1.1 mrg tree args = ARGUMENT_PACK_ARGS (node); 3769 1.1 mrg int i, length = TREE_VEC_LENGTH (args); 3770 1.1 mrg if (abi_version_at_least (6)) 3771 1.1 mrg write_char ('J'); 3772 1.1 mrg else 3773 1.1 mrg write_char ('I'); 3774 1.1 mrg if (abi_warn_or_compat_version_crosses (6)) 3775 1.1 mrg G.need_abi_warning = 1; 3776 1.1 mrg for (i = 0; i < length; ++i) 3777 1.1 mrg write_template_arg (TREE_VEC_ELT (args, i)); 3778 1.1 mrg write_char ('E'); 3779 1.1 mrg } 3780 1.1 mrg else if (TYPE_P (node)) 3781 1.1 mrg write_type (node); 3782 1.1 mrg else if (code == TEMPLATE_DECL) 3783 1.1 mrg /* A template appearing as a template arg is a template template arg. */ 3784 1.1 mrg write_template_template_arg (node); 3785 1.1 mrg else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST) 3786 1.1 mrg || code == CONST_DECL 3787 1.1 mrg || null_member_pointer_value_p (node)) 3788 1.1 mrg write_template_arg_literal (node); 3789 1.1 mrg else if (DECL_P (node)) 3790 1.1 mrg { 3791 1.1 mrg write_char ('L'); 3792 1.1 mrg /* Until ABI version 3, the underscore before the mangled name 3793 1.1 mrg was incorrectly omitted. */ 3794 1.1 mrg if (!abi_version_at_least (3)) 3795 1.1 mrg write_char ('Z'); 3796 1.1 mrg else 3797 1.1 mrg write_string ("_Z"); 3798 1.1 mrg if (abi_warn_or_compat_version_crosses (3)) 3799 1.1 mrg G.need_abi_warning = 1; 3800 1.1 mrg write_encoding (node); 3801 1.1 mrg write_char ('E'); 3802 1.1 mrg } 3803 1.1 mrg else 3804 1.1 mrg { 3805 1.1 mrg /* Template arguments may be expressions. */ 3806 1.1 mrg write_char ('X'); 3807 1.1 mrg write_expression (node); 3808 1.1 mrg write_char ('E'); 3809 1.1 mrg } 3810 1.1 mrg } 3811 1.1 mrg 3812 1.1 mrg /* <template-template-arg> 3813 1.1 mrg ::= <name> 3814 1.1 mrg ::= <substitution> */ 3815 1.1 mrg 3816 1.1 mrg static void 3817 1.1 mrg write_template_template_arg (const tree decl) 3818 1.1 mrg { 3819 1.1 mrg MANGLE_TRACE_TREE ("template-template-arg", decl); 3820 1.1 mrg 3821 1.1 mrg if (find_substitution (decl)) 3822 1.1 mrg return; 3823 1.1 mrg write_name (decl, /*ignore_local_scope=*/0); 3824 1.1 mrg add_substitution (decl); 3825 1.1 mrg } 3826 1.1 mrg 3827 1.1 mrg 3828 1.1 mrg /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE. 3829 1.1 mrg 3830 1.1 mrg <array-type> ::= A [</dimension/ number>] _ </element/ type> 3831 1.1 mrg ::= A <expression> _ </element/ type> 3832 1.1 mrg 3833 1.1 mrg "Array types encode the dimension (number of elements) and the 3834 1.1 mrg element type. For variable length arrays, the dimension (but not 3835 1.1 mrg the '_' separator) is omitted." 3836 1.1 mrg Note that for flexible array members, like for other arrays of 3837 1.1 mrg unspecified size, the dimension is also omitted. */ 3838 1.1 mrg 3839 1.1 mrg static void 3840 1.1 mrg write_array_type (const tree type) 3841 1.1 mrg { 3842 1.1 mrg write_char ('A'); 3843 1.1 mrg if (TYPE_DOMAIN (type)) 3844 1.1 mrg { 3845 1.1 mrg tree index_type; 3846 1.1 mrg 3847 1.1 mrg index_type = TYPE_DOMAIN (type); 3848 1.1 mrg /* The INDEX_TYPE gives the upper and lower bounds of the array. 3849 1.1 mrg It's null for flexible array members which have no upper bound 3850 1.1 mrg (this is a change from GCC 5 and prior where such members were 3851 1.1 mrg incorrectly mangled as zero-length arrays). */ 3852 1.1 mrg if (tree max = TYPE_MAX_VALUE (index_type)) 3853 1.1 mrg { 3854 1.1 mrg if (TREE_CODE (max) == INTEGER_CST) 3855 1.1 mrg { 3856 1.1 mrg /* The ABI specifies that we should mangle the number of 3857 1.1 mrg elements in the array, not the largest allowed index. */ 3858 1.1 mrg offset_int wmax = wi::to_offset (max) + 1; 3859 1.1 mrg /* Truncate the result - this will mangle [0, SIZE_INT_MAX] 3860 1.1 mrg number of elements as zero. */ 3861 1.1 mrg wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max))); 3862 1.1 mrg gcc_assert (wi::fits_uhwi_p (wmax)); 3863 1.1 mrg write_unsigned_number (wmax.to_uhwi ()); 3864 1.1 mrg } 3865 1.1 mrg else 3866 1.1 mrg { 3867 1.1 mrg max = TREE_OPERAND (max, 0); 3868 1.1 mrg write_expression (max); 3869 1.1 mrg } 3870 1.1 mrg } 3871 1.1 mrg } 3872 1.1 mrg write_char ('_'); 3873 1.1 mrg write_type (TREE_TYPE (type)); 3874 1.1 mrg } 3875 1.1 mrg 3876 1.1 mrg /* Non-terminal <pointer-to-member-type> for pointer-to-member 3877 1.1 mrg variables. TYPE is a pointer-to-member POINTER_TYPE. 3878 1.1 mrg 3879 1.1 mrg <pointer-to-member-type> ::= M </class/ type> </member/ type> */ 3880 1.1 mrg 3881 1.1 mrg static void 3882 1.1 mrg write_pointer_to_member_type (const tree type) 3883 1.1 mrg { 3884 1.1 mrg write_char ('M'); 3885 1.1 mrg write_type (TYPE_PTRMEM_CLASS_TYPE (type)); 3886 1.1 mrg write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type)); 3887 1.1 mrg } 3888 1.1 mrg 3889 1.1 mrg /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM, 3890 1.1 mrg TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a 3891 1.1 mrg TEMPLATE_PARM_INDEX. 3892 1.1 mrg 3893 1.1 mrg <template-param> ::= T </parameter/ number> _ */ 3894 1.1 mrg 3895 1.1 mrg static void 3896 1.1 mrg write_template_param (const tree parm) 3897 1.1 mrg { 3898 1.1 mrg int parm_index; 3899 1.1 mrg 3900 1.1 mrg MANGLE_TRACE_TREE ("template-parm", parm); 3901 1.1 mrg 3902 1.1 mrg switch (TREE_CODE (parm)) 3903 1.1 mrg { 3904 1.1 mrg case TEMPLATE_TYPE_PARM: 3905 1.1 mrg case TEMPLATE_TEMPLATE_PARM: 3906 1.1 mrg case BOUND_TEMPLATE_TEMPLATE_PARM: 3907 1.1 mrg parm_index = TEMPLATE_TYPE_IDX (parm); 3908 1.1 mrg break; 3909 1.1 mrg 3910 1.1 mrg case TEMPLATE_PARM_INDEX: 3911 1.1 mrg parm_index = TEMPLATE_PARM_IDX (parm); 3912 1.1 mrg break; 3913 1.1 mrg 3914 1.1 mrg default: 3915 1.1 mrg gcc_unreachable (); 3916 1.1 mrg } 3917 1.1 mrg 3918 1.1 mrg write_char ('T'); 3919 1.1 mrg /* NUMBER as it appears in the mangling is (-1)-indexed, with the 3920 1.1 mrg earliest template param denoted by `_'. */ 3921 1.1 mrg write_compact_number (parm_index); 3922 1.1 mrg } 3923 1.1 mrg 3924 1.1 mrg /* <template-template-param> 3925 1.1 mrg ::= <template-param> 3926 1.1 mrg ::= <substitution> */ 3927 1.1 mrg 3928 1.1 mrg static void 3929 1.1 mrg write_template_template_param (const tree parm) 3930 1.1 mrg { 3931 1.1 mrg tree templ = NULL_TREE; 3932 1.1 mrg 3933 1.1 mrg /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the 3934 1.1 mrg template template parameter. The substitution candidate here is 3935 1.1 mrg only the template. */ 3936 1.1 mrg if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 3937 1.1 mrg { 3938 1.1 mrg templ 3939 1.1 mrg = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)); 3940 1.1 mrg if (find_substitution (templ)) 3941 1.1 mrg return; 3942 1.1 mrg } 3943 1.1 mrg 3944 1.1 mrg /* <template-param> encodes only the template parameter position, 3945 1.1 mrg not its template arguments, which is fine here. */ 3946 1.1 mrg write_template_param (parm); 3947 1.1 mrg if (templ) 3948 1.1 mrg add_substitution (templ); 3949 1.1 mrg } 3950 1.1 mrg 3951 1.1 mrg /* Non-terminal <substitution>. 3952 1.1 mrg 3953 1.1 mrg <substitution> ::= S <seq-id> _ 3954 1.1 mrg ::= S_ */ 3955 1.1 mrg 3956 1.1 mrg static void 3957 1.1 mrg write_substitution (const int seq_id) 3958 1.1 mrg { 3959 1.1 mrg MANGLE_TRACE ("substitution", ""); 3960 1.1 mrg 3961 1.1 mrg write_char ('S'); 3962 1.1 mrg if (seq_id > 0) 3963 1.1 mrg write_number (seq_id - 1, /*unsigned=*/1, 36); 3964 1.1 mrg write_char ('_'); 3965 1.1 mrg } 3966 1.1 mrg 3967 1.1 mrg /* Start mangling ENTITY. */ 3968 1.1 mrg 3969 1.1 mrg static inline void 3970 1.1 mrg start_mangling (const tree entity) 3971 1.1 mrg { 3972 1.1 mrg G.entity = entity; 3973 1.1 mrg G.need_abi_warning = false; 3974 1.1 mrg G.need_cxx17_warning = false; 3975 1.1 mrg G.mod = false; 3976 1.1 mrg obstack_free (&name_obstack, name_base); 3977 1.1 mrg mangle_obstack = &name_obstack; 3978 1.1 mrg name_base = obstack_alloc (&name_obstack, 0); 3979 1.1 mrg } 3980 1.1 mrg 3981 1.1 mrg /* Done with mangling. Release the data. */ 3982 1.1 mrg 3983 1.1 mrg static void 3984 1.1 mrg finish_mangling_internal (void) 3985 1.1 mrg { 3986 1.1 mrg /* Clear all the substitutions. */ 3987 1.1 mrg vec_safe_truncate (G.substitutions, 0); 3988 1.1 mrg 3989 1.1 mrg if (G.mod) 3990 1.1 mrg mangle_module_fini (); 3991 1.1 mrg 3992 1.1 mrg /* Null-terminate the string. */ 3993 1.1 mrg write_char ('\0'); 3994 1.1 mrg } 3995 1.1 mrg 3996 1.1 mrg 3997 1.1 mrg /* Like finish_mangling_internal, but return the mangled string. */ 3998 1.1 mrg 3999 1.1 mrg static inline const char * 4000 1.1 mrg finish_mangling (void) 4001 1.1 mrg { 4002 1.1 mrg finish_mangling_internal (); 4003 1.1 mrg return (const char *) obstack_finish (mangle_obstack); 4004 1.1 mrg } 4005 1.1 mrg 4006 1.1 mrg /* Like finish_mangling_internal, but return an identifier. */ 4007 1.1 mrg 4008 1.1 mrg static tree 4009 1.1 mrg finish_mangling_get_identifier (void) 4010 1.1 mrg { 4011 1.1 mrg finish_mangling_internal (); 4012 1.1 mrg /* Don't obstack_finish here, and the next start_mangling will 4013 1.1 mrg remove the identifier. */ 4014 1.1 mrg return get_identifier ((const char *) obstack_base (mangle_obstack)); 4015 1.1 mrg } 4016 1.1 mrg 4017 1.1 mrg /* Initialize data structures for mangling. */ 4018 1.1 mrg 4019 1.1 mrg void 4020 1.1 mrg init_mangle (void) 4021 1.1 mrg { 4022 1.1 mrg gcc_obstack_init (&name_obstack); 4023 1.1 mrg name_base = obstack_alloc (&name_obstack, 0); 4024 1.1 mrg vec_alloc (G.substitutions, 0); 4025 1.1 mrg 4026 1.1 mrg /* Cache these identifiers for quick comparison when checking for 4027 1.1 mrg standard substitutions. */ 4028 1.1 mrg subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator"); 4029 1.1 mrg subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string"); 4030 1.1 mrg subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits"); 4031 1.1 mrg subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream"); 4032 1.1 mrg subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream"); 4033 1.1 mrg subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream"); 4034 1.1 mrg } 4035 1.1 mrg 4036 1.1 mrg /* Generate a mangling for MODULE's global initializer fn. */ 4037 1.1 mrg 4038 1.1 mrg tree 4039 1.1 mrg mangle_module_global_init (int module) 4040 1.1 mrg { 4041 1.1 mrg start_mangling (NULL_TREE); 4042 1.1 mrg 4043 1.1 mrg write_string ("_ZGI"); 4044 1.1 mrg write_module (module, true); 4045 1.1 mrg 4046 1.1 mrg return finish_mangling_get_identifier (); 4047 1.1 mrg } 4048 1.1 mrg 4049 1.1 mrg /* Generate the mangled name of DECL. */ 4050 1.1 mrg 4051 1.1 mrg static tree 4052 1.1 mrg mangle_decl_string (const tree decl) 4053 1.1 mrg { 4054 1.1 mrg tree result; 4055 1.1 mrg tree saved_fn = NULL_TREE; 4056 1.1 mrg bool template_p = false; 4057 1.1 mrg 4058 1.1 mrg /* We shouldn't be trying to mangle an uninstantiated template. */ 4059 1.1 mrg gcc_assert (!type_dependent_expression_p (decl)); 4060 1.1 mrg 4061 1.1 mrg if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 4062 1.1 mrg { 4063 1.1 mrg struct tinst_level *tl = current_instantiation (); 4064 1.1 mrg if ((!tl || tl->maybe_get_node () != decl) 4065 1.1 mrg && push_tinst_level (decl)) 4066 1.1 mrg { 4067 1.1 mrg template_p = true; 4068 1.1 mrg saved_fn = current_function_decl; 4069 1.1 mrg current_function_decl = NULL_TREE; 4070 1.1 mrg } 4071 1.1 mrg } 4072 1.1 mrg iloc_sentinel ils (DECL_SOURCE_LOCATION (decl)); 4073 1.1 mrg 4074 1.1 mrg start_mangling (decl); 4075 1.1 mrg 4076 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL) 4077 1.1 mrg write_type (TREE_TYPE (decl)); 4078 1.1 mrg else 4079 1.1 mrg write_mangled_name (decl, true); 4080 1.1 mrg 4081 1.1 mrg result = finish_mangling_get_identifier (); 4082 1.1 mrg if (DEBUG_MANGLE) 4083 1.1 mrg fprintf (stderr, "mangle_decl_string = '%s'\n\n", 4084 1.1 mrg IDENTIFIER_POINTER (result)); 4085 1.1 mrg 4086 1.1 mrg if (template_p) 4087 1.1 mrg { 4088 1.1 mrg pop_tinst_level (); 4089 1.1 mrg current_function_decl = saved_fn; 4090 1.1 mrg } 4091 1.1 mrg 4092 1.1 mrg return result; 4093 1.1 mrg } 4094 1.1 mrg 4095 1.1 mrg /* Return an identifier for the external mangled name of DECL. */ 4096 1.1 mrg 4097 1.1 mrg static tree 4098 1.1 mrg get_mangled_id (tree decl) 4099 1.1 mrg { 4100 1.1 mrg tree id = mangle_decl_string (decl); 4101 1.1 mrg return targetm.mangle_decl_assembler_name (decl, id); 4102 1.1 mrg } 4103 1.1 mrg 4104 1.1 mrg /* Create an identifier for the external mangled name of DECL. */ 4105 1.1 mrg 4106 1.1 mrg void 4107 1.1 mrg mangle_decl (const tree decl) 4108 1.1 mrg { 4109 1.1 mrg tree id; 4110 1.1 mrg bool dep; 4111 1.1 mrg 4112 1.1 mrg /* Don't bother mangling uninstantiated templates. */ 4113 1.1 mrg ++processing_template_decl; 4114 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL) 4115 1.1 mrg dep = dependent_type_p (TREE_TYPE (decl)); 4116 1.1 mrg else 4117 1.1 mrg dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) 4118 1.1 mrg && any_dependent_template_arguments_p (DECL_TI_ARGS (decl))); 4119 1.1 mrg --processing_template_decl; 4120 1.1 mrg if (dep) 4121 1.1 mrg return; 4122 1.1 mrg 4123 1.1 mrg /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging. 4124 1.1 mrg It is not needed to assign names to anonymous namespace, but we use the 4125 1.1 mrg "<anon>" marker to be able to tell if type is C++ ODR type or type 4126 1.1 mrg produced by other language. */ 4127 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL 4128 1.1 mrg && TYPE_STUB_DECL (TREE_TYPE (decl)) 4129 1.1 mrg && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl)))) 4130 1.1 mrg id = get_identifier ("<anon>"); 4131 1.1 mrg else 4132 1.1 mrg { 4133 1.1 mrg gcc_assert (TREE_CODE (decl) != TYPE_DECL 4134 1.1 mrg || !no_linkage_check (TREE_TYPE (decl), true)); 4135 1.1 mrg if (abi_version_at_least (10)) 4136 1.1 mrg if (tree fn = decl_function_context (decl)) 4137 1.1 mrg maybe_check_abi_tags (fn, decl); 4138 1.1 mrg id = get_mangled_id (decl); 4139 1.1 mrg } 4140 1.1 mrg SET_DECL_ASSEMBLER_NAME (decl, id); 4141 1.1 mrg 4142 1.1 mrg if (G.need_cxx17_warning 4143 1.1 mrg && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl))) 4144 1.1 mrg warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type, 4145 1.1 mrg "mangled name for %qD will change in C++17 because the " 4146 1.1 mrg "exception specification is part of a function type", 4147 1.1 mrg decl); 4148 1.1 mrg 4149 1.1 mrg if (id != DECL_NAME (decl) 4150 1.1 mrg /* Don't do this for a fake symbol we aren't going to emit anyway. */ 4151 1.1 mrg && TREE_CODE (decl) != TYPE_DECL 4152 1.1 mrg && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)) 4153 1.1 mrg { 4154 1.1 mrg int save_ver = flag_abi_version; 4155 1.1 mrg tree id2 = NULL_TREE; 4156 1.1 mrg 4157 1.1 mrg if (!DECL_REALLY_EXTERN (decl)) 4158 1.1 mrg { 4159 1.1 mrg record_mangling (decl, G.need_abi_warning); 4160 1.1 mrg 4161 1.1 mrg if (!G.need_abi_warning) 4162 1.1 mrg return; 4163 1.1 mrg 4164 1.1 mrg flag_abi_version = flag_abi_compat_version; 4165 1.1 mrg id2 = mangle_decl_string (decl); 4166 1.1 mrg id2 = targetm.mangle_decl_assembler_name (decl, id2); 4167 1.1 mrg flag_abi_version = save_ver; 4168 1.1 mrg 4169 1.1 mrg if (id2 != id) 4170 1.1 mrg note_mangling_alias (decl, id2); 4171 1.1 mrg } 4172 1.1 mrg 4173 1.1 mrg if (warn_abi) 4174 1.1 mrg { 4175 1.1 mrg const char fabi_version[] = "-fabi-version"; 4176 1.1 mrg 4177 1.1 mrg if (flag_abi_compat_version != warn_abi_version 4178 1.1 mrg || id2 == NULL_TREE) 4179 1.1 mrg { 4180 1.1 mrg flag_abi_version = warn_abi_version; 4181 1.1 mrg id2 = mangle_decl_string (decl); 4182 1.1 mrg id2 = targetm.mangle_decl_assembler_name (decl, id2); 4183 1.1 mrg } 4184 1.1 mrg flag_abi_version = save_ver; 4185 1.1 mrg 4186 1.1 mrg if (id2 == id) 4187 1.1 mrg /* OK. */; 4188 1.1 mrg else if (warn_abi_version != 0 4189 1.1 mrg && abi_version_at_least (warn_abi_version)) 4190 1.1 mrg warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi, 4191 1.1 mrg "the mangled name of %qD changed between " 4192 1.1 mrg "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)", 4193 1.1 mrg G.entity, fabi_version, warn_abi_version, id2, 4194 1.1 mrg fabi_version, save_ver, id); 4195 1.1 mrg else 4196 1.1 mrg warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi, 4197 1.1 mrg "the mangled name of %qD changes between " 4198 1.1 mrg "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)", 4199 1.1 mrg G.entity, fabi_version, save_ver, id, 4200 1.1 mrg fabi_version, warn_abi_version, id2); 4201 1.1 mrg } 4202 1.1 mrg 4203 1.1 mrg flag_abi_version = save_ver; 4204 1.1 mrg } 4205 1.1 mrg } 4206 1.1 mrg 4207 1.1 mrg /* Generate the mangled representation of TYPE. */ 4208 1.1 mrg 4209 1.1 mrg const char * 4210 1.1 mrg mangle_type_string (const tree type) 4211 1.1 mrg { 4212 1.1 mrg const char *result; 4213 1.1 mrg 4214 1.1 mrg start_mangling (type); 4215 1.1 mrg write_type (type); 4216 1.1 mrg result = finish_mangling (); 4217 1.1 mrg if (DEBUG_MANGLE) 4218 1.1 mrg fprintf (stderr, "mangle_type_string = '%s'\n\n", result); 4219 1.1 mrg return result; 4220 1.1 mrg } 4221 1.1 mrg 4222 1.1 mrg /* Create an identifier for the mangled name of a special component 4223 1.1 mrg for belonging to TYPE. CODE is the ABI-specified code for this 4224 1.1 mrg component. */ 4225 1.1 mrg 4226 1.1 mrg static tree 4227 1.1 mrg mangle_special_for_type (const tree type, const char *code) 4228 1.1 mrg { 4229 1.1 mrg tree result; 4230 1.1 mrg 4231 1.1 mrg /* We don't have an actual decl here for the special component, so 4232 1.1 mrg we can't just process the <encoded-name>. Instead, fake it. */ 4233 1.1 mrg start_mangling (type); 4234 1.1 mrg 4235 1.1 mrg /* Start the mangling. */ 4236 1.1 mrg write_string ("_Z"); 4237 1.1 mrg write_string (code); 4238 1.1 mrg 4239 1.1 mrg /* Add the type. */ 4240 1.1 mrg write_type (type); 4241 1.1 mrg result = finish_mangling_get_identifier (); 4242 1.1 mrg 4243 1.1 mrg if (DEBUG_MANGLE) 4244 1.1 mrg fprintf (stderr, "mangle_special_for_type = %s\n\n", 4245 1.1 mrg IDENTIFIER_POINTER (result)); 4246 1.1 mrg 4247 1.1 mrg return result; 4248 1.1 mrg } 4249 1.1 mrg 4250 1.1 mrg /* Create an identifier for the mangled representation of the typeinfo 4251 1.1 mrg structure for TYPE. */ 4252 1.1 mrg 4253 1.1 mrg tree 4254 1.1 mrg mangle_typeinfo_for_type (const tree type) 4255 1.1 mrg { 4256 1.1 mrg return mangle_special_for_type (type, "TI"); 4257 1.1 mrg } 4258 1.1 mrg 4259 1.1 mrg /* Create an identifier for the mangled name of the NTBS containing 4260 1.1 mrg the mangled name of TYPE. */ 4261 1.1 mrg 4262 1.1 mrg tree 4263 1.1 mrg mangle_typeinfo_string_for_type (const tree type) 4264 1.1 mrg { 4265 1.1 mrg return mangle_special_for_type (type, "TS"); 4266 1.1 mrg } 4267 1.1 mrg 4268 1.1 mrg /* Create an identifier for the mangled name of the vtable for TYPE. */ 4269 1.1 mrg 4270 1.1 mrg tree 4271 1.1 mrg mangle_vtbl_for_type (const tree type) 4272 1.1 mrg { 4273 1.1 mrg return mangle_special_for_type (type, "TV"); 4274 1.1 mrg } 4275 1.1 mrg 4276 1.1 mrg /* Returns an identifier for the mangled name of the VTT for TYPE. */ 4277 1.1 mrg 4278 1.1 mrg tree 4279 1.1 mrg mangle_vtt_for_type (const tree type) 4280 1.1 mrg { 4281 1.1 mrg return mangle_special_for_type (type, "TT"); 4282 1.1 mrg } 4283 1.1 mrg 4284 1.1 mrg /* Returns an identifier for the mangled name of the decomposition 4285 1.1 mrg artificial variable DECL. DECLS is the vector of the VAR_DECLs 4286 1.1 mrg for the identifier-list. */ 4287 1.1 mrg 4288 1.1 mrg tree 4289 1.1 mrg mangle_decomp (const tree decl, vec<tree> &decls) 4290 1.1 mrg { 4291 1.1 mrg gcc_assert (!type_dependent_expression_p (decl)); 4292 1.1 mrg 4293 1.1 mrg location_t saved_loc = input_location; 4294 1.1 mrg input_location = DECL_SOURCE_LOCATION (decl); 4295 1.1 mrg 4296 1.1 mrg start_mangling (decl); 4297 1.1 mrg write_string ("_Z"); 4298 1.1 mrg 4299 1.1 mrg tree context = decl_mangling_context (decl); 4300 1.1 mrg gcc_assert (context != NULL_TREE); 4301 1.1 mrg 4302 1.1 mrg bool nested = false; 4303 1.1 mrg if (DECL_NAMESPACE_STD_P (context)) 4304 1.1 mrg write_string ("St"); 4305 1.1 mrg else if (context != global_namespace) 4306 1.1 mrg { 4307 1.1 mrg nested = true; 4308 1.1 mrg write_char ('N'); 4309 1.1 mrg write_prefix (decl_mangling_context (decl)); 4310 1.1 mrg } 4311 1.1 mrg 4312 1.1 mrg write_string ("DC"); 4313 1.1 mrg unsigned int i; 4314 1.1 mrg tree d; 4315 1.1 mrg FOR_EACH_VEC_ELT (decls, i, d) 4316 1.1 mrg write_unqualified_name (d); 4317 1.1 mrg write_char ('E'); 4318 1.1 mrg 4319 1.1 mrg if (nested) 4320 1.1 mrg write_char ('E'); 4321 1.1 mrg 4322 1.1 mrg tree id = finish_mangling_get_identifier (); 4323 1.1 mrg if (DEBUG_MANGLE) 4324 1.1 mrg fprintf (stderr, "mangle_decomp = '%s'\n\n", 4325 1.1 mrg IDENTIFIER_POINTER (id)); 4326 1.1 mrg 4327 1.1 mrg input_location = saved_loc; 4328 1.1 mrg return id; 4329 1.1 mrg } 4330 1.1 mrg 4331 1.1 mrg /* Return an identifier for a construction vtable group. TYPE is 4332 1.1 mrg the most derived class in the hierarchy; BINFO is the base 4333 1.1 mrg subobject for which this construction vtable group will be used. 4334 1.1 mrg 4335 1.1 mrg This mangling isn't part of the ABI specification; in the ABI 4336 1.1 mrg specification, the vtable group is dumped in the same COMDAT as the 4337 1.1 mrg main vtable, and is referenced only from that vtable, so it doesn't 4338 1.1 mrg need an external name. For binary formats without COMDAT sections, 4339 1.1 mrg though, we need external names for the vtable groups. 4340 1.1 mrg 4341 1.1 mrg We use the production 4342 1.1 mrg 4343 1.1 mrg <special-name> ::= CT <type> <offset number> _ <base type> */ 4344 1.1 mrg 4345 1.1 mrg tree 4346 1.1 mrg mangle_ctor_vtbl_for_type (const tree type, const tree binfo) 4347 1.1 mrg { 4348 1.1 mrg tree result; 4349 1.1 mrg 4350 1.1 mrg start_mangling (type); 4351 1.1 mrg 4352 1.1 mrg write_string ("_Z"); 4353 1.1 mrg write_string ("TC"); 4354 1.1 mrg write_type (type); 4355 1.1 mrg write_integer_cst (BINFO_OFFSET (binfo)); 4356 1.1 mrg write_char ('_'); 4357 1.1 mrg write_type (BINFO_TYPE (binfo)); 4358 1.1 mrg 4359 1.1 mrg result = finish_mangling_get_identifier (); 4360 1.1 mrg if (DEBUG_MANGLE) 4361 1.1 mrg fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", 4362 1.1 mrg IDENTIFIER_POINTER (result)); 4363 1.1 mrg return result; 4364 1.1 mrg } 4365 1.1 mrg 4366 1.1 mrg /* Mangle a this pointer or result pointer adjustment. 4367 1.1 mrg 4368 1.1 mrg <call-offset> ::= h <fixed offset number> _ 4369 1.1 mrg ::= v <fixed offset number> _ <virtual offset number> _ */ 4370 1.1 mrg 4371 1.1 mrg static void 4372 1.1 mrg mangle_call_offset (const tree fixed_offset, const tree virtual_offset) 4373 1.1 mrg { 4374 1.1 mrg write_char (virtual_offset ? 'v' : 'h'); 4375 1.1 mrg 4376 1.1 mrg /* For either flavor, write the fixed offset. */ 4377 1.1 mrg write_integer_cst (fixed_offset); 4378 1.1 mrg write_char ('_'); 4379 1.1 mrg 4380 1.1 mrg /* For a virtual thunk, add the virtual offset. */ 4381 1.1 mrg if (virtual_offset) 4382 1.1 mrg { 4383 1.1 mrg write_integer_cst (virtual_offset); 4384 1.1 mrg write_char ('_'); 4385 1.1 mrg } 4386 1.1 mrg } 4387 1.1 mrg 4388 1.1 mrg /* Return an identifier for the mangled name of a this-adjusting or 4389 1.1 mrg covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment 4390 1.1 mrg to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this 4391 1.1 mrg is a virtual thunk, and it is the vtbl offset in 4392 1.1 mrg bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and 4393 1.1 mrg zero for a covariant thunk. Note, that FN_DECL might be a covariant 4394 1.1 mrg thunk itself. A covariant thunk name always includes the adjustment 4395 1.1 mrg for the this pointer, even if there is none. 4396 1.1 mrg 4397 1.1 mrg <special-name> ::= T <call-offset> <base encoding> 4398 1.1 mrg ::= Tc <this_adjust call-offset> <result_adjust call-offset> 4399 1.1 mrg <base encoding> */ 4400 1.1 mrg 4401 1.1 mrg tree 4402 1.1 mrg mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset, 4403 1.1 mrg tree virtual_offset, tree thunk) 4404 1.1 mrg { 4405 1.1 mrg tree result; 4406 1.1 mrg 4407 1.1 mrg if (abi_version_at_least (11)) 4408 1.1 mrg maybe_check_abi_tags (fn_decl, thunk, 11); 4409 1.1 mrg 4410 1.1 mrg start_mangling (fn_decl); 4411 1.1 mrg 4412 1.1 mrg write_string ("_Z"); 4413 1.1 mrg write_char ('T'); 4414 1.1 mrg 4415 1.1 mrg if (!this_adjusting) 4416 1.1 mrg { 4417 1.1 mrg /* Covariant thunk with no this adjustment */ 4418 1.1 mrg write_char ('c'); 4419 1.1 mrg mangle_call_offset (integer_zero_node, NULL_TREE); 4420 1.1 mrg mangle_call_offset (fixed_offset, virtual_offset); 4421 1.1 mrg } 4422 1.1 mrg else if (!DECL_THUNK_P (fn_decl)) 4423 1.1 mrg /* Plain this adjusting thunk. */ 4424 1.1 mrg mangle_call_offset (fixed_offset, virtual_offset); 4425 1.1 mrg else 4426 1.1 mrg { 4427 1.1 mrg /* This adjusting thunk to covariant thunk. */ 4428 1.1 mrg write_char ('c'); 4429 1.1 mrg mangle_call_offset (fixed_offset, virtual_offset); 4430 1.1 mrg fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl)); 4431 1.1 mrg virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl); 4432 1.1 mrg if (virtual_offset) 4433 1.1 mrg virtual_offset = BINFO_VPTR_FIELD (virtual_offset); 4434 1.1 mrg mangle_call_offset (fixed_offset, virtual_offset); 4435 1.1 mrg fn_decl = THUNK_TARGET (fn_decl); 4436 1.1 mrg } 4437 1.1 mrg 4438 1.1 mrg /* Scoped name. */ 4439 1.1 mrg write_encoding (fn_decl); 4440 1.1 mrg 4441 1.1 mrg result = finish_mangling_get_identifier (); 4442 1.1 mrg if (DEBUG_MANGLE) 4443 1.1 mrg fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result)); 4444 1.1 mrg return result; 4445 1.1 mrg } 4446 1.1 mrg 4447 1.1 mrg /* Handle ABI backwards compatibility for past bugs where we didn't call 4448 1.1 mrg check_abi_tags in places where it's needed: call check_abi_tags and warn if 4449 1.1 mrg it makes a difference. If FOR_DECL is non-null, it's the declaration 4450 1.1 mrg that we're actually trying to mangle; if it's null, we're mangling the 4451 1.1 mrg guard variable for T. */ 4452 1.1 mrg 4453 1.1 mrg static void 4454 1.1 mrg maybe_check_abi_tags (tree t, tree for_decl, int ver) 4455 1.1 mrg { 4456 1.1 mrg if (DECL_ASSEMBLER_NAME_SET_P (t)) 4457 1.1 mrg return; 4458 1.1 mrg 4459 1.1 mrg tree oldtags = get_abi_tags (t); 4460 1.1 mrg 4461 1.1 mrg mangle_decl (t); 4462 1.1 mrg 4463 1.1 mrg tree newtags = get_abi_tags (t); 4464 1.1 mrg if (newtags && newtags != oldtags 4465 1.1 mrg && abi_version_crosses (ver)) 4466 1.1 mrg { 4467 1.1 mrg if (for_decl && DECL_THUNK_P (for_decl)) 4468 1.1 mrg warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi, 4469 1.1 mrg "the mangled name of a thunk for %qD changes between " 4470 1.1 mrg "%<-fabi-version=%d%> and %<-fabi-version=%d%>", 4471 1.1 mrg t, flag_abi_version, warn_abi_version); 4472 1.1 mrg else if (for_decl) 4473 1.1 mrg warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi, 4474 1.1 mrg "the mangled name of %qD changes between " 4475 1.1 mrg "%<-fabi-version=%d%> and %<-fabi-version=%d%>", 4476 1.1 mrg for_decl, flag_abi_version, warn_abi_version); 4477 1.1 mrg else 4478 1.1 mrg warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi, 4479 1.1 mrg "the mangled name of the initialization guard variable " 4480 1.1 mrg "for %qD changes between %<-fabi-version=%d%> and " 4481 1.1 mrg "%<-fabi-version=%d%>", 4482 1.1 mrg t, flag_abi_version, warn_abi_version); 4483 1.1 mrg } 4484 1.1 mrg } 4485 1.1 mrg 4486 1.1 mrg /* Write out the appropriate string for this variable when generating 4487 1.1 mrg another mangled name based on this one. */ 4488 1.1 mrg 4489 1.1 mrg static void 4490 1.1 mrg write_guarded_var_name (const tree variable) 4491 1.1 mrg { 4492 1.1 mrg if (DECL_NAME (variable) 4493 1.1 mrg && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR")) 4494 1.1 mrg /* The name of a guard variable for a reference temporary should refer 4495 1.1 mrg to the reference, not the temporary. */ 4496 1.1 mrg write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4); 4497 1.1 mrg else 4498 1.1 mrg write_name (variable, /*ignore_local_scope=*/0); 4499 1.1 mrg } 4500 1.1 mrg 4501 1.1 mrg /* Return an identifier for the name of an initialization guard 4502 1.1 mrg variable for indicated VARIABLE. */ 4503 1.1 mrg 4504 1.1 mrg tree 4505 1.1 mrg mangle_guard_variable (const tree variable) 4506 1.1 mrg { 4507 1.1 mrg if (abi_version_at_least (10)) 4508 1.1 mrg maybe_check_abi_tags (variable); 4509 1.1 mrg start_mangling (variable); 4510 1.1 mrg write_string ("_ZGV"); 4511 1.1 mrg write_guarded_var_name (variable); 4512 1.1 mrg return finish_mangling_get_identifier (); 4513 1.1 mrg } 4514 1.1 mrg 4515 1.1 mrg /* Return an identifier for the name of a thread_local initialization 4516 1.1 mrg function for VARIABLE. */ 4517 1.1 mrg 4518 1.1 mrg tree 4519 1.1 mrg mangle_tls_init_fn (const tree variable) 4520 1.1 mrg { 4521 1.1 mrg check_abi_tags (variable); 4522 1.1 mrg start_mangling (variable); 4523 1.1 mrg write_string ("_ZTH"); 4524 1.1 mrg write_guarded_var_name (variable); 4525 1.1 mrg return finish_mangling_get_identifier (); 4526 1.1 mrg } 4527 1.1 mrg 4528 1.1 mrg /* Return an identifier for the name of a thread_local wrapper 4529 1.1 mrg function for VARIABLE. */ 4530 1.1 mrg 4531 1.1 mrg #define TLS_WRAPPER_PREFIX "_ZTW" 4532 1.1 mrg 4533 1.1 mrg tree 4534 1.1 mrg mangle_tls_wrapper_fn (const tree variable) 4535 1.1 mrg { 4536 1.1 mrg check_abi_tags (variable); 4537 1.1 mrg start_mangling (variable); 4538 1.1 mrg write_string (TLS_WRAPPER_PREFIX); 4539 1.1 mrg write_guarded_var_name (variable); 4540 1.1 mrg return finish_mangling_get_identifier (); 4541 1.1 mrg } 4542 1.1 mrg 4543 1.1 mrg /* Return true iff FN is a thread_local wrapper function. */ 4544 1.1 mrg 4545 1.1 mrg bool 4546 1.1 mrg decl_tls_wrapper_p (const tree fn) 4547 1.1 mrg { 4548 1.1 mrg if (TREE_CODE (fn) != FUNCTION_DECL) 4549 1.1 mrg return false; 4550 1.1 mrg tree name = DECL_NAME (fn); 4551 1.1 mrg return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX); 4552 1.1 mrg } 4553 1.1 mrg 4554 1.1 mrg /* Return an identifier for the name of a temporary variable used to 4555 1.1 mrg initialize a static reference. This is now part of the ABI. */ 4556 1.1 mrg 4557 1.1 mrg tree 4558 1.1 mrg mangle_ref_init_variable (const tree variable) 4559 1.1 mrg { 4560 1.1 mrg start_mangling (variable); 4561 1.1 mrg write_string ("_ZGR"); 4562 1.1 mrg check_abi_tags (variable); 4563 1.1 mrg write_name (variable, /*ignore_local_scope=*/0); 4564 1.1 mrg /* Avoid name clashes with aggregate initialization of multiple 4565 1.1 mrg references at once. */ 4566 1.1 mrg write_compact_number (current_ref_temp_count++); 4567 1.1 mrg return finish_mangling_get_identifier (); 4568 1.1 mrg } 4569 1.1 mrg 4570 1.1 mrg /* Return an identifier for the mangled name of a C++20 template parameter 4571 1.1 mrg object for template argument EXPR. */ 4572 1.1 mrg 4573 1.1 mrg tree 4574 1.1 mrg mangle_template_parm_object (tree expr) 4575 1.1 mrg { 4576 1.1 mrg start_mangling (expr); 4577 1.1 mrg write_string ("_ZTAX"); 4578 1.1 mrg write_expression (expr); 4579 1.1 mrg write_char ('E'); 4580 1.1 mrg return finish_mangling_get_identifier (); 4581 1.1 mrg } 4582 1.1 mrg 4583 1.1 mrg /* Given a CLASS_TYPE, such as a record for std::bad_exception this 4585 1.1 mrg function generates a mangled name for the vtable map variable of 4586 1.1 mrg the class type. For example, if the class type is 4587 1.1 mrg "std::bad_exception", the mangled name for the class is 4588 1.1 mrg "St13bad_exception". This function would generate the name 4589 1.1 mrg "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as: 4590 1.1 mrg "_VTV<std::bad_exception>::__vtable_map". */ 4591 1.1 mrg 4592 1.1 mrg 4593 1.1 mrg char * 4594 1.1 mrg get_mangled_vtable_map_var_name (tree class_type) 4595 1.1 mrg { 4596 1.1 mrg char *var_name = NULL; 4597 1.1 mrg const char *prefix = "_ZN4_VTVI"; 4598 1.1 mrg const char *postfix = "E12__vtable_mapE"; 4599 1.1 mrg 4600 1.1 mrg gcc_assert (TREE_CODE (class_type) == RECORD_TYPE); 4601 1.1 mrg 4602 1.1 mrg tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type)); 4603 1.1 mrg 4604 1.1 mrg if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL) 4605 1.1 mrg { 4606 1.1 mrg class_id = get_mangled_id (TYPE_NAME (class_type)); 4607 1.1 mrg vtbl_register_mangled_name (TYPE_NAME (class_type), class_id); 4608 1.1 mrg } 4609 1.1 mrg 4610 1.1 mrg unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) + 4611 1.1 mrg strlen (prefix) + 4612 1.1 mrg strlen (postfix) + 1; 4613 1.1 mrg 4614 1.1 mrg var_name = (char *) xmalloc (len); 4615 1.1 mrg 4616 1.1 mrg sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix); 4617 1.1 mrg 4618 1.1 mrg return var_name; 4619 1.1 mrg } 4620 1.1 mrg 4621 #include "gt-cp-mangle.h" 4622