re PR c++/70266 (ICE on invalid code on x86_64-linux-gnu: unexpected expression ...
[official-gcc.git] / libiberty / cplus-dem.c
bloba990e072975e2b7137817ec8b316201954c34a6e
1 /* Demangler for GNU C++
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
4 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
5 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
7 This file is part of the libiberty library.
8 Libiberty is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU Library General Public
14 License, the Free Software Foundation gives you unlimited permission
15 to link the compiled version of this file into combinations with other
16 programs, and to distribute those combinations without any restriction
17 coming from the use of this file. (The Library Public License
18 restrictions do apply in other respects; for example, they cover
19 modification of the file, and distribution when not linked into a
20 combined executable.)
22 Libiberty is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Library General Public License for more details.
27 You should have received a copy of the GNU Library General Public
28 License along with libiberty; see the file COPYING.LIB. If
29 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
30 Boston, MA 02110-1301, USA. */
32 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
34 This file imports xmalloc and xrealloc, which are like malloc and
35 realloc except that they generate a fatal error if there is no
36 available memory. */
38 /* This file lives in both GCC and libiberty. When making changes, please
39 try not to break either. */
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
45 #include "safe-ctype.h"
47 #include <sys/types.h>
48 #include <string.h>
49 #include <stdio.h>
51 #ifdef HAVE_STDLIB_H
52 #include <stdlib.h>
53 #else
54 void * malloc ();
55 void * realloc ();
56 #endif
58 #ifdef HAVE_LIMITS_H
59 #include <limits.h>
60 #endif
61 #ifndef INT_MAX
62 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
63 #endif
65 #include <demangle.h>
66 #undef CURRENT_DEMANGLING_STYLE
67 #define CURRENT_DEMANGLING_STYLE work->options
69 #include "libiberty.h"
71 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
73 /* A value at least one greater than the maximum number of characters
74 that will be output when using the `%d' format with `printf'. */
75 #define INTBUF_SIZE 32
77 extern void fancy_abort (void) ATTRIBUTE_NORETURN;
79 /* In order to allow a single demangler executable to demangle strings
80 using various common values of CPLUS_MARKER, as well as any specific
81 one set at compile time, we maintain a string containing all the
82 commonly used ones, and check to see if the marker we are looking for
83 is in that string. CPLUS_MARKER is usually '$' on systems where the
84 assembler can deal with that. Where the assembler can't, it's usually
85 '.' (but on many systems '.' is used for other things). We put the
86 current defined CPLUS_MARKER first (which defaults to '$'), followed
87 by the next most common value, followed by an explicit '$' in case
88 the value of CPLUS_MARKER is not '$'.
90 We could avoid this if we could just get g++ to tell us what the actual
91 cplus marker character is as part of the debug information, perhaps by
92 ensuring that it is the character that terminates the gcc<n>_compiled
93 marker symbol (FIXME). */
95 #if !defined (CPLUS_MARKER)
96 #define CPLUS_MARKER '$'
97 #endif
99 enum demangling_styles current_demangling_style = auto_demangling;
101 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
103 static char char_str[2] = { '\000', '\000' };
105 void
106 set_cplus_marker_for_demangling (int ch)
108 cplus_markers[0] = ch;
111 typedef struct string /* Beware: these aren't required to be */
112 { /* '\0' terminated. */
113 char *b; /* pointer to start of string */
114 char *p; /* pointer after last character */
115 char *e; /* pointer after end of allocated space */
116 } string;
118 /* Stuff that is shared between sub-routines.
119 Using a shared structure allows cplus_demangle to be reentrant. */
121 struct work_stuff
123 int options;
124 char **typevec;
125 char **ktypevec;
126 char **btypevec;
127 int numk;
128 int numb;
129 int ksize;
130 int bsize;
131 int ntypes;
132 int typevec_size;
133 int constructor;
134 int destructor;
135 int static_type; /* A static member function */
136 int temp_start; /* index in demangled to start of template args */
137 int type_quals; /* The type qualifiers. */
138 int dllimported; /* Symbol imported from a PE DLL */
139 char **tmpl_argvec; /* Template function arguments. */
140 int ntmpl_args; /* The number of template function arguments. */
141 int forgetting_types; /* Nonzero if we are not remembering the types
142 we see. */
143 string* previous_argument; /* The last function argument demangled. */
144 int nrepeats; /* The number of times to repeat the previous
145 argument. */
146 int *proctypevec; /* Indices of currently processed remembered typevecs. */
147 int proctypevec_size;
148 int nproctypes;
151 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
152 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
154 static const struct optable
156 const char *const in;
157 const char *const out;
158 const int flags;
159 } optable[] = {
160 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
161 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
162 {"new", " new", 0}, /* old (1.91, and 1.x) */
163 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
164 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
165 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
166 {"as", "=", DMGL_ANSI}, /* ansi */
167 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
168 {"eq", "==", DMGL_ANSI}, /* old, ansi */
169 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
170 {"gt", ">", DMGL_ANSI}, /* old, ansi */
171 {"le", "<=", DMGL_ANSI}, /* old, ansi */
172 {"lt", "<", DMGL_ANSI}, /* old, ansi */
173 {"plus", "+", 0}, /* old */
174 {"pl", "+", DMGL_ANSI}, /* ansi */
175 {"apl", "+=", DMGL_ANSI}, /* ansi */
176 {"minus", "-", 0}, /* old */
177 {"mi", "-", DMGL_ANSI}, /* ansi */
178 {"ami", "-=", DMGL_ANSI}, /* ansi */
179 {"mult", "*", 0}, /* old */
180 {"ml", "*", DMGL_ANSI}, /* ansi */
181 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
182 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
183 {"convert", "+", 0}, /* old (unary +) */
184 {"negate", "-", 0}, /* old (unary -) */
185 {"trunc_mod", "%", 0}, /* old */
186 {"md", "%", DMGL_ANSI}, /* ansi */
187 {"amd", "%=", DMGL_ANSI}, /* ansi */
188 {"trunc_div", "/", 0}, /* old */
189 {"dv", "/", DMGL_ANSI}, /* ansi */
190 {"adv", "/=", DMGL_ANSI}, /* ansi */
191 {"truth_andif", "&&", 0}, /* old */
192 {"aa", "&&", DMGL_ANSI}, /* ansi */
193 {"truth_orif", "||", 0}, /* old */
194 {"oo", "||", DMGL_ANSI}, /* ansi */
195 {"truth_not", "!", 0}, /* old */
196 {"nt", "!", DMGL_ANSI}, /* ansi */
197 {"postincrement","++", 0}, /* old */
198 {"pp", "++", DMGL_ANSI}, /* ansi */
199 {"postdecrement","--", 0}, /* old */
200 {"mm", "--", DMGL_ANSI}, /* ansi */
201 {"bit_ior", "|", 0}, /* old */
202 {"or", "|", DMGL_ANSI}, /* ansi */
203 {"aor", "|=", DMGL_ANSI}, /* ansi */
204 {"bit_xor", "^", 0}, /* old */
205 {"er", "^", DMGL_ANSI}, /* ansi */
206 {"aer", "^=", DMGL_ANSI}, /* ansi */
207 {"bit_and", "&", 0}, /* old */
208 {"ad", "&", DMGL_ANSI}, /* ansi */
209 {"aad", "&=", DMGL_ANSI}, /* ansi */
210 {"bit_not", "~", 0}, /* old */
211 {"co", "~", DMGL_ANSI}, /* ansi */
212 {"call", "()", 0}, /* old */
213 {"cl", "()", DMGL_ANSI}, /* ansi */
214 {"alshift", "<<", 0}, /* old */
215 {"ls", "<<", DMGL_ANSI}, /* ansi */
216 {"als", "<<=", DMGL_ANSI}, /* ansi */
217 {"arshift", ">>", 0}, /* old */
218 {"rs", ">>", DMGL_ANSI}, /* ansi */
219 {"ars", ">>=", DMGL_ANSI}, /* ansi */
220 {"component", "->", 0}, /* old */
221 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
222 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
223 {"indirect", "*", 0}, /* old */
224 {"method_call", "->()", 0}, /* old */
225 {"addr", "&", 0}, /* old (unary &) */
226 {"array", "[]", 0}, /* old */
227 {"vc", "[]", DMGL_ANSI}, /* ansi */
228 {"compound", ", ", 0}, /* old */
229 {"cm", ", ", DMGL_ANSI}, /* ansi */
230 {"cond", "?:", 0}, /* old */
231 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
232 {"max", ">?", 0}, /* old */
233 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
234 {"min", "<?", 0}, /* old */
235 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
236 {"nop", "", 0}, /* old (for operator=) */
237 {"rm", "->*", DMGL_ANSI}, /* ansi */
238 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
241 /* These values are used to indicate the various type varieties.
242 They are all non-zero so that they can be used as `success'
243 values. */
244 typedef enum type_kind_t
246 tk_none,
247 tk_pointer,
248 tk_reference,
249 tk_rvalue_reference,
250 tk_integral,
251 tk_bool,
252 tk_char,
253 tk_real
254 } type_kind_t;
256 const struct demangler_engine libiberty_demanglers[] =
259 NO_DEMANGLING_STYLE_STRING,
260 no_demangling,
261 "Demangling disabled"
265 AUTO_DEMANGLING_STYLE_STRING,
266 auto_demangling,
267 "Automatic selection based on executable"
271 GNU_DEMANGLING_STYLE_STRING,
272 gnu_demangling,
273 "GNU (g++) style demangling"
277 LUCID_DEMANGLING_STYLE_STRING,
278 lucid_demangling,
279 "Lucid (lcc) style demangling"
283 ARM_DEMANGLING_STYLE_STRING,
284 arm_demangling,
285 "ARM style demangling"
289 HP_DEMANGLING_STYLE_STRING,
290 hp_demangling,
291 "HP (aCC) style demangling"
295 EDG_DEMANGLING_STYLE_STRING,
296 edg_demangling,
297 "EDG style demangling"
301 GNU_V3_DEMANGLING_STYLE_STRING,
302 gnu_v3_demangling,
303 "GNU (g++) V3 ABI-style demangling"
307 JAVA_DEMANGLING_STYLE_STRING,
308 java_demangling,
309 "Java style demangling"
313 GNAT_DEMANGLING_STYLE_STRING,
314 gnat_demangling,
315 "GNAT style demangling"
319 DLANG_DEMANGLING_STYLE_STRING,
320 dlang_demangling,
321 "DLANG style demangling"
325 RUST_DEMANGLING_STYLE_STRING,
326 rust_demangling,
327 "Rust style demangling"
331 NULL, unknown_demangling, NULL
335 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
336 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
337 string_append(str, " ");}
338 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
340 /* The scope separator appropriate for the language being demangled. */
342 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
344 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
345 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
347 /* Prototypes for local functions */
349 static void delete_work_stuff (struct work_stuff *);
351 static void delete_non_B_K_work_stuff (struct work_stuff *);
353 static char *mop_up (struct work_stuff *, string *, int);
355 static void squangle_mop_up (struct work_stuff *);
357 static void work_stuff_copy_to_from (struct work_stuff *, struct work_stuff *);
359 #if 0
360 static int
361 demangle_method_args (struct work_stuff *, const char **, string *);
362 #endif
364 static char *
365 internal_cplus_demangle (struct work_stuff *, const char *);
367 static int
368 demangle_template_template_parm (struct work_stuff *work,
369 const char **, string *);
371 static int
372 demangle_template (struct work_stuff *work, const char **, string *,
373 string *, int, int);
375 static int
376 arm_pt (struct work_stuff *, const char *, int, const char **,
377 const char **);
379 static int
380 demangle_class_name (struct work_stuff *, const char **, string *);
382 static int
383 demangle_qualified (struct work_stuff *, const char **, string *,
384 int, int);
386 static int demangle_class (struct work_stuff *, const char **, string *);
388 static int demangle_fund_type (struct work_stuff *, const char **, string *);
390 static int demangle_signature (struct work_stuff *, const char **, string *);
392 static int demangle_prefix (struct work_stuff *, const char **, string *);
394 static int gnu_special (struct work_stuff *, const char **, string *);
396 static int arm_special (const char **, string *);
398 static void string_need (string *, int);
400 static void string_delete (string *);
402 static void
403 string_init (string *);
405 static void string_clear (string *);
407 #if 0
408 static int string_empty (string *);
409 #endif
411 static void string_append (string *, const char *);
413 static void string_appends (string *, string *);
415 static void string_appendn (string *, const char *, int);
417 static void string_prepend (string *, const char *);
419 static void string_prependn (string *, const char *, int);
421 static void string_append_template_idx (string *, int);
423 static int get_count (const char **, int *);
425 static int consume_count (const char **);
427 static int consume_count_with_underscores (const char**);
429 static int demangle_args (struct work_stuff *, const char **, string *);
431 static int demangle_nested_args (struct work_stuff*, const char**, string*);
433 static int do_type (struct work_stuff *, const char **, string *);
435 static int do_arg (struct work_stuff *, const char **, string *);
437 static int
438 demangle_function_name (struct work_stuff *, const char **, string *,
439 const char *);
441 static int
442 iterate_demangle_function (struct work_stuff *,
443 const char **, string *, const char *);
445 static void remember_type (struct work_stuff *, const char *, int);
447 static void push_processed_type (struct work_stuff *, int);
449 static void pop_processed_type (struct work_stuff *);
451 static void remember_Btype (struct work_stuff *, const char *, int, int);
453 static int register_Btype (struct work_stuff *);
455 static void remember_Ktype (struct work_stuff *, const char *, int);
457 static void forget_types (struct work_stuff *);
459 static void forget_B_and_K_types (struct work_stuff *);
461 static void string_prepends (string *, string *);
463 static int
464 demangle_template_value_parm (struct work_stuff*, const char**,
465 string*, type_kind_t);
467 static int
468 do_hpacc_template_const_value (struct work_stuff *, const char **, string *);
470 static int
471 do_hpacc_template_literal (struct work_stuff *, const char **, string *);
473 static int snarf_numeric_literal (const char **, string *);
475 /* There is a TYPE_QUAL value for each type qualifier. They can be
476 combined by bitwise-or to form the complete set of qualifiers for a
477 type. */
479 #define TYPE_UNQUALIFIED 0x0
480 #define TYPE_QUAL_CONST 0x1
481 #define TYPE_QUAL_VOLATILE 0x2
482 #define TYPE_QUAL_RESTRICT 0x4
484 static int code_for_qualifier (int);
486 static const char* qualifier_string (int);
488 static const char* demangle_qualifier (int);
490 static int demangle_expression (struct work_stuff *, const char **, string *,
491 type_kind_t);
493 static int
494 demangle_integral_value (struct work_stuff *, const char **, string *);
496 static int
497 demangle_real_value (struct work_stuff *, const char **, string *);
499 static void
500 demangle_arm_hp_template (struct work_stuff *, const char **, int, string *);
502 static void
503 recursively_demangle (struct work_stuff *, const char **, string *, int);
505 /* Translate count to integer, consuming tokens in the process.
506 Conversion terminates on the first non-digit character.
508 Trying to consume something that isn't a count results in no
509 consumption of input and a return of -1.
511 Overflow consumes the rest of the digits, and returns -1. */
513 static int
514 consume_count (const char **type)
516 int count = 0;
518 if (! ISDIGIT ((unsigned char)**type))
519 return -1;
521 while (ISDIGIT ((unsigned char)**type))
523 count *= 10;
525 /* Check for overflow.
526 We assume that count is represented using two's-complement;
527 no power of two is divisible by ten, so if an overflow occurs
528 when multiplying by ten, the result will not be a multiple of
529 ten. */
530 if ((count % 10) != 0)
532 while (ISDIGIT ((unsigned char) **type))
533 (*type)++;
534 return -1;
537 count += **type - '0';
538 (*type)++;
541 if (count < 0)
542 count = -1;
544 return (count);
548 /* Like consume_count, but for counts that are preceded and followed
549 by '_' if they are greater than 10. Also, -1 is returned for
550 failure, since 0 can be a valid value. */
552 static int
553 consume_count_with_underscores (const char **mangled)
555 int idx;
557 if (**mangled == '_')
559 (*mangled)++;
560 if (!ISDIGIT ((unsigned char)**mangled))
561 return -1;
563 idx = consume_count (mangled);
564 if (**mangled != '_')
565 /* The trailing underscore was missing. */
566 return -1;
568 (*mangled)++;
570 else
572 if (**mangled < '0' || **mangled > '9')
573 return -1;
575 idx = **mangled - '0';
576 (*mangled)++;
579 return idx;
582 /* C is the code for a type-qualifier. Return the TYPE_QUAL
583 corresponding to this qualifier. */
585 static int
586 code_for_qualifier (int c)
588 switch (c)
590 case 'C':
591 return TYPE_QUAL_CONST;
593 case 'V':
594 return TYPE_QUAL_VOLATILE;
596 case 'u':
597 return TYPE_QUAL_RESTRICT;
599 default:
600 break;
603 /* C was an invalid qualifier. */
604 abort ();
607 /* Return the string corresponding to the qualifiers given by
608 TYPE_QUALS. */
610 static const char*
611 qualifier_string (int type_quals)
613 switch (type_quals)
615 case TYPE_UNQUALIFIED:
616 return "";
618 case TYPE_QUAL_CONST:
619 return "const";
621 case TYPE_QUAL_VOLATILE:
622 return "volatile";
624 case TYPE_QUAL_RESTRICT:
625 return "__restrict";
627 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
628 return "const volatile";
630 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
631 return "const __restrict";
633 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
634 return "volatile __restrict";
636 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
637 return "const volatile __restrict";
639 default:
640 break;
643 /* TYPE_QUALS was an invalid qualifier set. */
644 abort ();
647 /* C is the code for a type-qualifier. Return the string
648 corresponding to this qualifier. This function should only be
649 called with a valid qualifier code. */
651 static const char*
652 demangle_qualifier (int c)
654 return qualifier_string (code_for_qualifier (c));
658 cplus_demangle_opname (const char *opname, char *result, int options)
660 int len, len1, ret;
661 string type;
662 struct work_stuff work[1];
663 const char *tem;
665 len = strlen(opname);
666 result[0] = '\0';
667 ret = 0;
668 memset ((char *) work, 0, sizeof (work));
669 work->options = options;
671 if (opname[0] == '_' && opname[1] == '_'
672 && opname[2] == 'o' && opname[3] == 'p')
674 /* ANSI. */
675 /* type conversion operator. */
676 tem = opname + 4;
677 if (do_type (work, &tem, &type))
679 strcat (result, "operator ");
680 strncat (result, type.b, type.p - type.b);
681 string_delete (&type);
682 ret = 1;
685 else if (opname[0] == '_' && opname[1] == '_'
686 && ISLOWER((unsigned char)opname[2])
687 && ISLOWER((unsigned char)opname[3]))
689 if (opname[4] == '\0')
691 /* Operator. */
692 size_t i;
693 for (i = 0; i < ARRAY_SIZE (optable); i++)
695 if (strlen (optable[i].in) == 2
696 && memcmp (optable[i].in, opname + 2, 2) == 0)
698 strcat (result, "operator");
699 strcat (result, optable[i].out);
700 ret = 1;
701 break;
705 else
707 if (opname[2] == 'a' && opname[5] == '\0')
709 /* Assignment. */
710 size_t i;
711 for (i = 0; i < ARRAY_SIZE (optable); i++)
713 if (strlen (optable[i].in) == 3
714 && memcmp (optable[i].in, opname + 2, 3) == 0)
716 strcat (result, "operator");
717 strcat (result, optable[i].out);
718 ret = 1;
719 break;
725 else if (len >= 3
726 && opname[0] == 'o'
727 && opname[1] == 'p'
728 && strchr (cplus_markers, opname[2]) != NULL)
730 /* see if it's an assignment expression */
731 if (len >= 10 /* op$assign_ */
732 && memcmp (opname + 3, "assign_", 7) == 0)
734 size_t i;
735 for (i = 0; i < ARRAY_SIZE (optable); i++)
737 len1 = len - 10;
738 if ((int) strlen (optable[i].in) == len1
739 && memcmp (optable[i].in, opname + 10, len1) == 0)
741 strcat (result, "operator");
742 strcat (result, optable[i].out);
743 strcat (result, "=");
744 ret = 1;
745 break;
749 else
751 size_t i;
752 for (i = 0; i < ARRAY_SIZE (optable); i++)
754 len1 = len - 3;
755 if ((int) strlen (optable[i].in) == len1
756 && memcmp (optable[i].in, opname + 3, len1) == 0)
758 strcat (result, "operator");
759 strcat (result, optable[i].out);
760 ret = 1;
761 break;
766 else if (len >= 5 && memcmp (opname, "type", 4) == 0
767 && strchr (cplus_markers, opname[4]) != NULL)
769 /* type conversion operator */
770 tem = opname + 5;
771 if (do_type (work, &tem, &type))
773 strcat (result, "operator ");
774 strncat (result, type.b, type.p - type.b);
775 string_delete (&type);
776 ret = 1;
779 squangle_mop_up (work);
780 return ret;
784 /* Takes operator name as e.g. "++" and returns mangled
785 operator name (e.g. "postincrement_expr"), or NULL if not found.
787 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
788 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
790 const char *
791 cplus_mangle_opname (const char *opname, int options)
793 size_t i;
794 int len;
796 len = strlen (opname);
797 for (i = 0; i < ARRAY_SIZE (optable); i++)
799 if ((int) strlen (optable[i].out) == len
800 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
801 && memcmp (optable[i].out, opname, len) == 0)
802 return optable[i].in;
804 return (0);
807 /* Add a routine to set the demangling style to be sure it is valid and
808 allow for any demangler initialization that maybe necessary. */
810 enum demangling_styles
811 cplus_demangle_set_style (enum demangling_styles style)
813 const struct demangler_engine *demangler = libiberty_demanglers;
815 for (; demangler->demangling_style != unknown_demangling; ++demangler)
816 if (style == demangler->demangling_style)
818 current_demangling_style = style;
819 return current_demangling_style;
822 return unknown_demangling;
825 /* Do string name to style translation */
827 enum demangling_styles
828 cplus_demangle_name_to_style (const char *name)
830 const struct demangler_engine *demangler = libiberty_demanglers;
832 for (; demangler->demangling_style != unknown_demangling; ++demangler)
833 if (strcmp (name, demangler->demangling_style_name) == 0)
834 return demangler->demangling_style;
836 return unknown_demangling;
839 /* char *cplus_demangle (const char *mangled, int options)
841 If MANGLED is a mangled function name produced by GNU C++, then
842 a pointer to a @code{malloc}ed string giving a C++ representation
843 of the name will be returned; otherwise NULL will be returned.
844 It is the caller's responsibility to free the string which
845 is returned.
847 The OPTIONS arg may contain one or more of the following bits:
849 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
850 included.
851 DMGL_PARAMS Function parameters are included.
853 For example,
855 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
856 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
857 cplus_demangle ("foo__1Ai", 0) => "A::foo"
859 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
860 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
861 cplus_demangle ("foo__1Afe", 0) => "A::foo"
863 Note that any leading underscores, or other such characters prepended by
864 the compilation system, are presumed to have already been stripped from
865 MANGLED. */
867 char *
868 cplus_demangle (const char *mangled, int options)
870 char *ret;
871 struct work_stuff work[1];
873 if (current_demangling_style == no_demangling)
874 return xstrdup (mangled);
876 memset ((char *) work, 0, sizeof (work));
877 work->options = options;
878 if ((work->options & DMGL_STYLE_MASK) == 0)
879 work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
881 /* The V3 ABI demangling is implemented elsewhere. */
882 if (GNU_V3_DEMANGLING || RUST_DEMANGLING || AUTO_DEMANGLING)
884 ret = cplus_demangle_v3 (mangled, work->options);
885 if (GNU_V3_DEMANGLING)
886 return ret;
888 if (ret)
890 /* Rust symbols are GNU_V3 mangled plus some extra subtitutions.
891 The subtitutions are always smaller, so do in place changes. */
892 if (rust_is_mangled (ret))
893 rust_demangle_sym (ret);
894 else if (RUST_DEMANGLING)
896 free (ret);
897 ret = NULL;
901 if (ret || RUST_DEMANGLING)
902 return ret;
905 if (JAVA_DEMANGLING)
907 ret = java_demangle_v3 (mangled);
908 if (ret)
909 return ret;
912 if (GNAT_DEMANGLING)
913 return ada_demangle (mangled, options);
915 if (DLANG_DEMANGLING)
917 ret = dlang_demangle (mangled, options);
918 if (ret)
919 return ret;
922 ret = internal_cplus_demangle (work, mangled);
923 squangle_mop_up (work);
924 return (ret);
927 char *
928 rust_demangle (const char *mangled, int options)
930 /* Rust symbols are GNU_V3 mangled plus some extra subtitutions. */
931 char *ret = cplus_demangle_v3 (mangled, options);
933 /* The Rust subtitutions are always smaller, so do in place changes. */
934 if (ret != NULL)
936 if (rust_is_mangled (ret))
937 rust_demangle_sym (ret);
938 else
940 free (ret);
941 ret = NULL;
945 return ret;
948 /* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
950 char *
951 ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
953 int len0;
954 const char* p;
955 char *d;
956 char *demangled = NULL;
958 /* Discard leading _ada_, which is used for library level subprograms. */
959 if (strncmp (mangled, "_ada_", 5) == 0)
960 mangled += 5;
962 /* All ada unit names are lower-case. */
963 if (!ISLOWER (mangled[0]))
964 goto unknown;
966 /* Most of the demangling will trivially remove chars. Operator names
967 may add one char but because they are always preceeded by '__' which is
968 replaced by '.', they eventually never expand the size.
969 A few special names such as '___elabs' add a few chars (at most 7), but
970 they occur only once. */
971 len0 = strlen (mangled) + 7 + 1;
972 demangled = XNEWVEC (char, len0);
974 d = demangled;
975 p = mangled;
976 while (1)
978 /* An entity names is expected. */
979 if (ISLOWER (*p))
981 /* An identifier, which is always lower case. */
983 *d++ = *p++;
984 while (ISLOWER(*p) || ISDIGIT (*p)
985 || (p[0] == '_' && (ISLOWER (p[1]) || ISDIGIT (p[1]))));
987 else if (p[0] == 'O')
989 /* An operator name. */
990 static const char * const operators[][2] =
991 {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
992 {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
993 {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
994 {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
995 {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
996 {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
997 {"Oexpon", "**"}, {NULL, NULL}};
998 int k;
1000 for (k = 0; operators[k][0] != NULL; k++)
1002 size_t slen = strlen (operators[k][0]);
1003 if (strncmp (p, operators[k][0], slen) == 0)
1005 p += slen;
1006 slen = strlen (operators[k][1]);
1007 *d++ = '"';
1008 memcpy (d, operators[k][1], slen);
1009 d += slen;
1010 *d++ = '"';
1011 break;
1014 /* Operator not found. */
1015 if (operators[k][0] == NULL)
1016 goto unknown;
1018 else
1020 /* Not a GNAT encoding. */
1021 goto unknown;
1024 /* The name can be directly followed by some uppercase letters. */
1025 if (p[0] == 'T' && p[1] == 'K')
1027 /* Task stuff. */
1028 if (p[2] == 'B' && p[3] == 0)
1030 /* Subprogram for task body. */
1031 break;
1033 else if (p[2] == '_' && p[3] == '_')
1035 /* Inner declarations in a task. */
1036 p += 4;
1037 *d++ = '.';
1038 continue;
1040 else
1041 goto unknown;
1043 if (p[0] == 'E' && p[1] == 0)
1045 /* Exception name. */
1046 goto unknown;
1048 if ((p[0] == 'P' || p[0] == 'N') && p[1] == 0)
1050 /* Protected type subprogram. */
1051 break;
1053 if ((*p == 'N' || *p == 'S') && p[1] == 0)
1055 /* Enumerated type name table. */
1056 goto unknown;
1058 if (p[0] == 'X')
1060 /* Body nested. */
1061 p++;
1062 while (p[0] == 'n' || p[0] == 'b')
1063 p++;
1065 if (p[0] == 'S' && p[1] != 0 && (p[2] == '_' || p[2] == 0))
1067 /* Stream operations. */
1068 const char *name;
1069 switch (p[1])
1071 case 'R':
1072 name = "'Read";
1073 break;
1074 case 'W':
1075 name = "'Write";
1076 break;
1077 case 'I':
1078 name = "'Input";
1079 break;
1080 case 'O':
1081 name = "'Output";
1082 break;
1083 default:
1084 goto unknown;
1086 p += 2;
1087 strcpy (d, name);
1088 d += strlen (name);
1090 else if (p[0] == 'D')
1092 /* Controlled type operation. */
1093 const char *name;
1094 switch (p[1])
1096 case 'F':
1097 name = ".Finalize";
1098 break;
1099 case 'A':
1100 name = ".Adjust";
1101 break;
1102 default:
1103 goto unknown;
1105 strcpy (d, name);
1106 d += strlen (name);
1107 break;
1110 if (p[0] == '_')
1112 /* Separator. */
1113 if (p[1] == '_')
1115 /* Standard separator. Handled first. */
1116 p += 2;
1118 if (ISDIGIT (*p))
1120 /* Overloading number. */
1122 p++;
1123 while (ISDIGIT (*p) || (p[0] == '_' && ISDIGIT (p[1])));
1124 if (*p == 'X')
1126 p++;
1127 while (p[0] == 'n' || p[0] == 'b')
1128 p++;
1131 else if (p[0] == '_' && p[1] != '_')
1133 /* Special names. */
1134 static const char * const special[][2] = {
1135 { "_elabb", "'Elab_Body" },
1136 { "_elabs", "'Elab_Spec" },
1137 { "_size", "'Size" },
1138 { "_alignment", "'Alignment" },
1139 { "_assign", ".\":=\"" },
1140 { NULL, NULL }
1142 int k;
1144 for (k = 0; special[k][0] != NULL; k++)
1146 size_t slen = strlen (special[k][0]);
1147 if (strncmp (p, special[k][0], slen) == 0)
1149 p += slen;
1150 slen = strlen (special[k][1]);
1151 memcpy (d, special[k][1], slen);
1152 d += slen;
1153 break;
1156 if (special[k][0] != NULL)
1157 break;
1158 else
1159 goto unknown;
1161 else
1163 *d++ = '.';
1164 continue;
1167 else if (p[1] == 'B' || p[1] == 'E')
1169 /* Entry Body or barrier Evaluation. */
1170 p += 2;
1171 while (ISDIGIT (*p))
1172 p++;
1173 if (p[0] == 's' && p[1] == 0)
1174 break;
1175 else
1176 goto unknown;
1178 else
1179 goto unknown;
1182 if (p[0] == '.' && ISDIGIT (p[1]))
1184 /* Nested subprogram. */
1185 p += 2;
1186 while (ISDIGIT (*p))
1187 p++;
1189 if (*p == 0)
1191 /* End of mangled name. */
1192 break;
1194 else
1195 goto unknown;
1197 *d = 0;
1198 return demangled;
1200 unknown:
1201 XDELETEVEC (demangled);
1202 len0 = strlen (mangled);
1203 demangled = XNEWVEC (char, len0 + 3);
1205 if (mangled[0] == '<')
1206 strcpy (demangled, mangled);
1207 else
1208 sprintf (demangled, "<%s>", mangled);
1210 return demangled;
1213 /* This function performs most of what cplus_demangle use to do, but
1214 to be able to demangle a name with a B, K or n code, we need to
1215 have a longer term memory of what types have been seen. The original
1216 now initializes and cleans up the squangle code info, while internal
1217 calls go directly to this routine to avoid resetting that info. */
1219 static char *
1220 internal_cplus_demangle (struct work_stuff *work, const char *mangled)
1223 string decl;
1224 int success = 0;
1225 char *demangled = NULL;
1226 int s1, s2, s3, s4;
1227 s1 = work->constructor;
1228 s2 = work->destructor;
1229 s3 = work->static_type;
1230 s4 = work->type_quals;
1231 work->constructor = work->destructor = 0;
1232 work->type_quals = TYPE_UNQUALIFIED;
1233 work->dllimported = 0;
1235 if ((mangled != NULL) && (*mangled != '\0'))
1237 string_init (&decl);
1239 /* First check to see if gnu style demangling is active and if the
1240 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1241 recognize one of the gnu special forms rather than looking for a
1242 standard prefix. In particular, don't worry about whether there
1243 is a "__" string in the mangled string. Consider "_$_5__foo" for
1244 example. */
1246 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
1248 success = gnu_special (work, &mangled, &decl);
1249 if (!success)
1251 delete_work_stuff (work);
1252 string_delete (&decl);
1255 if (!success)
1257 success = demangle_prefix (work, &mangled, &decl);
1259 if (success && (*mangled != '\0'))
1261 success = demangle_signature (work, &mangled, &decl);
1263 if (work->constructor == 2)
1265 string_prepend (&decl, "global constructors keyed to ");
1266 work->constructor = 0;
1268 else if (work->destructor == 2)
1270 string_prepend (&decl, "global destructors keyed to ");
1271 work->destructor = 0;
1273 else if (work->dllimported == 1)
1275 string_prepend (&decl, "import stub for ");
1276 work->dllimported = 0;
1278 demangled = mop_up (work, &decl, success);
1280 work->constructor = s1;
1281 work->destructor = s2;
1282 work->static_type = s3;
1283 work->type_quals = s4;
1284 return demangled;
1288 /* Clear out and squangling related storage */
1289 static void
1290 squangle_mop_up (struct work_stuff *work)
1292 /* clean up the B and K type mangling types. */
1293 forget_B_and_K_types (work);
1294 if (work -> btypevec != NULL)
1296 free ((char *) work -> btypevec);
1297 work->btypevec = NULL;
1298 work->bsize = 0;
1300 if (work -> ktypevec != NULL)
1302 free ((char *) work -> ktypevec);
1303 work->ktypevec = NULL;
1304 work->ksize = 0;
1309 /* Copy the work state and storage. */
1311 static void
1312 work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from)
1314 int i;
1316 delete_work_stuff (to);
1318 /* Shallow-copy scalars. */
1319 memcpy (to, from, sizeof (*to));
1321 /* Deep-copy dynamic storage. */
1322 if (from->typevec_size)
1323 to->typevec = XNEWVEC (char *, from->typevec_size);
1325 for (i = 0; i < from->ntypes; i++)
1327 int len = strlen (from->typevec[i]) + 1;
1329 to->typevec[i] = XNEWVEC (char, len);
1330 memcpy (to->typevec[i], from->typevec[i], len);
1333 if (from->ksize)
1334 to->ktypevec = XNEWVEC (char *, from->ksize);
1336 for (i = 0; i < from->numk; i++)
1338 int len = strlen (from->ktypevec[i]) + 1;
1340 to->ktypevec[i] = XNEWVEC (char, len);
1341 memcpy (to->ktypevec[i], from->ktypevec[i], len);
1344 if (from->bsize)
1345 to->btypevec = XNEWVEC (char *, from->bsize);
1347 for (i = 0; i < from->numb; i++)
1349 int len = strlen (from->btypevec[i]) + 1;
1351 to->btypevec[i] = XNEWVEC (char , len);
1352 memcpy (to->btypevec[i], from->btypevec[i], len);
1355 if (from->proctypevec)
1356 to->proctypevec =
1357 XDUPVEC (int, from->proctypevec, from->proctypevec_size);
1359 if (from->ntmpl_args)
1360 to->tmpl_argvec = XNEWVEC (char *, from->ntmpl_args);
1362 for (i = 0; i < from->ntmpl_args; i++)
1364 int len = strlen (from->tmpl_argvec[i]) + 1;
1366 to->tmpl_argvec[i] = XNEWVEC (char, len);
1367 memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
1370 if (from->previous_argument)
1372 to->previous_argument = XNEW (string);
1373 string_init (to->previous_argument);
1374 string_appends (to->previous_argument, from->previous_argument);
1379 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1381 static void
1382 delete_non_B_K_work_stuff (struct work_stuff *work)
1384 /* Discard the remembered types, if any. */
1386 forget_types (work);
1387 if (work->typevec != NULL)
1389 free ((char *) work->typevec);
1390 work->typevec = NULL;
1391 work->typevec_size = 0;
1393 if (work->proctypevec != NULL)
1395 free (work->proctypevec);
1396 work->proctypevec = NULL;
1397 work->proctypevec_size = 0;
1399 if (work->tmpl_argvec)
1401 int i;
1403 for (i = 0; i < work->ntmpl_args; i++)
1404 free ((char*) work->tmpl_argvec[i]);
1406 free ((char*) work->tmpl_argvec);
1407 work->tmpl_argvec = NULL;
1409 if (work->previous_argument)
1411 string_delete (work->previous_argument);
1412 free ((char*) work->previous_argument);
1413 work->previous_argument = NULL;
1418 /* Delete all dynamic storage in work_stuff. */
1419 static void
1420 delete_work_stuff (struct work_stuff *work)
1422 delete_non_B_K_work_stuff (work);
1423 squangle_mop_up (work);
1427 /* Clear out any mangled storage */
1429 static char *
1430 mop_up (struct work_stuff *work, string *declp, int success)
1432 char *demangled = NULL;
1434 delete_non_B_K_work_stuff (work);
1436 /* If demangling was successful, ensure that the demangled string is null
1437 terminated and return it. Otherwise, free the demangling decl. */
1439 if (!success)
1441 string_delete (declp);
1443 else
1445 string_appendn (declp, "", 1);
1446 demangled = declp->b;
1448 return (demangled);
1453 LOCAL FUNCTION
1455 demangle_signature -- demangle the signature part of a mangled name
1457 SYNOPSIS
1459 static int
1460 demangle_signature (struct work_stuff *work, const char **mangled,
1461 string *declp);
1463 DESCRIPTION
1465 Consume and demangle the signature portion of the mangled name.
1467 DECLP is the string where demangled output is being built. At
1468 entry it contains the demangled root name from the mangled name
1469 prefix. I.E. either a demangled operator name or the root function
1470 name. In some special cases, it may contain nothing.
1472 *MANGLED points to the current unconsumed location in the mangled
1473 name. As tokens are consumed and demangling is performed, the
1474 pointer is updated to continuously point at the next token to
1475 be consumed.
1477 Demangling GNU style mangled names is nasty because there is no
1478 explicit token that marks the start of the outermost function
1479 argument list. */
1481 static int
1482 demangle_signature (struct work_stuff *work,
1483 const char **mangled, string *declp)
1485 int success = 1;
1486 int func_done = 0;
1487 int expect_func = 0;
1488 int expect_return_type = 0;
1489 const char *oldmangled = NULL;
1490 string trawname;
1491 string tname;
1493 while (success && (**mangled != '\0'))
1495 switch (**mangled)
1497 case 'Q':
1498 oldmangled = *mangled;
1499 success = demangle_qualified (work, mangled, declp, 1, 0);
1500 if (success)
1501 remember_type (work, oldmangled, *mangled - oldmangled);
1502 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1503 expect_func = 1;
1504 oldmangled = NULL;
1505 break;
1507 case 'K':
1508 oldmangled = *mangled;
1509 success = demangle_qualified (work, mangled, declp, 1, 0);
1510 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1512 expect_func = 1;
1514 oldmangled = NULL;
1515 break;
1517 case 'S':
1518 /* Static member function */
1519 if (oldmangled == NULL)
1521 oldmangled = *mangled;
1523 (*mangled)++;
1524 work -> static_type = 1;
1525 break;
1527 case 'C':
1528 case 'V':
1529 case 'u':
1530 work->type_quals |= code_for_qualifier (**mangled);
1532 /* a qualified member function */
1533 if (oldmangled == NULL)
1534 oldmangled = *mangled;
1535 (*mangled)++;
1536 break;
1538 case 'L':
1539 /* Local class name follows after "Lnnn_" */
1540 if (HP_DEMANGLING)
1542 while (**mangled && (**mangled != '_'))
1543 (*mangled)++;
1544 if (!**mangled)
1545 success = 0;
1546 else
1547 (*mangled)++;
1549 else
1550 success = 0;
1551 break;
1553 case '0': case '1': case '2': case '3': case '4':
1554 case '5': case '6': case '7': case '8': case '9':
1555 if (oldmangled == NULL)
1557 oldmangled = *mangled;
1559 work->temp_start = -1; /* uppermost call to demangle_class */
1560 success = demangle_class (work, mangled, declp);
1561 if (success)
1563 remember_type (work, oldmangled, *mangled - oldmangled);
1565 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1567 /* EDG and others will have the "F", so we let the loop cycle
1568 if we are looking at one. */
1569 if (**mangled != 'F')
1570 expect_func = 1;
1572 oldmangled = NULL;
1573 break;
1575 case 'B':
1577 string s;
1578 success = do_type (work, mangled, &s);
1579 if (success)
1581 string_append (&s, SCOPE_STRING (work));
1582 string_prepends (declp, &s);
1583 string_delete (&s);
1585 oldmangled = NULL;
1586 expect_func = 1;
1588 break;
1590 case 'F':
1591 /* Function */
1592 /* ARM/HP style demangling includes a specific 'F' character after
1593 the class name. For GNU style, it is just implied. So we can
1594 safely just consume any 'F' at this point and be compatible
1595 with either style. */
1597 oldmangled = NULL;
1598 func_done = 1;
1599 (*mangled)++;
1601 /* For lucid/ARM/HP style we have to forget any types we might
1602 have remembered up to this point, since they were not argument
1603 types. GNU style considers all types seen as available for
1604 back references. See comment in demangle_args() */
1606 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1608 forget_types (work);
1610 success = demangle_args (work, mangled, declp);
1611 /* After picking off the function args, we expect to either
1612 find the function return type (preceded by an '_') or the
1613 end of the string. */
1614 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1616 ++(*mangled);
1617 /* At this level, we do not care about the return type. */
1618 success = do_type (work, mangled, &tname);
1619 string_delete (&tname);
1622 break;
1624 case 't':
1625 /* G++ Template */
1626 string_init(&trawname);
1627 string_init(&tname);
1628 if (oldmangled == NULL)
1630 oldmangled = *mangled;
1632 success = demangle_template (work, mangled, &tname,
1633 &trawname, 1, 1);
1634 if (success)
1636 remember_type (work, oldmangled, *mangled - oldmangled);
1638 string_append (&tname, SCOPE_STRING (work));
1640 string_prepends(declp, &tname);
1641 if (work -> destructor & 1)
1643 string_prepend (&trawname, "~");
1644 string_appends (declp, &trawname);
1645 work->destructor -= 1;
1647 if ((work->constructor & 1) || (work->destructor & 1))
1649 string_appends (declp, &trawname);
1650 work->constructor -= 1;
1652 string_delete(&trawname);
1653 string_delete(&tname);
1654 oldmangled = NULL;
1655 expect_func = 1;
1656 break;
1658 case '_':
1659 if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
1661 /* Read the return type. */
1662 string return_type;
1664 (*mangled)++;
1665 success = do_type (work, mangled, &return_type);
1666 APPEND_BLANK (&return_type);
1668 string_prepends (declp, &return_type);
1669 string_delete (&return_type);
1670 break;
1672 else
1673 /* At the outermost level, we cannot have a return type specified,
1674 so if we run into another '_' at this point we are dealing with
1675 a mangled name that is either bogus, or has been mangled by
1676 some algorithm we don't know how to deal with. So just
1677 reject the entire demangling. */
1678 /* However, "_nnn" is an expected suffix for alternate entry point
1679 numbered nnn for a function, with HP aCC, so skip over that
1680 without reporting failure. pai/1997-09-04 */
1681 if (HP_DEMANGLING)
1683 (*mangled)++;
1684 while (**mangled && ISDIGIT ((unsigned char)**mangled))
1685 (*mangled)++;
1687 else
1688 success = 0;
1689 break;
1691 case 'H':
1692 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1694 /* A G++ template function. Read the template arguments. */
1695 success = demangle_template (work, mangled, declp, 0, 0,
1697 if (!(work->constructor & 1))
1698 expect_return_type = 1;
1699 if (!**mangled)
1700 success = 0;
1701 else
1702 (*mangled)++;
1703 break;
1705 /* fall through */
1707 default:
1708 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1710 /* Assume we have stumbled onto the first outermost function
1711 argument token, and start processing args. */
1712 func_done = 1;
1713 success = demangle_args (work, mangled, declp);
1715 else
1717 /* Non-GNU demanglers use a specific token to mark the start
1718 of the outermost function argument tokens. Typically 'F',
1719 for ARM/HP-demangling, for example. So if we find something
1720 we are not prepared for, it must be an error. */
1721 success = 0;
1723 break;
1726 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1729 if (success && expect_func)
1731 func_done = 1;
1732 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1734 forget_types (work);
1736 success = demangle_args (work, mangled, declp);
1737 /* Since template include the mangling of their return types,
1738 we must set expect_func to 0 so that we don't try do
1739 demangle more arguments the next time we get here. */
1740 expect_func = 0;
1744 if (success && !func_done)
1746 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1748 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1749 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1750 first case, and need to ensure that the '(void)' gets added to
1751 the current declp. Note that with ARM/HP, the first case
1752 represents the name of a static data member 'foo::bar',
1753 which is in the current declp, so we leave it alone. */
1754 success = demangle_args (work, mangled, declp);
1757 if (success && PRINT_ARG_TYPES)
1759 if (work->static_type)
1760 string_append (declp, " static");
1761 if (work->type_quals != TYPE_UNQUALIFIED)
1763 APPEND_BLANK (declp);
1764 string_append (declp, qualifier_string (work->type_quals));
1768 return (success);
1771 #if 0
1773 static int
1774 demangle_method_args (struct work_stuff *work, const char **mangled,
1775 string *declp)
1777 int success = 0;
1779 if (work -> static_type)
1781 string_append (declp, *mangled + 1);
1782 *mangled += strlen (*mangled);
1783 success = 1;
1785 else
1787 success = demangle_args (work, mangled, declp);
1789 return (success);
1792 #endif
1794 static int
1795 demangle_template_template_parm (struct work_stuff *work,
1796 const char **mangled, string *tname)
1798 int i;
1799 int r;
1800 int need_comma = 0;
1801 int success = 1;
1802 string temp;
1804 string_append (tname, "template <");
1805 /* get size of template parameter list */
1806 if (get_count (mangled, &r))
1808 for (i = 0; i < r; i++)
1810 if (need_comma)
1812 string_append (tname, ", ");
1815 /* Z for type parameters */
1816 if (**mangled == 'Z')
1818 (*mangled)++;
1819 string_append (tname, "class");
1821 /* z for template parameters */
1822 else if (**mangled == 'z')
1824 (*mangled)++;
1825 success =
1826 demangle_template_template_parm (work, mangled, tname);
1827 if (!success)
1829 break;
1832 else
1834 /* temp is initialized in do_type */
1835 success = do_type (work, mangled, &temp);
1836 if (success)
1838 string_appends (tname, &temp);
1840 string_delete(&temp);
1841 if (!success)
1843 break;
1846 need_comma = 1;
1850 if (tname->p[-1] == '>')
1851 string_append (tname, " ");
1852 string_append (tname, "> class");
1853 return (success);
1856 static int
1857 demangle_expression (struct work_stuff *work, const char **mangled,
1858 string *s, type_kind_t tk)
1860 int need_operator = 0;
1861 int success;
1863 success = 1;
1864 string_appendn (s, "(", 1);
1865 (*mangled)++;
1866 while (success && **mangled != 'W' && **mangled != '\0')
1868 if (need_operator)
1870 size_t i;
1871 size_t len;
1873 success = 0;
1875 len = strlen (*mangled);
1877 for (i = 0; i < ARRAY_SIZE (optable); ++i)
1879 size_t l = strlen (optable[i].in);
1881 if (l <= len
1882 && memcmp (optable[i].in, *mangled, l) == 0)
1884 string_appendn (s, " ", 1);
1885 string_append (s, optable[i].out);
1886 string_appendn (s, " ", 1);
1887 success = 1;
1888 (*mangled) += l;
1889 break;
1893 if (!success)
1894 break;
1896 else
1897 need_operator = 1;
1899 success = demangle_template_value_parm (work, mangled, s, tk);
1902 if (**mangled != 'W')
1903 success = 0;
1904 else
1906 string_appendn (s, ")", 1);
1907 (*mangled)++;
1910 return success;
1913 static int
1914 demangle_integral_value (struct work_stuff *work,
1915 const char **mangled, string *s)
1917 int success;
1919 if (**mangled == 'E')
1920 success = demangle_expression (work, mangled, s, tk_integral);
1921 else if (**mangled == 'Q' || **mangled == 'K')
1922 success = demangle_qualified (work, mangled, s, 0, 1);
1923 else
1925 int value;
1927 /* By default, we let the number decide whether we shall consume an
1928 underscore. */
1929 int multidigit_without_leading_underscore = 0;
1930 int leave_following_underscore = 0;
1932 success = 0;
1934 if (**mangled == '_')
1936 if (mangled[0][1] == 'm')
1938 /* Since consume_count_with_underscores does not handle the
1939 `m'-prefix we must do it here, using consume_count and
1940 adjusting underscores: we have to consume the underscore
1941 matching the prepended one. */
1942 multidigit_without_leading_underscore = 1;
1943 string_appendn (s, "-", 1);
1944 (*mangled) += 2;
1946 else
1948 /* Do not consume a following underscore;
1949 consume_count_with_underscores will consume what
1950 should be consumed. */
1951 leave_following_underscore = 1;
1954 else
1956 /* Negative numbers are indicated with a leading `m'. */
1957 if (**mangled == 'm')
1959 string_appendn (s, "-", 1);
1960 (*mangled)++;
1962 /* Since consume_count_with_underscores does not handle
1963 multi-digit numbers that do not start with an underscore,
1964 and this number can be an integer template parameter,
1965 we have to call consume_count. */
1966 multidigit_without_leading_underscore = 1;
1967 /* These multi-digit numbers never end on an underscore,
1968 so if there is one then don't eat it. */
1969 leave_following_underscore = 1;
1972 /* We must call consume_count if we expect to remove a trailing
1973 underscore, since consume_count_with_underscores expects
1974 the leading underscore (that we consumed) if it is to handle
1975 multi-digit numbers. */
1976 if (multidigit_without_leading_underscore)
1977 value = consume_count (mangled);
1978 else
1979 value = consume_count_with_underscores (mangled);
1981 if (value != -1)
1983 char buf[INTBUF_SIZE];
1984 sprintf (buf, "%d", value);
1985 string_append (s, buf);
1987 /* Numbers not otherwise delimited, might have an underscore
1988 appended as a delimeter, which we should skip.
1990 ??? This used to always remove a following underscore, which
1991 is wrong. If other (arbitrary) cases are followed by an
1992 underscore, we need to do something more radical. */
1994 if ((value > 9 || multidigit_without_leading_underscore)
1995 && ! leave_following_underscore
1996 && **mangled == '_')
1997 (*mangled)++;
1999 /* All is well. */
2000 success = 1;
2004 return success;
2007 /* Demangle the real value in MANGLED. */
2009 static int
2010 demangle_real_value (struct work_stuff *work,
2011 const char **mangled, string *s)
2013 if (**mangled == 'E')
2014 return demangle_expression (work, mangled, s, tk_real);
2016 if (**mangled == 'm')
2018 string_appendn (s, "-", 1);
2019 (*mangled)++;
2021 while (ISDIGIT ((unsigned char)**mangled))
2023 string_appendn (s, *mangled, 1);
2024 (*mangled)++;
2026 if (**mangled == '.') /* fraction */
2028 string_appendn (s, ".", 1);
2029 (*mangled)++;
2030 while (ISDIGIT ((unsigned char)**mangled))
2032 string_appendn (s, *mangled, 1);
2033 (*mangled)++;
2036 if (**mangled == 'e') /* exponent */
2038 string_appendn (s, "e", 1);
2039 (*mangled)++;
2040 while (ISDIGIT ((unsigned char)**mangled))
2042 string_appendn (s, *mangled, 1);
2043 (*mangled)++;
2047 return 1;
2050 static int
2051 demangle_template_value_parm (struct work_stuff *work, const char **mangled,
2052 string *s, type_kind_t tk)
2054 int success = 1;
2056 if (**mangled == 'Y')
2058 /* The next argument is a template parameter. */
2059 int idx;
2061 (*mangled)++;
2062 idx = consume_count_with_underscores (mangled);
2063 if (idx == -1
2064 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2065 || consume_count_with_underscores (mangled) == -1)
2066 return -1;
2067 if (work->tmpl_argvec)
2068 string_append (s, work->tmpl_argvec[idx]);
2069 else
2070 string_append_template_idx (s, idx);
2072 else if (tk == tk_integral)
2073 success = demangle_integral_value (work, mangled, s);
2074 else if (tk == tk_char)
2076 char tmp[2];
2077 int val;
2078 if (**mangled == 'm')
2080 string_appendn (s, "-", 1);
2081 (*mangled)++;
2083 string_appendn (s, "'", 1);
2084 val = consume_count(mangled);
2085 if (val <= 0)
2086 success = 0;
2087 else
2089 tmp[0] = (char)val;
2090 tmp[1] = '\0';
2091 string_appendn (s, &tmp[0], 1);
2092 string_appendn (s, "'", 1);
2095 else if (tk == tk_bool)
2097 int val = consume_count (mangled);
2098 if (val == 0)
2099 string_appendn (s, "false", 5);
2100 else if (val == 1)
2101 string_appendn (s, "true", 4);
2102 else
2103 success = 0;
2105 else if (tk == tk_real)
2106 success = demangle_real_value (work, mangled, s);
2107 else if (tk == tk_pointer || tk == tk_reference
2108 || tk == tk_rvalue_reference)
2110 if (**mangled == 'Q')
2111 success = demangle_qualified (work, mangled, s,
2112 /*isfuncname=*/0,
2113 /*append=*/1);
2114 else
2116 int symbol_len = consume_count (mangled);
2117 if (symbol_len == -1
2118 || symbol_len > (long) strlen (*mangled))
2119 return -1;
2120 if (symbol_len == 0)
2121 string_appendn (s, "0", 1);
2122 else
2124 char *p = XNEWVEC (char, symbol_len + 1), *q;
2125 strncpy (p, *mangled, symbol_len);
2126 p [symbol_len] = '\0';
2127 /* We use cplus_demangle here, rather than
2128 internal_cplus_demangle, because the name of the entity
2129 mangled here does not make use of any of the squangling
2130 or type-code information we have built up thus far; it is
2131 mangled independently. */
2132 q = cplus_demangle (p, work->options);
2133 if (tk == tk_pointer)
2134 string_appendn (s, "&", 1);
2135 /* FIXME: Pointer-to-member constants should get a
2136 qualifying class name here. */
2137 if (q)
2139 string_append (s, q);
2140 free (q);
2142 else
2143 string_append (s, p);
2144 free (p);
2146 *mangled += symbol_len;
2150 return success;
2153 /* Demangle the template name in MANGLED. The full name of the
2154 template (e.g., S<int>) is placed in TNAME. The name without the
2155 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2156 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2157 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2158 the template is remembered in the list of back-referenceable
2159 types. */
2161 static int
2162 demangle_template (struct work_stuff *work, const char **mangled,
2163 string *tname, string *trawname,
2164 int is_type, int remember)
2166 int i;
2167 int r;
2168 int need_comma = 0;
2169 int success = 0;
2170 int is_java_array = 0;
2171 string temp;
2173 (*mangled)++;
2174 if (is_type)
2176 /* get template name */
2177 if (**mangled == 'z')
2179 int idx;
2180 (*mangled)++;
2181 if (**mangled == '\0')
2182 return (0);
2183 (*mangled)++;
2185 idx = consume_count_with_underscores (mangled);
2186 if (idx == -1
2187 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2188 || consume_count_with_underscores (mangled) == -1)
2189 return (0);
2191 if (work->tmpl_argvec)
2193 string_append (tname, work->tmpl_argvec[idx]);
2194 if (trawname)
2195 string_append (trawname, work->tmpl_argvec[idx]);
2197 else
2199 string_append_template_idx (tname, idx);
2200 if (trawname)
2201 string_append_template_idx (trawname, idx);
2204 else
2206 if ((r = consume_count (mangled)) <= 0
2207 || (int) strlen (*mangled) < r)
2209 return (0);
2211 is_java_array = (work -> options & DMGL_JAVA)
2212 && strncmp (*mangled, "JArray1Z", 8) == 0;
2213 if (! is_java_array)
2215 string_appendn (tname, *mangled, r);
2217 if (trawname)
2218 string_appendn (trawname, *mangled, r);
2219 *mangled += r;
2222 if (!is_java_array)
2223 string_append (tname, "<");
2224 /* get size of template parameter list */
2225 if (!get_count (mangled, &r))
2227 return (0);
2229 if (!is_type)
2231 /* Create an array for saving the template argument values. */
2232 work->tmpl_argvec = XNEWVEC (char *, r);
2233 work->ntmpl_args = r;
2234 for (i = 0; i < r; i++)
2235 work->tmpl_argvec[i] = 0;
2237 for (i = 0; i < r; i++)
2239 if (need_comma)
2241 string_append (tname, ", ");
2243 /* Z for type parameters */
2244 if (**mangled == 'Z')
2246 (*mangled)++;
2247 /* temp is initialized in do_type */
2248 success = do_type (work, mangled, &temp);
2249 if (success)
2251 string_appends (tname, &temp);
2253 if (!is_type)
2255 /* Save the template argument. */
2256 int len = temp.p - temp.b;
2257 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
2258 memcpy (work->tmpl_argvec[i], temp.b, len);
2259 work->tmpl_argvec[i][len] = '\0';
2262 string_delete(&temp);
2263 if (!success)
2265 break;
2268 /* z for template parameters */
2269 else if (**mangled == 'z')
2271 int r2;
2272 (*mangled)++;
2273 success = demangle_template_template_parm (work, mangled, tname);
2275 if (success
2276 && (r2 = consume_count (mangled)) > 0
2277 && (int) strlen (*mangled) >= r2)
2279 string_append (tname, " ");
2280 string_appendn (tname, *mangled, r2);
2281 if (!is_type)
2283 /* Save the template argument. */
2284 int len = r2;
2285 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
2286 memcpy (work->tmpl_argvec[i], *mangled, len);
2287 work->tmpl_argvec[i][len] = '\0';
2289 *mangled += r2;
2291 if (!success)
2293 break;
2296 else
2298 string param;
2299 string* s;
2301 /* otherwise, value parameter */
2303 /* temp is initialized in do_type */
2304 success = do_type (work, mangled, &temp);
2305 string_delete(&temp);
2306 if (!success)
2307 break;
2309 if (!is_type)
2311 s = &param;
2312 string_init (s);
2314 else
2315 s = tname;
2317 success = demangle_template_value_parm (work, mangled, s,
2318 (type_kind_t) success);
2320 if (!success)
2322 if (!is_type)
2323 string_delete (s);
2324 success = 0;
2325 break;
2328 if (!is_type)
2330 int len = s->p - s->b;
2331 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
2332 memcpy (work->tmpl_argvec[i], s->b, len);
2333 work->tmpl_argvec[i][len] = '\0';
2335 string_appends (tname, s);
2336 string_delete (s);
2339 need_comma = 1;
2341 if (is_java_array)
2343 string_append (tname, "[]");
2345 else
2347 if (tname->p[-1] == '>')
2348 string_append (tname, " ");
2349 string_append (tname, ">");
2352 if (is_type && remember)
2354 const int bindex = register_Btype (work);
2355 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
2359 if (work -> static_type)
2361 string_append (declp, *mangled + 1);
2362 *mangled += strlen (*mangled);
2363 success = 1;
2365 else
2367 success = demangle_args (work, mangled, declp);
2371 return (success);
2374 static int
2375 arm_pt (struct work_stuff *work, const char *mangled,
2376 int n, const char **anchor, const char **args)
2378 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2379 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2380 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
2382 int len;
2383 *args = *anchor + 6;
2384 len = consume_count (args);
2385 if (len == -1)
2386 return 0;
2387 if (*args + len == mangled + n && **args == '_')
2389 ++*args;
2390 return 1;
2393 if (AUTO_DEMANGLING || EDG_DEMANGLING)
2395 if ((*anchor = strstr (mangled, "__tm__"))
2396 || (*anchor = strstr (mangled, "__ps__"))
2397 || (*anchor = strstr (mangled, "__pt__")))
2399 int len;
2400 *args = *anchor + 6;
2401 len = consume_count (args);
2402 if (len == -1)
2403 return 0;
2404 if (*args + len == mangled + n && **args == '_')
2406 ++*args;
2407 return 1;
2410 else if ((*anchor = strstr (mangled, "__S")))
2412 int len;
2413 *args = *anchor + 3;
2414 len = consume_count (args);
2415 if (len == -1)
2416 return 0;
2417 if (*args + len == mangled + n && **args == '_')
2419 ++*args;
2420 return 1;
2425 return 0;
2428 static void
2429 demangle_arm_hp_template (struct work_stuff *work, const char **mangled,
2430 int n, string *declp)
2432 const char *p;
2433 const char *args;
2434 const char *e = *mangled + n;
2435 string arg;
2437 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2438 template args */
2439 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
2441 char *start_spec_args = NULL;
2442 int hold_options;
2444 /* First check for and omit template specialization pseudo-arguments,
2445 such as in "Spec<#1,#1.*>" */
2446 start_spec_args = strchr (*mangled, '<');
2447 if (start_spec_args && (start_spec_args - *mangled < n))
2448 string_appendn (declp, *mangled, start_spec_args - *mangled);
2449 else
2450 string_appendn (declp, *mangled, n);
2451 (*mangled) += n + 1;
2452 string_init (&arg);
2453 if (work->temp_start == -1) /* non-recursive call */
2454 work->temp_start = declp->p - declp->b;
2456 /* We want to unconditionally demangle parameter types in
2457 template parameters. */
2458 hold_options = work->options;
2459 work->options |= DMGL_PARAMS;
2461 string_append (declp, "<");
2462 while (1)
2464 string_delete (&arg);
2465 switch (**mangled)
2467 case 'T':
2468 /* 'T' signals a type parameter */
2469 (*mangled)++;
2470 if (!do_type (work, mangled, &arg))
2471 goto hpacc_template_args_done;
2472 break;
2474 case 'U':
2475 case 'S':
2476 /* 'U' or 'S' signals an integral value */
2477 if (!do_hpacc_template_const_value (work, mangled, &arg))
2478 goto hpacc_template_args_done;
2479 break;
2481 case 'A':
2482 /* 'A' signals a named constant expression (literal) */
2483 if (!do_hpacc_template_literal (work, mangled, &arg))
2484 goto hpacc_template_args_done;
2485 break;
2487 default:
2488 /* Today, 1997-09-03, we have only the above types
2489 of template parameters */
2490 /* FIXME: maybe this should fail and return null */
2491 goto hpacc_template_args_done;
2493 string_appends (declp, &arg);
2494 /* Check if we're at the end of template args.
2495 0 if at end of static member of template class,
2496 _ if done with template args for a function */
2497 if ((**mangled == '\000') || (**mangled == '_'))
2498 break;
2499 else
2500 string_append (declp, ",");
2502 hpacc_template_args_done:
2503 string_append (declp, ">");
2504 string_delete (&arg);
2505 if (**mangled == '_')
2506 (*mangled)++;
2507 work->options = hold_options;
2508 return;
2510 /* ARM template? (Also handles HP cfront extensions) */
2511 else if (arm_pt (work, *mangled, n, &p, &args))
2513 int hold_options;
2514 string type_str;
2516 string_init (&arg);
2517 string_appendn (declp, *mangled, p - *mangled);
2518 if (work->temp_start == -1) /* non-recursive call */
2519 work->temp_start = declp->p - declp->b;
2521 /* We want to unconditionally demangle parameter types in
2522 template parameters. */
2523 hold_options = work->options;
2524 work->options |= DMGL_PARAMS;
2526 string_append (declp, "<");
2527 /* should do error checking here */
2528 while (args < e) {
2529 string_delete (&arg);
2531 /* Check for type or literal here */
2532 switch (*args)
2534 /* HP cfront extensions to ARM for template args */
2535 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2536 /* FIXME: We handle only numeric literals for HP cfront */
2537 case 'X':
2538 /* A typed constant value follows */
2539 args++;
2540 if (!do_type (work, &args, &type_str))
2541 goto cfront_template_args_done;
2542 string_append (&arg, "(");
2543 string_appends (&arg, &type_str);
2544 string_delete (&type_str);
2545 string_append (&arg, ")");
2546 if (*args != 'L')
2547 goto cfront_template_args_done;
2548 args++;
2549 /* Now snarf a literal value following 'L' */
2550 if (!snarf_numeric_literal (&args, &arg))
2551 goto cfront_template_args_done;
2552 break;
2554 case 'L':
2555 /* Snarf a literal following 'L' */
2556 args++;
2557 if (!snarf_numeric_literal (&args, &arg))
2558 goto cfront_template_args_done;
2559 break;
2560 default:
2561 /* Not handling other HP cfront stuff */
2563 const char* old_args = args;
2564 if (!do_type (work, &args, &arg))
2565 goto cfront_template_args_done;
2567 /* Fail if we didn't make any progress: prevent infinite loop. */
2568 if (args == old_args)
2570 work->options = hold_options;
2571 return;
2575 string_appends (declp, &arg);
2576 string_append (declp, ",");
2578 cfront_template_args_done:
2579 string_delete (&arg);
2580 if (args >= e)
2581 --declp->p; /* remove extra comma */
2582 string_append (declp, ">");
2583 work->options = hold_options;
2585 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2586 && (*mangled)[9] == 'N'
2587 && (*mangled)[8] == (*mangled)[10]
2588 && strchr (cplus_markers, (*mangled)[8]))
2590 /* A member of the anonymous namespace. */
2591 string_append (declp, "{anonymous}");
2593 else
2595 if (work->temp_start == -1) /* non-recursive call only */
2596 work->temp_start = 0; /* disable in recursive calls */
2597 string_appendn (declp, *mangled, n);
2599 *mangled += n;
2602 /* Extract a class name, possibly a template with arguments, from the
2603 mangled string; qualifiers, local class indicators, etc. have
2604 already been dealt with */
2606 static int
2607 demangle_class_name (struct work_stuff *work, const char **mangled,
2608 string *declp)
2610 int n;
2611 int success = 0;
2613 n = consume_count (mangled);
2614 if (n == -1)
2615 return 0;
2616 if ((int) strlen (*mangled) >= n)
2618 demangle_arm_hp_template (work, mangled, n, declp);
2619 success = 1;
2622 return (success);
2627 LOCAL FUNCTION
2629 demangle_class -- demangle a mangled class sequence
2631 SYNOPSIS
2633 static int
2634 demangle_class (struct work_stuff *work, const char **mangled,
2635 strint *declp)
2637 DESCRIPTION
2639 DECLP points to the buffer into which demangling is being done.
2641 *MANGLED points to the current token to be demangled. On input,
2642 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2643 On exit, it points to the next token after the mangled class on
2644 success, or the first unconsumed token on failure.
2646 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2647 we are demangling a constructor or destructor. In this case
2648 we prepend "class::class" or "class::~class" to DECLP.
2650 Otherwise, we prepend "class::" to the current DECLP.
2652 Reset the constructor/destructor flags once they have been
2653 "consumed". This allows demangle_class to be called later during
2654 the same demangling, to do normal class demangling.
2656 Returns 1 if demangling is successful, 0 otherwise.
2660 static int
2661 demangle_class (struct work_stuff *work, const char **mangled, string *declp)
2663 int success = 0;
2664 int btype;
2665 string class_name;
2666 char *save_class_name_end = 0;
2668 string_init (&class_name);
2669 btype = register_Btype (work);
2670 if (demangle_class_name (work, mangled, &class_name))
2672 save_class_name_end = class_name.p;
2673 if ((work->constructor & 1) || (work->destructor & 1))
2675 /* adjust so we don't include template args */
2676 if (work->temp_start && (work->temp_start != -1))
2678 class_name.p = class_name.b + work->temp_start;
2680 string_prepends (declp, &class_name);
2681 if (work -> destructor & 1)
2683 string_prepend (declp, "~");
2684 work -> destructor -= 1;
2686 else
2688 work -> constructor -= 1;
2691 class_name.p = save_class_name_end;
2692 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2693 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2694 string_prepend (declp, SCOPE_STRING (work));
2695 string_prepends (declp, &class_name);
2696 success = 1;
2698 string_delete (&class_name);
2699 return (success);
2703 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2704 the rightmost guess.
2706 Find the correct "__"-sequence where the function name ends and the
2707 signature starts, which is ambiguous with GNU mangling.
2708 Call demangle_signature here, so we can make sure we found the right
2709 one; *mangled will be consumed so caller will not make further calls to
2710 demangle_signature. */
2712 static int
2713 iterate_demangle_function (struct work_stuff *work, const char **mangled,
2714 string *declp, const char *scan)
2716 const char *mangle_init = *mangled;
2717 int success = 0;
2718 string decl_init;
2719 struct work_stuff work_init;
2721 if (*(scan + 2) == '\0')
2722 return 0;
2724 /* Do not iterate for some demangling modes, or if there's only one
2725 "__"-sequence. This is the normal case. */
2726 if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
2727 || strstr (scan + 2, "__") == NULL)
2728 return demangle_function_name (work, mangled, declp, scan);
2730 /* Save state so we can restart if the guess at the correct "__" was
2731 wrong. */
2732 string_init (&decl_init);
2733 string_appends (&decl_init, declp);
2734 memset (&work_init, 0, sizeof work_init);
2735 work_stuff_copy_to_from (&work_init, work);
2737 /* Iterate over occurrences of __, allowing names and types to have a
2738 "__" sequence in them. We must start with the first (not the last)
2739 occurrence, since "__" most often occur between independent mangled
2740 parts, hence starting at the last occurence inside a signature
2741 might get us a "successful" demangling of the signature. */
2743 while (scan[2])
2745 if (demangle_function_name (work, mangled, declp, scan))
2747 success = demangle_signature (work, mangled, declp);
2748 if (success)
2749 break;
2752 /* Reset demangle state for the next round. */
2753 *mangled = mangle_init;
2754 string_clear (declp);
2755 string_appends (declp, &decl_init);
2756 work_stuff_copy_to_from (work, &work_init);
2758 /* Leave this underscore-sequence. */
2759 scan += 2;
2761 /* Scan for the next "__" sequence. */
2762 while (*scan && (scan[0] != '_' || scan[1] != '_'))
2763 scan++;
2765 /* Move to last "__" in this sequence. */
2766 while (*scan && *scan == '_')
2767 scan++;
2768 scan -= 2;
2771 /* Delete saved state. */
2772 delete_work_stuff (&work_init);
2773 string_delete (&decl_init);
2775 return success;
2780 LOCAL FUNCTION
2782 demangle_prefix -- consume the mangled name prefix and find signature
2784 SYNOPSIS
2786 static int
2787 demangle_prefix (struct work_stuff *work, const char **mangled,
2788 string *declp);
2790 DESCRIPTION
2792 Consume and demangle the prefix of the mangled name.
2793 While processing the function name root, arrange to call
2794 demangle_signature if the root is ambiguous.
2796 DECLP points to the string buffer into which demangled output is
2797 placed. On entry, the buffer is empty. On exit it contains
2798 the root function name, the demangled operator name, or in some
2799 special cases either nothing or the completely demangled result.
2801 MANGLED points to the current pointer into the mangled name. As each
2802 token of the mangled name is consumed, it is updated. Upon entry
2803 the current mangled name pointer points to the first character of
2804 the mangled name. Upon exit, it should point to the first character
2805 of the signature if demangling was successful, or to the first
2806 unconsumed character if demangling of the prefix was unsuccessful.
2808 Returns 1 on success, 0 otherwise.
2811 static int
2812 demangle_prefix (struct work_stuff *work, const char **mangled,
2813 string *declp)
2815 int success = 1;
2816 const char *scan;
2817 int i;
2819 if (strlen(*mangled) > 6
2820 && (strncmp(*mangled, "_imp__", 6) == 0
2821 || strncmp(*mangled, "__imp_", 6) == 0))
2823 /* it's a symbol imported from a PE dynamic library. Check for both
2824 new style prefix _imp__ and legacy __imp_ used by older versions
2825 of dlltool. */
2826 (*mangled) += 6;
2827 work->dllimported = 1;
2829 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2831 char *marker = strchr (cplus_markers, (*mangled)[8]);
2832 if (marker != NULL && *marker == (*mangled)[10])
2834 if ((*mangled)[9] == 'D')
2836 /* it's a GNU global destructor to be executed at program exit */
2837 (*mangled) += 11;
2838 work->destructor = 2;
2839 if (gnu_special (work, mangled, declp))
2840 return success;
2842 else if ((*mangled)[9] == 'I')
2844 /* it's a GNU global constructor to be executed at program init */
2845 (*mangled) += 11;
2846 work->constructor = 2;
2847 if (gnu_special (work, mangled, declp))
2848 return success;
2852 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2854 /* it's a ARM global destructor to be executed at program exit */
2855 (*mangled) += 7;
2856 work->destructor = 2;
2858 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2860 /* it's a ARM global constructor to be executed at program initial */
2861 (*mangled) += 7;
2862 work->constructor = 2;
2865 /* This block of code is a reduction in strength time optimization
2867 scan = strstr (*mangled, "__"); */
2870 scan = *mangled;
2872 do {
2873 scan = strchr (scan, '_');
2874 } while (scan != NULL && *++scan != '_');
2876 if (scan != NULL) --scan;
2879 if (scan != NULL)
2881 /* We found a sequence of two or more '_', ensure that we start at
2882 the last pair in the sequence. */
2883 i = strspn (scan, "_");
2884 if (i > 2)
2886 scan += (i - 2);
2890 if (scan == NULL)
2892 success = 0;
2894 else if (work -> static_type)
2896 if (!ISDIGIT ((unsigned char)scan[0]) && (scan[0] != 't'))
2898 success = 0;
2901 else if ((scan == *mangled)
2902 && (ISDIGIT ((unsigned char)scan[2]) || (scan[2] == 'Q')
2903 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2905 /* The ARM says nothing about the mangling of local variables.
2906 But cfront mangles local variables by prepending __<nesting_level>
2907 to them. As an extension to ARM demangling we handle this case. */
2908 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
2909 && ISDIGIT ((unsigned char)scan[2]))
2911 *mangled = scan + 2;
2912 consume_count (mangled);
2913 string_append (declp, *mangled);
2914 *mangled += strlen (*mangled);
2915 success = 1;
2917 else
2919 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2920 names like __Q2_3foo3bar for nested type names. So don't accept
2921 this style of constructor for cfront demangling. A GNU
2922 style member-template constructor starts with 'H'. */
2923 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2924 work -> constructor += 1;
2925 *mangled = scan + 2;
2928 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2930 /* Cfront-style parameterized type. Handled later as a signature. */
2931 success = 1;
2933 /* ARM template? */
2934 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2936 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2937 || (scan[2] == 'p' && scan[3] == 's')
2938 || (scan[2] == 'p' && scan[3] == 't')))
2940 /* EDG-style parameterized type. Handled later as a signature. */
2941 success = 1;
2943 /* EDG template? */
2944 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2946 else if ((scan == *mangled) && !ISDIGIT ((unsigned char)scan[2])
2947 && (scan[2] != 't'))
2949 /* Mangled name starts with "__". Skip over any leading '_' characters,
2950 then find the next "__" that separates the prefix from the signature.
2952 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2953 || (arm_special (mangled, declp) == 0))
2955 while (*scan == '_')
2957 scan++;
2959 if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
2961 /* No separator (I.E. "__not_mangled"), or empty signature
2962 (I.E. "__not_mangled_either__") */
2963 success = 0;
2965 else
2966 return iterate_demangle_function (work, mangled, declp, scan);
2969 else if (*(scan + 2) != '\0')
2971 /* Mangled name does not start with "__" but does have one somewhere
2972 in there with non empty stuff after it. Looks like a global
2973 function name. Iterate over all "__":s until the right
2974 one is found. */
2975 return iterate_demangle_function (work, mangled, declp, scan);
2977 else
2979 /* Doesn't look like a mangled name */
2980 success = 0;
2983 if (!success && (work->constructor == 2 || work->destructor == 2))
2985 string_append (declp, *mangled);
2986 *mangled += strlen (*mangled);
2987 success = 1;
2989 return (success);
2994 LOCAL FUNCTION
2996 gnu_special -- special handling of gnu mangled strings
2998 SYNOPSIS
3000 static int
3001 gnu_special (struct work_stuff *work, const char **mangled,
3002 string *declp);
3005 DESCRIPTION
3007 Process some special GNU style mangling forms that don't fit
3008 the normal pattern. For example:
3010 _$_3foo (destructor for class foo)
3011 _vt$foo (foo virtual table)
3012 _vt$foo$bar (foo::bar virtual table)
3013 __vt_foo (foo virtual table, new style with thunks)
3014 _3foo$varname (static data member)
3015 _Q22rs2tu$vw (static data member)
3016 __t6vector1Zii (constructor with template)
3017 __thunk_4__$_7ostream (virtual function thunk)
3020 static int
3021 gnu_special (struct work_stuff *work, const char **mangled, string *declp)
3023 int n;
3024 int success = 1;
3025 const char *p;
3027 if ((*mangled)[0] == '_' && (*mangled)[1] != '\0'
3028 && strchr (cplus_markers, (*mangled)[1]) != NULL
3029 && (*mangled)[2] == '_')
3031 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
3032 (*mangled) += 3;
3033 work -> destructor += 1;
3035 else if ((*mangled)[0] == '_'
3036 && (((*mangled)[1] == '_'
3037 && (*mangled)[2] == 'v'
3038 && (*mangled)[3] == 't'
3039 && (*mangled)[4] == '_')
3040 || ((*mangled)[1] == 'v'
3041 && (*mangled)[2] == 't' && (*mangled)[3] != '\0'
3042 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
3044 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
3045 and create the decl. Note that we consume the entire mangled
3046 input string, which means that demangle_signature has no work
3047 to do. */
3048 if ((*mangled)[2] == 'v')
3049 (*mangled) += 5; /* New style, with thunks: "__vt_" */
3050 else
3051 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
3052 while (**mangled != '\0')
3054 switch (**mangled)
3056 case 'Q':
3057 case 'K':
3058 success = demangle_qualified (work, mangled, declp, 0, 1);
3059 break;
3060 case 't':
3061 success = demangle_template (work, mangled, declp, 0, 1,
3063 break;
3064 default:
3065 if (ISDIGIT((unsigned char)*mangled[0]))
3067 n = consume_count(mangled);
3068 /* We may be seeing a too-large size, or else a
3069 ".<digits>" indicating a static local symbol. In
3070 any case, declare victory and move on; *don't* try
3071 to use n to allocate. */
3072 if (n > (int) strlen (*mangled))
3074 success = 1;
3075 break;
3077 else if (n == -1)
3079 success = 0;
3080 break;
3083 else
3085 n = strcspn (*mangled, cplus_markers);
3087 string_appendn (declp, *mangled, n);
3088 (*mangled) += n;
3091 p = strpbrk (*mangled, cplus_markers);
3092 if (success && ((p == NULL) || (p == *mangled)))
3094 if (p != NULL)
3096 string_append (declp, SCOPE_STRING (work));
3097 (*mangled)++;
3100 else
3102 success = 0;
3103 break;
3106 if (success)
3107 string_append (declp, " virtual table");
3109 else if ((*mangled)[0] == '_'
3110 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
3111 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
3113 /* static data member, "_3foo$varname" for example */
3114 (*mangled)++;
3115 switch (**mangled)
3117 case 'Q':
3118 case 'K':
3119 success = demangle_qualified (work, mangled, declp, 0, 1);
3120 break;
3121 case 't':
3122 success = demangle_template (work, mangled, declp, 0, 1, 1);
3123 break;
3124 default:
3125 n = consume_count (mangled);
3126 if (n < 0 || n > (long) strlen (*mangled))
3128 success = 0;
3129 break;
3132 if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
3133 && (*mangled)[9] == 'N'
3134 && (*mangled)[8] == (*mangled)[10]
3135 && strchr (cplus_markers, (*mangled)[8]))
3137 /* A member of the anonymous namespace. There's information
3138 about what identifier or filename it was keyed to, but
3139 it's just there to make the mangled name unique; we just
3140 step over it. */
3141 string_append (declp, "{anonymous}");
3142 (*mangled) += n;
3144 /* Now p points to the marker before the N, so we need to
3145 update it to the first marker after what we consumed. */
3146 p = strpbrk (*mangled, cplus_markers);
3147 break;
3150 string_appendn (declp, *mangled, n);
3151 (*mangled) += n;
3153 if (success && (p == *mangled))
3155 /* Consumed everything up to the cplus_marker, append the
3156 variable name. */
3157 (*mangled)++;
3158 string_append (declp, SCOPE_STRING (work));
3159 n = strlen (*mangled);
3160 string_appendn (declp, *mangled, n);
3161 (*mangled) += n;
3163 else
3165 success = 0;
3168 else if (strncmp (*mangled, "__thunk_", 8) == 0)
3170 int delta;
3172 (*mangled) += 8;
3173 delta = consume_count (mangled);
3174 if (delta == -1)
3175 success = 0;
3176 else
3178 char *method = internal_cplus_demangle (work, ++*mangled);
3180 if (method)
3182 char buf[50];
3183 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
3184 string_append (declp, buf);
3185 string_append (declp, method);
3186 free (method);
3187 n = strlen (*mangled);
3188 (*mangled) += n;
3190 else
3192 success = 0;
3196 else if (strncmp (*mangled, "__t", 3) == 0
3197 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
3199 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
3200 (*mangled) += 4;
3201 switch (**mangled)
3203 case 'Q':
3204 case 'K':
3205 success = demangle_qualified (work, mangled, declp, 0, 1);
3206 break;
3207 case 't':
3208 success = demangle_template (work, mangled, declp, 0, 1, 1);
3209 break;
3210 default:
3211 success = do_type (work, mangled, declp);
3212 break;
3214 if (success && **mangled != '\0')
3215 success = 0;
3216 if (success)
3217 string_append (declp, p);
3219 else
3221 success = 0;
3223 return (success);
3226 static void
3227 recursively_demangle(struct work_stuff *work, const char **mangled,
3228 string *result, int namelength)
3230 char * recurse = (char *)NULL;
3231 char * recurse_dem = (char *)NULL;
3233 recurse = XNEWVEC (char, namelength + 1);
3234 memcpy (recurse, *mangled, namelength);
3235 recurse[namelength] = '\000';
3237 recurse_dem = cplus_demangle (recurse, work->options);
3239 if (recurse_dem)
3241 string_append (result, recurse_dem);
3242 free (recurse_dem);
3244 else
3246 string_appendn (result, *mangled, namelength);
3248 free (recurse);
3249 *mangled += namelength;
3254 LOCAL FUNCTION
3256 arm_special -- special handling of ARM/lucid mangled strings
3258 SYNOPSIS
3260 static int
3261 arm_special (const char **mangled,
3262 string *declp);
3265 DESCRIPTION
3267 Process some special ARM style mangling forms that don't fit
3268 the normal pattern. For example:
3270 __vtbl__3foo (foo virtual table)
3271 __vtbl__3foo__3bar (bar::foo virtual table)
3275 static int
3276 arm_special (const char **mangled, string *declp)
3278 int n;
3279 int success = 1;
3280 const char *scan;
3282 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
3284 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3285 and create the decl. Note that we consume the entire mangled
3286 input string, which means that demangle_signature has no work
3287 to do. */
3288 scan = *mangled + ARM_VTABLE_STRLEN;
3289 while (*scan != '\0') /* first check it can be demangled */
3291 n = consume_count (&scan);
3292 if (n == -1)
3294 return (0); /* no good */
3296 scan += n;
3297 if (scan[0] == '_' && scan[1] == '_')
3299 scan += 2;
3302 (*mangled) += ARM_VTABLE_STRLEN;
3303 while (**mangled != '\0')
3305 n = consume_count (mangled);
3306 if (n == -1
3307 || n > (long) strlen (*mangled))
3308 return 0;
3309 string_prependn (declp, *mangled, n);
3310 (*mangled) += n;
3311 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
3313 string_prepend (declp, "::");
3314 (*mangled) += 2;
3317 string_append (declp, " virtual table");
3319 else
3321 success = 0;
3323 return (success);
3328 LOCAL FUNCTION
3330 demangle_qualified -- demangle 'Q' qualified name strings
3332 SYNOPSIS
3334 static int
3335 demangle_qualified (struct work_stuff *, const char *mangled,
3336 string *result, int isfuncname, int append);
3338 DESCRIPTION
3340 Demangle a qualified name, such as "Q25Outer5Inner" which is
3341 the mangled form of "Outer::Inner". The demangled output is
3342 prepended or appended to the result string according to the
3343 state of the append flag.
3345 If isfuncname is nonzero, then the qualified name we are building
3346 is going to be used as a member function name, so if it is a
3347 constructor or destructor function, append an appropriate
3348 constructor or destructor name. I.E. for the above example,
3349 the result for use as a constructor is "Outer::Inner::Inner"
3350 and the result for use as a destructor is "Outer::Inner::~Inner".
3352 BUGS
3354 Numeric conversion is ASCII dependent (FIXME).
3358 static int
3359 demangle_qualified (struct work_stuff *work, const char **mangled,
3360 string *result, int isfuncname, int append)
3362 int qualifiers = 0;
3363 int success = 1;
3364 char num[2];
3365 string temp;
3366 string last_name;
3367 int bindex = register_Btype (work);
3369 /* We only make use of ISFUNCNAME if the entity is a constructor or
3370 destructor. */
3371 isfuncname = (isfuncname
3372 && ((work->constructor & 1) || (work->destructor & 1)));
3374 string_init (&temp);
3375 string_init (&last_name);
3377 if ((*mangled)[0] == 'K')
3379 /* Squangling qualified name reuse */
3380 int idx;
3381 (*mangled)++;
3382 idx = consume_count_with_underscores (mangled);
3383 if (idx == -1 || idx >= work -> numk)
3384 success = 0;
3385 else
3386 string_append (&temp, work -> ktypevec[idx]);
3388 else
3389 switch ((*mangled)[1])
3391 case '_':
3392 /* GNU mangled name with more than 9 classes. The count is preceded
3393 by an underscore (to distinguish it from the <= 9 case) and followed
3394 by an underscore. */
3395 (*mangled)++;
3396 qualifiers = consume_count_with_underscores (mangled);
3397 if (qualifiers == -1)
3398 success = 0;
3399 break;
3401 case '1':
3402 case '2':
3403 case '3':
3404 case '4':
3405 case '5':
3406 case '6':
3407 case '7':
3408 case '8':
3409 case '9':
3410 /* The count is in a single digit. */
3411 num[0] = (*mangled)[1];
3412 num[1] = '\0';
3413 qualifiers = atoi (num);
3415 /* If there is an underscore after the digit, skip it. This is
3416 said to be for ARM-qualified names, but the ARM makes no
3417 mention of such an underscore. Perhaps cfront uses one. */
3418 if ((*mangled)[2] == '_')
3420 (*mangled)++;
3422 (*mangled) += 2;
3423 break;
3425 case '0':
3426 default:
3427 success = 0;
3430 if (!success)
3431 return success;
3433 /* Pick off the names and collect them in the temp buffer in the order
3434 in which they are found, separated by '::'. */
3436 while (qualifiers-- > 0)
3438 int remember_K = 1;
3439 string_clear (&last_name);
3441 if (*mangled[0] == '_')
3442 (*mangled)++;
3444 if (*mangled[0] == 't')
3446 /* Here we always append to TEMP since we will want to use
3447 the template name without the template parameters as a
3448 constructor or destructor name. The appropriate
3449 (parameter-less) value is returned by demangle_template
3450 in LAST_NAME. We do not remember the template type here,
3451 in order to match the G++ mangling algorithm. */
3452 success = demangle_template(work, mangled, &temp,
3453 &last_name, 1, 0);
3454 if (!success)
3455 break;
3457 else if (*mangled[0] == 'K')
3459 int idx;
3460 (*mangled)++;
3461 idx = consume_count_with_underscores (mangled);
3462 if (idx == -1 || idx >= work->numk)
3463 success = 0;
3464 else
3465 string_append (&temp, work->ktypevec[idx]);
3466 remember_K = 0;
3468 if (!success) break;
3470 else
3472 if (EDG_DEMANGLING)
3474 int namelength;
3475 /* Now recursively demangle the qualifier
3476 * This is necessary to deal with templates in
3477 * mangling styles like EDG */
3478 namelength = consume_count (mangled);
3479 if (namelength == -1)
3481 success = 0;
3482 break;
3484 recursively_demangle(work, mangled, &temp, namelength);
3486 else
3488 string_delete (&last_name);
3489 success = do_type (work, mangled, &last_name);
3490 if (!success)
3491 break;
3492 string_appends (&temp, &last_name);
3496 if (remember_K)
3497 remember_Ktype (work, temp.b, LEN_STRING (&temp));
3499 if (qualifiers > 0)
3500 string_append (&temp, SCOPE_STRING (work));
3503 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
3505 /* If we are using the result as a function name, we need to append
3506 the appropriate '::' separated constructor or destructor name.
3507 We do this here because this is the most convenient place, where
3508 we already have a pointer to the name and the length of the name. */
3510 if (isfuncname)
3512 string_append (&temp, SCOPE_STRING (work));
3513 if (work -> destructor & 1)
3514 string_append (&temp, "~");
3515 string_appends (&temp, &last_name);
3518 /* Now either prepend the temp buffer to the result, or append it,
3519 depending upon the state of the append flag. */
3521 if (append)
3522 string_appends (result, &temp);
3523 else
3525 if (!STRING_EMPTY (result))
3526 string_append (&temp, SCOPE_STRING (work));
3527 string_prepends (result, &temp);
3530 string_delete (&last_name);
3531 string_delete (&temp);
3532 return (success);
3537 LOCAL FUNCTION
3539 get_count -- convert an ascii count to integer, consuming tokens
3541 SYNOPSIS
3543 static int
3544 get_count (const char **type, int *count)
3546 DESCRIPTION
3548 Assume that *type points at a count in a mangled name; set
3549 *count to its value, and set *type to the next character after
3550 the count. There are some weird rules in effect here.
3552 If *type does not point at a string of digits, return zero.
3554 If *type points at a string of digits followed by an
3555 underscore, set *count to their value as an integer, advance
3556 *type to point *after the underscore, and return 1.
3558 If *type points at a string of digits not followed by an
3559 underscore, consume only the first digit. Set *count to its
3560 value as an integer, leave *type pointing after that digit,
3561 and return 1.
3563 The excuse for this odd behavior: in the ARM and HP demangling
3564 styles, a type can be followed by a repeat count of the form
3565 `Nxy', where:
3567 `x' is a single digit specifying how many additional copies
3568 of the type to append to the argument list, and
3570 `y' is one or more digits, specifying the zero-based index of
3571 the first repeated argument in the list. Yes, as you're
3572 unmangling the name you can figure this out yourself, but
3573 it's there anyway.
3575 So, for example, in `bar__3fooFPiN51', the first argument is a
3576 pointer to an integer (`Pi'), and then the next five arguments
3577 are the same (`N5'), and the first repeat is the function's
3578 second argument (`1').
3581 static int
3582 get_count (const char **type, int *count)
3584 const char *p;
3585 int n;
3587 if (!ISDIGIT ((unsigned char)**type))
3588 return (0);
3589 else
3591 *count = **type - '0';
3592 (*type)++;
3593 if (ISDIGIT ((unsigned char)**type))
3595 p = *type;
3596 n = *count;
3599 n *= 10;
3600 n += *p - '0';
3601 p++;
3603 while (ISDIGIT ((unsigned char)*p));
3604 if (*p == '_')
3606 *type = p + 1;
3607 *count = n;
3611 return (1);
3614 /* RESULT will be initialised here; it will be freed on failure. The
3615 value returned is really a type_kind_t. */
3617 static int
3618 do_type (struct work_stuff *work, const char **mangled, string *result)
3620 int n;
3621 int i;
3622 int is_proctypevec;
3623 int done;
3624 int success;
3625 string decl;
3626 const char *remembered_type;
3627 int type_quals;
3628 type_kind_t tk = tk_none;
3630 string_init (&decl);
3631 string_init (result);
3633 done = 0;
3634 success = 1;
3635 is_proctypevec = 0;
3636 while (success && !done)
3638 int member;
3639 switch (**mangled)
3642 /* A pointer type */
3643 case 'P':
3644 case 'p':
3645 (*mangled)++;
3646 if (! (work -> options & DMGL_JAVA))
3647 string_prepend (&decl, "*");
3648 if (tk == tk_none)
3649 tk = tk_pointer;
3650 break;
3652 /* A reference type */
3653 case 'R':
3654 (*mangled)++;
3655 string_prepend (&decl, "&");
3656 if (tk == tk_none)
3657 tk = tk_reference;
3658 break;
3660 /* An rvalue reference type */
3661 case 'O':
3662 (*mangled)++;
3663 string_prepend (&decl, "&&");
3664 if (tk == tk_none)
3665 tk = tk_rvalue_reference;
3666 break;
3668 /* An array */
3669 case 'A':
3671 ++(*mangled);
3672 if (!STRING_EMPTY (&decl)
3673 && (decl.b[0] == '*' || decl.b[0] == '&'))
3675 string_prepend (&decl, "(");
3676 string_append (&decl, ")");
3678 string_append (&decl, "[");
3679 if (**mangled != '_')
3680 success = demangle_template_value_parm (work, mangled, &decl,
3681 tk_integral);
3682 if (**mangled == '_')
3683 ++(*mangled);
3684 string_append (&decl, "]");
3685 break;
3688 /* A back reference to a previously seen type */
3689 case 'T':
3690 (*mangled)++;
3691 if (!get_count (mangled, &n) || n < 0 || n >= work -> ntypes)
3693 success = 0;
3695 else
3696 for (i = 0; i < work->nproctypes; i++)
3697 if (work -> proctypevec [i] == n)
3698 success = 0;
3700 if (success)
3702 is_proctypevec = 1;
3703 push_processed_type (work, n);
3704 remembered_type = work->typevec[n];
3705 mangled = &remembered_type;
3707 break;
3709 /* A function */
3710 case 'F':
3711 (*mangled)++;
3712 if (!STRING_EMPTY (&decl)
3713 && (decl.b[0] == '*' || decl.b[0] == '&'))
3715 string_prepend (&decl, "(");
3716 string_append (&decl, ")");
3718 /* After picking off the function args, we expect to either find the
3719 function return type (preceded by an '_') or the end of the
3720 string. */
3721 if (!demangle_nested_args (work, mangled, &decl)
3722 || (**mangled != '_' && **mangled != '\0'))
3724 success = 0;
3725 break;
3727 if (success && (**mangled == '_'))
3728 (*mangled)++;
3729 break;
3731 case 'M':
3733 type_quals = TYPE_UNQUALIFIED;
3735 member = **mangled == 'M';
3736 (*mangled)++;
3738 string_append (&decl, ")");
3740 /* We don't need to prepend `::' for a qualified name;
3741 demangle_qualified will do that for us. */
3742 if (**mangled != 'Q')
3743 string_prepend (&decl, SCOPE_STRING (work));
3745 if (ISDIGIT ((unsigned char)**mangled))
3747 n = consume_count (mangled);
3748 if (n == -1
3749 || (int) strlen (*mangled) < n)
3751 success = 0;
3752 break;
3754 string_prependn (&decl, *mangled, n);
3755 *mangled += n;
3757 else if (**mangled == 'X' || **mangled == 'Y')
3759 string temp;
3760 do_type (work, mangled, &temp);
3761 string_prepends (&decl, &temp);
3762 string_delete (&temp);
3764 else if (**mangled == 't')
3766 string temp;
3767 string_init (&temp);
3768 success = demangle_template (work, mangled, &temp,
3769 NULL, 1, 1);
3770 if (success)
3772 string_prependn (&decl, temp.b, temp.p - temp.b);
3773 string_delete (&temp);
3775 else
3777 string_delete (&temp);
3778 break;
3781 else if (**mangled == 'Q')
3783 success = demangle_qualified (work, mangled, &decl,
3784 /*isfuncnam=*/0,
3785 /*append=*/0);
3786 if (!success)
3787 break;
3789 else
3791 success = 0;
3792 break;
3795 string_prepend (&decl, "(");
3796 if (member)
3798 switch (**mangled)
3800 case 'C':
3801 case 'V':
3802 case 'u':
3803 type_quals |= code_for_qualifier (**mangled);
3804 (*mangled)++;
3805 break;
3807 default:
3808 break;
3811 if (*(*mangled) != 'F')
3813 success = 0;
3814 break;
3816 (*mangled)++;
3818 if ((member && !demangle_nested_args (work, mangled, &decl))
3819 || **mangled != '_')
3821 success = 0;
3822 break;
3824 (*mangled)++;
3825 if (! PRINT_ANSI_QUALIFIERS)
3827 break;
3829 if (type_quals != TYPE_UNQUALIFIED)
3831 APPEND_BLANK (&decl);
3832 string_append (&decl, qualifier_string (type_quals));
3834 break;
3836 case 'G':
3837 (*mangled)++;
3838 break;
3840 case 'C':
3841 case 'V':
3842 case 'u':
3843 if (PRINT_ANSI_QUALIFIERS)
3845 if (!STRING_EMPTY (&decl))
3846 string_prepend (&decl, " ");
3848 string_prepend (&decl, demangle_qualifier (**mangled));
3850 (*mangled)++;
3851 break;
3856 /* fall through */
3857 default:
3858 done = 1;
3859 break;
3863 if (success) switch (**mangled)
3865 /* A qualified name, such as "Outer::Inner". */
3866 case 'Q':
3867 case 'K':
3869 success = demangle_qualified (work, mangled, result, 0, 1);
3870 break;
3873 /* A back reference to a previously seen squangled type */
3874 case 'B':
3875 (*mangled)++;
3876 if (!get_count (mangled, &n) || n < 0 || n >= work -> numb)
3877 success = 0;
3878 else
3879 string_append (result, work->btypevec[n]);
3880 break;
3882 case 'X':
3883 case 'Y':
3884 /* A template parm. We substitute the corresponding argument. */
3886 int idx;
3888 (*mangled)++;
3889 idx = consume_count_with_underscores (mangled);
3891 if (idx == -1
3892 || (work->tmpl_argvec && idx >= work->ntmpl_args)
3893 || consume_count_with_underscores (mangled) == -1)
3895 success = 0;
3896 break;
3899 if (work->tmpl_argvec)
3900 string_append (result, work->tmpl_argvec[idx]);
3901 else
3902 string_append_template_idx (result, idx);
3904 success = 1;
3906 break;
3908 default:
3909 success = demangle_fund_type (work, mangled, result);
3910 if (tk == tk_none)
3911 tk = (type_kind_t) success;
3912 break;
3915 if (success)
3917 if (!STRING_EMPTY (&decl))
3919 string_append (result, " ");
3920 string_appends (result, &decl);
3923 else
3924 string_delete (result);
3925 string_delete (&decl);
3927 if (is_proctypevec)
3928 pop_processed_type (work);
3930 if (success)
3931 /* Assume an integral type, if we're not sure. */
3932 return (int) ((tk == tk_none) ? tk_integral : tk);
3933 else
3934 return 0;
3937 /* Given a pointer to a type string that represents a fundamental type
3938 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3939 string in which the demangled output is being built in RESULT, and
3940 the WORK structure, decode the types and add them to the result.
3942 For example:
3944 "Ci" => "const int"
3945 "Sl" => "signed long"
3946 "CUs" => "const unsigned short"
3948 The value returned is really a type_kind_t. */
3950 static int
3951 demangle_fund_type (struct work_stuff *work,
3952 const char **mangled, string *result)
3954 int done = 0;
3955 int success = 1;
3956 char buf[INTBUF_SIZE + 5 /* 'int%u_t' */];
3957 unsigned int dec = 0;
3958 type_kind_t tk = tk_integral;
3960 /* First pick off any type qualifiers. There can be more than one. */
3962 while (!done)
3964 switch (**mangled)
3966 case 'C':
3967 case 'V':
3968 case 'u':
3969 if (PRINT_ANSI_QUALIFIERS)
3971 if (!STRING_EMPTY (result))
3972 string_prepend (result, " ");
3973 string_prepend (result, demangle_qualifier (**mangled));
3975 (*mangled)++;
3976 break;
3977 case 'U':
3978 (*mangled)++;
3979 APPEND_BLANK (result);
3980 string_append (result, "unsigned");
3981 break;
3982 case 'S': /* signed char only */
3983 (*mangled)++;
3984 APPEND_BLANK (result);
3985 string_append (result, "signed");
3986 break;
3987 case 'J':
3988 (*mangled)++;
3989 APPEND_BLANK (result);
3990 string_append (result, "__complex");
3991 break;
3992 default:
3993 done = 1;
3994 break;
3998 /* Now pick off the fundamental type. There can be only one. */
4000 switch (**mangled)
4002 case '\0':
4003 case '_':
4004 break;
4005 case 'v':
4006 (*mangled)++;
4007 APPEND_BLANK (result);
4008 string_append (result, "void");
4009 break;
4010 case 'x':
4011 (*mangled)++;
4012 APPEND_BLANK (result);
4013 string_append (result, "long long");
4014 break;
4015 case 'l':
4016 (*mangled)++;
4017 APPEND_BLANK (result);
4018 string_append (result, "long");
4019 break;
4020 case 'i':
4021 (*mangled)++;
4022 APPEND_BLANK (result);
4023 string_append (result, "int");
4024 break;
4025 case 's':
4026 (*mangled)++;
4027 APPEND_BLANK (result);
4028 string_append (result, "short");
4029 break;
4030 case 'b':
4031 (*mangled)++;
4032 APPEND_BLANK (result);
4033 string_append (result, "bool");
4034 tk = tk_bool;
4035 break;
4036 case 'c':
4037 (*mangled)++;
4038 APPEND_BLANK (result);
4039 string_append (result, "char");
4040 tk = tk_char;
4041 break;
4042 case 'w':
4043 (*mangled)++;
4044 APPEND_BLANK (result);
4045 string_append (result, "wchar_t");
4046 tk = tk_char;
4047 break;
4048 case 'r':
4049 (*mangled)++;
4050 APPEND_BLANK (result);
4051 string_append (result, "long double");
4052 tk = tk_real;
4053 break;
4054 case 'd':
4055 (*mangled)++;
4056 APPEND_BLANK (result);
4057 string_append (result, "double");
4058 tk = tk_real;
4059 break;
4060 case 'f':
4061 (*mangled)++;
4062 APPEND_BLANK (result);
4063 string_append (result, "float");
4064 tk = tk_real;
4065 break;
4066 case 'G':
4067 (*mangled)++;
4068 if (!ISDIGIT ((unsigned char)**mangled))
4070 success = 0;
4071 break;
4073 /* fall through */
4074 case 'I':
4075 (*mangled)++;
4076 if (**mangled == '_')
4078 int i;
4079 (*mangled)++;
4080 for (i = 0;
4081 i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
4082 (*mangled)++, i++)
4083 buf[i] = **mangled;
4084 if (**mangled != '_')
4086 success = 0;
4087 break;
4089 buf[i] = '\0';
4090 (*mangled)++;
4092 else
4094 strncpy (buf, *mangled, 2);
4095 buf[2] = '\0';
4096 *mangled += min (strlen (*mangled), 2);
4098 sscanf (buf, "%x", &dec);
4099 sprintf (buf, "int%u_t", dec);
4100 APPEND_BLANK (result);
4101 string_append (result, buf);
4102 break;
4104 /* fall through */
4105 /* An explicit type, such as "6mytype" or "7integer" */
4106 case '0':
4107 case '1':
4108 case '2':
4109 case '3':
4110 case '4':
4111 case '5':
4112 case '6':
4113 case '7':
4114 case '8':
4115 case '9':
4117 int bindex = register_Btype (work);
4118 string btype;
4119 string_init (&btype);
4120 if (demangle_class_name (work, mangled, &btype)) {
4121 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
4122 APPEND_BLANK (result);
4123 string_appends (result, &btype);
4125 else
4126 success = 0;
4127 string_delete (&btype);
4128 break;
4130 case 't':
4132 string btype;
4133 string_init (&btype);
4134 success = demangle_template (work, mangled, &btype, 0, 1, 1);
4135 string_appends (result, &btype);
4136 string_delete (&btype);
4137 break;
4139 default:
4140 success = 0;
4141 break;
4144 return success ? ((int) tk) : 0;
4148 /* Handle a template's value parameter for HP aCC (extension from ARM)
4149 **mangled points to 'S' or 'U' */
4151 static int
4152 do_hpacc_template_const_value (struct work_stuff *work ATTRIBUTE_UNUSED,
4153 const char **mangled, string *result)
4155 int unsigned_const;
4157 if (**mangled != 'U' && **mangled != 'S')
4158 return 0;
4160 unsigned_const = (**mangled == 'U');
4162 (*mangled)++;
4164 switch (**mangled)
4166 case 'N':
4167 string_append (result, "-");
4168 /* fall through */
4169 case 'P':
4170 (*mangled)++;
4171 break;
4172 case 'M':
4173 /* special case for -2^31 */
4174 string_append (result, "-2147483648");
4175 (*mangled)++;
4176 return 1;
4177 default:
4178 return 0;
4181 /* We have to be looking at an integer now */
4182 if (!(ISDIGIT ((unsigned char)**mangled)))
4183 return 0;
4185 /* We only deal with integral values for template
4186 parameters -- so it's OK to look only for digits */
4187 while (ISDIGIT ((unsigned char)**mangled))
4189 char_str[0] = **mangled;
4190 string_append (result, char_str);
4191 (*mangled)++;
4194 if (unsigned_const)
4195 string_append (result, "U");
4197 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4198 with L or LL suffixes. pai/1997-09-03 */
4200 return 1; /* success */
4203 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4204 **mangled is pointing to the 'A' */
4206 static int
4207 do_hpacc_template_literal (struct work_stuff *work, const char **mangled,
4208 string *result)
4210 int literal_len = 0;
4211 char * recurse;
4212 char * recurse_dem;
4214 if (**mangled != 'A')
4215 return 0;
4217 (*mangled)++;
4219 literal_len = consume_count (mangled);
4221 if (literal_len <= 0
4222 || literal_len > (long) strlen (*mangled))
4223 return 0;
4225 /* Literal parameters are names of arrays, functions, etc. and the
4226 canonical representation uses the address operator */
4227 string_append (result, "&");
4229 /* Now recursively demangle the literal name */
4230 recurse = XNEWVEC (char, literal_len + 1);
4231 memcpy (recurse, *mangled, literal_len);
4232 recurse[literal_len] = '\000';
4234 recurse_dem = cplus_demangle (recurse, work->options);
4236 if (recurse_dem)
4238 string_append (result, recurse_dem);
4239 free (recurse_dem);
4241 else
4243 string_appendn (result, *mangled, literal_len);
4245 (*mangled) += literal_len;
4246 free (recurse);
4248 return 1;
4251 static int
4252 snarf_numeric_literal (const char **args, string *arg)
4254 if (**args == '-')
4256 char_str[0] = '-';
4257 string_append (arg, char_str);
4258 (*args)++;
4260 else if (**args == '+')
4261 (*args)++;
4263 if (!ISDIGIT ((unsigned char)**args))
4264 return 0;
4266 while (ISDIGIT ((unsigned char)**args))
4268 char_str[0] = **args;
4269 string_append (arg, char_str);
4270 (*args)++;
4273 return 1;
4276 /* Demangle the next argument, given by MANGLED into RESULT, which
4277 *should be an uninitialized* string. It will be initialized here,
4278 and free'd should anything go wrong. */
4280 static int
4281 do_arg (struct work_stuff *work, const char **mangled, string *result)
4283 /* Remember where we started so that we can record the type, for
4284 non-squangling type remembering. */
4285 const char *start = *mangled;
4287 string_init (result);
4289 if (work->nrepeats > 0)
4291 --work->nrepeats;
4293 if (work->previous_argument == 0)
4294 return 0;
4296 /* We want to reissue the previous type in this argument list. */
4297 string_appends (result, work->previous_argument);
4298 return 1;
4301 if (**mangled == 'n')
4303 /* A squangling-style repeat. */
4304 (*mangled)++;
4305 work->nrepeats = consume_count(mangled);
4307 if (work->nrepeats <= 0)
4308 /* This was not a repeat count after all. */
4309 return 0;
4311 if (work->nrepeats > 9)
4313 if (**mangled != '_')
4314 /* The repeat count should be followed by an '_' in this
4315 case. */
4316 return 0;
4317 else
4318 (*mangled)++;
4321 /* Now, the repeat is all set up. */
4322 return do_arg (work, mangled, result);
4325 /* Save the result in WORK->previous_argument so that we can find it
4326 if it's repeated. Note that saving START is not good enough: we
4327 do not want to add additional types to the back-referenceable
4328 type vector when processing a repeated type. */
4329 if (work->previous_argument)
4330 string_delete (work->previous_argument);
4331 else
4332 work->previous_argument = XNEW (string);
4334 if (!do_type (work, mangled, work->previous_argument))
4335 return 0;
4337 string_appends (result, work->previous_argument);
4339 remember_type (work, start, *mangled - start);
4340 return 1;
4343 static void
4344 push_processed_type (struct work_stuff *work, int typevec_index)
4346 if (work->nproctypes >= work->proctypevec_size)
4348 if (!work->proctypevec_size)
4350 work->proctypevec_size = 4;
4351 work->proctypevec = XNEWVEC (int, work->proctypevec_size);
4353 else
4355 if (work->proctypevec_size < 16)
4356 /* Double when small. */
4357 work->proctypevec_size *= 2;
4358 else
4360 /* Grow slower when large. */
4361 if (work->proctypevec_size > (INT_MAX / 3) * 2)
4362 xmalloc_failed (INT_MAX);
4363 work->proctypevec_size = (work->proctypevec_size * 3 / 2);
4365 work->proctypevec
4366 = XRESIZEVEC (int, work->proctypevec, work->proctypevec_size);
4369 work->proctypevec [work->nproctypes++] = typevec_index;
4372 static void
4373 pop_processed_type (struct work_stuff *work)
4375 work->nproctypes--;
4378 static void
4379 remember_type (struct work_stuff *work, const char *start, int len)
4381 char *tem;
4383 if (work->forgetting_types)
4384 return;
4386 if (work -> ntypes >= work -> typevec_size)
4388 if (work -> typevec_size == 0)
4390 work -> typevec_size = 3;
4391 work -> typevec = XNEWVEC (char *, work->typevec_size);
4393 else
4395 if (work -> typevec_size > INT_MAX / 2)
4396 xmalloc_failed (INT_MAX);
4397 work -> typevec_size *= 2;
4398 work -> typevec
4399 = XRESIZEVEC (char *, work->typevec, work->typevec_size);
4402 tem = XNEWVEC (char, len + 1);
4403 memcpy (tem, start, len);
4404 tem[len] = '\0';
4405 work -> typevec[work -> ntypes++] = tem;
4409 /* Remember a K type class qualifier. */
4410 static void
4411 remember_Ktype (struct work_stuff *work, const char *start, int len)
4413 char *tem;
4415 if (work -> numk >= work -> ksize)
4417 if (work -> ksize == 0)
4419 work -> ksize = 5;
4420 work -> ktypevec = XNEWVEC (char *, work->ksize);
4422 else
4424 if (work -> ksize > INT_MAX / 2)
4425 xmalloc_failed (INT_MAX);
4426 work -> ksize *= 2;
4427 work -> ktypevec
4428 = XRESIZEVEC (char *, work->ktypevec, work->ksize);
4431 tem = XNEWVEC (char, len + 1);
4432 memcpy (tem, start, len);
4433 tem[len] = '\0';
4434 work -> ktypevec[work -> numk++] = tem;
4437 /* Register a B code, and get an index for it. B codes are registered
4438 as they are seen, rather than as they are completed, so map<temp<char> >
4439 registers map<temp<char> > as B0, and temp<char> as B1 */
4441 static int
4442 register_Btype (struct work_stuff *work)
4444 int ret;
4446 if (work -> numb >= work -> bsize)
4448 if (work -> bsize == 0)
4450 work -> bsize = 5;
4451 work -> btypevec = XNEWVEC (char *, work->bsize);
4453 else
4455 if (work -> bsize > INT_MAX / 2)
4456 xmalloc_failed (INT_MAX);
4457 work -> bsize *= 2;
4458 work -> btypevec
4459 = XRESIZEVEC (char *, work->btypevec, work->bsize);
4462 ret = work -> numb++;
4463 work -> btypevec[ret] = NULL;
4464 return(ret);
4467 /* Store a value into a previously registered B code type. */
4469 static void
4470 remember_Btype (struct work_stuff *work, const char *start,
4471 int len, int index)
4473 char *tem;
4475 tem = XNEWVEC (char, len + 1);
4476 memcpy (tem, start, len);
4477 tem[len] = '\0';
4478 work -> btypevec[index] = tem;
4481 /* Lose all the info related to B and K type codes. */
4482 static void
4483 forget_B_and_K_types (struct work_stuff *work)
4485 int i;
4487 while (work -> numk > 0)
4489 i = --(work -> numk);
4490 if (work -> ktypevec[i] != NULL)
4492 free (work -> ktypevec[i]);
4493 work -> ktypevec[i] = NULL;
4497 while (work -> numb > 0)
4499 i = --(work -> numb);
4500 if (work -> btypevec[i] != NULL)
4502 free (work -> btypevec[i]);
4503 work -> btypevec[i] = NULL;
4507 /* Forget the remembered types, but not the type vector itself. */
4509 static void
4510 forget_types (struct work_stuff *work)
4512 int i;
4514 while (work -> ntypes > 0)
4516 i = --(work -> ntypes);
4517 if (work -> typevec[i] != NULL)
4519 free (work -> typevec[i]);
4520 work -> typevec[i] = NULL;
4525 /* Process the argument list part of the signature, after any class spec
4526 has been consumed, as well as the first 'F' character (if any). For
4527 example:
4529 "__als__3fooRT0" => process "RT0"
4530 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4532 DECLP must be already initialised, usually non-empty. It won't be freed
4533 on failure.
4535 Note that g++ differs significantly from ARM and lucid style mangling
4536 with regards to references to previously seen types. For example, given
4537 the source fragment:
4539 class foo {
4540 public:
4541 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4544 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4545 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4547 g++ produces the names:
4549 __3fooiRT0iT2iT2
4550 foo__FiR3fooiT1iT1
4552 while lcc (and presumably other ARM style compilers as well) produces:
4554 foo__FiR3fooT1T2T1T2
4555 __ct__3fooFiR3fooT1T2T1T2
4557 Note that g++ bases its type numbers starting at zero and counts all
4558 previously seen types, while lucid/ARM bases its type numbers starting
4559 at one and only considers types after it has seen the 'F' character
4560 indicating the start of the function args. For lucid/ARM style, we
4561 account for this difference by discarding any previously seen types when
4562 we see the 'F' character, and subtracting one from the type number
4563 reference.
4567 static int
4568 demangle_args (struct work_stuff *work, const char **mangled,
4569 string *declp)
4571 string arg;
4572 int need_comma = 0;
4573 int r;
4574 int t;
4575 const char *tem;
4576 char temptype;
4578 if (PRINT_ARG_TYPES)
4580 string_append (declp, "(");
4581 if (**mangled == '\0')
4583 string_append (declp, "void");
4587 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
4588 || work->nrepeats > 0)
4590 if ((**mangled == 'N') || (**mangled == 'T'))
4592 temptype = *(*mangled)++;
4594 if (temptype == 'N')
4596 if (!get_count (mangled, &r))
4598 return (0);
4601 else
4603 r = 1;
4605 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
4607 /* If we have 10 or more types we might have more than a 1 digit
4608 index so we'll have to consume the whole count here. This
4609 will lose if the next thing is a type name preceded by a
4610 count but it's impossible to demangle that case properly
4611 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4612 Pc, ...)" or "(..., type12, char *, ...)" */
4613 if ((t = consume_count(mangled)) <= 0)
4615 return (0);
4618 else
4620 if (!get_count (mangled, &t))
4622 return (0);
4625 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4627 t--;
4629 /* Validate the type index. Protect against illegal indices from
4630 malformed type strings. */
4631 if ((t < 0) || (t >= work -> ntypes))
4633 return (0);
4635 while (work->nrepeats > 0 || --r >= 0)
4637 tem = work -> typevec[t];
4638 if (need_comma && PRINT_ARG_TYPES)
4640 string_append (declp, ", ");
4642 push_processed_type (work, t);
4643 if (!do_arg (work, &tem, &arg))
4645 pop_processed_type (work);
4646 return (0);
4648 pop_processed_type (work);
4649 if (PRINT_ARG_TYPES)
4651 string_appends (declp, &arg);
4653 string_delete (&arg);
4654 need_comma = 1;
4657 else
4659 if (need_comma && PRINT_ARG_TYPES)
4660 string_append (declp, ", ");
4661 if (!do_arg (work, mangled, &arg))
4662 return (0);
4663 if (PRINT_ARG_TYPES)
4664 string_appends (declp, &arg);
4665 string_delete (&arg);
4666 need_comma = 1;
4670 if (**mangled == 'e')
4672 (*mangled)++;
4673 if (PRINT_ARG_TYPES)
4675 if (need_comma)
4677 string_append (declp, ",");
4679 string_append (declp, "...");
4683 if (PRINT_ARG_TYPES)
4685 string_append (declp, ")");
4687 return (1);
4690 /* Like demangle_args, but for demangling the argument lists of function
4691 and method pointers or references, not top-level declarations. */
4693 static int
4694 demangle_nested_args (struct work_stuff *work, const char **mangled,
4695 string *declp)
4697 string* saved_previous_argument;
4698 int result;
4699 int saved_nrepeats;
4701 /* The G++ name-mangling algorithm does not remember types on nested
4702 argument lists, unless -fsquangling is used, and in that case the
4703 type vector updated by remember_type is not used. So, we turn
4704 off remembering of types here. */
4705 ++work->forgetting_types;
4707 /* For the repeat codes used with -fsquangling, we must keep track of
4708 the last argument. */
4709 saved_previous_argument = work->previous_argument;
4710 saved_nrepeats = work->nrepeats;
4711 work->previous_argument = 0;
4712 work->nrepeats = 0;
4714 /* Actually demangle the arguments. */
4715 result = demangle_args (work, mangled, declp);
4717 /* Restore the previous_argument field. */
4718 if (work->previous_argument)
4720 string_delete (work->previous_argument);
4721 free ((char *) work->previous_argument);
4723 work->previous_argument = saved_previous_argument;
4724 --work->forgetting_types;
4725 work->nrepeats = saved_nrepeats;
4727 return result;
4730 /* Returns 1 if a valid function name was found or 0 otherwise. */
4732 static int
4733 demangle_function_name (struct work_stuff *work, const char **mangled,
4734 string *declp, const char *scan)
4736 size_t i;
4737 string type;
4738 const char *tem;
4740 string_appendn (declp, (*mangled), scan - (*mangled));
4741 string_need (declp, 1);
4742 *(declp -> p) = '\0';
4744 /* Consume the function name, including the "__" separating the name
4745 from the signature. We are guaranteed that SCAN points to the
4746 separator. */
4748 (*mangled) = scan + 2;
4749 /* We may be looking at an instantiation of a template function:
4750 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4751 following _F marks the start of the function arguments. Handle
4752 the template arguments first. */
4754 if (HP_DEMANGLING && (**mangled == 'X'))
4756 demangle_arm_hp_template (work, mangled, 0, declp);
4757 /* This leaves MANGLED pointing to the 'F' marking func args */
4760 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4763 /* See if we have an ARM style constructor or destructor operator.
4764 If so, then just record it, clear the decl, and return.
4765 We can't build the actual constructor/destructor decl until later,
4766 when we recover the class name from the signature. */
4768 if (strcmp (declp -> b, "__ct") == 0)
4770 work -> constructor += 1;
4771 string_clear (declp);
4772 return 1;
4774 else if (strcmp (declp -> b, "__dt") == 0)
4776 work -> destructor += 1;
4777 string_clear (declp);
4778 return 1;
4782 if (declp->p - declp->b >= 3
4783 && declp->b[0] == 'o'
4784 && declp->b[1] == 'p'
4785 && strchr (cplus_markers, declp->b[2]) != NULL)
4787 /* see if it's an assignment expression */
4788 if (declp->p - declp->b >= 10 /* op$assign_ */
4789 && memcmp (declp->b + 3, "assign_", 7) == 0)
4791 for (i = 0; i < ARRAY_SIZE (optable); i++)
4793 int len = declp->p - declp->b - 10;
4794 if ((int) strlen (optable[i].in) == len
4795 && memcmp (optable[i].in, declp->b + 10, len) == 0)
4797 string_clear (declp);
4798 string_append (declp, "operator");
4799 string_append (declp, optable[i].out);
4800 string_append (declp, "=");
4801 break;
4805 else
4807 for (i = 0; i < ARRAY_SIZE (optable); i++)
4809 int len = declp->p - declp->b - 3;
4810 if ((int) strlen (optable[i].in) == len
4811 && memcmp (optable[i].in, declp->b + 3, len) == 0)
4813 string_clear (declp);
4814 string_append (declp, "operator");
4815 string_append (declp, optable[i].out);
4816 break;
4821 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4822 && strchr (cplus_markers, declp->b[4]) != NULL)
4824 /* type conversion operator */
4825 tem = declp->b + 5;
4826 if (do_type (work, &tem, &type))
4828 string_clear (declp);
4829 string_append (declp, "operator ");
4830 string_appends (declp, &type);
4831 string_delete (&type);
4834 else if (declp->b[0] == '_' && declp->b[1] == '_'
4835 && declp->b[2] == 'o' && declp->b[3] == 'p')
4837 /* ANSI. */
4838 /* type conversion operator. */
4839 tem = declp->b + 4;
4840 if (do_type (work, &tem, &type))
4842 string_clear (declp);
4843 string_append (declp, "operator ");
4844 string_appends (declp, &type);
4845 string_delete (&type);
4848 else if (declp->b[0] == '_' && declp->b[1] == '_'
4849 && ISLOWER((unsigned char)declp->b[2])
4850 && ISLOWER((unsigned char)declp->b[3]))
4852 if (declp->b[4] == '\0')
4854 /* Operator. */
4855 for (i = 0; i < ARRAY_SIZE (optable); i++)
4857 if (strlen (optable[i].in) == 2
4858 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4860 string_clear (declp);
4861 string_append (declp, "operator");
4862 string_append (declp, optable[i].out);
4863 break;
4867 else
4869 if (declp->b[2] == 'a' && declp->b[5] == '\0')
4871 /* Assignment. */
4872 for (i = 0; i < ARRAY_SIZE (optable); i++)
4874 if (strlen (optable[i].in) == 3
4875 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4877 string_clear (declp);
4878 string_append (declp, "operator");
4879 string_append (declp, optable[i].out);
4880 break;
4887 /* If a function name was obtained but it's not valid, we were not
4888 successful. */
4889 if (LEN_STRING (declp) == 1 && declp->b[0] == '.')
4890 return 0;
4891 else
4892 return 1;
4895 /* a mini string-handling package */
4897 static void
4898 string_need (string *s, int n)
4900 int tem;
4902 if (s->b == NULL)
4904 if (n < 32)
4906 n = 32;
4908 s->p = s->b = XNEWVEC (char, n);
4909 s->e = s->b + n;
4911 else if (s->e - s->p < n)
4913 tem = s->p - s->b;
4914 if (n > INT_MAX / 2 - tem)
4915 xmalloc_failed (INT_MAX);
4916 n += tem;
4917 n *= 2;
4918 s->b = XRESIZEVEC (char, s->b, n);
4919 s->p = s->b + tem;
4920 s->e = s->b + n;
4924 static void
4925 string_delete (string *s)
4927 if (s->b != NULL)
4929 free (s->b);
4930 s->b = s->e = s->p = NULL;
4934 static void
4935 string_init (string *s)
4937 s->b = s->p = s->e = NULL;
4940 static void
4941 string_clear (string *s)
4943 s->p = s->b;
4946 #if 0
4948 static int
4949 string_empty (string *s)
4951 return (s->b == s->p);
4954 #endif
4956 static void
4957 string_append (string *p, const char *s)
4959 int n;
4960 if (s == NULL || *s == '\0')
4961 return;
4962 n = strlen (s);
4963 string_need (p, n);
4964 memcpy (p->p, s, n);
4965 p->p += n;
4968 static void
4969 string_appends (string *p, string *s)
4971 int n;
4973 if (s->b != s->p)
4975 n = s->p - s->b;
4976 string_need (p, n);
4977 memcpy (p->p, s->b, n);
4978 p->p += n;
4982 static void
4983 string_appendn (string *p, const char *s, int n)
4985 if (n != 0)
4987 string_need (p, n);
4988 memcpy (p->p, s, n);
4989 p->p += n;
4993 static void
4994 string_prepend (string *p, const char *s)
4996 if (s != NULL && *s != '\0')
4998 string_prependn (p, s, strlen (s));
5002 static void
5003 string_prepends (string *p, string *s)
5005 if (s->b != s->p)
5007 string_prependn (p, s->b, s->p - s->b);
5011 static void
5012 string_prependn (string *p, const char *s, int n)
5014 char *q;
5016 if (n != 0)
5018 string_need (p, n);
5019 for (q = p->p - 1; q >= p->b; q--)
5021 q[n] = q[0];
5023 memcpy (p->b, s, n);
5024 p->p += n;
5028 static void
5029 string_append_template_idx (string *s, int idx)
5031 char buf[INTBUF_SIZE + 1 /* 'T' */];
5032 sprintf(buf, "T%d", idx);
5033 string_append (s, buf);