* sr.po: Update.
[official-gcc.git] / libiberty / cplus-dem.c
blobd04c32a904a42bcf1906716e3c0e43aec800d76a
1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2010 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 In addition to the permissions in the GNU Library General Public
15 License, the Free Software Foundation gives you unlimited permission
16 to link the compiled version of this file into combinations with other
17 programs, and to distribute those combinations without any restriction
18 coming from the use of this file. (The Library Public License
19 restrictions do apply in other respects; for example, they cover
20 modification of the file, and distribution when not linked into a
21 combined executable.)
23 Libiberty is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Library General Public License for more details.
28 You should have received a copy of the GNU Library General Public
29 License along with libiberty; see the file COPYING.LIB. If
30 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
31 Boston, MA 02110-1301, USA. */
33 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
35 This file imports xmalloc and xrealloc, which are like malloc and
36 realloc except that they generate a fatal error if there is no
37 available memory. */
39 /* This file lives in both GCC and libiberty. When making changes, please
40 try not to break either. */
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
46 #include "safe-ctype.h"
48 #include <sys/types.h>
49 #include <string.h>
50 #include <stdio.h>
52 #ifdef HAVE_STDLIB_H
53 #include <stdlib.h>
54 #else
55 void * malloc ();
56 void * realloc ();
57 #endif
59 #ifdef HAVE_LIMITS_H
60 #include <limits.h>
61 #endif
62 #ifndef INT_MAX
63 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
64 #endif
66 #include <demangle.h>
67 #undef CURRENT_DEMANGLING_STYLE
68 #define CURRENT_DEMANGLING_STYLE work->options
70 #include "libiberty.h"
72 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
74 /* A value at least one greater than the maximum number of characters
75 that will be output when using the `%d' format with `printf'. */
76 #define INTBUF_SIZE 32
78 extern void fancy_abort (void) ATTRIBUTE_NORETURN;
80 /* In order to allow a single demangler executable to demangle strings
81 using various common values of CPLUS_MARKER, as well as any specific
82 one set at compile time, we maintain a string containing all the
83 commonly used ones, and check to see if the marker we are looking for
84 is in that string. CPLUS_MARKER is usually '$' on systems where the
85 assembler can deal with that. Where the assembler can't, it's usually
86 '.' (but on many systems '.' is used for other things). We put the
87 current defined CPLUS_MARKER first (which defaults to '$'), followed
88 by the next most common value, followed by an explicit '$' in case
89 the value of CPLUS_MARKER is not '$'.
91 We could avoid this if we could just get g++ to tell us what the actual
92 cplus marker character is as part of the debug information, perhaps by
93 ensuring that it is the character that terminates the gcc<n>_compiled
94 marker symbol (FIXME). */
96 #if !defined (CPLUS_MARKER)
97 #define CPLUS_MARKER '$'
98 #endif
100 enum demangling_styles current_demangling_style = auto_demangling;
102 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
104 static char char_str[2] = { '\000', '\000' };
106 void
107 set_cplus_marker_for_demangling (int ch)
109 cplus_markers[0] = ch;
112 typedef struct string /* Beware: these aren't required to be */
113 { /* '\0' terminated. */
114 char *b; /* pointer to start of string */
115 char *p; /* pointer after last character */
116 char *e; /* pointer after end of allocated space */
117 } string;
119 /* Stuff that is shared between sub-routines.
120 Using a shared structure allows cplus_demangle to be reentrant. */
122 struct work_stuff
124 int options;
125 char **typevec;
126 char **ktypevec;
127 char **btypevec;
128 int numk;
129 int numb;
130 int ksize;
131 int bsize;
132 int ntypes;
133 int typevec_size;
134 int constructor;
135 int destructor;
136 int static_type; /* A static member function */
137 int temp_start; /* index in demangled to start of template args */
138 int type_quals; /* The type qualifiers. */
139 int dllimported; /* Symbol imported from a PE DLL */
140 char **tmpl_argvec; /* Template function arguments. */
141 int ntmpl_args; /* The number of template function arguments. */
142 int forgetting_types; /* Nonzero if we are not remembering the types
143 we see. */
144 string* previous_argument; /* The last function argument demangled. */
145 int nrepeats; /* The number of times to repeat the previous
146 argument. */
149 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
150 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
152 static const struct optable
154 const char *const in;
155 const char *const out;
156 const int flags;
157 } optable[] = {
158 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
159 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
160 {"new", " new", 0}, /* old (1.91, and 1.x) */
161 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
162 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
163 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
164 {"as", "=", DMGL_ANSI}, /* ansi */
165 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
166 {"eq", "==", DMGL_ANSI}, /* old, ansi */
167 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
168 {"gt", ">", DMGL_ANSI}, /* old, ansi */
169 {"le", "<=", DMGL_ANSI}, /* old, ansi */
170 {"lt", "<", DMGL_ANSI}, /* old, ansi */
171 {"plus", "+", 0}, /* old */
172 {"pl", "+", DMGL_ANSI}, /* ansi */
173 {"apl", "+=", DMGL_ANSI}, /* ansi */
174 {"minus", "-", 0}, /* old */
175 {"mi", "-", DMGL_ANSI}, /* ansi */
176 {"ami", "-=", DMGL_ANSI}, /* ansi */
177 {"mult", "*", 0}, /* old */
178 {"ml", "*", DMGL_ANSI}, /* ansi */
179 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
180 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
181 {"convert", "+", 0}, /* old (unary +) */
182 {"negate", "-", 0}, /* old (unary -) */
183 {"trunc_mod", "%", 0}, /* old */
184 {"md", "%", DMGL_ANSI}, /* ansi */
185 {"amd", "%=", DMGL_ANSI}, /* ansi */
186 {"trunc_div", "/", 0}, /* old */
187 {"dv", "/", DMGL_ANSI}, /* ansi */
188 {"adv", "/=", DMGL_ANSI}, /* ansi */
189 {"truth_andif", "&&", 0}, /* old */
190 {"aa", "&&", DMGL_ANSI}, /* ansi */
191 {"truth_orif", "||", 0}, /* old */
192 {"oo", "||", DMGL_ANSI}, /* ansi */
193 {"truth_not", "!", 0}, /* old */
194 {"nt", "!", DMGL_ANSI}, /* ansi */
195 {"postincrement","++", 0}, /* old */
196 {"pp", "++", DMGL_ANSI}, /* ansi */
197 {"postdecrement","--", 0}, /* old */
198 {"mm", "--", DMGL_ANSI}, /* ansi */
199 {"bit_ior", "|", 0}, /* old */
200 {"or", "|", DMGL_ANSI}, /* ansi */
201 {"aor", "|=", DMGL_ANSI}, /* ansi */
202 {"bit_xor", "^", 0}, /* old */
203 {"er", "^", DMGL_ANSI}, /* ansi */
204 {"aer", "^=", DMGL_ANSI}, /* ansi */
205 {"bit_and", "&", 0}, /* old */
206 {"ad", "&", DMGL_ANSI}, /* ansi */
207 {"aad", "&=", DMGL_ANSI}, /* ansi */
208 {"bit_not", "~", 0}, /* old */
209 {"co", "~", DMGL_ANSI}, /* ansi */
210 {"call", "()", 0}, /* old */
211 {"cl", "()", DMGL_ANSI}, /* ansi */
212 {"alshift", "<<", 0}, /* old */
213 {"ls", "<<", DMGL_ANSI}, /* ansi */
214 {"als", "<<=", DMGL_ANSI}, /* ansi */
215 {"arshift", ">>", 0}, /* old */
216 {"rs", ">>", DMGL_ANSI}, /* ansi */
217 {"ars", ">>=", DMGL_ANSI}, /* ansi */
218 {"component", "->", 0}, /* old */
219 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
220 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
221 {"indirect", "*", 0}, /* old */
222 {"method_call", "->()", 0}, /* old */
223 {"addr", "&", 0}, /* old (unary &) */
224 {"array", "[]", 0}, /* old */
225 {"vc", "[]", DMGL_ANSI}, /* ansi */
226 {"compound", ", ", 0}, /* old */
227 {"cm", ", ", DMGL_ANSI}, /* ansi */
228 {"cond", "?:", 0}, /* old */
229 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
230 {"max", ">?", 0}, /* old */
231 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
232 {"min", "<?", 0}, /* old */
233 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
234 {"nop", "", 0}, /* old (for operator=) */
235 {"rm", "->*", DMGL_ANSI}, /* ansi */
236 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
239 /* These values are used to indicate the various type varieties.
240 They are all non-zero so that they can be used as `success'
241 values. */
242 typedef enum type_kind_t
244 tk_none,
245 tk_pointer,
246 tk_reference,
247 tk_rvalue_reference,
248 tk_integral,
249 tk_bool,
250 tk_char,
251 tk_real
252 } type_kind_t;
254 const struct demangler_engine libiberty_demanglers[] =
257 NO_DEMANGLING_STYLE_STRING,
258 no_demangling,
259 "Demangling disabled"
263 AUTO_DEMANGLING_STYLE_STRING,
264 auto_demangling,
265 "Automatic selection based on executable"
269 GNU_DEMANGLING_STYLE_STRING,
270 gnu_demangling,
271 "GNU (g++) style demangling"
275 LUCID_DEMANGLING_STYLE_STRING,
276 lucid_demangling,
277 "Lucid (lcc) style demangling"
281 ARM_DEMANGLING_STYLE_STRING,
282 arm_demangling,
283 "ARM style demangling"
287 HP_DEMANGLING_STYLE_STRING,
288 hp_demangling,
289 "HP (aCC) style demangling"
293 EDG_DEMANGLING_STYLE_STRING,
294 edg_demangling,
295 "EDG style demangling"
299 GNU_V3_DEMANGLING_STYLE_STRING,
300 gnu_v3_demangling,
301 "GNU (g++) V3 ABI-style demangling"
305 JAVA_DEMANGLING_STYLE_STRING,
306 java_demangling,
307 "Java style demangling"
311 GNAT_DEMANGLING_STYLE_STRING,
312 gnat_demangling,
313 "GNAT style demangling"
317 DLANG_DEMANGLING_STYLE_STRING,
318 dlang_demangling,
319 "DLANG style demangling"
323 NULL, unknown_demangling, NULL
327 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
328 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
329 string_append(str, " ");}
330 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
332 /* The scope separator appropriate for the language being demangled. */
334 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
336 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
337 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
339 /* Prototypes for local functions */
341 static void delete_work_stuff (struct work_stuff *);
343 static void delete_non_B_K_work_stuff (struct work_stuff *);
345 static char *mop_up (struct work_stuff *, string *, int);
347 static void squangle_mop_up (struct work_stuff *);
349 static void work_stuff_copy_to_from (struct work_stuff *, struct work_stuff *);
351 #if 0
352 static int
353 demangle_method_args (struct work_stuff *, const char **, string *);
354 #endif
356 static char *
357 internal_cplus_demangle (struct work_stuff *, const char *);
359 static int
360 demangle_template_template_parm (struct work_stuff *work,
361 const char **, string *);
363 static int
364 demangle_template (struct work_stuff *work, const char **, string *,
365 string *, int, int);
367 static int
368 arm_pt (struct work_stuff *, const char *, int, const char **,
369 const char **);
371 static int
372 demangle_class_name (struct work_stuff *, const char **, string *);
374 static int
375 demangle_qualified (struct work_stuff *, const char **, string *,
376 int, int);
378 static int demangle_class (struct work_stuff *, const char **, string *);
380 static int demangle_fund_type (struct work_stuff *, const char **, string *);
382 static int demangle_signature (struct work_stuff *, const char **, string *);
384 static int demangle_prefix (struct work_stuff *, const char **, string *);
386 static int gnu_special (struct work_stuff *, const char **, string *);
388 static int arm_special (const char **, string *);
390 static void string_need (string *, int);
392 static void string_delete (string *);
394 static void
395 string_init (string *);
397 static void string_clear (string *);
399 #if 0
400 static int string_empty (string *);
401 #endif
403 static void string_append (string *, const char *);
405 static void string_appends (string *, string *);
407 static void string_appendn (string *, const char *, int);
409 static void string_prepend (string *, const char *);
411 static void string_prependn (string *, const char *, int);
413 static void string_append_template_idx (string *, int);
415 static int get_count (const char **, int *);
417 static int consume_count (const char **);
419 static int consume_count_with_underscores (const char**);
421 static int demangle_args (struct work_stuff *, const char **, string *);
423 static int demangle_nested_args (struct work_stuff*, const char**, string*);
425 static int do_type (struct work_stuff *, const char **, string *);
427 static int do_arg (struct work_stuff *, const char **, string *);
429 static int
430 demangle_function_name (struct work_stuff *, const char **, string *,
431 const char *);
433 static int
434 iterate_demangle_function (struct work_stuff *,
435 const char **, string *, const char *);
437 static void remember_type (struct work_stuff *, const char *, int);
439 static void remember_Btype (struct work_stuff *, const char *, int, int);
441 static int register_Btype (struct work_stuff *);
443 static void remember_Ktype (struct work_stuff *, const char *, int);
445 static void forget_types (struct work_stuff *);
447 static void forget_B_and_K_types (struct work_stuff *);
449 static void string_prepends (string *, string *);
451 static int
452 demangle_template_value_parm (struct work_stuff*, const char**,
453 string*, type_kind_t);
455 static int
456 do_hpacc_template_const_value (struct work_stuff *, const char **, string *);
458 static int
459 do_hpacc_template_literal (struct work_stuff *, const char **, string *);
461 static int snarf_numeric_literal (const char **, string *);
463 /* There is a TYPE_QUAL value for each type qualifier. They can be
464 combined by bitwise-or to form the complete set of qualifiers for a
465 type. */
467 #define TYPE_UNQUALIFIED 0x0
468 #define TYPE_QUAL_CONST 0x1
469 #define TYPE_QUAL_VOLATILE 0x2
470 #define TYPE_QUAL_RESTRICT 0x4
472 static int code_for_qualifier (int);
474 static const char* qualifier_string (int);
476 static const char* demangle_qualifier (int);
478 static int demangle_expression (struct work_stuff *, const char **, string *,
479 type_kind_t);
481 static int
482 demangle_integral_value (struct work_stuff *, const char **, string *);
484 static int
485 demangle_real_value (struct work_stuff *, const char **, string *);
487 static void
488 demangle_arm_hp_template (struct work_stuff *, const char **, int, string *);
490 static void
491 recursively_demangle (struct work_stuff *, const char **, string *, int);
493 /* Translate count to integer, consuming tokens in the process.
494 Conversion terminates on the first non-digit character.
496 Trying to consume something that isn't a count results in no
497 consumption of input and a return of -1.
499 Overflow consumes the rest of the digits, and returns -1. */
501 static int
502 consume_count (const char **type)
504 int count = 0;
506 if (! ISDIGIT ((unsigned char)**type))
507 return -1;
509 while (ISDIGIT ((unsigned char)**type))
511 count *= 10;
513 /* Check for overflow.
514 We assume that count is represented using two's-complement;
515 no power of two is divisible by ten, so if an overflow occurs
516 when multiplying by ten, the result will not be a multiple of
517 ten. */
518 if ((count % 10) != 0)
520 while (ISDIGIT ((unsigned char) **type))
521 (*type)++;
522 return -1;
525 count += **type - '0';
526 (*type)++;
529 if (count < 0)
530 count = -1;
532 return (count);
536 /* Like consume_count, but for counts that are preceded and followed
537 by '_' if they are greater than 10. Also, -1 is returned for
538 failure, since 0 can be a valid value. */
540 static int
541 consume_count_with_underscores (const char **mangled)
543 int idx;
545 if (**mangled == '_')
547 (*mangled)++;
548 if (!ISDIGIT ((unsigned char)**mangled))
549 return -1;
551 idx = consume_count (mangled);
552 if (**mangled != '_')
553 /* The trailing underscore was missing. */
554 return -1;
556 (*mangled)++;
558 else
560 if (**mangled < '0' || **mangled > '9')
561 return -1;
563 idx = **mangled - '0';
564 (*mangled)++;
567 return idx;
570 /* C is the code for a type-qualifier. Return the TYPE_QUAL
571 corresponding to this qualifier. */
573 static int
574 code_for_qualifier (int c)
576 switch (c)
578 case 'C':
579 return TYPE_QUAL_CONST;
581 case 'V':
582 return TYPE_QUAL_VOLATILE;
584 case 'u':
585 return TYPE_QUAL_RESTRICT;
587 default:
588 break;
591 /* C was an invalid qualifier. */
592 abort ();
595 /* Return the string corresponding to the qualifiers given by
596 TYPE_QUALS. */
598 static const char*
599 qualifier_string (int type_quals)
601 switch (type_quals)
603 case TYPE_UNQUALIFIED:
604 return "";
606 case TYPE_QUAL_CONST:
607 return "const";
609 case TYPE_QUAL_VOLATILE:
610 return "volatile";
612 case TYPE_QUAL_RESTRICT:
613 return "__restrict";
615 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
616 return "const volatile";
618 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
619 return "const __restrict";
621 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
622 return "volatile __restrict";
624 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
625 return "const volatile __restrict";
627 default:
628 break;
631 /* TYPE_QUALS was an invalid qualifier set. */
632 abort ();
635 /* C is the code for a type-qualifier. Return the string
636 corresponding to this qualifier. This function should only be
637 called with a valid qualifier code. */
639 static const char*
640 demangle_qualifier (int c)
642 return qualifier_string (code_for_qualifier (c));
646 cplus_demangle_opname (const char *opname, char *result, int options)
648 int len, len1, ret;
649 string type;
650 struct work_stuff work[1];
651 const char *tem;
653 len = strlen(opname);
654 result[0] = '\0';
655 ret = 0;
656 memset ((char *) work, 0, sizeof (work));
657 work->options = options;
659 if (opname[0] == '_' && opname[1] == '_'
660 && opname[2] == 'o' && opname[3] == 'p')
662 /* ANSI. */
663 /* type conversion operator. */
664 tem = opname + 4;
665 if (do_type (work, &tem, &type))
667 strcat (result, "operator ");
668 strncat (result, type.b, type.p - type.b);
669 string_delete (&type);
670 ret = 1;
673 else if (opname[0] == '_' && opname[1] == '_'
674 && ISLOWER((unsigned char)opname[2])
675 && ISLOWER((unsigned char)opname[3]))
677 if (opname[4] == '\0')
679 /* Operator. */
680 size_t i;
681 for (i = 0; i < ARRAY_SIZE (optable); i++)
683 if (strlen (optable[i].in) == 2
684 && memcmp (optable[i].in, opname + 2, 2) == 0)
686 strcat (result, "operator");
687 strcat (result, optable[i].out);
688 ret = 1;
689 break;
693 else
695 if (opname[2] == 'a' && opname[5] == '\0')
697 /* Assignment. */
698 size_t i;
699 for (i = 0; i < ARRAY_SIZE (optable); i++)
701 if (strlen (optable[i].in) == 3
702 && memcmp (optable[i].in, opname + 2, 3) == 0)
704 strcat (result, "operator");
705 strcat (result, optable[i].out);
706 ret = 1;
707 break;
713 else if (len >= 3
714 && opname[0] == 'o'
715 && opname[1] == 'p'
716 && strchr (cplus_markers, opname[2]) != NULL)
718 /* see if it's an assignment expression */
719 if (len >= 10 /* op$assign_ */
720 && memcmp (opname + 3, "assign_", 7) == 0)
722 size_t i;
723 for (i = 0; i < ARRAY_SIZE (optable); i++)
725 len1 = len - 10;
726 if ((int) strlen (optable[i].in) == len1
727 && memcmp (optable[i].in, opname + 10, len1) == 0)
729 strcat (result, "operator");
730 strcat (result, optable[i].out);
731 strcat (result, "=");
732 ret = 1;
733 break;
737 else
739 size_t i;
740 for (i = 0; i < ARRAY_SIZE (optable); i++)
742 len1 = len - 3;
743 if ((int) strlen (optable[i].in) == len1
744 && memcmp (optable[i].in, opname + 3, len1) == 0)
746 strcat (result, "operator");
747 strcat (result, optable[i].out);
748 ret = 1;
749 break;
754 else if (len >= 5 && memcmp (opname, "type", 4) == 0
755 && strchr (cplus_markers, opname[4]) != NULL)
757 /* type conversion operator */
758 tem = opname + 5;
759 if (do_type (work, &tem, &type))
761 strcat (result, "operator ");
762 strncat (result, type.b, type.p - type.b);
763 string_delete (&type);
764 ret = 1;
767 squangle_mop_up (work);
768 return ret;
772 /* Takes operator name as e.g. "++" and returns mangled
773 operator name (e.g. "postincrement_expr"), or NULL if not found.
775 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
776 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
778 const char *
779 cplus_mangle_opname (const char *opname, int options)
781 size_t i;
782 int len;
784 len = strlen (opname);
785 for (i = 0; i < ARRAY_SIZE (optable); i++)
787 if ((int) strlen (optable[i].out) == len
788 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
789 && memcmp (optable[i].out, opname, len) == 0)
790 return optable[i].in;
792 return (0);
795 /* Add a routine to set the demangling style to be sure it is valid and
796 allow for any demangler initialization that maybe necessary. */
798 enum demangling_styles
799 cplus_demangle_set_style (enum demangling_styles style)
801 const struct demangler_engine *demangler = libiberty_demanglers;
803 for (; demangler->demangling_style != unknown_demangling; ++demangler)
804 if (style == demangler->demangling_style)
806 current_demangling_style = style;
807 return current_demangling_style;
810 return unknown_demangling;
813 /* Do string name to style translation */
815 enum demangling_styles
816 cplus_demangle_name_to_style (const char *name)
818 const struct demangler_engine *demangler = libiberty_demanglers;
820 for (; demangler->demangling_style != unknown_demangling; ++demangler)
821 if (strcmp (name, demangler->demangling_style_name) == 0)
822 return demangler->demangling_style;
824 return unknown_demangling;
827 /* char *cplus_demangle (const char *mangled, int options)
829 If MANGLED is a mangled function name produced by GNU C++, then
830 a pointer to a @code{malloc}ed string giving a C++ representation
831 of the name will be returned; otherwise NULL will be returned.
832 It is the caller's responsibility to free the string which
833 is returned.
835 The OPTIONS arg may contain one or more of the following bits:
837 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
838 included.
839 DMGL_PARAMS Function parameters are included.
841 For example,
843 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
844 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
845 cplus_demangle ("foo__1Ai", 0) => "A::foo"
847 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
848 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
849 cplus_demangle ("foo__1Afe", 0) => "A::foo"
851 Note that any leading underscores, or other such characters prepended by
852 the compilation system, are presumed to have already been stripped from
853 MANGLED. */
855 char *
856 cplus_demangle (const char *mangled, int options)
858 char *ret;
859 struct work_stuff work[1];
861 if (current_demangling_style == no_demangling)
862 return xstrdup (mangled);
864 memset ((char *) work, 0, sizeof (work));
865 work->options = options;
866 if ((work->options & DMGL_STYLE_MASK) == 0)
867 work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
869 /* The V3 ABI demangling is implemented elsewhere. */
870 if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
872 ret = cplus_demangle_v3 (mangled, work->options);
873 if (ret || GNU_V3_DEMANGLING)
874 return ret;
877 if (JAVA_DEMANGLING)
879 ret = java_demangle_v3 (mangled);
880 if (ret)
881 return ret;
884 if (GNAT_DEMANGLING)
885 return ada_demangle (mangled, options);
887 if (DLANG_DEMANGLING)
889 ret = dlang_demangle (mangled, options);
890 if (ret)
891 return ret;
894 ret = internal_cplus_demangle (work, mangled);
895 squangle_mop_up (work);
896 return (ret);
899 /* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
901 char *
902 ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
904 int len0;
905 const char* p;
906 char *d;
907 char *demangled;
909 /* Discard leading _ada_, which is used for library level subprograms. */
910 if (strncmp (mangled, "_ada_", 5) == 0)
911 mangled += 5;
913 /* All ada unit names are lower-case. */
914 if (!ISLOWER (mangled[0]))
915 goto unknown;
917 /* Most of the demangling will trivially remove chars. Operator names
918 may add one char but because they are always preceeded by '__' which is
919 replaced by '.', they eventually never expand the size.
920 A few special names such as '___elabs' add a few chars (at most 7), but
921 they occur only once. */
922 len0 = strlen (mangled) + 7 + 1;
923 demangled = XNEWVEC (char, len0);
925 d = demangled;
926 p = mangled;
927 while (1)
929 /* An entity names is expected. */
930 if (ISLOWER (*p))
932 /* An identifier, which is always lower case. */
934 *d++ = *p++;
935 while (ISLOWER(*p) || ISDIGIT (*p)
936 || (p[0] == '_' && (ISLOWER (p[1]) || ISDIGIT (p[1]))));
938 else if (p[0] == 'O')
940 /* An operator name. */
941 static const char * const operators[][2] =
942 {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
943 {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
944 {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
945 {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
946 {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
947 {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
948 {"Oexpon", "**"}, {NULL, NULL}};
949 int k;
951 for (k = 0; operators[k][0] != NULL; k++)
953 size_t slen = strlen (operators[k][0]);
954 if (strncmp (p, operators[k][0], slen) == 0)
956 p += slen;
957 slen = strlen (operators[k][1]);
958 *d++ = '"';
959 memcpy (d, operators[k][1], slen);
960 d += slen;
961 *d++ = '"';
962 break;
965 /* Operator not found. */
966 if (operators[k][0] == NULL)
967 goto unknown;
969 else
971 /* Not a GNAT encoding. */
972 goto unknown;
975 /* The name can be directly followed by some uppercase letters. */
976 if (p[0] == 'T' && p[1] == 'K')
978 /* Task stuff. */
979 if (p[2] == 'B' && p[3] == 0)
981 /* Subprogram for task body. */
982 break;
984 else if (p[2] == '_' && p[3] == '_')
986 /* Inner declarations in a task. */
987 p += 4;
988 *d++ = '.';
989 continue;
991 else
992 goto unknown;
994 if (p[0] == 'E' && p[1] == 0)
996 /* Exception name. */
997 goto unknown;
999 if ((p[0] == 'P' || p[0] == 'N') && p[1] == 0)
1001 /* Protected type subprogram. */
1002 break;
1004 if ((*p == 'N' || *p == 'S') && p[1] == 0)
1006 /* Enumerated type name table. */
1007 goto unknown;
1009 if (p[0] == 'X')
1011 /* Body nested. */
1012 p++;
1013 while (p[0] == 'n' || p[0] == 'b')
1014 p++;
1016 if (p[0] == 'S' && p[1] != 0 && (p[2] == '_' || p[2] == 0))
1018 /* Stream operations. */
1019 const char *name;
1020 switch (p[1])
1022 case 'R':
1023 name = "'Read";
1024 break;
1025 case 'W':
1026 name = "'Write";
1027 break;
1028 case 'I':
1029 name = "'Input";
1030 break;
1031 case 'O':
1032 name = "'Output";
1033 break;
1034 default:
1035 goto unknown;
1037 p += 2;
1038 strcpy (d, name);
1039 d += strlen (name);
1041 else if (p[0] == 'D')
1043 /* Controlled type operation. */
1044 const char *name;
1045 switch (p[1])
1047 case 'F':
1048 name = ".Finalize";
1049 break;
1050 case 'A':
1051 name = ".Adjust";
1052 break;
1053 default:
1054 goto unknown;
1056 strcpy (d, name);
1057 d += strlen (name);
1058 break;
1061 if (p[0] == '_')
1063 /* Separator. */
1064 if (p[1] == '_')
1066 /* Standard separator. Handled first. */
1067 p += 2;
1069 if (ISDIGIT (*p))
1071 /* Overloading number. */
1073 p++;
1074 while (ISDIGIT (*p) || (p[0] == '_' && ISDIGIT (p[1])));
1075 if (*p == 'X')
1077 p++;
1078 while (p[0] == 'n' || p[0] == 'b')
1079 p++;
1082 else if (p[0] == '_' && p[1] != '_')
1084 /* Special names. */
1085 static const char * const special[][2] = {
1086 { "_elabb", "'Elab_Body" },
1087 { "_elabs", "'Elab_Spec" },
1088 { "_size", "'Size" },
1089 { "_alignment", "'Alignment" },
1090 { "_assign", ".\":=\"" },
1091 { NULL, NULL }
1093 int k;
1095 for (k = 0; special[k][0] != NULL; k++)
1097 size_t slen = strlen (special[k][0]);
1098 if (strncmp (p, special[k][0], slen) == 0)
1100 p += slen;
1101 slen = strlen (special[k][1]);
1102 memcpy (d, special[k][1], slen);
1103 d += slen;
1104 break;
1107 if (special[k][0] != NULL)
1108 break;
1109 else
1110 goto unknown;
1112 else
1114 *d++ = '.';
1115 continue;
1118 else if (p[1] == 'B' || p[1] == 'E')
1120 /* Entry Body or barrier Evaluation. */
1121 p += 2;
1122 while (ISDIGIT (*p))
1123 p++;
1124 if (p[0] == 's' && p[1] == 0)
1125 break;
1126 else
1127 goto unknown;
1129 else
1130 goto unknown;
1133 if (p[0] == '.' && ISDIGIT (p[1]))
1135 /* Nested subprogram. */
1136 p += 2;
1137 while (ISDIGIT (*p))
1138 p++;
1140 if (*p == 0)
1142 /* End of mangled name. */
1143 break;
1145 else
1146 goto unknown;
1148 *d = 0;
1149 return demangled;
1151 unknown:
1152 len0 = strlen (mangled);
1153 demangled = XNEWVEC (char, len0 + 3);
1155 if (mangled[0] == '<')
1156 strcpy (demangled, mangled);
1157 else
1158 sprintf (demangled, "<%s>", mangled);
1160 return demangled;
1163 /* This function performs most of what cplus_demangle use to do, but
1164 to be able to demangle a name with a B, K or n code, we need to
1165 have a longer term memory of what types have been seen. The original
1166 now initializes and cleans up the squangle code info, while internal
1167 calls go directly to this routine to avoid resetting that info. */
1169 static char *
1170 internal_cplus_demangle (struct work_stuff *work, const char *mangled)
1173 string decl;
1174 int success = 0;
1175 char *demangled = NULL;
1176 int s1, s2, s3, s4;
1177 s1 = work->constructor;
1178 s2 = work->destructor;
1179 s3 = work->static_type;
1180 s4 = work->type_quals;
1181 work->constructor = work->destructor = 0;
1182 work->type_quals = TYPE_UNQUALIFIED;
1183 work->dllimported = 0;
1185 if ((mangled != NULL) && (*mangled != '\0'))
1187 string_init (&decl);
1189 /* First check to see if gnu style demangling is active and if the
1190 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1191 recognize one of the gnu special forms rather than looking for a
1192 standard prefix. In particular, don't worry about whether there
1193 is a "__" string in the mangled string. Consider "_$_5__foo" for
1194 example. */
1196 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
1198 success = gnu_special (work, &mangled, &decl);
1199 if (!success)
1201 delete_work_stuff (work);
1202 string_delete (&decl);
1205 if (!success)
1207 success = demangle_prefix (work, &mangled, &decl);
1209 if (success && (*mangled != '\0'))
1211 success = demangle_signature (work, &mangled, &decl);
1213 if (work->constructor == 2)
1215 string_prepend (&decl, "global constructors keyed to ");
1216 work->constructor = 0;
1218 else if (work->destructor == 2)
1220 string_prepend (&decl, "global destructors keyed to ");
1221 work->destructor = 0;
1223 else if (work->dllimported == 1)
1225 string_prepend (&decl, "import stub for ");
1226 work->dllimported = 0;
1228 demangled = mop_up (work, &decl, success);
1230 work->constructor = s1;
1231 work->destructor = s2;
1232 work->static_type = s3;
1233 work->type_quals = s4;
1234 return demangled;
1238 /* Clear out and squangling related storage */
1239 static void
1240 squangle_mop_up (struct work_stuff *work)
1242 /* clean up the B and K type mangling types. */
1243 forget_B_and_K_types (work);
1244 if (work -> btypevec != NULL)
1246 free ((char *) work -> btypevec);
1247 work->btypevec = NULL;
1248 work->bsize = 0;
1250 if (work -> ktypevec != NULL)
1252 free ((char *) work -> ktypevec);
1253 work->ktypevec = NULL;
1254 work->ksize = 0;
1259 /* Copy the work state and storage. */
1261 static void
1262 work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from)
1264 int i;
1266 delete_work_stuff (to);
1268 /* Shallow-copy scalars. */
1269 memcpy (to, from, sizeof (*to));
1271 /* Deep-copy dynamic storage. */
1272 if (from->typevec_size)
1273 to->typevec = XNEWVEC (char *, from->typevec_size);
1275 for (i = 0; i < from->ntypes; i++)
1277 int len = strlen (from->typevec[i]) + 1;
1279 to->typevec[i] = XNEWVEC (char, len);
1280 memcpy (to->typevec[i], from->typevec[i], len);
1283 if (from->ksize)
1284 to->ktypevec = XNEWVEC (char *, from->ksize);
1286 for (i = 0; i < from->numk; i++)
1288 int len = strlen (from->ktypevec[i]) + 1;
1290 to->ktypevec[i] = XNEWVEC (char, len);
1291 memcpy (to->ktypevec[i], from->ktypevec[i], len);
1294 if (from->bsize)
1295 to->btypevec = XNEWVEC (char *, from->bsize);
1297 for (i = 0; i < from->numb; i++)
1299 int len = strlen (from->btypevec[i]) + 1;
1301 to->btypevec[i] = XNEWVEC (char , len);
1302 memcpy (to->btypevec[i], from->btypevec[i], len);
1305 if (from->ntmpl_args)
1306 to->tmpl_argvec = XNEWVEC (char *, from->ntmpl_args);
1308 for (i = 0; i < from->ntmpl_args; i++)
1310 int len = strlen (from->tmpl_argvec[i]) + 1;
1312 to->tmpl_argvec[i] = XNEWVEC (char, len);
1313 memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
1316 if (from->previous_argument)
1318 to->previous_argument = XNEW (string);
1319 string_init (to->previous_argument);
1320 string_appends (to->previous_argument, from->previous_argument);
1325 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1327 static void
1328 delete_non_B_K_work_stuff (struct work_stuff *work)
1330 /* Discard the remembered types, if any. */
1332 forget_types (work);
1333 if (work -> typevec != NULL)
1335 free ((char *) work -> typevec);
1336 work -> typevec = NULL;
1337 work -> typevec_size = 0;
1339 if (work->tmpl_argvec)
1341 int i;
1343 for (i = 0; i < work->ntmpl_args; i++)
1344 free ((char*) work->tmpl_argvec[i]);
1346 free ((char*) work->tmpl_argvec);
1347 work->tmpl_argvec = NULL;
1349 if (work->previous_argument)
1351 string_delete (work->previous_argument);
1352 free ((char*) work->previous_argument);
1353 work->previous_argument = NULL;
1358 /* Delete all dynamic storage in work_stuff. */
1359 static void
1360 delete_work_stuff (struct work_stuff *work)
1362 delete_non_B_K_work_stuff (work);
1363 squangle_mop_up (work);
1367 /* Clear out any mangled storage */
1369 static char *
1370 mop_up (struct work_stuff *work, string *declp, int success)
1372 char *demangled = NULL;
1374 delete_non_B_K_work_stuff (work);
1376 /* If demangling was successful, ensure that the demangled string is null
1377 terminated and return it. Otherwise, free the demangling decl. */
1379 if (!success)
1381 string_delete (declp);
1383 else
1385 string_appendn (declp, "", 1);
1386 demangled = declp->b;
1388 return (demangled);
1393 LOCAL FUNCTION
1395 demangle_signature -- demangle the signature part of a mangled name
1397 SYNOPSIS
1399 static int
1400 demangle_signature (struct work_stuff *work, const char **mangled,
1401 string *declp);
1403 DESCRIPTION
1405 Consume and demangle the signature portion of the mangled name.
1407 DECLP is the string where demangled output is being built. At
1408 entry it contains the demangled root name from the mangled name
1409 prefix. I.E. either a demangled operator name or the root function
1410 name. In some special cases, it may contain nothing.
1412 *MANGLED points to the current unconsumed location in the mangled
1413 name. As tokens are consumed and demangling is performed, the
1414 pointer is updated to continuously point at the next token to
1415 be consumed.
1417 Demangling GNU style mangled names is nasty because there is no
1418 explicit token that marks the start of the outermost function
1419 argument list. */
1421 static int
1422 demangle_signature (struct work_stuff *work,
1423 const char **mangled, string *declp)
1425 int success = 1;
1426 int func_done = 0;
1427 int expect_func = 0;
1428 int expect_return_type = 0;
1429 const char *oldmangled = NULL;
1430 string trawname;
1431 string tname;
1433 while (success && (**mangled != '\0'))
1435 switch (**mangled)
1437 case 'Q':
1438 oldmangled = *mangled;
1439 success = demangle_qualified (work, mangled, declp, 1, 0);
1440 if (success)
1441 remember_type (work, oldmangled, *mangled - oldmangled);
1442 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1443 expect_func = 1;
1444 oldmangled = NULL;
1445 break;
1447 case 'K':
1448 oldmangled = *mangled;
1449 success = demangle_qualified (work, mangled, declp, 1, 0);
1450 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1452 expect_func = 1;
1454 oldmangled = NULL;
1455 break;
1457 case 'S':
1458 /* Static member function */
1459 if (oldmangled == NULL)
1461 oldmangled = *mangled;
1463 (*mangled)++;
1464 work -> static_type = 1;
1465 break;
1467 case 'C':
1468 case 'V':
1469 case 'u':
1470 work->type_quals |= code_for_qualifier (**mangled);
1472 /* a qualified member function */
1473 if (oldmangled == NULL)
1474 oldmangled = *mangled;
1475 (*mangled)++;
1476 break;
1478 case 'L':
1479 /* Local class name follows after "Lnnn_" */
1480 if (HP_DEMANGLING)
1482 while (**mangled && (**mangled != '_'))
1483 (*mangled)++;
1484 if (!**mangled)
1485 success = 0;
1486 else
1487 (*mangled)++;
1489 else
1490 success = 0;
1491 break;
1493 case '0': case '1': case '2': case '3': case '4':
1494 case '5': case '6': case '7': case '8': case '9':
1495 if (oldmangled == NULL)
1497 oldmangled = *mangled;
1499 work->temp_start = -1; /* uppermost call to demangle_class */
1500 success = demangle_class (work, mangled, declp);
1501 if (success)
1503 remember_type (work, oldmangled, *mangled - oldmangled);
1505 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1507 /* EDG and others will have the "F", so we let the loop cycle
1508 if we are looking at one. */
1509 if (**mangled != 'F')
1510 expect_func = 1;
1512 oldmangled = NULL;
1513 break;
1515 case 'B':
1517 string s;
1518 success = do_type (work, mangled, &s);
1519 if (success)
1521 string_append (&s, SCOPE_STRING (work));
1522 string_prepends (declp, &s);
1523 string_delete (&s);
1525 oldmangled = NULL;
1526 expect_func = 1;
1528 break;
1530 case 'F':
1531 /* Function */
1532 /* ARM/HP style demangling includes a specific 'F' character after
1533 the class name. For GNU style, it is just implied. So we can
1534 safely just consume any 'F' at this point and be compatible
1535 with either style. */
1537 oldmangled = NULL;
1538 func_done = 1;
1539 (*mangled)++;
1541 /* For lucid/ARM/HP style we have to forget any types we might
1542 have remembered up to this point, since they were not argument
1543 types. GNU style considers all types seen as available for
1544 back references. See comment in demangle_args() */
1546 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1548 forget_types (work);
1550 success = demangle_args (work, mangled, declp);
1551 /* After picking off the function args, we expect to either
1552 find the function return type (preceded by an '_') or the
1553 end of the string. */
1554 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1556 ++(*mangled);
1557 /* At this level, we do not care about the return type. */
1558 success = do_type (work, mangled, &tname);
1559 string_delete (&tname);
1562 break;
1564 case 't':
1565 /* G++ Template */
1566 string_init(&trawname);
1567 string_init(&tname);
1568 if (oldmangled == NULL)
1570 oldmangled = *mangled;
1572 success = demangle_template (work, mangled, &tname,
1573 &trawname, 1, 1);
1574 if (success)
1576 remember_type (work, oldmangled, *mangled - oldmangled);
1578 string_append (&tname, SCOPE_STRING (work));
1580 string_prepends(declp, &tname);
1581 if (work -> destructor & 1)
1583 string_prepend (&trawname, "~");
1584 string_appends (declp, &trawname);
1585 work->destructor -= 1;
1587 if ((work->constructor & 1) || (work->destructor & 1))
1589 string_appends (declp, &trawname);
1590 work->constructor -= 1;
1592 string_delete(&trawname);
1593 string_delete(&tname);
1594 oldmangled = NULL;
1595 expect_func = 1;
1596 break;
1598 case '_':
1599 if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
1601 /* Read the return type. */
1602 string return_type;
1604 (*mangled)++;
1605 success = do_type (work, mangled, &return_type);
1606 APPEND_BLANK (&return_type);
1608 string_prepends (declp, &return_type);
1609 string_delete (&return_type);
1610 break;
1612 else
1613 /* At the outermost level, we cannot have a return type specified,
1614 so if we run into another '_' at this point we are dealing with
1615 a mangled name that is either bogus, or has been mangled by
1616 some algorithm we don't know how to deal with. So just
1617 reject the entire demangling. */
1618 /* However, "_nnn" is an expected suffix for alternate entry point
1619 numbered nnn for a function, with HP aCC, so skip over that
1620 without reporting failure. pai/1997-09-04 */
1621 if (HP_DEMANGLING)
1623 (*mangled)++;
1624 while (**mangled && ISDIGIT ((unsigned char)**mangled))
1625 (*mangled)++;
1627 else
1628 success = 0;
1629 break;
1631 case 'H':
1632 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1634 /* A G++ template function. Read the template arguments. */
1635 success = demangle_template (work, mangled, declp, 0, 0,
1637 if (!(work->constructor & 1))
1638 expect_return_type = 1;
1639 (*mangled)++;
1640 break;
1642 else
1643 /* fall through */
1646 default:
1647 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1649 /* Assume we have stumbled onto the first outermost function
1650 argument token, and start processing args. */
1651 func_done = 1;
1652 success = demangle_args (work, mangled, declp);
1654 else
1656 /* Non-GNU demanglers use a specific token to mark the start
1657 of the outermost function argument tokens. Typically 'F',
1658 for ARM/HP-demangling, for example. So if we find something
1659 we are not prepared for, it must be an error. */
1660 success = 0;
1662 break;
1665 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1668 if (success && expect_func)
1670 func_done = 1;
1671 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1673 forget_types (work);
1675 success = demangle_args (work, mangled, declp);
1676 /* Since template include the mangling of their return types,
1677 we must set expect_func to 0 so that we don't try do
1678 demangle more arguments the next time we get here. */
1679 expect_func = 0;
1683 if (success && !func_done)
1685 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1687 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1688 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1689 first case, and need to ensure that the '(void)' gets added to
1690 the current declp. Note that with ARM/HP, the first case
1691 represents the name of a static data member 'foo::bar',
1692 which is in the current declp, so we leave it alone. */
1693 success = demangle_args (work, mangled, declp);
1696 if (success && PRINT_ARG_TYPES)
1698 if (work->static_type)
1699 string_append (declp, " static");
1700 if (work->type_quals != TYPE_UNQUALIFIED)
1702 APPEND_BLANK (declp);
1703 string_append (declp, qualifier_string (work->type_quals));
1707 return (success);
1710 #if 0
1712 static int
1713 demangle_method_args (struct work_stuff *work, const char **mangled,
1714 string *declp)
1716 int success = 0;
1718 if (work -> static_type)
1720 string_append (declp, *mangled + 1);
1721 *mangled += strlen (*mangled);
1722 success = 1;
1724 else
1726 success = demangle_args (work, mangled, declp);
1728 return (success);
1731 #endif
1733 static int
1734 demangle_template_template_parm (struct work_stuff *work,
1735 const char **mangled, string *tname)
1737 int i;
1738 int r;
1739 int need_comma = 0;
1740 int success = 1;
1741 string temp;
1743 string_append (tname, "template <");
1744 /* get size of template parameter list */
1745 if (get_count (mangled, &r))
1747 for (i = 0; i < r; i++)
1749 if (need_comma)
1751 string_append (tname, ", ");
1754 /* Z for type parameters */
1755 if (**mangled == 'Z')
1757 (*mangled)++;
1758 string_append (tname, "class");
1760 /* z for template parameters */
1761 else if (**mangled == 'z')
1763 (*mangled)++;
1764 success =
1765 demangle_template_template_parm (work, mangled, tname);
1766 if (!success)
1768 break;
1771 else
1773 /* temp is initialized in do_type */
1774 success = do_type (work, mangled, &temp);
1775 if (success)
1777 string_appends (tname, &temp);
1779 string_delete(&temp);
1780 if (!success)
1782 break;
1785 need_comma = 1;
1789 if (tname->p[-1] == '>')
1790 string_append (tname, " ");
1791 string_append (tname, "> class");
1792 return (success);
1795 static int
1796 demangle_expression (struct work_stuff *work, const char **mangled,
1797 string *s, type_kind_t tk)
1799 int need_operator = 0;
1800 int success;
1802 success = 1;
1803 string_appendn (s, "(", 1);
1804 (*mangled)++;
1805 while (success && **mangled != 'W' && **mangled != '\0')
1807 if (need_operator)
1809 size_t i;
1810 size_t len;
1812 success = 0;
1814 len = strlen (*mangled);
1816 for (i = 0; i < ARRAY_SIZE (optable); ++i)
1818 size_t l = strlen (optable[i].in);
1820 if (l <= len
1821 && memcmp (optable[i].in, *mangled, l) == 0)
1823 string_appendn (s, " ", 1);
1824 string_append (s, optable[i].out);
1825 string_appendn (s, " ", 1);
1826 success = 1;
1827 (*mangled) += l;
1828 break;
1832 if (!success)
1833 break;
1835 else
1836 need_operator = 1;
1838 success = demangle_template_value_parm (work, mangled, s, tk);
1841 if (**mangled != 'W')
1842 success = 0;
1843 else
1845 string_appendn (s, ")", 1);
1846 (*mangled)++;
1849 return success;
1852 static int
1853 demangle_integral_value (struct work_stuff *work,
1854 const char **mangled, string *s)
1856 int success;
1858 if (**mangled == 'E')
1859 success = demangle_expression (work, mangled, s, tk_integral);
1860 else if (**mangled == 'Q' || **mangled == 'K')
1861 success = demangle_qualified (work, mangled, s, 0, 1);
1862 else
1864 int value;
1866 /* By default, we let the number decide whether we shall consume an
1867 underscore. */
1868 int multidigit_without_leading_underscore = 0;
1869 int leave_following_underscore = 0;
1871 success = 0;
1873 if (**mangled == '_')
1875 if (mangled[0][1] == 'm')
1877 /* Since consume_count_with_underscores does not handle the
1878 `m'-prefix we must do it here, using consume_count and
1879 adjusting underscores: we have to consume the underscore
1880 matching the prepended one. */
1881 multidigit_without_leading_underscore = 1;
1882 string_appendn (s, "-", 1);
1883 (*mangled) += 2;
1885 else
1887 /* Do not consume a following underscore;
1888 consume_count_with_underscores will consume what
1889 should be consumed. */
1890 leave_following_underscore = 1;
1893 else
1895 /* Negative numbers are indicated with a leading `m'. */
1896 if (**mangled == 'm')
1898 string_appendn (s, "-", 1);
1899 (*mangled)++;
1901 /* Since consume_count_with_underscores does not handle
1902 multi-digit numbers that do not start with an underscore,
1903 and this number can be an integer template parameter,
1904 we have to call consume_count. */
1905 multidigit_without_leading_underscore = 1;
1906 /* These multi-digit numbers never end on an underscore,
1907 so if there is one then don't eat it. */
1908 leave_following_underscore = 1;
1911 /* We must call consume_count if we expect to remove a trailing
1912 underscore, since consume_count_with_underscores expects
1913 the leading underscore (that we consumed) if it is to handle
1914 multi-digit numbers. */
1915 if (multidigit_without_leading_underscore)
1916 value = consume_count (mangled);
1917 else
1918 value = consume_count_with_underscores (mangled);
1920 if (value != -1)
1922 char buf[INTBUF_SIZE];
1923 sprintf (buf, "%d", value);
1924 string_append (s, buf);
1926 /* Numbers not otherwise delimited, might have an underscore
1927 appended as a delimeter, which we should skip.
1929 ??? This used to always remove a following underscore, which
1930 is wrong. If other (arbitrary) cases are followed by an
1931 underscore, we need to do something more radical. */
1933 if ((value > 9 || multidigit_without_leading_underscore)
1934 && ! leave_following_underscore
1935 && **mangled == '_')
1936 (*mangled)++;
1938 /* All is well. */
1939 success = 1;
1943 return success;
1946 /* Demangle the real value in MANGLED. */
1948 static int
1949 demangle_real_value (struct work_stuff *work,
1950 const char **mangled, string *s)
1952 if (**mangled == 'E')
1953 return demangle_expression (work, mangled, s, tk_real);
1955 if (**mangled == 'm')
1957 string_appendn (s, "-", 1);
1958 (*mangled)++;
1960 while (ISDIGIT ((unsigned char)**mangled))
1962 string_appendn (s, *mangled, 1);
1963 (*mangled)++;
1965 if (**mangled == '.') /* fraction */
1967 string_appendn (s, ".", 1);
1968 (*mangled)++;
1969 while (ISDIGIT ((unsigned char)**mangled))
1971 string_appendn (s, *mangled, 1);
1972 (*mangled)++;
1975 if (**mangled == 'e') /* exponent */
1977 string_appendn (s, "e", 1);
1978 (*mangled)++;
1979 while (ISDIGIT ((unsigned char)**mangled))
1981 string_appendn (s, *mangled, 1);
1982 (*mangled)++;
1986 return 1;
1989 static int
1990 demangle_template_value_parm (struct work_stuff *work, const char **mangled,
1991 string *s, type_kind_t tk)
1993 int success = 1;
1995 if (**mangled == 'Y')
1997 /* The next argument is a template parameter. */
1998 int idx;
2000 (*mangled)++;
2001 idx = consume_count_with_underscores (mangled);
2002 if (idx == -1
2003 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2004 || consume_count_with_underscores (mangled) == -1)
2005 return -1;
2006 if (work->tmpl_argvec)
2007 string_append (s, work->tmpl_argvec[idx]);
2008 else
2009 string_append_template_idx (s, idx);
2011 else if (tk == tk_integral)
2012 success = demangle_integral_value (work, mangled, s);
2013 else if (tk == tk_char)
2015 char tmp[2];
2016 int val;
2017 if (**mangled == 'm')
2019 string_appendn (s, "-", 1);
2020 (*mangled)++;
2022 string_appendn (s, "'", 1);
2023 val = consume_count(mangled);
2024 if (val <= 0)
2025 success = 0;
2026 else
2028 tmp[0] = (char)val;
2029 tmp[1] = '\0';
2030 string_appendn (s, &tmp[0], 1);
2031 string_appendn (s, "'", 1);
2034 else if (tk == tk_bool)
2036 int val = consume_count (mangled);
2037 if (val == 0)
2038 string_appendn (s, "false", 5);
2039 else if (val == 1)
2040 string_appendn (s, "true", 4);
2041 else
2042 success = 0;
2044 else if (tk == tk_real)
2045 success = demangle_real_value (work, mangled, s);
2046 else if (tk == tk_pointer || tk == tk_reference
2047 || tk == tk_rvalue_reference)
2049 if (**mangled == 'Q')
2050 success = demangle_qualified (work, mangled, s,
2051 /*isfuncname=*/0,
2052 /*append=*/1);
2053 else
2055 int symbol_len = consume_count (mangled);
2056 if (symbol_len == -1)
2057 return -1;
2058 if (symbol_len == 0)
2059 string_appendn (s, "0", 1);
2060 else
2062 char *p = XNEWVEC (char, symbol_len + 1), *q;
2063 strncpy (p, *mangled, symbol_len);
2064 p [symbol_len] = '\0';
2065 /* We use cplus_demangle here, rather than
2066 internal_cplus_demangle, because the name of the entity
2067 mangled here does not make use of any of the squangling
2068 or type-code information we have built up thus far; it is
2069 mangled independently. */
2070 q = cplus_demangle (p, work->options);
2071 if (tk == tk_pointer)
2072 string_appendn (s, "&", 1);
2073 /* FIXME: Pointer-to-member constants should get a
2074 qualifying class name here. */
2075 if (q)
2077 string_append (s, q);
2078 free (q);
2080 else
2081 string_append (s, p);
2082 free (p);
2084 *mangled += symbol_len;
2088 return success;
2091 /* Demangle the template name in MANGLED. The full name of the
2092 template (e.g., S<int>) is placed in TNAME. The name without the
2093 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2094 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2095 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2096 the template is remembered in the list of back-referenceable
2097 types. */
2099 static int
2100 demangle_template (struct work_stuff *work, const char **mangled,
2101 string *tname, string *trawname,
2102 int is_type, int remember)
2104 int i;
2105 int r;
2106 int need_comma = 0;
2107 int success = 0;
2108 int is_java_array = 0;
2109 string temp;
2111 (*mangled)++;
2112 if (is_type)
2114 /* get template name */
2115 if (**mangled == 'z')
2117 int idx;
2118 (*mangled)++;
2119 (*mangled)++;
2121 idx = consume_count_with_underscores (mangled);
2122 if (idx == -1
2123 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2124 || consume_count_with_underscores (mangled) == -1)
2125 return (0);
2127 if (work->tmpl_argvec)
2129 string_append (tname, work->tmpl_argvec[idx]);
2130 if (trawname)
2131 string_append (trawname, work->tmpl_argvec[idx]);
2133 else
2135 string_append_template_idx (tname, idx);
2136 if (trawname)
2137 string_append_template_idx (trawname, idx);
2140 else
2142 if ((r = consume_count (mangled)) <= 0
2143 || (int) strlen (*mangled) < r)
2145 return (0);
2147 is_java_array = (work -> options & DMGL_JAVA)
2148 && strncmp (*mangled, "JArray1Z", 8) == 0;
2149 if (! is_java_array)
2151 string_appendn (tname, *mangled, r);
2153 if (trawname)
2154 string_appendn (trawname, *mangled, r);
2155 *mangled += r;
2158 if (!is_java_array)
2159 string_append (tname, "<");
2160 /* get size of template parameter list */
2161 if (!get_count (mangled, &r))
2163 return (0);
2165 if (!is_type)
2167 /* Create an array for saving the template argument values. */
2168 work->tmpl_argvec = XNEWVEC (char *, r);
2169 work->ntmpl_args = r;
2170 for (i = 0; i < r; i++)
2171 work->tmpl_argvec[i] = 0;
2173 for (i = 0; i < r; i++)
2175 if (need_comma)
2177 string_append (tname, ", ");
2179 /* Z for type parameters */
2180 if (**mangled == 'Z')
2182 (*mangled)++;
2183 /* temp is initialized in do_type */
2184 success = do_type (work, mangled, &temp);
2185 if (success)
2187 string_appends (tname, &temp);
2189 if (!is_type)
2191 /* Save the template argument. */
2192 int len = temp.p - temp.b;
2193 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
2194 memcpy (work->tmpl_argvec[i], temp.b, len);
2195 work->tmpl_argvec[i][len] = '\0';
2198 string_delete(&temp);
2199 if (!success)
2201 break;
2204 /* z for template parameters */
2205 else if (**mangled == 'z')
2207 int r2;
2208 (*mangled)++;
2209 success = demangle_template_template_parm (work, mangled, tname);
2211 if (success
2212 && (r2 = consume_count (mangled)) > 0
2213 && (int) strlen (*mangled) >= r2)
2215 string_append (tname, " ");
2216 string_appendn (tname, *mangled, r2);
2217 if (!is_type)
2219 /* Save the template argument. */
2220 int len = r2;
2221 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
2222 memcpy (work->tmpl_argvec[i], *mangled, len);
2223 work->tmpl_argvec[i][len] = '\0';
2225 *mangled += r2;
2227 if (!success)
2229 break;
2232 else
2234 string param;
2235 string* s;
2237 /* otherwise, value parameter */
2239 /* temp is initialized in do_type */
2240 success = do_type (work, mangled, &temp);
2241 string_delete(&temp);
2242 if (!success)
2243 break;
2245 if (!is_type)
2247 s = &param;
2248 string_init (s);
2250 else
2251 s = tname;
2253 success = demangle_template_value_parm (work, mangled, s,
2254 (type_kind_t) success);
2256 if (!success)
2258 if (!is_type)
2259 string_delete (s);
2260 success = 0;
2261 break;
2264 if (!is_type)
2266 int len = s->p - s->b;
2267 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
2268 memcpy (work->tmpl_argvec[i], s->b, len);
2269 work->tmpl_argvec[i][len] = '\0';
2271 string_appends (tname, s);
2272 string_delete (s);
2275 need_comma = 1;
2277 if (is_java_array)
2279 string_append (tname, "[]");
2281 else
2283 if (tname->p[-1] == '>')
2284 string_append (tname, " ");
2285 string_append (tname, ">");
2288 if (is_type && remember)
2290 const int bindex = register_Btype (work);
2291 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
2295 if (work -> static_type)
2297 string_append (declp, *mangled + 1);
2298 *mangled += strlen (*mangled);
2299 success = 1;
2301 else
2303 success = demangle_args (work, mangled, declp);
2307 return (success);
2310 static int
2311 arm_pt (struct work_stuff *work, const char *mangled,
2312 int n, const char **anchor, const char **args)
2314 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2315 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2316 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
2318 int len;
2319 *args = *anchor + 6;
2320 len = consume_count (args);
2321 if (len == -1)
2322 return 0;
2323 if (*args + len == mangled + n && **args == '_')
2325 ++*args;
2326 return 1;
2329 if (AUTO_DEMANGLING || EDG_DEMANGLING)
2331 if ((*anchor = strstr (mangled, "__tm__"))
2332 || (*anchor = strstr (mangled, "__ps__"))
2333 || (*anchor = strstr (mangled, "__pt__")))
2335 int len;
2336 *args = *anchor + 6;
2337 len = consume_count (args);
2338 if (len == -1)
2339 return 0;
2340 if (*args + len == mangled + n && **args == '_')
2342 ++*args;
2343 return 1;
2346 else if ((*anchor = strstr (mangled, "__S")))
2348 int len;
2349 *args = *anchor + 3;
2350 len = consume_count (args);
2351 if (len == -1)
2352 return 0;
2353 if (*args + len == mangled + n && **args == '_')
2355 ++*args;
2356 return 1;
2361 return 0;
2364 static void
2365 demangle_arm_hp_template (struct work_stuff *work, const char **mangled,
2366 int n, string *declp)
2368 const char *p;
2369 const char *args;
2370 const char *e = *mangled + n;
2371 string arg;
2373 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2374 template args */
2375 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
2377 char *start_spec_args = NULL;
2378 int hold_options;
2380 /* First check for and omit template specialization pseudo-arguments,
2381 such as in "Spec<#1,#1.*>" */
2382 start_spec_args = strchr (*mangled, '<');
2383 if (start_spec_args && (start_spec_args - *mangled < n))
2384 string_appendn (declp, *mangled, start_spec_args - *mangled);
2385 else
2386 string_appendn (declp, *mangled, n);
2387 (*mangled) += n + 1;
2388 string_init (&arg);
2389 if (work->temp_start == -1) /* non-recursive call */
2390 work->temp_start = declp->p - declp->b;
2392 /* We want to unconditionally demangle parameter types in
2393 template parameters. */
2394 hold_options = work->options;
2395 work->options |= DMGL_PARAMS;
2397 string_append (declp, "<");
2398 while (1)
2400 string_delete (&arg);
2401 switch (**mangled)
2403 case 'T':
2404 /* 'T' signals a type parameter */
2405 (*mangled)++;
2406 if (!do_type (work, mangled, &arg))
2407 goto hpacc_template_args_done;
2408 break;
2410 case 'U':
2411 case 'S':
2412 /* 'U' or 'S' signals an integral value */
2413 if (!do_hpacc_template_const_value (work, mangled, &arg))
2414 goto hpacc_template_args_done;
2415 break;
2417 case 'A':
2418 /* 'A' signals a named constant expression (literal) */
2419 if (!do_hpacc_template_literal (work, mangled, &arg))
2420 goto hpacc_template_args_done;
2421 break;
2423 default:
2424 /* Today, 1997-09-03, we have only the above types
2425 of template parameters */
2426 /* FIXME: maybe this should fail and return null */
2427 goto hpacc_template_args_done;
2429 string_appends (declp, &arg);
2430 /* Check if we're at the end of template args.
2431 0 if at end of static member of template class,
2432 _ if done with template args for a function */
2433 if ((**mangled == '\000') || (**mangled == '_'))
2434 break;
2435 else
2436 string_append (declp, ",");
2438 hpacc_template_args_done:
2439 string_append (declp, ">");
2440 string_delete (&arg);
2441 if (**mangled == '_')
2442 (*mangled)++;
2443 work->options = hold_options;
2444 return;
2446 /* ARM template? (Also handles HP cfront extensions) */
2447 else if (arm_pt (work, *mangled, n, &p, &args))
2449 int hold_options;
2450 string type_str;
2452 string_init (&arg);
2453 string_appendn (declp, *mangled, p - *mangled);
2454 if (work->temp_start == -1) /* non-recursive call */
2455 work->temp_start = declp->p - declp->b;
2457 /* We want to unconditionally demangle parameter types in
2458 template parameters. */
2459 hold_options = work->options;
2460 work->options |= DMGL_PARAMS;
2462 string_append (declp, "<");
2463 /* should do error checking here */
2464 while (args < e) {
2465 string_delete (&arg);
2467 /* Check for type or literal here */
2468 switch (*args)
2470 /* HP cfront extensions to ARM for template args */
2471 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2472 /* FIXME: We handle only numeric literals for HP cfront */
2473 case 'X':
2474 /* A typed constant value follows */
2475 args++;
2476 if (!do_type (work, &args, &type_str))
2477 goto cfront_template_args_done;
2478 string_append (&arg, "(");
2479 string_appends (&arg, &type_str);
2480 string_delete (&type_str);
2481 string_append (&arg, ")");
2482 if (*args != 'L')
2483 goto cfront_template_args_done;
2484 args++;
2485 /* Now snarf a literal value following 'L' */
2486 if (!snarf_numeric_literal (&args, &arg))
2487 goto cfront_template_args_done;
2488 break;
2490 case 'L':
2491 /* Snarf a literal following 'L' */
2492 args++;
2493 if (!snarf_numeric_literal (&args, &arg))
2494 goto cfront_template_args_done;
2495 break;
2496 default:
2497 /* Not handling other HP cfront stuff */
2499 const char* old_args = args;
2500 if (!do_type (work, &args, &arg))
2501 goto cfront_template_args_done;
2503 /* Fail if we didn't make any progress: prevent infinite loop. */
2504 if (args == old_args)
2506 work->options = hold_options;
2507 return;
2511 string_appends (declp, &arg);
2512 string_append (declp, ",");
2514 cfront_template_args_done:
2515 string_delete (&arg);
2516 if (args >= e)
2517 --declp->p; /* remove extra comma */
2518 string_append (declp, ">");
2519 work->options = hold_options;
2521 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2522 && (*mangled)[9] == 'N'
2523 && (*mangled)[8] == (*mangled)[10]
2524 && strchr (cplus_markers, (*mangled)[8]))
2526 /* A member of the anonymous namespace. */
2527 string_append (declp, "{anonymous}");
2529 else
2531 if (work->temp_start == -1) /* non-recursive call only */
2532 work->temp_start = 0; /* disable in recursive calls */
2533 string_appendn (declp, *mangled, n);
2535 *mangled += n;
2538 /* Extract a class name, possibly a template with arguments, from the
2539 mangled string; qualifiers, local class indicators, etc. have
2540 already been dealt with */
2542 static int
2543 demangle_class_name (struct work_stuff *work, const char **mangled,
2544 string *declp)
2546 int n;
2547 int success = 0;
2549 n = consume_count (mangled);
2550 if (n == -1)
2551 return 0;
2552 if ((int) strlen (*mangled) >= n)
2554 demangle_arm_hp_template (work, mangled, n, declp);
2555 success = 1;
2558 return (success);
2563 LOCAL FUNCTION
2565 demangle_class -- demangle a mangled class sequence
2567 SYNOPSIS
2569 static int
2570 demangle_class (struct work_stuff *work, const char **mangled,
2571 strint *declp)
2573 DESCRIPTION
2575 DECLP points to the buffer into which demangling is being done.
2577 *MANGLED points to the current token to be demangled. On input,
2578 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2579 On exit, it points to the next token after the mangled class on
2580 success, or the first unconsumed token on failure.
2582 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2583 we are demangling a constructor or destructor. In this case
2584 we prepend "class::class" or "class::~class" to DECLP.
2586 Otherwise, we prepend "class::" to the current DECLP.
2588 Reset the constructor/destructor flags once they have been
2589 "consumed". This allows demangle_class to be called later during
2590 the same demangling, to do normal class demangling.
2592 Returns 1 if demangling is successful, 0 otherwise.
2596 static int
2597 demangle_class (struct work_stuff *work, const char **mangled, string *declp)
2599 int success = 0;
2600 int btype;
2601 string class_name;
2602 char *save_class_name_end = 0;
2604 string_init (&class_name);
2605 btype = register_Btype (work);
2606 if (demangle_class_name (work, mangled, &class_name))
2608 save_class_name_end = class_name.p;
2609 if ((work->constructor & 1) || (work->destructor & 1))
2611 /* adjust so we don't include template args */
2612 if (work->temp_start && (work->temp_start != -1))
2614 class_name.p = class_name.b + work->temp_start;
2616 string_prepends (declp, &class_name);
2617 if (work -> destructor & 1)
2619 string_prepend (declp, "~");
2620 work -> destructor -= 1;
2622 else
2624 work -> constructor -= 1;
2627 class_name.p = save_class_name_end;
2628 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2629 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2630 string_prepend (declp, SCOPE_STRING (work));
2631 string_prepends (declp, &class_name);
2632 success = 1;
2634 string_delete (&class_name);
2635 return (success);
2639 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2640 the rightmost guess.
2642 Find the correct "__"-sequence where the function name ends and the
2643 signature starts, which is ambiguous with GNU mangling.
2644 Call demangle_signature here, so we can make sure we found the right
2645 one; *mangled will be consumed so caller will not make further calls to
2646 demangle_signature. */
2648 static int
2649 iterate_demangle_function (struct work_stuff *work, const char **mangled,
2650 string *declp, const char *scan)
2652 const char *mangle_init = *mangled;
2653 int success = 0;
2654 string decl_init;
2655 struct work_stuff work_init;
2657 if (*(scan + 2) == '\0')
2658 return 0;
2660 /* Do not iterate for some demangling modes, or if there's only one
2661 "__"-sequence. This is the normal case. */
2662 if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
2663 || strstr (scan + 2, "__") == NULL)
2664 return demangle_function_name (work, mangled, declp, scan);
2666 /* Save state so we can restart if the guess at the correct "__" was
2667 wrong. */
2668 string_init (&decl_init);
2669 string_appends (&decl_init, declp);
2670 memset (&work_init, 0, sizeof work_init);
2671 work_stuff_copy_to_from (&work_init, work);
2673 /* Iterate over occurrences of __, allowing names and types to have a
2674 "__" sequence in them. We must start with the first (not the last)
2675 occurrence, since "__" most often occur between independent mangled
2676 parts, hence starting at the last occurence inside a signature
2677 might get us a "successful" demangling of the signature. */
2679 while (scan[2])
2681 if (demangle_function_name (work, mangled, declp, scan))
2683 success = demangle_signature (work, mangled, declp);
2684 if (success)
2685 break;
2688 /* Reset demangle state for the next round. */
2689 *mangled = mangle_init;
2690 string_clear (declp);
2691 string_appends (declp, &decl_init);
2692 work_stuff_copy_to_from (work, &work_init);
2694 /* Leave this underscore-sequence. */
2695 scan += 2;
2697 /* Scan for the next "__" sequence. */
2698 while (*scan && (scan[0] != '_' || scan[1] != '_'))
2699 scan++;
2701 /* Move to last "__" in this sequence. */
2702 while (*scan && *scan == '_')
2703 scan++;
2704 scan -= 2;
2707 /* Delete saved state. */
2708 delete_work_stuff (&work_init);
2709 string_delete (&decl_init);
2711 return success;
2716 LOCAL FUNCTION
2718 demangle_prefix -- consume the mangled name prefix and find signature
2720 SYNOPSIS
2722 static int
2723 demangle_prefix (struct work_stuff *work, const char **mangled,
2724 string *declp);
2726 DESCRIPTION
2728 Consume and demangle the prefix of the mangled name.
2729 While processing the function name root, arrange to call
2730 demangle_signature if the root is ambiguous.
2732 DECLP points to the string buffer into which demangled output is
2733 placed. On entry, the buffer is empty. On exit it contains
2734 the root function name, the demangled operator name, or in some
2735 special cases either nothing or the completely demangled result.
2737 MANGLED points to the current pointer into the mangled name. As each
2738 token of the mangled name is consumed, it is updated. Upon entry
2739 the current mangled name pointer points to the first character of
2740 the mangled name. Upon exit, it should point to the first character
2741 of the signature if demangling was successful, or to the first
2742 unconsumed character if demangling of the prefix was unsuccessful.
2744 Returns 1 on success, 0 otherwise.
2747 static int
2748 demangle_prefix (struct work_stuff *work, const char **mangled,
2749 string *declp)
2751 int success = 1;
2752 const char *scan;
2753 int i;
2755 if (strlen(*mangled) > 6
2756 && (strncmp(*mangled, "_imp__", 6) == 0
2757 || strncmp(*mangled, "__imp_", 6) == 0))
2759 /* it's a symbol imported from a PE dynamic library. Check for both
2760 new style prefix _imp__ and legacy __imp_ used by older versions
2761 of dlltool. */
2762 (*mangled) += 6;
2763 work->dllimported = 1;
2765 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2767 char *marker = strchr (cplus_markers, (*mangled)[8]);
2768 if (marker != NULL && *marker == (*mangled)[10])
2770 if ((*mangled)[9] == 'D')
2772 /* it's a GNU global destructor to be executed at program exit */
2773 (*mangled) += 11;
2774 work->destructor = 2;
2775 if (gnu_special (work, mangled, declp))
2776 return success;
2778 else if ((*mangled)[9] == 'I')
2780 /* it's a GNU global constructor to be executed at program init */
2781 (*mangled) += 11;
2782 work->constructor = 2;
2783 if (gnu_special (work, mangled, declp))
2784 return success;
2788 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2790 /* it's a ARM global destructor to be executed at program exit */
2791 (*mangled) += 7;
2792 work->destructor = 2;
2794 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2796 /* it's a ARM global constructor to be executed at program initial */
2797 (*mangled) += 7;
2798 work->constructor = 2;
2801 /* This block of code is a reduction in strength time optimization
2803 scan = strstr (*mangled, "__"); */
2806 scan = *mangled;
2808 do {
2809 scan = strchr (scan, '_');
2810 } while (scan != NULL && *++scan != '_');
2812 if (scan != NULL) --scan;
2815 if (scan != NULL)
2817 /* We found a sequence of two or more '_', ensure that we start at
2818 the last pair in the sequence. */
2819 i = strspn (scan, "_");
2820 if (i > 2)
2822 scan += (i - 2);
2826 if (scan == NULL)
2828 success = 0;
2830 else if (work -> static_type)
2832 if (!ISDIGIT ((unsigned char)scan[0]) && (scan[0] != 't'))
2834 success = 0;
2837 else if ((scan == *mangled)
2838 && (ISDIGIT ((unsigned char)scan[2]) || (scan[2] == 'Q')
2839 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2841 /* The ARM says nothing about the mangling of local variables.
2842 But cfront mangles local variables by prepending __<nesting_level>
2843 to them. As an extension to ARM demangling we handle this case. */
2844 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
2845 && ISDIGIT ((unsigned char)scan[2]))
2847 *mangled = scan + 2;
2848 consume_count (mangled);
2849 string_append (declp, *mangled);
2850 *mangled += strlen (*mangled);
2851 success = 1;
2853 else
2855 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2856 names like __Q2_3foo3bar for nested type names. So don't accept
2857 this style of constructor for cfront demangling. A GNU
2858 style member-template constructor starts with 'H'. */
2859 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2860 work -> constructor += 1;
2861 *mangled = scan + 2;
2864 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2866 /* Cfront-style parameterized type. Handled later as a signature. */
2867 success = 1;
2869 /* ARM template? */
2870 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2872 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2873 || (scan[2] == 'p' && scan[3] == 's')
2874 || (scan[2] == 'p' && scan[3] == 't')))
2876 /* EDG-style parameterized type. Handled later as a signature. */
2877 success = 1;
2879 /* EDG template? */
2880 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2882 else if ((scan == *mangled) && !ISDIGIT ((unsigned char)scan[2])
2883 && (scan[2] != 't'))
2885 /* Mangled name starts with "__". Skip over any leading '_' characters,
2886 then find the next "__" that separates the prefix from the signature.
2888 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2889 || (arm_special (mangled, declp) == 0))
2891 while (*scan == '_')
2893 scan++;
2895 if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
2897 /* No separator (I.E. "__not_mangled"), or empty signature
2898 (I.E. "__not_mangled_either__") */
2899 success = 0;
2901 else
2902 return iterate_demangle_function (work, mangled, declp, scan);
2905 else if (*(scan + 2) != '\0')
2907 /* Mangled name does not start with "__" but does have one somewhere
2908 in there with non empty stuff after it. Looks like a global
2909 function name. Iterate over all "__":s until the right
2910 one is found. */
2911 return iterate_demangle_function (work, mangled, declp, scan);
2913 else
2915 /* Doesn't look like a mangled name */
2916 success = 0;
2919 if (!success && (work->constructor == 2 || work->destructor == 2))
2921 string_append (declp, *mangled);
2922 *mangled += strlen (*mangled);
2923 success = 1;
2925 return (success);
2930 LOCAL FUNCTION
2932 gnu_special -- special handling of gnu mangled strings
2934 SYNOPSIS
2936 static int
2937 gnu_special (struct work_stuff *work, const char **mangled,
2938 string *declp);
2941 DESCRIPTION
2943 Process some special GNU style mangling forms that don't fit
2944 the normal pattern. For example:
2946 _$_3foo (destructor for class foo)
2947 _vt$foo (foo virtual table)
2948 _vt$foo$bar (foo::bar virtual table)
2949 __vt_foo (foo virtual table, new style with thunks)
2950 _3foo$varname (static data member)
2951 _Q22rs2tu$vw (static data member)
2952 __t6vector1Zii (constructor with template)
2953 __thunk_4__$_7ostream (virtual function thunk)
2956 static int
2957 gnu_special (struct work_stuff *work, const char **mangled, string *declp)
2959 int n;
2960 int success = 1;
2961 const char *p;
2963 if ((*mangled)[0] == '_'
2964 && strchr (cplus_markers, (*mangled)[1]) != NULL
2965 && (*mangled)[2] == '_')
2967 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2968 (*mangled) += 3;
2969 work -> destructor += 1;
2971 else if ((*mangled)[0] == '_'
2972 && (((*mangled)[1] == '_'
2973 && (*mangled)[2] == 'v'
2974 && (*mangled)[3] == 't'
2975 && (*mangled)[4] == '_')
2976 || ((*mangled)[1] == 'v'
2977 && (*mangled)[2] == 't'
2978 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2980 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2981 and create the decl. Note that we consume the entire mangled
2982 input string, which means that demangle_signature has no work
2983 to do. */
2984 if ((*mangled)[2] == 'v')
2985 (*mangled) += 5; /* New style, with thunks: "__vt_" */
2986 else
2987 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2988 while (**mangled != '\0')
2990 switch (**mangled)
2992 case 'Q':
2993 case 'K':
2994 success = demangle_qualified (work, mangled, declp, 0, 1);
2995 break;
2996 case 't':
2997 success = demangle_template (work, mangled, declp, 0, 1,
2999 break;
3000 default:
3001 if (ISDIGIT((unsigned char)*mangled[0]))
3003 n = consume_count(mangled);
3004 /* We may be seeing a too-large size, or else a
3005 ".<digits>" indicating a static local symbol. In
3006 any case, declare victory and move on; *don't* try
3007 to use n to allocate. */
3008 if (n > (int) strlen (*mangled))
3010 success = 1;
3011 break;
3013 else if (n == -1)
3015 success = 0;
3016 break;
3019 else
3021 n = strcspn (*mangled, cplus_markers);
3023 string_appendn (declp, *mangled, n);
3024 (*mangled) += n;
3027 p = strpbrk (*mangled, cplus_markers);
3028 if (success && ((p == NULL) || (p == *mangled)))
3030 if (p != NULL)
3032 string_append (declp, SCOPE_STRING (work));
3033 (*mangled)++;
3036 else
3038 success = 0;
3039 break;
3042 if (success)
3043 string_append (declp, " virtual table");
3045 else if ((*mangled)[0] == '_'
3046 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
3047 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
3049 /* static data member, "_3foo$varname" for example */
3050 (*mangled)++;
3051 switch (**mangled)
3053 case 'Q':
3054 case 'K':
3055 success = demangle_qualified (work, mangled, declp, 0, 1);
3056 break;
3057 case 't':
3058 success = demangle_template (work, mangled, declp, 0, 1, 1);
3059 break;
3060 default:
3061 n = consume_count (mangled);
3062 if (n < 0 || n > (long) strlen (*mangled))
3064 success = 0;
3065 break;
3068 if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
3069 && (*mangled)[9] == 'N'
3070 && (*mangled)[8] == (*mangled)[10]
3071 && strchr (cplus_markers, (*mangled)[8]))
3073 /* A member of the anonymous namespace. There's information
3074 about what identifier or filename it was keyed to, but
3075 it's just there to make the mangled name unique; we just
3076 step over it. */
3077 string_append (declp, "{anonymous}");
3078 (*mangled) += n;
3080 /* Now p points to the marker before the N, so we need to
3081 update it to the first marker after what we consumed. */
3082 p = strpbrk (*mangled, cplus_markers);
3083 break;
3086 string_appendn (declp, *mangled, n);
3087 (*mangled) += n;
3089 if (success && (p == *mangled))
3091 /* Consumed everything up to the cplus_marker, append the
3092 variable name. */
3093 (*mangled)++;
3094 string_append (declp, SCOPE_STRING (work));
3095 n = strlen (*mangled);
3096 string_appendn (declp, *mangled, n);
3097 (*mangled) += n;
3099 else
3101 success = 0;
3104 else if (strncmp (*mangled, "__thunk_", 8) == 0)
3106 int delta;
3108 (*mangled) += 8;
3109 delta = consume_count (mangled);
3110 if (delta == -1)
3111 success = 0;
3112 else
3114 char *method = internal_cplus_demangle (work, ++*mangled);
3116 if (method)
3118 char buf[50];
3119 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
3120 string_append (declp, buf);
3121 string_append (declp, method);
3122 free (method);
3123 n = strlen (*mangled);
3124 (*mangled) += n;
3126 else
3128 success = 0;
3132 else if (strncmp (*mangled, "__t", 3) == 0
3133 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
3135 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
3136 (*mangled) += 4;
3137 switch (**mangled)
3139 case 'Q':
3140 case 'K':
3141 success = demangle_qualified (work, mangled, declp, 0, 1);
3142 break;
3143 case 't':
3144 success = demangle_template (work, mangled, declp, 0, 1, 1);
3145 break;
3146 default:
3147 success = do_type (work, mangled, declp);
3148 break;
3150 if (success && **mangled != '\0')
3151 success = 0;
3152 if (success)
3153 string_append (declp, p);
3155 else
3157 success = 0;
3159 return (success);
3162 static void
3163 recursively_demangle(struct work_stuff *work, const char **mangled,
3164 string *result, int namelength)
3166 char * recurse = (char *)NULL;
3167 char * recurse_dem = (char *)NULL;
3169 recurse = XNEWVEC (char, namelength + 1);
3170 memcpy (recurse, *mangled, namelength);
3171 recurse[namelength] = '\000';
3173 recurse_dem = cplus_demangle (recurse, work->options);
3175 if (recurse_dem)
3177 string_append (result, recurse_dem);
3178 free (recurse_dem);
3180 else
3182 string_appendn (result, *mangled, namelength);
3184 free (recurse);
3185 *mangled += namelength;
3190 LOCAL FUNCTION
3192 arm_special -- special handling of ARM/lucid mangled strings
3194 SYNOPSIS
3196 static int
3197 arm_special (const char **mangled,
3198 string *declp);
3201 DESCRIPTION
3203 Process some special ARM style mangling forms that don't fit
3204 the normal pattern. For example:
3206 __vtbl__3foo (foo virtual table)
3207 __vtbl__3foo__3bar (bar::foo virtual table)
3211 static int
3212 arm_special (const char **mangled, string *declp)
3214 int n;
3215 int success = 1;
3216 const char *scan;
3218 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
3220 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3221 and create the decl. Note that we consume the entire mangled
3222 input string, which means that demangle_signature has no work
3223 to do. */
3224 scan = *mangled + ARM_VTABLE_STRLEN;
3225 while (*scan != '\0') /* first check it can be demangled */
3227 n = consume_count (&scan);
3228 if (n == -1)
3230 return (0); /* no good */
3232 scan += n;
3233 if (scan[0] == '_' && scan[1] == '_')
3235 scan += 2;
3238 (*mangled) += ARM_VTABLE_STRLEN;
3239 while (**mangled != '\0')
3241 n = consume_count (mangled);
3242 if (n == -1
3243 || n > (long) strlen (*mangled))
3244 return 0;
3245 string_prependn (declp, *mangled, n);
3246 (*mangled) += n;
3247 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
3249 string_prepend (declp, "::");
3250 (*mangled) += 2;
3253 string_append (declp, " virtual table");
3255 else
3257 success = 0;
3259 return (success);
3264 LOCAL FUNCTION
3266 demangle_qualified -- demangle 'Q' qualified name strings
3268 SYNOPSIS
3270 static int
3271 demangle_qualified (struct work_stuff *, const char *mangled,
3272 string *result, int isfuncname, int append);
3274 DESCRIPTION
3276 Demangle a qualified name, such as "Q25Outer5Inner" which is
3277 the mangled form of "Outer::Inner". The demangled output is
3278 prepended or appended to the result string according to the
3279 state of the append flag.
3281 If isfuncname is nonzero, then the qualified name we are building
3282 is going to be used as a member function name, so if it is a
3283 constructor or destructor function, append an appropriate
3284 constructor or destructor name. I.E. for the above example,
3285 the result for use as a constructor is "Outer::Inner::Inner"
3286 and the result for use as a destructor is "Outer::Inner::~Inner".
3288 BUGS
3290 Numeric conversion is ASCII dependent (FIXME).
3294 static int
3295 demangle_qualified (struct work_stuff *work, const char **mangled,
3296 string *result, int isfuncname, int append)
3298 int qualifiers = 0;
3299 int success = 1;
3300 char num[2];
3301 string temp;
3302 string last_name;
3303 int bindex = register_Btype (work);
3305 /* We only make use of ISFUNCNAME if the entity is a constructor or
3306 destructor. */
3307 isfuncname = (isfuncname
3308 && ((work->constructor & 1) || (work->destructor & 1)));
3310 string_init (&temp);
3311 string_init (&last_name);
3313 if ((*mangled)[0] == 'K')
3315 /* Squangling qualified name reuse */
3316 int idx;
3317 (*mangled)++;
3318 idx = consume_count_with_underscores (mangled);
3319 if (idx == -1 || idx >= work -> numk)
3320 success = 0;
3321 else
3322 string_append (&temp, work -> ktypevec[idx]);
3324 else
3325 switch ((*mangled)[1])
3327 case '_':
3328 /* GNU mangled name with more than 9 classes. The count is preceded
3329 by an underscore (to distinguish it from the <= 9 case) and followed
3330 by an underscore. */
3331 (*mangled)++;
3332 qualifiers = consume_count_with_underscores (mangled);
3333 if (qualifiers == -1)
3334 success = 0;
3335 break;
3337 case '1':
3338 case '2':
3339 case '3':
3340 case '4':
3341 case '5':
3342 case '6':
3343 case '7':
3344 case '8':
3345 case '9':
3346 /* The count is in a single digit. */
3347 num[0] = (*mangled)[1];
3348 num[1] = '\0';
3349 qualifiers = atoi (num);
3351 /* If there is an underscore after the digit, skip it. This is
3352 said to be for ARM-qualified names, but the ARM makes no
3353 mention of such an underscore. Perhaps cfront uses one. */
3354 if ((*mangled)[2] == '_')
3356 (*mangled)++;
3358 (*mangled) += 2;
3359 break;
3361 case '0':
3362 default:
3363 success = 0;
3366 if (!success)
3367 return success;
3369 /* Pick off the names and collect them in the temp buffer in the order
3370 in which they are found, separated by '::'. */
3372 while (qualifiers-- > 0)
3374 int remember_K = 1;
3375 string_clear (&last_name);
3377 if (*mangled[0] == '_')
3378 (*mangled)++;
3380 if (*mangled[0] == 't')
3382 /* Here we always append to TEMP since we will want to use
3383 the template name without the template parameters as a
3384 constructor or destructor name. The appropriate
3385 (parameter-less) value is returned by demangle_template
3386 in LAST_NAME. We do not remember the template type here,
3387 in order to match the G++ mangling algorithm. */
3388 success = demangle_template(work, mangled, &temp,
3389 &last_name, 1, 0);
3390 if (!success)
3391 break;
3393 else if (*mangled[0] == 'K')
3395 int idx;
3396 (*mangled)++;
3397 idx = consume_count_with_underscores (mangled);
3398 if (idx == -1 || idx >= work->numk)
3399 success = 0;
3400 else
3401 string_append (&temp, work->ktypevec[idx]);
3402 remember_K = 0;
3404 if (!success) break;
3406 else
3408 if (EDG_DEMANGLING)
3410 int namelength;
3411 /* Now recursively demangle the qualifier
3412 * This is necessary to deal with templates in
3413 * mangling styles like EDG */
3414 namelength = consume_count (mangled);
3415 if (namelength == -1)
3417 success = 0;
3418 break;
3420 recursively_demangle(work, mangled, &temp, namelength);
3422 else
3424 string_delete (&last_name);
3425 success = do_type (work, mangled, &last_name);
3426 if (!success)
3427 break;
3428 string_appends (&temp, &last_name);
3432 if (remember_K)
3433 remember_Ktype (work, temp.b, LEN_STRING (&temp));
3435 if (qualifiers > 0)
3436 string_append (&temp, SCOPE_STRING (work));
3439 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
3441 /* If we are using the result as a function name, we need to append
3442 the appropriate '::' separated constructor or destructor name.
3443 We do this here because this is the most convenient place, where
3444 we already have a pointer to the name and the length of the name. */
3446 if (isfuncname)
3448 string_append (&temp, SCOPE_STRING (work));
3449 if (work -> destructor & 1)
3450 string_append (&temp, "~");
3451 string_appends (&temp, &last_name);
3454 /* Now either prepend the temp buffer to the result, or append it,
3455 depending upon the state of the append flag. */
3457 if (append)
3458 string_appends (result, &temp);
3459 else
3461 if (!STRING_EMPTY (result))
3462 string_append (&temp, SCOPE_STRING (work));
3463 string_prepends (result, &temp);
3466 string_delete (&last_name);
3467 string_delete (&temp);
3468 return (success);
3473 LOCAL FUNCTION
3475 get_count -- convert an ascii count to integer, consuming tokens
3477 SYNOPSIS
3479 static int
3480 get_count (const char **type, int *count)
3482 DESCRIPTION
3484 Assume that *type points at a count in a mangled name; set
3485 *count to its value, and set *type to the next character after
3486 the count. There are some weird rules in effect here.
3488 If *type does not point at a string of digits, return zero.
3490 If *type points at a string of digits followed by an
3491 underscore, set *count to their value as an integer, advance
3492 *type to point *after the underscore, and return 1.
3494 If *type points at a string of digits not followed by an
3495 underscore, consume only the first digit. Set *count to its
3496 value as an integer, leave *type pointing after that digit,
3497 and return 1.
3499 The excuse for this odd behavior: in the ARM and HP demangling
3500 styles, a type can be followed by a repeat count of the form
3501 `Nxy', where:
3503 `x' is a single digit specifying how many additional copies
3504 of the type to append to the argument list, and
3506 `y' is one or more digits, specifying the zero-based index of
3507 the first repeated argument in the list. Yes, as you're
3508 unmangling the name you can figure this out yourself, but
3509 it's there anyway.
3511 So, for example, in `bar__3fooFPiN51', the first argument is a
3512 pointer to an integer (`Pi'), and then the next five arguments
3513 are the same (`N5'), and the first repeat is the function's
3514 second argument (`1').
3517 static int
3518 get_count (const char **type, int *count)
3520 const char *p;
3521 int n;
3523 if (!ISDIGIT ((unsigned char)**type))
3524 return (0);
3525 else
3527 *count = **type - '0';
3528 (*type)++;
3529 if (ISDIGIT ((unsigned char)**type))
3531 p = *type;
3532 n = *count;
3535 n *= 10;
3536 n += *p - '0';
3537 p++;
3539 while (ISDIGIT ((unsigned char)*p));
3540 if (*p == '_')
3542 *type = p + 1;
3543 *count = n;
3547 return (1);
3550 /* RESULT will be initialised here; it will be freed on failure. The
3551 value returned is really a type_kind_t. */
3553 static int
3554 do_type (struct work_stuff *work, const char **mangled, string *result)
3556 int n;
3557 int done;
3558 int success;
3559 string decl;
3560 const char *remembered_type;
3561 int type_quals;
3562 type_kind_t tk = tk_none;
3564 string_init (&decl);
3565 string_init (result);
3567 done = 0;
3568 success = 1;
3569 while (success && !done)
3571 int member;
3572 switch (**mangled)
3575 /* A pointer type */
3576 case 'P':
3577 case 'p':
3578 (*mangled)++;
3579 if (! (work -> options & DMGL_JAVA))
3580 string_prepend (&decl, "*");
3581 if (tk == tk_none)
3582 tk = tk_pointer;
3583 break;
3585 /* A reference type */
3586 case 'R':
3587 (*mangled)++;
3588 string_prepend (&decl, "&");
3589 if (tk == tk_none)
3590 tk = tk_reference;
3591 break;
3593 /* An rvalue reference type */
3594 case 'O':
3595 (*mangled)++;
3596 string_prepend (&decl, "&&");
3597 if (tk == tk_none)
3598 tk = tk_rvalue_reference;
3599 break;
3601 /* An array */
3602 case 'A':
3604 ++(*mangled);
3605 if (!STRING_EMPTY (&decl)
3606 && (decl.b[0] == '*' || decl.b[0] == '&'))
3608 string_prepend (&decl, "(");
3609 string_append (&decl, ")");
3611 string_append (&decl, "[");
3612 if (**mangled != '_')
3613 success = demangle_template_value_parm (work, mangled, &decl,
3614 tk_integral);
3615 if (**mangled == '_')
3616 ++(*mangled);
3617 string_append (&decl, "]");
3618 break;
3621 /* A back reference to a previously seen type */
3622 case 'T':
3623 (*mangled)++;
3624 if (!get_count (mangled, &n) || n >= work -> ntypes)
3626 success = 0;
3628 else
3630 remembered_type = work -> typevec[n];
3631 mangled = &remembered_type;
3633 break;
3635 /* A function */
3636 case 'F':
3637 (*mangled)++;
3638 if (!STRING_EMPTY (&decl)
3639 && (decl.b[0] == '*' || decl.b[0] == '&'))
3641 string_prepend (&decl, "(");
3642 string_append (&decl, ")");
3644 /* After picking off the function args, we expect to either find the
3645 function return type (preceded by an '_') or the end of the
3646 string. */
3647 if (!demangle_nested_args (work, mangled, &decl)
3648 || (**mangled != '_' && **mangled != '\0'))
3650 success = 0;
3651 break;
3653 if (success && (**mangled == '_'))
3654 (*mangled)++;
3655 break;
3657 case 'M':
3659 type_quals = TYPE_UNQUALIFIED;
3661 member = **mangled == 'M';
3662 (*mangled)++;
3664 string_append (&decl, ")");
3666 /* We don't need to prepend `::' for a qualified name;
3667 demangle_qualified will do that for us. */
3668 if (**mangled != 'Q')
3669 string_prepend (&decl, SCOPE_STRING (work));
3671 if (ISDIGIT ((unsigned char)**mangled))
3673 n = consume_count (mangled);
3674 if (n == -1
3675 || (int) strlen (*mangled) < n)
3677 success = 0;
3678 break;
3680 string_prependn (&decl, *mangled, n);
3681 *mangled += n;
3683 else if (**mangled == 'X' || **mangled == 'Y')
3685 string temp;
3686 do_type (work, mangled, &temp);
3687 string_prepends (&decl, &temp);
3688 string_delete (&temp);
3690 else if (**mangled == 't')
3692 string temp;
3693 string_init (&temp);
3694 success = demangle_template (work, mangled, &temp,
3695 NULL, 1, 1);
3696 if (success)
3698 string_prependn (&decl, temp.b, temp.p - temp.b);
3699 string_delete (&temp);
3701 else
3703 string_delete (&temp);
3704 break;
3707 else if (**mangled == 'Q')
3709 success = demangle_qualified (work, mangled, &decl,
3710 /*isfuncnam=*/0,
3711 /*append=*/0);
3712 if (!success)
3713 break;
3715 else
3717 success = 0;
3718 break;
3721 string_prepend (&decl, "(");
3722 if (member)
3724 switch (**mangled)
3726 case 'C':
3727 case 'V':
3728 case 'u':
3729 type_quals |= code_for_qualifier (**mangled);
3730 (*mangled)++;
3731 break;
3733 default:
3734 break;
3737 if (*(*mangled)++ != 'F')
3739 success = 0;
3740 break;
3743 if ((member && !demangle_nested_args (work, mangled, &decl))
3744 || **mangled != '_')
3746 success = 0;
3747 break;
3749 (*mangled)++;
3750 if (! PRINT_ANSI_QUALIFIERS)
3752 break;
3754 if (type_quals != TYPE_UNQUALIFIED)
3756 APPEND_BLANK (&decl);
3757 string_append (&decl, qualifier_string (type_quals));
3759 break;
3761 case 'G':
3762 (*mangled)++;
3763 break;
3765 case 'C':
3766 case 'V':
3767 case 'u':
3768 if (PRINT_ANSI_QUALIFIERS)
3770 if (!STRING_EMPTY (&decl))
3771 string_prepend (&decl, " ");
3773 string_prepend (&decl, demangle_qualifier (**mangled));
3775 (*mangled)++;
3776 break;
3781 /* fall through */
3782 default:
3783 done = 1;
3784 break;
3788 if (success) switch (**mangled)
3790 /* A qualified name, such as "Outer::Inner". */
3791 case 'Q':
3792 case 'K':
3794 success = demangle_qualified (work, mangled, result, 0, 1);
3795 break;
3798 /* A back reference to a previously seen squangled type */
3799 case 'B':
3800 (*mangled)++;
3801 if (!get_count (mangled, &n) || n >= work -> numb)
3802 success = 0;
3803 else
3804 string_append (result, work->btypevec[n]);
3805 break;
3807 case 'X':
3808 case 'Y':
3809 /* A template parm. We substitute the corresponding argument. */
3811 int idx;
3813 (*mangled)++;
3814 idx = consume_count_with_underscores (mangled);
3816 if (idx == -1
3817 || (work->tmpl_argvec && idx >= work->ntmpl_args)
3818 || consume_count_with_underscores (mangled) == -1)
3820 success = 0;
3821 break;
3824 if (work->tmpl_argvec)
3825 string_append (result, work->tmpl_argvec[idx]);
3826 else
3827 string_append_template_idx (result, idx);
3829 success = 1;
3831 break;
3833 default:
3834 success = demangle_fund_type (work, mangled, result);
3835 if (tk == tk_none)
3836 tk = (type_kind_t) success;
3837 break;
3840 if (success)
3842 if (!STRING_EMPTY (&decl))
3844 string_append (result, " ");
3845 string_appends (result, &decl);
3848 else
3849 string_delete (result);
3850 string_delete (&decl);
3852 if (success)
3853 /* Assume an integral type, if we're not sure. */
3854 return (int) ((tk == tk_none) ? tk_integral : tk);
3855 else
3856 return 0;
3859 /* Given a pointer to a type string that represents a fundamental type
3860 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3861 string in which the demangled output is being built in RESULT, and
3862 the WORK structure, decode the types and add them to the result.
3864 For example:
3866 "Ci" => "const int"
3867 "Sl" => "signed long"
3868 "CUs" => "const unsigned short"
3870 The value returned is really a type_kind_t. */
3872 static int
3873 demangle_fund_type (struct work_stuff *work,
3874 const char **mangled, string *result)
3876 int done = 0;
3877 int success = 1;
3878 char buf[INTBUF_SIZE + 5 /* 'int%u_t' */];
3879 unsigned int dec = 0;
3880 type_kind_t tk = tk_integral;
3882 /* First pick off any type qualifiers. There can be more than one. */
3884 while (!done)
3886 switch (**mangled)
3888 case 'C':
3889 case 'V':
3890 case 'u':
3891 if (PRINT_ANSI_QUALIFIERS)
3893 if (!STRING_EMPTY (result))
3894 string_prepend (result, " ");
3895 string_prepend (result, demangle_qualifier (**mangled));
3897 (*mangled)++;
3898 break;
3899 case 'U':
3900 (*mangled)++;
3901 APPEND_BLANK (result);
3902 string_append (result, "unsigned");
3903 break;
3904 case 'S': /* signed char only */
3905 (*mangled)++;
3906 APPEND_BLANK (result);
3907 string_append (result, "signed");
3908 break;
3909 case 'J':
3910 (*mangled)++;
3911 APPEND_BLANK (result);
3912 string_append (result, "__complex");
3913 break;
3914 default:
3915 done = 1;
3916 break;
3920 /* Now pick off the fundamental type. There can be only one. */
3922 switch (**mangled)
3924 case '\0':
3925 case '_':
3926 break;
3927 case 'v':
3928 (*mangled)++;
3929 APPEND_BLANK (result);
3930 string_append (result, "void");
3931 break;
3932 case 'x':
3933 (*mangled)++;
3934 APPEND_BLANK (result);
3935 string_append (result, "long long");
3936 break;
3937 case 'l':
3938 (*mangled)++;
3939 APPEND_BLANK (result);
3940 string_append (result, "long");
3941 break;
3942 case 'i':
3943 (*mangled)++;
3944 APPEND_BLANK (result);
3945 string_append (result, "int");
3946 break;
3947 case 's':
3948 (*mangled)++;
3949 APPEND_BLANK (result);
3950 string_append (result, "short");
3951 break;
3952 case 'b':
3953 (*mangled)++;
3954 APPEND_BLANK (result);
3955 string_append (result, "bool");
3956 tk = tk_bool;
3957 break;
3958 case 'c':
3959 (*mangled)++;
3960 APPEND_BLANK (result);
3961 string_append (result, "char");
3962 tk = tk_char;
3963 break;
3964 case 'w':
3965 (*mangled)++;
3966 APPEND_BLANK (result);
3967 string_append (result, "wchar_t");
3968 tk = tk_char;
3969 break;
3970 case 'r':
3971 (*mangled)++;
3972 APPEND_BLANK (result);
3973 string_append (result, "long double");
3974 tk = tk_real;
3975 break;
3976 case 'd':
3977 (*mangled)++;
3978 APPEND_BLANK (result);
3979 string_append (result, "double");
3980 tk = tk_real;
3981 break;
3982 case 'f':
3983 (*mangled)++;
3984 APPEND_BLANK (result);
3985 string_append (result, "float");
3986 tk = tk_real;
3987 break;
3988 case 'G':
3989 (*mangled)++;
3990 if (!ISDIGIT ((unsigned char)**mangled))
3992 success = 0;
3993 break;
3995 case 'I':
3996 (*mangled)++;
3997 if (**mangled == '_')
3999 int i;
4000 (*mangled)++;
4001 for (i = 0;
4002 i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
4003 (*mangled)++, i++)
4004 buf[i] = **mangled;
4005 if (**mangled != '_')
4007 success = 0;
4008 break;
4010 buf[i] = '\0';
4011 (*mangled)++;
4013 else
4015 strncpy (buf, *mangled, 2);
4016 buf[2] = '\0';
4017 *mangled += min (strlen (*mangled), 2);
4019 sscanf (buf, "%x", &dec);
4020 sprintf (buf, "int%u_t", dec);
4021 APPEND_BLANK (result);
4022 string_append (result, buf);
4023 break;
4025 /* fall through */
4026 /* An explicit type, such as "6mytype" or "7integer" */
4027 case '0':
4028 case '1':
4029 case '2':
4030 case '3':
4031 case '4':
4032 case '5':
4033 case '6':
4034 case '7':
4035 case '8':
4036 case '9':
4038 int bindex = register_Btype (work);
4039 string btype;
4040 string_init (&btype);
4041 if (demangle_class_name (work, mangled, &btype)) {
4042 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
4043 APPEND_BLANK (result);
4044 string_appends (result, &btype);
4046 else
4047 success = 0;
4048 string_delete (&btype);
4049 break;
4051 case 't':
4053 string btype;
4054 string_init (&btype);
4055 success = demangle_template (work, mangled, &btype, 0, 1, 1);
4056 string_appends (result, &btype);
4057 string_delete (&btype);
4058 break;
4060 default:
4061 success = 0;
4062 break;
4065 return success ? ((int) tk) : 0;
4069 /* Handle a template's value parameter for HP aCC (extension from ARM)
4070 **mangled points to 'S' or 'U' */
4072 static int
4073 do_hpacc_template_const_value (struct work_stuff *work ATTRIBUTE_UNUSED,
4074 const char **mangled, string *result)
4076 int unsigned_const;
4078 if (**mangled != 'U' && **mangled != 'S')
4079 return 0;
4081 unsigned_const = (**mangled == 'U');
4083 (*mangled)++;
4085 switch (**mangled)
4087 case 'N':
4088 string_append (result, "-");
4089 /* fall through */
4090 case 'P':
4091 (*mangled)++;
4092 break;
4093 case 'M':
4094 /* special case for -2^31 */
4095 string_append (result, "-2147483648");
4096 (*mangled)++;
4097 return 1;
4098 default:
4099 return 0;
4102 /* We have to be looking at an integer now */
4103 if (!(ISDIGIT ((unsigned char)**mangled)))
4104 return 0;
4106 /* We only deal with integral values for template
4107 parameters -- so it's OK to look only for digits */
4108 while (ISDIGIT ((unsigned char)**mangled))
4110 char_str[0] = **mangled;
4111 string_append (result, char_str);
4112 (*mangled)++;
4115 if (unsigned_const)
4116 string_append (result, "U");
4118 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4119 with L or LL suffixes. pai/1997-09-03 */
4121 return 1; /* success */
4124 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4125 **mangled is pointing to the 'A' */
4127 static int
4128 do_hpacc_template_literal (struct work_stuff *work, const char **mangled,
4129 string *result)
4131 int literal_len = 0;
4132 char * recurse;
4133 char * recurse_dem;
4135 if (**mangled != 'A')
4136 return 0;
4138 (*mangled)++;
4140 literal_len = consume_count (mangled);
4142 if (literal_len <= 0)
4143 return 0;
4145 /* Literal parameters are names of arrays, functions, etc. and the
4146 canonical representation uses the address operator */
4147 string_append (result, "&");
4149 /* Now recursively demangle the literal name */
4150 recurse = XNEWVEC (char, literal_len + 1);
4151 memcpy (recurse, *mangled, literal_len);
4152 recurse[literal_len] = '\000';
4154 recurse_dem = cplus_demangle (recurse, work->options);
4156 if (recurse_dem)
4158 string_append (result, recurse_dem);
4159 free (recurse_dem);
4161 else
4163 string_appendn (result, *mangled, literal_len);
4165 (*mangled) += literal_len;
4166 free (recurse);
4168 return 1;
4171 static int
4172 snarf_numeric_literal (const char **args, string *arg)
4174 if (**args == '-')
4176 char_str[0] = '-';
4177 string_append (arg, char_str);
4178 (*args)++;
4180 else if (**args == '+')
4181 (*args)++;
4183 if (!ISDIGIT ((unsigned char)**args))
4184 return 0;
4186 while (ISDIGIT ((unsigned char)**args))
4188 char_str[0] = **args;
4189 string_append (arg, char_str);
4190 (*args)++;
4193 return 1;
4196 /* Demangle the next argument, given by MANGLED into RESULT, which
4197 *should be an uninitialized* string. It will be initialized here,
4198 and free'd should anything go wrong. */
4200 static int
4201 do_arg (struct work_stuff *work, const char **mangled, string *result)
4203 /* Remember where we started so that we can record the type, for
4204 non-squangling type remembering. */
4205 const char *start = *mangled;
4207 string_init (result);
4209 if (work->nrepeats > 0)
4211 --work->nrepeats;
4213 if (work->previous_argument == 0)
4214 return 0;
4216 /* We want to reissue the previous type in this argument list. */
4217 string_appends (result, work->previous_argument);
4218 return 1;
4221 if (**mangled == 'n')
4223 /* A squangling-style repeat. */
4224 (*mangled)++;
4225 work->nrepeats = consume_count(mangled);
4227 if (work->nrepeats <= 0)
4228 /* This was not a repeat count after all. */
4229 return 0;
4231 if (work->nrepeats > 9)
4233 if (**mangled != '_')
4234 /* The repeat count should be followed by an '_' in this
4235 case. */
4236 return 0;
4237 else
4238 (*mangled)++;
4241 /* Now, the repeat is all set up. */
4242 return do_arg (work, mangled, result);
4245 /* Save the result in WORK->previous_argument so that we can find it
4246 if it's repeated. Note that saving START is not good enough: we
4247 do not want to add additional types to the back-referenceable
4248 type vector when processing a repeated type. */
4249 if (work->previous_argument)
4250 string_delete (work->previous_argument);
4251 else
4252 work->previous_argument = XNEW (string);
4254 if (!do_type (work, mangled, work->previous_argument))
4255 return 0;
4257 string_appends (result, work->previous_argument);
4259 remember_type (work, start, *mangled - start);
4260 return 1;
4263 static void
4264 remember_type (struct work_stuff *work, const char *start, int len)
4266 char *tem;
4268 if (work->forgetting_types)
4269 return;
4271 if (work -> ntypes >= work -> typevec_size)
4273 if (work -> typevec_size == 0)
4275 work -> typevec_size = 3;
4276 work -> typevec = XNEWVEC (char *, work->typevec_size);
4278 else
4280 if (work -> typevec_size > INT_MAX / 2)
4281 xmalloc_failed (INT_MAX);
4282 work -> typevec_size *= 2;
4283 work -> typevec
4284 = XRESIZEVEC (char *, work->typevec, work->typevec_size);
4287 tem = XNEWVEC (char, len + 1);
4288 memcpy (tem, start, len);
4289 tem[len] = '\0';
4290 work -> typevec[work -> ntypes++] = tem;
4294 /* Remember a K type class qualifier. */
4295 static void
4296 remember_Ktype (struct work_stuff *work, const char *start, int len)
4298 char *tem;
4300 if (work -> numk >= work -> ksize)
4302 if (work -> ksize == 0)
4304 work -> ksize = 5;
4305 work -> ktypevec = XNEWVEC (char *, work->ksize);
4307 else
4309 if (work -> ksize > INT_MAX / 2)
4310 xmalloc_failed (INT_MAX);
4311 work -> ksize *= 2;
4312 work -> ktypevec
4313 = XRESIZEVEC (char *, work->ktypevec, work->ksize);
4316 tem = XNEWVEC (char, len + 1);
4317 memcpy (tem, start, len);
4318 tem[len] = '\0';
4319 work -> ktypevec[work -> numk++] = tem;
4322 /* Register a B code, and get an index for it. B codes are registered
4323 as they are seen, rather than as they are completed, so map<temp<char> >
4324 registers map<temp<char> > as B0, and temp<char> as B1 */
4326 static int
4327 register_Btype (struct work_stuff *work)
4329 int ret;
4331 if (work -> numb >= work -> bsize)
4333 if (work -> bsize == 0)
4335 work -> bsize = 5;
4336 work -> btypevec = XNEWVEC (char *, work->bsize);
4338 else
4340 if (work -> bsize > INT_MAX / 2)
4341 xmalloc_failed (INT_MAX);
4342 work -> bsize *= 2;
4343 work -> btypevec
4344 = XRESIZEVEC (char *, work->btypevec, work->bsize);
4347 ret = work -> numb++;
4348 work -> btypevec[ret] = NULL;
4349 return(ret);
4352 /* Store a value into a previously registered B code type. */
4354 static void
4355 remember_Btype (struct work_stuff *work, const char *start,
4356 int len, int index)
4358 char *tem;
4360 tem = XNEWVEC (char, len + 1);
4361 memcpy (tem, start, len);
4362 tem[len] = '\0';
4363 work -> btypevec[index] = tem;
4366 /* Lose all the info related to B and K type codes. */
4367 static void
4368 forget_B_and_K_types (struct work_stuff *work)
4370 int i;
4372 while (work -> numk > 0)
4374 i = --(work -> numk);
4375 if (work -> ktypevec[i] != NULL)
4377 free (work -> ktypevec[i]);
4378 work -> ktypevec[i] = NULL;
4382 while (work -> numb > 0)
4384 i = --(work -> numb);
4385 if (work -> btypevec[i] != NULL)
4387 free (work -> btypevec[i]);
4388 work -> btypevec[i] = NULL;
4392 /* Forget the remembered types, but not the type vector itself. */
4394 static void
4395 forget_types (struct work_stuff *work)
4397 int i;
4399 while (work -> ntypes > 0)
4401 i = --(work -> ntypes);
4402 if (work -> typevec[i] != NULL)
4404 free (work -> typevec[i]);
4405 work -> typevec[i] = NULL;
4410 /* Process the argument list part of the signature, after any class spec
4411 has been consumed, as well as the first 'F' character (if any). For
4412 example:
4414 "__als__3fooRT0" => process "RT0"
4415 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4417 DECLP must be already initialised, usually non-empty. It won't be freed
4418 on failure.
4420 Note that g++ differs significantly from ARM and lucid style mangling
4421 with regards to references to previously seen types. For example, given
4422 the source fragment:
4424 class foo {
4425 public:
4426 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4429 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4430 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4432 g++ produces the names:
4434 __3fooiRT0iT2iT2
4435 foo__FiR3fooiT1iT1
4437 while lcc (and presumably other ARM style compilers as well) produces:
4439 foo__FiR3fooT1T2T1T2
4440 __ct__3fooFiR3fooT1T2T1T2
4442 Note that g++ bases its type numbers starting at zero and counts all
4443 previously seen types, while lucid/ARM bases its type numbers starting
4444 at one and only considers types after it has seen the 'F' character
4445 indicating the start of the function args. For lucid/ARM style, we
4446 account for this difference by discarding any previously seen types when
4447 we see the 'F' character, and subtracting one from the type number
4448 reference.
4452 static int
4453 demangle_args (struct work_stuff *work, const char **mangled,
4454 string *declp)
4456 string arg;
4457 int need_comma = 0;
4458 int r;
4459 int t;
4460 const char *tem;
4461 char temptype;
4463 if (PRINT_ARG_TYPES)
4465 string_append (declp, "(");
4466 if (**mangled == '\0')
4468 string_append (declp, "void");
4472 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
4473 || work->nrepeats > 0)
4475 if ((**mangled == 'N') || (**mangled == 'T'))
4477 temptype = *(*mangled)++;
4479 if (temptype == 'N')
4481 if (!get_count (mangled, &r))
4483 return (0);
4486 else
4488 r = 1;
4490 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
4492 /* If we have 10 or more types we might have more than a 1 digit
4493 index so we'll have to consume the whole count here. This
4494 will lose if the next thing is a type name preceded by a
4495 count but it's impossible to demangle that case properly
4496 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4497 Pc, ...)" or "(..., type12, char *, ...)" */
4498 if ((t = consume_count(mangled)) <= 0)
4500 return (0);
4503 else
4505 if (!get_count (mangled, &t))
4507 return (0);
4510 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4512 t--;
4514 /* Validate the type index. Protect against illegal indices from
4515 malformed type strings. */
4516 if ((t < 0) || (t >= work -> ntypes))
4518 return (0);
4520 while (work->nrepeats > 0 || --r >= 0)
4522 tem = work -> typevec[t];
4523 if (need_comma && PRINT_ARG_TYPES)
4525 string_append (declp, ", ");
4527 if (!do_arg (work, &tem, &arg))
4529 return (0);
4531 if (PRINT_ARG_TYPES)
4533 string_appends (declp, &arg);
4535 string_delete (&arg);
4536 need_comma = 1;
4539 else
4541 if (need_comma && PRINT_ARG_TYPES)
4542 string_append (declp, ", ");
4543 if (!do_arg (work, mangled, &arg))
4544 return (0);
4545 if (PRINT_ARG_TYPES)
4546 string_appends (declp, &arg);
4547 string_delete (&arg);
4548 need_comma = 1;
4552 if (**mangled == 'e')
4554 (*mangled)++;
4555 if (PRINT_ARG_TYPES)
4557 if (need_comma)
4559 string_append (declp, ",");
4561 string_append (declp, "...");
4565 if (PRINT_ARG_TYPES)
4567 string_append (declp, ")");
4569 return (1);
4572 /* Like demangle_args, but for demangling the argument lists of function
4573 and method pointers or references, not top-level declarations. */
4575 static int
4576 demangle_nested_args (struct work_stuff *work, const char **mangled,
4577 string *declp)
4579 string* saved_previous_argument;
4580 int result;
4581 int saved_nrepeats;
4583 /* The G++ name-mangling algorithm does not remember types on nested
4584 argument lists, unless -fsquangling is used, and in that case the
4585 type vector updated by remember_type is not used. So, we turn
4586 off remembering of types here. */
4587 ++work->forgetting_types;
4589 /* For the repeat codes used with -fsquangling, we must keep track of
4590 the last argument. */
4591 saved_previous_argument = work->previous_argument;
4592 saved_nrepeats = work->nrepeats;
4593 work->previous_argument = 0;
4594 work->nrepeats = 0;
4596 /* Actually demangle the arguments. */
4597 result = demangle_args (work, mangled, declp);
4599 /* Restore the previous_argument field. */
4600 if (work->previous_argument)
4602 string_delete (work->previous_argument);
4603 free ((char *) work->previous_argument);
4605 work->previous_argument = saved_previous_argument;
4606 --work->forgetting_types;
4607 work->nrepeats = saved_nrepeats;
4609 return result;
4612 /* Returns 1 if a valid function name was found or 0 otherwise. */
4614 static int
4615 demangle_function_name (struct work_stuff *work, const char **mangled,
4616 string *declp, const char *scan)
4618 size_t i;
4619 string type;
4620 const char *tem;
4622 string_appendn (declp, (*mangled), scan - (*mangled));
4623 string_need (declp, 1);
4624 *(declp -> p) = '\0';
4626 /* Consume the function name, including the "__" separating the name
4627 from the signature. We are guaranteed that SCAN points to the
4628 separator. */
4630 (*mangled) = scan + 2;
4631 /* We may be looking at an instantiation of a template function:
4632 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4633 following _F marks the start of the function arguments. Handle
4634 the template arguments first. */
4636 if (HP_DEMANGLING && (**mangled == 'X'))
4638 demangle_arm_hp_template (work, mangled, 0, declp);
4639 /* This leaves MANGLED pointing to the 'F' marking func args */
4642 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4645 /* See if we have an ARM style constructor or destructor operator.
4646 If so, then just record it, clear the decl, and return.
4647 We can't build the actual constructor/destructor decl until later,
4648 when we recover the class name from the signature. */
4650 if (strcmp (declp -> b, "__ct") == 0)
4652 work -> constructor += 1;
4653 string_clear (declp);
4654 return 1;
4656 else if (strcmp (declp -> b, "__dt") == 0)
4658 work -> destructor += 1;
4659 string_clear (declp);
4660 return 1;
4664 if (declp->p - declp->b >= 3
4665 && declp->b[0] == 'o'
4666 && declp->b[1] == 'p'
4667 && strchr (cplus_markers, declp->b[2]) != NULL)
4669 /* see if it's an assignment expression */
4670 if (declp->p - declp->b >= 10 /* op$assign_ */
4671 && memcmp (declp->b + 3, "assign_", 7) == 0)
4673 for (i = 0; i < ARRAY_SIZE (optable); i++)
4675 int len = declp->p - declp->b - 10;
4676 if ((int) strlen (optable[i].in) == len
4677 && memcmp (optable[i].in, declp->b + 10, len) == 0)
4679 string_clear (declp);
4680 string_append (declp, "operator");
4681 string_append (declp, optable[i].out);
4682 string_append (declp, "=");
4683 break;
4687 else
4689 for (i = 0; i < ARRAY_SIZE (optable); i++)
4691 int len = declp->p - declp->b - 3;
4692 if ((int) strlen (optable[i].in) == len
4693 && memcmp (optable[i].in, declp->b + 3, len) == 0)
4695 string_clear (declp);
4696 string_append (declp, "operator");
4697 string_append (declp, optable[i].out);
4698 break;
4703 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4704 && strchr (cplus_markers, declp->b[4]) != NULL)
4706 /* type conversion operator */
4707 tem = declp->b + 5;
4708 if (do_type (work, &tem, &type))
4710 string_clear (declp);
4711 string_append (declp, "operator ");
4712 string_appends (declp, &type);
4713 string_delete (&type);
4716 else if (declp->b[0] == '_' && declp->b[1] == '_'
4717 && declp->b[2] == 'o' && declp->b[3] == 'p')
4719 /* ANSI. */
4720 /* type conversion operator. */
4721 tem = declp->b + 4;
4722 if (do_type (work, &tem, &type))
4724 string_clear (declp);
4725 string_append (declp, "operator ");
4726 string_appends (declp, &type);
4727 string_delete (&type);
4730 else if (declp->b[0] == '_' && declp->b[1] == '_'
4731 && ISLOWER((unsigned char)declp->b[2])
4732 && ISLOWER((unsigned char)declp->b[3]))
4734 if (declp->b[4] == '\0')
4736 /* Operator. */
4737 for (i = 0; i < ARRAY_SIZE (optable); i++)
4739 if (strlen (optable[i].in) == 2
4740 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4742 string_clear (declp);
4743 string_append (declp, "operator");
4744 string_append (declp, optable[i].out);
4745 break;
4749 else
4751 if (declp->b[2] == 'a' && declp->b[5] == '\0')
4753 /* Assignment. */
4754 for (i = 0; i < ARRAY_SIZE (optable); i++)
4756 if (strlen (optable[i].in) == 3
4757 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4759 string_clear (declp);
4760 string_append (declp, "operator");
4761 string_append (declp, optable[i].out);
4762 break;
4769 /* If a function name was obtained but it's not valid, we were not
4770 successful. */
4771 if (LEN_STRING (declp) == 1 && declp->b[0] == '.')
4772 return 0;
4773 else
4774 return 1;
4777 /* a mini string-handling package */
4779 static void
4780 string_need (string *s, int n)
4782 int tem;
4784 if (s->b == NULL)
4786 if (n < 32)
4788 n = 32;
4790 s->p = s->b = XNEWVEC (char, n);
4791 s->e = s->b + n;
4793 else if (s->e - s->p < n)
4795 tem = s->p - s->b;
4796 if (n > INT_MAX / 2 - tem)
4797 xmalloc_failed (INT_MAX);
4798 n += tem;
4799 n *= 2;
4800 s->b = XRESIZEVEC (char, s->b, n);
4801 s->p = s->b + tem;
4802 s->e = s->b + n;
4806 static void
4807 string_delete (string *s)
4809 if (s->b != NULL)
4811 free (s->b);
4812 s->b = s->e = s->p = NULL;
4816 static void
4817 string_init (string *s)
4819 s->b = s->p = s->e = NULL;
4822 static void
4823 string_clear (string *s)
4825 s->p = s->b;
4828 #if 0
4830 static int
4831 string_empty (string *s)
4833 return (s->b == s->p);
4836 #endif
4838 static void
4839 string_append (string *p, const char *s)
4841 int n;
4842 if (s == NULL || *s == '\0')
4843 return;
4844 n = strlen (s);
4845 string_need (p, n);
4846 memcpy (p->p, s, n);
4847 p->p += n;
4850 static void
4851 string_appends (string *p, string *s)
4853 int n;
4855 if (s->b != s->p)
4857 n = s->p - s->b;
4858 string_need (p, n);
4859 memcpy (p->p, s->b, n);
4860 p->p += n;
4864 static void
4865 string_appendn (string *p, const char *s, int n)
4867 if (n != 0)
4869 string_need (p, n);
4870 memcpy (p->p, s, n);
4871 p->p += n;
4875 static void
4876 string_prepend (string *p, const char *s)
4878 if (s != NULL && *s != '\0')
4880 string_prependn (p, s, strlen (s));
4884 static void
4885 string_prepends (string *p, string *s)
4887 if (s->b != s->p)
4889 string_prependn (p, s->b, s->p - s->b);
4893 static void
4894 string_prependn (string *p, const char *s, int n)
4896 char *q;
4898 if (n != 0)
4900 string_need (p, n);
4901 for (q = p->p - 1; q >= p->b; q--)
4903 q[n] = q[0];
4905 memcpy (p->b, s, n);
4906 p->p += n;
4910 static void
4911 string_append_template_idx (string *s, int idx)
4913 char buf[INTBUF_SIZE + 1 /* 'T' */];
4914 sprintf(buf, "T%d", idx);
4915 string_append (s, buf);