1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000 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 Libiberty is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with libiberty; see the file COPYING.LIB. If
21 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
26 This file imports xmalloc and xrealloc, which are like malloc and
27 realloc except that they generate a fatal error if there is no
30 /* This file lives in both GCC and libiberty. When making changes, please
31 try not to break either. */
38 #include <sys/types.h>
50 #undef CURRENT_DEMANGLING_STYLE
51 #define CURRENT_DEMANGLING_STYLE work->options
53 #include "libiberty.h"
55 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
57 /* A value at least one greater than the maximum number of characters
58 that will be output when using the `%d' format with `printf'. */
59 #define INTBUF_SIZE 32
61 extern void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
63 static const char *mystrstr
PARAMS ((const char *, const char *));
69 register const char *p
= s1
;
70 register int len
= strlen (s2
);
72 for (; (p
= strchr (p
, *s2
)) != 0; p
++)
74 if (strncmp (p
, s2
, len
) == 0)
82 /* In order to allow a single demangler executable to demangle strings
83 using various common values of CPLUS_MARKER, as well as any specific
84 one set at compile time, we maintain a string containing all the
85 commonly used ones, and check to see if the marker we are looking for
86 is in that string. CPLUS_MARKER is usually '$' on systems where the
87 assembler can deal with that. Where the assembler can't, it's usually
88 '.' (but on many systems '.' is used for other things). We put the
89 current defined CPLUS_MARKER first (which defaults to '$'), followed
90 by the next most common value, followed by an explicit '$' in case
91 the value of CPLUS_MARKER is not '$'.
93 We could avoid this if we could just get g++ to tell us what the actual
94 cplus marker character is as part of the debug information, perhaps by
95 ensuring that it is the character that terminates the gcc<n>_compiled
96 marker symbol (FIXME). */
98 #if !defined (CPLUS_MARKER)
99 #define CPLUS_MARKER '$'
102 enum demangling_styles current_demangling_style
= gnu_demangling
;
104 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
106 static char char_str
[2] = { '\000', '\000' };
109 set_cplus_marker_for_demangling (ch
)
112 cplus_markers
[0] = ch
;
115 typedef struct string
/* Beware: these aren't required to be */
116 { /* '\0' terminated. */
117 char *b
; /* pointer to start of string */
118 char *p
; /* pointer after last character */
119 char *e
; /* pointer after end of allocated space */
122 /* Stuff that is shared between sub-routines.
123 Using a shared structure allows cplus_demangle to be reentrant. */
139 int static_type
; /* A static member function */
140 int temp_start
; /* index in demangled to start of template args */
141 int type_quals
; /* The type qualifiers. */
142 int dllimported
; /* Symbol imported from a PE DLL */
143 char **tmpl_argvec
; /* Template function arguments. */
144 int ntmpl_args
; /* The number of template function arguments. */
145 int forgetting_types
; /* Nonzero if we are not remembering the types
147 string
* previous_argument
; /* The last function argument demangled. */
148 int nrepeats
; /* The number of times to repeat the previous
152 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
153 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
155 static const struct optable
161 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
162 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
163 {"new", " new", 0}, /* old (1.91, and 1.x) */
164 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
165 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
166 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
167 {"as", "=", DMGL_ANSI
}, /* ansi */
168 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
169 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
170 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
171 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
172 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
173 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
174 {"plus", "+", 0}, /* old */
175 {"pl", "+", DMGL_ANSI
}, /* ansi */
176 {"apl", "+=", DMGL_ANSI
}, /* ansi */
177 {"minus", "-", 0}, /* old */
178 {"mi", "-", DMGL_ANSI
}, /* ansi */
179 {"ami", "-=", DMGL_ANSI
}, /* ansi */
180 {"mult", "*", 0}, /* old */
181 {"ml", "*", DMGL_ANSI
}, /* ansi */
182 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
183 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
184 {"convert", "+", 0}, /* old (unary +) */
185 {"negate", "-", 0}, /* old (unary -) */
186 {"trunc_mod", "%", 0}, /* old */
187 {"md", "%", DMGL_ANSI
}, /* ansi */
188 {"amd", "%=", DMGL_ANSI
}, /* ansi */
189 {"trunc_div", "/", 0}, /* old */
190 {"dv", "/", DMGL_ANSI
}, /* ansi */
191 {"adv", "/=", DMGL_ANSI
}, /* ansi */
192 {"truth_andif", "&&", 0}, /* old */
193 {"aa", "&&", DMGL_ANSI
}, /* ansi */
194 {"truth_orif", "||", 0}, /* old */
195 {"oo", "||", DMGL_ANSI
}, /* ansi */
196 {"truth_not", "!", 0}, /* old */
197 {"nt", "!", DMGL_ANSI
}, /* ansi */
198 {"postincrement","++", 0}, /* old */
199 {"pp", "++", DMGL_ANSI
}, /* ansi */
200 {"postdecrement","--", 0}, /* old */
201 {"mm", "--", DMGL_ANSI
}, /* ansi */
202 {"bit_ior", "|", 0}, /* old */
203 {"or", "|", DMGL_ANSI
}, /* ansi */
204 {"aor", "|=", DMGL_ANSI
}, /* ansi */
205 {"bit_xor", "^", 0}, /* old */
206 {"er", "^", DMGL_ANSI
}, /* ansi */
207 {"aer", "^=", DMGL_ANSI
}, /* ansi */
208 {"bit_and", "&", 0}, /* old */
209 {"ad", "&", DMGL_ANSI
}, /* ansi */
210 {"aad", "&=", DMGL_ANSI
}, /* ansi */
211 {"bit_not", "~", 0}, /* old */
212 {"co", "~", DMGL_ANSI
}, /* ansi */
213 {"call", "()", 0}, /* old */
214 {"cl", "()", DMGL_ANSI
}, /* ansi */
215 {"alshift", "<<", 0}, /* old */
216 {"ls", "<<", DMGL_ANSI
}, /* ansi */
217 {"als", "<<=", DMGL_ANSI
}, /* ansi */
218 {"arshift", ">>", 0}, /* old */
219 {"rs", ">>", DMGL_ANSI
}, /* ansi */
220 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
221 {"component", "->", 0}, /* old */
222 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
223 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
224 {"indirect", "*", 0}, /* old */
225 {"method_call", "->()", 0}, /* old */
226 {"addr", "&", 0}, /* old (unary &) */
227 {"array", "[]", 0}, /* old */
228 {"vc", "[]", DMGL_ANSI
}, /* ansi */
229 {"compound", ", ", 0}, /* old */
230 {"cm", ", ", DMGL_ANSI
}, /* ansi */
231 {"cond", "?:", 0}, /* old */
232 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
233 {"max", ">?", 0}, /* old */
234 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
235 {"min", "<?", 0}, /* old */
236 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
237 {"nop", "", 0}, /* old (for operator=) */
238 {"rm", "->*", DMGL_ANSI
}, /* ansi */
239 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
242 /* These values are used to indicate the various type varieties.
243 They are all non-zero so that they can be used as `success'
245 typedef enum type_kind_t
256 struct demangler_engine libiberty_demanglers
[] =
259 AUTO_DEMANGLING_STYLE_STRING
,
261 "Automatic selection based on executable"
265 GNU_DEMANGLING_STYLE_STRING
,
267 "GNU (g++) style demangling"
271 LUCID_DEMANGLING_STYLE_STRING
,
273 "Lucid (lcc) style demangling"
277 ARM_DEMANGLING_STYLE_STRING
,
279 "ARM style demangling"
283 HP_DEMANGLING_STYLE_STRING
,
285 "HP (aCC) style demangling"
289 EDG_DEMANGLING_STYLE_STRING
,
291 "EDG style demangling"
295 GNU_NEW_ABI_DEMANGLING_STYLE_STRING
,
296 gnu_new_abi_demangling
,
297 "GNU (g++) new-ABI-style demangling"
301 NULL
, unknown_demangling
, NULL
305 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
306 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
307 string_prepend(str, " ");}
308 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
309 string_append(str, " ");}
310 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
312 /* The scope separator appropriate for the language being demangled. */
314 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
316 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
317 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
319 /* Prototypes for local functions */
322 delete_work_stuff
PARAMS ((struct work_stuff
*));
325 delete_non_B_K_work_stuff
PARAMS ((struct work_stuff
*));
328 mop_up
PARAMS ((struct work_stuff
*, string
*, int));
331 squangle_mop_up
PARAMS ((struct work_stuff
*));
334 work_stuff_copy_to_from
PARAMS ((struct work_stuff
*, struct work_stuff
*));
338 demangle_method_args
PARAMS ((struct work_stuff
*, const char **, string
*));
342 internal_cplus_demangle
PARAMS ((struct work_stuff
*, const char *));
345 demangle_template_template_parm
PARAMS ((struct work_stuff
*work
,
346 const char **, string
*));
349 demangle_template
PARAMS ((struct work_stuff
*work
, const char **, string
*,
350 string
*, int, int));
353 arm_pt
PARAMS ((struct work_stuff
*, const char *, int, const char **,
357 demangle_class_name
PARAMS ((struct work_stuff
*, const char **, string
*));
360 demangle_qualified
PARAMS ((struct work_stuff
*, const char **, string
*,
364 demangle_class
PARAMS ((struct work_stuff
*, const char **, string
*));
367 demangle_fund_type
PARAMS ((struct work_stuff
*, const char **, string
*));
370 demangle_signature
PARAMS ((struct work_stuff
*, const char **, string
*));
373 demangle_prefix
PARAMS ((struct work_stuff
*, const char **, string
*));
376 gnu_special
PARAMS ((struct work_stuff
*, const char **, string
*));
379 arm_special
PARAMS ((const char **, string
*));
382 string_need
PARAMS ((string
*, int));
385 string_delete
PARAMS ((string
*));
388 string_init
PARAMS ((string
*));
391 string_clear
PARAMS ((string
*));
395 string_empty
PARAMS ((string
*));
399 string_append
PARAMS ((string
*, const char *));
402 string_appends
PARAMS ((string
*, string
*));
405 string_appendn
PARAMS ((string
*, const char *, int));
408 string_prepend
PARAMS ((string
*, const char *));
411 string_prependn
PARAMS ((string
*, const char *, int));
414 string_append_template_idx
PARAMS ((string
*, int));
417 get_count
PARAMS ((const char **, int *));
420 consume_count
PARAMS ((const char **));
423 consume_count_with_underscores
PARAMS ((const char**));
426 demangle_args
PARAMS ((struct work_stuff
*, const char **, string
*));
429 demangle_nested_args
PARAMS ((struct work_stuff
*, const char**, string
*));
432 do_type
PARAMS ((struct work_stuff
*, const char **, string
*));
435 do_arg
PARAMS ((struct work_stuff
*, const char **, string
*));
438 demangle_function_name
PARAMS ((struct work_stuff
*, const char **, string
*,
442 iterate_demangle_function
PARAMS ((struct work_stuff
*,
443 const char **, string
*, const char *));
446 remember_type
PARAMS ((struct work_stuff
*, const char *, int));
449 remember_Btype
PARAMS ((struct work_stuff
*, const char *, int, int));
452 register_Btype
PARAMS ((struct work_stuff
*));
455 remember_Ktype
PARAMS ((struct work_stuff
*, const char *, int));
458 forget_types
PARAMS ((struct work_stuff
*));
461 forget_B_and_K_types
PARAMS ((struct work_stuff
*));
464 string_prepends
PARAMS ((string
*, string
*));
467 demangle_template_value_parm
PARAMS ((struct work_stuff
*, const char**,
468 string
*, type_kind_t
));
471 do_hpacc_template_const_value
PARAMS ((struct work_stuff
*, const char **, string
*));
474 do_hpacc_template_literal
PARAMS ((struct work_stuff
*, const char **, string
*));
477 snarf_numeric_literal
PARAMS ((const char **, string
*));
479 /* There is a TYPE_QUAL value for each type qualifier. They can be
480 combined by bitwise-or to form the complete set of qualifiers for a
483 #define TYPE_UNQUALIFIED 0x0
484 #define TYPE_QUAL_CONST 0x1
485 #define TYPE_QUAL_VOLATILE 0x2
486 #define TYPE_QUAL_RESTRICT 0x4
489 code_for_qualifier
PARAMS ((int));
492 qualifier_string
PARAMS ((int));
495 demangle_qualifier
PARAMS ((int));
498 demangle_expression
PARAMS ((struct work_stuff
*, const char **, string
*,
502 demangle_integral_value
PARAMS ((struct work_stuff
*, const char **,
506 demangle_real_value
PARAMS ((struct work_stuff
*, const char **, string
*));
509 demangle_arm_hp_template
PARAMS ((struct work_stuff
*, const char **, int,
513 recursively_demangle
PARAMS ((struct work_stuff
*, const char **, string
*,
516 /* Translate count to integer, consuming tokens in the process.
517 Conversion terminates on the first non-digit character.
519 Trying to consume something that isn't a count results in no
520 consumption of input and a return of -1.
522 Overflow consumes the rest of the digits, and returns -1. */
530 if (! isdigit ((unsigned char)**type
))
533 while (isdigit ((unsigned char)**type
))
537 /* Check for overflow.
538 We assume that count is represented using two's-complement;
539 no power of two is divisible by ten, so if an overflow occurs
540 when multiplying by ten, the result will not be a multiple of
542 if ((count
% 10) != 0)
544 while (isdigit ((unsigned char) **type
))
549 count
+= **type
- '0';
557 /* Like consume_count, but for counts that are preceded and followed
558 by '_' if they are greater than 10. Also, -1 is returned for
559 failure, since 0 can be a valid value. */
562 consume_count_with_underscores (mangled
)
563 const char **mangled
;
567 if (**mangled
== '_')
570 if (!isdigit ((unsigned char)**mangled
))
573 idx
= consume_count (mangled
);
574 if (**mangled
!= '_')
575 /* The trailing underscore was missing. */
582 if (**mangled
< '0' || **mangled
> '9')
585 idx
= **mangled
- '0';
592 /* C is the code for a type-qualifier. Return the TYPE_QUAL
593 corresponding to this qualifier. */
596 code_for_qualifier (c
)
602 return TYPE_QUAL_CONST
;
605 return TYPE_QUAL_VOLATILE
;
608 return TYPE_QUAL_RESTRICT
;
614 /* C was an invalid qualifier. */
618 /* Return the string corresponding to the qualifiers given by
622 qualifier_string (type_quals
)
627 case TYPE_UNQUALIFIED
:
630 case TYPE_QUAL_CONST
:
633 case TYPE_QUAL_VOLATILE
:
636 case TYPE_QUAL_RESTRICT
:
639 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
640 return "const volatile";
642 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
643 return "const __restrict";
645 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
646 return "volatile __restrict";
648 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
649 return "const volatile __restrict";
655 /* TYPE_QUALS was an invalid qualifier set. */
659 /* C is the code for a type-qualifier. Return the string
660 corresponding to this qualifier. This function should only be
661 called with a valid qualifier code. */
664 demangle_qualifier (c
)
667 return qualifier_string (code_for_qualifier (c
));
671 cplus_demangle_opname (opname
, result
, options
)
678 struct work_stuff work
[1];
681 len
= strlen(opname
);
684 memset ((char *) work
, 0, sizeof (work
));
685 work
->options
= options
;
687 if (opname
[0] == '_' && opname
[1] == '_'
688 && opname
[2] == 'o' && opname
[3] == 'p')
691 /* type conversion operator. */
693 if (do_type (work
, &tem
, &type
))
695 strcat (result
, "operator ");
696 strncat (result
, type
.b
, type
.p
- type
.b
);
697 string_delete (&type
);
701 else if (opname
[0] == '_' && opname
[1] == '_'
702 && islower((unsigned char)opname
[2])
703 && islower((unsigned char)opname
[3]))
705 if (opname
[4] == '\0')
709 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
711 if (strlen (optable
[i
].in
) == 2
712 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
714 strcat (result
, "operator");
715 strcat (result
, optable
[i
].out
);
723 if (opname
[2] == 'a' && opname
[5] == '\0')
727 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
729 if (strlen (optable
[i
].in
) == 3
730 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
732 strcat (result
, "operator");
733 strcat (result
, optable
[i
].out
);
744 && strchr (cplus_markers
, opname
[2]) != NULL
)
746 /* see if it's an assignment expression */
747 if (len
>= 10 /* op$assign_ */
748 && memcmp (opname
+ 3, "assign_", 7) == 0)
751 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
754 if ((int) strlen (optable
[i
].in
) == len1
755 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
757 strcat (result
, "operator");
758 strcat (result
, optable
[i
].out
);
759 strcat (result
, "=");
768 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
771 if ((int) strlen (optable
[i
].in
) == len1
772 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
774 strcat (result
, "operator");
775 strcat (result
, optable
[i
].out
);
782 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
783 && strchr (cplus_markers
, opname
[4]) != NULL
)
785 /* type conversion operator */
787 if (do_type (work
, &tem
, &type
))
789 strcat (result
, "operator ");
790 strncat (result
, type
.b
, type
.p
- type
.b
);
791 string_delete (&type
);
795 squangle_mop_up (work
);
800 /* Takes operator name as e.g. "++" and returns mangled
801 operator name (e.g. "postincrement_expr"), or NULL if not found.
803 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
804 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
807 cplus_mangle_opname (opname
, options
)
814 len
= strlen (opname
);
815 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
817 if ((int) strlen (optable
[i
].out
) == len
818 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
819 && memcmp (optable
[i
].out
, opname
, len
) == 0)
820 return optable
[i
].in
;
825 /* Add a routine to set the demangling style to be sure it is valid and
826 allow for any demangler initialization that maybe necessary. */
828 enum demangling_styles
829 cplus_demangle_set_style (style
)
830 enum demangling_styles style
;
832 struct demangler_engine
*demangler
= libiberty_demanglers
;
834 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
835 if (style
== demangler
->demangling_style
)
837 current_demangling_style
= style
;
838 return current_demangling_style
;
841 return unknown_demangling
;
844 /* Do string name to style translation */
846 enum demangling_styles
847 cplus_demangle_name_to_style (name
)
850 struct demangler_engine
*demangler
= libiberty_demanglers
;
852 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
853 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
854 return demangler
->demangling_style
;
856 return unknown_demangling
;
859 /* char *cplus_demangle (const char *mangled, int options)
861 If MANGLED is a mangled function name produced by GNU C++, then
862 a pointer to a malloced string giving a C++ representation
863 of the name will be returned; otherwise NULL will be returned.
864 It is the caller's responsibility to free the string which
867 The OPTIONS arg may contain one or more of the following bits:
869 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
871 DMGL_PARAMS Function parameters are included.
875 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
876 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
877 cplus_demangle ("foo__1Ai", 0) => "A::foo"
879 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
880 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
881 cplus_demangle ("foo__1Afe", 0) => "A::foo"
883 Note that any leading underscores, or other such characters prepended by
884 the compilation system, are presumed to have already been stripped from
888 cplus_demangle (mangled
, options
)
893 struct work_stuff work
[1];
894 memset ((char *) work
, 0, sizeof (work
));
895 work
-> options
= options
;
896 if ((work
-> options
& DMGL_STYLE_MASK
) == 0)
897 work
-> options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
899 /* The new-ABI demangling is implemented elsewhere. */
900 if (GNU_NEW_ABI_DEMANGLING
)
901 return cplus_demangle_new_abi (mangled
);
903 ret
= internal_cplus_demangle (work
, mangled
);
904 squangle_mop_up (work
);
909 /* This function performs most of what cplus_demangle use to do, but
910 to be able to demangle a name with a B, K or n code, we need to
911 have a longer term memory of what types have been seen. The original
912 now intializes and cleans up the squangle code info, while internal
913 calls go directly to this routine to avoid resetting that info. */
916 internal_cplus_demangle (work
, mangled
)
917 struct work_stuff
*work
;
923 char *demangled
= NULL
;
925 s1
= work
->constructor
;
926 s2
= work
->destructor
;
927 s3
= work
->static_type
;
928 s4
= work
->type_quals
;
929 work
->constructor
= work
->destructor
= 0;
930 work
->type_quals
= TYPE_UNQUALIFIED
;
931 work
->dllimported
= 0;
933 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
937 /* First check to see if gnu style demangling is active and if the
938 string to be demangled contains a CPLUS_MARKER. If so, attempt to
939 recognize one of the gnu special forms rather than looking for a
940 standard prefix. In particular, don't worry about whether there
941 is a "__" string in the mangled string. Consider "_$_5__foo" for
944 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
946 success
= gnu_special (work
, &mangled
, &decl
);
950 success
= demangle_prefix (work
, &mangled
, &decl
);
952 if (success
&& (*mangled
!= '\0'))
954 success
= demangle_signature (work
, &mangled
, &decl
);
956 if (work
->constructor
== 2)
958 string_prepend (&decl
, "global constructors keyed to ");
959 work
->constructor
= 0;
961 else if (work
->destructor
== 2)
963 string_prepend (&decl
, "global destructors keyed to ");
964 work
->destructor
= 0;
966 else if (work
->dllimported
== 1)
968 string_prepend (&decl
, "import stub for ");
969 work
->dllimported
= 0;
971 demangled
= mop_up (work
, &decl
, success
);
973 work
->constructor
= s1
;
974 work
->destructor
= s2
;
975 work
->static_type
= s3
;
976 work
->type_quals
= s4
;
981 /* Clear out and squangling related storage */
983 squangle_mop_up (work
)
984 struct work_stuff
*work
;
986 /* clean up the B and K type mangling types. */
987 forget_B_and_K_types (work
);
988 if (work
-> btypevec
!= NULL
)
990 free ((char *) work
-> btypevec
);
992 if (work
-> ktypevec
!= NULL
)
994 free ((char *) work
-> ktypevec
);
999 /* Copy the work state and storage. */
1002 work_stuff_copy_to_from (to
, from
)
1003 struct work_stuff
*to
;
1004 struct work_stuff
*from
;
1008 delete_work_stuff (to
);
1010 /* Shallow-copy scalars. */
1011 memcpy (to
, from
, sizeof (*to
));
1013 /* Deep-copy dynamic storage. */
1014 if (from
->typevec_size
)
1016 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1018 for (i
= 0; i
< from
->ntypes
; i
++)
1020 int len
= strlen (from
->typevec
[i
]) + 1;
1022 to
->typevec
[i
] = xmalloc (len
);
1023 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1028 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1030 for (i
= 0; i
< from
->numk
; i
++)
1032 int len
= strlen (from
->ktypevec
[i
]) + 1;
1034 to
->ktypevec
[i
] = xmalloc (len
);
1035 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1040 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1042 for (i
= 0; i
< from
->numb
; i
++)
1044 int len
= strlen (from
->btypevec
[i
]) + 1;
1046 to
->btypevec
[i
] = xmalloc (len
);
1047 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1050 if (from
->ntmpl_args
)
1052 = xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1054 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1056 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1058 to
->tmpl_argvec
[i
] = xmalloc (len
);
1059 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1062 if (from
->previous_argument
)
1064 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1065 string_init (to
->previous_argument
);
1066 string_appends (to
->previous_argument
, from
->previous_argument
);
1071 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1074 delete_non_B_K_work_stuff (work
)
1075 struct work_stuff
*work
;
1077 /* Discard the remembered types, if any. */
1079 forget_types (work
);
1080 if (work
-> typevec
!= NULL
)
1082 free ((char *) work
-> typevec
);
1083 work
-> typevec
= NULL
;
1084 work
-> typevec_size
= 0;
1086 if (work
->tmpl_argvec
)
1090 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1091 if (work
->tmpl_argvec
[i
])
1092 free ((char*) work
->tmpl_argvec
[i
]);
1094 free ((char*) work
->tmpl_argvec
);
1095 work
->tmpl_argvec
= NULL
;
1097 if (work
->previous_argument
)
1099 string_delete (work
->previous_argument
);
1100 free ((char*) work
->previous_argument
);
1101 work
->previous_argument
= NULL
;
1106 /* Delete all dynamic storage in work_stuff. */
1108 delete_work_stuff (work
)
1109 struct work_stuff
*work
;
1111 delete_non_B_K_work_stuff (work
);
1112 squangle_mop_up (work
);
1116 /* Clear out any mangled storage */
1119 mop_up (work
, declp
, success
)
1120 struct work_stuff
*work
;
1124 char *demangled
= NULL
;
1126 delete_non_B_K_work_stuff (work
);
1128 /* If demangling was successful, ensure that the demangled string is null
1129 terminated and return it. Otherwise, free the demangling decl. */
1133 string_delete (declp
);
1137 string_appendn (declp
, "", 1);
1138 demangled
= declp
-> b
;
1147 demangle_signature -- demangle the signature part of a mangled name
1152 demangle_signature (struct work_stuff *work, const char **mangled,
1157 Consume and demangle the signature portion of the mangled name.
1159 DECLP is the string where demangled output is being built. At
1160 entry it contains the demangled root name from the mangled name
1161 prefix. I.E. either a demangled operator name or the root function
1162 name. In some special cases, it may contain nothing.
1164 *MANGLED points to the current unconsumed location in the mangled
1165 name. As tokens are consumed and demangling is performed, the
1166 pointer is updated to continuously point at the next token to
1169 Demangling GNU style mangled names is nasty because there is no
1170 explicit token that marks the start of the outermost function
1174 demangle_signature (work
, mangled
, declp
)
1175 struct work_stuff
*work
;
1176 const char **mangled
;
1181 int expect_func
= 0;
1182 int expect_return_type
= 0;
1183 const char *oldmangled
= NULL
;
1187 while (success
&& (**mangled
!= '\0'))
1192 oldmangled
= *mangled
;
1193 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1195 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1196 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1202 oldmangled
= *mangled
;
1203 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1204 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1212 /* Static member function */
1213 if (oldmangled
== NULL
)
1215 oldmangled
= *mangled
;
1218 work
-> static_type
= 1;
1224 work
->type_quals
|= code_for_qualifier (**mangled
);
1226 /* a qualified member function */
1227 if (oldmangled
== NULL
)
1228 oldmangled
= *mangled
;
1233 /* Local class name follows after "Lnnn_" */
1236 while (**mangled
&& (**mangled
!= '_'))
1247 case '0': case '1': case '2': case '3': case '4':
1248 case '5': case '6': case '7': case '8': case '9':
1249 if (oldmangled
== NULL
)
1251 oldmangled
= *mangled
;
1253 work
->temp_start
= -1; /* uppermost call to demangle_class */
1254 success
= demangle_class (work
, mangled
, declp
);
1257 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1259 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1261 /* EDG and others will have the "F", so we let the loop cycle
1262 if we are looking at one. */
1263 if (**mangled
!= 'F')
1272 success
= do_type (work
, mangled
, &s
);
1275 string_append (&s
, SCOPE_STRING (work
));
1276 string_prepends (declp
, &s
);
1285 /* ARM/HP style demangling includes a specific 'F' character after
1286 the class name. For GNU style, it is just implied. So we can
1287 safely just consume any 'F' at this point and be compatible
1288 with either style. */
1294 /* For lucid/ARM/HP style we have to forget any types we might
1295 have remembered up to this point, since they were not argument
1296 types. GNU style considers all types seen as available for
1297 back references. See comment in demangle_args() */
1299 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1301 forget_types (work
);
1303 success
= demangle_args (work
, mangled
, declp
);
1304 /* After picking off the function args, we expect to either
1305 find the function return type (preceded by an '_') or the
1306 end of the string. */
1307 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1310 /* At this level, we do not care about the return type. */
1311 success
= do_type (work
, mangled
, &tname
);
1312 string_delete (&tname
);
1319 string_init(&trawname
);
1320 string_init(&tname
);
1321 if (oldmangled
== NULL
)
1323 oldmangled
= *mangled
;
1325 success
= demangle_template (work
, mangled
, &tname
,
1329 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1331 string_append (&tname
, SCOPE_STRING (work
));
1333 string_prepends(declp
, &tname
);
1334 if (work
-> destructor
& 1)
1336 string_prepend (&trawname
, "~");
1337 string_appends (declp
, &trawname
);
1338 work
->destructor
-= 1;
1340 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1342 string_appends (declp
, &trawname
);
1343 work
->constructor
-= 1;
1345 string_delete(&trawname
);
1346 string_delete(&tname
);
1352 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1354 /* Read the return type. */
1356 string_init (&return_type
);
1359 success
= do_type (work
, mangled
, &return_type
);
1360 APPEND_BLANK (&return_type
);
1362 string_prepends (declp
, &return_type
);
1363 string_delete (&return_type
);
1367 /* At the outermost level, we cannot have a return type specified,
1368 so if we run into another '_' at this point we are dealing with
1369 a mangled name that is either bogus, or has been mangled by
1370 some algorithm we don't know how to deal with. So just
1371 reject the entire demangling. */
1372 /* However, "_nnn" is an expected suffix for alternate entry point
1373 numbered nnn for a function, with HP aCC, so skip over that
1374 without reporting failure. pai/1997-09-04 */
1378 while (**mangled
&& isdigit ((unsigned char)**mangled
))
1386 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1388 /* A G++ template function. Read the template arguments. */
1389 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1391 if (!(work
->constructor
& 1))
1392 expect_return_type
= 1;
1401 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1403 /* Assume we have stumbled onto the first outermost function
1404 argument token, and start processing args. */
1406 success
= demangle_args (work
, mangled
, declp
);
1410 /* Non-GNU demanglers use a specific token to mark the start
1411 of the outermost function argument tokens. Typically 'F',
1412 for ARM/HP-demangling, for example. So if we find something
1413 we are not prepared for, it must be an error. */
1419 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1422 if (success
&& expect_func
)
1425 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1427 forget_types (work
);
1429 success
= demangle_args (work
, mangled
, declp
);
1430 /* Since template include the mangling of their return types,
1431 we must set expect_func to 0 so that we don't try do
1432 demangle more arguments the next time we get here. */
1437 if (success
&& !func_done
)
1439 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1441 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1442 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1443 first case, and need to ensure that the '(void)' gets added to
1444 the current declp. Note that with ARM/HP, the first case
1445 represents the name of a static data member 'foo::bar',
1446 which is in the current declp, so we leave it alone. */
1447 success
= demangle_args (work
, mangled
, declp
);
1450 if (success
&& PRINT_ARG_TYPES
)
1452 if (work
->static_type
)
1453 string_append (declp
, " static");
1454 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1456 APPEND_BLANK (declp
);
1457 string_append (declp
, qualifier_string (work
->type_quals
));
1467 demangle_method_args (work
, mangled
, declp
)
1468 struct work_stuff
*work
;
1469 const char **mangled
;
1474 if (work
-> static_type
)
1476 string_append (declp
, *mangled
+ 1);
1477 *mangled
+= strlen (*mangled
);
1482 success
= demangle_args (work
, mangled
, declp
);
1490 demangle_template_template_parm (work
, mangled
, tname
)
1491 struct work_stuff
*work
;
1492 const char **mangled
;
1501 string_append (tname
, "template <");
1502 /* get size of template parameter list */
1503 if (get_count (mangled
, &r
))
1505 for (i
= 0; i
< r
; i
++)
1509 string_append (tname
, ", ");
1512 /* Z for type parameters */
1513 if (**mangled
== 'Z')
1516 string_append (tname
, "class");
1518 /* z for template parameters */
1519 else if (**mangled
== 'z')
1523 demangle_template_template_parm (work
, mangled
, tname
);
1531 /* temp is initialized in do_type */
1532 success
= do_type (work
, mangled
, &temp
);
1535 string_appends (tname
, &temp
);
1537 string_delete(&temp
);
1547 if (tname
->p
[-1] == '>')
1548 string_append (tname
, " ");
1549 string_append (tname
, "> class");
1554 demangle_expression (work
, mangled
, s
, tk
)
1555 struct work_stuff
*work
;
1556 const char** mangled
;
1560 int need_operator
= 0;
1564 string_appendn (s
, "(", 1);
1566 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1575 len
= strlen (*mangled
);
1577 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1579 size_t l
= strlen (optable
[i
].in
);
1582 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1584 string_appendn (s
, " ", 1);
1585 string_append (s
, optable
[i
].out
);
1586 string_appendn (s
, " ", 1);
1599 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1602 if (**mangled
!= 'W')
1606 string_appendn (s
, ")", 1);
1614 demangle_integral_value (work
, mangled
, s
)
1615 struct work_stuff
*work
;
1616 const char** mangled
;
1621 if (**mangled
== 'E')
1622 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1623 else if (**mangled
== 'Q' || **mangled
== 'K')
1624 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1629 /* By default, we let the number decide whether we shall consume an
1631 int consume_following_underscore
= 0;
1632 int leave_following_underscore
= 0;
1636 /* Negative numbers are indicated with a leading `m'. */
1637 if (**mangled
== 'm')
1639 string_appendn (s
, "-", 1);
1642 else if (mangled
[0][0] == '_' && mangled
[0][1] == 'm')
1644 /* Since consume_count_with_underscores does not handle the
1645 `m'-prefix we must do it here, using consume_count and
1646 adjusting underscores: we have to consume the underscore
1647 matching the prepended one. */
1648 consume_following_underscore
= 1;
1649 string_appendn (s
, "-", 1);
1652 else if (**mangled
== '_')
1654 /* Do not consume a following underscore;
1655 consume_following_underscore will consume what should be
1657 leave_following_underscore
= 1;
1660 /* We must call consume_count if we expect to remove a trailing
1661 underscore, since consume_count_with_underscores expects
1662 the leading underscore (that we consumed) if it is to handle
1663 multi-digit numbers. */
1664 if (consume_following_underscore
)
1665 value
= consume_count (mangled
);
1667 value
= consume_count_with_underscores (mangled
);
1671 char buf
[INTBUF_SIZE
];
1672 sprintf (buf
, "%d", value
);
1673 string_append (s
, buf
);
1675 /* Numbers not otherwise delimited, might have an underscore
1676 appended as a delimeter, which we should skip.
1678 ??? This used to always remove a following underscore, which
1679 is wrong. If other (arbitrary) cases are followed by an
1680 underscore, we need to do something more radical. */
1682 if ((value
> 9 || consume_following_underscore
)
1683 && ! leave_following_underscore
1684 && **mangled
== '_')
1695 /* Demangle the real value in MANGLED. */
1698 demangle_real_value (work
, mangled
, s
)
1699 struct work_stuff
*work
;
1700 const char **mangled
;
1703 if (**mangled
== 'E')
1704 return demangle_expression (work
, mangled
, s
, tk_real
);
1706 if (**mangled
== 'm')
1708 string_appendn (s
, "-", 1);
1711 while (isdigit ((unsigned char)**mangled
))
1713 string_appendn (s
, *mangled
, 1);
1716 if (**mangled
== '.') /* fraction */
1718 string_appendn (s
, ".", 1);
1720 while (isdigit ((unsigned char)**mangled
))
1722 string_appendn (s
, *mangled
, 1);
1726 if (**mangled
== 'e') /* exponent */
1728 string_appendn (s
, "e", 1);
1730 while (isdigit ((unsigned char)**mangled
))
1732 string_appendn (s
, *mangled
, 1);
1741 demangle_template_value_parm (work
, mangled
, s
, tk
)
1742 struct work_stuff
*work
;
1743 const char **mangled
;
1749 if (**mangled
== 'Y')
1751 /* The next argument is a template parameter. */
1755 idx
= consume_count_with_underscores (mangled
);
1757 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1758 || consume_count_with_underscores (mangled
) == -1)
1760 if (work
->tmpl_argvec
)
1761 string_append (s
, work
->tmpl_argvec
[idx
]);
1763 string_append_template_idx (s
, idx
);
1765 else if (tk
== tk_integral
)
1766 success
= demangle_integral_value (work
, mangled
, s
);
1767 else if (tk
== tk_char
)
1771 if (**mangled
== 'm')
1773 string_appendn (s
, "-", 1);
1776 string_appendn (s
, "'", 1);
1777 val
= consume_count(mangled
);
1784 string_appendn (s
, &tmp
[0], 1);
1785 string_appendn (s
, "'", 1);
1788 else if (tk
== tk_bool
)
1790 int val
= consume_count (mangled
);
1792 string_appendn (s
, "false", 5);
1794 string_appendn (s
, "true", 4);
1798 else if (tk
== tk_real
)
1799 success
= demangle_real_value (work
, mangled
, s
);
1800 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1802 if (**mangled
== 'Q')
1803 success
= demangle_qualified (work
, mangled
, s
,
1808 int symbol_len
= consume_count (mangled
);
1809 if (symbol_len
== -1)
1811 if (symbol_len
== 0)
1812 string_appendn (s
, "0", 1);
1815 char *p
= xmalloc (symbol_len
+ 1), *q
;
1816 strncpy (p
, *mangled
, symbol_len
);
1817 p
[symbol_len
] = '\0';
1818 /* We use cplus_demangle here, rather than
1819 internal_cplus_demangle, because the name of the entity
1820 mangled here does not make use of any of the squangling
1821 or type-code information we have built up thus far; it is
1822 mangled independently. */
1823 q
= cplus_demangle (p
, work
->options
);
1824 if (tk
== tk_pointer
)
1825 string_appendn (s
, "&", 1);
1826 /* FIXME: Pointer-to-member constants should get a
1827 qualifying class name here. */
1830 string_append (s
, q
);
1834 string_append (s
, p
);
1837 *mangled
+= symbol_len
;
1844 /* Demangle the template name in MANGLED. The full name of the
1845 template (e.g., S<int>) is placed in TNAME. The name without the
1846 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1847 non-NULL. If IS_TYPE is nonzero, this template is a type template,
1848 not a function template. If both IS_TYPE and REMEMBER are nonzero,
1849 the template is remembered in the list of back-referenceable
1853 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
1854 struct work_stuff
*work
;
1855 const char **mangled
;
1866 int is_java_array
= 0;
1874 bindex
= register_Btype (work
);
1876 /* get template name */
1877 if (**mangled
== 'z')
1883 idx
= consume_count_with_underscores (mangled
);
1885 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1886 || consume_count_with_underscores (mangled
) == -1)
1889 if (work
->tmpl_argvec
)
1891 string_append (tname
, work
->tmpl_argvec
[idx
]);
1893 string_append (trawname
, work
->tmpl_argvec
[idx
]);
1897 string_append_template_idx (tname
, idx
);
1899 string_append_template_idx (trawname
, idx
);
1904 if ((r
= consume_count (mangled
)) <= 0
1905 || (int) strlen (*mangled
) < r
)
1909 is_java_array
= (work
-> options
& DMGL_JAVA
)
1910 && strncmp (*mangled
, "JArray1Z", 8) == 0;
1911 if (! is_java_array
)
1913 string_appendn (tname
, *mangled
, r
);
1916 string_appendn (trawname
, *mangled
, r
);
1921 string_append (tname
, "<");
1922 /* get size of template parameter list */
1923 if (!get_count (mangled
, &r
))
1929 /* Create an array for saving the template argument values. */
1930 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
1931 work
->ntmpl_args
= r
;
1932 for (i
= 0; i
< r
; i
++)
1933 work
->tmpl_argvec
[i
] = 0;
1935 for (i
= 0; i
< r
; i
++)
1939 string_append (tname
, ", ");
1941 /* Z for type parameters */
1942 if (**mangled
== 'Z')
1945 /* temp is initialized in do_type */
1946 success
= do_type (work
, mangled
, &temp
);
1949 string_appends (tname
, &temp
);
1953 /* Save the template argument. */
1954 int len
= temp
.p
- temp
.b
;
1955 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
1956 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
1957 work
->tmpl_argvec
[i
][len
] = '\0';
1960 string_delete(&temp
);
1966 /* z for template parameters */
1967 else if (**mangled
== 'z')
1971 success
= demangle_template_template_parm (work
, mangled
, tname
);
1974 && (r2
= consume_count (mangled
)) > 0
1975 && (int) strlen (*mangled
) >= r2
)
1977 string_append (tname
, " ");
1978 string_appendn (tname
, *mangled
, r2
);
1981 /* Save the template argument. */
1983 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
1984 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
1985 work
->tmpl_argvec
[i
][len
] = '\0';
1999 /* otherwise, value parameter */
2001 /* temp is initialized in do_type */
2002 success
= do_type (work
, mangled
, &temp
);
2003 string_delete(&temp
);
2015 success
= demangle_template_value_parm (work
, mangled
, s
,
2016 (type_kind_t
) success
);
2028 int len
= s
->p
- s
->b
;
2029 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2030 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2031 work
->tmpl_argvec
[i
][len
] = '\0';
2033 string_appends (tname
, s
);
2041 string_append (tname
, "[]");
2045 if (tname
->p
[-1] == '>')
2046 string_append (tname
, " ");
2047 string_append (tname
, ">");
2050 if (is_type
&& remember
)
2051 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2054 if (work -> static_type)
2056 string_append (declp, *mangled + 1);
2057 *mangled += strlen (*mangled);
2062 success = demangle_args (work, mangled, declp);
2070 arm_pt (work
, mangled
, n
, anchor
, args
)
2071 struct work_stuff
*work
;
2072 const char *mangled
;
2074 const char **anchor
, **args
;
2076 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2077 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2078 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= mystrstr (mangled
, "__pt__")))
2081 *args
= *anchor
+ 6;
2082 len
= consume_count (args
);
2085 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2091 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2093 if ((*anchor
= mystrstr (mangled
, "__tm__"))
2094 || (*anchor
= mystrstr (mangled
, "__ps__"))
2095 || (*anchor
= mystrstr (mangled
, "__pt__")))
2098 *args
= *anchor
+ 6;
2099 len
= consume_count (args
);
2102 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2108 else if ((*anchor
= mystrstr (mangled
, "__S")))
2111 *args
= *anchor
+ 3;
2112 len
= consume_count (args
);
2115 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2127 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2128 struct work_stuff
*work
;
2129 const char **mangled
;
2135 const char *e
= *mangled
+ n
;
2138 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2140 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2142 char *start_spec_args
= NULL
;
2144 /* First check for and omit template specialization pseudo-arguments,
2145 such as in "Spec<#1,#1.*>" */
2146 start_spec_args
= strchr (*mangled
, '<');
2147 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2148 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2150 string_appendn (declp
, *mangled
, n
);
2151 (*mangled
) += n
+ 1;
2153 if (work
->temp_start
== -1) /* non-recursive call */
2154 work
->temp_start
= declp
->p
- declp
->b
;
2155 string_append (declp
, "<");
2158 string_clear (&arg
);
2162 /* 'T' signals a type parameter */
2164 if (!do_type (work
, mangled
, &arg
))
2165 goto hpacc_template_args_done
;
2170 /* 'U' or 'S' signals an integral value */
2171 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2172 goto hpacc_template_args_done
;
2176 /* 'A' signals a named constant expression (literal) */
2177 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2178 goto hpacc_template_args_done
;
2182 /* Today, 1997-09-03, we have only the above types
2183 of template parameters */
2184 /* FIXME: maybe this should fail and return null */
2185 goto hpacc_template_args_done
;
2187 string_appends (declp
, &arg
);
2188 /* Check if we're at the end of template args.
2189 0 if at end of static member of template class,
2190 _ if done with template args for a function */
2191 if ((**mangled
== '\000') || (**mangled
== '_'))
2194 string_append (declp
, ",");
2196 hpacc_template_args_done
:
2197 string_append (declp
, ">");
2198 string_delete (&arg
);
2199 if (**mangled
== '_')
2203 /* ARM template? (Also handles HP cfront extensions) */
2204 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2209 string_appendn (declp
, *mangled
, p
- *mangled
);
2210 if (work
->temp_start
== -1) /* non-recursive call */
2211 work
->temp_start
= declp
->p
- declp
->b
;
2212 string_append (declp
, "<");
2213 /* should do error checking here */
2215 string_clear (&arg
);
2217 /* Check for type or literal here */
2220 /* HP cfront extensions to ARM for template args */
2221 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2222 /* FIXME: We handle only numeric literals for HP cfront */
2224 /* A typed constant value follows */
2226 if (!do_type (work
, &args
, &type_str
))
2227 goto cfront_template_args_done
;
2228 string_append (&arg
, "(");
2229 string_appends (&arg
, &type_str
);
2230 string_append (&arg
, ")");
2232 goto cfront_template_args_done
;
2234 /* Now snarf a literal value following 'L' */
2235 if (!snarf_numeric_literal (&args
, &arg
))
2236 goto cfront_template_args_done
;
2240 /* Snarf a literal following 'L' */
2242 if (!snarf_numeric_literal (&args
, &arg
))
2243 goto cfront_template_args_done
;
2246 /* Not handling other HP cfront stuff */
2247 if (!do_type (work
, &args
, &arg
))
2248 goto cfront_template_args_done
;
2250 string_appends (declp
, &arg
);
2251 string_append (declp
, ",");
2253 cfront_template_args_done
:
2254 string_delete (&arg
);
2256 --declp
->p
; /* remove extra comma */
2257 string_append (declp
, ">");
2259 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2260 && (*mangled
)[9] == 'N'
2261 && (*mangled
)[8] == (*mangled
)[10]
2262 && strchr (cplus_markers
, (*mangled
)[8]))
2264 /* A member of the anonymous namespace. */
2265 string_append (declp
, "{anonymous}");
2269 if (work
->temp_start
== -1) /* non-recursive call only */
2270 work
->temp_start
= 0; /* disable in recursive calls */
2271 string_appendn (declp
, *mangled
, n
);
2276 /* Extract a class name, possibly a template with arguments, from the
2277 mangled string; qualifiers, local class indicators, etc. have
2278 already been dealt with */
2281 demangle_class_name (work
, mangled
, declp
)
2282 struct work_stuff
*work
;
2283 const char **mangled
;
2289 n
= consume_count (mangled
);
2292 if ((int) strlen (*mangled
) >= n
)
2294 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2305 demangle_class -- demangle a mangled class sequence
2310 demangle_class (struct work_stuff *work, const char **mangled,
2315 DECLP points to the buffer into which demangling is being done.
2317 *MANGLED points to the current token to be demangled. On input,
2318 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2319 On exit, it points to the next token after the mangled class on
2320 success, or the first unconsumed token on failure.
2322 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2323 we are demangling a constructor or destructor. In this case
2324 we prepend "class::class" or "class::~class" to DECLP.
2326 Otherwise, we prepend "class::" to the current DECLP.
2328 Reset the constructor/destructor flags once they have been
2329 "consumed". This allows demangle_class to be called later during
2330 the same demangling, to do normal class demangling.
2332 Returns 1 if demangling is successful, 0 otherwise.
2337 demangle_class (work
, mangled
, declp
)
2338 struct work_stuff
*work
;
2339 const char **mangled
;
2345 char *save_class_name_end
= 0;
2347 string_init (&class_name
);
2348 btype
= register_Btype (work
);
2349 if (demangle_class_name (work
, mangled
, &class_name
))
2351 save_class_name_end
= class_name
.p
;
2352 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2354 /* adjust so we don't include template args */
2355 if (work
->temp_start
&& (work
->temp_start
!= -1))
2357 class_name
.p
= class_name
.b
+ work
->temp_start
;
2359 string_prepends (declp
, &class_name
);
2360 if (work
-> destructor
& 1)
2362 string_prepend (declp
, "~");
2363 work
-> destructor
-= 1;
2367 work
-> constructor
-= 1;
2370 class_name
.p
= save_class_name_end
;
2371 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2372 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2373 string_prepend (declp
, SCOPE_STRING (work
));
2374 string_prepends (declp
, &class_name
);
2377 string_delete (&class_name
);
2382 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2383 the rightmost guess.
2385 Find the correct "__"-sequence where the function name ends and the
2386 signature starts, which is ambiguous with GNU mangling.
2387 Call demangle_signature here, so we can make sure we found the right
2388 one; *mangled will be consumed so caller will not make further calls to
2389 demangle_signature. */
2392 iterate_demangle_function (work
, mangled
, declp
, scan
)
2393 struct work_stuff
*work
;
2394 const char **mangled
;
2398 const char *mangle_init
= *mangled
;
2401 struct work_stuff work_init
;
2403 if (*(scan
+ 2) == '\0')
2406 /* Do not iterate for some demangling modes, or if there's only one
2407 "__"-sequence. This is the normal case. */
2408 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2409 || mystrstr (scan
+ 2, "__") == NULL
)
2411 demangle_function_name (work
, mangled
, declp
, scan
);
2415 /* Save state so we can restart if the guess at the correct "__" was
2417 string_init (&decl_init
);
2418 string_appends (&decl_init
, declp
);
2419 memset (&work_init
, 0, sizeof work_init
);
2420 work_stuff_copy_to_from (&work_init
, work
);
2422 /* Iterate over occurrences of __, allowing names and types to have a
2423 "__" sequence in them. We must start with the first (not the last)
2424 occurrence, since "__" most often occur between independent mangled
2425 parts, hence starting at the last occurence inside a signature
2426 might get us a "successful" demangling of the signature. */
2430 demangle_function_name (work
, mangled
, declp
, scan
);
2431 success
= demangle_signature (work
, mangled
, declp
);
2435 /* Reset demangle state for the next round. */
2436 *mangled
= mangle_init
;
2437 string_clear (declp
);
2438 string_appends (declp
, &decl_init
);
2439 work_stuff_copy_to_from (work
, &work_init
);
2441 /* Leave this underscore-sequence. */
2444 /* Scan for the next "__" sequence. */
2445 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2448 /* Move to last "__" in this sequence. */
2449 while (*scan
&& *scan
== '_')
2454 /* Delete saved state. */
2455 delete_work_stuff (&work_init
);
2456 string_delete (&decl_init
);
2465 demangle_prefix -- consume the mangled name prefix and find signature
2470 demangle_prefix (struct work_stuff *work, const char **mangled,
2475 Consume and demangle the prefix of the mangled name.
2476 While processing the function name root, arrange to call
2477 demangle_signature if the root is ambiguous.
2479 DECLP points to the string buffer into which demangled output is
2480 placed. On entry, the buffer is empty. On exit it contains
2481 the root function name, the demangled operator name, or in some
2482 special cases either nothing or the completely demangled result.
2484 MANGLED points to the current pointer into the mangled name. As each
2485 token of the mangled name is consumed, it is updated. Upon entry
2486 the current mangled name pointer points to the first character of
2487 the mangled name. Upon exit, it should point to the first character
2488 of the signature if demangling was successful, or to the first
2489 unconsumed character if demangling of the prefix was unsuccessful.
2491 Returns 1 on success, 0 otherwise.
2495 demangle_prefix (work
, mangled
, declp
)
2496 struct work_stuff
*work
;
2497 const char **mangled
;
2504 if (strlen(*mangled
) > 6
2505 && (strncmp(*mangled
, "_imp__", 6) == 0
2506 || strncmp(*mangled
, "__imp_", 6) == 0))
2508 /* it's a symbol imported from a PE dynamic library. Check for both
2509 new style prefix _imp__ and legacy __imp_ used by older versions
2512 work
->dllimported
= 1;
2514 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2516 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2517 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2519 if ((*mangled
)[9] == 'D')
2521 /* it's a GNU global destructor to be executed at program exit */
2523 work
->destructor
= 2;
2524 if (gnu_special (work
, mangled
, declp
))
2527 else if ((*mangled
)[9] == 'I')
2529 /* it's a GNU global constructor to be executed at program init */
2531 work
->constructor
= 2;
2532 if (gnu_special (work
, mangled
, declp
))
2537 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2539 /* it's a ARM global destructor to be executed at program exit */
2541 work
->destructor
= 2;
2543 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2545 /* it's a ARM global constructor to be executed at program initial */
2547 work
->constructor
= 2;
2550 /* This block of code is a reduction in strength time optimization
2552 scan = mystrstr (*mangled, "__"); */
2558 scan
= strchr (scan
, '_');
2559 } while (scan
!= NULL
&& *++scan
!= '_');
2561 if (scan
!= NULL
) --scan
;
2566 /* We found a sequence of two or more '_', ensure that we start at
2567 the last pair in the sequence. */
2568 i
= strspn (scan
, "_");
2579 else if (work
-> static_type
)
2581 if (!isdigit ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2586 else if ((scan
== *mangled
)
2587 && (isdigit ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2588 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2590 /* The ARM says nothing about the mangling of local variables.
2591 But cfront mangles local variables by prepending __<nesting_level>
2592 to them. As an extension to ARM demangling we handle this case. */
2593 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2594 && isdigit ((unsigned char)scan
[2]))
2596 *mangled
= scan
+ 2;
2597 consume_count (mangled
);
2598 string_append (declp
, *mangled
);
2599 *mangled
+= strlen (*mangled
);
2604 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2605 names like __Q2_3foo3bar for nested type names. So don't accept
2606 this style of constructor for cfront demangling. A GNU
2607 style member-template constructor starts with 'H'. */
2608 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2609 work
-> constructor
+= 1;
2610 *mangled
= scan
+ 2;
2613 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2615 /* Cfront-style parameterized type. Handled later as a signature. */
2619 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2621 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2622 || (scan
[2] == 'p' && scan
[3] == 's')
2623 || (scan
[2] == 'p' && scan
[3] == 't')))
2625 /* EDG-style parameterized type. Handled later as a signature. */
2629 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2631 else if ((scan
== *mangled
) && !isdigit ((unsigned char)scan
[2])
2632 && (scan
[2] != 't'))
2634 /* Mangled name starts with "__". Skip over any leading '_' characters,
2635 then find the next "__" that separates the prefix from the signature.
2637 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2638 || (arm_special (mangled
, declp
) == 0))
2640 while (*scan
== '_')
2644 if ((scan
= mystrstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2646 /* No separator (I.E. "__not_mangled"), or empty signature
2647 (I.E. "__not_mangled_either__") */
2651 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2654 else if (*(scan
+ 2) != '\0')
2656 /* Mangled name does not start with "__" but does have one somewhere
2657 in there with non empty stuff after it. Looks like a global
2658 function name. Iterate over all "__":s until the right
2660 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2664 /* Doesn't look like a mangled name */
2668 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2670 string_append (declp
, *mangled
);
2671 *mangled
+= strlen (*mangled
);
2681 gnu_special -- special handling of gnu mangled strings
2686 gnu_special (struct work_stuff *work, const char **mangled,
2692 Process some special GNU style mangling forms that don't fit
2693 the normal pattern. For example:
2695 _$_3foo (destructor for class foo)
2696 _vt$foo (foo virtual table)
2697 _vt$foo$bar (foo::bar virtual table)
2698 __vt_foo (foo virtual table, new style with thunks)
2699 _3foo$varname (static data member)
2700 _Q22rs2tu$vw (static data member)
2701 __t6vector1Zii (constructor with template)
2702 __thunk_4__$_7ostream (virtual function thunk)
2706 gnu_special (work
, mangled
, declp
)
2707 struct work_stuff
*work
;
2708 const char **mangled
;
2715 if ((*mangled
)[0] == '_'
2716 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2717 && (*mangled
)[2] == '_')
2719 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2721 work
-> destructor
+= 1;
2723 else if ((*mangled
)[0] == '_'
2724 && (((*mangled
)[1] == '_'
2725 && (*mangled
)[2] == 'v'
2726 && (*mangled
)[3] == 't'
2727 && (*mangled
)[4] == '_')
2728 || ((*mangled
)[1] == 'v'
2729 && (*mangled
)[2] == 't'
2730 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2732 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2733 and create the decl. Note that we consume the entire mangled
2734 input string, which means that demangle_signature has no work
2736 if ((*mangled
)[2] == 'v')
2737 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2739 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2740 while (**mangled
!= '\0')
2746 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2749 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2753 if (isdigit((unsigned char)*mangled
[0]))
2755 n
= consume_count(mangled
);
2756 /* We may be seeing a too-large size, or else a
2757 ".<digits>" indicating a static local symbol. In
2758 any case, declare victory and move on; *don't* try
2759 to use n to allocate. */
2760 if (n
> (int) strlen (*mangled
))
2768 n
= strcspn (*mangled
, cplus_markers
);
2770 string_appendn (declp
, *mangled
, n
);
2774 p
= strpbrk (*mangled
, cplus_markers
);
2775 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2779 string_append (declp
, SCOPE_STRING (work
));
2790 string_append (declp
, " virtual table");
2792 else if ((*mangled
)[0] == '_'
2793 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2794 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2796 /* static data member, "_3foo$varname" for example */
2802 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2805 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2808 n
= consume_count (mangled
);
2809 if (n
< 0 || n
> (long) strlen (*mangled
))
2814 string_appendn (declp
, *mangled
, n
);
2817 if (success
&& (p
== *mangled
))
2819 /* Consumed everything up to the cplus_marker, append the
2822 string_append (declp
, SCOPE_STRING (work
));
2823 n
= strlen (*mangled
);
2824 string_appendn (declp
, *mangled
, n
);
2832 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
2837 delta
= consume_count (mangled
);
2842 char *method
= internal_cplus_demangle (work
, ++*mangled
);
2847 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
2848 string_append (declp
, buf
);
2849 string_append (declp
, method
);
2851 n
= strlen (*mangled
);
2860 else if (strncmp (*mangled
, "__t", 3) == 0
2861 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
2863 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
2869 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2872 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2875 success
= demangle_fund_type (work
, mangled
, declp
);
2878 if (success
&& **mangled
!= '\0')
2881 string_append (declp
, p
);
2891 recursively_demangle(work
, mangled
, result
, namelength
)
2892 struct work_stuff
*work
;
2893 const char **mangled
;
2897 char * recurse
= (char *)NULL
;
2898 char * recurse_dem
= (char *)NULL
;
2900 recurse
= (char *) xmalloc (namelength
+ 1);
2901 memcpy (recurse
, *mangled
, namelength
);
2902 recurse
[namelength
] = '\000';
2904 recurse_dem
= cplus_demangle (recurse
, work
->options
);
2908 string_append (result
, recurse_dem
);
2913 string_appendn (result
, *mangled
, namelength
);
2916 *mangled
+= namelength
;
2923 arm_special -- special handling of ARM/lucid mangled strings
2928 arm_special (const char **mangled,
2934 Process some special ARM style mangling forms that don't fit
2935 the normal pattern. For example:
2937 __vtbl__3foo (foo virtual table)
2938 __vtbl__3foo__3bar (bar::foo virtual table)
2943 arm_special (mangled
, declp
)
2944 const char **mangled
;
2951 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
2953 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
2954 and create the decl. Note that we consume the entire mangled
2955 input string, which means that demangle_signature has no work
2957 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
2958 while (*scan
!= '\0') /* first check it can be demangled */
2960 n
= consume_count (&scan
);
2963 return (0); /* no good */
2966 if (scan
[0] == '_' && scan
[1] == '_')
2971 (*mangled
) += ARM_VTABLE_STRLEN
;
2972 while (**mangled
!= '\0')
2974 n
= consume_count (mangled
);
2976 || n
> (long) strlen (*mangled
))
2978 string_prependn (declp
, *mangled
, n
);
2980 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
2982 string_prepend (declp
, "::");
2986 string_append (declp
, " virtual table");
2999 demangle_qualified -- demangle 'Q' qualified name strings
3004 demangle_qualified (struct work_stuff *, const char *mangled,
3005 string *result, int isfuncname, int append);
3009 Demangle a qualified name, such as "Q25Outer5Inner" which is
3010 the mangled form of "Outer::Inner". The demangled output is
3011 prepended or appended to the result string according to the
3012 state of the append flag.
3014 If isfuncname is nonzero, then the qualified name we are building
3015 is going to be used as a member function name, so if it is a
3016 constructor or destructor function, append an appropriate
3017 constructor or destructor name. I.E. for the above example,
3018 the result for use as a constructor is "Outer::Inner::Inner"
3019 and the result for use as a destructor is "Outer::Inner::~Inner".
3023 Numeric conversion is ASCII dependent (FIXME).
3028 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3029 struct work_stuff
*work
;
3030 const char **mangled
;
3040 int bindex
= register_Btype (work
);
3042 /* We only make use of ISFUNCNAME if the entity is a constructor or
3044 isfuncname
= (isfuncname
3045 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3047 string_init (&temp
);
3048 string_init (&last_name
);
3050 if ((*mangled
)[0] == 'K')
3052 /* Squangling qualified name reuse */
3055 idx
= consume_count_with_underscores (mangled
);
3056 if (idx
== -1 || idx
>= work
-> numk
)
3059 string_append (&temp
, work
-> ktypevec
[idx
]);
3062 switch ((*mangled
)[1])
3065 /* GNU mangled name with more than 9 classes. The count is preceded
3066 by an underscore (to distinguish it from the <= 9 case) and followed
3067 by an underscore. */
3069 qualifiers
= consume_count_with_underscores (mangled
);
3070 if (qualifiers
== -1)
3083 /* The count is in a single digit. */
3084 num
[0] = (*mangled
)[1];
3086 qualifiers
= atoi (num
);
3088 /* If there is an underscore after the digit, skip it. This is
3089 said to be for ARM-qualified names, but the ARM makes no
3090 mention of such an underscore. Perhaps cfront uses one. */
3091 if ((*mangled
)[2] == '_')
3106 /* Pick off the names and collect them in the temp buffer in the order
3107 in which they are found, separated by '::'. */
3109 while (qualifiers
-- > 0)
3112 string_clear (&last_name
);
3114 if (*mangled
[0] == '_')
3117 if (*mangled
[0] == 't')
3119 /* Here we always append to TEMP since we will want to use
3120 the template name without the template parameters as a
3121 constructor or destructor name. The appropriate
3122 (parameter-less) value is returned by demangle_template
3123 in LAST_NAME. We do not remember the template type here,
3124 in order to match the G++ mangling algorithm. */
3125 success
= demangle_template(work
, mangled
, &temp
,
3130 else if (*mangled
[0] == 'K')
3134 idx
= consume_count_with_underscores (mangled
);
3135 if (idx
== -1 || idx
>= work
->numk
)
3138 string_append (&temp
, work
->ktypevec
[idx
]);
3141 if (!success
) break;
3148 /* Now recursively demangle the qualifier
3149 * This is necessary to deal with templates in
3150 * mangling styles like EDG */
3151 namelength
= consume_count (mangled
);
3152 if (namelength
== -1)
3157 recursively_demangle(work
, mangled
, &temp
, namelength
);
3161 success
= do_type (work
, mangled
, &last_name
);
3164 string_appends (&temp
, &last_name
);
3169 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3172 string_append (&temp
, SCOPE_STRING (work
));
3175 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3177 /* If we are using the result as a function name, we need to append
3178 the appropriate '::' separated constructor or destructor name.
3179 We do this here because this is the most convenient place, where
3180 we already have a pointer to the name and the length of the name. */
3184 string_append (&temp
, SCOPE_STRING (work
));
3185 if (work
-> destructor
& 1)
3186 string_append (&temp
, "~");
3187 string_appends (&temp
, &last_name
);
3190 /* Now either prepend the temp buffer to the result, or append it,
3191 depending upon the state of the append flag. */
3194 string_appends (result
, &temp
);
3197 if (!STRING_EMPTY (result
))
3198 string_append (&temp
, SCOPE_STRING (work
));
3199 string_prepends (result
, &temp
);
3202 string_delete (&last_name
);
3203 string_delete (&temp
);
3211 get_count -- convert an ascii count to integer, consuming tokens
3216 get_count (const char **type, int *count)
3220 Assume that *type points at a count in a mangled name; set
3221 *count to its value, and set *type to the next character after
3222 the count. There are some weird rules in effect here.
3224 If *type does not point at a string of digits, return zero.
3226 If *type points at a string of digits followed by an
3227 underscore, set *count to their value as an integer, advance
3228 *type to point *after the underscore, and return 1.
3230 If *type points at a string of digits not followed by an
3231 underscore, consume only the first digit. Set *count to its
3232 value as an integer, leave *type pointing after that digit,
3235 The excuse for this odd behavior: in the ARM and HP demangling
3236 styles, a type can be followed by a repeat count of the form
3239 `x' is a single digit specifying how many additional copies
3240 of the type to append to the argument list, and
3242 `y' is one or more digits, specifying the zero-based index of
3243 the first repeated argument in the list. Yes, as you're
3244 unmangling the name you can figure this out yourself, but
3247 So, for example, in `bar__3fooFPiN51', the first argument is a
3248 pointer to an integer (`Pi'), and then the next five arguments
3249 are the same (`N5'), and the first repeat is the function's
3250 second argument (`1').
3254 get_count (type
, count
)
3261 if (!isdigit ((unsigned char)**type
))
3265 *count
= **type
- '0';
3267 if (isdigit ((unsigned char)**type
))
3277 while (isdigit ((unsigned char)*p
));
3288 /* RESULT will be initialised here; it will be freed on failure. The
3289 value returned is really a type_kind_t. */
3292 do_type (work
, mangled
, result
)
3293 struct work_stuff
*work
;
3294 const char **mangled
;
3301 const char *remembered_type
;
3304 type_kind_t tk
= tk_none
;
3306 string_init (&btype
);
3307 string_init (&decl
);
3308 string_init (result
);
3312 while (success
&& !done
)
3318 /* A pointer type */
3322 if (! (work
-> options
& DMGL_JAVA
))
3323 string_prepend (&decl
, "*");
3328 /* A reference type */
3331 string_prepend (&decl
, "&");
3340 if (!STRING_EMPTY (&decl
)
3341 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3343 string_prepend (&decl
, "(");
3344 string_append (&decl
, ")");
3346 string_append (&decl
, "[");
3347 if (**mangled
!= '_')
3348 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3350 if (**mangled
== '_')
3352 string_append (&decl
, "]");
3356 /* A back reference to a previously seen type */
3359 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3365 remembered_type
= work
-> typevec
[n
];
3366 mangled
= &remembered_type
;
3373 if (!STRING_EMPTY (&decl
)
3374 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3376 string_prepend (&decl
, "(");
3377 string_append (&decl
, ")");
3379 /* After picking off the function args, we expect to either find the
3380 function return type (preceded by an '_') or the end of the
3382 if (!demangle_nested_args (work
, mangled
, &decl
)
3383 || (**mangled
!= '_' && **mangled
!= '\0'))
3388 if (success
&& (**mangled
== '_'))
3395 type_quals
= TYPE_UNQUALIFIED
;
3397 member
= **mangled
== 'M';
3400 string_append (&decl
, ")");
3402 /* We don't need to prepend `::' for a qualified name;
3403 demangle_qualified will do that for us. */
3404 if (**mangled
!= 'Q')
3405 string_prepend (&decl
, SCOPE_STRING (work
));
3407 if (isdigit ((unsigned char)**mangled
))
3409 n
= consume_count (mangled
);
3411 || (int) strlen (*mangled
) < n
)
3416 string_prependn (&decl
, *mangled
, n
);
3419 else if (**mangled
== 'X' || **mangled
== 'Y')
3422 do_type (work
, mangled
, &temp
);
3423 string_prepends (&decl
, &temp
);
3425 else if (**mangled
== 't')
3428 string_init (&temp
);
3429 success
= demangle_template (work
, mangled
, &temp
,
3433 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3434 string_clear (&temp
);
3439 else if (**mangled
== 'Q')
3441 success
= demangle_qualified (work
, mangled
, &decl
,
3453 string_prepend (&decl
, "(");
3461 type_quals
|= code_for_qualifier (**mangled
);
3469 if (*(*mangled
)++ != 'F')
3475 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3476 || **mangled
!= '_')
3482 if (! PRINT_ANSI_QUALIFIERS
)
3486 if (type_quals
!= TYPE_UNQUALIFIED
)
3488 APPEND_BLANK (&decl
);
3489 string_append (&decl
, qualifier_string (type_quals
));
3500 if (PRINT_ANSI_QUALIFIERS
)
3502 if (!STRING_EMPTY (&decl
))
3503 string_prepend (&decl
, " ");
3505 string_prepend (&decl
, demangle_qualifier (**mangled
));
3520 if (success
) switch (**mangled
)
3522 /* A qualified name, such as "Outer::Inner". */
3526 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3530 /* A back reference to a previously seen squangled type */
3533 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3536 string_append (result
, work
->btypevec
[n
]);
3541 /* A template parm. We substitute the corresponding argument. */
3546 idx
= consume_count_with_underscores (mangled
);
3549 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3550 || consume_count_with_underscores (mangled
) == -1)
3556 if (work
->tmpl_argvec
)
3557 string_append (result
, work
->tmpl_argvec
[idx
]);
3559 string_append_template_idx (result
, idx
);
3566 success
= demangle_fund_type (work
, mangled
, result
);
3568 tk
= (type_kind_t
) success
;
3574 if (!STRING_EMPTY (&decl
))
3576 string_append (result
, " ");
3577 string_appends (result
, &decl
);
3581 string_delete (result
);
3582 string_delete (&decl
);
3585 /* Assume an integral type, if we're not sure. */
3586 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3591 /* Given a pointer to a type string that represents a fundamental type
3592 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3593 string in which the demangled output is being built in RESULT, and
3594 the WORK structure, decode the types and add them to the result.
3599 "Sl" => "signed long"
3600 "CUs" => "const unsigned short"
3602 The value returned is really a type_kind_t. */
3605 demangle_fund_type (work
, mangled
, result
)
3606 struct work_stuff
*work
;
3607 const char **mangled
;
3613 unsigned int dec
= 0;
3615 type_kind_t tk
= tk_integral
;
3617 string_init (&btype
);
3619 /* First pick off any type qualifiers. There can be more than one. */
3628 if (PRINT_ANSI_QUALIFIERS
)
3630 if (!STRING_EMPTY (result
))
3631 string_prepend (result
, " ");
3632 string_prepend (result
, demangle_qualifier (**mangled
));
3638 APPEND_BLANK (result
);
3639 string_append (result
, "unsigned");
3641 case 'S': /* signed char only */
3643 APPEND_BLANK (result
);
3644 string_append (result
, "signed");
3648 APPEND_BLANK (result
);
3649 string_append (result
, "__complex");
3657 /* Now pick off the fundamental type. There can be only one. */
3666 APPEND_BLANK (result
);
3667 string_append (result
, "void");
3671 APPEND_BLANK (result
);
3672 string_append (result
, "long long");
3676 APPEND_BLANK (result
);
3677 string_append (result
, "long");
3681 APPEND_BLANK (result
);
3682 string_append (result
, "int");
3686 APPEND_BLANK (result
);
3687 string_append (result
, "short");
3691 APPEND_BLANK (result
);
3692 string_append (result
, "bool");
3697 APPEND_BLANK (result
);
3698 string_append (result
, "char");
3703 APPEND_BLANK (result
);
3704 string_append (result
, "wchar_t");
3709 APPEND_BLANK (result
);
3710 string_append (result
, "long double");
3715 APPEND_BLANK (result
);
3716 string_append (result
, "double");
3721 APPEND_BLANK (result
);
3722 string_append (result
, "float");
3727 if (!isdigit ((unsigned char)**mangled
))
3734 if (**mangled
== '_')
3739 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3742 if (**mangled
!= '_')
3752 strncpy (buf
, *mangled
, 2);
3754 *mangled
+= min (strlen (*mangled
), 2);
3756 sscanf (buf
, "%x", &dec
);
3757 sprintf (buf
, "int%u_t", dec
);
3758 APPEND_BLANK (result
);
3759 string_append (result
, buf
);
3763 /* An explicit type, such as "6mytype" or "7integer" */
3775 int bindex
= register_Btype (work
);
3777 string_init (&btype
);
3778 if (demangle_class_name (work
, mangled
, &btype
)) {
3779 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3780 APPEND_BLANK (result
);
3781 string_appends (result
, &btype
);
3785 string_delete (&btype
);
3790 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
3791 string_appends (result
, &btype
);
3799 return success
? ((int) tk
) : 0;
3803 /* Handle a template's value parameter for HP aCC (extension from ARM)
3804 **mangled points to 'S' or 'U' */
3807 do_hpacc_template_const_value (work
, mangled
, result
)
3808 struct work_stuff
*work ATTRIBUTE_UNUSED
;
3809 const char **mangled
;
3814 if (**mangled
!= 'U' && **mangled
!= 'S')
3817 unsigned_const
= (**mangled
== 'U');
3824 string_append (result
, "-");
3830 /* special case for -2^31 */
3831 string_append (result
, "-2147483648");
3838 /* We have to be looking at an integer now */
3839 if (!(isdigit ((unsigned char)**mangled
)))
3842 /* We only deal with integral values for template
3843 parameters -- so it's OK to look only for digits */
3844 while (isdigit ((unsigned char)**mangled
))
3846 char_str
[0] = **mangled
;
3847 string_append (result
, char_str
);
3852 string_append (result
, "U");
3854 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
3855 with L or LL suffixes. pai/1997-09-03 */
3857 return 1; /* success */
3860 /* Handle a template's literal parameter for HP aCC (extension from ARM)
3861 **mangled is pointing to the 'A' */
3864 do_hpacc_template_literal (work
, mangled
, result
)
3865 struct work_stuff
*work
;
3866 const char **mangled
;
3869 int literal_len
= 0;
3873 if (**mangled
!= 'A')
3878 literal_len
= consume_count (mangled
);
3880 if (literal_len
<= 0)
3883 /* Literal parameters are names of arrays, functions, etc. and the
3884 canonical representation uses the address operator */
3885 string_append (result
, "&");
3887 /* Now recursively demangle the literal name */
3888 recurse
= (char *) xmalloc (literal_len
+ 1);
3889 memcpy (recurse
, *mangled
, literal_len
);
3890 recurse
[literal_len
] = '\000';
3892 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3896 string_append (result
, recurse_dem
);
3901 string_appendn (result
, *mangled
, literal_len
);
3903 (*mangled
) += literal_len
;
3910 snarf_numeric_literal (args
, arg
)
3917 string_append (arg
, char_str
);
3920 else if (**args
== '+')
3923 if (!isdigit ((unsigned char)**args
))
3926 while (isdigit ((unsigned char)**args
))
3928 char_str
[0] = **args
;
3929 string_append (arg
, char_str
);
3936 /* Demangle the next argument, given by MANGLED into RESULT, which
3937 *should be an uninitialized* string. It will be initialized here,
3938 and free'd should anything go wrong. */
3941 do_arg (work
, mangled
, result
)
3942 struct work_stuff
*work
;
3943 const char **mangled
;
3946 /* Remember where we started so that we can record the type, for
3947 non-squangling type remembering. */
3948 const char *start
= *mangled
;
3950 string_init (result
);
3952 if (work
->nrepeats
> 0)
3956 if (work
->previous_argument
== 0)
3959 /* We want to reissue the previous type in this argument list. */
3960 string_appends (result
, work
->previous_argument
);
3964 if (**mangled
== 'n')
3966 /* A squangling-style repeat. */
3968 work
->nrepeats
= consume_count(mangled
);
3970 if (work
->nrepeats
<= 0)
3971 /* This was not a repeat count after all. */
3974 if (work
->nrepeats
> 9)
3976 if (**mangled
!= '_')
3977 /* The repeat count should be followed by an '_' in this
3984 /* Now, the repeat is all set up. */
3985 return do_arg (work
, mangled
, result
);
3988 /* Save the result in WORK->previous_argument so that we can find it
3989 if it's repeated. Note that saving START is not good enough: we
3990 do not want to add additional types to the back-referenceable
3991 type vector when processing a repeated type. */
3992 if (work
->previous_argument
)
3993 string_clear (work
->previous_argument
);
3996 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
3997 string_init (work
->previous_argument
);
4000 if (!do_type (work
, mangled
, work
->previous_argument
))
4003 string_appends (result
, work
->previous_argument
);
4005 remember_type (work
, start
, *mangled
- start
);
4010 remember_type (work
, start
, len
)
4011 struct work_stuff
*work
;
4017 if (work
->forgetting_types
)
4020 if (work
-> ntypes
>= work
-> typevec_size
)
4022 if (work
-> typevec_size
== 0)
4024 work
-> typevec_size
= 3;
4026 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4030 work
-> typevec_size
*= 2;
4032 = (char **) xrealloc ((char *)work
-> typevec
,
4033 sizeof (char *) * work
-> typevec_size
);
4036 tem
= xmalloc (len
+ 1);
4037 memcpy (tem
, start
, len
);
4039 work
-> typevec
[work
-> ntypes
++] = tem
;
4043 /* Remember a K type class qualifier. */
4045 remember_Ktype (work
, start
, len
)
4046 struct work_stuff
*work
;
4052 if (work
-> numk
>= work
-> ksize
)
4054 if (work
-> ksize
== 0)
4058 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4064 = (char **) xrealloc ((char *)work
-> ktypevec
,
4065 sizeof (char *) * work
-> ksize
);
4068 tem
= xmalloc (len
+ 1);
4069 memcpy (tem
, start
, len
);
4071 work
-> ktypevec
[work
-> numk
++] = tem
;
4074 /* Register a B code, and get an index for it. B codes are registered
4075 as they are seen, rather than as they are completed, so map<temp<char> >
4076 registers map<temp<char> > as B0, and temp<char> as B1 */
4079 register_Btype (work
)
4080 struct work_stuff
*work
;
4084 if (work
-> numb
>= work
-> bsize
)
4086 if (work
-> bsize
== 0)
4090 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4096 = (char **) xrealloc ((char *)work
-> btypevec
,
4097 sizeof (char *) * work
-> bsize
);
4100 ret
= work
-> numb
++;
4101 work
-> btypevec
[ret
] = NULL
;
4105 /* Store a value into a previously registered B code type. */
4108 remember_Btype (work
, start
, len
, index
)
4109 struct work_stuff
*work
;
4115 tem
= xmalloc (len
+ 1);
4116 memcpy (tem
, start
, len
);
4118 work
-> btypevec
[index
] = tem
;
4121 /* Lose all the info related to B and K type codes. */
4123 forget_B_and_K_types (work
)
4124 struct work_stuff
*work
;
4128 while (work
-> numk
> 0)
4130 i
= --(work
-> numk
);
4131 if (work
-> ktypevec
[i
] != NULL
)
4133 free (work
-> ktypevec
[i
]);
4134 work
-> ktypevec
[i
] = NULL
;
4138 while (work
-> numb
> 0)
4140 i
= --(work
-> numb
);
4141 if (work
-> btypevec
[i
] != NULL
)
4143 free (work
-> btypevec
[i
]);
4144 work
-> btypevec
[i
] = NULL
;
4148 /* Forget the remembered types, but not the type vector itself. */
4152 struct work_stuff
*work
;
4156 while (work
-> ntypes
> 0)
4158 i
= --(work
-> ntypes
);
4159 if (work
-> typevec
[i
] != NULL
)
4161 free (work
-> typevec
[i
]);
4162 work
-> typevec
[i
] = NULL
;
4167 /* Process the argument list part of the signature, after any class spec
4168 has been consumed, as well as the first 'F' character (if any). For
4171 "__als__3fooRT0" => process "RT0"
4172 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4174 DECLP must be already initialised, usually non-empty. It won't be freed
4177 Note that g++ differs significantly from ARM and lucid style mangling
4178 with regards to references to previously seen types. For example, given
4179 the source fragment:
4183 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4186 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4187 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4189 g++ produces the names:
4194 while lcc (and presumably other ARM style compilers as well) produces:
4196 foo__FiR3fooT1T2T1T2
4197 __ct__3fooFiR3fooT1T2T1T2
4199 Note that g++ bases its type numbers starting at zero and counts all
4200 previously seen types, while lucid/ARM bases its type numbers starting
4201 at one and only considers types after it has seen the 'F' character
4202 indicating the start of the function args. For lucid/ARM style, we
4203 account for this difference by discarding any previously seen types when
4204 we see the 'F' character, and subtracting one from the type number
4210 demangle_args (work
, mangled
, declp
)
4211 struct work_stuff
*work
;
4212 const char **mangled
;
4222 if (PRINT_ARG_TYPES
)
4224 string_append (declp
, "(");
4225 if (**mangled
== '\0')
4227 string_append (declp
, "void");
4231 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4232 || work
->nrepeats
> 0)
4234 if ((**mangled
== 'N') || (**mangled
== 'T'))
4236 temptype
= *(*mangled
)++;
4238 if (temptype
== 'N')
4240 if (!get_count (mangled
, &r
))
4249 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4251 /* If we have 10 or more types we might have more than a 1 digit
4252 index so we'll have to consume the whole count here. This
4253 will lose if the next thing is a type name preceded by a
4254 count but it's impossible to demangle that case properly
4255 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4256 Pc, ...)" or "(..., type12, char *, ...)" */
4257 if ((t
= consume_count(mangled
)) <= 0)
4264 if (!get_count (mangled
, &t
))
4269 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4273 /* Validate the type index. Protect against illegal indices from
4274 malformed type strings. */
4275 if ((t
< 0) || (t
>= work
-> ntypes
))
4279 while (work
->nrepeats
> 0 || --r
>= 0)
4281 tem
= work
-> typevec
[t
];
4282 if (need_comma
&& PRINT_ARG_TYPES
)
4284 string_append (declp
, ", ");
4286 if (!do_arg (work
, &tem
, &arg
))
4290 if (PRINT_ARG_TYPES
)
4292 string_appends (declp
, &arg
);
4294 string_delete (&arg
);
4300 if (need_comma
&& PRINT_ARG_TYPES
)
4301 string_append (declp
, ", ");
4302 if (!do_arg (work
, mangled
, &arg
))
4304 if (PRINT_ARG_TYPES
)
4305 string_appends (declp
, &arg
);
4306 string_delete (&arg
);
4311 if (**mangled
== 'e')
4314 if (PRINT_ARG_TYPES
)
4318 string_append (declp
, ",");
4320 string_append (declp
, "...");
4324 if (PRINT_ARG_TYPES
)
4326 string_append (declp
, ")");
4331 /* Like demangle_args, but for demangling the argument lists of function
4332 and method pointers or references, not top-level declarations. */
4335 demangle_nested_args (work
, mangled
, declp
)
4336 struct work_stuff
*work
;
4337 const char **mangled
;
4340 string
* saved_previous_argument
;
4344 /* The G++ name-mangling algorithm does not remember types on nested
4345 argument lists, unless -fsquangling is used, and in that case the
4346 type vector updated by remember_type is not used. So, we turn
4347 off remembering of types here. */
4348 ++work
->forgetting_types
;
4350 /* For the repeat codes used with -fsquangling, we must keep track of
4351 the last argument. */
4352 saved_previous_argument
= work
->previous_argument
;
4353 saved_nrepeats
= work
->nrepeats
;
4354 work
->previous_argument
= 0;
4357 /* Actually demangle the arguments. */
4358 result
= demangle_args (work
, mangled
, declp
);
4360 /* Restore the previous_argument field. */
4361 if (work
->previous_argument
)
4362 string_delete (work
->previous_argument
);
4363 work
->previous_argument
= saved_previous_argument
;
4364 --work
->forgetting_types
;
4365 work
->nrepeats
= saved_nrepeats
;
4371 demangle_function_name (work
, mangled
, declp
, scan
)
4372 struct work_stuff
*work
;
4373 const char **mangled
;
4381 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4382 string_need (declp
, 1);
4383 *(declp
-> p
) = '\0';
4385 /* Consume the function name, including the "__" separating the name
4386 from the signature. We are guaranteed that SCAN points to the
4389 (*mangled
) = scan
+ 2;
4390 /* We may be looking at an instantiation of a template function:
4391 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4392 following _F marks the start of the function arguments. Handle
4393 the template arguments first. */
4395 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4397 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4398 /* This leaves MANGLED pointing to the 'F' marking func args */
4401 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4404 /* See if we have an ARM style constructor or destructor operator.
4405 If so, then just record it, clear the decl, and return.
4406 We can't build the actual constructor/destructor decl until later,
4407 when we recover the class name from the signature. */
4409 if (strcmp (declp
-> b
, "__ct") == 0)
4411 work
-> constructor
+= 1;
4412 string_clear (declp
);
4415 else if (strcmp (declp
-> b
, "__dt") == 0)
4417 work
-> destructor
+= 1;
4418 string_clear (declp
);
4423 if (declp
->p
- declp
->b
>= 3
4424 && declp
->b
[0] == 'o'
4425 && declp
->b
[1] == 'p'
4426 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4428 /* see if it's an assignment expression */
4429 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4430 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4432 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4434 int len
= declp
->p
- declp
->b
- 10;
4435 if ((int) strlen (optable
[i
].in
) == len
4436 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4438 string_clear (declp
);
4439 string_append (declp
, "operator");
4440 string_append (declp
, optable
[i
].out
);
4441 string_append (declp
, "=");
4448 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4450 int len
= declp
->p
- declp
->b
- 3;
4451 if ((int) strlen (optable
[i
].in
) == len
4452 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4454 string_clear (declp
);
4455 string_append (declp
, "operator");
4456 string_append (declp
, optable
[i
].out
);
4462 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4463 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4465 /* type conversion operator */
4467 if (do_type (work
, &tem
, &type
))
4469 string_clear (declp
);
4470 string_append (declp
, "operator ");
4471 string_appends (declp
, &type
);
4472 string_delete (&type
);
4475 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4476 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4479 /* type conversion operator. */
4481 if (do_type (work
, &tem
, &type
))
4483 string_clear (declp
);
4484 string_append (declp
, "operator ");
4485 string_appends (declp
, &type
);
4486 string_delete (&type
);
4489 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4490 && islower((unsigned char)declp
->b
[2])
4491 && islower((unsigned char)declp
->b
[3]))
4493 if (declp
->b
[4] == '\0')
4496 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4498 if (strlen (optable
[i
].in
) == 2
4499 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4501 string_clear (declp
);
4502 string_append (declp
, "operator");
4503 string_append (declp
, optable
[i
].out
);
4510 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4513 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4515 if (strlen (optable
[i
].in
) == 3
4516 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4518 string_clear (declp
);
4519 string_append (declp
, "operator");
4520 string_append (declp
, optable
[i
].out
);
4529 /* a mini string-handling package */
4544 s
->p
= s
->b
= xmalloc (n
);
4547 else if (s
->e
- s
->p
< n
)
4552 s
->b
= xrealloc (s
->b
, n
);
4565 s
->b
= s
->e
= s
->p
= NULL
;
4573 s
->b
= s
->p
= s
->e
= NULL
;
4589 return (s
->b
== s
->p
);
4595 string_append (p
, s
)
4600 if (s
== NULL
|| *s
== '\0')
4604 memcpy (p
->p
, s
, n
);
4609 string_appends (p
, s
)
4618 memcpy (p
->p
, s
->b
, n
);
4624 string_appendn (p
, s
, n
)
4632 memcpy (p
->p
, s
, n
);
4638 string_prepend (p
, s
)
4642 if (s
!= NULL
&& *s
!= '\0')
4644 string_prependn (p
, s
, strlen (s
));
4649 string_prepends (p
, s
)
4654 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4659 string_prependn (p
, s
, n
)
4669 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4673 memcpy (p
->b
, s
, n
);
4679 string_append_template_idx (s
, idx
)
4683 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4684 sprintf(buf
, "T%d", idx
);
4685 string_append (s
, buf
);
4688 /* To generate a standalone demangler program for testing purposes,
4689 just compile and link this file with -DMAIN and libiberty.a. When
4690 run, it demangles each command line arg, or each stdin string, and
4691 prints the result on stdout. */
4697 static const char *program_name
;
4698 static const char *program_version
= VERSION
;
4699 static int flags
= DMGL_PARAMS
| DMGL_ANSI
;
4701 static void demangle_it
PARAMS ((char *));
4702 static void usage
PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN
;
4703 static void fatal
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
4704 static void print_demangler_list
PARAMS ((FILE *));
4707 demangle_it (mangled_name
)
4712 result
= cplus_demangle (mangled_name
, flags
);
4715 printf ("%s\n", mangled_name
);
4719 printf ("%s\n", result
);
4725 print_demangler_list (stream
)
4728 struct demangler_engine
*demangler
;
4730 fprintf (stream
, "{%s", libiberty_demanglers
->demangling_style_name
);
4732 for (demangler
= libiberty_demanglers
+ 1;
4733 demangler
->demangling_style
!= unknown_demangling
;
4735 fprintf (stream
, ",%s", demangler
->demangling_style_name
);
4737 fprintf (stream
, "}");
4741 usage (stream
, status
)
4746 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4751 print_demangler_list (stream
);
4752 fprintf (stream
, "]\n");
4756 print_demangler_list (stream
);
4757 fprintf (stream
, "]\n");
4760 [--help] [--version] [arg...]\n");
4764 #define MBUF_SIZE 32767
4765 char mbuffer
[MBUF_SIZE
];
4767 /* Defined in the automatically-generated underscore.c. */
4768 extern int prepends_underscore
;
4770 int strip_underscore
= 0;
4772 static struct option long_options
[] = {
4773 {"strip-underscores", no_argument
, 0, '_'},
4774 {"format", required_argument
, 0, 's'},
4775 {"help", no_argument
, 0, 'h'},
4776 {"java", no_argument
, 0, 'j'},
4777 {"no-strip-underscores", no_argument
, 0, 'n'},
4778 {"version", no_argument
, 0, 'v'},
4779 {0, no_argument
, 0, 0}
4782 /* More 'friendly' abort that prints the line and file.
4783 config.h can #define abort fancy_abort if you like that sort of thing. */
4788 fatal ("Internal gcc abort.");
4793 standard_symbol_characters
PARAMS ((void));
4796 hp_symbol_characters
PARAMS ((void));
4799 gnu_new_abi_symbol_characters
PARAMS ((void));
4801 /* Return the string of non-alnum characters that may occur
4802 as a valid symbol component, in the standard assembler symbol
4806 standard_symbol_characters ()
4812 /* Return the string of non-alnum characters that may occur
4813 as a valid symbol name component in an HP object file.
4815 Note that, since HP's compiler generates object code straight from
4816 C++ source, without going through an assembler, its mangled
4817 identifiers can use all sorts of characters that no assembler would
4818 tolerate, so the alphabet this function creates is a little odd.
4819 Here are some sample mangled identifiers offered by HP:
4821 typeid*__XT24AddressIndExpClassMember_
4822 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4823 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
4825 This still seems really weird to me, since nowhere else in this
4826 file is there anything to recognize curly brackets, parens, etc.
4827 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
4828 this is right, but I still strongly suspect that there's a
4829 misunderstanding here.
4831 If we decide it's better for c++filt to use HP's assembler syntax
4832 to scrape identifiers out of its input, here's the definition of
4833 the symbol name syntax from the HP assembler manual:
4835 Symbols are composed of uppercase and lowercase letters, decimal
4836 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
4837 underscore (_). A symbol can begin with a letter, digit underscore or
4838 dollar sign. If a symbol begins with a digit, it must contain a
4839 non-digit character.
4843 hp_symbol_characters ()
4845 return "_$.<>#,*&[]:(){}";
4849 /* Return the string of non-alnum characters that may occur
4850 as a valid symbol component in the GNU standard C++ ABI mangling
4854 gnu_new_abi_symbol_characters ()
4860 extern int main
PARAMS ((int, char **));
4869 const char *valid_symbols
;
4871 program_name
= argv
[0];
4873 strip_underscore
= prepends_underscore
;
4875 while ((c
= getopt_long (argc
, argv
, "_ns:j", long_options
, (int *) 0)) != EOF
)
4885 strip_underscore
= 0;
4888 printf ("GNU %s (C++ demangler), version %s\n", program_name
, program_version
);
4891 strip_underscore
= 1;
4898 enum demangling_styles style
;
4900 style
= cplus_demangle_name_to_style (optarg
);
4901 if (style
== unknown_demangling
)
4903 fprintf (stderr
, "%s: unknown demangling style `%s'\n",
4904 program_name
, optarg
);
4908 cplus_demangle_set_style (style
);
4916 for ( ; optind
< argc
; optind
++)
4918 demangle_it (argv
[optind
]);
4923 switch (current_demangling_style
)
4925 case gnu_demangling
:
4926 case lucid_demangling
:
4927 case arm_demangling
:
4928 case edg_demangling
:
4929 valid_symbols
= standard_symbol_characters ();
4932 valid_symbols
= hp_symbol_characters ();
4934 case gnu_new_abi_demangling
:
4935 valid_symbols
= gnu_new_abi_symbol_characters ();
4938 /* Folks should explicitly indicate the appropriate alphabet for
4939 each demangling. Providing a default would allow the
4940 question to go unconsidered. */
4948 /* Try to read a label. */
4949 while (c
!= EOF
&& (isalnum (c
) || strchr (valid_symbols
, c
)))
4951 if (i
>= MBUF_SIZE
-1)
4960 if (mbuffer
[0] == '.')
4962 if (strip_underscore
&& mbuffer
[skip_first
] == '_')
4970 result
= cplus_demangle (mbuffer
+ skip_first
, flags
);
4973 if (mbuffer
[0] == '.')
4975 fputs (result
, stdout
);
4979 fputs (mbuffer
, stdout
);
4997 fprintf (stderr
, "%s: %s\n", program_name
, str
);
5005 register PTR value
= (PTR
) malloc (size
);
5007 fatal ("virtual memory exhausted");
5012 xrealloc (ptr
, size
)
5016 register PTR value
= (PTR
) realloc (ptr
, size
);
5018 fatal ("virtual memory exhausted");