1 /* Demangler for g++ V3 ABI. 2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. 3 Written by Ian Lance Taylor <ian@wasabisystems.com>. 4 5 This file is part of the libiberty library, which is part of GCC. 6 7 This file is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 In addition to the permissions in the GNU General Public License, the 13 Free Software Foundation gives you unlimited permission to link the 14 compiled version of this file into combinations with other programs, 15 and to distribute those combinations without any restriction coming 16 from the use of this file. (The General Public License restrictions 17 do apply in other respects; for example, they cover modification of 18 the file, and distribution when not linked into a combined 19 executable.) 20 21 This program is distributed in the hope that it will be useful, 22 but WITHOUT ANY WARRANTY; without even the implied warranty of 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 GNU General Public License for more details. 25 26 You should have received a copy of the GNU General Public License 27 along with this program; if not, write to the Free Software 28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 29 */ 30 31 /* This code implements a demangler for the g++ V3 ABI. The ABI is 32 described on this web page: 33 http://www.codesourcery.com/cxx-abi/abi.html#mangling 34 35 This code was written while looking at the demangler written by 36 Alex Samuel <samuel@codesourcery.com>. 37 38 This code first pulls the mangled name apart into a list of 39 components, and then walks the list generating the demangled 40 name. 41 42 This file will normally define the following functions, q.v.: 43 char *cplus_demangle_v3(const char *mangled, int options) 44 char *java_demangle_v3(const char *mangled) 45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name) 46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name) 47 48 Also, the interface to the component list is public, and defined in 49 demangle.h. The interface consists of these types, which are 50 defined in demangle.h: 51 enum demangle_component_type 52 struct demangle_component 53 and these functions defined in this file: 54 cplus_demangle_fill_name 55 cplus_demangle_fill_extended_operator 56 cplus_demangle_fill_ctor 57 cplus_demangle_fill_dtor 58 cplus_demangle_print 59 and other functions defined in the file cp-demint.c. 60 61 This file also defines some other functions and variables which are 62 only to be used by the file cp-demint.c. 63 64 Preprocessor macros you can define while compiling this file: 65 66 IN_LIBGCC2 67 If defined, this file defines the following function, q.v.: 68 char *__cxa_demangle (const char *mangled, char *buf, size_t *len, 69 int *status) 70 instead of cplus_demangle_v3() and java_demangle_v3(). 71 72 IN_GLIBCPP_V3 73 If defined, this file defines only __cxa_demangle(), and no other 74 publically visible functions or variables. 75 76 STANDALONE_DEMANGLER 77 If defined, this file defines a main() function which demangles 78 any arguments, or, if none, demangles stdin. 79 80 CP_DEMANGLE_DEBUG 81 If defined, turns on debugging mode, which prints information on 82 stdout about the mangled string. This is not generally useful. 83 */ 84 85 #ifdef HAVE_CONFIG_H 86 #include "config.h" 87 #endif 88 89 #include <stdio.h> 90 91 #ifdef HAVE_STDLIB_H 92 #include <stdlib.h> 93 #endif 94 #ifdef HAVE_STRING_H 95 #include <string.h> 96 #endif 97 98 #include "ansidecl.h" 99 #include "libiberty.h" 100 #include "demangle.h" 101 #include "cp-demangle.h" 102 103 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We 104 also rename them via #define to avoid compiler errors when the 105 static definition conflicts with the extern declaration in a header 106 file. */ 107 #ifdef IN_GLIBCPP_V3 108 109 #define CP_STATIC_IF_GLIBCPP_V3 static 110 111 #define cplus_demangle_fill_name d_fill_name 112 static int d_fill_name (struct demangle_component *, const char *, int); 113 114 #define cplus_demangle_fill_extended_operator d_fill_extended_operator 115 static int 116 d_fill_extended_operator (struct demangle_component *, int, 117 struct demangle_component *); 118 119 #define cplus_demangle_fill_ctor d_fill_ctor 120 static int 121 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds, 122 struct demangle_component *); 123 124 #define cplus_demangle_fill_dtor d_fill_dtor 125 static int 126 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds, 127 struct demangle_component *); 128 129 #define cplus_demangle_mangled_name d_mangled_name 130 static struct demangle_component *d_mangled_name (struct d_info *, int); 131 132 #define cplus_demangle_type d_type 133 static struct demangle_component *d_type (struct d_info *); 134 135 #define cplus_demangle_print d_print 136 static char *d_print (int, const struct demangle_component *, int, size_t *); 137 138 #define cplus_demangle_init_info d_init_info 139 static void d_init_info (const char *, int, size_t, struct d_info *); 140 141 #else /* ! defined(IN_GLIBCPP_V3) */ 142 #define CP_STATIC_IF_GLIBCPP_V3 143 #endif /* ! defined(IN_GLIBCPP_V3) */ 144 145 /* See if the compiler supports dynamic arrays. */ 146 147 #ifdef __GNUC__ 148 #define CP_DYNAMIC_ARRAYS 149 #else 150 #ifdef __STDC__ 151 #ifdef __STDC_VERSION__ 152 #if __STDC_VERSION__ >= 199901L 153 #define CP_DYNAMIC_ARRAYS 154 #endif /* __STDC__VERSION >= 199901L */ 155 #endif /* defined (__STDC_VERSION__) */ 156 #endif /* defined (__STDC__) */ 157 #endif /* ! defined (__GNUC__) */ 158 159 /* We avoid pulling in the ctype tables, to prevent pulling in 160 additional unresolved symbols when this code is used in a library. 161 FIXME: Is this really a valid reason? This comes from the original 162 V3 demangler code. 163 164 As of this writing this file has the following undefined references 165 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy, 166 strcpy, strcat, strlen. */ 167 168 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') 169 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z') 170 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z') 171 172 /* The prefix prepended by GCC to an identifier represnting the 173 anonymous namespace. */ 174 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_" 175 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \ 176 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1) 177 178 /* Information we keep for the standard substitutions. */ 179 180 struct d_standard_sub_info 181 { 182 /* The code for this substitution. */ 183 char code; 184 /* The simple string it expands to. */ 185 const char *simple_expansion; 186 /* The length of the simple expansion. */ 187 int simple_len; 188 /* The results of a full, verbose, expansion. This is used when 189 qualifying a constructor/destructor, or when in verbose mode. */ 190 const char *full_expansion; 191 /* The length of the full expansion. */ 192 int full_len; 193 /* What to set the last_name field of d_info to; NULL if we should 194 not set it. This is only relevant when qualifying a 195 constructor/destructor. */ 196 const char *set_last_name; 197 /* The length of set_last_name. */ 198 int set_last_name_len; 199 }; 200 201 /* Accessors for subtrees of struct demangle_component. */ 202 203 #define d_left(dc) ((dc)->u.s_binary.left) 204 #define d_right(dc) ((dc)->u.s_binary.right) 205 206 /* A list of templates. This is used while printing. */ 207 208 struct d_print_template 209 { 210 /* Next template on the list. */ 211 struct d_print_template *next; 212 /* This template. */ 213 const struct demangle_component *template_decl; 214 }; 215 216 /* A list of type modifiers. This is used while printing. */ 217 218 struct d_print_mod 219 { 220 /* Next modifier on the list. These are in the reverse of the order 221 in which they appeared in the mangled string. */ 222 struct d_print_mod *next; 223 /* The modifier. */ 224 const struct demangle_component *mod; 225 /* Whether this modifier was printed. */ 226 int printed; 227 /* The list of templates which applies to this modifier. */ 228 struct d_print_template *templates; 229 }; 230 231 /* We use this structure to hold information during printing. */ 232 233 struct d_print_info 234 { 235 /* The options passed to the demangler. */ 236 int options; 237 /* Buffer holding the result. */ 238 char *buf; 239 /* Current length of data in buffer. */ 240 size_t len; 241 /* Allocated size of buffer. */ 242 size_t alc; 243 /* The current list of templates, if any. */ 244 struct d_print_template *templates; 245 /* The current list of modifiers (e.g., pointer, reference, etc.), 246 if any. */ 247 struct d_print_mod *modifiers; 248 /* Set to 1 if we had a memory allocation failure. */ 249 int allocation_failure; 250 }; 251 252 #define d_print_saw_error(dpi) ((dpi)->buf == NULL) 253 254 #define d_append_char(dpi, c) \ 255 do \ 256 { \ 257 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \ 258 (dpi)->buf[(dpi)->len++] = (c); \ 259 else \ 260 d_print_append_char ((dpi), (c)); \ 261 } \ 262 while (0) 263 264 #define d_append_buffer(dpi, s, l) \ 265 do \ 266 { \ 267 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \ 268 { \ 269 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \ 270 (dpi)->len += l; \ 271 } \ 272 else \ 273 d_print_append_buffer ((dpi), (s), (l)); \ 274 } \ 275 while (0) 276 277 #define d_append_string_constant(dpi, s) \ 278 d_append_buffer (dpi, (s), sizeof (s) - 1) 279 280 #define d_last_char(dpi) \ 281 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1]) 282 283 #ifdef CP_DEMANGLE_DEBUG 284 static void d_dump (struct demangle_component *, int); 285 #endif 286 287 static struct demangle_component * 288 d_make_empty (struct d_info *); 289 290 static struct demangle_component * 291 d_make_comp (struct d_info *, enum demangle_component_type, 292 struct demangle_component *, 293 struct demangle_component *); 294 295 static struct demangle_component * 296 d_make_name (struct d_info *, const char *, int); 297 298 static struct demangle_component * 299 d_make_builtin_type (struct d_info *, 300 const struct demangle_builtin_type_info *); 301 302 static struct demangle_component * 303 d_make_operator (struct d_info *, 304 const struct demangle_operator_info *); 305 306 static struct demangle_component * 307 d_make_extended_operator (struct d_info *, int, 308 struct demangle_component *); 309 310 static struct demangle_component * 311 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds, 312 struct demangle_component *); 313 314 static struct demangle_component * 315 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds, 316 struct demangle_component *); 317 318 static struct demangle_component * 319 d_make_template_param (struct d_info *, long); 320 321 static struct demangle_component * 322 d_make_sub (struct d_info *, const char *, int); 323 324 static int 325 has_return_type (struct demangle_component *); 326 327 static int 328 is_ctor_dtor_or_conversion (struct demangle_component *); 329 330 static struct demangle_component *d_encoding (struct d_info *, int); 331 332 static struct demangle_component *d_name (struct d_info *); 333 334 static struct demangle_component *d_nested_name (struct d_info *); 335 336 static struct demangle_component *d_prefix (struct d_info *); 337 338 static struct demangle_component *d_unqualified_name (struct d_info *); 339 340 static struct demangle_component *d_source_name (struct d_info *); 341 342 static long d_number (struct d_info *); 343 344 static struct demangle_component *d_identifier (struct d_info *, int); 345 346 static struct demangle_component *d_operator_name (struct d_info *); 347 348 static struct demangle_component *d_special_name (struct d_info *); 349 350 static int d_call_offset (struct d_info *, int); 351 352 static struct demangle_component *d_ctor_dtor_name (struct d_info *); 353 354 static struct demangle_component ** 355 d_cv_qualifiers (struct d_info *, struct demangle_component **, int); 356 357 static struct demangle_component * 358 d_function_type (struct d_info *); 359 360 static struct demangle_component * 361 d_bare_function_type (struct d_info *, int); 362 363 static struct demangle_component * 364 d_class_enum_type (struct d_info *); 365 366 static struct demangle_component *d_array_type (struct d_info *); 367 368 static struct demangle_component * 369 d_pointer_to_member_type (struct d_info *); 370 371 static struct demangle_component * 372 d_template_param (struct d_info *); 373 374 static struct demangle_component *d_template_args (struct d_info *); 375 376 static struct demangle_component * 377 d_template_arg (struct d_info *); 378 379 static struct demangle_component *d_expression (struct d_info *); 380 381 static struct demangle_component *d_expr_primary (struct d_info *); 382 383 static struct demangle_component *d_local_name (struct d_info *); 384 385 static int d_discriminator (struct d_info *); 386 387 static int 388 d_add_substitution (struct d_info *, struct demangle_component *); 389 390 static struct demangle_component *d_substitution (struct d_info *, int); 391 392 static void d_print_resize (struct d_print_info *, size_t); 393 394 static void d_print_append_char (struct d_print_info *, int); 395 396 static void 397 d_print_append_buffer (struct d_print_info *, const char *, size_t); 398 399 static void d_print_error (struct d_print_info *); 400 401 static void 402 d_print_comp (struct d_print_info *, const struct demangle_component *); 403 404 static void 405 d_print_java_identifier (struct d_print_info *, const char *, int); 406 407 static void 408 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int); 409 410 static void 411 d_print_mod (struct d_print_info *, const struct demangle_component *); 412 413 static void 414 d_print_function_type (struct d_print_info *, 415 const struct demangle_component *, 416 struct d_print_mod *); 417 418 static void 419 d_print_array_type (struct d_print_info *, 420 const struct demangle_component *, 421 struct d_print_mod *); 422 423 static void 424 d_print_expr_op (struct d_print_info *, const struct demangle_component *); 425 426 static void 427 d_print_cast (struct d_print_info *, const struct demangle_component *); 428 429 static char *d_demangle (const char *, int, size_t *); 430 431 #ifdef CP_DEMANGLE_DEBUG 432 433 static void 434 d_dump (struct demangle_component *dc, int indent) 435 { 436 int i; 437 438 if (dc == NULL) 439 return; 440 441 for (i = 0; i < indent; ++i) 442 putchar (' '); 443 444 switch (dc->type) 445 { 446 case DEMANGLE_COMPONENT_NAME: 447 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s); 448 return; 449 case DEMANGLE_COMPONENT_TEMPLATE_PARAM: 450 printf ("template parameter %ld\n", dc->u.s_number.number); 451 return; 452 case DEMANGLE_COMPONENT_CTOR: 453 printf ("constructor %d\n", (int) dc->u.s_ctor.kind); 454 d_dump (dc->u.s_ctor.name, indent + 2); 455 return; 456 case DEMANGLE_COMPONENT_DTOR: 457 printf ("destructor %d\n", (int) dc->u.s_dtor.kind); 458 d_dump (dc->u.s_dtor.name, indent + 2); 459 return; 460 case DEMANGLE_COMPONENT_SUB_STD: 461 printf ("standard substitution %s\n", dc->u.s_string.string); 462 return; 463 case DEMANGLE_COMPONENT_BUILTIN_TYPE: 464 printf ("builtin type %s\n", dc->u.s_builtin.type->name); 465 return; 466 case DEMANGLE_COMPONENT_OPERATOR: 467 printf ("operator %s\n", dc->u.s_operator.op->name); 468 return; 469 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 470 printf ("extended operator with %d args\n", 471 dc->u.s_extended_operator.args); 472 d_dump (dc->u.s_extended_operator.name, indent + 2); 473 return; 474 475 case DEMANGLE_COMPONENT_QUAL_NAME: 476 printf ("qualified name\n"); 477 break; 478 case DEMANGLE_COMPONENT_LOCAL_NAME: 479 printf ("local name\n"); 480 break; 481 case DEMANGLE_COMPONENT_TYPED_NAME: 482 printf ("typed name\n"); 483 break; 484 case DEMANGLE_COMPONENT_TEMPLATE: 485 printf ("template\n"); 486 break; 487 case DEMANGLE_COMPONENT_VTABLE: 488 printf ("vtable\n"); 489 break; 490 case DEMANGLE_COMPONENT_VTT: 491 printf ("VTT\n"); 492 break; 493 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: 494 printf ("construction vtable\n"); 495 break; 496 case DEMANGLE_COMPONENT_TYPEINFO: 497 printf ("typeinfo\n"); 498 break; 499 case DEMANGLE_COMPONENT_TYPEINFO_NAME: 500 printf ("typeinfo name\n"); 501 break; 502 case DEMANGLE_COMPONENT_TYPEINFO_FN: 503 printf ("typeinfo function\n"); 504 break; 505 case DEMANGLE_COMPONENT_THUNK: 506 printf ("thunk\n"); 507 break; 508 case DEMANGLE_COMPONENT_VIRTUAL_THUNK: 509 printf ("virtual thunk\n"); 510 break; 511 case DEMANGLE_COMPONENT_COVARIANT_THUNK: 512 printf ("covariant thunk\n"); 513 break; 514 case DEMANGLE_COMPONENT_JAVA_CLASS: 515 printf ("java class\n"); 516 break; 517 case DEMANGLE_COMPONENT_GUARD: 518 printf ("guard\n"); 519 break; 520 case DEMANGLE_COMPONENT_REFTEMP: 521 printf ("reference temporary\n"); 522 break; 523 case DEMANGLE_COMPONENT_HIDDEN_ALIAS: 524 printf ("hidden alias\n"); 525 break; 526 case DEMANGLE_COMPONENT_RESTRICT: 527 printf ("restrict\n"); 528 break; 529 case DEMANGLE_COMPONENT_VOLATILE: 530 printf ("volatile\n"); 531 break; 532 case DEMANGLE_COMPONENT_CONST: 533 printf ("const\n"); 534 break; 535 case DEMANGLE_COMPONENT_RESTRICT_THIS: 536 printf ("restrict this\n"); 537 break; 538 case DEMANGLE_COMPONENT_VOLATILE_THIS: 539 printf ("volatile this\n"); 540 break; 541 case DEMANGLE_COMPONENT_CONST_THIS: 542 printf ("const this\n"); 543 break; 544 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 545 printf ("vendor type qualifier\n"); 546 break; 547 case DEMANGLE_COMPONENT_POINTER: 548 printf ("pointer\n"); 549 break; 550 case DEMANGLE_COMPONENT_REFERENCE: 551 printf ("reference\n"); 552 break; 553 case DEMANGLE_COMPONENT_COMPLEX: 554 printf ("complex\n"); 555 break; 556 case DEMANGLE_COMPONENT_IMAGINARY: 557 printf ("imaginary\n"); 558 break; 559 case DEMANGLE_COMPONENT_VENDOR_TYPE: 560 printf ("vendor type\n"); 561 break; 562 case DEMANGLE_COMPONENT_FUNCTION_TYPE: 563 printf ("function type\n"); 564 break; 565 case DEMANGLE_COMPONENT_ARRAY_TYPE: 566 printf ("array type\n"); 567 break; 568 case DEMANGLE_COMPONENT_PTRMEM_TYPE: 569 printf ("pointer to member type\n"); 570 break; 571 case DEMANGLE_COMPONENT_ARGLIST: 572 printf ("argument list\n"); 573 break; 574 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: 575 printf ("template argument list\n"); 576 break; 577 case DEMANGLE_COMPONENT_CAST: 578 printf ("cast\n"); 579 break; 580 case DEMANGLE_COMPONENT_UNARY: 581 printf ("unary operator\n"); 582 break; 583 case DEMANGLE_COMPONENT_BINARY: 584 printf ("binary operator\n"); 585 break; 586 case DEMANGLE_COMPONENT_BINARY_ARGS: 587 printf ("binary operator arguments\n"); 588 break; 589 case DEMANGLE_COMPONENT_TRINARY: 590 printf ("trinary operator\n"); 591 break; 592 case DEMANGLE_COMPONENT_TRINARY_ARG1: 593 printf ("trinary operator arguments 1\n"); 594 break; 595 case DEMANGLE_COMPONENT_TRINARY_ARG2: 596 printf ("trinary operator arguments 1\n"); 597 break; 598 case DEMANGLE_COMPONENT_LITERAL: 599 printf ("literal\n"); 600 break; 601 case DEMANGLE_COMPONENT_LITERAL_NEG: 602 printf ("negative literal\n"); 603 break; 604 } 605 606 d_dump (d_left (dc), indent + 2); 607 d_dump (d_right (dc), indent + 2); 608 } 609 610 #endif /* CP_DEMANGLE_DEBUG */ 611 612 /* Fill in a DEMANGLE_COMPONENT_NAME. */ 613 614 CP_STATIC_IF_GLIBCPP_V3 615 int 616 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len) 617 { 618 if (p == NULL || s == NULL || len == 0) 619 return 0; 620 p->type = DEMANGLE_COMPONENT_NAME; 621 p->u.s_name.s = s; 622 p->u.s_name.len = len; 623 return 1; 624 } 625 626 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */ 627 628 CP_STATIC_IF_GLIBCPP_V3 629 int 630 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args, 631 struct demangle_component *name) 632 { 633 if (p == NULL || args < 0 || name == NULL) 634 return 0; 635 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR; 636 p->u.s_extended_operator.args = args; 637 p->u.s_extended_operator.name = name; 638 return 1; 639 } 640 641 /* Fill in a DEMANGLE_COMPONENT_CTOR. */ 642 643 CP_STATIC_IF_GLIBCPP_V3 644 int 645 cplus_demangle_fill_ctor (struct demangle_component *p, 646 enum gnu_v3_ctor_kinds kind, 647 struct demangle_component *name) 648 { 649 if (p == NULL 650 || name == NULL 651 || (kind < gnu_v3_complete_object_ctor 652 && kind > gnu_v3_complete_object_allocating_ctor)) 653 return 0; 654 p->type = DEMANGLE_COMPONENT_CTOR; 655 p->u.s_ctor.kind = kind; 656 p->u.s_ctor.name = name; 657 return 1; 658 } 659 660 /* Fill in a DEMANGLE_COMPONENT_DTOR. */ 661 662 CP_STATIC_IF_GLIBCPP_V3 663 int 664 cplus_demangle_fill_dtor (struct demangle_component *p, 665 enum gnu_v3_dtor_kinds kind, 666 struct demangle_component *name) 667 { 668 if (p == NULL 669 || name == NULL 670 || (kind < gnu_v3_deleting_dtor 671 && kind > gnu_v3_base_object_dtor)) 672 return 0; 673 p->type = DEMANGLE_COMPONENT_DTOR; 674 p->u.s_dtor.kind = kind; 675 p->u.s_dtor.name = name; 676 return 1; 677 } 678 679 /* Add a new component. */ 680 681 static struct demangle_component * 682 d_make_empty (struct d_info *di) 683 { 684 struct demangle_component *p; 685 686 if (di->next_comp >= di->num_comps) 687 return NULL; 688 p = &di->comps[di->next_comp]; 689 ++di->next_comp; 690 return p; 691 } 692 693 /* Add a new generic component. */ 694 695 static struct demangle_component * 696 d_make_comp (struct d_info *di, enum demangle_component_type type, 697 struct demangle_component *left, 698 struct demangle_component *right) 699 { 700 struct demangle_component *p; 701 702 /* We check for errors here. A typical error would be a NULL return 703 from a subroutine. We catch those here, and return NULL 704 upward. */ 705 switch (type) 706 { 707 /* These types require two parameters. */ 708 case DEMANGLE_COMPONENT_QUAL_NAME: 709 case DEMANGLE_COMPONENT_LOCAL_NAME: 710 case DEMANGLE_COMPONENT_TYPED_NAME: 711 case DEMANGLE_COMPONENT_TEMPLATE: 712 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: 713 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 714 case DEMANGLE_COMPONENT_PTRMEM_TYPE: 715 case DEMANGLE_COMPONENT_UNARY: 716 case DEMANGLE_COMPONENT_BINARY: 717 case DEMANGLE_COMPONENT_BINARY_ARGS: 718 case DEMANGLE_COMPONENT_TRINARY: 719 case DEMANGLE_COMPONENT_TRINARY_ARG1: 720 case DEMANGLE_COMPONENT_TRINARY_ARG2: 721 case DEMANGLE_COMPONENT_LITERAL: 722 case DEMANGLE_COMPONENT_LITERAL_NEG: 723 if (left == NULL || right == NULL) 724 return NULL; 725 break; 726 727 /* These types only require one parameter. */ 728 case DEMANGLE_COMPONENT_VTABLE: 729 case DEMANGLE_COMPONENT_VTT: 730 case DEMANGLE_COMPONENT_TYPEINFO: 731 case DEMANGLE_COMPONENT_TYPEINFO_NAME: 732 case DEMANGLE_COMPONENT_TYPEINFO_FN: 733 case DEMANGLE_COMPONENT_THUNK: 734 case DEMANGLE_COMPONENT_VIRTUAL_THUNK: 735 case DEMANGLE_COMPONENT_COVARIANT_THUNK: 736 case DEMANGLE_COMPONENT_JAVA_CLASS: 737 case DEMANGLE_COMPONENT_GUARD: 738 case DEMANGLE_COMPONENT_REFTEMP: 739 case DEMANGLE_COMPONENT_HIDDEN_ALIAS: 740 case DEMANGLE_COMPONENT_POINTER: 741 case DEMANGLE_COMPONENT_REFERENCE: 742 case DEMANGLE_COMPONENT_COMPLEX: 743 case DEMANGLE_COMPONENT_IMAGINARY: 744 case DEMANGLE_COMPONENT_VENDOR_TYPE: 745 case DEMANGLE_COMPONENT_ARGLIST: 746 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: 747 case DEMANGLE_COMPONENT_CAST: 748 if (left == NULL) 749 return NULL; 750 break; 751 752 /* This needs a right parameter, but the left parameter can be 753 empty. */ 754 case DEMANGLE_COMPONENT_ARRAY_TYPE: 755 if (right == NULL) 756 return NULL; 757 break; 758 759 /* These are allowed to have no parameters--in some cases they 760 will be filled in later. */ 761 case DEMANGLE_COMPONENT_FUNCTION_TYPE: 762 case DEMANGLE_COMPONENT_RESTRICT: 763 case DEMANGLE_COMPONENT_VOLATILE: 764 case DEMANGLE_COMPONENT_CONST: 765 case DEMANGLE_COMPONENT_RESTRICT_THIS: 766 case DEMANGLE_COMPONENT_VOLATILE_THIS: 767 case DEMANGLE_COMPONENT_CONST_THIS: 768 break; 769 770 /* Other types should not be seen here. */ 771 default: 772 return NULL; 773 } 774 775 p = d_make_empty (di); 776 if (p != NULL) 777 { 778 p->type = type; 779 p->u.s_binary.left = left; 780 p->u.s_binary.right = right; 781 } 782 return p; 783 } 784 785 /* Add a new name component. */ 786 787 static struct demangle_component * 788 d_make_name (struct d_info *di, const char *s, int len) 789 { 790 struct demangle_component *p; 791 792 p = d_make_empty (di); 793 if (! cplus_demangle_fill_name (p, s, len)) 794 return NULL; 795 return p; 796 } 797 798 /* Add a new builtin type component. */ 799 800 static struct demangle_component * 801 d_make_builtin_type (struct d_info *di, 802 const struct demangle_builtin_type_info *type) 803 { 804 struct demangle_component *p; 805 806 if (type == NULL) 807 return NULL; 808 p = d_make_empty (di); 809 if (p != NULL) 810 { 811 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE; 812 p->u.s_builtin.type = type; 813 } 814 return p; 815 } 816 817 /* Add a new operator component. */ 818 819 static struct demangle_component * 820 d_make_operator (struct d_info *di, const struct demangle_operator_info *op) 821 { 822 struct demangle_component *p; 823 824 p = d_make_empty (di); 825 if (p != NULL) 826 { 827 p->type = DEMANGLE_COMPONENT_OPERATOR; 828 p->u.s_operator.op = op; 829 } 830 return p; 831 } 832 833 /* Add a new extended operator component. */ 834 835 static struct demangle_component * 836 d_make_extended_operator (struct d_info *di, int args, 837 struct demangle_component *name) 838 { 839 struct demangle_component *p; 840 841 p = d_make_empty (di); 842 if (! cplus_demangle_fill_extended_operator (p, args, name)) 843 return NULL; 844 return p; 845 } 846 847 /* Add a new constructor component. */ 848 849 static struct demangle_component * 850 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind, 851 struct demangle_component *name) 852 { 853 struct demangle_component *p; 854 855 p = d_make_empty (di); 856 if (! cplus_demangle_fill_ctor (p, kind, name)) 857 return NULL; 858 return p; 859 } 860 861 /* Add a new destructor component. */ 862 863 static struct demangle_component * 864 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind, 865 struct demangle_component *name) 866 { 867 struct demangle_component *p; 868 869 p = d_make_empty (di); 870 if (! cplus_demangle_fill_dtor (p, kind, name)) 871 return NULL; 872 return p; 873 } 874 875 /* Add a new template parameter. */ 876 877 static struct demangle_component * 878 d_make_template_param (struct d_info *di, long i) 879 { 880 struct demangle_component *p; 881 882 p = d_make_empty (di); 883 if (p != NULL) 884 { 885 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM; 886 p->u.s_number.number = i; 887 } 888 return p; 889 } 890 891 /* Add a new standard substitution component. */ 892 893 static struct demangle_component * 894 d_make_sub (struct d_info *di, const char *name, int len) 895 { 896 struct demangle_component *p; 897 898 p = d_make_empty (di); 899 if (p != NULL) 900 { 901 p->type = DEMANGLE_COMPONENT_SUB_STD; 902 p->u.s_string.string = name; 903 p->u.s_string.len = len; 904 } 905 return p; 906 } 907 908 /* <mangled-name> ::= _Z <encoding> 909 910 TOP_LEVEL is non-zero when called at the top level. */ 911 912 CP_STATIC_IF_GLIBCPP_V3 913 struct demangle_component * 914 cplus_demangle_mangled_name (struct d_info *di, int top_level) 915 { 916 if (d_next_char (di) != '_') 917 return NULL; 918 if (d_next_char (di) != 'Z') 919 return NULL; 920 return d_encoding (di, top_level); 921 } 922 923 /* Return whether a function should have a return type. The argument 924 is the function name, which may be qualified in various ways. The 925 rules are that template functions have return types with some 926 exceptions, function types which are not part of a function name 927 mangling have return types with some exceptions, and non-template 928 function names do not have return types. The exceptions are that 929 constructors, destructors, and conversion operators do not have 930 return types. */ 931 932 static int 933 has_return_type (struct demangle_component *dc) 934 { 935 if (dc == NULL) 936 return 0; 937 switch (dc->type) 938 { 939 default: 940 return 0; 941 case DEMANGLE_COMPONENT_TEMPLATE: 942 return ! is_ctor_dtor_or_conversion (d_left (dc)); 943 case DEMANGLE_COMPONENT_RESTRICT_THIS: 944 case DEMANGLE_COMPONENT_VOLATILE_THIS: 945 case DEMANGLE_COMPONENT_CONST_THIS: 946 return has_return_type (d_left (dc)); 947 } 948 } 949 950 /* Return whether a name is a constructor, a destructor, or a 951 conversion operator. */ 952 953 static int 954 is_ctor_dtor_or_conversion (struct demangle_component *dc) 955 { 956 if (dc == NULL) 957 return 0; 958 switch (dc->type) 959 { 960 default: 961 return 0; 962 case DEMANGLE_COMPONENT_QUAL_NAME: 963 case DEMANGLE_COMPONENT_LOCAL_NAME: 964 return is_ctor_dtor_or_conversion (d_right (dc)); 965 case DEMANGLE_COMPONENT_CTOR: 966 case DEMANGLE_COMPONENT_DTOR: 967 case DEMANGLE_COMPONENT_CAST: 968 return 1; 969 } 970 } 971 972 /* <encoding> ::= <(function) name> <bare-function-type> 973 ::= <(data) name> 974 ::= <special-name> 975 976 TOP_LEVEL is non-zero when called at the top level, in which case 977 if DMGL_PARAMS is not set we do not demangle the function 978 parameters. We only set this at the top level, because otherwise 979 we would not correctly demangle names in local scopes. */ 980 981 static struct demangle_component * 982 d_encoding (struct d_info *di, int top_level) 983 { 984 char peek = d_peek_char (di); 985 986 if (peek == 'G' || peek == 'T') 987 return d_special_name (di); 988 else 989 { 990 struct demangle_component *dc; 991 992 dc = d_name (di); 993 994 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0) 995 { 996 /* Strip off any initial CV-qualifiers, as they really apply 997 to the `this' parameter, and they were not output by the 998 v2 demangler without DMGL_PARAMS. */ 999 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS 1000 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS 1001 || dc->type == DEMANGLE_COMPONENT_CONST_THIS) 1002 dc = d_left (dc); 1003 1004 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then 1005 there may be CV-qualifiers on its right argument which 1006 really apply here; this happens when parsing a class 1007 which is local to a function. */ 1008 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME) 1009 { 1010 struct demangle_component *dcr; 1011 1012 dcr = d_right (dc); 1013 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS 1014 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS 1015 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS) 1016 dcr = d_left (dcr); 1017 dc->u.s_binary.right = dcr; 1018 } 1019 1020 return dc; 1021 } 1022 1023 peek = d_peek_char (di); 1024 if (peek == '\0' || peek == 'E') 1025 return dc; 1026 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc, 1027 d_bare_function_type (di, has_return_type (dc))); 1028 } 1029 } 1030 1031 /* <name> ::= <nested-name> 1032 ::= <unscoped-name> 1033 ::= <unscoped-template-name> <template-args> 1034 ::= <local-name> 1035 1036 <unscoped-name> ::= <unqualified-name> 1037 ::= St <unqualified-name> 1038 1039 <unscoped-template-name> ::= <unscoped-name> 1040 ::= <substitution> 1041 */ 1042 1043 static struct demangle_component * 1044 d_name (struct d_info *di) 1045 { 1046 char peek = d_peek_char (di); 1047 struct demangle_component *dc; 1048 1049 switch (peek) 1050 { 1051 case 'N': 1052 return d_nested_name (di); 1053 1054 case 'Z': 1055 return d_local_name (di); 1056 1057 case 'S': 1058 { 1059 int subst; 1060 1061 if (d_peek_next_char (di) != 't') 1062 { 1063 dc = d_substitution (di, 0); 1064 subst = 1; 1065 } 1066 else 1067 { 1068 d_advance (di, 2); 1069 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, 1070 d_make_name (di, "std", 3), 1071 d_unqualified_name (di)); 1072 di->expansion += 3; 1073 subst = 0; 1074 } 1075 1076 if (d_peek_char (di) != 'I') 1077 { 1078 /* The grammar does not permit this case to occur if we 1079 called d_substitution() above (i.e., subst == 1). We 1080 don't bother to check. */ 1081 } 1082 else 1083 { 1084 /* This is <template-args>, which means that we just saw 1085 <unscoped-template-name>, which is a substitution 1086 candidate if we didn't just get it from a 1087 substitution. */ 1088 if (! subst) 1089 { 1090 if (! d_add_substitution (di, dc)) 1091 return NULL; 1092 } 1093 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc, 1094 d_template_args (di)); 1095 } 1096 1097 return dc; 1098 } 1099 1100 default: 1101 dc = d_unqualified_name (di); 1102 if (d_peek_char (di) == 'I') 1103 { 1104 /* This is <template-args>, which means that we just saw 1105 <unscoped-template-name>, which is a substitution 1106 candidate. */ 1107 if (! d_add_substitution (di, dc)) 1108 return NULL; 1109 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc, 1110 d_template_args (di)); 1111 } 1112 return dc; 1113 } 1114 } 1115 1116 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E 1117 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E 1118 */ 1119 1120 static struct demangle_component * 1121 d_nested_name (struct d_info *di) 1122 { 1123 struct demangle_component *ret; 1124 struct demangle_component **pret; 1125 1126 if (d_next_char (di) != 'N') 1127 return NULL; 1128 1129 pret = d_cv_qualifiers (di, &ret, 1); 1130 if (pret == NULL) 1131 return NULL; 1132 1133 *pret = d_prefix (di); 1134 if (*pret == NULL) 1135 return NULL; 1136 1137 if (d_next_char (di) != 'E') 1138 return NULL; 1139 1140 return ret; 1141 } 1142 1143 /* <prefix> ::= <prefix> <unqualified-name> 1144 ::= <template-prefix> <template-args> 1145 ::= <template-param> 1146 ::= 1147 ::= <substitution> 1148 1149 <template-prefix> ::= <prefix> <(template) unqualified-name> 1150 ::= <template-param> 1151 ::= <substitution> 1152 */ 1153 1154 static struct demangle_component * 1155 d_prefix (struct d_info *di) 1156 { 1157 struct demangle_component *ret = NULL; 1158 1159 while (1) 1160 { 1161 char peek; 1162 enum demangle_component_type comb_type; 1163 struct demangle_component *dc; 1164 1165 peek = d_peek_char (di); 1166 if (peek == '\0') 1167 return NULL; 1168 1169 /* The older code accepts a <local-name> here, but I don't see 1170 that in the grammar. The older code does not accept a 1171 <template-param> here. */ 1172 1173 comb_type = DEMANGLE_COMPONENT_QUAL_NAME; 1174 if (IS_DIGIT (peek) 1175 || IS_LOWER (peek) 1176 || peek == 'C' 1177 || peek == 'D') 1178 dc = d_unqualified_name (di); 1179 else if (peek == 'S') 1180 dc = d_substitution (di, 1); 1181 else if (peek == 'I') 1182 { 1183 if (ret == NULL) 1184 return NULL; 1185 comb_type = DEMANGLE_COMPONENT_TEMPLATE; 1186 dc = d_template_args (di); 1187 } 1188 else if (peek == 'T') 1189 dc = d_template_param (di); 1190 else if (peek == 'E') 1191 return ret; 1192 else 1193 return NULL; 1194 1195 if (ret == NULL) 1196 ret = dc; 1197 else 1198 ret = d_make_comp (di, comb_type, ret, dc); 1199 1200 if (peek != 'S' && d_peek_char (di) != 'E') 1201 { 1202 if (! d_add_substitution (di, ret)) 1203 return NULL; 1204 } 1205 } 1206 } 1207 1208 /* <unqualified-name> ::= <operator-name> 1209 ::= <ctor-dtor-name> 1210 ::= <source-name> 1211 */ 1212 1213 static struct demangle_component * 1214 d_unqualified_name (struct d_info *di) 1215 { 1216 char peek; 1217 1218 peek = d_peek_char (di); 1219 if (IS_DIGIT (peek)) 1220 return d_source_name (di); 1221 else if (IS_LOWER (peek)) 1222 { 1223 struct demangle_component *ret; 1224 1225 ret = d_operator_name (di); 1226 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR) 1227 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2; 1228 return ret; 1229 } 1230 else if (peek == 'C' || peek == 'D') 1231 return d_ctor_dtor_name (di); 1232 else 1233 return NULL; 1234 } 1235 1236 /* <source-name> ::= <(positive length) number> <identifier> */ 1237 1238 static struct demangle_component * 1239 d_source_name (struct d_info *di) 1240 { 1241 long len; 1242 struct demangle_component *ret; 1243 1244 len = d_number (di); 1245 if (len <= 0) 1246 return NULL; 1247 ret = d_identifier (di, len); 1248 di->last_name = ret; 1249 return ret; 1250 } 1251 1252 /* number ::= [n] <(non-negative decimal integer)> */ 1253 1254 static long 1255 d_number (struct d_info *di) 1256 { 1257 int negative; 1258 char peek; 1259 long ret; 1260 1261 negative = 0; 1262 peek = d_peek_char (di); 1263 if (peek == 'n') 1264 { 1265 negative = 1; 1266 d_advance (di, 1); 1267 peek = d_peek_char (di); 1268 } 1269 1270 ret = 0; 1271 while (1) 1272 { 1273 if (! IS_DIGIT (peek)) 1274 { 1275 if (negative) 1276 ret = - ret; 1277 return ret; 1278 } 1279 ret = ret * 10 + peek - '0'; 1280 d_advance (di, 1); 1281 peek = d_peek_char (di); 1282 } 1283 } 1284 1285 /* identifier ::= <(unqualified source code identifier)> */ 1286 1287 static struct demangle_component * 1288 d_identifier (struct d_info *di, int len) 1289 { 1290 const char *name; 1291 1292 name = d_str (di); 1293 1294 if (di->send - name < len) 1295 return NULL; 1296 1297 d_advance (di, len); 1298 1299 /* A Java mangled name may have a trailing '$' if it is a C++ 1300 keyword. This '$' is not included in the length count. We just 1301 ignore the '$'. */ 1302 if ((di->options & DMGL_JAVA) != 0 1303 && d_peek_char (di) == '$') 1304 d_advance (di, 1); 1305 1306 /* Look for something which looks like a gcc encoding of an 1307 anonymous namespace, and replace it with a more user friendly 1308 name. */ 1309 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2 1310 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX, 1311 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0) 1312 { 1313 const char *s; 1314 1315 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN; 1316 if ((*s == '.' || *s == '_' || *s == '$') 1317 && s[1] == 'N') 1318 { 1319 di->expansion -= len - sizeof "(anonymous namespace)"; 1320 return d_make_name (di, "(anonymous namespace)", 1321 sizeof "(anonymous namespace)" - 1); 1322 } 1323 } 1324 1325 return d_make_name (di, name, len); 1326 } 1327 1328 /* operator_name ::= many different two character encodings. 1329 ::= cv <type> 1330 ::= v <digit> <source-name> 1331 */ 1332 1333 #define NL(s) s, (sizeof s) - 1 1334 1335 CP_STATIC_IF_GLIBCPP_V3 1336 const struct demangle_operator_info cplus_demangle_operators[] = 1337 { 1338 { "aN", NL ("&="), 2 }, 1339 { "aS", NL ("="), 2 }, 1340 { "aa", NL ("&&"), 2 }, 1341 { "ad", NL ("&"), 1 }, 1342 { "an", NL ("&"), 2 }, 1343 { "cl", NL ("()"), 0 }, 1344 { "cm", NL (","), 2 }, 1345 { "co", NL ("~"), 1 }, 1346 { "dV", NL ("/="), 2 }, 1347 { "da", NL ("delete[]"), 1 }, 1348 { "de", NL ("*"), 1 }, 1349 { "dl", NL ("delete"), 1 }, 1350 { "dv", NL ("/"), 2 }, 1351 { "eO", NL ("^="), 2 }, 1352 { "eo", NL ("^"), 2 }, 1353 { "eq", NL ("=="), 2 }, 1354 { "ge", NL (">="), 2 }, 1355 { "gt", NL (">"), 2 }, 1356 { "ix", NL ("[]"), 2 }, 1357 { "lS", NL ("<<="), 2 }, 1358 { "le", NL ("<="), 2 }, 1359 { "ls", NL ("<<"), 2 }, 1360 { "lt", NL ("<"), 2 }, 1361 { "mI", NL ("-="), 2 }, 1362 { "mL", NL ("*="), 2 }, 1363 { "mi", NL ("-"), 2 }, 1364 { "ml", NL ("*"), 2 }, 1365 { "mm", NL ("--"), 1 }, 1366 { "na", NL ("new[]"), 1 }, 1367 { "ne", NL ("!="), 2 }, 1368 { "ng", NL ("-"), 1 }, 1369 { "nt", NL ("!"), 1 }, 1370 { "nw", NL ("new"), 1 }, 1371 { "oR", NL ("|="), 2 }, 1372 { "oo", NL ("||"), 2 }, 1373 { "or", NL ("|"), 2 }, 1374 { "pL", NL ("+="), 2 }, 1375 { "pl", NL ("+"), 2 }, 1376 { "pm", NL ("->*"), 2 }, 1377 { "pp", NL ("++"), 1 }, 1378 { "ps", NL ("+"), 1 }, 1379 { "pt", NL ("->"), 2 }, 1380 { "qu", NL ("?"), 3 }, 1381 { "rM", NL ("%="), 2 }, 1382 { "rS", NL (">>="), 2 }, 1383 { "rm", NL ("%"), 2 }, 1384 { "rs", NL (">>"), 2 }, 1385 { "st", NL ("sizeof "), 1 }, 1386 { "sz", NL ("sizeof "), 1 }, 1387 { NULL, NULL, 0, 0 } 1388 }; 1389 1390 static struct demangle_component * 1391 d_operator_name (struct d_info *di) 1392 { 1393 char c1; 1394 char c2; 1395 1396 c1 = d_next_char (di); 1397 c2 = d_next_char (di); 1398 if (c1 == 'v' && IS_DIGIT (c2)) 1399 return d_make_extended_operator (di, c2 - '0', d_source_name (di)); 1400 else if (c1 == 'c' && c2 == 'v') 1401 return d_make_comp (di, DEMANGLE_COMPONENT_CAST, 1402 cplus_demangle_type (di), NULL); 1403 else 1404 { 1405 /* LOW is the inclusive lower bound. */ 1406 int low = 0; 1407 /* HIGH is the exclusive upper bound. We subtract one to ignore 1408 the sentinel at the end of the array. */ 1409 int high = ((sizeof (cplus_demangle_operators) 1410 / sizeof (cplus_demangle_operators[0])) 1411 - 1); 1412 1413 while (1) 1414 { 1415 int i; 1416 const struct demangle_operator_info *p; 1417 1418 i = low + (high - low) / 2; 1419 p = cplus_demangle_operators + i; 1420 1421 if (c1 == p->code[0] && c2 == p->code[1]) 1422 return d_make_operator (di, p); 1423 1424 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1])) 1425 high = i; 1426 else 1427 low = i + 1; 1428 if (low == high) 1429 return NULL; 1430 } 1431 } 1432 } 1433 1434 /* <special-name> ::= TV <type> 1435 ::= TT <type> 1436 ::= TI <type> 1437 ::= TS <type> 1438 ::= GV <(object) name> 1439 ::= T <call-offset> <(base) encoding> 1440 ::= Tc <call-offset> <call-offset> <(base) encoding> 1441 Also g++ extensions: 1442 ::= TC <type> <(offset) number> _ <(base) type> 1443 ::= TF <type> 1444 ::= TJ <type> 1445 ::= GR <name> 1446 ::= GA <encoding> 1447 */ 1448 1449 static struct demangle_component * 1450 d_special_name (struct d_info *di) 1451 { 1452 char c; 1453 1454 di->expansion += 20; 1455 c = d_next_char (di); 1456 if (c == 'T') 1457 { 1458 switch (d_next_char (di)) 1459 { 1460 case 'V': 1461 di->expansion -= 5; 1462 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE, 1463 cplus_demangle_type (di), NULL); 1464 case 'T': 1465 di->expansion -= 10; 1466 return d_make_comp (di, DEMANGLE_COMPONENT_VTT, 1467 cplus_demangle_type (di), NULL); 1468 case 'I': 1469 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO, 1470 cplus_demangle_type (di), NULL); 1471 case 'S': 1472 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME, 1473 cplus_demangle_type (di), NULL); 1474 1475 case 'h': 1476 if (! d_call_offset (di, 'h')) 1477 return NULL; 1478 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK, 1479 d_encoding (di, 0), NULL); 1480 1481 case 'v': 1482 if (! d_call_offset (di, 'v')) 1483 return NULL; 1484 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK, 1485 d_encoding (di, 0), NULL); 1486 1487 case 'c': 1488 if (! d_call_offset (di, '\0')) 1489 return NULL; 1490 if (! d_call_offset (di, '\0')) 1491 return NULL; 1492 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK, 1493 d_encoding (di, 0), NULL); 1494 1495 case 'C': 1496 { 1497 struct demangle_component *derived_type; 1498 long offset; 1499 struct demangle_component *base_type; 1500 1501 derived_type = cplus_demangle_type (di); 1502 offset = d_number (di); 1503 if (offset < 0) 1504 return NULL; 1505 if (d_next_char (di) != '_') 1506 return NULL; 1507 base_type = cplus_demangle_type (di); 1508 /* We don't display the offset. FIXME: We should display 1509 it in verbose mode. */ 1510 di->expansion += 5; 1511 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, 1512 base_type, derived_type); 1513 } 1514 1515 case 'F': 1516 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN, 1517 cplus_demangle_type (di), NULL); 1518 case 'J': 1519 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS, 1520 cplus_demangle_type (di), NULL); 1521 1522 default: 1523 return NULL; 1524 } 1525 } 1526 else if (c == 'G') 1527 { 1528 switch (d_next_char (di)) 1529 { 1530 case 'V': 1531 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL); 1532 1533 case 'R': 1534 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di), 1535 NULL); 1536 1537 case 'A': 1538 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS, 1539 d_encoding (di, 0), NULL); 1540 1541 default: 1542 return NULL; 1543 } 1544 } 1545 else 1546 return NULL; 1547 } 1548 1549 /* <call-offset> ::= h <nv-offset> _ 1550 ::= v <v-offset> _ 1551 1552 <nv-offset> ::= <(offset) number> 1553 1554 <v-offset> ::= <(offset) number> _ <(virtual offset) number> 1555 1556 The C parameter, if not '\0', is a character we just read which is 1557 the start of the <call-offset>. 1558 1559 We don't display the offset information anywhere. FIXME: We should 1560 display it in verbose mode. */ 1561 1562 static int 1563 d_call_offset (struct d_info *di, int c) 1564 { 1565 if (c == '\0') 1566 c = d_next_char (di); 1567 1568 if (c == 'h') 1569 d_number (di); 1570 else if (c == 'v') 1571 { 1572 d_number (di); 1573 if (d_next_char (di) != '_') 1574 return 0; 1575 d_number (di); 1576 } 1577 else 1578 return 0; 1579 1580 if (d_next_char (di) != '_') 1581 return 0; 1582 1583 return 1; 1584 } 1585 1586 /* <ctor-dtor-name> ::= C1 1587 ::= C2 1588 ::= C3 1589 ::= D0 1590 ::= D1 1591 ::= D2 1592 */ 1593 1594 static struct demangle_component * 1595 d_ctor_dtor_name (struct d_info *di) 1596 { 1597 if (di->last_name != NULL) 1598 { 1599 if (di->last_name->type == DEMANGLE_COMPONENT_NAME) 1600 di->expansion += di->last_name->u.s_name.len; 1601 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD) 1602 di->expansion += di->last_name->u.s_string.len; 1603 } 1604 switch (d_next_char (di)) 1605 { 1606 case 'C': 1607 { 1608 enum gnu_v3_ctor_kinds kind; 1609 1610 switch (d_next_char (di)) 1611 { 1612 case '1': 1613 kind = gnu_v3_complete_object_ctor; 1614 break; 1615 case '2': 1616 kind = gnu_v3_base_object_ctor; 1617 break; 1618 case '3': 1619 kind = gnu_v3_complete_object_allocating_ctor; 1620 break; 1621 default: 1622 return NULL; 1623 } 1624 return d_make_ctor (di, kind, di->last_name); 1625 } 1626 1627 case 'D': 1628 { 1629 enum gnu_v3_dtor_kinds kind; 1630 1631 switch (d_next_char (di)) 1632 { 1633 case '0': 1634 kind = gnu_v3_deleting_dtor; 1635 break; 1636 case '1': 1637 kind = gnu_v3_complete_object_dtor; 1638 break; 1639 case '2': 1640 kind = gnu_v3_base_object_dtor; 1641 break; 1642 default: 1643 return NULL; 1644 } 1645 return d_make_dtor (di, kind, di->last_name); 1646 } 1647 1648 default: 1649 return NULL; 1650 } 1651 } 1652 1653 /* <type> ::= <builtin-type> 1654 ::= <function-type> 1655 ::= <class-enum-type> 1656 ::= <array-type> 1657 ::= <pointer-to-member-type> 1658 ::= <template-param> 1659 ::= <template-template-param> <template-args> 1660 ::= <substitution> 1661 ::= <CV-qualifiers> <type> 1662 ::= P <type> 1663 ::= R <type> 1664 ::= C <type> 1665 ::= G <type> 1666 ::= U <source-name> <type> 1667 1668 <builtin-type> ::= various one letter codes 1669 ::= u <source-name> 1670 */ 1671 1672 CP_STATIC_IF_GLIBCPP_V3 1673 const struct demangle_builtin_type_info 1674 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] = 1675 { 1676 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT }, 1677 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL }, 1678 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT }, 1679 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT }, 1680 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT }, 1681 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT }, 1682 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT }, 1683 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT }, 1684 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT }, 1685 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED }, 1686 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, 1687 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG }, 1688 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG }, 1689 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT }, 1690 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"), 1691 D_PRINT_DEFAULT }, 1692 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, 1693 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, 1694 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, 1695 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT }, 1696 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT }, 1697 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, 1698 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID }, 1699 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT }, 1700 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG }, 1701 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"), 1702 D_PRINT_UNSIGNED_LONG_LONG }, 1703 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT }, 1704 }; 1705 1706 CP_STATIC_IF_GLIBCPP_V3 1707 struct demangle_component * 1708 cplus_demangle_type (struct d_info *di) 1709 { 1710 char peek; 1711 struct demangle_component *ret; 1712 int can_subst; 1713 1714 /* The ABI specifies that when CV-qualifiers are used, the base type 1715 is substitutable, and the fully qualified type is substitutable, 1716 but the base type with a strict subset of the CV-qualifiers is 1717 not substitutable. The natural recursive implementation of the 1718 CV-qualifiers would cause subsets to be substitutable, so instead 1719 we pull them all off now. 1720 1721 FIXME: The ABI says that order-insensitive vendor qualifiers 1722 should be handled in the same way, but we have no way to tell 1723 which vendor qualifiers are order-insensitive and which are 1724 order-sensitive. So we just assume that they are all 1725 order-sensitive. g++ 3.4 supports only one vendor qualifier, 1726 __vector, and it treats it as order-sensitive when mangling 1727 names. */ 1728 1729 peek = d_peek_char (di); 1730 if (peek == 'r' || peek == 'V' || peek == 'K') 1731 { 1732 struct demangle_component **pret; 1733 1734 pret = d_cv_qualifiers (di, &ret, 0); 1735 if (pret == NULL) 1736 return NULL; 1737 *pret = cplus_demangle_type (di); 1738 if (! d_add_substitution (di, ret)) 1739 return NULL; 1740 return ret; 1741 } 1742 1743 can_subst = 1; 1744 1745 switch (peek) 1746 { 1747 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 1748 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n': 1749 case 'o': case 's': case 't': 1750 case 'v': case 'w': case 'x': case 'y': case 'z': 1751 ret = d_make_builtin_type (di, 1752 &cplus_demangle_builtin_types[peek - 'a']); 1753 di->expansion += ret->u.s_builtin.type->len; 1754 can_subst = 0; 1755 d_advance (di, 1); 1756 break; 1757 1758 case 'u': 1759 d_advance (di, 1); 1760 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE, 1761 d_source_name (di), NULL); 1762 break; 1763 1764 case 'F': 1765 ret = d_function_type (di); 1766 break; 1767 1768 case '0': case '1': case '2': case '3': case '4': 1769 case '5': case '6': case '7': case '8': case '9': 1770 case 'N': 1771 case 'Z': 1772 ret = d_class_enum_type (di); 1773 break; 1774 1775 case 'A': 1776 ret = d_array_type (di); 1777 break; 1778 1779 case 'M': 1780 ret = d_pointer_to_member_type (di); 1781 break; 1782 1783 case 'T': 1784 ret = d_template_param (di); 1785 if (d_peek_char (di) == 'I') 1786 { 1787 /* This is <template-template-param> <template-args>. The 1788 <template-template-param> part is a substitution 1789 candidate. */ 1790 if (! d_add_substitution (di, ret)) 1791 return NULL; 1792 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, 1793 d_template_args (di)); 1794 } 1795 break; 1796 1797 case 'S': 1798 /* If this is a special substitution, then it is the start of 1799 <class-enum-type>. */ 1800 { 1801 char peek_next; 1802 1803 peek_next = d_peek_next_char (di); 1804 if (IS_DIGIT (peek_next) 1805 || peek_next == '_' 1806 || IS_UPPER (peek_next)) 1807 { 1808 ret = d_substitution (di, 0); 1809 /* The substituted name may have been a template name and 1810 may be followed by tepmlate args. */ 1811 if (d_peek_char (di) == 'I') 1812 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, 1813 d_template_args (di)); 1814 else 1815 can_subst = 0; 1816 } 1817 else 1818 { 1819 ret = d_class_enum_type (di); 1820 /* If the substitution was a complete type, then it is not 1821 a new substitution candidate. However, if the 1822 substitution was followed by template arguments, then 1823 the whole thing is a substitution candidate. */ 1824 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD) 1825 can_subst = 0; 1826 } 1827 } 1828 break; 1829 1830 case 'P': 1831 d_advance (di, 1); 1832 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER, 1833 cplus_demangle_type (di), NULL); 1834 break; 1835 1836 case 'R': 1837 d_advance (di, 1); 1838 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE, 1839 cplus_demangle_type (di), NULL); 1840 break; 1841 1842 case 'C': 1843 d_advance (di, 1); 1844 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX, 1845 cplus_demangle_type (di), NULL); 1846 break; 1847 1848 case 'G': 1849 d_advance (di, 1); 1850 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY, 1851 cplus_demangle_type (di), NULL); 1852 break; 1853 1854 case 'U': 1855 d_advance (di, 1); 1856 ret = d_source_name (di); 1857 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL, 1858 cplus_demangle_type (di), ret); 1859 break; 1860 1861 default: 1862 return NULL; 1863 } 1864 1865 if (can_subst) 1866 { 1867 if (! d_add_substitution (di, ret)) 1868 return NULL; 1869 } 1870 1871 return ret; 1872 } 1873 1874 /* <CV-qualifiers> ::= [r] [V] [K] */ 1875 1876 static struct demangle_component ** 1877 d_cv_qualifiers (struct d_info *di, 1878 struct demangle_component **pret, int member_fn) 1879 { 1880 char peek; 1881 1882 peek = d_peek_char (di); 1883 while (peek == 'r' || peek == 'V' || peek == 'K') 1884 { 1885 enum demangle_component_type t; 1886 1887 d_advance (di, 1); 1888 if (peek == 'r') 1889 { 1890 t = (member_fn 1891 ? DEMANGLE_COMPONENT_RESTRICT_THIS 1892 : DEMANGLE_COMPONENT_RESTRICT); 1893 di->expansion += sizeof "restrict"; 1894 } 1895 else if (peek == 'V') 1896 { 1897 t = (member_fn 1898 ? DEMANGLE_COMPONENT_VOLATILE_THIS 1899 : DEMANGLE_COMPONENT_VOLATILE); 1900 di->expansion += sizeof "volatile"; 1901 } 1902 else 1903 { 1904 t = (member_fn 1905 ? DEMANGLE_COMPONENT_CONST_THIS 1906 : DEMANGLE_COMPONENT_CONST); 1907 di->expansion += sizeof "const"; 1908 } 1909 1910 *pret = d_make_comp (di, t, NULL, NULL); 1911 if (*pret == NULL) 1912 return NULL; 1913 pret = &d_left (*pret); 1914 1915 peek = d_peek_char (di); 1916 } 1917 1918 return pret; 1919 } 1920 1921 /* <function-type> ::= F [Y] <bare-function-type> E */ 1922 1923 static struct demangle_component * 1924 d_function_type (struct d_info *di) 1925 { 1926 struct demangle_component *ret; 1927 1928 if (d_next_char (di) != 'F') 1929 return NULL; 1930 if (d_peek_char (di) == 'Y') 1931 { 1932 /* Function has C linkage. We don't print this information. 1933 FIXME: We should print it in verbose mode. */ 1934 d_advance (di, 1); 1935 } 1936 ret = d_bare_function_type (di, 1); 1937 if (d_next_char (di) != 'E') 1938 return NULL; 1939 return ret; 1940 } 1941 1942 /* <bare-function-type> ::= [J]<type>+ */ 1943 1944 static struct demangle_component * 1945 d_bare_function_type (struct d_info *di, int has_return_type) 1946 { 1947 struct demangle_component *return_type; 1948 struct demangle_component *tl; 1949 struct demangle_component **ptl; 1950 char peek; 1951 1952 /* Detect special qualifier indicating that the first argument 1953 is the return type. */ 1954 peek = d_peek_char (di); 1955 if (peek == 'J') 1956 { 1957 d_advance (di, 1); 1958 has_return_type = 1; 1959 } 1960 1961 return_type = NULL; 1962 tl = NULL; 1963 ptl = &tl; 1964 while (1) 1965 { 1966 struct demangle_component *type; 1967 1968 peek = d_peek_char (di); 1969 if (peek == '\0' || peek == 'E') 1970 break; 1971 type = cplus_demangle_type (di); 1972 if (type == NULL) 1973 return NULL; 1974 if (has_return_type) 1975 { 1976 return_type = type; 1977 has_return_type = 0; 1978 } 1979 else 1980 { 1981 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL); 1982 if (*ptl == NULL) 1983 return NULL; 1984 ptl = &d_right (*ptl); 1985 } 1986 } 1987 1988 /* There should be at least one parameter type besides the optional 1989 return type. A function which takes no arguments will have a 1990 single parameter type void. */ 1991 if (tl == NULL) 1992 return NULL; 1993 1994 /* If we have a single parameter type void, omit it. */ 1995 if (d_right (tl) == NULL 1996 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE 1997 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID) 1998 { 1999 di->expansion -= d_left (tl)->u.s_builtin.type->len; 2000 tl = NULL; 2001 } 2002 2003 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl); 2004 } 2005 2006 /* <class-enum-type> ::= <name> */ 2007 2008 static struct demangle_component * 2009 d_class_enum_type (struct d_info *di) 2010 { 2011 return d_name (di); 2012 } 2013 2014 /* <array-type> ::= A <(positive dimension) number> _ <(element) type> 2015 ::= A [<(dimension) expression>] _ <(element) type> 2016 */ 2017 2018 static struct demangle_component * 2019 d_array_type (struct d_info *di) 2020 { 2021 char peek; 2022 struct demangle_component *dim; 2023 2024 if (d_next_char (di) != 'A') 2025 return NULL; 2026 2027 peek = d_peek_char (di); 2028 if (peek == '_') 2029 dim = NULL; 2030 else if (IS_DIGIT (peek)) 2031 { 2032 const char *s; 2033 2034 s = d_str (di); 2035 do 2036 { 2037 d_advance (di, 1); 2038 peek = d_peek_char (di); 2039 } 2040 while (IS_DIGIT (peek)); 2041 dim = d_make_name (di, s, d_str (di) - s); 2042 if (dim == NULL) 2043 return NULL; 2044 } 2045 else 2046 { 2047 dim = d_expression (di); 2048 if (dim == NULL) 2049 return NULL; 2050 } 2051 2052 if (d_next_char (di) != '_') 2053 return NULL; 2054 2055 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim, 2056 cplus_demangle_type (di)); 2057 } 2058 2059 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */ 2060 2061 static struct demangle_component * 2062 d_pointer_to_member_type (struct d_info *di) 2063 { 2064 struct demangle_component *cl; 2065 struct demangle_component *mem; 2066 struct demangle_component **pmem; 2067 2068 if (d_next_char (di) != 'M') 2069 return NULL; 2070 2071 cl = cplus_demangle_type (di); 2072 2073 /* The ABI specifies that any type can be a substitution source, and 2074 that M is followed by two types, and that when a CV-qualified 2075 type is seen both the base type and the CV-qualified types are 2076 substitution sources. The ABI also specifies that for a pointer 2077 to a CV-qualified member function, the qualifiers are attached to 2078 the second type. Given the grammar, a plain reading of the ABI 2079 suggests that both the CV-qualified member function and the 2080 non-qualified member function are substitution sources. However, 2081 g++ does not work that way. g++ treats only the CV-qualified 2082 member function as a substitution source. FIXME. So to work 2083 with g++, we need to pull off the CV-qualifiers here, in order to 2084 avoid calling add_substitution() in cplus_demangle_type(). */ 2085 2086 pmem = d_cv_qualifiers (di, &mem, 1); 2087 if (pmem == NULL) 2088 return NULL; 2089 *pmem = cplus_demangle_type (di); 2090 2091 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem); 2092 } 2093 2094 /* <template-param> ::= T_ 2095 ::= T <(parameter-2 non-negative) number> _ 2096 */ 2097 2098 static struct demangle_component * 2099 d_template_param (struct d_info *di) 2100 { 2101 long param; 2102 2103 if (d_next_char (di) != 'T') 2104 return NULL; 2105 2106 if (d_peek_char (di) == '_') 2107 param = 0; 2108 else 2109 { 2110 param = d_number (di); 2111 if (param < 0) 2112 return NULL; 2113 param += 1; 2114 } 2115 2116 if (d_next_char (di) != '_') 2117 return NULL; 2118 2119 ++di->did_subs; 2120 2121 return d_make_template_param (di, param); 2122 } 2123 2124 /* <template-args> ::= I <template-arg>+ E */ 2125 2126 static struct demangle_component * 2127 d_template_args (struct d_info *di) 2128 { 2129 struct demangle_component *hold_last_name; 2130 struct demangle_component *al; 2131 struct demangle_component **pal; 2132 2133 /* Preserve the last name we saw--don't let the template arguments 2134 clobber it, as that would give us the wrong name for a subsequent 2135 constructor or destructor. */ 2136 hold_last_name = di->last_name; 2137 2138 if (d_next_char (di) != 'I') 2139 return NULL; 2140 2141 al = NULL; 2142 pal = &al; 2143 while (1) 2144 { 2145 struct demangle_component *a; 2146 2147 a = d_template_arg (di); 2148 if (a == NULL) 2149 return NULL; 2150 2151 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL); 2152 if (*pal == NULL) 2153 return NULL; 2154 pal = &d_right (*pal); 2155 2156 if (d_peek_char (di) == 'E') 2157 { 2158 d_advance (di, 1); 2159 break; 2160 } 2161 } 2162 2163 di->last_name = hold_last_name; 2164 2165 return al; 2166 } 2167 2168 /* <template-arg> ::= <type> 2169 ::= X <expression> E 2170 ::= <expr-primary> 2171 */ 2172 2173 static struct demangle_component * 2174 d_template_arg (struct d_info *di) 2175 { 2176 struct demangle_component *ret; 2177 2178 switch (d_peek_char (di)) 2179 { 2180 case 'X': 2181 d_advance (di, 1); 2182 ret = d_expression (di); 2183 if (d_next_char (di) != 'E') 2184 return NULL; 2185 return ret; 2186 2187 case 'L': 2188 return d_expr_primary (di); 2189 2190 default: 2191 return cplus_demangle_type (di); 2192 } 2193 } 2194 2195 /* <expression> ::= <(unary) operator-name> <expression> 2196 ::= <(binary) operator-name> <expression> <expression> 2197 ::= <(trinary) operator-name> <expression> <expression> <expression> 2198 ::= st <type> 2199 ::= <template-param> 2200 ::= sr <type> <unqualified-name> 2201 ::= sr <type> <unqualified-name> <template-args> 2202 ::= <expr-primary> 2203 */ 2204 2205 static struct demangle_component * 2206 d_expression (struct d_info *di) 2207 { 2208 char peek; 2209 2210 peek = d_peek_char (di); 2211 if (peek == 'L') 2212 return d_expr_primary (di); 2213 else if (peek == 'T') 2214 return d_template_param (di); 2215 else if (peek == 's' && d_peek_next_char (di) == 'r') 2216 { 2217 struct demangle_component *type; 2218 struct demangle_component *name; 2219 2220 d_advance (di, 2); 2221 type = cplus_demangle_type (di); 2222 name = d_unqualified_name (di); 2223 if (d_peek_char (di) != 'I') 2224 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name); 2225 else 2226 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, 2227 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name, 2228 d_template_args (di))); 2229 } 2230 else 2231 { 2232 struct demangle_component *op; 2233 int args; 2234 2235 op = d_operator_name (di); 2236 if (op == NULL) 2237 return NULL; 2238 2239 if (op->type == DEMANGLE_COMPONENT_OPERATOR) 2240 di->expansion += op->u.s_operator.op->len - 2; 2241 2242 if (op->type == DEMANGLE_COMPONENT_OPERATOR 2243 && strcmp (op->u.s_operator.op->code, "st") == 0) 2244 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, 2245 cplus_demangle_type (di)); 2246 2247 switch (op->type) 2248 { 2249 default: 2250 return NULL; 2251 case DEMANGLE_COMPONENT_OPERATOR: 2252 args = op->u.s_operator.op->args; 2253 break; 2254 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 2255 args = op->u.s_extended_operator.args; 2256 break; 2257 case DEMANGLE_COMPONENT_CAST: 2258 args = 1; 2259 break; 2260 } 2261 2262 switch (args) 2263 { 2264 case 1: 2265 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, 2266 d_expression (di)); 2267 case 2: 2268 { 2269 struct demangle_component *left; 2270 2271 left = d_expression (di); 2272 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op, 2273 d_make_comp (di, 2274 DEMANGLE_COMPONENT_BINARY_ARGS, 2275 left, 2276 d_expression (di))); 2277 } 2278 case 3: 2279 { 2280 struct demangle_component *first; 2281 struct demangle_component *second; 2282 2283 first = d_expression (di); 2284 second = d_expression (di); 2285 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op, 2286 d_make_comp (di, 2287 DEMANGLE_COMPONENT_TRINARY_ARG1, 2288 first, 2289 d_make_comp (di, 2290 DEMANGLE_COMPONENT_TRINARY_ARG2, 2291 second, 2292 d_expression (di)))); 2293 } 2294 default: 2295 return NULL; 2296 } 2297 } 2298 } 2299 2300 /* <expr-primary> ::= L <type> <(value) number> E 2301 ::= L <type> <(value) float> E 2302 ::= L <mangled-name> E 2303 */ 2304 2305 static struct demangle_component * 2306 d_expr_primary (struct d_info *di) 2307 { 2308 struct demangle_component *ret; 2309 2310 if (d_next_char (di) != 'L') 2311 return NULL; 2312 if (d_peek_char (di) == '_') 2313 ret = cplus_demangle_mangled_name (di, 0); 2314 else 2315 { 2316 struct demangle_component *type; 2317 enum demangle_component_type t; 2318 const char *s; 2319 2320 type = cplus_demangle_type (di); 2321 if (type == NULL) 2322 return NULL; 2323 2324 /* If we have a type we know how to print, we aren't going to 2325 print the type name itself. */ 2326 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE 2327 && type->u.s_builtin.type->print != D_PRINT_DEFAULT) 2328 di->expansion -= type->u.s_builtin.type->len; 2329 2330 /* Rather than try to interpret the literal value, we just 2331 collect it as a string. Note that it's possible to have a 2332 floating point literal here. The ABI specifies that the 2333 format of such literals is machine independent. That's fine, 2334 but what's not fine is that versions of g++ up to 3.2 with 2335 -fabi-version=1 used upper case letters in the hex constant, 2336 and dumped out gcc's internal representation. That makes it 2337 hard to tell where the constant ends, and hard to dump the 2338 constant in any readable form anyhow. We don't attempt to 2339 handle these cases. */ 2340 2341 t = DEMANGLE_COMPONENT_LITERAL; 2342 if (d_peek_char (di) == 'n') 2343 { 2344 t = DEMANGLE_COMPONENT_LITERAL_NEG; 2345 d_advance (di, 1); 2346 } 2347 s = d_str (di); 2348 while (d_peek_char (di) != 'E') 2349 { 2350 if (d_peek_char (di) == '\0') 2351 return NULL; 2352 d_advance (di, 1); 2353 } 2354 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s)); 2355 } 2356 if (d_next_char (di) != 'E') 2357 return NULL; 2358 return ret; 2359 } 2360 2361 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>] 2362 ::= Z <(function) encoding> E s [<discriminator>] 2363 */ 2364 2365 static struct demangle_component * 2366 d_local_name (struct d_info *di) 2367 { 2368 struct demangle_component *function; 2369 2370 if (d_next_char (di) != 'Z') 2371 return NULL; 2372 2373 function = d_encoding (di, 0); 2374 2375 if (d_next_char (di) != 'E') 2376 return NULL; 2377 2378 if (d_peek_char (di) == 's') 2379 { 2380 d_advance (di, 1); 2381 if (! d_discriminator (di)) 2382 return NULL; 2383 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, 2384 d_make_name (di, "string literal", 2385 sizeof "string literal" - 1)); 2386 } 2387 else 2388 { 2389 struct demangle_component *name; 2390 2391 name = d_name (di); 2392 if (! d_discriminator (di)) 2393 return NULL; 2394 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name); 2395 } 2396 } 2397 2398 /* <discriminator> ::= _ <(non-negative) number> 2399 2400 We demangle the discriminator, but we don't print it out. FIXME: 2401 We should print it out in verbose mode. */ 2402 2403 static int 2404 d_discriminator (struct d_info *di) 2405 { 2406 long discrim; 2407 2408 if (d_peek_char (di) != '_') 2409 return 1; 2410 d_advance (di, 1); 2411 discrim = d_number (di); 2412 if (discrim < 0) 2413 return 0; 2414 return 1; 2415 } 2416 2417 /* Add a new substitution. */ 2418 2419 static int 2420 d_add_substitution (struct d_info *di, struct demangle_component *dc) 2421 { 2422 if (dc == NULL) 2423 return 0; 2424 if (di->next_sub >= di->num_subs) 2425 return 0; 2426 di->subs[di->next_sub] = dc; 2427 ++di->next_sub; 2428 return 1; 2429 } 2430 2431 /* <substitution> ::= S <seq-id> _ 2432 ::= S_ 2433 ::= St 2434 ::= Sa 2435 ::= Sb 2436 ::= Ss 2437 ::= Si 2438 ::= So 2439 ::= Sd 2440 2441 If PREFIX is non-zero, then this type is being used as a prefix in 2442 a qualified name. In this case, for the standard substitutions, we 2443 need to check whether we are being used as a prefix for a 2444 constructor or destructor, and return a full template name. 2445 Otherwise we will get something like std::iostream::~iostream() 2446 which does not correspond particularly well to any function which 2447 actually appears in the source. 2448 */ 2449 2450 static const struct d_standard_sub_info standard_subs[] = 2451 { 2452 { 't', NL ("std"), 2453 NL ("std"), 2454 NULL, 0 }, 2455 { 'a', NL ("std::allocator"), 2456 NL ("std::allocator"), 2457 NL ("allocator") }, 2458 { 'b', NL ("std::basic_string"), 2459 NL ("std::basic_string"), 2460 NL ("basic_string") }, 2461 { 's', NL ("std::string"), 2462 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"), 2463 NL ("basic_string") }, 2464 { 'i', NL ("std::istream"), 2465 NL ("std::basic_istream<char, std::char_traits<char> >"), 2466 NL ("basic_istream") }, 2467 { 'o', NL ("std::ostream"), 2468 NL ("std::basic_ostream<char, std::char_traits<char> >"), 2469 NL ("basic_ostream") }, 2470 { 'd', NL ("std::iostream"), 2471 NL ("std::basic_iostream<char, std::char_traits<char> >"), 2472 NL ("basic_iostream") } 2473 }; 2474 2475 static struct demangle_component * 2476 d_substitution (struct d_info *di, int prefix) 2477 { 2478 char c; 2479 2480 if (d_next_char (di) != 'S') 2481 return NULL; 2482 2483 c = d_next_char (di); 2484 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c)) 2485 { 2486 int id; 2487 2488 id = 0; 2489 if (c != '_') 2490 { 2491 do 2492 { 2493 if (IS_DIGIT (c)) 2494 id = id * 36 + c - '0'; 2495 else if (IS_UPPER (c)) 2496 id = id * 36 + c - 'A' + 10; 2497 else 2498 return NULL; 2499 c = d_next_char (di); 2500 } 2501 while (c != '_'); 2502 2503 ++id; 2504 } 2505 2506 if (id >= di->next_sub) 2507 return NULL; 2508 2509 ++di->did_subs; 2510 2511 return di->subs[id]; 2512 } 2513 else 2514 { 2515 int verbose; 2516 const struct d_standard_sub_info *p; 2517 const struct d_standard_sub_info *pend; 2518 2519 verbose = (di->options & DMGL_VERBOSE) != 0; 2520 if (! verbose && prefix) 2521 { 2522 char peek; 2523 2524 peek = d_peek_char (di); 2525 if (peek == 'C' || peek == 'D') 2526 verbose = 1; 2527 } 2528 2529 pend = (&standard_subs[0] 2530 + sizeof standard_subs / sizeof standard_subs[0]); 2531 for (p = &standard_subs[0]; p < pend; ++p) 2532 { 2533 if (c == p->code) 2534 { 2535 const char *s; 2536 int len; 2537 2538 if (p->set_last_name != NULL) 2539 di->last_name = d_make_sub (di, p->set_last_name, 2540 p->set_last_name_len); 2541 if (verbose) 2542 { 2543 s = p->full_expansion; 2544 len = p->full_len; 2545 } 2546 else 2547 { 2548 s = p->simple_expansion; 2549 len = p->simple_len; 2550 } 2551 di->expansion += len; 2552 return d_make_sub (di, s, len); 2553 } 2554 } 2555 2556 return NULL; 2557 } 2558 } 2559 2560 /* Resize the print buffer. */ 2561 2562 static void 2563 d_print_resize (struct d_print_info *dpi, size_t add) 2564 { 2565 size_t need; 2566 2567 if (dpi->buf == NULL) 2568 return; 2569 need = dpi->len + add; 2570 while (need > dpi->alc) 2571 { 2572 size_t newalc; 2573 char *newbuf; 2574 2575 newalc = dpi->alc * 2; 2576 newbuf = (char *) realloc (dpi->buf, newalc); 2577 if (newbuf == NULL) 2578 { 2579 free (dpi->buf); 2580 dpi->buf = NULL; 2581 dpi->allocation_failure = 1; 2582 return; 2583 } 2584 dpi->buf = newbuf; 2585 dpi->alc = newalc; 2586 } 2587 } 2588 2589 /* Append a character to the print buffer. */ 2590 2591 static void 2592 d_print_append_char (struct d_print_info *dpi, int c) 2593 { 2594 if (dpi->buf != NULL) 2595 { 2596 if (dpi->len >= dpi->alc) 2597 { 2598 d_print_resize (dpi, 1); 2599 if (dpi->buf == NULL) 2600 return; 2601 } 2602 2603 dpi->buf[dpi->len] = c; 2604 ++dpi->len; 2605 } 2606 } 2607 2608 /* Append a buffer to the print buffer. */ 2609 2610 static void 2611 d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l) 2612 { 2613 if (dpi->buf != NULL) 2614 { 2615 if (dpi->len + l > dpi->alc) 2616 { 2617 d_print_resize (dpi, l); 2618 if (dpi->buf == NULL) 2619 return; 2620 } 2621 2622 memcpy (dpi->buf + dpi->len, s, l); 2623 dpi->len += l; 2624 } 2625 } 2626 2627 /* Indicate that an error occurred during printing. */ 2628 2629 static void 2630 d_print_error (struct d_print_info *dpi) 2631 { 2632 free (dpi->buf); 2633 dpi->buf = NULL; 2634 } 2635 2636 /* Turn components into a human readable string. OPTIONS is the 2637 options bits passed to the demangler. DC is the tree to print. 2638 ESTIMATE is a guess at the length of the result. This returns a 2639 string allocated by malloc, or NULL on error. On success, this 2640 sets *PALC to the size of the allocated buffer. On failure, this 2641 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation 2642 failure. */ 2643 2644 CP_STATIC_IF_GLIBCPP_V3 2645 char * 2646 cplus_demangle_print (int options, const struct demangle_component *dc, 2647 int estimate, size_t *palc) 2648 { 2649 struct d_print_info dpi; 2650 2651 dpi.options = options; 2652 2653 dpi.alc = estimate + 1; 2654 dpi.buf = (char *) malloc (dpi.alc); 2655 if (dpi.buf == NULL) 2656 { 2657 *palc = 1; 2658 return NULL; 2659 } 2660 2661 dpi.len = 0; 2662 dpi.templates = NULL; 2663 dpi.modifiers = NULL; 2664 2665 dpi.allocation_failure = 0; 2666 2667 d_print_comp (&dpi, dc); 2668 2669 d_append_char (&dpi, '\0'); 2670 2671 if (dpi.buf != NULL) 2672 *palc = dpi.alc; 2673 else 2674 *palc = dpi.allocation_failure; 2675 2676 return dpi.buf; 2677 } 2678 2679 /* Subroutine to handle components. */ 2680 2681 static void 2682 d_print_comp (struct d_print_info *dpi, 2683 const struct demangle_component *dc) 2684 { 2685 if (dc == NULL) 2686 { 2687 d_print_error (dpi); 2688 return; 2689 } 2690 if (d_print_saw_error (dpi)) 2691 return; 2692 2693 switch (dc->type) 2694 { 2695 case DEMANGLE_COMPONENT_NAME: 2696 if ((dpi->options & DMGL_JAVA) == 0) 2697 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len); 2698 else 2699 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len); 2700 return; 2701 2702 case DEMANGLE_COMPONENT_QUAL_NAME: 2703 case DEMANGLE_COMPONENT_LOCAL_NAME: 2704 d_print_comp (dpi, d_left (dc)); 2705 if ((dpi->options & DMGL_JAVA) == 0) 2706 d_append_string_constant (dpi, "::"); 2707 else 2708 d_append_char (dpi, '.'); 2709 d_print_comp (dpi, d_right (dc)); 2710 return; 2711 2712 case DEMANGLE_COMPONENT_TYPED_NAME: 2713 { 2714 struct d_print_mod *hold_modifiers; 2715 struct demangle_component *typed_name; 2716 struct d_print_mod adpm[4]; 2717 unsigned int i; 2718 struct d_print_template dpt; 2719 2720 /* Pass the name down to the type so that it can be printed in 2721 the right place for the type. We also have to pass down 2722 any CV-qualifiers, which apply to the this parameter. */ 2723 hold_modifiers = dpi->modifiers; 2724 i = 0; 2725 typed_name = d_left (dc); 2726 while (typed_name != NULL) 2727 { 2728 if (i >= sizeof adpm / sizeof adpm[0]) 2729 { 2730 d_print_error (dpi); 2731 return; 2732 } 2733 2734 adpm[i].next = dpi->modifiers; 2735 dpi->modifiers = &adpm[i]; 2736 adpm[i].mod = typed_name; 2737 adpm[i].printed = 0; 2738 adpm[i].templates = dpi->templates; 2739 ++i; 2740 2741 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS 2742 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS 2743 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS) 2744 break; 2745 2746 typed_name = d_left (typed_name); 2747 } 2748 2749 /* If typed_name is a template, then it applies to the 2750 function type as well. */ 2751 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) 2752 { 2753 dpt.next = dpi->templates; 2754 dpi->templates = &dpt; 2755 dpt.template_decl = typed_name; 2756 } 2757 2758 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then 2759 there may be CV-qualifiers on its right argument which 2760 really apply here; this happens when parsing a class which 2761 is local to a function. */ 2762 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME) 2763 { 2764 struct demangle_component *local_name; 2765 2766 local_name = d_right (typed_name); 2767 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS 2768 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS 2769 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS) 2770 { 2771 if (i >= sizeof adpm / sizeof adpm[0]) 2772 { 2773 d_print_error (dpi); 2774 return; 2775 } 2776 2777 adpm[i] = adpm[i - 1]; 2778 adpm[i].next = &adpm[i - 1]; 2779 dpi->modifiers = &adpm[i]; 2780 2781 adpm[i - 1].mod = local_name; 2782 adpm[i - 1].printed = 0; 2783 adpm[i - 1].templates = dpi->templates; 2784 ++i; 2785 2786 local_name = d_left (local_name); 2787 } 2788 } 2789 2790 d_print_comp (dpi, d_right (dc)); 2791 2792 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) 2793 dpi->templates = dpt.next; 2794 2795 /* If the modifiers didn't get printed by the type, print them 2796 now. */ 2797 while (i > 0) 2798 { 2799 --i; 2800 if (! adpm[i].printed) 2801 { 2802 d_append_char (dpi, ' '); 2803 d_print_mod (dpi, adpm[i].mod); 2804 } 2805 } 2806 2807 dpi->modifiers = hold_modifiers; 2808 2809 return; 2810 } 2811 2812 case DEMANGLE_COMPONENT_TEMPLATE: 2813 { 2814 struct d_print_mod *hold_dpm; 2815 2816 /* Don't push modifiers into a template definition. Doing so 2817 could give the wrong definition for a template argument. 2818 Instead, treat the template essentially as a name. */ 2819 2820 hold_dpm = dpi->modifiers; 2821 dpi->modifiers = NULL; 2822 2823 d_print_comp (dpi, d_left (dc)); 2824 if (d_last_char (dpi) == '<') 2825 d_append_char (dpi, ' '); 2826 d_append_char (dpi, '<'); 2827 d_print_comp (dpi, d_right (dc)); 2828 /* Avoid generating two consecutive '>' characters, to avoid 2829 the C++ syntactic ambiguity. */ 2830 if (d_last_char (dpi) == '>') 2831 d_append_char (dpi, ' '); 2832 d_append_char (dpi, '>'); 2833 2834 dpi->modifiers = hold_dpm; 2835 2836 return; 2837 } 2838 2839 case DEMANGLE_COMPONENT_TEMPLATE_PARAM: 2840 { 2841 long i; 2842 struct demangle_component *a; 2843 struct d_print_template *hold_dpt; 2844 2845 if (dpi->templates == NULL) 2846 { 2847 d_print_error (dpi); 2848 return; 2849 } 2850 i = dc->u.s_number.number; 2851 for (a = d_right (dpi->templates->template_decl); 2852 a != NULL; 2853 a = d_right (a)) 2854 { 2855 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) 2856 { 2857 d_print_error (dpi); 2858 return; 2859 } 2860 if (i <= 0) 2861 break; 2862 --i; 2863 } 2864 if (i != 0 || a == NULL) 2865 { 2866 d_print_error (dpi); 2867 return; 2868 } 2869 2870 /* While processing this parameter, we need to pop the list of 2871 templates. This is because the template parameter may 2872 itself be a reference to a parameter of an outer 2873 template. */ 2874 2875 hold_dpt = dpi->templates; 2876 dpi->templates = hold_dpt->next; 2877 2878 d_print_comp (dpi, d_left (a)); 2879 2880 dpi->templates = hold_dpt; 2881 2882 return; 2883 } 2884 2885 case DEMANGLE_COMPONENT_CTOR: 2886 d_print_comp (dpi, dc->u.s_ctor.name); 2887 return; 2888 2889 case DEMANGLE_COMPONENT_DTOR: 2890 d_append_char (dpi, '~'); 2891 d_print_comp (dpi, dc->u.s_dtor.name); 2892 return; 2893 2894 case DEMANGLE_COMPONENT_VTABLE: 2895 d_append_string_constant (dpi, "vtable for "); 2896 d_print_comp (dpi, d_left (dc)); 2897 return; 2898 2899 case DEMANGLE_COMPONENT_VTT: 2900 d_append_string_constant (dpi, "VTT for "); 2901 d_print_comp (dpi, d_left (dc)); 2902 return; 2903 2904 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: 2905 d_append_string_constant (dpi, "construction vtable for "); 2906 d_print_comp (dpi, d_left (dc)); 2907 d_append_string_constant (dpi, "-in-"); 2908 d_print_comp (dpi, d_right (dc)); 2909 return; 2910 2911 case DEMANGLE_COMPONENT_TYPEINFO: 2912 d_append_string_constant (dpi, "typeinfo for "); 2913 d_print_comp (dpi, d_left (dc)); 2914 return; 2915 2916 case DEMANGLE_COMPONENT_TYPEINFO_NAME: 2917 d_append_string_constant (dpi, "typeinfo name for "); 2918 d_print_comp (dpi, d_left (dc)); 2919 return; 2920 2921 case DEMANGLE_COMPONENT_TYPEINFO_FN: 2922 d_append_string_constant (dpi, "typeinfo fn for "); 2923 d_print_comp (dpi, d_left (dc)); 2924 return; 2925 2926 case DEMANGLE_COMPONENT_THUNK: 2927 d_append_string_constant (dpi, "non-virtual thunk to "); 2928 d_print_comp (dpi, d_left (dc)); 2929 return; 2930 2931 case DEMANGLE_COMPONENT_VIRTUAL_THUNK: 2932 d_append_string_constant (dpi, "virtual thunk to "); 2933 d_print_comp (dpi, d_left (dc)); 2934 return; 2935 2936 case DEMANGLE_COMPONENT_COVARIANT_THUNK: 2937 d_append_string_constant (dpi, "covariant return thunk to "); 2938 d_print_comp (dpi, d_left (dc)); 2939 return; 2940 2941 case DEMANGLE_COMPONENT_JAVA_CLASS: 2942 d_append_string_constant (dpi, "java Class for "); 2943 d_print_comp (dpi, d_left (dc)); 2944 return; 2945 2946 case DEMANGLE_COMPONENT_GUARD: 2947 d_append_string_constant (dpi, "guard variable for "); 2948 d_print_comp (dpi, d_left (dc)); 2949 return; 2950 2951 case DEMANGLE_COMPONENT_REFTEMP: 2952 d_append_string_constant (dpi, "reference temporary for "); 2953 d_print_comp (dpi, d_left (dc)); 2954 return; 2955 2956 case DEMANGLE_COMPONENT_HIDDEN_ALIAS: 2957 d_append_string_constant (dpi, "hidden alias for "); 2958 d_print_comp (dpi, d_left (dc)); 2959 return; 2960 2961 case DEMANGLE_COMPONENT_SUB_STD: 2962 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len); 2963 return; 2964 2965 case DEMANGLE_COMPONENT_RESTRICT: 2966 case DEMANGLE_COMPONENT_VOLATILE: 2967 case DEMANGLE_COMPONENT_CONST: 2968 { 2969 struct d_print_mod *pdpm; 2970 2971 /* When printing arrays, it's possible to have cases where the 2972 same CV-qualifier gets pushed on the stack multiple times. 2973 We only need to print it once. */ 2974 2975 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next) 2976 { 2977 if (! pdpm->printed) 2978 { 2979 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT 2980 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE 2981 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST) 2982 break; 2983 if (pdpm->mod->type == dc->type) 2984 { 2985 d_print_comp (dpi, d_left (dc)); 2986 return; 2987 } 2988 } 2989 } 2990 } 2991 /* Fall through. */ 2992 case DEMANGLE_COMPONENT_RESTRICT_THIS: 2993 case DEMANGLE_COMPONENT_VOLATILE_THIS: 2994 case DEMANGLE_COMPONENT_CONST_THIS: 2995 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 2996 case DEMANGLE_COMPONENT_POINTER: 2997 case DEMANGLE_COMPONENT_REFERENCE: 2998 case DEMANGLE_COMPONENT_COMPLEX: 2999 case DEMANGLE_COMPONENT_IMAGINARY: 3000 { 3001 /* We keep a list of modifiers on the stack. */ 3002 struct d_print_mod dpm; 3003 3004 dpm.next = dpi->modifiers; 3005 dpi->modifiers = &dpm; 3006 dpm.mod = dc; 3007 dpm.printed = 0; 3008 dpm.templates = dpi->templates; 3009 3010 d_print_comp (dpi, d_left (dc)); 3011 3012 /* If the modifier didn't get printed by the type, print it 3013 now. */ 3014 if (! dpm.printed) 3015 d_print_mod (dpi, dc); 3016 3017 dpi->modifiers = dpm.next; 3018 3019 return; 3020 } 3021 3022 case DEMANGLE_COMPONENT_BUILTIN_TYPE: 3023 if ((dpi->options & DMGL_JAVA) == 0) 3024 d_append_buffer (dpi, dc->u.s_builtin.type->name, 3025 dc->u.s_builtin.type->len); 3026 else 3027 d_append_buffer (dpi, dc->u.s_builtin.type->java_name, 3028 dc->u.s_builtin.type->java_len); 3029 return; 3030 3031 case DEMANGLE_COMPONENT_VENDOR_TYPE: 3032 d_print_comp (dpi, d_left (dc)); 3033 return; 3034 3035 case DEMANGLE_COMPONENT_FUNCTION_TYPE: 3036 { 3037 if ((dpi->options & DMGL_RET_POSTFIX) != 0) 3038 d_print_function_type (dpi, dc, dpi->modifiers); 3039 3040 /* Print return type if present */ 3041 if (d_left (dc) != NULL) 3042 { 3043 struct d_print_mod dpm; 3044 3045 /* We must pass this type down as a modifier in order to 3046 print it in the right location. */ 3047 dpm.next = dpi->modifiers; 3048 dpi->modifiers = &dpm; 3049 dpm.mod = dc; 3050 dpm.printed = 0; 3051 dpm.templates = dpi->templates; 3052 3053 d_print_comp (dpi, d_left (dc)); 3054 3055 dpi->modifiers = dpm.next; 3056 3057 if (dpm.printed) 3058 return; 3059 3060 /* In standard prefix notation, there is a space between the 3061 return type and the function signature. */ 3062 if ((dpi->options & DMGL_RET_POSTFIX) == 0) 3063 d_append_char (dpi, ' '); 3064 } 3065 3066 if ((dpi->options & DMGL_RET_POSTFIX) == 0) 3067 d_print_function_type (dpi, dc, dpi->modifiers); 3068 3069 return; 3070 } 3071 3072 case DEMANGLE_COMPONENT_ARRAY_TYPE: 3073 { 3074 struct d_print_mod *hold_modifiers; 3075 struct d_print_mod adpm[4]; 3076 unsigned int i; 3077 struct d_print_mod *pdpm; 3078 3079 /* We must pass this type down as a modifier in order to print 3080 multi-dimensional arrays correctly. If the array itself is 3081 CV-qualified, we act as though the element type were 3082 CV-qualified. We do this by copying the modifiers down 3083 rather than fiddling pointers, so that we don't wind up 3084 with a d_print_mod higher on the stack pointing into our 3085 stack frame after we return. */ 3086 3087 hold_modifiers = dpi->modifiers; 3088 3089 adpm[0].next = hold_modifiers; 3090 dpi->modifiers = &adpm[0]; 3091 adpm[0].mod = dc; 3092 adpm[0].printed = 0; 3093 adpm[0].templates = dpi->templates; 3094 3095 i = 1; 3096 pdpm = hold_modifiers; 3097 while (pdpm != NULL 3098 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT 3099 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE 3100 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST)) 3101 { 3102 if (! pdpm->printed) 3103 { 3104 if (i >= sizeof adpm / sizeof adpm[0]) 3105 { 3106 d_print_error (dpi); 3107 return; 3108 } 3109 3110 adpm[i] = *pdpm; 3111 adpm[i].next = dpi->modifiers; 3112 dpi->modifiers = &adpm[i]; 3113 pdpm->printed = 1; 3114 ++i; 3115 } 3116 3117 pdpm = pdpm->next; 3118 } 3119 3120 d_print_comp (dpi, d_right (dc)); 3121 3122 dpi->modifiers = hold_modifiers; 3123 3124 if (adpm[0].printed) 3125 return; 3126 3127 while (i > 1) 3128 { 3129 --i; 3130 d_print_mod (dpi, adpm[i].mod); 3131 } 3132 3133 d_print_array_type (dpi, dc, dpi->modifiers); 3134 3135 return; 3136 } 3137 3138 case DEMANGLE_COMPONENT_PTRMEM_TYPE: 3139 { 3140 struct d_print_mod dpm; 3141 3142 dpm.next = dpi->modifiers; 3143 dpi->modifiers = &dpm; 3144 dpm.mod = dc; 3145 dpm.printed = 0; 3146 dpm.templates = dpi->templates; 3147 3148 d_print_comp (dpi, d_right (dc)); 3149 3150 /* If the modifier didn't get printed by the type, print it 3151 now. */ 3152 if (! dpm.printed) 3153 { 3154 d_append_char (dpi, ' '); 3155 d_print_comp (dpi, d_left (dc)); 3156 d_append_string_constant (dpi, "::*"); 3157 } 3158 3159 dpi->modifiers = dpm.next; 3160 3161 return; 3162 } 3163 3164 case DEMANGLE_COMPONENT_ARGLIST: 3165 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: 3166 d_print_comp (dpi, d_left (dc)); 3167 if (d_right (dc) != NULL) 3168 { 3169 d_append_string_constant (dpi, ", "); 3170 d_print_comp (dpi, d_right (dc)); 3171 } 3172 return; 3173 3174 case DEMANGLE_COMPONENT_OPERATOR: 3175 { 3176 char c; 3177 3178 d_append_string_constant (dpi, "operator"); 3179 c = dc->u.s_operator.op->name[0]; 3180 if (IS_LOWER (c)) 3181 d_append_char (dpi, ' '); 3182 d_append_buffer (dpi, dc->u.s_operator.op->name, 3183 dc->u.s_operator.op->len); 3184 return; 3185 } 3186 3187 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 3188 d_append_string_constant (dpi, "operator "); 3189 d_print_comp (dpi, dc->u.s_extended_operator.name); 3190 return; 3191 3192 case DEMANGLE_COMPONENT_CAST: 3193 d_append_string_constant (dpi, "operator "); 3194 d_print_cast (dpi, dc); 3195 return; 3196 3197 case DEMANGLE_COMPONENT_UNARY: 3198 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST) 3199 d_print_expr_op (dpi, d_left (dc)); 3200 else 3201 { 3202 d_append_char (dpi, '('); 3203 d_print_cast (dpi, d_left (dc)); 3204 d_append_char (dpi, ')'); 3205 } 3206 d_append_char (dpi, '('); 3207 d_print_comp (dpi, d_right (dc)); 3208 d_append_char (dpi, ')'); 3209 return; 3210 3211 case DEMANGLE_COMPONENT_BINARY: 3212 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS) 3213 { 3214 d_print_error (dpi); 3215 return; 3216 } 3217 3218 /* We wrap an expression which uses the greater-than operator in 3219 an extra layer of parens so that it does not get confused 3220 with the '>' which ends the template parameters. */ 3221 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR 3222 && d_left (dc)->u.s_operator.op->len == 1 3223 && d_left (dc)->u.s_operator.op->name[0] == '>') 3224 d_append_char (dpi, '('); 3225 3226 d_append_char (dpi, '('); 3227 d_print_comp (dpi, d_left (d_right (dc))); 3228 d_append_string_constant (dpi, ") "); 3229 d_print_expr_op (dpi, d_left (dc)); 3230 d_append_string_constant (dpi, " ("); 3231 d_print_comp (dpi, d_right (d_right (dc))); 3232 d_append_char (dpi, ')'); 3233 3234 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR 3235 && d_left (dc)->u.s_operator.op->len == 1 3236 && d_left (dc)->u.s_operator.op->name[0] == '>') 3237 d_append_char (dpi, ')'); 3238 3239 return; 3240 3241 case DEMANGLE_COMPONENT_BINARY_ARGS: 3242 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */ 3243 d_print_error (dpi); 3244 return; 3245 3246 case DEMANGLE_COMPONENT_TRINARY: 3247 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1 3248 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2) 3249 { 3250 d_print_error (dpi); 3251 return; 3252 } 3253 d_append_char (dpi, '('); 3254 d_print_comp (dpi, d_left (d_right (dc))); 3255 d_append_string_constant (dpi, ") "); 3256 d_print_expr_op (dpi, d_left (dc)); 3257 d_append_string_constant (dpi, " ("); 3258 d_print_comp (dpi, d_left (d_right (d_right (dc)))); 3259 d_append_string_constant (dpi, ") : ("); 3260 d_print_comp (dpi, d_right (d_right (d_right (dc)))); 3261 d_append_char (dpi, ')'); 3262 return; 3263 3264 case DEMANGLE_COMPONENT_TRINARY_ARG1: 3265 case DEMANGLE_COMPONENT_TRINARY_ARG2: 3266 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */ 3267 d_print_error (dpi); 3268 return; 3269 3270 case DEMANGLE_COMPONENT_LITERAL: 3271 case DEMANGLE_COMPONENT_LITERAL_NEG: 3272 { 3273 enum d_builtin_type_print tp; 3274 3275 /* For some builtin types, produce simpler output. */ 3276 tp = D_PRINT_DEFAULT; 3277 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE) 3278 { 3279 tp = d_left (dc)->u.s_builtin.type->print; 3280 switch (tp) 3281 { 3282 case D_PRINT_INT: 3283 case D_PRINT_UNSIGNED: 3284 case D_PRINT_LONG: 3285 case D_PRINT_UNSIGNED_LONG: 3286 case D_PRINT_LONG_LONG: 3287 case D_PRINT_UNSIGNED_LONG_LONG: 3288 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME) 3289 { 3290 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG) 3291 d_append_char (dpi, '-'); 3292 d_print_comp (dpi, d_right (dc)); 3293 switch (tp) 3294 { 3295 default: 3296 break; 3297 case D_PRINT_UNSIGNED: 3298 d_append_char (dpi, 'u'); 3299 break; 3300 case D_PRINT_LONG: 3301 d_append_char (dpi, 'l'); 3302 break; 3303 case D_PRINT_UNSIGNED_LONG: 3304 d_append_string_constant (dpi, "ul"); 3305 break; 3306 case D_PRINT_LONG_LONG: 3307 d_append_string_constant (dpi, "ll"); 3308 break; 3309 case D_PRINT_UNSIGNED_LONG_LONG: 3310 d_append_string_constant (dpi, "ull"); 3311 break; 3312 } 3313 return; 3314 } 3315 break; 3316 3317 case D_PRINT_BOOL: 3318 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME 3319 && d_right (dc)->u.s_name.len == 1 3320 && dc->type == DEMANGLE_COMPONENT_LITERAL) 3321 { 3322 switch (d_right (dc)->u.s_name.s[0]) 3323 { 3324 case '0': 3325 d_append_string_constant (dpi, "false"); 3326 return; 3327 case '1': 3328 d_append_string_constant (dpi, "true"); 3329 return; 3330 default: 3331 break; 3332 } 3333 } 3334 break; 3335 3336 default: 3337 break; 3338 } 3339 } 3340 3341 d_append_char (dpi, '('); 3342 d_print_comp (dpi, d_left (dc)); 3343 d_append_char (dpi, ')'); 3344 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG) 3345 d_append_char (dpi, '-'); 3346 if (tp == D_PRINT_FLOAT) 3347 d_append_char (dpi, '['); 3348 d_print_comp (dpi, d_right (dc)); 3349 if (tp == D_PRINT_FLOAT) 3350 d_append_char (dpi, ']'); 3351 } 3352 return; 3353 3354 default: 3355 d_print_error (dpi); 3356 return; 3357 } 3358 } 3359 3360 /* Print a Java dentifier. For Java we try to handle encoded extended 3361 Unicode characters. The C++ ABI doesn't mention Unicode encoding, 3362 so we don't it for C++. Characters are encoded as 3363 __U<hex-char>+_. */ 3364 3365 static void 3366 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len) 3367 { 3368 const char *p; 3369 const char *end; 3370 3371 end = name + len; 3372 for (p = name; p < end; ++p) 3373 { 3374 if (end - p > 3 3375 && p[0] == '_' 3376 && p[1] == '_' 3377 && p[2] == 'U') 3378 { 3379 unsigned long c; 3380 const char *q; 3381 3382 c = 0; 3383 for (q = p + 3; q < end; ++q) 3384 { 3385 int dig; 3386 3387 if (IS_DIGIT (*q)) 3388 dig = *q - '0'; 3389 else if (*q >= 'A' && *q <= 'F') 3390 dig = *q - 'A' + 10; 3391 else if (*q >= 'a' && *q <= 'f') 3392 dig = *q - 'a' + 10; 3393 else 3394 break; 3395 3396 c = c * 16 + dig; 3397 } 3398 /* If the Unicode character is larger than 256, we don't try 3399 to deal with it here. FIXME. */ 3400 if (q < end && *q == '_' && c < 256) 3401 { 3402 d_append_char (dpi, c); 3403 p = q; 3404 continue; 3405 } 3406 } 3407 3408 d_append_char (dpi, *p); 3409 } 3410 } 3411 3412 /* Print a list of modifiers. SUFFIX is 1 if we are printing 3413 qualifiers on this after printing a function. */ 3414 3415 static void 3416 d_print_mod_list (struct d_print_info *dpi, 3417 struct d_print_mod *mods, int suffix) 3418 { 3419 struct d_print_template *hold_dpt; 3420 3421 if (mods == NULL || d_print_saw_error (dpi)) 3422 return; 3423 3424 if (mods->printed 3425 || (! suffix 3426 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS 3427 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS 3428 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS))) 3429 { 3430 d_print_mod_list (dpi, mods->next, suffix); 3431 return; 3432 } 3433 3434 mods->printed = 1; 3435 3436 hold_dpt = dpi->templates; 3437 dpi->templates = mods->templates; 3438 3439 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE) 3440 { 3441 d_print_function_type (dpi, mods->mod, mods->next); 3442 dpi->templates = hold_dpt; 3443 return; 3444 } 3445 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) 3446 { 3447 d_print_array_type (dpi, mods->mod, mods->next); 3448 dpi->templates = hold_dpt; 3449 return; 3450 } 3451 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME) 3452 { 3453 struct d_print_mod *hold_modifiers; 3454 struct demangle_component *dc; 3455 3456 /* When this is on the modifier stack, we have pulled any 3457 qualifiers off the right argument already. Otherwise, we 3458 print it as usual, but don't let the left argument see any 3459 modifiers. */ 3460 3461 hold_modifiers = dpi->modifiers; 3462 dpi->modifiers = NULL; 3463 d_print_comp (dpi, d_left (mods->mod)); 3464 dpi->modifiers = hold_modifiers; 3465 3466 if ((dpi->options & DMGL_JAVA) == 0) 3467 d_append_string_constant (dpi, "::"); 3468 else 3469 d_append_char (dpi, '.'); 3470 3471 dc = d_right (mods->mod); 3472 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS 3473 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS 3474 || dc->type == DEMANGLE_COMPONENT_CONST_THIS) 3475 dc = d_left (dc); 3476 3477 d_print_comp (dpi, dc); 3478 3479 dpi->templates = hold_dpt; 3480 return; 3481 } 3482 3483 d_print_mod (dpi, mods->mod); 3484 3485 dpi->templates = hold_dpt; 3486 3487 d_print_mod_list (dpi, mods->next, suffix); 3488 } 3489 3490 /* Print a modifier. */ 3491 3492 static void 3493 d_print_mod (struct d_print_info *dpi, 3494 const struct demangle_component *mod) 3495 { 3496 switch (mod->type) 3497 { 3498 case DEMANGLE_COMPONENT_RESTRICT: 3499 case DEMANGLE_COMPONENT_RESTRICT_THIS: 3500 d_append_string_constant (dpi, " restrict"); 3501 return; 3502 case DEMANGLE_COMPONENT_VOLATILE: 3503 case DEMANGLE_COMPONENT_VOLATILE_THIS: 3504 d_append_string_constant (dpi, " volatile"); 3505 return; 3506 case DEMANGLE_COMPONENT_CONST: 3507 case DEMANGLE_COMPONENT_CONST_THIS: 3508 d_append_string_constant (dpi, " const"); 3509 return; 3510 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 3511 d_append_char (dpi, ' '); 3512 d_print_comp (dpi, d_right (mod)); 3513 return; 3514 case DEMANGLE_COMPONENT_POINTER: 3515 /* There is no pointer symbol in Java. */ 3516 if ((dpi->options & DMGL_JAVA) == 0) 3517 d_append_char (dpi, '*'); 3518 return; 3519 case DEMANGLE_COMPONENT_REFERENCE: 3520 d_append_char (dpi, '&'); 3521 return; 3522 case DEMANGLE_COMPONENT_COMPLEX: 3523 d_append_string_constant (dpi, "complex "); 3524 return; 3525 case DEMANGLE_COMPONENT_IMAGINARY: 3526 d_append_string_constant (dpi, "imaginary "); 3527 return; 3528 case DEMANGLE_COMPONENT_PTRMEM_TYPE: 3529 if (d_last_char (dpi) != '(') 3530 d_append_char (dpi, ' '); 3531 d_print_comp (dpi, d_left (mod)); 3532 d_append_string_constant (dpi, "::*"); 3533 return; 3534 case DEMANGLE_COMPONENT_TYPED_NAME: 3535 d_print_comp (dpi, d_left (mod)); 3536 return; 3537 default: 3538 /* Otherwise, we have something that won't go back on the 3539 modifier stack, so we can just print it. */ 3540 d_print_comp (dpi, mod); 3541 return; 3542 } 3543 } 3544 3545 /* Print a function type, except for the return type. */ 3546 3547 static void 3548 d_print_function_type (struct d_print_info *dpi, 3549 const struct demangle_component *dc, 3550 struct d_print_mod *mods) 3551 { 3552 int need_paren; 3553 int saw_mod; 3554 int need_space; 3555 struct d_print_mod *p; 3556 struct d_print_mod *hold_modifiers; 3557 3558 need_paren = 0; 3559 saw_mod = 0; 3560 need_space = 0; 3561 for (p = mods; p != NULL; p = p->next) 3562 { 3563 if (p->printed) 3564 break; 3565 3566 saw_mod = 1; 3567 switch (p->mod->type) 3568 { 3569 case DEMANGLE_COMPONENT_POINTER: 3570 case DEMANGLE_COMPONENT_REFERENCE: 3571 need_paren = 1; 3572 break; 3573 case DEMANGLE_COMPONENT_RESTRICT: 3574 case DEMANGLE_COMPONENT_VOLATILE: 3575 case DEMANGLE_COMPONENT_CONST: 3576 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 3577 case DEMANGLE_COMPONENT_COMPLEX: 3578 case DEMANGLE_COMPONENT_IMAGINARY: 3579 case DEMANGLE_COMPONENT_PTRMEM_TYPE: 3580 need_space = 1; 3581 need_paren = 1; 3582 break; 3583 case DEMANGLE_COMPONENT_RESTRICT_THIS: 3584 case DEMANGLE_COMPONENT_VOLATILE_THIS: 3585 case DEMANGLE_COMPONENT_CONST_THIS: 3586 break; 3587 default: 3588 break; 3589 } 3590 if (need_paren) 3591 break; 3592 } 3593 3594 if (d_left (dc) != NULL && ! saw_mod) 3595 need_paren = 1; 3596 3597 if (need_paren) 3598 { 3599 if (! need_space) 3600 { 3601 if (d_last_char (dpi) != '(' 3602 && d_last_char (dpi) != '*') 3603 need_space = 1; 3604 } 3605 if (need_space && d_last_char (dpi) != ' ') 3606 d_append_char (dpi, ' '); 3607 d_append_char (dpi, '('); 3608 } 3609 3610 hold_modifiers = dpi->modifiers; 3611 dpi->modifiers = NULL; 3612 3613 d_print_mod_list (dpi, mods, 0); 3614 3615 if (need_paren) 3616 d_append_char (dpi, ')'); 3617 3618 d_append_char (dpi, '('); 3619 3620 if (d_right (dc) != NULL) 3621 d_print_comp (dpi, d_right (dc)); 3622 3623 d_append_char (dpi, ')'); 3624 3625 d_print_mod_list (dpi, mods, 1); 3626 3627 dpi->modifiers = hold_modifiers; 3628 } 3629 3630 /* Print an array type, except for the element type. */ 3631 3632 static void 3633 d_print_array_type (struct d_print_info *dpi, 3634 const struct demangle_component *dc, 3635 struct d_print_mod *mods) 3636 { 3637 int need_space; 3638 3639 need_space = 1; 3640 if (mods != NULL) 3641 { 3642 int need_paren; 3643 struct d_print_mod *p; 3644 3645 need_paren = 0; 3646 for (p = mods; p != NULL; p = p->next) 3647 { 3648 if (! p->printed) 3649 { 3650 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) 3651 { 3652 need_space = 0; 3653 break; 3654 } 3655 else 3656 { 3657 need_paren = 1; 3658 need_space = 1; 3659 break; 3660 } 3661 } 3662 } 3663 3664 if (need_paren) 3665 d_append_string_constant (dpi, " ("); 3666 3667 d_print_mod_list (dpi, mods, 0); 3668 3669 if (need_paren) 3670 d_append_char (dpi, ')'); 3671 } 3672 3673 if (need_space) 3674 d_append_char (dpi, ' '); 3675 3676 d_append_char (dpi, '['); 3677 3678 if (d_left (dc) != NULL) 3679 d_print_comp (dpi, d_left (dc)); 3680 3681 d_append_char (dpi, ']'); 3682 } 3683 3684 /* Print an operator in an expression. */ 3685 3686 static void 3687 d_print_expr_op (struct d_print_info *dpi, 3688 const struct demangle_component *dc) 3689 { 3690 if (dc->type == DEMANGLE_COMPONENT_OPERATOR) 3691 d_append_buffer (dpi, dc->u.s_operator.op->name, 3692 dc->u.s_operator.op->len); 3693 else 3694 d_print_comp (dpi, dc); 3695 } 3696 3697 /* Print a cast. */ 3698 3699 static void 3700 d_print_cast (struct d_print_info *dpi, 3701 const struct demangle_component *dc) 3702 { 3703 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE) 3704 d_print_comp (dpi, d_left (dc)); 3705 else 3706 { 3707 struct d_print_mod *hold_dpm; 3708 struct d_print_template dpt; 3709 3710 /* It appears that for a templated cast operator, we need to put 3711 the template parameters in scope for the operator name, but 3712 not for the parameters. The effect is that we need to handle 3713 the template printing here. */ 3714 3715 hold_dpm = dpi->modifiers; 3716 dpi->modifiers = NULL; 3717 3718 dpt.next = dpi->templates; 3719 dpi->templates = &dpt; 3720 dpt.template_decl = d_left (dc); 3721 3722 d_print_comp (dpi, d_left (d_left (dc))); 3723 3724 dpi->templates = dpt.next; 3725 3726 if (d_last_char (dpi) == '<') 3727 d_append_char (dpi, ' '); 3728 d_append_char (dpi, '<'); 3729 d_print_comp (dpi, d_right (d_left (dc))); 3730 /* Avoid generating two consecutive '>' characters, to avoid 3731 the C++ syntactic ambiguity. */ 3732 if (d_last_char (dpi) == '>') 3733 d_append_char (dpi, ' '); 3734 d_append_char (dpi, '>'); 3735 3736 dpi->modifiers = hold_dpm; 3737 } 3738 } 3739 3740 /* Initialize the information structure we use to pass around 3741 information. */ 3742 3743 CP_STATIC_IF_GLIBCPP_V3 3744 void 3745 cplus_demangle_init_info (const char *mangled, int options, size_t len, 3746 struct d_info *di) 3747 { 3748 di->s = mangled; 3749 di->send = mangled + len; 3750 di->options = options; 3751 3752 di->n = mangled; 3753 3754 /* We can not need more components than twice the number of chars in 3755 the mangled string. Most components correspond directly to 3756 chars, but the ARGLIST types are exceptions. */ 3757 di->num_comps = 2 * len; 3758 di->next_comp = 0; 3759 3760 /* Similarly, we can not need more substitutions than there are 3761 chars in the mangled string. */ 3762 di->num_subs = len; 3763 di->next_sub = 0; 3764 di->did_subs = 0; 3765 3766 di->last_name = NULL; 3767 3768 di->expansion = 0; 3769 } 3770 3771 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled 3772 name, return a buffer allocated with malloc holding the demangled 3773 name. OPTIONS is the usual libiberty demangler options. On 3774 success, this sets *PALC to the allocated size of the returned 3775 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for 3776 a memory allocation failure. On failure, this returns NULL. */ 3777 3778 static char * 3779 d_demangle (const char* mangled, int options, size_t *palc) 3780 { 3781 size_t len; 3782 int type; 3783 struct d_info di; 3784 struct demangle_component *dc; 3785 int estimate; 3786 char *ret; 3787 3788 *palc = 0; 3789 3790 len = strlen (mangled); 3791 3792 if (mangled[0] == '_' && mangled[1] == 'Z') 3793 type = 0; 3794 else if (strncmp (mangled, "_GLOBAL_", 8) == 0 3795 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$') 3796 && (mangled[9] == 'D' || mangled[9] == 'I') 3797 && mangled[10] == '_') 3798 { 3799 char *r; 3800 size_t sz = 40 + len - 11; 3801 3802 r = (char *) malloc (sz); 3803 if (r == NULL) 3804 *palc = 1; 3805 else 3806 { 3807 if (mangled[9] == 'I') 3808 strlcpy (r, "global constructors keyed to ", sz); 3809 else 3810 strlcpy (r, "global destructors keyed to ", sz); 3811 strlcat (r, mangled + 11, sz); 3812 } 3813 return r; 3814 } 3815 else 3816 { 3817 if ((options & DMGL_TYPES) == 0) 3818 return NULL; 3819 type = 1; 3820 } 3821 3822 cplus_demangle_init_info (mangled, options, len, &di); 3823 3824 { 3825 #ifdef CP_DYNAMIC_ARRAYS 3826 __extension__ struct demangle_component comps[di.num_comps]; 3827 __extension__ struct demangle_component *subs[di.num_subs]; 3828 3829 di.comps = &comps[0]; 3830 di.subs = &subs[0]; 3831 #else 3832 di.comps = ((struct demangle_component *) 3833 malloc (di.num_comps * sizeof (struct demangle_component))); 3834 di.subs = ((struct demangle_component **) 3835 malloc (di.num_subs * sizeof (struct demangle_component *))); 3836 if (di.comps == NULL || di.subs == NULL) 3837 { 3838 if (di.comps != NULL) 3839 free (di.comps); 3840 if (di.subs != NULL) 3841 free (di.subs); 3842 *palc = 1; 3843 return NULL; 3844 } 3845 #endif 3846 3847 if (! type) 3848 dc = cplus_demangle_mangled_name (&di, 1); 3849 else 3850 dc = cplus_demangle_type (&di); 3851 3852 /* If DMGL_PARAMS is set, then if we didn't consume the entire 3853 mangled string, then we didn't successfully demangle it. If 3854 DMGL_PARAMS is not set, we didn't look at the trailing 3855 parameters. */ 3856 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0') 3857 dc = NULL; 3858 3859 #ifdef CP_DEMANGLE_DEBUG 3860 if (dc == NULL) 3861 printf ("failed demangling\n"); 3862 else 3863 d_dump (dc, 0); 3864 #endif 3865 3866 /* We try to guess the length of the demangled string, to minimize 3867 calls to realloc during demangling. */ 3868 estimate = len + di.expansion + 10 * di.did_subs; 3869 estimate += estimate / 8; 3870 3871 ret = NULL; 3872 if (dc != NULL) 3873 ret = cplus_demangle_print (options, dc, estimate, palc); 3874 3875 #ifndef CP_DYNAMIC_ARRAYS 3876 free (di.comps); 3877 free (di.subs); 3878 #endif 3879 3880 #ifdef CP_DEMANGLE_DEBUG 3881 if (ret != NULL) 3882 { 3883 int rlen; 3884 3885 rlen = strlen (ret); 3886 if (rlen > 2 * estimate) 3887 printf ("*** Length %d much greater than estimate %d\n", 3888 rlen, estimate); 3889 else if (rlen > estimate) 3890 printf ("*** Length %d greater than estimate %d\n", 3891 rlen, estimate); 3892 else if (rlen < estimate / 2) 3893 printf ("*** Length %d much less than estimate %d\n", 3894 rlen, estimate); 3895 } 3896 #endif 3897 } 3898 3899 return ret; 3900 } 3901 3902 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3) 3903 3904 extern char *__cxa_demangle (const char *, char *, size_t *, int *); 3905 3906 /* ia64 ABI-mandated entry point in the C++ runtime library for 3907 performing demangling. MANGLED_NAME is a NUL-terminated character 3908 string containing the name to be demangled. 3909 3910 OUTPUT_BUFFER is a region of memory, allocated with malloc, of 3911 *LENGTH bytes, into which the demangled name is stored. If 3912 OUTPUT_BUFFER is not long enough, it is expanded using realloc. 3913 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name 3914 is placed in a region of memory allocated with malloc. 3915 3916 If LENGTH is non-NULL, the length of the buffer containing the 3917 demangled name, is placed in *LENGTH. 3918 3919 The return value is a pointer to the start of the NUL-terminated 3920 demangled name, or NULL if the demangling fails. The caller is 3921 responsible for deallocating this memory using free. 3922 3923 *STATUS is set to one of the following values: 3924 0: The demangling operation succeeded. 3925 -1: A memory allocation failure occurred. 3926 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules. 3927 -3: One of the arguments is invalid. 3928 3929 The demangling is performed using the C++ ABI mangling rules, with 3930 GNU extensions. */ 3931 3932 char * 3933 __cxa_demangle (const char *mangled_name, char *output_buffer, 3934 size_t *length, int *status) 3935 { 3936 char *demangled; 3937 size_t alc; 3938 3939 if (mangled_name == NULL) 3940 { 3941 if (status != NULL) 3942 *status = -3; 3943 return NULL; 3944 } 3945 3946 if (output_buffer != NULL && length == NULL) 3947 { 3948 if (status != NULL) 3949 *status = -3; 3950 return NULL; 3951 } 3952 3953 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc); 3954 3955 if (demangled == NULL) 3956 { 3957 if (status != NULL) 3958 { 3959 if (alc == 1) 3960 *status = -1; 3961 else 3962 *status = -2; 3963 } 3964 return NULL; 3965 } 3966 3967 if (output_buffer == NULL) 3968 { 3969 if (length != NULL) 3970 *length = alc; 3971 } 3972 else 3973 { 3974 if (strlen (demangled) < *length) 3975 { 3976 strlcpy (output_buffer, demangled, *length); 3977 free (demangled); 3978 demangled = output_buffer; 3979 } 3980 else 3981 { 3982 free (output_buffer); 3983 *length = alc; 3984 } 3985 } 3986 3987 if (status != NULL) 3988 *status = 0; 3989 3990 return demangled; 3991 } 3992 3993 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */ 3994 3995 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI 3996 mangled name, return a buffer allocated with malloc holding the 3997 demangled name. Otherwise, return NULL. */ 3998 3999 char * 4000 cplus_demangle_v3 (const char* mangled, int options) 4001 { 4002 size_t alc; 4003 4004 return d_demangle (mangled, options, &alc); 4005 } 4006 4007 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling 4008 conventions, but the output formatting is a little different. 4009 This instructs the C++ demangler not to emit pointer characters ("*"), and 4010 to use Java's namespace separator symbol ("." instead of "::"). It then 4011 does an additional pass over the demangled output to replace instances 4012 of JArray<TYPE> with TYPE[]. */ 4013 4014 char * 4015 java_demangle_v3 (const char* mangled) 4016 { 4017 size_t alc; 4018 char *demangled; 4019 int nesting; 4020 char *from; 4021 char *to; 4022 4023 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, 4024 &alc); 4025 4026 if (demangled == NULL) 4027 return NULL; 4028 4029 nesting = 0; 4030 from = demangled; 4031 to = from; 4032 while (*from != '\0') 4033 { 4034 if (strncmp (from, "JArray<", 7) == 0) 4035 { 4036 from += 7; 4037 ++nesting; 4038 } 4039 else if (nesting > 0 && *from == '>') 4040 { 4041 while (to > demangled && to[-1] == ' ') 4042 --to; 4043 *to++ = '['; 4044 *to++ = ']'; 4045 --nesting; 4046 ++from; 4047 } 4048 else 4049 *to++ = *from++; 4050 } 4051 4052 *to = '\0'; 4053 4054 return demangled; 4055 } 4056 4057 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */ 4058 4059 #ifndef IN_GLIBCPP_V3 4060 4061 /* Demangle a string in order to find out whether it is a constructor 4062 or destructor. Return non-zero on success. Set *CTOR_KIND and 4063 *DTOR_KIND appropriately. */ 4064 4065 static int 4066 is_ctor_or_dtor (const char *mangled, 4067 enum gnu_v3_ctor_kinds *ctor_kind, 4068 enum gnu_v3_dtor_kinds *dtor_kind) 4069 { 4070 struct d_info di; 4071 struct demangle_component *dc; 4072 int ret; 4073 4074 *ctor_kind = (enum gnu_v3_ctor_kinds) 0; 4075 *dtor_kind = (enum gnu_v3_dtor_kinds) 0; 4076 4077 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di); 4078 4079 { 4080 #ifdef CP_DYNAMIC_ARRAYS 4081 __extension__ struct demangle_component comps[di.num_comps]; 4082 __extension__ struct demangle_component *subs[di.num_subs]; 4083 4084 di.comps = &comps[0]; 4085 di.subs = &subs[0]; 4086 #else 4087 di.comps = ((struct demangle_component *) 4088 malloc (di.num_comps * sizeof (struct demangle_component))); 4089 di.subs = ((struct demangle_component **) 4090 malloc (di.num_subs * sizeof (struct demangle_component *))); 4091 if (di.comps == NULL || di.subs == NULL) 4092 { 4093 if (di.comps != NULL) 4094 free (di.comps); 4095 if (di.subs != NULL) 4096 free (di.subs); 4097 return 0; 4098 } 4099 #endif 4100 4101 dc = cplus_demangle_mangled_name (&di, 1); 4102 4103 /* Note that because we did not pass DMGL_PARAMS, we don't expect 4104 to demangle the entire string. */ 4105 4106 ret = 0; 4107 while (dc != NULL) 4108 { 4109 switch (dc->type) 4110 { 4111 default: 4112 dc = NULL; 4113 break; 4114 case DEMANGLE_COMPONENT_TYPED_NAME: 4115 case DEMANGLE_COMPONENT_TEMPLATE: 4116 case DEMANGLE_COMPONENT_RESTRICT_THIS: 4117 case DEMANGLE_COMPONENT_VOLATILE_THIS: 4118 case DEMANGLE_COMPONENT_CONST_THIS: 4119 dc = d_left (dc); 4120 break; 4121 case DEMANGLE_COMPONENT_QUAL_NAME: 4122 case DEMANGLE_COMPONENT_LOCAL_NAME: 4123 dc = d_right (dc); 4124 break; 4125 case DEMANGLE_COMPONENT_CTOR: 4126 *ctor_kind = dc->u.s_ctor.kind; 4127 ret = 1; 4128 dc = NULL; 4129 break; 4130 case DEMANGLE_COMPONENT_DTOR: 4131 *dtor_kind = dc->u.s_dtor.kind; 4132 ret = 1; 4133 dc = NULL; 4134 break; 4135 } 4136 } 4137 4138 #ifndef CP_DYNAMIC_ARRAYS 4139 free (di.subs); 4140 free (di.comps); 4141 #endif 4142 } 4143 4144 return ret; 4145 } 4146 4147 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor 4148 name. A non-zero return indicates the type of constructor. */ 4149 4150 enum gnu_v3_ctor_kinds 4151 is_gnu_v3_mangled_ctor (const char *name) 4152 { 4153 enum gnu_v3_ctor_kinds ctor_kind; 4154 enum gnu_v3_dtor_kinds dtor_kind; 4155 4156 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind)) 4157 return (enum gnu_v3_ctor_kinds) 0; 4158 return ctor_kind; 4159 } 4160 4161 4162 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor 4163 name. A non-zero return indicates the type of destructor. */ 4164 4165 enum gnu_v3_dtor_kinds 4166 is_gnu_v3_mangled_dtor (const char *name) 4167 { 4168 enum gnu_v3_ctor_kinds ctor_kind; 4169 enum gnu_v3_dtor_kinds dtor_kind; 4170 4171 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind)) 4172 return (enum gnu_v3_dtor_kinds) 0; 4173 return dtor_kind; 4174 } 4175 4176 #endif /* IN_GLIBCPP_V3 */ 4177 4178 #ifdef STANDALONE_DEMANGLER 4179 4180 #include "getopt.h" 4181 #include "dyn-string.h" 4182 4183 static void print_usage (FILE* fp, int exit_value); 4184 4185 #define IS_ALPHA(CHAR) \ 4186 (((CHAR) >= 'a' && (CHAR) <= 'z') \ 4187 || ((CHAR) >= 'A' && (CHAR) <= 'Z')) 4188 4189 /* Non-zero if CHAR is a character than can occur in a mangled name. */ 4190 #define is_mangled_char(CHAR) \ 4191 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \ 4192 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$') 4193 4194 /* The name of this program, as invoked. */ 4195 const char* program_name; 4196 4197 /* Prints usage summary to FP and then exits with EXIT_VALUE. */ 4198 4199 static void 4200 print_usage (FILE* fp, int exit_value) 4201 { 4202 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name); 4203 fprintf (fp, "Options:\n"); 4204 fprintf (fp, " -h,--help Display this message.\n"); 4205 fprintf (fp, " -p,--no-params Don't display function parameters\n"); 4206 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n"); 4207 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n"); 4208 4209 exit (exit_value); 4210 } 4211 4212 /* Option specification for getopt_long. */ 4213 static const struct option long_options[] = 4214 { 4215 { "help", no_argument, NULL, 'h' }, 4216 { "no-params", no_argument, NULL, 'p' }, 4217 { "verbose", no_argument, NULL, 'v' }, 4218 { NULL, no_argument, NULL, 0 }, 4219 }; 4220 4221 /* Main entry for a demangling filter executable. It will demangle 4222 its command line arguments, if any. If none are provided, it will 4223 filter stdin to stdout, replacing any recognized mangled C++ names 4224 with their demangled equivalents. */ 4225 4226 int 4227 main (int argc, char *argv[]) 4228 { 4229 int i; 4230 int opt_char; 4231 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES; 4232 4233 /* Use the program name of this program, as invoked. */ 4234 program_name = argv[0]; 4235 4236 /* Parse options. */ 4237 do 4238 { 4239 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL); 4240 switch (opt_char) 4241 { 4242 case '?': /* Unrecognized option. */ 4243 print_usage (stderr, 1); 4244 break; 4245 4246 case 'h': 4247 print_usage (stdout, 0); 4248 break; 4249 4250 case 'p': 4251 options &= ~ DMGL_PARAMS; 4252 break; 4253 4254 case 'v': 4255 options |= DMGL_VERBOSE; 4256 break; 4257 } 4258 } 4259 while (opt_char != -1); 4260 4261 if (optind == argc) 4262 /* No command line arguments were provided. Filter stdin. */ 4263 { 4264 dyn_string_t mangled = dyn_string_new (3); 4265 char *s; 4266 4267 /* Read all of input. */ 4268 while (!feof (stdin)) 4269 { 4270 char c; 4271 4272 /* Pile characters into mangled until we hit one that can't 4273 occur in a mangled name. */ 4274 c = getchar (); 4275 while (!feof (stdin) && is_mangled_char (c)) 4276 { 4277 dyn_string_append_char (mangled, c); 4278 if (feof (stdin)) 4279 break; 4280 c = getchar (); 4281 } 4282 4283 if (dyn_string_length (mangled) > 0) 4284 { 4285 #ifdef IN_GLIBCPP_V3 4286 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL); 4287 #else 4288 s = cplus_demangle_v3 (dyn_string_buf (mangled), options); 4289 #endif 4290 4291 if (s != NULL) 4292 { 4293 fputs (s, stdout); 4294 free (s); 4295 } 4296 else 4297 { 4298 /* It might not have been a mangled name. Print the 4299 original text. */ 4300 fputs (dyn_string_buf (mangled), stdout); 4301 } 4302 4303 dyn_string_clear (mangled); 4304 } 4305 4306 /* If we haven't hit EOF yet, we've read one character that 4307 can't occur in a mangled name, so print it out. */ 4308 if (!feof (stdin)) 4309 putchar (c); 4310 } 4311 4312 dyn_string_delete (mangled); 4313 } 4314 else 4315 /* Demangle command line arguments. */ 4316 { 4317 /* Loop over command line arguments. */ 4318 for (i = optind; i < argc; ++i) 4319 { 4320 char *s; 4321 #ifdef IN_GLIBCPP_V3 4322 int status; 4323 #endif 4324 4325 /* Attempt to demangle. */ 4326 #ifdef IN_GLIBCPP_V3 4327 s = __cxa_demangle (argv[i], NULL, NULL, &status); 4328 #else 4329 s = cplus_demangle_v3 (argv[i], options); 4330 #endif 4331 4332 /* If it worked, print the demangled name. */ 4333 if (s != NULL) 4334 { 4335 printf ("%s\n", s); 4336 free (s); 4337 } 4338 else 4339 { 4340 #ifdef IN_GLIBCPP_V3 4341 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status); 4342 #else 4343 fprintf (stderr, "Failed: %s\n", argv[i]); 4344 #endif 4345 } 4346 } 4347 } 4348 4349 return 0; 4350 } 4351 4352 #endif /* STANDALONE_DEMANGLER */ 4353