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