Commit | Line | Data |
---|---|---|
d00edca5 | 1 | /* Demangler for g++ V3 ABI. |
04aed652 | 2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
dddc49b7 | 3 | Free Software Foundation, Inc. |
d00edca5 | 4 | Written by Ian Lance Taylor <ian@wasabisystems.com>. |
eb383413 | 5 | |
9ad1aa29 | 6 | This file is part of the libiberty library, which is part of GCC. |
74bcd529 | 7 | |
9ad1aa29 | 8 | This file is free software; you can redistribute it and/or modify |
eb383413 L |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
35efcd67 DD |
13 | In addition to the permissions in the GNU General Public License, the |
14 | Free Software Foundation gives you unlimited permission to link the | |
15 | compiled version of this file into combinations with other programs, | |
16 | and to distribute those combinations without any restriction coming | |
17 | from the use of this file. (The General Public License restrictions | |
18 | do apply in other respects; for example, they cover modification of | |
19 | the file, and distribution when not linked into a combined | |
20 | executable.) | |
21 | ||
eb383413 L |
22 | This program is distributed in the hope that it will be useful, |
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | GNU General Public License for more details. | |
26 | ||
27 | You should have received a copy of the GNU General Public License | |
28 | along with this program; if not, write to the Free Software | |
979c05d3 | 29 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
eb383413 L |
30 | */ |
31 | ||
858b45cf DD |
32 | /* This code implements a demangler for the g++ V3 ABI. The ABI is |
33 | described on this web page: | |
34 | http://www.codesourcery.com/cxx-abi/abi.html#mangling | |
35 | ||
36 | This code was written while looking at the demangler written by | |
37 | Alex Samuel <samuel@codesourcery.com>. | |
38 | ||
39 | This code first pulls the mangled name apart into a list of | |
40 | components, and then walks the list generating the demangled | |
41 | name. | |
42 | ||
43 | This file will normally define the following functions, q.v.: | |
44 | char *cplus_demangle_v3(const char *mangled, int options) | |
45 | char *java_demangle_v3(const char *mangled) | |
208c1674 DD |
46 | int cplus_demangle_v3_callback(const char *mangled, int options, |
47 | demangle_callbackref callback) | |
48 | int java_demangle_v3_callback(const char *mangled, | |
49 | demangle_callbackref callback) | |
858b45cf DD |
50 | enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name) |
51 | enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name) | |
52 | ||
59727473 DD |
53 | Also, the interface to the component list is public, and defined in |
54 | demangle.h. The interface consists of these types, which are | |
55 | defined in demangle.h: | |
56 | enum demangle_component_type | |
57 | struct demangle_component | |
208c1674 | 58 | demangle_callbackref |
59727473 DD |
59 | and these functions defined in this file: |
60 | cplus_demangle_fill_name | |
61 | cplus_demangle_fill_extended_operator | |
62 | cplus_demangle_fill_ctor | |
63 | cplus_demangle_fill_dtor | |
64 | cplus_demangle_print | |
208c1674 | 65 | cplus_demangle_print_callback |
59727473 DD |
66 | and other functions defined in the file cp-demint.c. |
67 | ||
68 | This file also defines some other functions and variables which are | |
69 | only to be used by the file cp-demint.c. | |
70 | ||
858b45cf DD |
71 | Preprocessor macros you can define while compiling this file: |
72 | ||
73 | IN_LIBGCC2 | |
208c1674 | 74 | If defined, this file defines the following functions, q.v.: |
858b45cf DD |
75 | char *__cxa_demangle (const char *mangled, char *buf, size_t *len, |
76 | int *status) | |
208c1674 DD |
77 | int __gcclibcxx_demangle_callback (const char *, |
78 | void (*) | |
79 | (const char *, size_t, void *), | |
80 | void *) | |
81 | instead of cplus_demangle_v3[_callback]() and | |
82 | java_demangle_v3[_callback](). | |
858b45cf DD |
83 | |
84 | IN_GLIBCPP_V3 | |
208c1674 DD |
85 | If defined, this file defines only __cxa_demangle() and |
86 | __gcclibcxx_demangle_callback(), and no other publically visible | |
87 | functions or variables. | |
858b45cf DD |
88 | |
89 | STANDALONE_DEMANGLER | |
90 | If defined, this file defines a main() function which demangles | |
91 | any arguments, or, if none, demangles stdin. | |
92 | ||
93 | CP_DEMANGLE_DEBUG | |
94 | If defined, turns on debugging mode, which prints information on | |
95 | stdout about the mangled string. This is not generally useful. | |
96 | */ | |
97 | ||
208c1674 DD |
98 | #if defined (_AIX) && !defined (__GNUC__) |
99 | #pragma alloca | |
100 | #endif | |
101 | ||
eb383413 L |
102 | #ifdef HAVE_CONFIG_H |
103 | #include "config.h" | |
104 | #endif | |
105 | ||
d00edca5 | 106 | #include <stdio.h> |
b1233257 | 107 | |
eb383413 L |
108 | #ifdef HAVE_STDLIB_H |
109 | #include <stdlib.h> | |
110 | #endif | |
eb383413 L |
111 | #ifdef HAVE_STRING_H |
112 | #include <string.h> | |
113 | #endif | |
114 | ||
208c1674 DD |
115 | #ifdef HAVE_ALLOCA_H |
116 | # include <alloca.h> | |
117 | #else | |
118 | # ifndef alloca | |
119 | # ifdef __GNUC__ | |
120 | # define alloca __builtin_alloca | |
121 | # else | |
122 | extern char *alloca (); | |
123 | # endif /* __GNUC__ */ | |
124 | # endif /* alloca */ | |
125 | #endif /* HAVE_ALLOCA_H */ | |
126 | ||
eb383413 L |
127 | #include "ansidecl.h" |
128 | #include "libiberty.h" | |
eb383413 | 129 | #include "demangle.h" |
59727473 DD |
130 | #include "cp-demangle.h" |
131 | ||
132 | /* If IN_GLIBCPP_V3 is defined, some functions are made static. We | |
133 | also rename them via #define to avoid compiler errors when the | |
134 | static definition conflicts with the extern declaration in a header | |
135 | file. */ | |
136 | #ifdef IN_GLIBCPP_V3 | |
137 | ||
138 | #define CP_STATIC_IF_GLIBCPP_V3 static | |
139 | ||
140 | #define cplus_demangle_fill_name d_fill_name | |
9334f9c6 | 141 | static int d_fill_name (struct demangle_component *, const char *, int); |
59727473 DD |
142 | |
143 | #define cplus_demangle_fill_extended_operator d_fill_extended_operator | |
144 | static int | |
9334f9c6 DD |
145 | d_fill_extended_operator (struct demangle_component *, int, |
146 | struct demangle_component *); | |
59727473 DD |
147 | |
148 | #define cplus_demangle_fill_ctor d_fill_ctor | |
149 | static int | |
9334f9c6 DD |
150 | d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds, |
151 | struct demangle_component *); | |
59727473 DD |
152 | |
153 | #define cplus_demangle_fill_dtor d_fill_dtor | |
154 | static int | |
9334f9c6 DD |
155 | d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds, |
156 | struct demangle_component *); | |
59727473 DD |
157 | |
158 | #define cplus_demangle_mangled_name d_mangled_name | |
9334f9c6 | 159 | static struct demangle_component *d_mangled_name (struct d_info *, int); |
59727473 DD |
160 | |
161 | #define cplus_demangle_type d_type | |
9334f9c6 | 162 | static struct demangle_component *d_type (struct d_info *); |
59727473 DD |
163 | |
164 | #define cplus_demangle_print d_print | |
9334f9c6 | 165 | static char *d_print (int, const struct demangle_component *, int, size_t *); |
59727473 | 166 | |
208c1674 DD |
167 | #define cplus_demangle_print_callback d_print_callback |
168 | static int d_print_callback (int, const struct demangle_component *, | |
169 | demangle_callbackref, void *); | |
170 | ||
59727473 | 171 | #define cplus_demangle_init_info d_init_info |
9334f9c6 | 172 | static void d_init_info (const char *, int, size_t, struct d_info *); |
59727473 DD |
173 | |
174 | #else /* ! defined(IN_GLIBCPP_V3) */ | |
175 | #define CP_STATIC_IF_GLIBCPP_V3 | |
176 | #endif /* ! defined(IN_GLIBCPP_V3) */ | |
eb383413 | 177 | |
b6fb00c0 DD |
178 | /* See if the compiler supports dynamic arrays. */ |
179 | ||
180 | #ifdef __GNUC__ | |
181 | #define CP_DYNAMIC_ARRAYS | |
182 | #else | |
183 | #ifdef __STDC__ | |
184 | #ifdef __STDC_VERSION__ | |
185 | #if __STDC_VERSION__ >= 199901L | |
186 | #define CP_DYNAMIC_ARRAYS | |
187 | #endif /* __STDC__VERSION >= 199901L */ | |
188 | #endif /* defined (__STDC_VERSION__) */ | |
189 | #endif /* defined (__STDC__) */ | |
190 | #endif /* ! defined (__GNUC__) */ | |
191 | ||
858b45cf DD |
192 | /* We avoid pulling in the ctype tables, to prevent pulling in |
193 | additional unresolved symbols when this code is used in a library. | |
194 | FIXME: Is this really a valid reason? This comes from the original | |
195 | V3 demangler code. | |
d00edca5 | 196 | |
858b45cf | 197 | As of this writing this file has the following undefined references |
208c1674 DD |
198 | when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy, |
199 | strcat, strlen. */ | |
d00edca5 | 200 | |
d00edca5 | 201 | #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') |
858b45cf DD |
202 | #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z') |
203 | #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z') | |
03d5f569 | 204 | |
74bcd529 DD |
205 | /* The prefix prepended by GCC to an identifier represnting the |
206 | anonymous namespace. */ | |
207 | #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_" | |
d00edca5 DD |
208 | #define ANONYMOUS_NAMESPACE_PREFIX_LEN \ |
209 | (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1) | |
74bcd529 | 210 | |
97ceaf5b DD |
211 | /* Information we keep for the standard substitutions. */ |
212 | ||
213 | struct d_standard_sub_info | |
214 | { | |
215 | /* The code for this substitution. */ | |
216 | char code; | |
217 | /* The simple string it expands to. */ | |
218 | const char *simple_expansion; | |
b6fb00c0 DD |
219 | /* The length of the simple expansion. */ |
220 | int simple_len; | |
97ceaf5b DD |
221 | /* The results of a full, verbose, expansion. This is used when |
222 | qualifying a constructor/destructor, or when in verbose mode. */ | |
223 | const char *full_expansion; | |
b6fb00c0 DD |
224 | /* The length of the full expansion. */ |
225 | int full_len; | |
97ceaf5b DD |
226 | /* What to set the last_name field of d_info to; NULL if we should |
227 | not set it. This is only relevant when qualifying a | |
228 | constructor/destructor. */ | |
229 | const char *set_last_name; | |
b6fb00c0 DD |
230 | /* The length of set_last_name. */ |
231 | int set_last_name_len; | |
97ceaf5b DD |
232 | }; |
233 | ||
59727473 | 234 | /* Accessors for subtrees of struct demangle_component. */ |
eb383413 | 235 | |
d00edca5 DD |
236 | #define d_left(dc) ((dc)->u.s_binary.left) |
237 | #define d_right(dc) ((dc)->u.s_binary.right) | |
238 | ||
d00edca5 | 239 | /* A list of templates. This is used while printing. */ |
eb383413 | 240 | |
d00edca5 DD |
241 | struct d_print_template |
242 | { | |
243 | /* Next template on the list. */ | |
244 | struct d_print_template *next; | |
245 | /* This template. */ | |
abf6a75b | 246 | const struct demangle_component *template_decl; |
d00edca5 | 247 | }; |
eb383413 | 248 | |
d00edca5 | 249 | /* A list of type modifiers. This is used while printing. */ |
eb383413 | 250 | |
d00edca5 DD |
251 | struct d_print_mod |
252 | { | |
253 | /* Next modifier on the list. These are in the reverse of the order | |
254 | in which they appeared in the mangled string. */ | |
255 | struct d_print_mod *next; | |
256 | /* The modifier. */ | |
59727473 | 257 | const struct demangle_component *mod; |
d00edca5 DD |
258 | /* Whether this modifier was printed. */ |
259 | int printed; | |
331c3da2 DD |
260 | /* The list of templates which applies to this modifier. */ |
261 | struct d_print_template *templates; | |
d00edca5 | 262 | }; |
eb383413 | 263 | |
208c1674 | 264 | /* We use these structures to hold information during printing. */ |
d00edca5 | 265 | |
208c1674 | 266 | struct d_growable_string |
d00edca5 | 267 | { |
d00edca5 DD |
268 | /* Buffer holding the result. */ |
269 | char *buf; | |
270 | /* Current length of data in buffer. */ | |
271 | size_t len; | |
272 | /* Allocated size of buffer. */ | |
273 | size_t alc; | |
208c1674 DD |
274 | /* Set to 1 if we had a memory allocation failure. */ |
275 | int allocation_failure; | |
276 | }; | |
277 | ||
278 | enum { D_PRINT_BUFFER_LENGTH = 256 }; | |
279 | struct d_print_info | |
280 | { | |
208c1674 DD |
281 | /* Fixed-length allocated buffer for demangled data, flushed to the |
282 | callback with a NUL termination once full. */ | |
283 | char buf[D_PRINT_BUFFER_LENGTH]; | |
284 | /* Current length of data in buffer. */ | |
285 | size_t len; | |
286 | /* The last character printed, saved individually so that it survives | |
287 | any buffer flush. */ | |
288 | char last_char; | |
289 | /* Callback function to handle demangled buffer flush. */ | |
290 | demangle_callbackref callback; | |
291 | /* Opaque callback argument. */ | |
292 | void *opaque; | |
d00edca5 DD |
293 | /* The current list of templates, if any. */ |
294 | struct d_print_template *templates; | |
295 | /* The current list of modifiers (e.g., pointer, reference, etc.), | |
296 | if any. */ | |
297 | struct d_print_mod *modifiers; | |
208c1674 DD |
298 | /* Set to 1 if we saw a demangling error. */ |
299 | int demangle_failure; | |
1c08f2c8 DD |
300 | /* The current index into any template argument packs we are using |
301 | for printing. */ | |
302 | int pack_index; | |
3baae9d6 JJ |
303 | /* Number of d_print_flush calls so far. */ |
304 | unsigned long int flush_count; | |
d00edca5 | 305 | }; |
e61231f1 | 306 | |
eb383413 | 307 | #ifdef CP_DEMANGLE_DEBUG |
9334f9c6 | 308 | static void d_dump (struct demangle_component *, int); |
eb383413 | 309 | #endif |
59727473 DD |
310 | |
311 | static struct demangle_component * | |
9334f9c6 | 312 | d_make_empty (struct d_info *); |
59727473 DD |
313 | |
314 | static struct demangle_component * | |
9334f9c6 DD |
315 | d_make_comp (struct d_info *, enum demangle_component_type, |
316 | struct demangle_component *, | |
317 | struct demangle_component *); | |
59727473 DD |
318 | |
319 | static struct demangle_component * | |
9334f9c6 | 320 | d_make_name (struct d_info *, const char *, int); |
59727473 | 321 | |
a0692e36 L |
322 | static struct demangle_component * |
323 | d_make_demangle_mangled_name (struct d_info *, const char *); | |
324 | ||
59727473 | 325 | static struct demangle_component * |
9334f9c6 DD |
326 | d_make_builtin_type (struct d_info *, |
327 | const struct demangle_builtin_type_info *); | |
59727473 DD |
328 | |
329 | static struct demangle_component * | |
9334f9c6 DD |
330 | d_make_operator (struct d_info *, |
331 | const struct demangle_operator_info *); | |
59727473 DD |
332 | |
333 | static struct demangle_component * | |
9334f9c6 DD |
334 | d_make_extended_operator (struct d_info *, int, |
335 | struct demangle_component *); | |
59727473 DD |
336 | |
337 | static struct demangle_component * | |
9334f9c6 DD |
338 | d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds, |
339 | struct demangle_component *); | |
59727473 DD |
340 | |
341 | static struct demangle_component * | |
9334f9c6 DD |
342 | d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds, |
343 | struct demangle_component *); | |
59727473 DD |
344 | |
345 | static struct demangle_component * | |
9334f9c6 | 346 | d_make_template_param (struct d_info *, long); |
59727473 DD |
347 | |
348 | static struct demangle_component * | |
9334f9c6 | 349 | d_make_sub (struct d_info *, const char *, int); |
59727473 DD |
350 | |
351 | static int | |
9334f9c6 | 352 | has_return_type (struct demangle_component *); |
59727473 DD |
353 | |
354 | static int | |
9334f9c6 | 355 | is_ctor_dtor_or_conversion (struct demangle_component *); |
59727473 | 356 | |
9334f9c6 | 357 | static struct demangle_component *d_encoding (struct d_info *, int); |
59727473 | 358 | |
9334f9c6 | 359 | static struct demangle_component *d_name (struct d_info *); |
59727473 | 360 | |
9334f9c6 | 361 | static struct demangle_component *d_nested_name (struct d_info *); |
59727473 | 362 | |
9334f9c6 | 363 | static struct demangle_component *d_prefix (struct d_info *); |
59727473 | 364 | |
9334f9c6 | 365 | static struct demangle_component *d_unqualified_name (struct d_info *); |
59727473 | 366 | |
9334f9c6 | 367 | static struct demangle_component *d_source_name (struct d_info *); |
59727473 | 368 | |
9334f9c6 | 369 | static long d_number (struct d_info *); |
59727473 | 370 | |
9334f9c6 | 371 | static struct demangle_component *d_identifier (struct d_info *, int); |
59727473 | 372 | |
9334f9c6 | 373 | static struct demangle_component *d_operator_name (struct d_info *); |
59727473 | 374 | |
9334f9c6 | 375 | static struct demangle_component *d_special_name (struct d_info *); |
59727473 | 376 | |
9334f9c6 | 377 | static int d_call_offset (struct d_info *, int); |
59727473 | 378 | |
9334f9c6 | 379 | static struct demangle_component *d_ctor_dtor_name (struct d_info *); |
59727473 DD |
380 | |
381 | static struct demangle_component ** | |
9334f9c6 | 382 | d_cv_qualifiers (struct d_info *, struct demangle_component **, int); |
59727473 DD |
383 | |
384 | static struct demangle_component * | |
9334f9c6 | 385 | d_function_type (struct d_info *); |
59727473 DD |
386 | |
387 | static struct demangle_component * | |
9334f9c6 | 388 | d_bare_function_type (struct d_info *, int); |
59727473 DD |
389 | |
390 | static struct demangle_component * | |
9334f9c6 | 391 | d_class_enum_type (struct d_info *); |
59727473 | 392 | |
9334f9c6 | 393 | static struct demangle_component *d_array_type (struct d_info *); |
59727473 | 394 | |
cbc43128 DD |
395 | static struct demangle_component *d_vector_type (struct d_info *); |
396 | ||
59727473 | 397 | static struct demangle_component * |
9334f9c6 | 398 | d_pointer_to_member_type (struct d_info *); |
59727473 DD |
399 | |
400 | static struct demangle_component * | |
9334f9c6 | 401 | d_template_param (struct d_info *); |
59727473 | 402 | |
9334f9c6 | 403 | static struct demangle_component *d_template_args (struct d_info *); |
59727473 DD |
404 | |
405 | static struct demangle_component * | |
9334f9c6 | 406 | d_template_arg (struct d_info *); |
59727473 | 407 | |
9334f9c6 | 408 | static struct demangle_component *d_expression (struct d_info *); |
59727473 | 409 | |
9334f9c6 | 410 | static struct demangle_component *d_expr_primary (struct d_info *); |
59727473 | 411 | |
9334f9c6 | 412 | static struct demangle_component *d_local_name (struct d_info *); |
59727473 | 413 | |
9334f9c6 | 414 | static int d_discriminator (struct d_info *); |
59727473 | 415 | |
664aa91f DD |
416 | static struct demangle_component *d_lambda (struct d_info *); |
417 | ||
418 | static struct demangle_component *d_unnamed_type (struct d_info *); | |
419 | ||
7955ede5 DD |
420 | static struct demangle_component * |
421 | d_clone_suffix (struct d_info *, struct demangle_component *); | |
422 | ||
59727473 | 423 | static int |
9334f9c6 | 424 | d_add_substitution (struct d_info *, struct demangle_component *); |
59727473 | 425 | |
9334f9c6 | 426 | static struct demangle_component *d_substitution (struct d_info *, int); |
59727473 | 427 | |
208c1674 | 428 | static void d_growable_string_init (struct d_growable_string *, size_t); |
59727473 | 429 | |
208c1674 DD |
430 | static inline void |
431 | d_growable_string_resize (struct d_growable_string *, size_t); | |
59727473 | 432 | |
208c1674 DD |
433 | static inline void |
434 | d_growable_string_append_buffer (struct d_growable_string *, | |
435 | const char *, size_t); | |
59727473 | 436 | static void |
208c1674 DD |
437 | d_growable_string_callback_adapter (const char *, size_t, void *); |
438 | ||
439 | static void | |
ddee5e46 | 440 | d_print_init (struct d_print_info *, demangle_callbackref, void *); |
208c1674 DD |
441 | |
442 | static inline void d_print_error (struct d_print_info *); | |
443 | ||
444 | static inline int d_print_saw_error (struct d_print_info *); | |
445 | ||
446 | static inline void d_print_flush (struct d_print_info *); | |
447 | ||
448 | static inline void d_append_char (struct d_print_info *, char); | |
59727473 | 449 | |
208c1674 DD |
450 | static inline void d_append_buffer (struct d_print_info *, |
451 | const char *, size_t); | |
452 | ||
453 | static inline void d_append_string (struct d_print_info *, const char *); | |
454 | ||
455 | static inline char d_last_char (struct d_print_info *); | |
59727473 DD |
456 | |
457 | static void | |
ddee5e46 | 458 | d_print_comp (struct d_print_info *, int, const struct demangle_component *); |
59727473 DD |
459 | |
460 | static void | |
9334f9c6 | 461 | d_print_java_identifier (struct d_print_info *, const char *, int); |
59727473 DD |
462 | |
463 | static void | |
ddee5e46 | 464 | d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int); |
59727473 DD |
465 | |
466 | static void | |
ddee5e46 | 467 | d_print_mod (struct d_print_info *, int, const struct demangle_component *); |
59727473 DD |
468 | |
469 | static void | |
ddee5e46 | 470 | d_print_function_type (struct d_print_info *, int, |
9334f9c6 DD |
471 | const struct demangle_component *, |
472 | struct d_print_mod *); | |
59727473 DD |
473 | |
474 | static void | |
ddee5e46 | 475 | d_print_array_type (struct d_print_info *, int, |
9334f9c6 DD |
476 | const struct demangle_component *, |
477 | struct d_print_mod *); | |
59727473 DD |
478 | |
479 | static void | |
ddee5e46 | 480 | d_print_expr_op (struct d_print_info *, int, const struct demangle_component *); |
59727473 DD |
481 | |
482 | static void | |
ddee5e46 | 483 | d_print_cast (struct d_print_info *, int, const struct demangle_component *); |
59727473 | 484 | |
208c1674 DD |
485 | static int d_demangle_callback (const char *, int, |
486 | demangle_callbackref, void *); | |
9334f9c6 | 487 | static char *d_demangle (const char *, int, size_t *); |
d00edca5 | 488 | |
eb383413 | 489 | #ifdef CP_DEMANGLE_DEBUG |
d00edca5 DD |
490 | |
491 | static void | |
9334f9c6 | 492 | d_dump (struct demangle_component *dc, int indent) |
eb383413 L |
493 | { |
494 | int i; | |
eb383413 | 495 | |
d00edca5 | 496 | if (dc == NULL) |
208c1674 DD |
497 | { |
498 | if (indent == 0) | |
499 | printf ("failed demangling\n"); | |
500 | return; | |
501 | } | |
d00edca5 DD |
502 | |
503 | for (i = 0; i < indent; ++i) | |
504 | putchar (' '); | |
505 | ||
506 | switch (dc->type) | |
507 | { | |
59727473 | 508 | case DEMANGLE_COMPONENT_NAME: |
d00edca5 DD |
509 | printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s); |
510 | return; | |
59727473 | 511 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: |
d00edca5 DD |
512 | printf ("template parameter %ld\n", dc->u.s_number.number); |
513 | return; | |
59727473 | 514 | case DEMANGLE_COMPONENT_CTOR: |
d00edca5 DD |
515 | printf ("constructor %d\n", (int) dc->u.s_ctor.kind); |
516 | d_dump (dc->u.s_ctor.name, indent + 2); | |
517 | return; | |
59727473 | 518 | case DEMANGLE_COMPONENT_DTOR: |
d00edca5 DD |
519 | printf ("destructor %d\n", (int) dc->u.s_dtor.kind); |
520 | d_dump (dc->u.s_dtor.name, indent + 2); | |
521 | return; | |
59727473 | 522 | case DEMANGLE_COMPONENT_SUB_STD: |
d00edca5 DD |
523 | printf ("standard substitution %s\n", dc->u.s_string.string); |
524 | return; | |
59727473 | 525 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: |
d00edca5 DD |
526 | printf ("builtin type %s\n", dc->u.s_builtin.type->name); |
527 | return; | |
59727473 | 528 | case DEMANGLE_COMPONENT_OPERATOR: |
d00edca5 DD |
529 | printf ("operator %s\n", dc->u.s_operator.op->name); |
530 | return; | |
59727473 | 531 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
d00edca5 DD |
532 | printf ("extended operator with %d args\n", |
533 | dc->u.s_extended_operator.args); | |
534 | d_dump (dc->u.s_extended_operator.name, indent + 2); | |
535 | return; | |
536 | ||
59727473 | 537 | case DEMANGLE_COMPONENT_QUAL_NAME: |
d00edca5 DD |
538 | printf ("qualified name\n"); |
539 | break; | |
59727473 | 540 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
d4edd112 DD |
541 | printf ("local name\n"); |
542 | break; | |
59727473 | 543 | case DEMANGLE_COMPONENT_TYPED_NAME: |
d00edca5 DD |
544 | printf ("typed name\n"); |
545 | break; | |
59727473 | 546 | case DEMANGLE_COMPONENT_TEMPLATE: |
d00edca5 DD |
547 | printf ("template\n"); |
548 | break; | |
59727473 | 549 | case DEMANGLE_COMPONENT_VTABLE: |
d00edca5 DD |
550 | printf ("vtable\n"); |
551 | break; | |
59727473 | 552 | case DEMANGLE_COMPONENT_VTT: |
d00edca5 DD |
553 | printf ("VTT\n"); |
554 | break; | |
59727473 | 555 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
d00edca5 DD |
556 | printf ("construction vtable\n"); |
557 | break; | |
59727473 | 558 | case DEMANGLE_COMPONENT_TYPEINFO: |
d00edca5 DD |
559 | printf ("typeinfo\n"); |
560 | break; | |
59727473 | 561 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: |
d00edca5 DD |
562 | printf ("typeinfo name\n"); |
563 | break; | |
59727473 | 564 | case DEMANGLE_COMPONENT_TYPEINFO_FN: |
d00edca5 DD |
565 | printf ("typeinfo function\n"); |
566 | break; | |
59727473 | 567 | case DEMANGLE_COMPONENT_THUNK: |
d00edca5 DD |
568 | printf ("thunk\n"); |
569 | break; | |
59727473 | 570 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: |
d00edca5 DD |
571 | printf ("virtual thunk\n"); |
572 | break; | |
59727473 | 573 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: |
d00edca5 DD |
574 | printf ("covariant thunk\n"); |
575 | break; | |
59727473 | 576 | case DEMANGLE_COMPONENT_JAVA_CLASS: |
d00edca5 DD |
577 | printf ("java class\n"); |
578 | break; | |
59727473 | 579 | case DEMANGLE_COMPONENT_GUARD: |
d00edca5 DD |
580 | printf ("guard\n"); |
581 | break; | |
59727473 | 582 | case DEMANGLE_COMPONENT_REFTEMP: |
d00edca5 DD |
583 | printf ("reference temporary\n"); |
584 | break; | |
839e4798 RH |
585 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
586 | printf ("hidden alias\n"); | |
587 | break; | |
956a8f8b DD |
588 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
589 | printf ("transaction clone\n"); | |
590 | break; | |
591 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: | |
592 | printf ("non-transaction clone\n"); | |
593 | break; | |
59727473 | 594 | case DEMANGLE_COMPONENT_RESTRICT: |
d00edca5 DD |
595 | printf ("restrict\n"); |
596 | break; | |
59727473 | 597 | case DEMANGLE_COMPONENT_VOLATILE: |
d00edca5 DD |
598 | printf ("volatile\n"); |
599 | break; | |
59727473 | 600 | case DEMANGLE_COMPONENT_CONST: |
d00edca5 DD |
601 | printf ("const\n"); |
602 | break; | |
59727473 | 603 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
858b45cf DD |
604 | printf ("restrict this\n"); |
605 | break; | |
59727473 | 606 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
858b45cf DD |
607 | printf ("volatile this\n"); |
608 | break; | |
59727473 | 609 | case DEMANGLE_COMPONENT_CONST_THIS: |
858b45cf DD |
610 | printf ("const this\n"); |
611 | break; | |
59727473 | 612 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
d00edca5 DD |
613 | printf ("vendor type qualifier\n"); |
614 | break; | |
59727473 | 615 | case DEMANGLE_COMPONENT_POINTER: |
d00edca5 DD |
616 | printf ("pointer\n"); |
617 | break; | |
59727473 | 618 | case DEMANGLE_COMPONENT_REFERENCE: |
d00edca5 DD |
619 | printf ("reference\n"); |
620 | break; | |
8969a67f DD |
621 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
622 | printf ("rvalue reference\n"); | |
623 | break; | |
59727473 | 624 | case DEMANGLE_COMPONENT_COMPLEX: |
d00edca5 DD |
625 | printf ("complex\n"); |
626 | break; | |
59727473 | 627 | case DEMANGLE_COMPONENT_IMAGINARY: |
d00edca5 DD |
628 | printf ("imaginary\n"); |
629 | break; | |
59727473 | 630 | case DEMANGLE_COMPONENT_VENDOR_TYPE: |
d00edca5 DD |
631 | printf ("vendor type\n"); |
632 | break; | |
59727473 | 633 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
d00edca5 DD |
634 | printf ("function type\n"); |
635 | break; | |
59727473 | 636 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
d00edca5 DD |
637 | printf ("array type\n"); |
638 | break; | |
59727473 | 639 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
d00edca5 DD |
640 | printf ("pointer to member type\n"); |
641 | break; | |
d2825c1a DD |
642 | case DEMANGLE_COMPONENT_FIXED_TYPE: |
643 | printf ("fixed-point type\n"); | |
644 | break; | |
59727473 | 645 | case DEMANGLE_COMPONENT_ARGLIST: |
d00edca5 DD |
646 | printf ("argument list\n"); |
647 | break; | |
59727473 | 648 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: |
d00edca5 DD |
649 | printf ("template argument list\n"); |
650 | break; | |
eb7b5ddb DD |
651 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
652 | printf ("initializer list\n"); | |
653 | break; | |
59727473 | 654 | case DEMANGLE_COMPONENT_CAST: |
d00edca5 DD |
655 | printf ("cast\n"); |
656 | break; | |
eb7b5ddb DD |
657 | case DEMANGLE_COMPONENT_NULLARY: |
658 | printf ("nullary operator\n"); | |
659 | break; | |
59727473 | 660 | case DEMANGLE_COMPONENT_UNARY: |
d00edca5 DD |
661 | printf ("unary operator\n"); |
662 | break; | |
59727473 | 663 | case DEMANGLE_COMPONENT_BINARY: |
d00edca5 DD |
664 | printf ("binary operator\n"); |
665 | break; | |
59727473 | 666 | case DEMANGLE_COMPONENT_BINARY_ARGS: |
d00edca5 DD |
667 | printf ("binary operator arguments\n"); |
668 | break; | |
59727473 | 669 | case DEMANGLE_COMPONENT_TRINARY: |
d00edca5 DD |
670 | printf ("trinary operator\n"); |
671 | break; | |
59727473 | 672 | case DEMANGLE_COMPONENT_TRINARY_ARG1: |
d00edca5 DD |
673 | printf ("trinary operator arguments 1\n"); |
674 | break; | |
59727473 | 675 | case DEMANGLE_COMPONENT_TRINARY_ARG2: |
d00edca5 DD |
676 | printf ("trinary operator arguments 1\n"); |
677 | break; | |
59727473 | 678 | case DEMANGLE_COMPONENT_LITERAL: |
d00edca5 DD |
679 | printf ("literal\n"); |
680 | break; | |
59727473 | 681 | case DEMANGLE_COMPONENT_LITERAL_NEG: |
97ceaf5b DD |
682 | printf ("negative literal\n"); |
683 | break; | |
830ef634 DD |
684 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
685 | printf ("java resource\n"); | |
686 | break; | |
687 | case DEMANGLE_COMPONENT_COMPOUND_NAME: | |
688 | printf ("compound name\n"); | |
689 | break; | |
690 | case DEMANGLE_COMPONENT_CHARACTER: | |
691 | printf ("character '%c'\n", dc->u.s_character.character); | |
692 | return; | |
ba8cb4ba DD |
693 | case DEMANGLE_COMPONENT_DECLTYPE: |
694 | printf ("decltype\n"); | |
695 | break; | |
1c08f2c8 DD |
696 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
697 | printf ("pack expansion\n"); | |
698 | break; | |
eb383413 L |
699 | } |
700 | ||
d00edca5 DD |
701 | d_dump (d_left (dc), indent + 2); |
702 | d_dump (d_right (dc), indent + 2); | |
703 | } | |
704 | ||
705 | #endif /* CP_DEMANGLE_DEBUG */ | |
706 | ||
59727473 DD |
707 | /* Fill in a DEMANGLE_COMPONENT_NAME. */ |
708 | ||
709 | CP_STATIC_IF_GLIBCPP_V3 | |
710 | int | |
9334f9c6 | 711 | cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len) |
59727473 DD |
712 | { |
713 | if (p == NULL || s == NULL || len == 0) | |
714 | return 0; | |
715 | p->type = DEMANGLE_COMPONENT_NAME; | |
716 | p->u.s_name.s = s; | |
717 | p->u.s_name.len = len; | |
718 | return 1; | |
719 | } | |
720 | ||
721 | /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */ | |
722 | ||
723 | CP_STATIC_IF_GLIBCPP_V3 | |
724 | int | |
9334f9c6 DD |
725 | cplus_demangle_fill_extended_operator (struct demangle_component *p, int args, |
726 | struct demangle_component *name) | |
59727473 DD |
727 | { |
728 | if (p == NULL || args < 0 || name == NULL) | |
729 | return 0; | |
730 | p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR; | |
731 | p->u.s_extended_operator.args = args; | |
732 | p->u.s_extended_operator.name = name; | |
733 | return 1; | |
734 | } | |
735 | ||
736 | /* Fill in a DEMANGLE_COMPONENT_CTOR. */ | |
737 | ||
738 | CP_STATIC_IF_GLIBCPP_V3 | |
739 | int | |
9334f9c6 DD |
740 | cplus_demangle_fill_ctor (struct demangle_component *p, |
741 | enum gnu_v3_ctor_kinds kind, | |
742 | struct demangle_component *name) | |
59727473 DD |
743 | { |
744 | if (p == NULL | |
745 | || name == NULL | |
4e55d6c3 | 746 | || (int) kind < gnu_v3_complete_object_ctor |
956a8f8b | 747 | || (int) kind > gnu_v3_object_ctor_group) |
59727473 DD |
748 | return 0; |
749 | p->type = DEMANGLE_COMPONENT_CTOR; | |
750 | p->u.s_ctor.kind = kind; | |
751 | p->u.s_ctor.name = name; | |
752 | return 1; | |
753 | } | |
754 | ||
755 | /* Fill in a DEMANGLE_COMPONENT_DTOR. */ | |
756 | ||
757 | CP_STATIC_IF_GLIBCPP_V3 | |
758 | int | |
9334f9c6 DD |
759 | cplus_demangle_fill_dtor (struct demangle_component *p, |
760 | enum gnu_v3_dtor_kinds kind, | |
761 | struct demangle_component *name) | |
59727473 DD |
762 | { |
763 | if (p == NULL | |
764 | || name == NULL | |
4e55d6c3 | 765 | || (int) kind < gnu_v3_deleting_dtor |
956a8f8b | 766 | || (int) kind > gnu_v3_object_dtor_group) |
59727473 DD |
767 | return 0; |
768 | p->type = DEMANGLE_COMPONENT_DTOR; | |
769 | p->u.s_dtor.kind = kind; | |
770 | p->u.s_dtor.name = name; | |
771 | return 1; | |
772 | } | |
773 | ||
d00edca5 DD |
774 | /* Add a new component. */ |
775 | ||
59727473 | 776 | static struct demangle_component * |
9334f9c6 | 777 | d_make_empty (struct d_info *di) |
d00edca5 | 778 | { |
59727473 | 779 | struct demangle_component *p; |
d00edca5 DD |
780 | |
781 | if (di->next_comp >= di->num_comps) | |
782 | return NULL; | |
783 | p = &di->comps[di->next_comp]; | |
d00edca5 DD |
784 | ++di->next_comp; |
785 | return p; | |
786 | } | |
787 | ||
788 | /* Add a new generic component. */ | |
789 | ||
59727473 | 790 | static struct demangle_component * |
9334f9c6 DD |
791 | d_make_comp (struct d_info *di, enum demangle_component_type type, |
792 | struct demangle_component *left, | |
793 | struct demangle_component *right) | |
d00edca5 | 794 | { |
59727473 | 795 | struct demangle_component *p; |
d00edca5 DD |
796 | |
797 | /* We check for errors here. A typical error would be a NULL return | |
331c3da2 DD |
798 | from a subroutine. We catch those here, and return NULL |
799 | upward. */ | |
d00edca5 DD |
800 | switch (type) |
801 | { | |
802 | /* These types require two parameters. */ | |
59727473 DD |
803 | case DEMANGLE_COMPONENT_QUAL_NAME: |
804 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
805 | case DEMANGLE_COMPONENT_TYPED_NAME: | |
806 | case DEMANGLE_COMPONENT_TEMPLATE: | |
2d6520ee | 807 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
59727473 DD |
808 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
809 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: | |
810 | case DEMANGLE_COMPONENT_UNARY: | |
811 | case DEMANGLE_COMPONENT_BINARY: | |
812 | case DEMANGLE_COMPONENT_BINARY_ARGS: | |
813 | case DEMANGLE_COMPONENT_TRINARY: | |
814 | case DEMANGLE_COMPONENT_TRINARY_ARG1: | |
59727473 DD |
815 | case DEMANGLE_COMPONENT_LITERAL: |
816 | case DEMANGLE_COMPONENT_LITERAL_NEG: | |
830ef634 | 817 | case DEMANGLE_COMPONENT_COMPOUND_NAME: |
cbc43128 | 818 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
7955ede5 | 819 | case DEMANGLE_COMPONENT_CLONE: |
d00edca5 DD |
820 | if (left == NULL || right == NULL) |
821 | return NULL; | |
822 | break; | |
823 | ||
824 | /* These types only require one parameter. */ | |
59727473 DD |
825 | case DEMANGLE_COMPONENT_VTABLE: |
826 | case DEMANGLE_COMPONENT_VTT: | |
59727473 DD |
827 | case DEMANGLE_COMPONENT_TYPEINFO: |
828 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: | |
829 | case DEMANGLE_COMPONENT_TYPEINFO_FN: | |
830 | case DEMANGLE_COMPONENT_THUNK: | |
831 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: | |
832 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: | |
833 | case DEMANGLE_COMPONENT_JAVA_CLASS: | |
834 | case DEMANGLE_COMPONENT_GUARD: | |
835 | case DEMANGLE_COMPONENT_REFTEMP: | |
839e4798 | 836 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
956a8f8b DD |
837 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
838 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: | |
59727473 DD |
839 | case DEMANGLE_COMPONENT_POINTER: |
840 | case DEMANGLE_COMPONENT_REFERENCE: | |
8969a67f | 841 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
59727473 DD |
842 | case DEMANGLE_COMPONENT_COMPLEX: |
843 | case DEMANGLE_COMPONENT_IMAGINARY: | |
844 | case DEMANGLE_COMPONENT_VENDOR_TYPE: | |
59727473 | 845 | case DEMANGLE_COMPONENT_CAST: |
830ef634 | 846 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
ba8cb4ba | 847 | case DEMANGLE_COMPONENT_DECLTYPE: |
1c08f2c8 | 848 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
d5031754 DD |
849 | case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS: |
850 | case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS: | |
eb7b5ddb DD |
851 | case DEMANGLE_COMPONENT_NULLARY: |
852 | case DEMANGLE_COMPONENT_TRINARY_ARG2: | |
d00edca5 DD |
853 | if (left == NULL) |
854 | return NULL; | |
855 | break; | |
856 | ||
857 | /* This needs a right parameter, but the left parameter can be | |
858 | empty. */ | |
59727473 | 859 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
eb7b5ddb | 860 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
d00edca5 DD |
861 | if (right == NULL) |
862 | return NULL; | |
863 | break; | |
864 | ||
865 | /* These are allowed to have no parameters--in some cases they | |
866 | will be filled in later. */ | |
59727473 DD |
867 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
868 | case DEMANGLE_COMPONENT_RESTRICT: | |
869 | case DEMANGLE_COMPONENT_VOLATILE: | |
870 | case DEMANGLE_COMPONENT_CONST: | |
871 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
872 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
873 | case DEMANGLE_COMPONENT_CONST_THIS: | |
1c08f2c8 DD |
874 | case DEMANGLE_COMPONENT_ARGLIST: |
875 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: | |
d00edca5 DD |
876 | break; |
877 | ||
878 | /* Other types should not be seen here. */ | |
879 | default: | |
880 | return NULL; | |
eb383413 | 881 | } |
d00edca5 | 882 | |
59727473 | 883 | p = d_make_empty (di); |
d00edca5 | 884 | if (p != NULL) |
eb383413 | 885 | { |
59727473 | 886 | p->type = type; |
d00edca5 DD |
887 | p->u.s_binary.left = left; |
888 | p->u.s_binary.right = right; | |
eb383413 | 889 | } |
d00edca5 DD |
890 | return p; |
891 | } | |
eb383413 | 892 | |
a0692e36 L |
893 | /* Add a new demangle mangled name component. */ |
894 | ||
895 | static struct demangle_component * | |
896 | d_make_demangle_mangled_name (struct d_info *di, const char *s) | |
897 | { | |
898 | if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z') | |
899 | return d_make_name (di, s, strlen (s)); | |
900 | d_advance (di, 2); | |
901 | return d_encoding (di, 0); | |
902 | } | |
903 | ||
d00edca5 | 904 | /* Add a new name component. */ |
03d5f569 | 905 | |
59727473 | 906 | static struct demangle_component * |
9334f9c6 | 907 | d_make_name (struct d_info *di, const char *s, int len) |
d00edca5 | 908 | { |
59727473 | 909 | struct demangle_component *p; |
03d5f569 | 910 | |
59727473 DD |
911 | p = d_make_empty (di); |
912 | if (! cplus_demangle_fill_name (p, s, len)) | |
858b45cf | 913 | return NULL; |
d00edca5 | 914 | return p; |
eb383413 L |
915 | } |
916 | ||
d00edca5 | 917 | /* Add a new builtin type component. */ |
eb383413 | 918 | |
59727473 | 919 | static struct demangle_component * |
9334f9c6 DD |
920 | d_make_builtin_type (struct d_info *di, |
921 | const struct demangle_builtin_type_info *type) | |
eb383413 | 922 | { |
59727473 | 923 | struct demangle_component *p; |
d00edca5 | 924 | |
331c3da2 DD |
925 | if (type == NULL) |
926 | return NULL; | |
59727473 | 927 | p = d_make_empty (di); |
d00edca5 | 928 | if (p != NULL) |
59727473 DD |
929 | { |
930 | p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE; | |
931 | p->u.s_builtin.type = type; | |
932 | } | |
d00edca5 DD |
933 | return p; |
934 | } | |
eb383413 | 935 | |
d00edca5 | 936 | /* Add a new operator component. */ |
eb383413 | 937 | |
59727473 | 938 | static struct demangle_component * |
9334f9c6 | 939 | d_make_operator (struct d_info *di, const struct demangle_operator_info *op) |
eb383413 | 940 | { |
59727473 | 941 | struct demangle_component *p; |
d00edca5 | 942 | |
59727473 | 943 | p = d_make_empty (di); |
d00edca5 | 944 | if (p != NULL) |
59727473 DD |
945 | { |
946 | p->type = DEMANGLE_COMPONENT_OPERATOR; | |
947 | p->u.s_operator.op = op; | |
948 | } | |
d00edca5 | 949 | return p; |
eb383413 L |
950 | } |
951 | ||
d00edca5 | 952 | /* Add a new extended operator component. */ |
eb383413 | 953 | |
59727473 | 954 | static struct demangle_component * |
9334f9c6 DD |
955 | d_make_extended_operator (struct d_info *di, int args, |
956 | struct demangle_component *name) | |
eb383413 | 957 | { |
59727473 | 958 | struct demangle_component *p; |
03d5f569 | 959 | |
59727473 DD |
960 | p = d_make_empty (di); |
961 | if (! cplus_demangle_fill_extended_operator (p, args, name)) | |
331c3da2 | 962 | return NULL; |
d00edca5 | 963 | return p; |
eb383413 L |
964 | } |
965 | ||
664aa91f DD |
966 | static struct demangle_component * |
967 | d_make_default_arg (struct d_info *di, int num, | |
968 | struct demangle_component *sub) | |
969 | { | |
970 | struct demangle_component *p = d_make_empty (di); | |
971 | if (p) | |
972 | { | |
973 | p->type = DEMANGLE_COMPONENT_DEFAULT_ARG; | |
974 | p->u.s_unary_num.num = num; | |
975 | p->u.s_unary_num.sub = sub; | |
976 | } | |
977 | return p; | |
978 | } | |
979 | ||
d00edca5 | 980 | /* Add a new constructor component. */ |
eb383413 | 981 | |
59727473 | 982 | static struct demangle_component * |
9334f9c6 DD |
983 | d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind, |
984 | struct demangle_component *name) | |
eb383413 | 985 | { |
59727473 | 986 | struct demangle_component *p; |
d00edca5 | 987 | |
59727473 DD |
988 | p = d_make_empty (di); |
989 | if (! cplus_demangle_fill_ctor (p, kind, name)) | |
331c3da2 | 990 | return NULL; |
d00edca5 | 991 | return p; |
eb383413 L |
992 | } |
993 | ||
d00edca5 | 994 | /* Add a new destructor component. */ |
eb383413 | 995 | |
59727473 | 996 | static struct demangle_component * |
9334f9c6 DD |
997 | d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind, |
998 | struct demangle_component *name) | |
eb383413 | 999 | { |
59727473 | 1000 | struct demangle_component *p; |
d00edca5 | 1001 | |
59727473 DD |
1002 | p = d_make_empty (di); |
1003 | if (! cplus_demangle_fill_dtor (p, kind, name)) | |
331c3da2 | 1004 | return NULL; |
d00edca5 | 1005 | return p; |
eb383413 L |
1006 | } |
1007 | ||
d00edca5 | 1008 | /* Add a new template parameter. */ |
59666b35 | 1009 | |
59727473 | 1010 | static struct demangle_component * |
9334f9c6 | 1011 | d_make_template_param (struct d_info *di, long i) |
59666b35 | 1012 | { |
59727473 | 1013 | struct demangle_component *p; |
d00edca5 | 1014 | |
59727473 | 1015 | p = d_make_empty (di); |
d00edca5 | 1016 | if (p != NULL) |
59727473 DD |
1017 | { |
1018 | p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM; | |
1019 | p->u.s_number.number = i; | |
1020 | } | |
d00edca5 | 1021 | return p; |
59666b35 DD |
1022 | } |
1023 | ||
c743cf5d DD |
1024 | /* Add a new function parameter. */ |
1025 | ||
1026 | static struct demangle_component * | |
1027 | d_make_function_param (struct d_info *di, long i) | |
1028 | { | |
1029 | struct demangle_component *p; | |
1030 | ||
1031 | p = d_make_empty (di); | |
1032 | if (p != NULL) | |
1033 | { | |
1034 | p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM; | |
1035 | p->u.s_number.number = i; | |
1036 | } | |
1037 | return p; | |
1038 | } | |
1039 | ||
d00edca5 | 1040 | /* Add a new standard substitution component. */ |
59666b35 | 1041 | |
59727473 | 1042 | static struct demangle_component * |
9334f9c6 | 1043 | d_make_sub (struct d_info *di, const char *name, int len) |
59666b35 | 1044 | { |
59727473 | 1045 | struct demangle_component *p; |
d00edca5 | 1046 | |
59727473 | 1047 | p = d_make_empty (di); |
d00edca5 | 1048 | if (p != NULL) |
b6fb00c0 | 1049 | { |
59727473 | 1050 | p->type = DEMANGLE_COMPONENT_SUB_STD; |
b6fb00c0 DD |
1051 | p->u.s_string.string = name; |
1052 | p->u.s_string.len = len; | |
1053 | } | |
d00edca5 | 1054 | return p; |
59666b35 DD |
1055 | } |
1056 | ||
7955ede5 | 1057 | /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]* |
331c3da2 DD |
1058 | |
1059 | TOP_LEVEL is non-zero when called at the top level. */ | |
59666b35 | 1060 | |
59727473 DD |
1061 | CP_STATIC_IF_GLIBCPP_V3 |
1062 | struct demangle_component * | |
9334f9c6 | 1063 | cplus_demangle_mangled_name (struct d_info *di, int top_level) |
59666b35 | 1064 | { |
7955ede5 DD |
1065 | struct demangle_component *p; |
1066 | ||
c743cf5d DD |
1067 | if (! d_check_char (di, '_') |
1068 | /* Allow missing _ if not at toplevel to work around a | |
1069 | bug in G++ abi-version=2 mangling; see the comment in | |
1070 | write_template_arg. */ | |
1071 | && top_level) | |
d00edca5 | 1072 | return NULL; |
6ef6358e | 1073 | if (! d_check_char (di, 'Z')) |
d00edca5 | 1074 | return NULL; |
7955ede5 DD |
1075 | p = d_encoding (di, top_level); |
1076 | ||
1077 | /* If at top level and parsing parameters, check for a clone | |
1078 | suffix. */ | |
1079 | if (top_level && (di->options & DMGL_PARAMS) != 0) | |
1080 | while (d_peek_char (di) == '.' | |
1081 | && (IS_LOWER (d_peek_next_char (di)) | |
1082 | || d_peek_next_char (di) == '_' | |
1083 | || IS_DIGIT (d_peek_next_char (di)))) | |
1084 | p = d_clone_suffix (di, p); | |
1085 | ||
1086 | return p; | |
59666b35 DD |
1087 | } |
1088 | ||
d00edca5 DD |
1089 | /* Return whether a function should have a return type. The argument |
1090 | is the function name, which may be qualified in various ways. The | |
1091 | rules are that template functions have return types with some | |
1092 | exceptions, function types which are not part of a function name | |
1093 | mangling have return types with some exceptions, and non-template | |
1094 | function names do not have return types. The exceptions are that | |
1095 | constructors, destructors, and conversion operators do not have | |
1096 | return types. */ | |
59666b35 DD |
1097 | |
1098 | static int | |
9334f9c6 | 1099 | has_return_type (struct demangle_component *dc) |
59666b35 | 1100 | { |
d00edca5 DD |
1101 | if (dc == NULL) |
1102 | return 0; | |
1103 | switch (dc->type) | |
1104 | { | |
1105 | default: | |
1106 | return 0; | |
59727473 | 1107 | case DEMANGLE_COMPONENT_TEMPLATE: |
d00edca5 | 1108 | return ! is_ctor_dtor_or_conversion (d_left (dc)); |
59727473 DD |
1109 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
1110 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
1111 | case DEMANGLE_COMPONENT_CONST_THIS: | |
54a962d9 | 1112 | return has_return_type (d_left (dc)); |
d00edca5 | 1113 | } |
59666b35 DD |
1114 | } |
1115 | ||
d00edca5 DD |
1116 | /* Return whether a name is a constructor, a destructor, or a |
1117 | conversion operator. */ | |
eb383413 L |
1118 | |
1119 | static int | |
9334f9c6 | 1120 | is_ctor_dtor_or_conversion (struct demangle_component *dc) |
eb383413 | 1121 | { |
d00edca5 DD |
1122 | if (dc == NULL) |
1123 | return 0; | |
1124 | switch (dc->type) | |
1125 | { | |
1126 | default: | |
1127 | return 0; | |
59727473 DD |
1128 | case DEMANGLE_COMPONENT_QUAL_NAME: |
1129 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
d00edca5 | 1130 | return is_ctor_dtor_or_conversion (d_right (dc)); |
59727473 DD |
1131 | case DEMANGLE_COMPONENT_CTOR: |
1132 | case DEMANGLE_COMPONENT_DTOR: | |
1133 | case DEMANGLE_COMPONENT_CAST: | |
d00edca5 DD |
1134 | return 1; |
1135 | } | |
eb383413 L |
1136 | } |
1137 | ||
d00edca5 DD |
1138 | /* <encoding> ::= <(function) name> <bare-function-type> |
1139 | ::= <(data) name> | |
6d95373e DD |
1140 | ::= <special-name> |
1141 | ||
1142 | TOP_LEVEL is non-zero when called at the top level, in which case | |
1143 | if DMGL_PARAMS is not set we do not demangle the function | |
1144 | parameters. We only set this at the top level, because otherwise | |
1145 | we would not correctly demangle names in local scopes. */ | |
eb383413 | 1146 | |
59727473 | 1147 | static struct demangle_component * |
9334f9c6 | 1148 | d_encoding (struct d_info *di, int top_level) |
eb383413 | 1149 | { |
d00edca5 | 1150 | char peek = d_peek_char (di); |
03d5f569 | 1151 | |
d00edca5 DD |
1152 | if (peek == 'G' || peek == 'T') |
1153 | return d_special_name (di); | |
1154 | else | |
03d5f569 | 1155 | { |
59727473 | 1156 | struct demangle_component *dc; |
d00edca5 DD |
1157 | |
1158 | dc = d_name (di); | |
331c3da2 DD |
1159 | |
1160 | if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0) | |
1161 | { | |
1162 | /* Strip off any initial CV-qualifiers, as they really apply | |
1163 | to the `this' parameter, and they were not output by the | |
1164 | v2 demangler without DMGL_PARAMS. */ | |
59727473 DD |
1165 | while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
1166 | || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS | |
1167 | || dc->type == DEMANGLE_COMPONENT_CONST_THIS) | |
331c3da2 | 1168 | dc = d_left (dc); |
820542c9 | 1169 | |
59727473 DD |
1170 | /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then |
1171 | there may be CV-qualifiers on its right argument which | |
1172 | really apply here; this happens when parsing a class | |
1173 | which is local to a function. */ | |
1174 | if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME) | |
820542c9 | 1175 | { |
59727473 | 1176 | struct demangle_component *dcr; |
820542c9 DD |
1177 | |
1178 | dcr = d_right (dc); | |
59727473 DD |
1179 | while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
1180 | || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS | |
1181 | || dcr->type == DEMANGLE_COMPONENT_CONST_THIS) | |
820542c9 DD |
1182 | dcr = d_left (dcr); |
1183 | dc->u.s_binary.right = dcr; | |
1184 | } | |
1185 | ||
331c3da2 DD |
1186 | return dc; |
1187 | } | |
1188 | ||
d00edca5 | 1189 | peek = d_peek_char (di); |
8d301070 | 1190 | if (dc == NULL || peek == '\0' || peek == 'E') |
d00edca5 | 1191 | return dc; |
59727473 | 1192 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc, |
d00edca5 | 1193 | d_bare_function_type (di, has_return_type (dc))); |
03d5f569 | 1194 | } |
d00edca5 DD |
1195 | } |
1196 | ||
1197 | /* <name> ::= <nested-name> | |
1198 | ::= <unscoped-name> | |
1199 | ::= <unscoped-template-name> <template-args> | |
1200 | ::= <local-name> | |
1201 | ||
1202 | <unscoped-name> ::= <unqualified-name> | |
1203 | ::= St <unqualified-name> | |
eb383413 | 1204 | |
d00edca5 DD |
1205 | <unscoped-template-name> ::= <unscoped-name> |
1206 | ::= <substitution> | |
1207 | */ | |
1208 | ||
59727473 | 1209 | static struct demangle_component * |
9334f9c6 | 1210 | d_name (struct d_info *di) |
d00edca5 DD |
1211 | { |
1212 | char peek = d_peek_char (di); | |
59727473 | 1213 | struct demangle_component *dc; |
d00edca5 DD |
1214 | |
1215 | switch (peek) | |
eb383413 | 1216 | { |
d00edca5 DD |
1217 | case 'N': |
1218 | return d_nested_name (di); | |
1219 | ||
1220 | case 'Z': | |
1221 | return d_local_name (di); | |
1222 | ||
8bf955e1 | 1223 | case 'L': |
664aa91f | 1224 | case 'U': |
8bf955e1 | 1225 | return d_unqualified_name (di); |
664aa91f | 1226 | |
d00edca5 DD |
1227 | case 'S': |
1228 | { | |
1229 | int subst; | |
1230 | ||
1231 | if (d_peek_next_char (di) != 't') | |
1232 | { | |
97ceaf5b | 1233 | dc = d_substitution (di, 0); |
d00edca5 DD |
1234 | subst = 1; |
1235 | } | |
1236 | else | |
1237 | { | |
1238 | d_advance (di, 2); | |
59727473 DD |
1239 | dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, |
1240 | d_make_name (di, "std", 3), | |
d00edca5 | 1241 | d_unqualified_name (di)); |
b6fb00c0 | 1242 | di->expansion += 3; |
d00edca5 DD |
1243 | subst = 0; |
1244 | } | |
1245 | ||
1246 | if (d_peek_char (di) != 'I') | |
1247 | { | |
1248 | /* The grammar does not permit this case to occur if we | |
1249 | called d_substitution() above (i.e., subst == 1). We | |
1250 | don't bother to check. */ | |
1251 | } | |
1252 | else | |
1253 | { | |
1254 | /* This is <template-args>, which means that we just saw | |
1255 | <unscoped-template-name>, which is a substitution | |
1256 | candidate if we didn't just get it from a | |
1257 | substitution. */ | |
1258 | if (! subst) | |
1259 | { | |
1260 | if (! d_add_substitution (di, dc)) | |
1261 | return NULL; | |
1262 | } | |
59727473 DD |
1263 | dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc, |
1264 | d_template_args (di)); | |
d00edca5 DD |
1265 | } |
1266 | ||
1267 | return dc; | |
1268 | } | |
1269 | ||
1270 | default: | |
1271 | dc = d_unqualified_name (di); | |
1272 | if (d_peek_char (di) == 'I') | |
03d5f569 | 1273 | { |
d00edca5 DD |
1274 | /* This is <template-args>, which means that we just saw |
1275 | <unscoped-template-name>, which is a substitution | |
1276 | candidate. */ | |
1277 | if (! d_add_substitution (di, dc)) | |
1278 | return NULL; | |
59727473 DD |
1279 | dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc, |
1280 | d_template_args (di)); | |
03d5f569 | 1281 | } |
d00edca5 | 1282 | return dc; |
eb383413 | 1283 | } |
d00edca5 | 1284 | } |
eb383413 | 1285 | |
d00edca5 DD |
1286 | /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
1287 | ::= N [<CV-qualifiers>] <template-prefix> <template-args> E | |
1288 | */ | |
eb383413 | 1289 | |
59727473 | 1290 | static struct demangle_component * |
9334f9c6 | 1291 | d_nested_name (struct d_info *di) |
d00edca5 | 1292 | { |
59727473 DD |
1293 | struct demangle_component *ret; |
1294 | struct demangle_component **pret; | |
03d5f569 | 1295 | |
6ef6358e | 1296 | if (! d_check_char (di, 'N')) |
d00edca5 | 1297 | return NULL; |
eb383413 | 1298 | |
858b45cf | 1299 | pret = d_cv_qualifiers (di, &ret, 1); |
d00edca5 DD |
1300 | if (pret == NULL) |
1301 | return NULL; | |
1302 | ||
1303 | *pret = d_prefix (di); | |
1304 | if (*pret == NULL) | |
1305 | return NULL; | |
eb383413 | 1306 | |
6ef6358e | 1307 | if (! d_check_char (di, 'E')) |
eb383413 L |
1308 | return NULL; |
1309 | ||
d00edca5 | 1310 | return ret; |
eb383413 L |
1311 | } |
1312 | ||
d00edca5 DD |
1313 | /* <prefix> ::= <prefix> <unqualified-name> |
1314 | ::= <template-prefix> <template-args> | |
1315 | ::= <template-param> | |
6b6bd65a | 1316 | ::= <decltype> |
d00edca5 DD |
1317 | ::= |
1318 | ::= <substitution> | |
eb383413 | 1319 | |
d00edca5 DD |
1320 | <template-prefix> ::= <prefix> <(template) unqualified-name> |
1321 | ::= <template-param> | |
1322 | ::= <substitution> | |
1323 | */ | |
1324 | ||
59727473 | 1325 | static struct demangle_component * |
9334f9c6 | 1326 | d_prefix (struct d_info *di) |
eb383413 | 1327 | { |
59727473 | 1328 | struct demangle_component *ret = NULL; |
eb383413 | 1329 | |
d00edca5 | 1330 | while (1) |
eb383413 | 1331 | { |
d00edca5 | 1332 | char peek; |
59727473 DD |
1333 | enum demangle_component_type comb_type; |
1334 | struct demangle_component *dc; | |
d00edca5 DD |
1335 | |
1336 | peek = d_peek_char (di); | |
1337 | if (peek == '\0') | |
1338 | return NULL; | |
1339 | ||
1340 | /* The older code accepts a <local-name> here, but I don't see | |
1341 | that in the grammar. The older code does not accept a | |
1342 | <template-param> here. */ | |
eb383413 | 1343 | |
59727473 | 1344 | comb_type = DEMANGLE_COMPONENT_QUAL_NAME; |
6b6bd65a DD |
1345 | if (peek == 'D') |
1346 | { | |
1347 | char peek2 = d_peek_next_char (di); | |
1348 | if (peek2 == 'T' || peek2 == 't') | |
1349 | /* Decltype. */ | |
1350 | dc = cplus_demangle_type (di); | |
1351 | else | |
1352 | /* Destructor name. */ | |
1353 | dc = d_unqualified_name (di); | |
1354 | } | |
1355 | else if (IS_DIGIT (peek) | |
858b45cf | 1356 | || IS_LOWER (peek) |
d00edca5 | 1357 | || peek == 'C' |
664aa91f | 1358 | || peek == 'U' |
8bf955e1 | 1359 | || peek == 'L') |
d00edca5 DD |
1360 | dc = d_unqualified_name (di); |
1361 | else if (peek == 'S') | |
97ceaf5b | 1362 | dc = d_substitution (di, 1); |
d00edca5 DD |
1363 | else if (peek == 'I') |
1364 | { | |
1365 | if (ret == NULL) | |
1366 | return NULL; | |
59727473 | 1367 | comb_type = DEMANGLE_COMPONENT_TEMPLATE; |
d00edca5 DD |
1368 | dc = d_template_args (di); |
1369 | } | |
1370 | else if (peek == 'T') | |
1371 | dc = d_template_param (di); | |
1372 | else if (peek == 'E') | |
1373 | return ret; | |
664aa91f DD |
1374 | else if (peek == 'M') |
1375 | { | |
1376 | /* Initializer scope for a lambda. We don't need to represent | |
1377 | this; the normal code will just treat the variable as a type | |
1378 | scope, which gives appropriate output. */ | |
1379 | if (ret == NULL) | |
1380 | return NULL; | |
1381 | d_advance (di, 1); | |
1382 | continue; | |
1383 | } | |
d00edca5 DD |
1384 | else |
1385 | return NULL; | |
1386 | ||
1387 | if (ret == NULL) | |
1388 | ret = dc; | |
eb383413 | 1389 | else |
d00edca5 DD |
1390 | ret = d_make_comp (di, comb_type, ret, dc); |
1391 | ||
1392 | if (peek != 'S' && d_peek_char (di) != 'E') | |
1393 | { | |
1394 | if (! d_add_substitution (di, ret)) | |
1395 | return NULL; | |
1396 | } | |
eb383413 L |
1397 | } |
1398 | } | |
1399 | ||
d00edca5 DD |
1400 | /* <unqualified-name> ::= <operator-name> |
1401 | ::= <ctor-dtor-name> | |
1402 | ::= <source-name> | |
8bf955e1 GK |
1403 | ::= <local-source-name> |
1404 | ||
1405 | <local-source-name> ::= L <source-name> <discriminator> | |
d00edca5 | 1406 | */ |
eb383413 | 1407 | |
59727473 | 1408 | static struct demangle_component * |
9334f9c6 | 1409 | d_unqualified_name (struct d_info *di) |
eb383413 | 1410 | { |
d00edca5 DD |
1411 | char peek; |
1412 | ||
1413 | peek = d_peek_char (di); | |
1414 | if (IS_DIGIT (peek)) | |
1415 | return d_source_name (di); | |
858b45cf | 1416 | else if (IS_LOWER (peek)) |
b6fb00c0 | 1417 | { |
59727473 | 1418 | struct demangle_component *ret; |
b6fb00c0 DD |
1419 | |
1420 | ret = d_operator_name (di); | |
59727473 | 1421 | if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR) |
b6fb00c0 DD |
1422 | di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2; |
1423 | return ret; | |
1424 | } | |
d00edca5 DD |
1425 | else if (peek == 'C' || peek == 'D') |
1426 | return d_ctor_dtor_name (di); | |
8bf955e1 GK |
1427 | else if (peek == 'L') |
1428 | { | |
1429 | struct demangle_component * ret; | |
1430 | ||
1431 | d_advance (di, 1); | |
1432 | ||
1433 | ret = d_source_name (di); | |
1434 | if (ret == NULL) | |
1435 | return NULL; | |
1436 | if (! d_discriminator (di)) | |
1437 | return NULL; | |
1438 | return ret; | |
1439 | } | |
664aa91f DD |
1440 | else if (peek == 'U') |
1441 | { | |
1442 | switch (d_peek_next_char (di)) | |
1443 | { | |
1444 | case 'l': | |
1445 | return d_lambda (di); | |
1446 | case 't': | |
1447 | return d_unnamed_type (di); | |
1448 | default: | |
1449 | return NULL; | |
1450 | } | |
1451 | } | |
d00edca5 | 1452 | else |
03d5f569 | 1453 | return NULL; |
eb383413 L |
1454 | } |
1455 | ||
d00edca5 | 1456 | /* <source-name> ::= <(positive length) number> <identifier> */ |
eb383413 | 1457 | |
59727473 | 1458 | static struct demangle_component * |
9334f9c6 | 1459 | d_source_name (struct d_info *di) |
eb383413 | 1460 | { |
d00edca5 | 1461 | long len; |
59727473 | 1462 | struct demangle_component *ret; |
d00edca5 DD |
1463 | |
1464 | len = d_number (di); | |
1465 | if (len <= 0) | |
1466 | return NULL; | |
1467 | ret = d_identifier (di, len); | |
1468 | di->last_name = ret; | |
1469 | return ret; | |
eb383413 L |
1470 | } |
1471 | ||
d00edca5 | 1472 | /* number ::= [n] <(non-negative decimal integer)> */ |
eb383413 | 1473 | |
d00edca5 | 1474 | static long |
9334f9c6 | 1475 | d_number (struct d_info *di) |
eb383413 | 1476 | { |
b6fb00c0 | 1477 | int negative; |
d00edca5 DD |
1478 | char peek; |
1479 | long ret; | |
eb383413 | 1480 | |
b6fb00c0 | 1481 | negative = 0; |
d00edca5 DD |
1482 | peek = d_peek_char (di); |
1483 | if (peek == 'n') | |
1484 | { | |
b6fb00c0 | 1485 | negative = 1; |
d00edca5 DD |
1486 | d_advance (di, 1); |
1487 | peek = d_peek_char (di); | |
1488 | } | |
eb383413 | 1489 | |
d00edca5 DD |
1490 | ret = 0; |
1491 | while (1) | |
eb383413 | 1492 | { |
d00edca5 | 1493 | if (! IS_DIGIT (peek)) |
b6fb00c0 DD |
1494 | { |
1495 | if (negative) | |
1496 | ret = - ret; | |
1497 | return ret; | |
1498 | } | |
d00edca5 DD |
1499 | ret = ret * 10 + peek - '0'; |
1500 | d_advance (di, 1); | |
1501 | peek = d_peek_char (di); | |
eb383413 | 1502 | } |
eb383413 L |
1503 | } |
1504 | ||
cbc43128 DD |
1505 | /* Like d_number, but returns a demangle_component. */ |
1506 | ||
1507 | static struct demangle_component * | |
1508 | d_number_component (struct d_info *di) | |
1509 | { | |
1510 | struct demangle_component *ret = d_make_empty (di); | |
1511 | if (ret) | |
1512 | { | |
1513 | ret->type = DEMANGLE_COMPONENT_NUMBER; | |
1514 | ret->u.s_number.number = d_number (di); | |
1515 | } | |
1516 | return ret; | |
1517 | } | |
1518 | ||
d00edca5 | 1519 | /* identifier ::= <(unqualified source code identifier)> */ |
eb383413 | 1520 | |
59727473 | 1521 | static struct demangle_component * |
9334f9c6 | 1522 | d_identifier (struct d_info *di, int len) |
eb383413 | 1523 | { |
d00edca5 | 1524 | const char *name; |
eb383413 | 1525 | |
d00edca5 | 1526 | name = d_str (di); |
b6fb00c0 DD |
1527 | |
1528 | if (di->send - name < len) | |
1529 | return NULL; | |
1530 | ||
d00edca5 | 1531 | d_advance (di, len); |
eb383413 | 1532 | |
2730f651 DD |
1533 | /* A Java mangled name may have a trailing '$' if it is a C++ |
1534 | keyword. This '$' is not included in the length count. We just | |
1535 | ignore the '$'. */ | |
1536 | if ((di->options & DMGL_JAVA) != 0 | |
1537 | && d_peek_char (di) == '$') | |
1538 | d_advance (di, 1); | |
1539 | ||
d00edca5 DD |
1540 | /* Look for something which looks like a gcc encoding of an |
1541 | anonymous namespace, and replace it with a more user friendly | |
1542 | name. */ | |
1543 | if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2 | |
1544 | && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX, | |
1545 | ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0) | |
eb383413 | 1546 | { |
d00edca5 DD |
1547 | const char *s; |
1548 | ||
1549 | s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN; | |
1550 | if ((*s == '.' || *s == '_' || *s == '$') | |
1551 | && s[1] == 'N') | |
b6fb00c0 DD |
1552 | { |
1553 | di->expansion -= len - sizeof "(anonymous namespace)"; | |
1554 | return d_make_name (di, "(anonymous namespace)", | |
1555 | sizeof "(anonymous namespace)" - 1); | |
1556 | } | |
eb383413 | 1557 | } |
d00edca5 DD |
1558 | |
1559 | return d_make_name (di, name, len); | |
eb383413 L |
1560 | } |
1561 | ||
d00edca5 DD |
1562 | /* operator_name ::= many different two character encodings. |
1563 | ::= cv <type> | |
1564 | ::= v <digit> <source-name> | |
eb7b5ddb DD |
1565 | |
1566 | This list is sorted for binary search. */ | |
eb383413 | 1567 | |
b6fb00c0 DD |
1568 | #define NL(s) s, (sizeof s) - 1 |
1569 | ||
59727473 DD |
1570 | CP_STATIC_IF_GLIBCPP_V3 |
1571 | const struct demangle_operator_info cplus_demangle_operators[] = | |
d00edca5 | 1572 | { |
b6fb00c0 DD |
1573 | { "aN", NL ("&="), 2 }, |
1574 | { "aS", NL ("="), 2 }, | |
1575 | { "aa", NL ("&&"), 2 }, | |
1576 | { "ad", NL ("&"), 1 }, | |
1577 | { "an", NL ("&"), 2 }, | |
eb7b5ddb DD |
1578 | { "at", NL ("alignof "), 1 }, |
1579 | { "az", NL ("alignof "), 1 }, | |
ba8cb4ba | 1580 | { "cl", NL ("()"), 2 }, |
b6fb00c0 DD |
1581 | { "cm", NL (","), 2 }, |
1582 | { "co", NL ("~"), 1 }, | |
1583 | { "dV", NL ("/="), 2 }, | |
eb7b5ddb | 1584 | { "da", NL ("delete[] "), 1 }, |
b6fb00c0 | 1585 | { "de", NL ("*"), 1 }, |
eb7b5ddb DD |
1586 | { "dl", NL ("delete "), 1 }, |
1587 | { "ds", NL (".*"), 2 }, | |
1c08f2c8 | 1588 | { "dt", NL ("."), 2 }, |
b6fb00c0 DD |
1589 | { "dv", NL ("/"), 2 }, |
1590 | { "eO", NL ("^="), 2 }, | |
1591 | { "eo", NL ("^"), 2 }, | |
1592 | { "eq", NL ("=="), 2 }, | |
1593 | { "ge", NL (">="), 2 }, | |
eb7b5ddb | 1594 | { "gs", NL ("::"), 1 }, |
b6fb00c0 DD |
1595 | { "gt", NL (">"), 2 }, |
1596 | { "ix", NL ("[]"), 2 }, | |
1597 | { "lS", NL ("<<="), 2 }, | |
1598 | { "le", NL ("<="), 2 }, | |
1599 | { "ls", NL ("<<"), 2 }, | |
1600 | { "lt", NL ("<"), 2 }, | |
1601 | { "mI", NL ("-="), 2 }, | |
1602 | { "mL", NL ("*="), 2 }, | |
1603 | { "mi", NL ("-"), 2 }, | |
1604 | { "ml", NL ("*"), 2 }, | |
1605 | { "mm", NL ("--"), 1 }, | |
eb7b5ddb | 1606 | { "na", NL ("new[]"), 3 }, |
b6fb00c0 DD |
1607 | { "ne", NL ("!="), 2 }, |
1608 | { "ng", NL ("-"), 1 }, | |
1609 | { "nt", NL ("!"), 1 }, | |
eb7b5ddb | 1610 | { "nw", NL ("new"), 3 }, |
b6fb00c0 DD |
1611 | { "oR", NL ("|="), 2 }, |
1612 | { "oo", NL ("||"), 2 }, | |
1613 | { "or", NL ("|"), 2 }, | |
1614 | { "pL", NL ("+="), 2 }, | |
1615 | { "pl", NL ("+"), 2 }, | |
1616 | { "pm", NL ("->*"), 2 }, | |
1617 | { "pp", NL ("++"), 1 }, | |
1618 | { "ps", NL ("+"), 1 }, | |
1619 | { "pt", NL ("->"), 2 }, | |
1620 | { "qu", NL ("?"), 3 }, | |
1621 | { "rM", NL ("%="), 2 }, | |
1622 | { "rS", NL (">>="), 2 }, | |
1623 | { "rm", NL ("%"), 2 }, | |
1624 | { "rs", NL (">>"), 2 }, | |
1625 | { "st", NL ("sizeof "), 1 }, | |
59727473 | 1626 | { "sz", NL ("sizeof "), 1 }, |
eb7b5ddb DD |
1627 | { "tr", NL ("throw"), 0 }, |
1628 | { "tw", NL ("throw "), 1 }, | |
59727473 | 1629 | { NULL, NULL, 0, 0 } |
d00edca5 | 1630 | }; |
eb383413 | 1631 | |
59727473 | 1632 | static struct demangle_component * |
9334f9c6 | 1633 | d_operator_name (struct d_info *di) |
eb383413 | 1634 | { |
d00edca5 DD |
1635 | char c1; |
1636 | char c2; | |
eb383413 | 1637 | |
d00edca5 DD |
1638 | c1 = d_next_char (di); |
1639 | c2 = d_next_char (di); | |
1640 | if (c1 == 'v' && IS_DIGIT (c2)) | |
1641 | return d_make_extended_operator (di, c2 - '0', d_source_name (di)); | |
1642 | else if (c1 == 'c' && c2 == 'v') | |
59727473 DD |
1643 | return d_make_comp (di, DEMANGLE_COMPONENT_CAST, |
1644 | cplus_demangle_type (di), NULL); | |
d00edca5 | 1645 | else |
eb383413 | 1646 | { |
59727473 | 1647 | /* LOW is the inclusive lower bound. */ |
d00edca5 | 1648 | int low = 0; |
59727473 DD |
1649 | /* HIGH is the exclusive upper bound. We subtract one to ignore |
1650 | the sentinel at the end of the array. */ | |
1651 | int high = ((sizeof (cplus_demangle_operators) | |
1652 | / sizeof (cplus_demangle_operators[0])) | |
1653 | - 1); | |
eb383413 | 1654 | |
d00edca5 DD |
1655 | while (1) |
1656 | { | |
1657 | int i; | |
59727473 | 1658 | const struct demangle_operator_info *p; |
eb383413 | 1659 | |
d00edca5 | 1660 | i = low + (high - low) / 2; |
59727473 | 1661 | p = cplus_demangle_operators + i; |
eb383413 | 1662 | |
d00edca5 DD |
1663 | if (c1 == p->code[0] && c2 == p->code[1]) |
1664 | return d_make_operator (di, p); | |
1665 | ||
1666 | if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1])) | |
1667 | high = i; | |
1668 | else | |
1669 | low = i + 1; | |
1670 | if (low == high) | |
1671 | return NULL; | |
1672 | } | |
1673 | } | |
eb383413 L |
1674 | } |
1675 | ||
830ef634 DD |
1676 | static struct demangle_component * |
1677 | d_make_character (struct d_info *di, int c) | |
1678 | { | |
1679 | struct demangle_component *p; | |
1680 | p = d_make_empty (di); | |
1681 | if (p != NULL) | |
1682 | { | |
1683 | p->type = DEMANGLE_COMPONENT_CHARACTER; | |
1684 | p->u.s_character.character = c; | |
1685 | } | |
1686 | return p; | |
1687 | } | |
1688 | ||
1689 | static struct demangle_component * | |
1690 | d_java_resource (struct d_info *di) | |
1691 | { | |
1692 | struct demangle_component *p = NULL; | |
1693 | struct demangle_component *next = NULL; | |
1694 | long len, i; | |
1695 | char c; | |
1696 | const char *str; | |
1697 | ||
1698 | len = d_number (di); | |
1699 | if (len <= 1) | |
1700 | return NULL; | |
1701 | ||
1702 | /* Eat the leading '_'. */ | |
1703 | if (d_next_char (di) != '_') | |
1704 | return NULL; | |
1705 | len--; | |
1706 | ||
1707 | str = d_str (di); | |
1708 | i = 0; | |
1709 | ||
1710 | while (len > 0) | |
1711 | { | |
1712 | c = str[i]; | |
1713 | if (!c) | |
1714 | return NULL; | |
1715 | ||
1716 | /* Each chunk is either a '$' escape... */ | |
1717 | if (c == '$') | |
1718 | { | |
1719 | i++; | |
1720 | switch (str[i++]) | |
1721 | { | |
1722 | case 'S': | |
1723 | c = '/'; | |
1724 | break; | |
1725 | case '_': | |
1726 | c = '.'; | |
1727 | break; | |
1728 | case '$': | |
1729 | c = '$'; | |
1730 | break; | |
1731 | default: | |
1732 | return NULL; | |
1733 | } | |
1734 | next = d_make_character (di, c); | |
1735 | d_advance (di, i); | |
1736 | str = d_str (di); | |
1737 | len -= i; | |
1738 | i = 0; | |
1739 | if (next == NULL) | |
1740 | return NULL; | |
1741 | } | |
1742 | /* ... or a sequence of characters. */ | |
1743 | else | |
1744 | { | |
1745 | while (i < len && str[i] && str[i] != '$') | |
1746 | i++; | |
1747 | ||
1748 | next = d_make_name (di, str, i); | |
1749 | d_advance (di, i); | |
1750 | str = d_str (di); | |
1751 | len -= i; | |
1752 | i = 0; | |
1753 | if (next == NULL) | |
1754 | return NULL; | |
1755 | } | |
1756 | ||
1757 | if (p == NULL) | |
1758 | p = next; | |
1759 | else | |
1760 | { | |
1761 | p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next); | |
1762 | if (p == NULL) | |
1763 | return NULL; | |
1764 | } | |
1765 | } | |
1766 | ||
1767 | p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL); | |
1768 | ||
1769 | return p; | |
1770 | } | |
1771 | ||
d00edca5 DD |
1772 | /* <special-name> ::= TV <type> |
1773 | ::= TT <type> | |
1774 | ::= TI <type> | |
1775 | ::= TS <type> | |
1776 | ::= GV <(object) name> | |
1777 | ::= T <call-offset> <(base) encoding> | |
1778 | ::= Tc <call-offset> <call-offset> <(base) encoding> | |
1779 | Also g++ extensions: | |
1780 | ::= TC <type> <(offset) number> _ <(base) type> | |
1781 | ::= TF <type> | |
1782 | ::= TJ <type> | |
1783 | ::= GR <name> | |
839e4798 | 1784 | ::= GA <encoding> |
830ef634 | 1785 | ::= Gr <resource name> |
956a8f8b DD |
1786 | ::= GTt <encoding> |
1787 | ::= GTn <encoding> | |
d00edca5 | 1788 | */ |
eb383413 | 1789 | |
59727473 | 1790 | static struct demangle_component * |
9334f9c6 | 1791 | d_special_name (struct d_info *di) |
eb383413 | 1792 | { |
b6fb00c0 | 1793 | di->expansion += 20; |
6ef6358e | 1794 | if (d_check_char (di, 'T')) |
03d5f569 | 1795 | { |
d00edca5 DD |
1796 | switch (d_next_char (di)) |
1797 | { | |
1798 | case 'V': | |
b6fb00c0 | 1799 | di->expansion -= 5; |
59727473 DD |
1800 | return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE, |
1801 | cplus_demangle_type (di), NULL); | |
d00edca5 | 1802 | case 'T': |
b6fb00c0 | 1803 | di->expansion -= 10; |
59727473 DD |
1804 | return d_make_comp (di, DEMANGLE_COMPONENT_VTT, |
1805 | cplus_demangle_type (di), NULL); | |
d00edca5 | 1806 | case 'I': |
59727473 DD |
1807 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO, |
1808 | cplus_demangle_type (di), NULL); | |
d00edca5 | 1809 | case 'S': |
59727473 DD |
1810 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME, |
1811 | cplus_demangle_type (di), NULL); | |
eb383413 | 1812 | |
d00edca5 DD |
1813 | case 'h': |
1814 | if (! d_call_offset (di, 'h')) | |
1815 | return NULL; | |
59727473 DD |
1816 | return d_make_comp (di, DEMANGLE_COMPONENT_THUNK, |
1817 | d_encoding (di, 0), NULL); | |
eb383413 | 1818 | |
d00edca5 DD |
1819 | case 'v': |
1820 | if (! d_call_offset (di, 'v')) | |
1821 | return NULL; | |
59727473 DD |
1822 | return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK, |
1823 | d_encoding (di, 0), NULL); | |
eb383413 | 1824 | |
d00edca5 DD |
1825 | case 'c': |
1826 | if (! d_call_offset (di, '\0')) | |
1827 | return NULL; | |
1828 | if (! d_call_offset (di, '\0')) | |
1829 | return NULL; | |
59727473 DD |
1830 | return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK, |
1831 | d_encoding (di, 0), NULL); | |
eb383413 | 1832 | |
d00edca5 DD |
1833 | case 'C': |
1834 | { | |
59727473 | 1835 | struct demangle_component *derived_type; |
d00edca5 | 1836 | long offset; |
59727473 | 1837 | struct demangle_component *base_type; |
d00edca5 | 1838 | |
59727473 | 1839 | derived_type = cplus_demangle_type (di); |
d00edca5 DD |
1840 | offset = d_number (di); |
1841 | if (offset < 0) | |
1842 | return NULL; | |
6ef6358e | 1843 | if (! d_check_char (di, '_')) |
d00edca5 | 1844 | return NULL; |
59727473 | 1845 | base_type = cplus_demangle_type (di); |
d00edca5 DD |
1846 | /* We don't display the offset. FIXME: We should display |
1847 | it in verbose mode. */ | |
b6fb00c0 | 1848 | di->expansion += 5; |
59727473 DD |
1849 | return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, |
1850 | base_type, derived_type); | |
d00edca5 | 1851 | } |
eb383413 | 1852 | |
d00edca5 | 1853 | case 'F': |
59727473 DD |
1854 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN, |
1855 | cplus_demangle_type (di), NULL); | |
d00edca5 | 1856 | case 'J': |
59727473 DD |
1857 | return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS, |
1858 | cplus_demangle_type (di), NULL); | |
eb383413 | 1859 | |
d00edca5 DD |
1860 | default: |
1861 | return NULL; | |
1862 | } | |
eb383413 | 1863 | } |
6ef6358e | 1864 | else if (d_check_char (di, 'G')) |
eb383413 | 1865 | { |
d00edca5 DD |
1866 | switch (d_next_char (di)) |
1867 | { | |
1868 | case 'V': | |
59727473 | 1869 | return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL); |
d00edca5 DD |
1870 | |
1871 | case 'R': | |
abc6552b DD |
1872 | { |
1873 | struct demangle_component *name = d_name (di); | |
1874 | return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name, | |
1875 | d_number_component (di)); | |
1876 | } | |
d00edca5 | 1877 | |
839e4798 RH |
1878 | case 'A': |
1879 | return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS, | |
1880 | d_encoding (di, 0), NULL); | |
1881 | ||
956a8f8b DD |
1882 | case 'T': |
1883 | switch (d_next_char (di)) | |
1884 | { | |
1885 | case 'n': | |
1886 | return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE, | |
1887 | d_encoding (di, 0), NULL); | |
1888 | default: | |
1889 | /* ??? The proposal is that other letters (such as 'h') stand | |
1890 | for different variants of transaction cloning, such as | |
1891 | compiling directly for hardware transaction support. But | |
1892 | they still should all be transactional clones of some sort | |
1893 | so go ahead and call them that. */ | |
1894 | case 't': | |
1895 | return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE, | |
1896 | d_encoding (di, 0), NULL); | |
1897 | } | |
1898 | ||
830ef634 DD |
1899 | case 'r': |
1900 | return d_java_resource (di); | |
1901 | ||
d00edca5 DD |
1902 | default: |
1903 | return NULL; | |
1904 | } | |
eb383413 | 1905 | } |
d00edca5 DD |
1906 | else |
1907 | return NULL; | |
eb383413 L |
1908 | } |
1909 | ||
d00edca5 DD |
1910 | /* <call-offset> ::= h <nv-offset> _ |
1911 | ::= v <v-offset> _ | |
eb383413 | 1912 | |
d00edca5 | 1913 | <nv-offset> ::= <(offset) number> |
eb383413 | 1914 | |
d00edca5 | 1915 | <v-offset> ::= <(offset) number> _ <(virtual offset) number> |
eb383413 | 1916 | |
d00edca5 DD |
1917 | The C parameter, if not '\0', is a character we just read which is |
1918 | the start of the <call-offset>. | |
eb383413 | 1919 | |
d00edca5 DD |
1920 | We don't display the offset information anywhere. FIXME: We should |
1921 | display it in verbose mode. */ | |
eb383413 | 1922 | |
d00edca5 | 1923 | static int |
9334f9c6 | 1924 | d_call_offset (struct d_info *di, int c) |
eb383413 | 1925 | { |
d00edca5 DD |
1926 | if (c == '\0') |
1927 | c = d_next_char (di); | |
eb383413 | 1928 | |
d00edca5 | 1929 | if (c == 'h') |
eb129e35 | 1930 | d_number (di); |
d00edca5 | 1931 | else if (c == 'v') |
eb383413 | 1932 | { |
eb129e35 | 1933 | d_number (di); |
6ef6358e | 1934 | if (! d_check_char (di, '_')) |
d00edca5 | 1935 | return 0; |
eb129e35 | 1936 | d_number (di); |
eb383413 | 1937 | } |
d00edca5 DD |
1938 | else |
1939 | return 0; | |
eb383413 | 1940 | |
6ef6358e | 1941 | if (! d_check_char (di, '_')) |
d00edca5 | 1942 | return 0; |
eb383413 | 1943 | |
d00edca5 | 1944 | return 1; |
eb383413 L |
1945 | } |
1946 | ||
d00edca5 DD |
1947 | /* <ctor-dtor-name> ::= C1 |
1948 | ::= C2 | |
1949 | ::= C3 | |
1950 | ::= D0 | |
1951 | ::= D1 | |
1952 | ::= D2 | |
1953 | */ | |
1954 | ||
59727473 | 1955 | static struct demangle_component * |
9334f9c6 | 1956 | d_ctor_dtor_name (struct d_info *di) |
d00edca5 | 1957 | { |
b6fb00c0 DD |
1958 | if (di->last_name != NULL) |
1959 | { | |
59727473 | 1960 | if (di->last_name->type == DEMANGLE_COMPONENT_NAME) |
b6fb00c0 | 1961 | di->expansion += di->last_name->u.s_name.len; |
59727473 | 1962 | else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD) |
b6fb00c0 DD |
1963 | di->expansion += di->last_name->u.s_string.len; |
1964 | } | |
6ef6358e | 1965 | switch (d_peek_char (di)) |
d00edca5 DD |
1966 | { |
1967 | case 'C': | |
1968 | { | |
1969 | enum gnu_v3_ctor_kinds kind; | |
1970 | ||
6ef6358e | 1971 | switch (d_peek_next_char (di)) |
d00edca5 DD |
1972 | { |
1973 | case '1': | |
1974 | kind = gnu_v3_complete_object_ctor; | |
1975 | break; | |
1976 | case '2': | |
1977 | kind = gnu_v3_base_object_ctor; | |
1978 | break; | |
1979 | case '3': | |
1980 | kind = gnu_v3_complete_object_allocating_ctor; | |
1981 | break; | |
956a8f8b DD |
1982 | case '5': |
1983 | kind = gnu_v3_object_ctor_group; | |
1984 | break; | |
d00edca5 DD |
1985 | default: |
1986 | return NULL; | |
1987 | } | |
6ef6358e | 1988 | d_advance (di, 2); |
d00edca5 DD |
1989 | return d_make_ctor (di, kind, di->last_name); |
1990 | } | |
1991 | ||
1992 | case 'D': | |
1993 | { | |
1994 | enum gnu_v3_dtor_kinds kind; | |
1995 | ||
6ef6358e | 1996 | switch (d_peek_next_char (di)) |
d00edca5 DD |
1997 | { |
1998 | case '0': | |
1999 | kind = gnu_v3_deleting_dtor; | |
2000 | break; | |
2001 | case '1': | |
2002 | kind = gnu_v3_complete_object_dtor; | |
2003 | break; | |
2004 | case '2': | |
2005 | kind = gnu_v3_base_object_dtor; | |
2006 | break; | |
956a8f8b DD |
2007 | case '5': |
2008 | kind = gnu_v3_object_dtor_group; | |
2009 | break; | |
d00edca5 DD |
2010 | default: |
2011 | return NULL; | |
2012 | } | |
6ef6358e | 2013 | d_advance (di, 2); |
d00edca5 DD |
2014 | return d_make_dtor (di, kind, di->last_name); |
2015 | } | |
eb383413 | 2016 | |
d00edca5 DD |
2017 | default: |
2018 | return NULL; | |
2019 | } | |
2020 | } | |
eb383413 | 2021 | |
d00edca5 DD |
2022 | /* <type> ::= <builtin-type> |
2023 | ::= <function-type> | |
2024 | ::= <class-enum-type> | |
2025 | ::= <array-type> | |
2026 | ::= <pointer-to-member-type> | |
2027 | ::= <template-param> | |
2028 | ::= <template-template-param> <template-args> | |
2029 | ::= <substitution> | |
2030 | ::= <CV-qualifiers> <type> | |
2031 | ::= P <type> | |
2032 | ::= R <type> | |
8969a67f | 2033 | ::= O <type> (C++0x) |
d00edca5 DD |
2034 | ::= C <type> |
2035 | ::= G <type> | |
2036 | ::= U <source-name> <type> | |
2037 | ||
2038 | <builtin-type> ::= various one letter codes | |
2039 | ::= u <source-name> | |
2040 | */ | |
eb383413 | 2041 | |
59727473 DD |
2042 | CP_STATIC_IF_GLIBCPP_V3 |
2043 | const struct demangle_builtin_type_info | |
2044 | cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] = | |
d00edca5 | 2045 | { |
2d733211 | 2046 | /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT }, |
b6fb00c0 | 2047 | /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL }, |
2d733211 DD |
2048 | /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT }, |
2049 | /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT }, | |
2050 | /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT }, | |
2051 | /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT }, | |
2052 | /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT }, | |
2053 | /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT }, | |
b6fb00c0 | 2054 | /* i */ { NL ("int"), NL ("int"), D_PRINT_INT }, |
2d733211 | 2055 | /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED }, |
b6fb00c0 DD |
2056 | /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
2057 | /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG }, | |
2d733211 | 2058 | /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG }, |
b6fb00c0 | 2059 | /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT }, |
2d733211 DD |
2060 | /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"), |
2061 | D_PRINT_DEFAULT }, | |
1c08f2c8 DD |
2062 | /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
2063 | /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, | |
2064 | /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, | |
2d733211 DD |
2065 | /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT }, |
2066 | /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT }, | |
1c08f2c8 | 2067 | /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
b6fb00c0 | 2068 | /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID }, |
2d733211 DD |
2069 | /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT }, |
2070 | /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG }, | |
2071 | /* y */ { NL ("unsigned long long"), NL ("unsigned long long"), | |
2072 | D_PRINT_UNSIGNED_LONG_LONG }, | |
b6fb00c0 | 2073 | /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT }, |
1c08f2c8 DD |
2074 | /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT }, |
2075 | /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT }, | |
2076 | /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT }, | |
2077 | /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT }, | |
2078 | /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT }, | |
2079 | /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT }, | |
cf383746 DD |
2080 | /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"), |
2081 | D_PRINT_DEFAULT }, | |
d00edca5 | 2082 | }; |
eb383413 | 2083 | |
59727473 DD |
2084 | CP_STATIC_IF_GLIBCPP_V3 |
2085 | struct demangle_component * | |
9334f9c6 | 2086 | cplus_demangle_type (struct d_info *di) |
eb383413 | 2087 | { |
d00edca5 | 2088 | char peek; |
59727473 | 2089 | struct demangle_component *ret; |
d00edca5 DD |
2090 | int can_subst; |
2091 | ||
2092 | /* The ABI specifies that when CV-qualifiers are used, the base type | |
2093 | is substitutable, and the fully qualified type is substitutable, | |
2094 | but the base type with a strict subset of the CV-qualifiers is | |
2095 | not substitutable. The natural recursive implementation of the | |
2096 | CV-qualifiers would cause subsets to be substitutable, so instead | |
2097 | we pull them all off now. | |
2098 | ||
331c3da2 DD |
2099 | FIXME: The ABI says that order-insensitive vendor qualifiers |
2100 | should be handled in the same way, but we have no way to tell | |
2101 | which vendor qualifiers are order-insensitive and which are | |
2102 | order-sensitive. So we just assume that they are all | |
2103 | order-sensitive. g++ 3.4 supports only one vendor qualifier, | |
2104 | __vector, and it treats it as order-sensitive when mangling | |
2105 | names. */ | |
d00edca5 DD |
2106 | |
2107 | peek = d_peek_char (di); | |
2108 | if (peek == 'r' || peek == 'V' || peek == 'K') | |
2109 | { | |
59727473 | 2110 | struct demangle_component **pret; |
74bcd529 | 2111 | |
858b45cf | 2112 | pret = d_cv_qualifiers (di, &ret, 0); |
331c3da2 DD |
2113 | if (pret == NULL) |
2114 | return NULL; | |
59727473 | 2115 | *pret = cplus_demangle_type (di); |
8d301070 | 2116 | if (! *pret || ! d_add_substitution (di, ret)) |
d00edca5 DD |
2117 | return NULL; |
2118 | return ret; | |
2119 | } | |
eb383413 | 2120 | |
d00edca5 | 2121 | can_subst = 1; |
eb383413 | 2122 | |
74bcd529 | 2123 | switch (peek) |
eb383413 | 2124 | { |
d00edca5 DD |
2125 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': |
2126 | case 'h': case 'i': case 'j': case 'l': case 'm': case 'n': | |
2127 | case 'o': case 's': case 't': | |
2128 | case 'v': case 'w': case 'x': case 'y': case 'z': | |
59727473 DD |
2129 | ret = d_make_builtin_type (di, |
2130 | &cplus_demangle_builtin_types[peek - 'a']); | |
b6fb00c0 | 2131 | di->expansion += ret->u.s_builtin.type->len; |
d00edca5 DD |
2132 | can_subst = 0; |
2133 | d_advance (di, 1); | |
2134 | break; | |
2135 | ||
2136 | case 'u': | |
2137 | d_advance (di, 1); | |
59727473 DD |
2138 | ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE, |
2139 | d_source_name (di), NULL); | |
d00edca5 DD |
2140 | break; |
2141 | ||
2142 | case 'F': | |
2143 | ret = d_function_type (di); | |
eb383413 L |
2144 | break; |
2145 | ||
d00edca5 DD |
2146 | case '0': case '1': case '2': case '3': case '4': |
2147 | case '5': case '6': case '7': case '8': case '9': | |
2148 | case 'N': | |
eb383413 | 2149 | case 'Z': |
d00edca5 | 2150 | ret = d_class_enum_type (di); |
eb383413 L |
2151 | break; |
2152 | ||
d00edca5 DD |
2153 | case 'A': |
2154 | ret = d_array_type (di); | |
2155 | break; | |
2156 | ||
2157 | case 'M': | |
2158 | ret = d_pointer_to_member_type (di); | |
2159 | break; | |
2160 | ||
2161 | case 'T': | |
2162 | ret = d_template_param (di); | |
2163 | if (d_peek_char (di) == 'I') | |
03d5f569 | 2164 | { |
d00edca5 DD |
2165 | /* This is <template-template-param> <template-args>. The |
2166 | <template-template-param> part is a substitution | |
2167 | candidate. */ | |
2168 | if (! d_add_substitution (di, ret)) | |
2169 | return NULL; | |
59727473 DD |
2170 | ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, |
2171 | d_template_args (di)); | |
03d5f569 | 2172 | } |
d00edca5 DD |
2173 | break; |
2174 | ||
2175 | case 'S': | |
2176 | /* If this is a special substitution, then it is the start of | |
2177 | <class-enum-type>. */ | |
2178 | { | |
2179 | char peek_next; | |
74bcd529 | 2180 | |
d00edca5 DD |
2181 | peek_next = d_peek_next_char (di); |
2182 | if (IS_DIGIT (peek_next) | |
2183 | || peek_next == '_' | |
858b45cf | 2184 | || IS_UPPER (peek_next)) |
d00edca5 | 2185 | { |
97ceaf5b | 2186 | ret = d_substitution (di, 0); |
d00edca5 DD |
2187 | /* The substituted name may have been a template name and |
2188 | may be followed by tepmlate args. */ | |
2189 | if (d_peek_char (di) == 'I') | |
59727473 | 2190 | ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, |
d00edca5 DD |
2191 | d_template_args (di)); |
2192 | else | |
2193 | can_subst = 0; | |
2194 | } | |
2195 | else | |
2196 | { | |
2197 | ret = d_class_enum_type (di); | |
2198 | /* If the substitution was a complete type, then it is not | |
2199 | a new substitution candidate. However, if the | |
2200 | substitution was followed by template arguments, then | |
2201 | the whole thing is a substitution candidate. */ | |
59727473 | 2202 | if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD) |
d00edca5 DD |
2203 | can_subst = 0; |
2204 | } | |
2205 | } | |
eb383413 L |
2206 | break; |
2207 | ||
8969a67f DD |
2208 | case 'O': |
2209 | d_advance (di, 1); | |
2210 | ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE, | |
2211 | cplus_demangle_type (di), NULL); | |
2212 | break; | |
2213 | ||
d00edca5 DD |
2214 | case 'P': |
2215 | d_advance (di, 1); | |
59727473 DD |
2216 | ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER, |
2217 | cplus_demangle_type (di), NULL); | |
d00edca5 | 2218 | break; |
eb383413 | 2219 | |
d00edca5 DD |
2220 | case 'R': |
2221 | d_advance (di, 1); | |
59727473 | 2222 | ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE, |
8969a67f | 2223 | cplus_demangle_type (di), NULL); |
d00edca5 | 2224 | break; |
eb383413 | 2225 | |
d00edca5 DD |
2226 | case 'C': |
2227 | d_advance (di, 1); | |
59727473 DD |
2228 | ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX, |
2229 | cplus_demangle_type (di), NULL); | |
d00edca5 DD |
2230 | break; |
2231 | ||
2232 | case 'G': | |
2233 | d_advance (di, 1); | |
59727473 DD |
2234 | ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY, |
2235 | cplus_demangle_type (di), NULL); | |
d00edca5 | 2236 | break; |
eb383413 | 2237 | |
d00edca5 DD |
2238 | case 'U': |
2239 | d_advance (di, 1); | |
2240 | ret = d_source_name (di); | |
59727473 DD |
2241 | ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL, |
2242 | cplus_demangle_type (di), ret); | |
eb383413 | 2243 | break; |
d00edca5 | 2244 | |
ba8cb4ba DD |
2245 | case 'D': |
2246 | can_subst = 0; | |
2247 | d_advance (di, 1); | |
2248 | peek = d_next_char (di); | |
2249 | switch (peek) | |
2250 | { | |
2251 | case 'T': | |
2252 | case 't': | |
2253 | /* decltype (expression) */ | |
2254 | ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE, | |
2255 | d_expression (di), NULL); | |
2256 | if (ret && d_next_char (di) != 'E') | |
2257 | ret = NULL; | |
eb7b5ddb | 2258 | can_subst = 1; |
ba8cb4ba DD |
2259 | break; |
2260 | ||
2261 | case 'p': | |
2262 | /* Pack expansion. */ | |
1c08f2c8 DD |
2263 | ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION, |
2264 | cplus_demangle_type (di), NULL); | |
eb7b5ddb | 2265 | can_subst = 1; |
1c08f2c8 | 2266 | break; |
ba8cb4ba DD |
2267 | |
2268 | case 'f': | |
1c08f2c8 DD |
2269 | /* 32-bit decimal floating point */ |
2270 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]); | |
ba8cb4ba DD |
2271 | di->expansion += ret->u.s_builtin.type->len; |
2272 | break; | |
2273 | case 'd': | |
1c08f2c8 DD |
2274 | /* 64-bit DFP */ |
2275 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]); | |
ba8cb4ba DD |
2276 | di->expansion += ret->u.s_builtin.type->len; |
2277 | break; | |
2278 | case 'e': | |
2279 | /* 128-bit DFP */ | |
1c08f2c8 | 2280 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]); |
ba8cb4ba DD |
2281 | di->expansion += ret->u.s_builtin.type->len; |
2282 | break; | |
2283 | case 'h': | |
2284 | /* 16-bit half-precision FP */ | |
1c08f2c8 DD |
2285 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]); |
2286 | di->expansion += ret->u.s_builtin.type->len; | |
2287 | break; | |
2288 | case 's': | |
2289 | /* char16_t */ | |
2290 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]); | |
2291 | di->expansion += ret->u.s_builtin.type->len; | |
2292 | break; | |
2293 | case 'i': | |
2294 | /* char32_t */ | |
2295 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]); | |
ba8cb4ba DD |
2296 | di->expansion += ret->u.s_builtin.type->len; |
2297 | break; | |
d2825c1a DD |
2298 | |
2299 | case 'F': | |
2300 | /* Fixed point types. DF<int bits><length><fract bits><sat> */ | |
2301 | ret = d_make_empty (di); | |
2302 | ret->type = DEMANGLE_COMPONENT_FIXED_TYPE; | |
2303 | if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di)))) | |
2304 | /* For demangling we don't care about the bits. */ | |
2305 | d_number (di); | |
2306 | ret->u.s_fixed.length = cplus_demangle_type (di); | |
cbc43128 DD |
2307 | if (ret->u.s_fixed.length == NULL) |
2308 | return NULL; | |
d2825c1a DD |
2309 | d_number (di); |
2310 | peek = d_next_char (di); | |
2311 | ret->u.s_fixed.sat = (peek == 's'); | |
2312 | break; | |
60cf58f5 | 2313 | |
cbc43128 DD |
2314 | case 'v': |
2315 | ret = d_vector_type (di); | |
eb7b5ddb | 2316 | can_subst = 1; |
cbc43128 DD |
2317 | break; |
2318 | ||
cf383746 DD |
2319 | case 'n': |
2320 | /* decltype(nullptr) */ | |
2321 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]); | |
2322 | di->expansion += ret->u.s_builtin.type->len; | |
2323 | break; | |
2324 | ||
60cf58f5 DD |
2325 | default: |
2326 | return NULL; | |
ba8cb4ba DD |
2327 | } |
2328 | break; | |
2329 | ||
d00edca5 DD |
2330 | default: |
2331 | return NULL; | |
eb383413 L |
2332 | } |
2333 | ||
d00edca5 DD |
2334 | if (can_subst) |
2335 | { | |
2336 | if (! d_add_substitution (di, ret)) | |
2337 | return NULL; | |
2338 | } | |
eb383413 | 2339 | |
d00edca5 DD |
2340 | return ret; |
2341 | } | |
eb383413 | 2342 | |
d00edca5 | 2343 | /* <CV-qualifiers> ::= [r] [V] [K] */ |
eb383413 | 2344 | |
59727473 | 2345 | static struct demangle_component ** |
9334f9c6 DD |
2346 | d_cv_qualifiers (struct d_info *di, |
2347 | struct demangle_component **pret, int member_fn) | |
eb383413 | 2348 | { |
f9fb0b2d | 2349 | struct demangle_component **pstart; |
eb383413 L |
2350 | char peek; |
2351 | ||
f9fb0b2d | 2352 | pstart = pret; |
d00edca5 DD |
2353 | peek = d_peek_char (di); |
2354 | while (peek == 'r' || peek == 'V' || peek == 'K') | |
eb383413 | 2355 | { |
59727473 | 2356 | enum demangle_component_type t; |
59666b35 | 2357 | |
d00edca5 DD |
2358 | d_advance (di, 1); |
2359 | if (peek == 'r') | |
b6fb00c0 | 2360 | { |
59727473 DD |
2361 | t = (member_fn |
2362 | ? DEMANGLE_COMPONENT_RESTRICT_THIS | |
2363 | : DEMANGLE_COMPONENT_RESTRICT); | |
b6fb00c0 DD |
2364 | di->expansion += sizeof "restrict"; |
2365 | } | |
d00edca5 | 2366 | else if (peek == 'V') |
b6fb00c0 | 2367 | { |
59727473 DD |
2368 | t = (member_fn |
2369 | ? DEMANGLE_COMPONENT_VOLATILE_THIS | |
2370 | : DEMANGLE_COMPONENT_VOLATILE); | |
b6fb00c0 DD |
2371 | di->expansion += sizeof "volatile"; |
2372 | } | |
d00edca5 | 2373 | else |
b6fb00c0 | 2374 | { |
59727473 DD |
2375 | t = (member_fn |
2376 | ? DEMANGLE_COMPONENT_CONST_THIS | |
2377 | : DEMANGLE_COMPONENT_CONST); | |
b6fb00c0 DD |
2378 | di->expansion += sizeof "const"; |
2379 | } | |
eb383413 | 2380 | |
d00edca5 DD |
2381 | *pret = d_make_comp (di, t, NULL, NULL); |
2382 | if (*pret == NULL) | |
2383 | return NULL; | |
2384 | pret = &d_left (*pret); | |
eb383413 | 2385 | |
d00edca5 DD |
2386 | peek = d_peek_char (di); |
2387 | } | |
eb383413 | 2388 | |
f9fb0b2d DD |
2389 | if (!member_fn && peek == 'F') |
2390 | { | |
2391 | while (pstart != pret) | |
2392 | { | |
2393 | switch ((*pstart)->type) | |
2394 | { | |
2395 | case DEMANGLE_COMPONENT_RESTRICT: | |
2396 | (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS; | |
2397 | break; | |
2398 | case DEMANGLE_COMPONENT_VOLATILE: | |
2399 | (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS; | |
2400 | break; | |
2401 | case DEMANGLE_COMPONENT_CONST: | |
2402 | (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS; | |
2403 | break; | |
2404 | default: | |
2405 | break; | |
2406 | } | |
2407 | pstart = &d_left (*pstart); | |
2408 | } | |
2409 | } | |
2410 | ||
d00edca5 DD |
2411 | return pret; |
2412 | } | |
eb383413 | 2413 | |
d00edca5 | 2414 | /* <function-type> ::= F [Y] <bare-function-type> E */ |
eb383413 | 2415 | |
59727473 | 2416 | static struct demangle_component * |
9334f9c6 | 2417 | d_function_type (struct d_info *di) |
eb383413 | 2418 | { |
59727473 | 2419 | struct demangle_component *ret; |
eb383413 | 2420 | |
6ef6358e | 2421 | if (! d_check_char (di, 'F')) |
d00edca5 DD |
2422 | return NULL; |
2423 | if (d_peek_char (di) == 'Y') | |
2424 | { | |
2425 | /* Function has C linkage. We don't print this information. | |
2426 | FIXME: We should print it in verbose mode. */ | |
2427 | d_advance (di, 1); | |
2428 | } | |
2429 | ret = d_bare_function_type (di, 1); | |
6ef6358e | 2430 | if (! d_check_char (di, 'E')) |
d00edca5 DD |
2431 | return NULL; |
2432 | return ret; | |
2433 | } | |
74bcd529 | 2434 | |
664aa91f | 2435 | /* <type>+ */ |
eb383413 | 2436 | |
59727473 | 2437 | static struct demangle_component * |
664aa91f | 2438 | d_parmlist (struct d_info *di) |
d00edca5 | 2439 | { |
59727473 DD |
2440 | struct demangle_component *tl; |
2441 | struct demangle_component **ptl; | |
7887b2ce | 2442 | |
d00edca5 DD |
2443 | tl = NULL; |
2444 | ptl = &tl; | |
eb383413 L |
2445 | while (1) |
2446 | { | |
59727473 | 2447 | struct demangle_component *type; |
eb383413 | 2448 | |
664aa91f | 2449 | char peek = d_peek_char (di); |
7955ede5 | 2450 | if (peek == '\0' || peek == 'E' || peek == '.') |
d00edca5 | 2451 | break; |
59727473 | 2452 | type = cplus_demangle_type (di); |
d00edca5 DD |
2453 | if (type == NULL) |
2454 | return NULL; | |
664aa91f DD |
2455 | *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL); |
2456 | if (*ptl == NULL) | |
2457 | return NULL; | |
2458 | ptl = &d_right (*ptl); | |
eb383413 | 2459 | } |
eb383413 | 2460 | |
d00edca5 DD |
2461 | /* There should be at least one parameter type besides the optional |
2462 | return type. A function which takes no arguments will have a | |
2463 | single parameter type void. */ | |
2464 | if (tl == NULL) | |
2465 | return NULL; | |
eb383413 | 2466 | |
d00edca5 DD |
2467 | /* If we have a single parameter type void, omit it. */ |
2468 | if (d_right (tl) == NULL | |
59727473 | 2469 | && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE |
d00edca5 | 2470 | && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID) |
b6fb00c0 DD |
2471 | { |
2472 | di->expansion -= d_left (tl)->u.s_builtin.type->len; | |
664aa91f | 2473 | d_left (tl) = NULL; |
b6fb00c0 | 2474 | } |
eb383413 | 2475 | |
664aa91f DD |
2476 | return tl; |
2477 | } | |
2478 | ||
2479 | /* <bare-function-type> ::= [J]<type>+ */ | |
2480 | ||
2481 | static struct demangle_component * | |
2482 | d_bare_function_type (struct d_info *di, int has_return_type) | |
2483 | { | |
2484 | struct demangle_component *return_type; | |
2485 | struct demangle_component *tl; | |
2486 | char peek; | |
2487 | ||
2488 | /* Detect special qualifier indicating that the first argument | |
2489 | is the return type. */ | |
2490 | peek = d_peek_char (di); | |
2491 | if (peek == 'J') | |
2492 | { | |
2493 | d_advance (di, 1); | |
2494 | has_return_type = 1; | |
2495 | } | |
2496 | ||
2497 | if (has_return_type) | |
2498 | { | |
2499 | return_type = cplus_demangle_type (di); | |
2500 | if (return_type == NULL) | |
2501 | return NULL; | |
2502 | } | |
2503 | else | |
2504 | return_type = NULL; | |
2505 | ||
2506 | tl = d_parmlist (di); | |
2507 | if (tl == NULL) | |
2508 | return NULL; | |
2509 | ||
2510 | return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, | |
2511 | return_type, tl); | |
d00edca5 | 2512 | } |
eb383413 | 2513 | |
d00edca5 | 2514 | /* <class-enum-type> ::= <name> */ |
eb383413 | 2515 | |
59727473 | 2516 | static struct demangle_component * |
9334f9c6 | 2517 | d_class_enum_type (struct d_info *di) |
d00edca5 DD |
2518 | { |
2519 | return d_name (di); | |
2520 | } | |
74bcd529 | 2521 | |
d00edca5 DD |
2522 | /* <array-type> ::= A <(positive dimension) number> _ <(element) type> |
2523 | ::= A [<(dimension) expression>] _ <(element) type> | |
2524 | */ | |
74bcd529 | 2525 | |
59727473 | 2526 | static struct demangle_component * |
9334f9c6 | 2527 | d_array_type (struct d_info *di) |
d00edca5 DD |
2528 | { |
2529 | char peek; | |
59727473 | 2530 | struct demangle_component *dim; |
74bcd529 | 2531 | |
6ef6358e | 2532 | if (! d_check_char (di, 'A')) |
d00edca5 DD |
2533 | return NULL; |
2534 | ||
2535 | peek = d_peek_char (di); | |
2536 | if (peek == '_') | |
2537 | dim = NULL; | |
2538 | else if (IS_DIGIT (peek)) | |
74bcd529 | 2539 | { |
d00edca5 | 2540 | const char *s; |
74bcd529 | 2541 | |
d00edca5 DD |
2542 | s = d_str (di); |
2543 | do | |
2544 | { | |
2545 | d_advance (di, 1); | |
2546 | peek = d_peek_char (di); | |
2547 | } | |
2548 | while (IS_DIGIT (peek)); | |
2549 | dim = d_make_name (di, s, d_str (di) - s); | |
331c3da2 DD |
2550 | if (dim == NULL) |
2551 | return NULL; | |
74bcd529 | 2552 | } |
eb383413 | 2553 | else |
d00edca5 DD |
2554 | { |
2555 | dim = d_expression (di); | |
2556 | if (dim == NULL) | |
2557 | return NULL; | |
2558 | } | |
eb383413 | 2559 | |
6ef6358e | 2560 | if (! d_check_char (di, '_')) |
d00edca5 | 2561 | return NULL; |
eb383413 | 2562 | |
59727473 DD |
2563 | return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim, |
2564 | cplus_demangle_type (di)); | |
d00edca5 | 2565 | } |
eb383413 | 2566 | |
cbc43128 DD |
2567 | /* <vector-type> ::= Dv <number> _ <type> |
2568 | ::= Dv _ <expression> _ <type> */ | |
2569 | ||
2570 | static struct demangle_component * | |
2571 | d_vector_type (struct d_info *di) | |
2572 | { | |
2573 | char peek; | |
2574 | struct demangle_component *dim; | |
2575 | ||
2576 | peek = d_peek_char (di); | |
2577 | if (peek == '_') | |
2578 | { | |
2579 | d_advance (di, 1); | |
2580 | dim = d_expression (di); | |
2581 | } | |
2582 | else | |
2583 | dim = d_number_component (di); | |
2584 | ||
2585 | if (dim == NULL) | |
2586 | return NULL; | |
2587 | ||
2588 | if (! d_check_char (di, '_')) | |
2589 | return NULL; | |
2590 | ||
2591 | return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim, | |
2592 | cplus_demangle_type (di)); | |
2593 | } | |
2594 | ||
d00edca5 | 2595 | /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */ |
eb383413 | 2596 | |
59727473 | 2597 | static struct demangle_component * |
9334f9c6 | 2598 | d_pointer_to_member_type (struct d_info *di) |
eb383413 | 2599 | { |
59727473 DD |
2600 | struct demangle_component *cl; |
2601 | struct demangle_component *mem; | |
2602 | struct demangle_component **pmem; | |
eb383413 | 2603 | |
6ef6358e | 2604 | if (! d_check_char (di, 'M')) |
d00edca5 | 2605 | return NULL; |
eb383413 | 2606 | |
59727473 | 2607 | cl = cplus_demangle_type (di); |
eb383413 | 2608 | |
d00edca5 DD |
2609 | /* The ABI specifies that any type can be a substitution source, and |
2610 | that M is followed by two types, and that when a CV-qualified | |
2611 | type is seen both the base type and the CV-qualified types are | |
2612 | substitution sources. The ABI also specifies that for a pointer | |
2613 | to a CV-qualified member function, the qualifiers are attached to | |
2614 | the second type. Given the grammar, a plain reading of the ABI | |
2615 | suggests that both the CV-qualified member function and the | |
2616 | non-qualified member function are substitution sources. However, | |
2617 | g++ does not work that way. g++ treats only the CV-qualified | |
2618 | member function as a substitution source. FIXME. So to work | |
2619 | with g++, we need to pull off the CV-qualifiers here, in order to | |
cb6c09ac DD |
2620 | avoid calling add_substitution() in cplus_demangle_type(). But |
2621 | for a CV-qualified member which is not a function, g++ does | |
2622 | follow the ABI, so we need to handle that case here by calling | |
2623 | d_add_substitution ourselves. */ | |
eb383413 | 2624 | |
858b45cf | 2625 | pmem = d_cv_qualifiers (di, &mem, 1); |
331c3da2 DD |
2626 | if (pmem == NULL) |
2627 | return NULL; | |
59727473 | 2628 | *pmem = cplus_demangle_type (di); |
8d301070 GK |
2629 | if (*pmem == NULL) |
2630 | return NULL; | |
eb383413 | 2631 | |
cb6c09ac DD |
2632 | if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE) |
2633 | { | |
2634 | if (! d_add_substitution (di, mem)) | |
2635 | return NULL; | |
2636 | } | |
2637 | ||
59727473 | 2638 | return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem); |
eb383413 L |
2639 | } |
2640 | ||
664aa91f DD |
2641 | /* <non-negative number> _ */ |
2642 | ||
2643 | static long | |
2644 | d_compact_number (struct d_info *di) | |
2645 | { | |
2646 | long num; | |
2647 | if (d_peek_char (di) == '_') | |
2648 | num = 0; | |
2649 | else if (d_peek_char (di) == 'n') | |
2650 | return -1; | |
2651 | else | |
2652 | num = d_number (di) + 1; | |
2653 | ||
2654 | if (! d_check_char (di, '_')) | |
2655 | return -1; | |
2656 | return num; | |
2657 | } | |
2658 | ||
d00edca5 DD |
2659 | /* <template-param> ::= T_ |
2660 | ::= T <(parameter-2 non-negative) number> _ | |
2661 | */ | |
eb383413 | 2662 | |
59727473 | 2663 | static struct demangle_component * |
9334f9c6 | 2664 | d_template_param (struct d_info *di) |
eb383413 | 2665 | { |
d00edca5 | 2666 | long param; |
eb383413 | 2667 | |
6ef6358e | 2668 | if (! d_check_char (di, 'T')) |
d00edca5 | 2669 | return NULL; |
eb383413 | 2670 | |
664aa91f DD |
2671 | param = d_compact_number (di); |
2672 | if (param < 0) | |
d00edca5 | 2673 | return NULL; |
eb383413 | 2674 | |
b6fb00c0 DD |
2675 | ++di->did_subs; |
2676 | ||
d00edca5 | 2677 | return d_make_template_param (di, param); |
eb383413 L |
2678 | } |
2679 | ||
d00edca5 DD |
2680 | /* <template-args> ::= I <template-arg>+ E */ |
2681 | ||
59727473 | 2682 | static struct demangle_component * |
9334f9c6 | 2683 | d_template_args (struct d_info *di) |
eb383413 | 2684 | { |
59727473 DD |
2685 | struct demangle_component *hold_last_name; |
2686 | struct demangle_component *al; | |
2687 | struct demangle_component **pal; | |
eb383413 | 2688 | |
d00edca5 DD |
2689 | /* Preserve the last name we saw--don't let the template arguments |
2690 | clobber it, as that would give us the wrong name for a subsequent | |
2691 | constructor or destructor. */ | |
2692 | hold_last_name = di->last_name; | |
eb383413 | 2693 | |
eb7b5ddb DD |
2694 | if (d_peek_char (di) != 'I' |
2695 | && d_peek_char (di) != 'J') | |
d00edca5 | 2696 | return NULL; |
eb7b5ddb | 2697 | d_advance (di, 1); |
eb383413 | 2698 | |
1c08f2c8 DD |
2699 | if (d_peek_char (di) == 'E') |
2700 | { | |
2701 | /* An argument pack can be empty. */ | |
2702 | d_advance (di, 1); | |
2703 | return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL); | |
2704 | } | |
2705 | ||
d00edca5 DD |
2706 | al = NULL; |
2707 | pal = &al; | |
eb383413 L |
2708 | while (1) |
2709 | { | |
59727473 | 2710 | struct demangle_component *a; |
d00edca5 DD |
2711 | |
2712 | a = d_template_arg (di); | |
2713 | if (a == NULL) | |
2714 | return NULL; | |
2715 | ||
59727473 | 2716 | *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL); |
331c3da2 DD |
2717 | if (*pal == NULL) |
2718 | return NULL; | |
d00edca5 DD |
2719 | pal = &d_right (*pal); |
2720 | ||
2721 | if (d_peek_char (di) == 'E') | |
03d5f569 | 2722 | { |
d00edca5 DD |
2723 | d_advance (di, 1); |
2724 | break; | |
03d5f569 | 2725 | } |
eb383413 L |
2726 | } |
2727 | ||
d00edca5 DD |
2728 | di->last_name = hold_last_name; |
2729 | ||
2730 | return al; | |
eb383413 L |
2731 | } |
2732 | ||
d00edca5 DD |
2733 | /* <template-arg> ::= <type> |
2734 | ::= X <expression> E | |
2735 | ::= <expr-primary> | |
2736 | */ | |
eb383413 | 2737 | |
59727473 | 2738 | static struct demangle_component * |
9334f9c6 | 2739 | d_template_arg (struct d_info *di) |
eb383413 | 2740 | { |
59727473 | 2741 | struct demangle_component *ret; |
03d5f569 | 2742 | |
d00edca5 | 2743 | switch (d_peek_char (di)) |
eb383413 | 2744 | { |
d00edca5 DD |
2745 | case 'X': |
2746 | d_advance (di, 1); | |
2747 | ret = d_expression (di); | |
6ef6358e | 2748 | if (! d_check_char (di, 'E')) |
d00edca5 DD |
2749 | return NULL; |
2750 | return ret; | |
b851d07b | 2751 | |
d00edca5 DD |
2752 | case 'L': |
2753 | return d_expr_primary (di); | |
eb383413 | 2754 | |
1c08f2c8 | 2755 | case 'I': |
eb7b5ddb | 2756 | case 'J': |
1c08f2c8 DD |
2757 | /* An argument pack. */ |
2758 | return d_template_args (di); | |
2759 | ||
d00edca5 | 2760 | default: |
59727473 | 2761 | return cplus_demangle_type (di); |
74bcd529 | 2762 | } |
eb383413 L |
2763 | } |
2764 | ||
eb7b5ddb DD |
2765 | /* Parse a sequence of expressions until we hit the terminator |
2766 | character. */ | |
ba8cb4ba DD |
2767 | |
2768 | static struct demangle_component * | |
eb7b5ddb | 2769 | d_exprlist (struct d_info *di, char terminator) |
ba8cb4ba DD |
2770 | { |
2771 | struct demangle_component *list = NULL; | |
2772 | struct demangle_component **p = &list; | |
2773 | ||
eb7b5ddb | 2774 | if (d_peek_char (di) == terminator) |
1c08f2c8 DD |
2775 | { |
2776 | d_advance (di, 1); | |
2777 | return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL); | |
2778 | } | |
2779 | ||
ba8cb4ba DD |
2780 | while (1) |
2781 | { | |
2782 | struct demangle_component *arg = d_expression (di); | |
2783 | if (arg == NULL) | |
2784 | return NULL; | |
2785 | ||
2786 | *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL); | |
2787 | if (*p == NULL) | |
2788 | return NULL; | |
2789 | p = &d_right (*p); | |
2790 | ||
eb7b5ddb | 2791 | if (d_peek_char (di) == terminator) |
ba8cb4ba DD |
2792 | { |
2793 | d_advance (di, 1); | |
2794 | break; | |
2795 | } | |
2796 | } | |
2797 | ||
2798 | return list; | |
2799 | } | |
2800 | ||
d00edca5 DD |
2801 | /* <expression> ::= <(unary) operator-name> <expression> |
2802 | ::= <(binary) operator-name> <expression> <expression> | |
2803 | ::= <(trinary) operator-name> <expression> <expression> <expression> | |
ba8cb4ba | 2804 | ::= cl <expression>+ E |
d00edca5 DD |
2805 | ::= st <type> |
2806 | ::= <template-param> | |
2807 | ::= sr <type> <unqualified-name> | |
2808 | ::= sr <type> <unqualified-name> <template-args> | |
2809 | ::= <expr-primary> | |
2810 | */ | |
2811 | ||
59727473 | 2812 | static struct demangle_component * |
9334f9c6 | 2813 | d_expression (struct d_info *di) |
eb383413 | 2814 | { |
d00edca5 | 2815 | char peek; |
eb383413 | 2816 | |
d00edca5 DD |
2817 | peek = d_peek_char (di); |
2818 | if (peek == 'L') | |
2819 | return d_expr_primary (di); | |
2820 | else if (peek == 'T') | |
2821 | return d_template_param (di); | |
2822 | else if (peek == 's' && d_peek_next_char (di) == 'r') | |
eb383413 | 2823 | { |
59727473 DD |
2824 | struct demangle_component *type; |
2825 | struct demangle_component *name; | |
eb383413 | 2826 | |
d00edca5 | 2827 | d_advance (di, 2); |
59727473 | 2828 | type = cplus_demangle_type (di); |
d00edca5 DD |
2829 | name = d_unqualified_name (di); |
2830 | if (d_peek_char (di) != 'I') | |
59727473 | 2831 | return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name); |
d00edca5 | 2832 | else |
59727473 DD |
2833 | return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, |
2834 | d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name, | |
d00edca5 | 2835 | d_template_args (di))); |
793011ca | 2836 | } |
e2e1864d DD |
2837 | else if (peek == 's' && d_peek_next_char (di) == 'p') |
2838 | { | |
2839 | d_advance (di, 2); | |
2840 | return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION, | |
2841 | d_expression (di), NULL); | |
2842 | } | |
c743cf5d | 2843 | else if (peek == 'f' && d_peek_next_char (di) == 'p') |
ba8cb4ba | 2844 | { |
c743cf5d DD |
2845 | /* Function parameter used in a late-specified return type. */ |
2846 | int index; | |
ba8cb4ba | 2847 | d_advance (di, 2); |
f2917a30 DD |
2848 | if (d_peek_char (di) == 'T') |
2849 | { | |
2850 | /* 'this' parameter. */ | |
2851 | d_advance (di, 1); | |
2852 | index = 0; | |
2853 | } | |
2854 | else | |
2855 | { | |
2856 | index = d_compact_number (di) + 1; | |
2857 | if (index == 0) | |
2858 | return NULL; | |
2859 | } | |
c743cf5d | 2860 | return d_make_function_param (di, index); |
ba8cb4ba | 2861 | } |
cbc43128 DD |
2862 | else if (IS_DIGIT (peek) |
2863 | || (peek == 'o' && d_peek_next_char (di) == 'n')) | |
1c08f2c8 DD |
2864 | { |
2865 | /* We can get an unqualified name as an expression in the case of | |
cbc43128 DD |
2866 | a dependent function call, i.e. decltype(f(t)). */ |
2867 | struct demangle_component *name; | |
2868 | ||
2869 | if (peek == 'o') | |
2870 | /* operator-function-id, i.e. operator+(t). */ | |
2871 | d_advance (di, 2); | |
2872 | ||
2873 | name = d_unqualified_name (di); | |
1c08f2c8 DD |
2874 | if (name == NULL) |
2875 | return NULL; | |
2876 | if (d_peek_char (di) == 'I') | |
2877 | return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name, | |
2878 | d_template_args (di)); | |
2879 | else | |
2880 | return name; | |
2881 | } | |
eb7b5ddb DD |
2882 | else if ((peek == 'i' || peek == 't') |
2883 | && d_peek_next_char (di) == 'l') | |
2884 | { | |
2885 | /* Brace-enclosed initializer list, untyped or typed. */ | |
2886 | struct demangle_component *type = NULL; | |
2887 | if (peek == 't') | |
2888 | type = cplus_demangle_type (di); | |
2889 | d_advance (di, 2); | |
2890 | return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST, | |
2891 | type, d_exprlist (di, 'E')); | |
2892 | } | |
d00edca5 | 2893 | else |
eb383413 | 2894 | { |
59727473 | 2895 | struct demangle_component *op; |
eb7b5ddb | 2896 | const char *code = NULL; |
d00edca5 | 2897 | int args; |
eb383413 | 2898 | |
d00edca5 DD |
2899 | op = d_operator_name (di); |
2900 | if (op == NULL) | |
2901 | return NULL; | |
eb383413 | 2902 | |
59727473 | 2903 | if (op->type == DEMANGLE_COMPONENT_OPERATOR) |
eb7b5ddb DD |
2904 | { |
2905 | code = op->u.s_operator.op->code; | |
2906 | di->expansion += op->u.s_operator.op->len - 2; | |
2907 | if (strcmp (code, "st") == 0) | |
2908 | return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, | |
2909 | cplus_demangle_type (di)); | |
2910 | } | |
eb383413 | 2911 | |
d00edca5 DD |
2912 | switch (op->type) |
2913 | { | |
2914 | default: | |
2915 | return NULL; | |
59727473 | 2916 | case DEMANGLE_COMPONENT_OPERATOR: |
d00edca5 DD |
2917 | args = op->u.s_operator.op->args; |
2918 | break; | |
59727473 | 2919 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
d00edca5 DD |
2920 | args = op->u.s_extended_operator.args; |
2921 | break; | |
59727473 | 2922 | case DEMANGLE_COMPONENT_CAST: |
60cf58f5 | 2923 | args = 1; |
d00edca5 DD |
2924 | break; |
2925 | } | |
2926 | ||
2927 | switch (args) | |
2928 | { | |
eb7b5ddb DD |
2929 | case 0: |
2930 | return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL); | |
2931 | ||
d00edca5 | 2932 | case 1: |
c743cf5d DD |
2933 | { |
2934 | struct demangle_component *operand; | |
eb7b5ddb DD |
2935 | int suffix = 0; |
2936 | ||
2937 | if (code && (code[0] == 'p' || code[0] == 'm') | |
2938 | && code[1] == code[0]) | |
2939 | /* pp_ and mm_ are the prefix variants. */ | |
2940 | suffix = !d_check_char (di, '_'); | |
2941 | ||
c743cf5d DD |
2942 | if (op->type == DEMANGLE_COMPONENT_CAST |
2943 | && d_check_char (di, '_')) | |
eb7b5ddb | 2944 | operand = d_exprlist (di, 'E'); |
c743cf5d DD |
2945 | else |
2946 | operand = d_expression (di); | |
eb7b5ddb DD |
2947 | |
2948 | if (suffix) | |
2949 | /* Indicate the suffix variant for d_print_comp. */ | |
2950 | return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, | |
2951 | d_make_comp (di, | |
2952 | DEMANGLE_COMPONENT_BINARY_ARGS, | |
2953 | operand, operand)); | |
2954 | else | |
2955 | return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, | |
2956 | operand); | |
c743cf5d | 2957 | } |
d00edca5 DD |
2958 | case 2: |
2959 | { | |
59727473 | 2960 | struct demangle_component *left; |
ba8cb4ba | 2961 | struct demangle_component *right; |
d00edca5 DD |
2962 | |
2963 | left = d_expression (di); | |
cbc43128 | 2964 | if (!strcmp (code, "cl")) |
eb7b5ddb | 2965 | right = d_exprlist (di, 'E'); |
cbc43128 DD |
2966 | else if (!strcmp (code, "dt") || !strcmp (code, "pt")) |
2967 | { | |
2968 | right = d_unqualified_name (di); | |
2969 | if (d_peek_char (di) == 'I') | |
2970 | right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, | |
2971 | right, d_template_args (di)); | |
2972 | } | |
ba8cb4ba DD |
2973 | else |
2974 | right = d_expression (di); | |
2975 | ||
59727473 DD |
2976 | return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op, |
2977 | d_make_comp (di, | |
2978 | DEMANGLE_COMPONENT_BINARY_ARGS, | |
ba8cb4ba | 2979 | left, right)); |
d00edca5 DD |
2980 | } |
2981 | case 3: | |
2982 | { | |
59727473 DD |
2983 | struct demangle_component *first; |
2984 | struct demangle_component *second; | |
eb7b5ddb | 2985 | struct demangle_component *third; |
d00edca5 | 2986 | |
eb7b5ddb DD |
2987 | if (!strcmp (code, "qu")) |
2988 | { | |
2989 | /* ?: expression. */ | |
2990 | first = d_expression (di); | |
2991 | second = d_expression (di); | |
2992 | third = d_expression (di); | |
2993 | } | |
2994 | else if (code[0] == 'n') | |
2995 | { | |
2996 | /* new-expression. */ | |
2997 | if (code[1] != 'w' && code[1] != 'a') | |
2998 | return NULL; | |
2999 | first = d_exprlist (di, '_'); | |
3000 | second = cplus_demangle_type (di); | |
3001 | if (d_peek_char (di) == 'E') | |
3002 | { | |
3003 | d_advance (di, 1); | |
3004 | third = NULL; | |
3005 | } | |
3006 | else if (d_peek_char (di) == 'p' | |
3007 | && d_peek_next_char (di) == 'i') | |
3008 | { | |
3009 | /* Parenthesized initializer. */ | |
3010 | d_advance (di, 2); | |
3011 | third = d_exprlist (di, 'E'); | |
3012 | } | |
3013 | else if (d_peek_char (di) == 'i' | |
3014 | && d_peek_next_char (di) == 'l') | |
3015 | /* initializer-list. */ | |
3016 | third = d_expression (di); | |
3017 | else | |
3018 | return NULL; | |
3019 | } | |
3020 | else | |
3021 | return NULL; | |
59727473 DD |
3022 | return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op, |
3023 | d_make_comp (di, | |
3024 | DEMANGLE_COMPONENT_TRINARY_ARG1, | |
3025 | first, | |
d00edca5 | 3026 | d_make_comp (di, |
59727473 | 3027 | DEMANGLE_COMPONENT_TRINARY_ARG2, |
eb7b5ddb | 3028 | second, third))); |
d00edca5 DD |
3029 | } |
3030 | default: | |
3031 | return NULL; | |
3032 | } | |
eb383413 L |
3033 | } |
3034 | } | |
3035 | ||
d00edca5 DD |
3036 | /* <expr-primary> ::= L <type> <(value) number> E |
3037 | ::= L <type> <(value) float> E | |
3038 | ::= L <mangled-name> E | |
3039 | */ | |
74bcd529 | 3040 | |
59727473 | 3041 | static struct demangle_component * |
9334f9c6 | 3042 | d_expr_primary (struct d_info *di) |
74bcd529 | 3043 | { |
59727473 | 3044 | struct demangle_component *ret; |
74bcd529 | 3045 | |
6ef6358e | 3046 | if (! d_check_char (di, 'L')) |
d00edca5 | 3047 | return NULL; |
c743cf5d DD |
3048 | if (d_peek_char (di) == '_' |
3049 | /* Workaround for G++ bug; see comment in write_template_arg. */ | |
3050 | || d_peek_char (di) == 'Z') | |
59727473 | 3051 | ret = cplus_demangle_mangled_name (di, 0); |
d00edca5 | 3052 | else |
74bcd529 | 3053 | { |
59727473 DD |
3054 | struct demangle_component *type; |
3055 | enum demangle_component_type t; | |
d00edca5 DD |
3056 | const char *s; |
3057 | ||
59727473 | 3058 | type = cplus_demangle_type (di); |
a21da8bf DD |
3059 | if (type == NULL) |
3060 | return NULL; | |
d00edca5 | 3061 | |
b6fb00c0 DD |
3062 | /* If we have a type we know how to print, we aren't going to |
3063 | print the type name itself. */ | |
59727473 | 3064 | if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE |
b6fb00c0 DD |
3065 | && type->u.s_builtin.type->print != D_PRINT_DEFAULT) |
3066 | di->expansion -= type->u.s_builtin.type->len; | |
3067 | ||
d00edca5 DD |
3068 | /* Rather than try to interpret the literal value, we just |
3069 | collect it as a string. Note that it's possible to have a | |
3070 | floating point literal here. The ABI specifies that the | |
3071 | format of such literals is machine independent. That's fine, | |
3072 | but what's not fine is that versions of g++ up to 3.2 with | |
3073 | -fabi-version=1 used upper case letters in the hex constant, | |
3074 | and dumped out gcc's internal representation. That makes it | |
3075 | hard to tell where the constant ends, and hard to dump the | |
3076 | constant in any readable form anyhow. We don't attempt to | |
3077 | handle these cases. */ | |
3078 | ||
59727473 | 3079 | t = DEMANGLE_COMPONENT_LITERAL; |
97ceaf5b DD |
3080 | if (d_peek_char (di) == 'n') |
3081 | { | |
59727473 | 3082 | t = DEMANGLE_COMPONENT_LITERAL_NEG; |
97ceaf5b DD |
3083 | d_advance (di, 1); |
3084 | } | |
d00edca5 DD |
3085 | s = d_str (di); |
3086 | while (d_peek_char (di) != 'E') | |
6ba85b8c DD |
3087 | { |
3088 | if (d_peek_char (di) == '\0') | |
3089 | return NULL; | |
3090 | d_advance (di, 1); | |
3091 | } | |
97ceaf5b | 3092 | ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s)); |
d00edca5 | 3093 | } |
6ef6358e | 3094 | if (! d_check_char (di, 'E')) |
d00edca5 DD |
3095 | return NULL; |
3096 | return ret; | |
74bcd529 DD |
3097 | } |
3098 | ||
d00edca5 DD |
3099 | /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>] |
3100 | ::= Z <(function) encoding> E s [<discriminator>] | |
3101 | */ | |
74bcd529 | 3102 | |
59727473 | 3103 | static struct demangle_component * |
9334f9c6 | 3104 | d_local_name (struct d_info *di) |
74bcd529 | 3105 | { |
59727473 | 3106 | struct demangle_component *function; |
74bcd529 | 3107 | |
6ef6358e | 3108 | if (! d_check_char (di, 'Z')) |
d00edca5 | 3109 | return NULL; |
74bcd529 | 3110 | |
6d95373e | 3111 | function = d_encoding (di, 0); |
74bcd529 | 3112 | |
6ef6358e | 3113 | if (! d_check_char (di, 'E')) |
d00edca5 | 3114 | return NULL; |
74bcd529 | 3115 | |
d00edca5 | 3116 | if (d_peek_char (di) == 's') |
74bcd529 | 3117 | { |
d00edca5 DD |
3118 | d_advance (di, 1); |
3119 | if (! d_discriminator (di)) | |
3120 | return NULL; | |
59727473 | 3121 | return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, |
d00edca5 DD |
3122 | d_make_name (di, "string literal", |
3123 | sizeof "string literal" - 1)); | |
74bcd529 | 3124 | } |
d00edca5 | 3125 | else |
74bcd529 | 3126 | { |
59727473 | 3127 | struct demangle_component *name; |
664aa91f DD |
3128 | int num = -1; |
3129 | ||
3130 | if (d_peek_char (di) == 'd') | |
3131 | { | |
3132 | /* Default argument scope: d <number> _. */ | |
3133 | d_advance (di, 1); | |
3134 | num = d_compact_number (di); | |
3135 | if (num < 0) | |
3136 | return NULL; | |
3137 | } | |
74bcd529 | 3138 | |
d00edca5 | 3139 | name = d_name (di); |
664aa91f DD |
3140 | if (name) |
3141 | switch (name->type) | |
3142 | { | |
3143 | /* Lambdas and unnamed types have internal discriminators. */ | |
3144 | case DEMANGLE_COMPONENT_LAMBDA: | |
3145 | case DEMANGLE_COMPONENT_UNNAMED_TYPE: | |
3146 | break; | |
3147 | default: | |
3148 | if (! d_discriminator (di)) | |
3149 | return NULL; | |
3150 | } | |
3151 | if (num >= 0) | |
3152 | name = d_make_default_arg (di, num, name); | |
59727473 | 3153 | return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name); |
74bcd529 | 3154 | } |
74bcd529 DD |
3155 | } |
3156 | ||
d00edca5 | 3157 | /* <discriminator> ::= _ <(non-negative) number> |
eb383413 | 3158 | |
d00edca5 DD |
3159 | We demangle the discriminator, but we don't print it out. FIXME: |
3160 | We should print it out in verbose mode. */ | |
74bcd529 | 3161 | |
d00edca5 | 3162 | static int |
9334f9c6 | 3163 | d_discriminator (struct d_info *di) |
d00edca5 DD |
3164 | { |
3165 | long discrim; | |
74bcd529 | 3166 | |
d00edca5 DD |
3167 | if (d_peek_char (di) != '_') |
3168 | return 1; | |
3169 | d_advance (di, 1); | |
3170 | discrim = d_number (di); | |
3171 | if (discrim < 0) | |
3172 | return 0; | |
3173 | return 1; | |
3174 | } | |
eb383413 | 3175 | |
664aa91f DD |
3176 | /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */ |
3177 | ||
3178 | static struct demangle_component * | |
3179 | d_lambda (struct d_info *di) | |
3180 | { | |
3181 | struct demangle_component *tl; | |
3182 | struct demangle_component *ret; | |
3183 | int num; | |
3184 | ||
3185 | if (! d_check_char (di, 'U')) | |
3186 | return NULL; | |
3187 | if (! d_check_char (di, 'l')) | |
3188 | return NULL; | |
3189 | ||
3190 | tl = d_parmlist (di); | |
3191 | if (tl == NULL) | |
3192 | return NULL; | |
3193 | ||
3194 | if (! d_check_char (di, 'E')) | |
3195 | return NULL; | |
3196 | ||
3197 | num = d_compact_number (di); | |
3198 | if (num < 0) | |
3199 | return NULL; | |
3200 | ||
3201 | ret = d_make_empty (di); | |
3202 | if (ret) | |
3203 | { | |
3204 | ret->type = DEMANGLE_COMPONENT_LAMBDA; | |
3205 | ret->u.s_unary_num.sub = tl; | |
3206 | ret->u.s_unary_num.num = num; | |
3207 | } | |
3208 | ||
3209 | if (! d_add_substitution (di, ret)) | |
3210 | return NULL; | |
3211 | ||
3212 | return ret; | |
3213 | } | |
3214 | ||
3215 | /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */ | |
3216 | ||
3217 | static struct demangle_component * | |
3218 | d_unnamed_type (struct d_info *di) | |
3219 | { | |
3220 | struct demangle_component *ret; | |
3221 | long num; | |
3222 | ||
3223 | if (! d_check_char (di, 'U')) | |
3224 | return NULL; | |
3225 | if (! d_check_char (di, 't')) | |
3226 | return NULL; | |
3227 | ||
3228 | num = d_compact_number (di); | |
3229 | if (num < 0) | |
3230 | return NULL; | |
3231 | ||
3232 | ret = d_make_empty (di); | |
3233 | if (ret) | |
3234 | { | |
3235 | ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE; | |
3236 | ret->u.s_number.number = num; | |
3237 | } | |
3238 | ||
3239 | if (! d_add_substitution (di, ret)) | |
3240 | return NULL; | |
3241 | ||
3242 | return ret; | |
3243 | } | |
3244 | ||
7955ede5 DD |
3245 | /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]* |
3246 | */ | |
3247 | ||
3248 | static struct demangle_component * | |
3249 | d_clone_suffix (struct d_info *di, struct demangle_component *encoding) | |
3250 | { | |
3251 | const char *suffix = d_str (di); | |
3252 | const char *pend = suffix; | |
3253 | struct demangle_component *n; | |
3254 | ||
3255 | if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_')) | |
3256 | { | |
3257 | pend += 2; | |
3258 | while (IS_LOWER (*pend) || *pend == '_') | |
3259 | ++pend; | |
3260 | } | |
3261 | while (*pend == '.' && IS_DIGIT (pend[1])) | |
3262 | { | |
3263 | pend += 2; | |
3264 | while (IS_DIGIT (*pend)) | |
3265 | ++pend; | |
3266 | } | |
3267 | d_advance (di, pend - suffix); | |
3268 | n = d_make_name (di, suffix, pend - suffix); | |
3269 | return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n); | |
3270 | } | |
3271 | ||
d00edca5 | 3272 | /* Add a new substitution. */ |
eb383413 | 3273 | |
d00edca5 | 3274 | static int |
9334f9c6 | 3275 | d_add_substitution (struct d_info *di, struct demangle_component *dc) |
eb383413 | 3276 | { |
331c3da2 DD |
3277 | if (dc == NULL) |
3278 | return 0; | |
d00edca5 DD |
3279 | if (di->next_sub >= di->num_subs) |
3280 | return 0; | |
3281 | di->subs[di->next_sub] = dc; | |
3282 | ++di->next_sub; | |
3283 | return 1; | |
3284 | } | |
3285 | ||
3286 | /* <substitution> ::= S <seq-id> _ | |
3287 | ::= S_ | |
3288 | ::= St | |
3289 | ::= Sa | |
3290 | ::= Sb | |
3291 | ::= Ss | |
3292 | ::= Si | |
3293 | ::= So | |
3294 | ::= Sd | |
97ceaf5b DD |
3295 | |
3296 | If PREFIX is non-zero, then this type is being used as a prefix in | |
3297 | a qualified name. In this case, for the standard substitutions, we | |
3298 | need to check whether we are being used as a prefix for a | |
3299 | constructor or destructor, and return a full template name. | |
3300 | Otherwise we will get something like std::iostream::~iostream() | |
3301 | which does not correspond particularly well to any function which | |
3302 | actually appears in the source. | |
d00edca5 | 3303 | */ |
eb383413 | 3304 | |
97ceaf5b DD |
3305 | static const struct d_standard_sub_info standard_subs[] = |
3306 | { | |
b6fb00c0 DD |
3307 | { 't', NL ("std"), |
3308 | NL ("std"), | |
3309 | NULL, 0 }, | |
3310 | { 'a', NL ("std::allocator"), | |
3311 | NL ("std::allocator"), | |
3312 | NL ("allocator") }, | |
3313 | { 'b', NL ("std::basic_string"), | |
3314 | NL ("std::basic_string"), | |
3315 | NL ("basic_string") }, | |
3316 | { 's', NL ("std::string"), | |
3317 | NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"), | |
3318 | NL ("basic_string") }, | |
3319 | { 'i', NL ("std::istream"), | |
3320 | NL ("std::basic_istream<char, std::char_traits<char> >"), | |
3321 | NL ("basic_istream") }, | |
3322 | { 'o', NL ("std::ostream"), | |
3323 | NL ("std::basic_ostream<char, std::char_traits<char> >"), | |
3324 | NL ("basic_ostream") }, | |
3325 | { 'd', NL ("std::iostream"), | |
3326 | NL ("std::basic_iostream<char, std::char_traits<char> >"), | |
3327 | NL ("basic_iostream") } | |
97ceaf5b DD |
3328 | }; |
3329 | ||
59727473 | 3330 | static struct demangle_component * |
9334f9c6 | 3331 | d_substitution (struct d_info *di, int prefix) |
d00edca5 DD |
3332 | { |
3333 | char c; | |
eb383413 | 3334 | |
6ef6358e | 3335 | if (! d_check_char (di, 'S')) |
d00edca5 | 3336 | return NULL; |
e7e9b069 | 3337 | |
d00edca5 | 3338 | c = d_next_char (di); |
858b45cf | 3339 | if (c == '_' || IS_DIGIT (c) || IS_UPPER (c)) |
eb383413 | 3340 | { |
dddc49b7 | 3341 | unsigned int id; |
eb383413 | 3342 | |
d00edca5 DD |
3343 | id = 0; |
3344 | if (c != '_') | |
eb383413 | 3345 | { |
d00edca5 | 3346 | do |
eb383413 | 3347 | { |
dddc49b7 DD |
3348 | unsigned int new_id; |
3349 | ||
d00edca5 | 3350 | if (IS_DIGIT (c)) |
dddc49b7 | 3351 | new_id = id * 36 + c - '0'; |
858b45cf | 3352 | else if (IS_UPPER (c)) |
dddc49b7 | 3353 | new_id = id * 36 + c - 'A' + 10; |
d00edca5 DD |
3354 | else |
3355 | return NULL; | |
dddc49b7 | 3356 | if (new_id < id) |
e63f184e | 3357 | return NULL; |
dddc49b7 | 3358 | id = new_id; |
d00edca5 | 3359 | c = d_next_char (di); |
eb383413 | 3360 | } |
d00edca5 | 3361 | while (c != '_'); |
eb383413 | 3362 | |
d00edca5 | 3363 | ++id; |
eb383413 | 3364 | } |
eb383413 | 3365 | |
dddc49b7 | 3366 | if (id >= (unsigned int) di->next_sub) |
d00edca5 | 3367 | return NULL; |
eb383413 | 3368 | |
b6fb00c0 DD |
3369 | ++di->did_subs; |
3370 | ||
d00edca5 | 3371 | return di->subs[id]; |
eb383413 | 3372 | } |
d00edca5 | 3373 | else |
eb383413 | 3374 | { |
97ceaf5b DD |
3375 | int verbose; |
3376 | const struct d_standard_sub_info *p; | |
3377 | const struct d_standard_sub_info *pend; | |
3378 | ||
3379 | verbose = (di->options & DMGL_VERBOSE) != 0; | |
3380 | if (! verbose && prefix) | |
e61231f1 | 3381 | { |
97ceaf5b DD |
3382 | char peek; |
3383 | ||
3384 | peek = d_peek_char (di); | |
3385 | if (peek == 'C' || peek == 'D') | |
3386 | verbose = 1; | |
eb383413 | 3387 | } |
97ceaf5b DD |
3388 | |
3389 | pend = (&standard_subs[0] | |
3390 | + sizeof standard_subs / sizeof standard_subs[0]); | |
3391 | for (p = &standard_subs[0]; p < pend; ++p) | |
3392 | { | |
3393 | if (c == p->code) | |
3394 | { | |
b6fb00c0 DD |
3395 | const char *s; |
3396 | int len; | |
3397 | ||
97ceaf5b | 3398 | if (p->set_last_name != NULL) |
b6fb00c0 DD |
3399 | di->last_name = d_make_sub (di, p->set_last_name, |
3400 | p->set_last_name_len); | |
97ceaf5b | 3401 | if (verbose) |
b6fb00c0 DD |
3402 | { |
3403 | s = p->full_expansion; | |
3404 | len = p->full_len; | |
3405 | } | |
97ceaf5b | 3406 | else |
b6fb00c0 DD |
3407 | { |
3408 | s = p->simple_expansion; | |
3409 | len = p->simple_len; | |
3410 | } | |
3411 | di->expansion += len; | |
3412 | return d_make_sub (di, s, len); | |
97ceaf5b DD |
3413 | } |
3414 | } | |
3415 | ||
3416 | return NULL; | |
eb383413 | 3417 | } |
eb383413 L |
3418 | } |
3419 | ||
208c1674 | 3420 | /* Initialize a growable string. */ |
eb383413 | 3421 | |
d00edca5 | 3422 | static void |
208c1674 | 3423 | d_growable_string_init (struct d_growable_string *dgs, size_t estimate) |
d00edca5 | 3424 | { |
208c1674 DD |
3425 | dgs->buf = NULL; |
3426 | dgs->len = 0; | |
3427 | dgs->alc = 0; | |
3428 | dgs->allocation_failure = 0; | |
eb383413 | 3429 | |
208c1674 DD |
3430 | if (estimate > 0) |
3431 | d_growable_string_resize (dgs, estimate); | |
3432 | } | |
3433 | ||
3434 | /* Grow a growable string to a given size. */ | |
3435 | ||
3436 | static inline void | |
3437 | d_growable_string_resize (struct d_growable_string *dgs, size_t need) | |
3438 | { | |
3439 | size_t newalc; | |
3440 | char *newbuf; | |
3441 | ||
3442 | if (dgs->allocation_failure) | |
331c3da2 | 3443 | return; |
59666b35 | 3444 | |
208c1674 DD |
3445 | /* Start allocation at two bytes to avoid any possibility of confusion |
3446 | with the special value of 1 used as a return in *palc to indicate | |
3447 | allocation failures. */ | |
3448 | newalc = dgs->alc > 0 ? dgs->alc : 2; | |
3449 | while (newalc < need) | |
3450 | newalc <<= 1; | |
3451 | ||
3452 | newbuf = (char *) realloc (dgs->buf, newalc); | |
3453 | if (newbuf == NULL) | |
3454 | { | |
3455 | free (dgs->buf); | |
3456 | dgs->buf = NULL; | |
3457 | dgs->len = 0; | |
3458 | dgs->alc = 0; | |
3459 | dgs->allocation_failure = 1; | |
3460 | return; | |
eb383413 | 3461 | } |
208c1674 DD |
3462 | dgs->buf = newbuf; |
3463 | dgs->alc = newalc; | |
d00edca5 | 3464 | } |
0976f6a7 | 3465 | |
208c1674 | 3466 | /* Append a buffer to a growable string. */ |
0976f6a7 | 3467 | |
208c1674 DD |
3468 | static inline void |
3469 | d_growable_string_append_buffer (struct d_growable_string *dgs, | |
3470 | const char *s, size_t l) | |
d00edca5 | 3471 | { |
208c1674 | 3472 | size_t need; |
0976f6a7 | 3473 | |
208c1674 DD |
3474 | need = dgs->len + l + 1; |
3475 | if (need > dgs->alc) | |
3476 | d_growable_string_resize (dgs, need); | |
3477 | ||
3478 | if (dgs->allocation_failure) | |
3479 | return; | |
3480 | ||
3481 | memcpy (dgs->buf + dgs->len, s, l); | |
3482 | dgs->buf[dgs->len + l] = '\0'; | |
3483 | dgs->len += l; | |
eb383413 L |
3484 | } |
3485 | ||
208c1674 | 3486 | /* Bridge growable strings to the callback mechanism. */ |
d00edca5 DD |
3487 | |
3488 | static void | |
208c1674 | 3489 | d_growable_string_callback_adapter (const char *s, size_t l, void *opaque) |
eb383413 | 3490 | { |
208c1674 | 3491 | struct d_growable_string *dgs = (struct d_growable_string*) opaque; |
eb383413 | 3492 | |
208c1674 | 3493 | d_growable_string_append_buffer (dgs, s, l); |
eb383413 L |
3494 | } |
3495 | ||
208c1674 | 3496 | /* Initialize a print information structure. */ |
eb383413 | 3497 | |
d00edca5 | 3498 | static void |
ddee5e46 DD |
3499 | d_print_init (struct d_print_info *dpi, demangle_callbackref callback, |
3500 | void *opaque) | |
208c1674 | 3501 | { |
208c1674 DD |
3502 | dpi->len = 0; |
3503 | dpi->last_char = '\0'; | |
3504 | dpi->templates = NULL; | |
3505 | dpi->modifiers = NULL; | |
04aed652 | 3506 | dpi->pack_index = 0; |
3baae9d6 | 3507 | dpi->flush_count = 0; |
208c1674 DD |
3508 | |
3509 | dpi->callback = callback; | |
3510 | dpi->opaque = opaque; | |
3511 | ||
3512 | dpi->demangle_failure = 0; | |
3513 | } | |
3514 | ||
3515 | /* Indicate that an error occurred during printing, and test for error. */ | |
3516 | ||
3517 | static inline void | |
9334f9c6 | 3518 | d_print_error (struct d_print_info *dpi) |
bc9bf259 | 3519 | { |
208c1674 DD |
3520 | dpi->demangle_failure = 1; |
3521 | } | |
3522 | ||
3523 | static inline int | |
3524 | d_print_saw_error (struct d_print_info *dpi) | |
3525 | { | |
3526 | return dpi->demangle_failure != 0; | |
3527 | } | |
3528 | ||
3529 | /* Flush buffered characters to the callback. */ | |
3530 | ||
3531 | static inline void | |
3532 | d_print_flush (struct d_print_info *dpi) | |
3533 | { | |
3534 | dpi->buf[dpi->len] = '\0'; | |
3535 | dpi->callback (dpi->buf, dpi->len, dpi->opaque); | |
3536 | dpi->len = 0; | |
3baae9d6 | 3537 | dpi->flush_count++; |
208c1674 DD |
3538 | } |
3539 | ||
3540 | /* Append characters and buffers for printing. */ | |
3541 | ||
3542 | static inline void | |
3543 | d_append_char (struct d_print_info *dpi, char c) | |
3544 | { | |
3545 | if (dpi->len == sizeof (dpi->buf) - 1) | |
3546 | d_print_flush (dpi); | |
3547 | ||
3548 | dpi->buf[dpi->len++] = c; | |
3549 | dpi->last_char = c; | |
3550 | } | |
3551 | ||
3552 | static inline void | |
3553 | d_append_buffer (struct d_print_info *dpi, const char *s, size_t l) | |
3554 | { | |
3555 | size_t i; | |
3556 | ||
3557 | for (i = 0; i < l; i++) | |
3558 | d_append_char (dpi, s[i]); | |
3559 | } | |
3560 | ||
3561 | static inline void | |
3562 | d_append_string (struct d_print_info *dpi, const char *s) | |
3563 | { | |
3564 | d_append_buffer (dpi, s, strlen (s)); | |
3565 | } | |
3566 | ||
664aa91f DD |
3567 | static inline void |
3568 | d_append_num (struct d_print_info *dpi, long l) | |
3569 | { | |
3570 | char buf[25]; | |
3571 | sprintf (buf,"%ld", l); | |
3572 | d_append_string (dpi, buf); | |
3573 | } | |
3574 | ||
208c1674 DD |
3575 | static inline char |
3576 | d_last_char (struct d_print_info *dpi) | |
3577 | { | |
3578 | return dpi->last_char; | |
3579 | } | |
3580 | ||
3581 | /* Turn components into a human readable string. OPTIONS is the | |
3582 | options bits passed to the demangler. DC is the tree to print. | |
3583 | CALLBACK is a function to call to flush demangled string segments | |
3584 | as they fill the intermediate buffer, and OPAQUE is a generalized | |
3585 | callback argument. On success, this returns 1. On failure, | |
3586 | it returns 0, indicating a bad parse. It does not use heap | |
3587 | memory to build an output string, so cannot encounter memory | |
3588 | allocation failure. */ | |
3589 | ||
3590 | CP_STATIC_IF_GLIBCPP_V3 | |
3591 | int | |
3592 | cplus_demangle_print_callback (int options, | |
3593 | const struct demangle_component *dc, | |
3594 | demangle_callbackref callback, void *opaque) | |
3595 | { | |
3596 | struct d_print_info dpi; | |
3597 | ||
ddee5e46 | 3598 | d_print_init (&dpi, callback, opaque); |
208c1674 | 3599 | |
ddee5e46 | 3600 | d_print_comp (&dpi, options, dc); |
208c1674 DD |
3601 | |
3602 | d_print_flush (&dpi); | |
3603 | ||
3604 | return ! d_print_saw_error (&dpi); | |
d00edca5 | 3605 | } |
bc9bf259 | 3606 | |
b6fb00c0 DD |
3607 | /* Turn components into a human readable string. OPTIONS is the |
3608 | options bits passed to the demangler. DC is the tree to print. | |
3609 | ESTIMATE is a guess at the length of the result. This returns a | |
3610 | string allocated by malloc, or NULL on error. On success, this | |
3611 | sets *PALC to the size of the allocated buffer. On failure, this | |
3612 | sets *PALC to 0 for a bad parse, or to 1 for a memory allocation | |
3613 | failure. */ | |
eb383413 | 3614 | |
59727473 DD |
3615 | CP_STATIC_IF_GLIBCPP_V3 |
3616 | char * | |
9334f9c6 DD |
3617 | cplus_demangle_print (int options, const struct demangle_component *dc, |
3618 | int estimate, size_t *palc) | |
d00edca5 | 3619 | { |
208c1674 | 3620 | struct d_growable_string dgs; |
eb383413 | 3621 | |
208c1674 | 3622 | d_growable_string_init (&dgs, estimate); |
eb383413 | 3623 | |
208c1674 DD |
3624 | if (! cplus_demangle_print_callback (options, dc, |
3625 | d_growable_string_callback_adapter, | |
3626 | &dgs)) | |
eb383413 | 3627 | { |
208c1674 DD |
3628 | free (dgs.buf); |
3629 | *palc = 0; | |
d00edca5 | 3630 | return NULL; |
eb383413 | 3631 | } |
eb383413 | 3632 | |
208c1674 DD |
3633 | *palc = dgs.allocation_failure ? 1 : dgs.alc; |
3634 | return dgs.buf; | |
eb383413 L |
3635 | } |
3636 | ||
1c08f2c8 DD |
3637 | /* Returns the I'th element of the template arglist ARGS, or NULL on |
3638 | failure. */ | |
3639 | ||
3640 | static struct demangle_component * | |
3641 | d_index_template_argument (struct demangle_component *args, int i) | |
3642 | { | |
3643 | struct demangle_component *a; | |
3644 | ||
3645 | for (a = args; | |
3646 | a != NULL; | |
3647 | a = d_right (a)) | |
3648 | { | |
3649 | if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) | |
3650 | return NULL; | |
3651 | if (i <= 0) | |
3652 | break; | |
3653 | --i; | |
3654 | } | |
3655 | if (i != 0 || a == NULL) | |
3656 | return NULL; | |
3657 | ||
3658 | return d_left (a); | |
3659 | } | |
3660 | ||
3661 | /* Returns the template argument from the current context indicated by DC, | |
3662 | which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */ | |
3663 | ||
3664 | static struct demangle_component * | |
3665 | d_lookup_template_argument (struct d_print_info *dpi, | |
3666 | const struct demangle_component *dc) | |
3667 | { | |
3668 | if (dpi->templates == NULL) | |
3669 | { | |
3670 | d_print_error (dpi); | |
3671 | return NULL; | |
3672 | } | |
3673 | ||
3674 | return d_index_template_argument | |
3675 | (d_right (dpi->templates->template_decl), | |
3676 | dc->u.s_number.number); | |
3677 | } | |
3678 | ||
3679 | /* Returns a template argument pack used in DC (any will do), or NULL. */ | |
3680 | ||
3681 | static struct demangle_component * | |
3682 | d_find_pack (struct d_print_info *dpi, | |
3683 | const struct demangle_component *dc) | |
3684 | { | |
3685 | struct demangle_component *a; | |
3686 | if (dc == NULL) | |
3687 | return NULL; | |
3688 | ||
3689 | switch (dc->type) | |
3690 | { | |
3691 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: | |
3692 | a = d_lookup_template_argument (dpi, dc); | |
3693 | if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) | |
3694 | return a; | |
3695 | return NULL; | |
3696 | ||
3697 | case DEMANGLE_COMPONENT_PACK_EXPANSION: | |
3698 | return NULL; | |
3699 | ||
57cf60a5 | 3700 | case DEMANGLE_COMPONENT_LAMBDA: |
1c08f2c8 DD |
3701 | case DEMANGLE_COMPONENT_NAME: |
3702 | case DEMANGLE_COMPONENT_OPERATOR: | |
3703 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: | |
3704 | case DEMANGLE_COMPONENT_SUB_STD: | |
3705 | case DEMANGLE_COMPONENT_CHARACTER: | |
e2e1864d | 3706 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: |
1c08f2c8 DD |
3707 | return NULL; |
3708 | ||
3709 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
3710 | return d_find_pack (dpi, dc->u.s_extended_operator.name); | |
3711 | case DEMANGLE_COMPONENT_CTOR: | |
3712 | return d_find_pack (dpi, dc->u.s_ctor.name); | |
3713 | case DEMANGLE_COMPONENT_DTOR: | |
3714 | return d_find_pack (dpi, dc->u.s_dtor.name); | |
3715 | ||
3716 | default: | |
3717 | a = d_find_pack (dpi, d_left (dc)); | |
3718 | if (a) | |
3719 | return a; | |
3720 | return d_find_pack (dpi, d_right (dc)); | |
3721 | } | |
3722 | } | |
3723 | ||
3724 | /* Returns the length of the template argument pack DC. */ | |
3725 | ||
3726 | static int | |
3727 | d_pack_length (const struct demangle_component *dc) | |
3728 | { | |
3729 | int count = 0; | |
3730 | while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST | |
3731 | && d_left (dc) != NULL) | |
3732 | { | |
3733 | ++count; | |
3734 | dc = d_right (dc); | |
3735 | } | |
3736 | return count; | |
3737 | } | |
3738 | ||
3739 | /* DC is a component of a mangled expression. Print it, wrapped in parens | |
3740 | if needed. */ | |
3741 | ||
3742 | static void | |
ddee5e46 | 3743 | d_print_subexpr (struct d_print_info *dpi, int options, |
1c08f2c8 DD |
3744 | const struct demangle_component *dc) |
3745 | { | |
3746 | int simple = 0; | |
e2e1864d | 3747 | if (dc->type == DEMANGLE_COMPONENT_NAME |
eb7b5ddb DD |
3748 | || dc->type == DEMANGLE_COMPONENT_QUAL_NAME |
3749 | || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST | |
e2e1864d | 3750 | || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM) |
1c08f2c8 DD |
3751 | simple = 1; |
3752 | if (!simple) | |
3753 | d_append_char (dpi, '('); | |
ddee5e46 | 3754 | d_print_comp (dpi, options, dc); |
1c08f2c8 DD |
3755 | if (!simple) |
3756 | d_append_char (dpi, ')'); | |
3757 | } | |
3758 | ||
d00edca5 | 3759 | /* Subroutine to handle components. */ |
eb383413 | 3760 | |
d00edca5 | 3761 | static void |
ddee5e46 | 3762 | d_print_comp (struct d_print_info *dpi, int options, |
9334f9c6 | 3763 | const struct demangle_component *dc) |
eb383413 | 3764 | { |
b24539b3 DD |
3765 | /* Magic variable to let reference smashing skip over the next modifier |
3766 | without needing to modify *dc. */ | |
3767 | const struct demangle_component *mod_inner = NULL; | |
3768 | ||
d00edca5 | 3769 | if (dc == NULL) |
eb383413 | 3770 | { |
d00edca5 DD |
3771 | d_print_error (dpi); |
3772 | return; | |
eb383413 | 3773 | } |
d00edca5 DD |
3774 | if (d_print_saw_error (dpi)) |
3775 | return; | |
eb383413 | 3776 | |
d00edca5 | 3777 | switch (dc->type) |
eb383413 | 3778 | { |
59727473 | 3779 | case DEMANGLE_COMPONENT_NAME: |
ddee5e46 | 3780 | if ((options & DMGL_JAVA) == 0) |
b6fb00c0 DD |
3781 | d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len); |
3782 | else | |
3783 | d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len); | |
d00edca5 | 3784 | return; |
eb383413 | 3785 | |
59727473 DD |
3786 | case DEMANGLE_COMPONENT_QUAL_NAME: |
3787 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
ddee5e46 DD |
3788 | d_print_comp (dpi, options, d_left (dc)); |
3789 | if ((options & DMGL_JAVA) == 0) | |
208c1674 | 3790 | d_append_string (dpi, "::"); |
b6fb00c0 DD |
3791 | else |
3792 | d_append_char (dpi, '.'); | |
ddee5e46 | 3793 | d_print_comp (dpi, options, d_right (dc)); |
d00edca5 | 3794 | return; |
eb383413 | 3795 | |
59727473 | 3796 | case DEMANGLE_COMPONENT_TYPED_NAME: |
d00edca5 | 3797 | { |
858b45cf | 3798 | struct d_print_mod *hold_modifiers; |
59727473 | 3799 | struct demangle_component *typed_name; |
858b45cf DD |
3800 | struct d_print_mod adpm[4]; |
3801 | unsigned int i; | |
d00edca5 DD |
3802 | struct d_print_template dpt; |
3803 | ||
3804 | /* Pass the name down to the type so that it can be printed in | |
858b45cf DD |
3805 | the right place for the type. We also have to pass down |
3806 | any CV-qualifiers, which apply to the this parameter. */ | |
3807 | hold_modifiers = dpi->modifiers; | |
c743cf5d | 3808 | dpi->modifiers = 0; |
858b45cf | 3809 | i = 0; |
d00edca5 | 3810 | typed_name = d_left (dc); |
858b45cf DD |
3811 | while (typed_name != NULL) |
3812 | { | |
3813 | if (i >= sizeof adpm / sizeof adpm[0]) | |
3814 | { | |
3815 | d_print_error (dpi); | |
3816 | return; | |
3817 | } | |
d00edca5 | 3818 | |
858b45cf DD |
3819 | adpm[i].next = dpi->modifiers; |
3820 | dpi->modifiers = &adpm[i]; | |
3821 | adpm[i].mod = typed_name; | |
3822 | adpm[i].printed = 0; | |
3823 | adpm[i].templates = dpi->templates; | |
3824 | ++i; | |
3825 | ||
59727473 DD |
3826 | if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS |
3827 | && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS | |
3828 | && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS) | |
858b45cf DD |
3829 | break; |
3830 | ||
3831 | typed_name = d_left (typed_name); | |
3832 | } | |
d00edca5 | 3833 | |
168b8298 MS |
3834 | if (typed_name == NULL) |
3835 | { | |
3836 | d_print_error (dpi); | |
3837 | return; | |
3838 | } | |
3839 | ||
d00edca5 DD |
3840 | /* If typed_name is a template, then it applies to the |
3841 | function type as well. */ | |
59727473 | 3842 | if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) |
d00edca5 DD |
3843 | { |
3844 | dpt.next = dpi->templates; | |
3845 | dpi->templates = &dpt; | |
abf6a75b | 3846 | dpt.template_decl = typed_name; |
d00edca5 | 3847 | } |
eb383413 | 3848 | |
59727473 DD |
3849 | /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then |
3850 | there may be CV-qualifiers on its right argument which | |
3851 | really apply here; this happens when parsing a class which | |
3852 | is local to a function. */ | |
3853 | if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME) | |
d4edd112 | 3854 | { |
59727473 | 3855 | struct demangle_component *local_name; |
d4edd112 DD |
3856 | |
3857 | local_name = d_right (typed_name); | |
664aa91f DD |
3858 | if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG) |
3859 | local_name = local_name->u.s_unary_num.sub; | |
59727473 DD |
3860 | while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
3861 | || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS | |
3862 | || local_name->type == DEMANGLE_COMPONENT_CONST_THIS) | |
d4edd112 DD |
3863 | { |
3864 | if (i >= sizeof adpm / sizeof adpm[0]) | |
3865 | { | |
3866 | d_print_error (dpi); | |
3867 | return; | |
3868 | } | |
3869 | ||
3870 | adpm[i] = adpm[i - 1]; | |
3871 | adpm[i].next = &adpm[i - 1]; | |
3872 | dpi->modifiers = &adpm[i]; | |
3873 | ||
3874 | adpm[i - 1].mod = local_name; | |
3875 | adpm[i - 1].printed = 0; | |
3876 | adpm[i - 1].templates = dpi->templates; | |
3877 | ++i; | |
3878 | ||
3879 | local_name = d_left (local_name); | |
3880 | } | |
3881 | } | |
3882 | ||
ddee5e46 | 3883 | d_print_comp (dpi, options, d_right (dc)); |
74bcd529 | 3884 | |
59727473 | 3885 | if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) |
d00edca5 | 3886 | dpi->templates = dpt.next; |
eb383413 | 3887 | |
858b45cf | 3888 | /* If the modifiers didn't get printed by the type, print them |
d00edca5 | 3889 | now. */ |
858b45cf | 3890 | while (i > 0) |
d00edca5 | 3891 | { |
858b45cf DD |
3892 | --i; |
3893 | if (! adpm[i].printed) | |
3894 | { | |
3895 | d_append_char (dpi, ' '); | |
ddee5e46 | 3896 | d_print_mod (dpi, options, adpm[i].mod); |
858b45cf | 3897 | } |
d00edca5 | 3898 | } |
eb383413 | 3899 | |
858b45cf | 3900 | dpi->modifiers = hold_modifiers; |
eb383413 | 3901 | |
d00edca5 DD |
3902 | return; |
3903 | } | |
eb383413 | 3904 | |
59727473 | 3905 | case DEMANGLE_COMPONENT_TEMPLATE: |
331c3da2 DD |
3906 | { |
3907 | struct d_print_mod *hold_dpm; | |
208c1674 | 3908 | struct demangle_component *dcl; |
331c3da2 DD |
3909 | |
3910 | /* Don't push modifiers into a template definition. Doing so | |
3911 | could give the wrong definition for a template argument. | |
3912 | Instead, treat the template essentially as a name. */ | |
3913 | ||
3914 | hold_dpm = dpi->modifiers; | |
3915 | dpi->modifiers = NULL; | |
3916 | ||
208c1674 DD |
3917 | dcl = d_left (dc); |
3918 | ||
ddee5e46 | 3919 | if ((options & DMGL_JAVA) != 0 |
208c1674 DD |
3920 | && dcl->type == DEMANGLE_COMPONENT_NAME |
3921 | && dcl->u.s_name.len == 6 | |
3922 | && strncmp (dcl->u.s_name.s, "JArray", 6) == 0) | |
3923 | { | |
3924 | /* Special-case Java arrays, so that JArray<TYPE> appears | |
3925 | instead as TYPE[]. */ | |
3926 | ||
ddee5e46 | 3927 | d_print_comp (dpi, options, d_right (dc)); |
208c1674 DD |
3928 | d_append_string (dpi, "[]"); |
3929 | } | |
3930 | else | |
3931 | { | |
ddee5e46 | 3932 | d_print_comp (dpi, options, dcl); |
208c1674 DD |
3933 | if (d_last_char (dpi) == '<') |
3934 | d_append_char (dpi, ' '); | |
3935 | d_append_char (dpi, '<'); | |
ddee5e46 | 3936 | d_print_comp (dpi, options, d_right (dc)); |
208c1674 DD |
3937 | /* Avoid generating two consecutive '>' characters, to avoid |
3938 | the C++ syntactic ambiguity. */ | |
3939 | if (d_last_char (dpi) == '>') | |
3940 | d_append_char (dpi, ' '); | |
3941 | d_append_char (dpi, '>'); | |
3942 | } | |
331c3da2 DD |
3943 | |
3944 | dpi->modifiers = hold_dpm; | |
3945 | ||
3946 | return; | |
3947 | } | |
d00edca5 | 3948 | |
59727473 | 3949 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: |
d00edca5 | 3950 | { |
d00edca5 | 3951 | struct d_print_template *hold_dpt; |
1c08f2c8 | 3952 | struct demangle_component *a = d_lookup_template_argument (dpi, dc); |
eb383413 | 3953 | |
1c08f2c8 DD |
3954 | if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) |
3955 | a = d_index_template_argument (a, dpi->pack_index); | |
3956 | ||
3957 | if (a == NULL) | |
d00edca5 DD |
3958 | { |
3959 | d_print_error (dpi); | |
3960 | return; | |
3961 | } | |
59666b35 | 3962 | |
d00edca5 DD |
3963 | /* While processing this parameter, we need to pop the list of |
3964 | templates. This is because the template parameter may | |
3965 | itself be a reference to a parameter of an outer | |
3966 | template. */ | |
59666b35 | 3967 | |
d00edca5 DD |
3968 | hold_dpt = dpi->templates; |
3969 | dpi->templates = hold_dpt->next; | |
eb383413 | 3970 | |
ddee5e46 | 3971 | d_print_comp (dpi, options, a); |
03d5f569 | 3972 | |
d00edca5 | 3973 | dpi->templates = hold_dpt; |
59666b35 | 3974 | |
d00edca5 DD |
3975 | return; |
3976 | } | |
eb383413 | 3977 | |
59727473 | 3978 | case DEMANGLE_COMPONENT_CTOR: |
ddee5e46 | 3979 | d_print_comp (dpi, options, dc->u.s_ctor.name); |
d00edca5 DD |
3980 | return; |
3981 | ||
59727473 | 3982 | case DEMANGLE_COMPONENT_DTOR: |
d00edca5 | 3983 | d_append_char (dpi, '~'); |
ddee5e46 | 3984 | d_print_comp (dpi, options, dc->u.s_dtor.name); |
d00edca5 DD |
3985 | return; |
3986 | ||
59727473 | 3987 | case DEMANGLE_COMPONENT_VTABLE: |
208c1674 | 3988 | d_append_string (dpi, "vtable for "); |
ddee5e46 | 3989 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
3990 | return; |
3991 | ||
59727473 | 3992 | case DEMANGLE_COMPONENT_VTT: |
208c1674 | 3993 | d_append_string (dpi, "VTT for "); |
ddee5e46 | 3994 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
3995 | return; |
3996 | ||
59727473 | 3997 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
208c1674 | 3998 | d_append_string (dpi, "construction vtable for "); |
ddee5e46 | 3999 | d_print_comp (dpi, options, d_left (dc)); |
208c1674 | 4000 | d_append_string (dpi, "-in-"); |
ddee5e46 | 4001 | d_print_comp (dpi, options, d_right (dc)); |
d00edca5 DD |
4002 | return; |
4003 | ||
59727473 | 4004 | case DEMANGLE_COMPONENT_TYPEINFO: |
208c1674 | 4005 | d_append_string (dpi, "typeinfo for "); |
ddee5e46 | 4006 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4007 | return; |
4008 | ||
59727473 | 4009 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: |
208c1674 | 4010 | d_append_string (dpi, "typeinfo name for "); |
ddee5e46 | 4011 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4012 | return; |
4013 | ||
59727473 | 4014 | case DEMANGLE_COMPONENT_TYPEINFO_FN: |
208c1674 | 4015 | d_append_string (dpi, "typeinfo fn for "); |
ddee5e46 | 4016 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4017 | return; |
4018 | ||
59727473 | 4019 | case DEMANGLE_COMPONENT_THUNK: |
208c1674 | 4020 | d_append_string (dpi, "non-virtual thunk to "); |
ddee5e46 | 4021 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4022 | return; |
4023 | ||
59727473 | 4024 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: |
208c1674 | 4025 | d_append_string (dpi, "virtual thunk to "); |
ddee5e46 | 4026 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4027 | return; |
4028 | ||
59727473 | 4029 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: |
208c1674 | 4030 | d_append_string (dpi, "covariant return thunk to "); |
ddee5e46 | 4031 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4032 | return; |
4033 | ||
59727473 | 4034 | case DEMANGLE_COMPONENT_JAVA_CLASS: |
208c1674 | 4035 | d_append_string (dpi, "java Class for "); |
ddee5e46 | 4036 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4037 | return; |
4038 | ||
59727473 | 4039 | case DEMANGLE_COMPONENT_GUARD: |
208c1674 | 4040 | d_append_string (dpi, "guard variable for "); |
ddee5e46 | 4041 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4042 | return; |
4043 | ||
59727473 | 4044 | case DEMANGLE_COMPONENT_REFTEMP: |
abc6552b DD |
4045 | d_append_string (dpi, "reference temporary #"); |
4046 | d_print_comp (dpi, options, d_right (dc)); | |
4047 | d_append_string (dpi, " for "); | |
ddee5e46 | 4048 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4049 | return; |
4050 | ||
839e4798 | 4051 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
208c1674 | 4052 | d_append_string (dpi, "hidden alias for "); |
ddee5e46 | 4053 | d_print_comp (dpi, options, d_left (dc)); |
839e4798 RH |
4054 | return; |
4055 | ||
956a8f8b DD |
4056 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
4057 | d_append_string (dpi, "transaction clone for "); | |
4058 | d_print_comp (dpi, options, d_left (dc)); | |
4059 | return; | |
4060 | ||
4061 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: | |
4062 | d_append_string (dpi, "non-transaction clone for "); | |
4063 | d_print_comp (dpi, options, d_left (dc)); | |
4064 | return; | |
4065 | ||
59727473 | 4066 | case DEMANGLE_COMPONENT_SUB_STD: |
b6fb00c0 | 4067 | d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len); |
d00edca5 DD |
4068 | return; |
4069 | ||
59727473 DD |
4070 | case DEMANGLE_COMPONENT_RESTRICT: |
4071 | case DEMANGLE_COMPONENT_VOLATILE: | |
4072 | case DEMANGLE_COMPONENT_CONST: | |
74aee4eb DD |
4073 | { |
4074 | struct d_print_mod *pdpm; | |
4075 | ||
4076 | /* When printing arrays, it's possible to have cases where the | |
4077 | same CV-qualifier gets pushed on the stack multiple times. | |
4078 | We only need to print it once. */ | |
4079 | ||
4080 | for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next) | |
4081 | { | |
4082 | if (! pdpm->printed) | |
4083 | { | |
4084 | if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT | |
4085 | && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE | |
4086 | && pdpm->mod->type != DEMANGLE_COMPONENT_CONST) | |
4087 | break; | |
4088 | if (pdpm->mod->type == dc->type) | |
4089 | { | |
ddee5e46 | 4090 | d_print_comp (dpi, options, d_left (dc)); |
74aee4eb DD |
4091 | return; |
4092 | } | |
4093 | } | |
4094 | } | |
4095 | } | |
b24539b3 DD |
4096 | goto modifier; |
4097 | ||
4098 | case DEMANGLE_COMPONENT_REFERENCE: | |
4099 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: | |
4100 | { | |
4101 | /* Handle reference smashing: & + && = &. */ | |
4102 | const struct demangle_component *sub = d_left (dc); | |
4103 | if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM) | |
4104 | { | |
4105 | struct demangle_component *a = d_lookup_template_argument (dpi, sub); | |
4106 | if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) | |
4107 | a = d_index_template_argument (a, dpi->pack_index); | |
04aed652 DD |
4108 | |
4109 | if (a == NULL) | |
4110 | { | |
4111 | d_print_error (dpi); | |
4112 | return; | |
4113 | } | |
4114 | ||
b24539b3 DD |
4115 | sub = a; |
4116 | } | |
4117 | ||
4118 | if (sub->type == DEMANGLE_COMPONENT_REFERENCE | |
4119 | || sub->type == dc->type) | |
4120 | dc = sub; | |
4121 | else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE) | |
4122 | mod_inner = d_left (sub); | |
4123 | } | |
74aee4eb | 4124 | /* Fall through. */ |
b24539b3 | 4125 | |
59727473 DD |
4126 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
4127 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
4128 | case DEMANGLE_COMPONENT_CONST_THIS: | |
4129 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
4130 | case DEMANGLE_COMPONENT_POINTER: | |
59727473 DD |
4131 | case DEMANGLE_COMPONENT_COMPLEX: |
4132 | case DEMANGLE_COMPONENT_IMAGINARY: | |
b24539b3 | 4133 | modifier: |
d00edca5 DD |
4134 | { |
4135 | /* We keep a list of modifiers on the stack. */ | |
4136 | struct d_print_mod dpm; | |
eb383413 | 4137 | |
d00edca5 DD |
4138 | dpm.next = dpi->modifiers; |
4139 | dpi->modifiers = &dpm; | |
4140 | dpm.mod = dc; | |
4141 | dpm.printed = 0; | |
331c3da2 | 4142 | dpm.templates = dpi->templates; |
eb383413 | 4143 | |
b24539b3 DD |
4144 | if (!mod_inner) |
4145 | mod_inner = d_left (dc); | |
4146 | ||
4147 | d_print_comp (dpi, options, mod_inner); | |
59666b35 | 4148 | |
d00edca5 DD |
4149 | /* If the modifier didn't get printed by the type, print it |
4150 | now. */ | |
4151 | if (! dpm.printed) | |
ddee5e46 | 4152 | d_print_mod (dpi, options, dc); |
eb383413 | 4153 | |
d00edca5 | 4154 | dpi->modifiers = dpm.next; |
eb383413 | 4155 | |
d00edca5 DD |
4156 | return; |
4157 | } | |
eb383413 | 4158 | |
59727473 | 4159 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: |
ddee5e46 | 4160 | if ((options & DMGL_JAVA) == 0) |
b6fb00c0 DD |
4161 | d_append_buffer (dpi, dc->u.s_builtin.type->name, |
4162 | dc->u.s_builtin.type->len); | |
d00edca5 | 4163 | else |
b6fb00c0 DD |
4164 | d_append_buffer (dpi, dc->u.s_builtin.type->java_name, |
4165 | dc->u.s_builtin.type->java_len); | |
d00edca5 | 4166 | return; |
eb383413 | 4167 | |
59727473 | 4168 | case DEMANGLE_COMPONENT_VENDOR_TYPE: |
ddee5e46 | 4169 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 | 4170 | return; |
eb383413 | 4171 | |
59727473 | 4172 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
d00edca5 | 4173 | { |
ddee5e46 DD |
4174 | if ((options & DMGL_RET_POSTFIX) != 0) |
4175 | d_print_function_type (dpi, | |
4176 | options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), | |
4177 | dc, dpi->modifiers); | |
7887b2ce DD |
4178 | |
4179 | /* Print return type if present */ | |
ddee5e46 DD |
4180 | if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0) |
4181 | d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), | |
4182 | d_left (dc)); | |
4183 | else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0) | |
d00edca5 DD |
4184 | { |
4185 | struct d_print_mod dpm; | |
eb383413 | 4186 | |
d00edca5 DD |
4187 | /* We must pass this type down as a modifier in order to |
4188 | print it in the right location. */ | |
d00edca5 DD |
4189 | dpm.next = dpi->modifiers; |
4190 | dpi->modifiers = &dpm; | |
4191 | dpm.mod = dc; | |
4192 | dpm.printed = 0; | |
331c3da2 | 4193 | dpm.templates = dpi->templates; |
eb383413 | 4194 | |
ddee5e46 DD |
4195 | d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), |
4196 | d_left (dc)); | |
eb383413 | 4197 | |
d00edca5 | 4198 | dpi->modifiers = dpm.next; |
eb383413 | 4199 | |
d00edca5 DD |
4200 | if (dpm.printed) |
4201 | return; | |
eb383413 | 4202 | |
7887b2ce DD |
4203 | /* In standard prefix notation, there is a space between the |
4204 | return type and the function signature. */ | |
ddee5e46 | 4205 | if ((options & DMGL_RET_POSTFIX) == 0) |
7887b2ce | 4206 | d_append_char (dpi, ' '); |
d00edca5 | 4207 | } |
eb383413 | 4208 | |
ddee5e46 DD |
4209 | if ((options & DMGL_RET_POSTFIX) == 0) |
4210 | d_print_function_type (dpi, | |
4211 | options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), | |
4212 | dc, dpi->modifiers); | |
03d5f569 | 4213 | |
d00edca5 DD |
4214 | return; |
4215 | } | |
eb383413 | 4216 | |
59727473 | 4217 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
d00edca5 | 4218 | { |
74aee4eb DD |
4219 | struct d_print_mod *hold_modifiers; |
4220 | struct d_print_mod adpm[4]; | |
4221 | unsigned int i; | |
4222 | struct d_print_mod *pdpm; | |
eb383413 | 4223 | |
d00edca5 | 4224 | /* We must pass this type down as a modifier in order to print |
74aee4eb DD |
4225 | multi-dimensional arrays correctly. If the array itself is |
4226 | CV-qualified, we act as though the element type were | |
4227 | CV-qualified. We do this by copying the modifiers down | |
4228 | rather than fiddling pointers, so that we don't wind up | |
4229 | with a d_print_mod higher on the stack pointing into our | |
4230 | stack frame after we return. */ | |
03d5f569 | 4231 | |
74aee4eb DD |
4232 | hold_modifiers = dpi->modifiers; |
4233 | ||
4234 | adpm[0].next = hold_modifiers; | |
4235 | dpi->modifiers = &adpm[0]; | |
4236 | adpm[0].mod = dc; | |
4237 | adpm[0].printed = 0; | |
4238 | adpm[0].templates = dpi->templates; | |
4239 | ||
4240 | i = 1; | |
4241 | pdpm = hold_modifiers; | |
4242 | while (pdpm != NULL | |
4243 | && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT | |
4244 | || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE | |
4245 | || pdpm->mod->type == DEMANGLE_COMPONENT_CONST)) | |
4246 | { | |
4247 | if (! pdpm->printed) | |
4248 | { | |
4249 | if (i >= sizeof adpm / sizeof adpm[0]) | |
4250 | { | |
4251 | d_print_error (dpi); | |
4252 | return; | |
4253 | } | |
4254 | ||
4255 | adpm[i] = *pdpm; | |
4256 | adpm[i].next = dpi->modifiers; | |
4257 | dpi->modifiers = &adpm[i]; | |
4258 | pdpm->printed = 1; | |
4259 | ++i; | |
4260 | } | |
4261 | ||
4262 | pdpm = pdpm->next; | |
4263 | } | |
eb383413 | 4264 | |
ddee5e46 | 4265 | d_print_comp (dpi, options, d_right (dc)); |
eb383413 | 4266 | |
74aee4eb | 4267 | dpi->modifiers = hold_modifiers; |
eb383413 | 4268 | |
74aee4eb | 4269 | if (adpm[0].printed) |
d00edca5 | 4270 | return; |
eb383413 | 4271 | |
74aee4eb DD |
4272 | while (i > 1) |
4273 | { | |
4274 | --i; | |
ddee5e46 | 4275 | d_print_mod (dpi, options, adpm[i].mod); |
74aee4eb DD |
4276 | } |
4277 | ||
ddee5e46 | 4278 | d_print_array_type (dpi, options, dc, dpi->modifiers); |
eb383413 | 4279 | |
d00edca5 DD |
4280 | return; |
4281 | } | |
eb383413 | 4282 | |
59727473 | 4283 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
cbc43128 | 4284 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
d00edca5 | 4285 | { |
d00edca5 DD |
4286 | struct d_print_mod dpm; |
4287 | ||
d00edca5 DD |
4288 | dpm.next = dpi->modifiers; |
4289 | dpi->modifiers = &dpm; | |
4290 | dpm.mod = dc; | |
4291 | dpm.printed = 0; | |
331c3da2 | 4292 | dpm.templates = dpi->templates; |
d00edca5 | 4293 | |
ddee5e46 | 4294 | d_print_comp (dpi, options, d_right (dc)); |
d00edca5 DD |
4295 | |
4296 | /* If the modifier didn't get printed by the type, print it | |
4297 | now. */ | |
4298 | if (! dpm.printed) | |
ddee5e46 | 4299 | d_print_mod (dpi, options, dc); |
eb383413 | 4300 | |
d00edca5 | 4301 | dpi->modifiers = dpm.next; |
eb383413 | 4302 | |
d00edca5 DD |
4303 | return; |
4304 | } | |
eb383413 | 4305 | |
d2825c1a DD |
4306 | case DEMANGLE_COMPONENT_FIXED_TYPE: |
4307 | if (dc->u.s_fixed.sat) | |
4308 | d_append_string (dpi, "_Sat "); | |
4309 | /* Don't print "int _Accum". */ | |
4310 | if (dc->u.s_fixed.length->u.s_builtin.type | |
4311 | != &cplus_demangle_builtin_types['i'-'a']) | |
4312 | { | |
ddee5e46 | 4313 | d_print_comp (dpi, options, dc->u.s_fixed.length); |
d2825c1a DD |
4314 | d_append_char (dpi, ' '); |
4315 | } | |
4316 | if (dc->u.s_fixed.accum) | |
4317 | d_append_string (dpi, "_Accum"); | |
4318 | else | |
4319 | d_append_string (dpi, "_Fract"); | |
4320 | return; | |
4321 | ||
59727473 DD |
4322 | case DEMANGLE_COMPONENT_ARGLIST: |
4323 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: | |
1c08f2c8 | 4324 | if (d_left (dc) != NULL) |
ddee5e46 | 4325 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
4326 | if (d_right (dc) != NULL) |
4327 | { | |
4e59450e | 4328 | size_t len; |
3baae9d6 JJ |
4329 | unsigned long int flush_count; |
4330 | /* Make sure ", " isn't flushed by d_append_string, otherwise | |
4331 | dpi->len -= 2 wouldn't work. */ | |
4332 | if (dpi->len >= sizeof (dpi->buf) - 2) | |
4333 | d_print_flush (dpi); | |
208c1674 | 4334 | d_append_string (dpi, ", "); |
4e59450e | 4335 | len = dpi->len; |
3baae9d6 | 4336 | flush_count = dpi->flush_count; |
ddee5e46 | 4337 | d_print_comp (dpi, options, d_right (dc)); |
4e59450e DD |
4338 | /* If that didn't print anything (which can happen with empty |
4339 | template argument packs), remove the comma and space. */ | |
3baae9d6 | 4340 | if (dpi->flush_count == flush_count && dpi->len == len) |
4e59450e | 4341 | dpi->len -= 2; |
d00edca5 DD |
4342 | } |
4343 | return; | |
eb383413 | 4344 | |
eb7b5ddb DD |
4345 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
4346 | { | |
4347 | struct demangle_component *type = d_left (dc); | |
4348 | struct demangle_component *list = d_right (dc); | |
4349 | ||
4350 | if (type) | |
4351 | d_print_comp (dpi, options, type); | |
4352 | d_append_char (dpi, '{'); | |
4353 | d_print_comp (dpi, options, list); | |
4354 | d_append_char (dpi, '}'); | |
4355 | } | |
4356 | return; | |
4357 | ||
59727473 | 4358 | case DEMANGLE_COMPONENT_OPERATOR: |
d00edca5 DD |
4359 | { |
4360 | char c; | |
4361 | ||
208c1674 | 4362 | d_append_string (dpi, "operator"); |
d00edca5 | 4363 | c = dc->u.s_operator.op->name[0]; |
858b45cf | 4364 | if (IS_LOWER (c)) |
d00edca5 | 4365 | d_append_char (dpi, ' '); |
b6fb00c0 DD |
4366 | d_append_buffer (dpi, dc->u.s_operator.op->name, |
4367 | dc->u.s_operator.op->len); | |
d00edca5 DD |
4368 | return; |
4369 | } | |
eb383413 | 4370 | |
59727473 | 4371 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
208c1674 | 4372 | d_append_string (dpi, "operator "); |
ddee5e46 | 4373 | d_print_comp (dpi, options, dc->u.s_extended_operator.name); |
d00edca5 | 4374 | return; |
eb383413 | 4375 | |
59727473 | 4376 | case DEMANGLE_COMPONENT_CAST: |
208c1674 | 4377 | d_append_string (dpi, "operator "); |
ddee5e46 | 4378 | d_print_cast (dpi, options, dc); |
d00edca5 | 4379 | return; |
eb383413 | 4380 | |
eb7b5ddb DD |
4381 | case DEMANGLE_COMPONENT_NULLARY: |
4382 | d_print_expr_op (dpi, options, d_left (dc)); | |
4383 | return; | |
4384 | ||
59727473 | 4385 | case DEMANGLE_COMPONENT_UNARY: |
eb7b5ddb DD |
4386 | { |
4387 | struct demangle_component *op = d_left (dc); | |
4388 | struct demangle_component *operand = d_right (dc); | |
4389 | const char *code = NULL; | |
02e7efbf | 4390 | |
eb7b5ddb DD |
4391 | if (op->type == DEMANGLE_COMPONENT_OPERATOR) |
4392 | { | |
4393 | code = op->u.s_operator.op->code; | |
4394 | if (!strcmp (code, "ad")) | |
4395 | { | |
4396 | /* Don't print the argument list for the address of a | |
4397 | function. */ | |
4398 | if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME | |
4399 | && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME | |
4400 | && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE) | |
4401 | operand = d_left (operand); | |
4402 | } | |
4403 | if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS) | |
4404 | { | |
4405 | /* This indicates a suffix operator. */ | |
4406 | operand = d_left (operand); | |
4407 | d_print_subexpr (dpi, options, operand); | |
4408 | d_print_expr_op (dpi, options, op); | |
4409 | return; | |
4410 | } | |
4411 | } | |
02e7efbf | 4412 | |
eb7b5ddb DD |
4413 | if (op->type != DEMANGLE_COMPONENT_CAST) |
4414 | d_print_expr_op (dpi, options, op); | |
4415 | else | |
4416 | { | |
4417 | d_append_char (dpi, '('); | |
4418 | d_print_cast (dpi, options, op); | |
4419 | d_append_char (dpi, ')'); | |
4420 | } | |
4421 | if (code && !strcmp (code, "gs")) | |
4422 | /* Avoid parens after '::'. */ | |
4423 | d_print_comp (dpi, options, operand); | |
4424 | else if (code && !strcmp (code, "st")) | |
4425 | /* Always print parens for sizeof (type). */ | |
4426 | { | |
4427 | d_append_char (dpi, '('); | |
4428 | d_print_comp (dpi, options, operand); | |
4429 | d_append_char (dpi, ')'); | |
4430 | } | |
4431 | else | |
4432 | d_print_subexpr (dpi, options, operand); | |
4433 | } | |
d00edca5 DD |
4434 | return; |
4435 | ||
59727473 DD |
4436 | case DEMANGLE_COMPONENT_BINARY: |
4437 | if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS) | |
eb383413 | 4438 | { |
d00edca5 DD |
4439 | d_print_error (dpi); |
4440 | return; | |
eb383413 | 4441 | } |
858b45cf DD |
4442 | |
4443 | /* We wrap an expression which uses the greater-than operator in | |
4444 | an extra layer of parens so that it does not get confused | |
4445 | with the '>' which ends the template parameters. */ | |
59727473 | 4446 | if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR |
b6fb00c0 DD |
4447 | && d_left (dc)->u.s_operator.op->len == 1 |
4448 | && d_left (dc)->u.s_operator.op->name[0] == '>') | |
858b45cf DD |
4449 | d_append_char (dpi, '('); |
4450 | ||
02e7efbf JK |
4451 | if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0 |
4452 | && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
4453 | { | |
4454 | /* Function call used in an expression should not have printed types | |
4455 | of the function arguments. Values of the function arguments still | |
4456 | get printed below. */ | |
4457 | ||
4458 | const struct demangle_component *func = d_left (d_right (dc)); | |
4459 | ||
4460 | if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE) | |
4461 | d_print_error (dpi); | |
4462 | d_print_subexpr (dpi, options, d_left (func)); | |
4463 | } | |
4464 | else | |
4465 | d_print_subexpr (dpi, options, d_left (d_right (dc))); | |
9ac9c2b6 DD |
4466 | if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0) |
4467 | { | |
4468 | d_append_char (dpi, '['); | |
ddee5e46 | 4469 | d_print_comp (dpi, options, d_right (d_right (dc))); |
9ac9c2b6 DD |
4470 | d_append_char (dpi, ']'); |
4471 | } | |
4472 | else | |
4473 | { | |
4474 | if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0) | |
ddee5e46 DD |
4475 | d_print_expr_op (dpi, options, d_left (dc)); |
4476 | d_print_subexpr (dpi, options, d_right (d_right (dc))); | |
9ac9c2b6 | 4477 | } |
858b45cf | 4478 | |
59727473 | 4479 | if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR |
b6fb00c0 DD |
4480 | && d_left (dc)->u.s_operator.op->len == 1 |
4481 | && d_left (dc)->u.s_operator.op->name[0] == '>') | |
858b45cf DD |
4482 | d_append_char (dpi, ')'); |
4483 | ||
d00edca5 DD |
4484 | return; |
4485 | ||
59727473 DD |
4486 | case DEMANGLE_COMPONENT_BINARY_ARGS: |
4487 | /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */ | |
d00edca5 DD |
4488 | d_print_error (dpi); |
4489 | return; | |
4490 | ||
59727473 DD |
4491 | case DEMANGLE_COMPONENT_TRINARY: |
4492 | if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1 | |
4493 | || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2) | |
d00edca5 DD |
4494 | { |
4495 | d_print_error (dpi); | |
4496 | return; | |
4497 | } | |
eb7b5ddb DD |
4498 | { |
4499 | struct demangle_component *op = d_left (dc); | |
4500 | struct demangle_component *first = d_left (d_right (dc)); | |
4501 | struct demangle_component *second = d_left (d_right (d_right (dc))); | |
4502 | struct demangle_component *third = d_right (d_right (d_right (dc))); | |
4503 | ||
4504 | if (!strcmp (op->u.s_operator.op->code, "qu")) | |
4505 | { | |
4506 | d_print_subexpr (dpi, options, first); | |
4507 | d_print_expr_op (dpi, options, op); | |
4508 | d_print_subexpr (dpi, options, second); | |
4509 | d_append_string (dpi, " : "); | |
4510 | d_print_subexpr (dpi, options, third); | |
4511 | } | |
4512 | else | |
4513 | { | |
4514 | d_append_string (dpi, "new "); | |
4515 | if (d_left (first) != NULL) | |
4516 | { | |
4517 | d_print_subexpr (dpi, options, first); | |
4518 | d_append_char (dpi, ' '); | |
4519 | } | |
4520 | d_print_comp (dpi, options, second); | |
4521 | if (third) | |
4522 | d_print_subexpr (dpi, options, third); | |
4523 | } | |
4524 | } | |
d00edca5 DD |
4525 | return; |
4526 | ||
59727473 DD |
4527 | case DEMANGLE_COMPONENT_TRINARY_ARG1: |
4528 | case DEMANGLE_COMPONENT_TRINARY_ARG2: | |
4529 | /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */ | |
d00edca5 DD |
4530 | d_print_error (dpi); |
4531 | return; | |
4532 | ||
59727473 DD |
4533 | case DEMANGLE_COMPONENT_LITERAL: |
4534 | case DEMANGLE_COMPONENT_LITERAL_NEG: | |
2d733211 DD |
4535 | { |
4536 | enum d_builtin_type_print tp; | |
d00edca5 | 4537 | |
2d733211 DD |
4538 | /* For some builtin types, produce simpler output. */ |
4539 | tp = D_PRINT_DEFAULT; | |
4540 | if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE) | |
4541 | { | |
4542 | tp = d_left (dc)->u.s_builtin.type->print; | |
4543 | switch (tp) | |
4544 | { | |
4545 | case D_PRINT_INT: | |
4546 | case D_PRINT_UNSIGNED: | |
4547 | case D_PRINT_LONG: | |
4548 | case D_PRINT_UNSIGNED_LONG: | |
4549 | case D_PRINT_LONG_LONG: | |
4550 | case D_PRINT_UNSIGNED_LONG_LONG: | |
4551 | if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME) | |
4552 | { | |
4553 | if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG) | |
4554 | d_append_char (dpi, '-'); | |
ddee5e46 | 4555 | d_print_comp (dpi, options, d_right (dc)); |
2d733211 DD |
4556 | switch (tp) |
4557 | { | |
4558 | default: | |
4559 | break; | |
4560 | case D_PRINT_UNSIGNED: | |
4561 | d_append_char (dpi, 'u'); | |
4562 | break; | |
4563 | case D_PRINT_LONG: | |
4564 | d_append_char (dpi, 'l'); | |
4565 | break; | |
4566 | case D_PRINT_UNSIGNED_LONG: | |
208c1674 | 4567 | d_append_string (dpi, "ul"); |
2d733211 DD |
4568 | break; |
4569 | case D_PRINT_LONG_LONG: | |
208c1674 | 4570 | d_append_string (dpi, "ll"); |
2d733211 DD |
4571 | break; |
4572 | case D_PRINT_UNSIGNED_LONG_LONG: | |
208c1674 | 4573 | d_append_string (dpi, "ull"); |
2d733211 DD |
4574 | break; |
4575 | } | |
4576 | return; | |
4577 | } | |
4578 | break; | |
eb383413 | 4579 | |
2d733211 DD |
4580 | case D_PRINT_BOOL: |
4581 | if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME | |
4582 | && d_right (dc)->u.s_name.len == 1 | |
4583 | && dc->type == DEMANGLE_COMPONENT_LITERAL) | |
4584 | { | |
4585 | switch (d_right (dc)->u.s_name.s[0]) | |
4586 | { | |
4587 | case '0': | |
208c1674 | 4588 | d_append_string (dpi, "false"); |
2d733211 DD |
4589 | return; |
4590 | case '1': | |
208c1674 | 4591 | d_append_string (dpi, "true"); |
2d733211 DD |
4592 | return; |
4593 | default: | |
4594 | break; | |
4595 | } | |
4596 | } | |
4597 | break; | |
03d5f569 | 4598 | |
2d733211 DD |
4599 | default: |
4600 | break; | |
4601 | } | |
4602 | } | |
eb383413 | 4603 | |
2d733211 | 4604 | d_append_char (dpi, '('); |
ddee5e46 | 4605 | d_print_comp (dpi, options, d_left (dc)); |
2d733211 DD |
4606 | d_append_char (dpi, ')'); |
4607 | if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG) | |
4608 | d_append_char (dpi, '-'); | |
4609 | if (tp == D_PRINT_FLOAT) | |
4610 | d_append_char (dpi, '['); | |
ddee5e46 | 4611 | d_print_comp (dpi, options, d_right (dc)); |
2d733211 DD |
4612 | if (tp == D_PRINT_FLOAT) |
4613 | d_append_char (dpi, ']'); | |
4614 | } | |
d00edca5 | 4615 | return; |
eb383413 | 4616 | |
cbc43128 DD |
4617 | case DEMANGLE_COMPONENT_NUMBER: |
4618 | d_append_num (dpi, dc->u.s_number.number); | |
4619 | return; | |
4620 | ||
830ef634 DD |
4621 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
4622 | d_append_string (dpi, "java resource "); | |
ddee5e46 | 4623 | d_print_comp (dpi, options, d_left (dc)); |
830ef634 DD |
4624 | return; |
4625 | ||
4626 | case DEMANGLE_COMPONENT_COMPOUND_NAME: | |
ddee5e46 DD |
4627 | d_print_comp (dpi, options, d_left (dc)); |
4628 | d_print_comp (dpi, options, d_right (dc)); | |
830ef634 DD |
4629 | return; |
4630 | ||
4631 | case DEMANGLE_COMPONENT_CHARACTER: | |
4632 | d_append_char (dpi, dc->u.s_character.character); | |
4633 | return; | |
4634 | ||
ba8cb4ba DD |
4635 | case DEMANGLE_COMPONENT_DECLTYPE: |
4636 | d_append_string (dpi, "decltype ("); | |
ddee5e46 | 4637 | d_print_comp (dpi, options, d_left (dc)); |
ba8cb4ba DD |
4638 | d_append_char (dpi, ')'); |
4639 | return; | |
4640 | ||
1c08f2c8 DD |
4641 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
4642 | { | |
e2e1864d | 4643 | int len; |
1c08f2c8 | 4644 | int i; |
e2e1864d DD |
4645 | struct demangle_component *a = d_find_pack (dpi, d_left (dc)); |
4646 | if (a == NULL) | |
4647 | { | |
4648 | /* d_find_pack won't find anything if the only packs involved | |
4649 | in this expansion are function parameter packs; in that | |
4650 | case, just print the pattern and "...". */ | |
ddee5e46 | 4651 | d_print_subexpr (dpi, options, d_left (dc)); |
e2e1864d DD |
4652 | d_append_string (dpi, "..."); |
4653 | return; | |
4654 | } | |
1c08f2c8 | 4655 | |
e2e1864d | 4656 | len = d_pack_length (a); |
1c08f2c8 DD |
4657 | dc = d_left (dc); |
4658 | for (i = 0; i < len; ++i) | |
4659 | { | |
4660 | dpi->pack_index = i; | |
ddee5e46 | 4661 | d_print_comp (dpi, options, dc); |
1c08f2c8 DD |
4662 | if (i < len-1) |
4663 | d_append_string (dpi, ", "); | |
4664 | } | |
4665 | } | |
4666 | return; | |
4667 | ||
c743cf5d | 4668 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: |
f2917a30 DD |
4669 | { |
4670 | long num = dc->u.s_number.number; | |
4671 | if (num == 0) | |
4672 | d_append_string (dpi, "this"); | |
4673 | else | |
4674 | { | |
4675 | d_append_string (dpi, "{parm#"); | |
4676 | d_append_num (dpi, num); | |
4677 | d_append_char (dpi, '}'); | |
4678 | } | |
4679 | } | |
664aa91f | 4680 | return; |
c743cf5d | 4681 | |
d5031754 DD |
4682 | case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS: |
4683 | d_append_string (dpi, "global constructors keyed to "); | |
ddee5e46 | 4684 | d_print_comp (dpi, options, dc->u.s_binary.left); |
d5031754 DD |
4685 | return; |
4686 | ||
4687 | case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS: | |
4688 | d_append_string (dpi, "global destructors keyed to "); | |
ddee5e46 | 4689 | d_print_comp (dpi, options, dc->u.s_binary.left); |
d5031754 DD |
4690 | return; |
4691 | ||
664aa91f DD |
4692 | case DEMANGLE_COMPONENT_LAMBDA: |
4693 | d_append_string (dpi, "{lambda("); | |
ddee5e46 | 4694 | d_print_comp (dpi, options, dc->u.s_unary_num.sub); |
664aa91f DD |
4695 | d_append_string (dpi, ")#"); |
4696 | d_append_num (dpi, dc->u.s_unary_num.num + 1); | |
4697 | d_append_char (dpi, '}'); | |
4698 | return; | |
4699 | ||
4700 | case DEMANGLE_COMPONENT_UNNAMED_TYPE: | |
4701 | d_append_string (dpi, "{unnamed type#"); | |
4702 | d_append_num (dpi, dc->u.s_number.number + 1); | |
4703 | d_append_char (dpi, '}'); | |
4704 | return; | |
4705 | ||
7955ede5 DD |
4706 | case DEMANGLE_COMPONENT_CLONE: |
4707 | d_print_comp (dpi, options, d_left (dc)); | |
4708 | d_append_string (dpi, " [clone "); | |
4709 | d_print_comp (dpi, options, d_right (dc)); | |
4710 | d_append_char (dpi, ']'); | |
4711 | return; | |
4712 | ||
d00edca5 DD |
4713 | default: |
4714 | d_print_error (dpi); | |
4715 | return; | |
4716 | } | |
eb383413 L |
4717 | } |
4718 | ||
b6fb00c0 DD |
4719 | /* Print a Java dentifier. For Java we try to handle encoded extended |
4720 | Unicode characters. The C++ ABI doesn't mention Unicode encoding, | |
4721 | so we don't it for C++. Characters are encoded as | |
4722 | __U<hex-char>+_. */ | |
eb383413 | 4723 | |
d00edca5 | 4724 | static void |
9334f9c6 | 4725 | d_print_java_identifier (struct d_print_info *dpi, const char *name, int len) |
eb383413 | 4726 | { |
b6fb00c0 DD |
4727 | const char *p; |
4728 | const char *end; | |
eb383413 | 4729 | |
b6fb00c0 DD |
4730 | end = name + len; |
4731 | for (p = name; p < end; ++p) | |
4732 | { | |
4733 | if (end - p > 3 | |
4734 | && p[0] == '_' | |
4735 | && p[1] == '_' | |
4736 | && p[2] == 'U') | |
eb383413 | 4737 | { |
b6fb00c0 DD |
4738 | unsigned long c; |
4739 | const char *q; | |
4740 | ||
4741 | c = 0; | |
4742 | for (q = p + 3; q < end; ++q) | |
d00edca5 | 4743 | { |
b6fb00c0 DD |
4744 | int dig; |
4745 | ||
4746 | if (IS_DIGIT (*q)) | |
4747 | dig = *q - '0'; | |
4748 | else if (*q >= 'A' && *q <= 'F') | |
4749 | dig = *q - 'A' + 10; | |
4750 | else if (*q >= 'a' && *q <= 'f') | |
4751 | dig = *q - 'a' + 10; | |
4752 | else | |
4753 | break; | |
eb383413 | 4754 | |
b6fb00c0 DD |
4755 | c = c * 16 + dig; |
4756 | } | |
4757 | /* If the Unicode character is larger than 256, we don't try | |
4758 | to deal with it here. FIXME. */ | |
4759 | if (q < end && *q == '_' && c < 256) | |
4760 | { | |
4761 | d_append_char (dpi, c); | |
4762 | p = q; | |
4763 | continue; | |
d00edca5 | 4764 | } |
d00edca5 | 4765 | } |
b6fb00c0 DD |
4766 | |
4767 | d_append_char (dpi, *p); | |
eb383413 | 4768 | } |
eb383413 L |
4769 | } |
4770 | ||
858b45cf DD |
4771 | /* Print a list of modifiers. SUFFIX is 1 if we are printing |
4772 | qualifiers on this after printing a function. */ | |
eb383413 | 4773 | |
d00edca5 | 4774 | static void |
ddee5e46 | 4775 | d_print_mod_list (struct d_print_info *dpi, int options, |
9334f9c6 | 4776 | struct d_print_mod *mods, int suffix) |
eb383413 | 4777 | { |
331c3da2 DD |
4778 | struct d_print_template *hold_dpt; |
4779 | ||
858b45cf | 4780 | if (mods == NULL || d_print_saw_error (dpi)) |
d00edca5 | 4781 | return; |
eb383413 | 4782 | |
858b45cf DD |
4783 | if (mods->printed |
4784 | || (! suffix | |
59727473 DD |
4785 | && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
4786 | || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS | |
4787 | || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS))) | |
858b45cf | 4788 | { |
ddee5e46 | 4789 | d_print_mod_list (dpi, options, mods->next, suffix); |
858b45cf DD |
4790 | return; |
4791 | } | |
4792 | ||
331c3da2 DD |
4793 | mods->printed = 1; |
4794 | ||
4795 | hold_dpt = dpi->templates; | |
4796 | dpi->templates = mods->templates; | |
4797 | ||
59727473 | 4798 | if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE) |
eb383413 | 4799 | { |
ddee5e46 | 4800 | d_print_function_type (dpi, options, mods->mod, mods->next); |
331c3da2 | 4801 | dpi->templates = hold_dpt; |
d00edca5 DD |
4802 | return; |
4803 | } | |
59727473 | 4804 | else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) |
d00edca5 | 4805 | { |
ddee5e46 | 4806 | d_print_array_type (dpi, options, mods->mod, mods->next); |
331c3da2 | 4807 | dpi->templates = hold_dpt; |
d00edca5 DD |
4808 | return; |
4809 | } | |
59727473 | 4810 | else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME) |
d4edd112 DD |
4811 | { |
4812 | struct d_print_mod *hold_modifiers; | |
59727473 | 4813 | struct demangle_component *dc; |
d4edd112 DD |
4814 | |
4815 | /* When this is on the modifier stack, we have pulled any | |
4816 | qualifiers off the right argument already. Otherwise, we | |
4817 | print it as usual, but don't let the left argument see any | |
4818 | modifiers. */ | |
4819 | ||
4820 | hold_modifiers = dpi->modifiers; | |
4821 | dpi->modifiers = NULL; | |
ddee5e46 | 4822 | d_print_comp (dpi, options, d_left (mods->mod)); |
d4edd112 DD |
4823 | dpi->modifiers = hold_modifiers; |
4824 | ||
ddee5e46 | 4825 | if ((options & DMGL_JAVA) == 0) |
208c1674 | 4826 | d_append_string (dpi, "::"); |
b6fb00c0 DD |
4827 | else |
4828 | d_append_char (dpi, '.'); | |
d4edd112 DD |
4829 | |
4830 | dc = d_right (mods->mod); | |
664aa91f DD |
4831 | |
4832 | if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG) | |
4833 | { | |
4834 | d_append_string (dpi, "{default arg#"); | |
4835 | d_append_num (dpi, dc->u.s_unary_num.num + 1); | |
4836 | d_append_string (dpi, "}::"); | |
4837 | dc = dc->u.s_unary_num.sub; | |
4838 | } | |
4839 | ||
59727473 DD |
4840 | while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
4841 | || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS | |
4842 | || dc->type == DEMANGLE_COMPONENT_CONST_THIS) | |
d4edd112 DD |
4843 | dc = d_left (dc); |
4844 | ||
ddee5e46 | 4845 | d_print_comp (dpi, options, dc); |
d4edd112 DD |
4846 | |
4847 | dpi->templates = hold_dpt; | |
4848 | return; | |
4849 | } | |
eb383413 | 4850 | |
ddee5e46 | 4851 | d_print_mod (dpi, options, mods->mod); |
eb383413 | 4852 | |
331c3da2 DD |
4853 | dpi->templates = hold_dpt; |
4854 | ||
ddee5e46 | 4855 | d_print_mod_list (dpi, options, mods->next, suffix); |
eb383413 | 4856 | } |
331c3da2 | 4857 | |
d00edca5 | 4858 | /* Print a modifier. */ |
eb383413 | 4859 | |
d00edca5 | 4860 | static void |
ddee5e46 | 4861 | d_print_mod (struct d_print_info *dpi, int options, |
9334f9c6 | 4862 | const struct demangle_component *mod) |
d00edca5 DD |
4863 | { |
4864 | switch (mod->type) | |
4865 | { | |
59727473 DD |
4866 | case DEMANGLE_COMPONENT_RESTRICT: |
4867 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
208c1674 | 4868 | d_append_string (dpi, " restrict"); |
d00edca5 | 4869 | return; |
59727473 DD |
4870 | case DEMANGLE_COMPONENT_VOLATILE: |
4871 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
208c1674 | 4872 | d_append_string (dpi, " volatile"); |
d00edca5 | 4873 | return; |
59727473 DD |
4874 | case DEMANGLE_COMPONENT_CONST: |
4875 | case DEMANGLE_COMPONENT_CONST_THIS: | |
208c1674 | 4876 | d_append_string (dpi, " const"); |
d00edca5 | 4877 | return; |
59727473 | 4878 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
d00edca5 | 4879 | d_append_char (dpi, ' '); |
ddee5e46 | 4880 | d_print_comp (dpi, options, d_right (mod)); |
d00edca5 | 4881 | return; |
59727473 | 4882 | case DEMANGLE_COMPONENT_POINTER: |
d00edca5 | 4883 | /* There is no pointer symbol in Java. */ |
ddee5e46 | 4884 | if ((options & DMGL_JAVA) == 0) |
d00edca5 DD |
4885 | d_append_char (dpi, '*'); |
4886 | return; | |
59727473 | 4887 | case DEMANGLE_COMPONENT_REFERENCE: |
d00edca5 DD |
4888 | d_append_char (dpi, '&'); |
4889 | return; | |
8969a67f DD |
4890 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
4891 | d_append_string (dpi, "&&"); | |
4892 | return; | |
59727473 | 4893 | case DEMANGLE_COMPONENT_COMPLEX: |
208c1674 | 4894 | d_append_string (dpi, "complex "); |
d00edca5 | 4895 | return; |
59727473 | 4896 | case DEMANGLE_COMPONENT_IMAGINARY: |
208c1674 | 4897 | d_append_string (dpi, "imaginary "); |
d00edca5 | 4898 | return; |
59727473 | 4899 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
858b45cf | 4900 | if (d_last_char (dpi) != '(') |
d00edca5 | 4901 | d_append_char (dpi, ' '); |
ddee5e46 | 4902 | d_print_comp (dpi, options, d_left (mod)); |
208c1674 | 4903 | d_append_string (dpi, "::*"); |
d00edca5 | 4904 | return; |
59727473 | 4905 | case DEMANGLE_COMPONENT_TYPED_NAME: |
ddee5e46 | 4906 | d_print_comp (dpi, options, d_left (mod)); |
d00edca5 | 4907 | return; |
cbc43128 | 4908 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
f9b58c5b | 4909 | d_append_string (dpi, " __vector("); |
ddee5e46 | 4910 | d_print_comp (dpi, options, d_left (mod)); |
f9b58c5b | 4911 | d_append_char (dpi, ')'); |
cbc43128 DD |
4912 | return; |
4913 | ||
d00edca5 DD |
4914 | default: |
4915 | /* Otherwise, we have something that won't go back on the | |
4916 | modifier stack, so we can just print it. */ | |
ddee5e46 | 4917 | d_print_comp (dpi, options, mod); |
d00edca5 DD |
4918 | return; |
4919 | } | |
4920 | } | |
eb383413 | 4921 | |
d00edca5 | 4922 | /* Print a function type, except for the return type. */ |
eb383413 | 4923 | |
d00edca5 | 4924 | static void |
ddee5e46 | 4925 | d_print_function_type (struct d_print_info *dpi, int options, |
9334f9c6 DD |
4926 | const struct demangle_component *dc, |
4927 | struct d_print_mod *mods) | |
eb383413 | 4928 | { |
331c3da2 | 4929 | int need_paren; |
2d733211 | 4930 | int need_space; |
331c3da2 | 4931 | struct d_print_mod *p; |
d4edd112 | 4932 | struct d_print_mod *hold_modifiers; |
331c3da2 DD |
4933 | |
4934 | need_paren = 0; | |
2d733211 | 4935 | need_space = 0; |
331c3da2 | 4936 | for (p = mods; p != NULL; p = p->next) |
d00edca5 | 4937 | { |
331c3da2 DD |
4938 | if (p->printed) |
4939 | break; | |
eb383413 | 4940 | |
331c3da2 | 4941 | switch (p->mod->type) |
d00edca5 | 4942 | { |
2d733211 DD |
4943 | case DEMANGLE_COMPONENT_POINTER: |
4944 | case DEMANGLE_COMPONENT_REFERENCE: | |
8969a67f | 4945 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
2d733211 DD |
4946 | need_paren = 1; |
4947 | break; | |
59727473 DD |
4948 | case DEMANGLE_COMPONENT_RESTRICT: |
4949 | case DEMANGLE_COMPONENT_VOLATILE: | |
4950 | case DEMANGLE_COMPONENT_CONST: | |
4951 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
59727473 DD |
4952 | case DEMANGLE_COMPONENT_COMPLEX: |
4953 | case DEMANGLE_COMPONENT_IMAGINARY: | |
4954 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: | |
2d733211 | 4955 | need_space = 1; |
331c3da2 DD |
4956 | need_paren = 1; |
4957 | break; | |
59727473 DD |
4958 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
4959 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
4960 | case DEMANGLE_COMPONENT_CONST_THIS: | |
858b45cf | 4961 | break; |
331c3da2 DD |
4962 | default: |
4963 | break; | |
d00edca5 | 4964 | } |
331c3da2 DD |
4965 | if (need_paren) |
4966 | break; | |
4967 | } | |
eb383413 | 4968 | |
331c3da2 | 4969 | if (need_paren) |
858b45cf | 4970 | { |
2d733211 | 4971 | if (! need_space) |
858b45cf | 4972 | { |
2d733211 DD |
4973 | if (d_last_char (dpi) != '(' |
4974 | && d_last_char (dpi) != '*') | |
4975 | need_space = 1; | |
858b45cf | 4976 | } |
2d733211 DD |
4977 | if (need_space && d_last_char (dpi) != ' ') |
4978 | d_append_char (dpi, ' '); | |
858b45cf DD |
4979 | d_append_char (dpi, '('); |
4980 | } | |
eb383413 | 4981 | |
d4edd112 DD |
4982 | hold_modifiers = dpi->modifiers; |
4983 | dpi->modifiers = NULL; | |
4984 | ||
ddee5e46 | 4985 | d_print_mod_list (dpi, options, mods, 0); |
eb383413 | 4986 | |
331c3da2 DD |
4987 | if (need_paren) |
4988 | d_append_char (dpi, ')'); | |
eb383413 | 4989 | |
d00edca5 | 4990 | d_append_char (dpi, '('); |
eb383413 | 4991 | |
d00edca5 | 4992 | if (d_right (dc) != NULL) |
ddee5e46 | 4993 | d_print_comp (dpi, options, d_right (dc)); |
eb383413 | 4994 | |
d00edca5 | 4995 | d_append_char (dpi, ')'); |
858b45cf | 4996 | |
ddee5e46 | 4997 | d_print_mod_list (dpi, options, mods, 1); |
d4edd112 DD |
4998 | |
4999 | dpi->modifiers = hold_modifiers; | |
d00edca5 | 5000 | } |
eb383413 | 5001 | |
d00edca5 | 5002 | /* Print an array type, except for the element type. */ |
eb383413 | 5003 | |
d00edca5 | 5004 | static void |
ddee5e46 | 5005 | d_print_array_type (struct d_print_info *dpi, int options, |
9334f9c6 DD |
5006 | const struct demangle_component *dc, |
5007 | struct d_print_mod *mods) | |
d00edca5 DD |
5008 | { |
5009 | int need_space; | |
eb383413 | 5010 | |
d00edca5 DD |
5011 | need_space = 1; |
5012 | if (mods != NULL) | |
eb383413 | 5013 | { |
d00edca5 DD |
5014 | int need_paren; |
5015 | struct d_print_mod *p; | |
03d5f569 | 5016 | |
d00edca5 DD |
5017 | need_paren = 0; |
5018 | for (p = mods; p != NULL; p = p->next) | |
eb383413 | 5019 | { |
74aee4eb | 5020 | if (! p->printed) |
eb383413 | 5021 | { |
74aee4eb DD |
5022 | if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) |
5023 | { | |
5024 | need_space = 0; | |
5025 | break; | |
5026 | } | |
5027 | else | |
5028 | { | |
5029 | need_paren = 1; | |
5030 | need_space = 1; | |
5031 | break; | |
5032 | } | |
eb383413 | 5033 | } |
d00edca5 | 5034 | } |
eb383413 | 5035 | |
d00edca5 | 5036 | if (need_paren) |
208c1674 | 5037 | d_append_string (dpi, " ("); |
eb383413 | 5038 | |
ddee5e46 | 5039 | d_print_mod_list (dpi, options, mods, 0); |
eb383413 | 5040 | |
d00edca5 DD |
5041 | if (need_paren) |
5042 | d_append_char (dpi, ')'); | |
5043 | } | |
eb383413 | 5044 | |
d00edca5 DD |
5045 | if (need_space) |
5046 | d_append_char (dpi, ' '); | |
03d5f569 | 5047 | |
d00edca5 | 5048 | d_append_char (dpi, '['); |
03d5f569 | 5049 | |
d00edca5 | 5050 | if (d_left (dc) != NULL) |
ddee5e46 | 5051 | d_print_comp (dpi, options, d_left (dc)); |
eb383413 | 5052 | |
d00edca5 DD |
5053 | d_append_char (dpi, ']'); |
5054 | } | |
eb383413 | 5055 | |
d00edca5 | 5056 | /* Print an operator in an expression. */ |
eb383413 | 5057 | |
d00edca5 | 5058 | static void |
ddee5e46 | 5059 | d_print_expr_op (struct d_print_info *dpi, int options, |
9334f9c6 | 5060 | const struct demangle_component *dc) |
d00edca5 | 5061 | { |
59727473 | 5062 | if (dc->type == DEMANGLE_COMPONENT_OPERATOR) |
b6fb00c0 DD |
5063 | d_append_buffer (dpi, dc->u.s_operator.op->name, |
5064 | dc->u.s_operator.op->len); | |
d00edca5 | 5065 | else |
ddee5e46 | 5066 | d_print_comp (dpi, options, dc); |
eb383413 L |
5067 | } |
5068 | ||
d00edca5 | 5069 | /* Print a cast. */ |
eb383413 | 5070 | |
d00edca5 | 5071 | static void |
ddee5e46 | 5072 | d_print_cast (struct d_print_info *dpi, int options, |
9334f9c6 | 5073 | const struct demangle_component *dc) |
eb383413 | 5074 | { |
59727473 | 5075 | if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE) |
ddee5e46 | 5076 | d_print_comp (dpi, options, d_left (dc)); |
d00edca5 DD |
5077 | else |
5078 | { | |
331c3da2 | 5079 | struct d_print_mod *hold_dpm; |
d00edca5 | 5080 | struct d_print_template dpt; |
0976f6a7 | 5081 | |
d00edca5 DD |
5082 | /* It appears that for a templated cast operator, we need to put |
5083 | the template parameters in scope for the operator name, but | |
5084 | not for the parameters. The effect is that we need to handle | |
24afc00d | 5085 | the template printing here. */ |
eb383413 | 5086 | |
331c3da2 DD |
5087 | hold_dpm = dpi->modifiers; |
5088 | dpi->modifiers = NULL; | |
5089 | ||
d00edca5 DD |
5090 | dpt.next = dpi->templates; |
5091 | dpi->templates = &dpt; | |
abf6a75b | 5092 | dpt.template_decl = d_left (dc); |
0976f6a7 | 5093 | |
ddee5e46 | 5094 | d_print_comp (dpi, options, d_left (d_left (dc))); |
0976f6a7 | 5095 | |
d00edca5 | 5096 | dpi->templates = dpt.next; |
eb383413 | 5097 | |
858b45cf DD |
5098 | if (d_last_char (dpi) == '<') |
5099 | d_append_char (dpi, ' '); | |
d00edca5 | 5100 | d_append_char (dpi, '<'); |
ddee5e46 | 5101 | d_print_comp (dpi, options, d_right (d_left (dc))); |
d00edca5 DD |
5102 | /* Avoid generating two consecutive '>' characters, to avoid |
5103 | the C++ syntactic ambiguity. */ | |
858b45cf | 5104 | if (d_last_char (dpi) == '>') |
d00edca5 DD |
5105 | d_append_char (dpi, ' '); |
5106 | d_append_char (dpi, '>'); | |
331c3da2 DD |
5107 | |
5108 | dpi->modifiers = hold_dpm; | |
eb383413 | 5109 | } |
d00edca5 DD |
5110 | } |
5111 | ||
5112 | /* Initialize the information structure we use to pass around | |
5113 | information. */ | |
5114 | ||
59727473 DD |
5115 | CP_STATIC_IF_GLIBCPP_V3 |
5116 | void | |
9334f9c6 DD |
5117 | cplus_demangle_init_info (const char *mangled, int options, size_t len, |
5118 | struct d_info *di) | |
eb383413 | 5119 | { |
d00edca5 | 5120 | di->s = mangled; |
b6fb00c0 | 5121 | di->send = mangled + len; |
d00edca5 | 5122 | di->options = options; |
eb383413 | 5123 | |
d00edca5 DD |
5124 | di->n = mangled; |
5125 | ||
5126 | /* We can not need more components than twice the number of chars in | |
5127 | the mangled string. Most components correspond directly to | |
5128 | chars, but the ARGLIST types are exceptions. */ | |
5129 | di->num_comps = 2 * len; | |
d00edca5 DD |
5130 | di->next_comp = 0; |
5131 | ||
5132 | /* Similarly, we can not need more substitutions than there are | |
331c3da2 DD |
5133 | chars in the mangled string. */ |
5134 | di->num_subs = len; | |
d00edca5 | 5135 | di->next_sub = 0; |
b6fb00c0 | 5136 | di->did_subs = 0; |
d00edca5 DD |
5137 | |
5138 | di->last_name = NULL; | |
5139 | ||
b6fb00c0 | 5140 | di->expansion = 0; |
eb383413 L |
5141 | } |
5142 | ||
208c1674 DD |
5143 | /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI |
5144 | mangled name, return strings in repeated callback giving the demangled | |
5145 | name. OPTIONS is the usual libiberty demangler options. On success, | |
5146 | this returns 1. On failure, returns 0. */ | |
eb383413 | 5147 | |
208c1674 DD |
5148 | static int |
5149 | d_demangle_callback (const char *mangled, int options, | |
5150 | demangle_callbackref callback, void *opaque) | |
eb383413 | 5151 | { |
d5031754 DD |
5152 | enum |
5153 | { | |
5154 | DCT_TYPE, | |
5155 | DCT_MANGLED, | |
5156 | DCT_GLOBAL_CTORS, | |
5157 | DCT_GLOBAL_DTORS | |
5158 | } | |
5159 | type; | |
d00edca5 | 5160 | struct d_info di; |
59727473 | 5161 | struct demangle_component *dc; |
208c1674 | 5162 | int status; |
d00edca5 DD |
5163 | |
5164 | if (mangled[0] == '_' && mangled[1] == 'Z') | |
d5031754 | 5165 | type = DCT_MANGLED; |
d00edca5 DD |
5166 | else if (strncmp (mangled, "_GLOBAL_", 8) == 0 |
5167 | && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$') | |
5168 | && (mangled[9] == 'D' || mangled[9] == 'I') | |
5169 | && mangled[10] == '_') | |
d5031754 | 5170 | type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS; |
eb383413 L |
5171 | else |
5172 | { | |
d00edca5 | 5173 | if ((options & DMGL_TYPES) == 0) |
208c1674 | 5174 | return 0; |
d5031754 | 5175 | type = DCT_TYPE; |
eb383413 L |
5176 | } |
5177 | ||
208c1674 | 5178 | cplus_demangle_init_info (mangled, options, strlen (mangled), &di); |
03d5f569 | 5179 | |
b6fb00c0 DD |
5180 | { |
5181 | #ifdef CP_DYNAMIC_ARRAYS | |
59727473 DD |
5182 | __extension__ struct demangle_component comps[di.num_comps]; |
5183 | __extension__ struct demangle_component *subs[di.num_subs]; | |
b6fb00c0 | 5184 | |
208c1674 DD |
5185 | di.comps = comps; |
5186 | di.subs = subs; | |
b6fb00c0 | 5187 | #else |
208c1674 DD |
5188 | di.comps = alloca (di.num_comps * sizeof (*di.comps)); |
5189 | di.subs = alloca (di.num_subs * sizeof (*di.subs)); | |
b6fb00c0 DD |
5190 | #endif |
5191 | ||
d5031754 DD |
5192 | switch (type) |
5193 | { | |
5194 | case DCT_TYPE: | |
5195 | dc = cplus_demangle_type (&di); | |
5196 | break; | |
5197 | case DCT_MANGLED: | |
5198 | dc = cplus_demangle_mangled_name (&di, 1); | |
5199 | break; | |
5200 | case DCT_GLOBAL_CTORS: | |
5201 | case DCT_GLOBAL_DTORS: | |
5202 | d_advance (&di, 11); | |
5203 | dc = d_make_comp (&di, | |
5204 | (type == DCT_GLOBAL_CTORS | |
5205 | ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS | |
5206 | : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS), | |
a0692e36 | 5207 | d_make_demangle_mangled_name (&di, d_str (&di)), |
d5031754 DD |
5208 | NULL); |
5209 | d_advance (&di, strlen (d_str (&di))); | |
5210 | break; | |
5211 | } | |
d00edca5 | 5212 | |
b6fb00c0 DD |
5213 | /* If DMGL_PARAMS is set, then if we didn't consume the entire |
5214 | mangled string, then we didn't successfully demangle it. If | |
5215 | DMGL_PARAMS is not set, we didn't look at the trailing | |
5216 | parameters. */ | |
5217 | if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0') | |
5218 | dc = NULL; | |
24afc00d | 5219 | |
d00edca5 | 5220 | #ifdef CP_DEMANGLE_DEBUG |
208c1674 | 5221 | d_dump (dc, 0); |
d00edca5 DD |
5222 | #endif |
5223 | ||
208c1674 DD |
5224 | status = (dc != NULL) |
5225 | ? cplus_demangle_print_callback (options, dc, callback, opaque) | |
5226 | : 0; | |
5227 | } | |
03d5f569 | 5228 | |
208c1674 DD |
5229 | return status; |
5230 | } | |
03d5f569 | 5231 | |
208c1674 DD |
5232 | /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled |
5233 | name, return a buffer allocated with malloc holding the demangled | |
5234 | name. OPTIONS is the usual libiberty demangler options. On | |
5235 | success, this sets *PALC to the allocated size of the returned | |
5236 | buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for | |
5237 | a memory allocation failure, and returns NULL. */ | |
b6fb00c0 | 5238 | |
208c1674 DD |
5239 | static char * |
5240 | d_demangle (const char *mangled, int options, size_t *palc) | |
5241 | { | |
5242 | struct d_growable_string dgs; | |
5243 | int status; | |
03d5f569 | 5244 | |
208c1674 DD |
5245 | d_growable_string_init (&dgs, 0); |
5246 | ||
5247 | status = d_demangle_callback (mangled, options, | |
5248 | d_growable_string_callback_adapter, &dgs); | |
5249 | if (status == 0) | |
5250 | { | |
5251 | free (dgs.buf); | |
5252 | *palc = 0; | |
5253 | return NULL; | |
5254 | } | |
5255 | ||
ffe7cfdf | 5256 | *palc = dgs.allocation_failure ? 1 : dgs.alc; |
208c1674 | 5257 | return dgs.buf; |
eb383413 L |
5258 | } |
5259 | ||
0c4460bb | 5260 | #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3) |
d00edca5 | 5261 | |
9334f9c6 | 5262 | extern char *__cxa_demangle (const char *, char *, size_t *, int *); |
03d5f569 | 5263 | |
d00edca5 DD |
5264 | /* ia64 ABI-mandated entry point in the C++ runtime library for |
5265 | performing demangling. MANGLED_NAME is a NUL-terminated character | |
5266 | string containing the name to be demangled. | |
03d5f569 JM |
5267 | |
5268 | OUTPUT_BUFFER is a region of memory, allocated with malloc, of | |
5269 | *LENGTH bytes, into which the demangled name is stored. If | |
5270 | OUTPUT_BUFFER is not long enough, it is expanded using realloc. | |
5271 | OUTPUT_BUFFER may instead be NULL; in that case, the demangled name | |
d00edca5 | 5272 | is placed in a region of memory allocated with malloc. |
03d5f569 | 5273 | |
208c1674 | 5274 | If LENGTH is non-NULL, the length of the buffer containing the |
d00edca5 | 5275 | demangled name, is placed in *LENGTH. |
03d5f569 JM |
5276 | |
5277 | The return value is a pointer to the start of the NUL-terminated | |
5278 | demangled name, or NULL if the demangling fails. The caller is | |
d00edca5 | 5279 | responsible for deallocating this memory using free. |
03d5f569 JM |
5280 | |
5281 | *STATUS is set to one of the following values: | |
5282 | 0: The demangling operation succeeded. | |
d00edca5 | 5283 | -1: A memory allocation failure occurred. |
03d5f569 JM |
5284 | -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules. |
5285 | -3: One of the arguments is invalid. | |
5286 | ||
d00edca5 | 5287 | The demangling is performed using the C++ ABI mangling rules, with |
03d5f569 JM |
5288 | GNU extensions. */ |
5289 | ||
5290 | char * | |
9334f9c6 DD |
5291 | __cxa_demangle (const char *mangled_name, char *output_buffer, |
5292 | size_t *length, int *status) | |
03d5f569 | 5293 | { |
d00edca5 DD |
5294 | char *demangled; |
5295 | size_t alc; | |
03d5f569 | 5296 | |
d00edca5 DD |
5297 | if (mangled_name == NULL) |
5298 | { | |
74aee4eb DD |
5299 | if (status != NULL) |
5300 | *status = -3; | |
03d5f569 JM |
5301 | return NULL; |
5302 | } | |
03d5f569 | 5303 | |
d00edca5 | 5304 | if (output_buffer != NULL && length == NULL) |
03d5f569 | 5305 | { |
74aee4eb DD |
5306 | if (status != NULL) |
5307 | *status = -3; | |
d00edca5 | 5308 | return NULL; |
03d5f569 | 5309 | } |
d00edca5 | 5310 | |
74aee4eb | 5311 | demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc); |
d00edca5 DD |
5312 | |
5313 | if (demangled == NULL) | |
03d5f569 | 5314 | { |
74aee4eb DD |
5315 | if (status != NULL) |
5316 | { | |
5317 | if (alc == 1) | |
5318 | *status = -1; | |
5319 | else | |
5320 | *status = -2; | |
5321 | } | |
03d5f569 JM |
5322 | return NULL; |
5323 | } | |
d00edca5 DD |
5324 | |
5325 | if (output_buffer == NULL) | |
5326 | { | |
5327 | if (length != NULL) | |
5328 | *length = alc; | |
5329 | } | |
03d5f569 | 5330 | else |
03d5f569 | 5331 | { |
d00edca5 DD |
5332 | if (strlen (demangled) < *length) |
5333 | { | |
5334 | strcpy (output_buffer, demangled); | |
5335 | free (demangled); | |
5336 | demangled = output_buffer; | |
5337 | } | |
5338 | else | |
5339 | { | |
5340 | free (output_buffer); | |
5341 | *length = alc; | |
5342 | } | |
03d5f569 | 5343 | } |
d00edca5 | 5344 | |
74aee4eb DD |
5345 | if (status != NULL) |
5346 | *status = 0; | |
d00edca5 DD |
5347 | |
5348 | return demangled; | |
03d5f569 JM |
5349 | } |
5350 | ||
208c1674 DD |
5351 | extern int __gcclibcxx_demangle_callback (const char *, |
5352 | void (*) | |
5353 | (const char *, size_t, void *), | |
5354 | void *); | |
5355 | ||
5356 | /* Alternative, allocationless entry point in the C++ runtime library | |
5357 | for performing demangling. MANGLED_NAME is a NUL-terminated character | |
5358 | string containing the name to be demangled. | |
5359 | ||
5360 | CALLBACK is a callback function, called with demangled string | |
5361 | segments as demangling progresses; it is called at least once, | |
5362 | but may be called more than once. OPAQUE is a generalized pointer | |
5363 | used as a callback argument. | |
5364 | ||
5365 | The return code is one of the following values, equivalent to | |
5366 | the STATUS values of __cxa_demangle() (excluding -1, since this | |
5367 | function performs no memory allocations): | |
5368 | 0: The demangling operation succeeded. | |
5369 | -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules. | |
5370 | -3: One of the arguments is invalid. | |
5371 | ||
5372 | The demangling is performed using the C++ ABI mangling rules, with | |
5373 | GNU extensions. */ | |
5374 | ||
5375 | int | |
5376 | __gcclibcxx_demangle_callback (const char *mangled_name, | |
5377 | void (*callback) (const char *, size_t, void *), | |
5378 | void *opaque) | |
5379 | { | |
5380 | int status; | |
5381 | ||
5382 | if (mangled_name == NULL || callback == NULL) | |
5383 | return -3; | |
5384 | ||
5385 | status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES, | |
5386 | callback, opaque); | |
5387 | if (status == 0) | |
5388 | return -2; | |
5389 | ||
5390 | return 0; | |
5391 | } | |
5392 | ||
0c4460bb | 5393 | #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */ |
03d5f569 | 5394 | |
d00edca5 DD |
5395 | /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI |
5396 | mangled name, return a buffer allocated with malloc holding the | |
5397 | demangled name. Otherwise, return NULL. */ | |
eb383413 L |
5398 | |
5399 | char * | |
208c1674 | 5400 | cplus_demangle_v3 (const char *mangled, int options) |
eb383413 | 5401 | { |
d00edca5 | 5402 | size_t alc; |
849ee224 | 5403 | |
d00edca5 | 5404 | return d_demangle (mangled, options, &alc); |
eb383413 L |
5405 | } |
5406 | ||
208c1674 DD |
5407 | int |
5408 | cplus_demangle_v3_callback (const char *mangled, int options, | |
5409 | demangle_callbackref callback, void *opaque) | |
5410 | { | |
5411 | return d_demangle_callback (mangled, options, callback, opaque); | |
5412 | } | |
5413 | ||
bc9bf259 DD |
5414 | /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling |
5415 | conventions, but the output formatting is a little different. | |
208c1674 DD |
5416 | This instructs the C++ demangler not to emit pointer characters ("*"), to |
5417 | use Java's namespace separator symbol ("." instead of "::"), and to output | |
5418 | JArray<TYPE> as TYPE[]. */ | |
bc9bf259 DD |
5419 | |
5420 | char * | |
208c1674 | 5421 | java_demangle_v3 (const char *mangled) |
bc9bf259 | 5422 | { |
d00edca5 | 5423 | size_t alc; |
bc9bf259 | 5424 | |
208c1674 DD |
5425 | return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc); |
5426 | } | |
f2160d2b | 5427 | |
208c1674 DD |
5428 | int |
5429 | java_demangle_v3_callback (const char *mangled, | |
5430 | demangle_callbackref callback, void *opaque) | |
5431 | { | |
5432 | return d_demangle_callback (mangled, | |
5433 | DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, | |
5434 | callback, opaque); | |
bc9bf259 DD |
5435 | } |
5436 | ||
0c4460bb | 5437 | #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */ |
03d5f569 | 5438 | |
2a9dffbf | 5439 | #ifndef IN_GLIBCPP_V3 |
d00edca5 DD |
5440 | |
5441 | /* Demangle a string in order to find out whether it is a constructor | |
5442 | or destructor. Return non-zero on success. Set *CTOR_KIND and | |
5443 | *DTOR_KIND appropriately. */ | |
5444 | ||
5445 | static int | |
9334f9c6 DD |
5446 | is_ctor_or_dtor (const char *mangled, |
5447 | enum gnu_v3_ctor_kinds *ctor_kind, | |
5448 | enum gnu_v3_dtor_kinds *dtor_kind) | |
e61231f1 | 5449 | { |
d00edca5 | 5450 | struct d_info di; |
59727473 | 5451 | struct demangle_component *dc; |
858b45cf | 5452 | int ret; |
e61231f1 | 5453 | |
d00edca5 DD |
5454 | *ctor_kind = (enum gnu_v3_ctor_kinds) 0; |
5455 | *dtor_kind = (enum gnu_v3_dtor_kinds) 0; | |
5456 | ||
59727473 | 5457 | cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di); |
e61231f1 | 5458 | |
b6fb00c0 DD |
5459 | { |
5460 | #ifdef CP_DYNAMIC_ARRAYS | |
59727473 DD |
5461 | __extension__ struct demangle_component comps[di.num_comps]; |
5462 | __extension__ struct demangle_component *subs[di.num_subs]; | |
b6fb00c0 | 5463 | |
208c1674 DD |
5464 | di.comps = comps; |
5465 | di.subs = subs; | |
b6fb00c0 | 5466 | #else |
208c1674 DD |
5467 | di.comps = alloca (di.num_comps * sizeof (*di.comps)); |
5468 | di.subs = alloca (di.num_subs * sizeof (*di.subs)); | |
b6fb00c0 | 5469 | #endif |
d00edca5 | 5470 | |
59727473 | 5471 | dc = cplus_demangle_mangled_name (&di, 1); |
d35d0cd4 | 5472 | |
b6fb00c0 DD |
5473 | /* Note that because we did not pass DMGL_PARAMS, we don't expect |
5474 | to demangle the entire string. */ | |
e61231f1 | 5475 | |
b6fb00c0 DD |
5476 | ret = 0; |
5477 | while (dc != NULL) | |
5478 | { | |
5479 | switch (dc->type) | |
5480 | { | |
5481 | default: | |
5482 | dc = NULL; | |
5483 | break; | |
59727473 DD |
5484 | case DEMANGLE_COMPONENT_TYPED_NAME: |
5485 | case DEMANGLE_COMPONENT_TEMPLATE: | |
5486 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
5487 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
5488 | case DEMANGLE_COMPONENT_CONST_THIS: | |
b6fb00c0 DD |
5489 | dc = d_left (dc); |
5490 | break; | |
59727473 DD |
5491 | case DEMANGLE_COMPONENT_QUAL_NAME: |
5492 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
b6fb00c0 DD |
5493 | dc = d_right (dc); |
5494 | break; | |
59727473 | 5495 | case DEMANGLE_COMPONENT_CTOR: |
b6fb00c0 DD |
5496 | *ctor_kind = dc->u.s_ctor.kind; |
5497 | ret = 1; | |
5498 | dc = NULL; | |
5499 | break; | |
59727473 | 5500 | case DEMANGLE_COMPONENT_DTOR: |
b6fb00c0 DD |
5501 | *dtor_kind = dc->u.s_dtor.kind; |
5502 | ret = 1; | |
5503 | dc = NULL; | |
5504 | break; | |
5505 | } | |
5506 | } | |
b6fb00c0 | 5507 | } |
858b45cf DD |
5508 | |
5509 | return ret; | |
e61231f1 JB |
5510 | } |
5511 | ||
d00edca5 DD |
5512 | /* Return whether NAME is the mangled form of a g++ V3 ABI constructor |
5513 | name. A non-zero return indicates the type of constructor. */ | |
e61231f1 | 5514 | |
e61231f1 | 5515 | enum gnu_v3_ctor_kinds |
9334f9c6 | 5516 | is_gnu_v3_mangled_ctor (const char *name) |
e61231f1 | 5517 | { |
d00edca5 DD |
5518 | enum gnu_v3_ctor_kinds ctor_kind; |
5519 | enum gnu_v3_dtor_kinds dtor_kind; | |
e61231f1 | 5520 | |
d00edca5 | 5521 | if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind)) |
585cc78f | 5522 | return (enum gnu_v3_ctor_kinds) 0; |
d00edca5 | 5523 | return ctor_kind; |
e61231f1 JB |
5524 | } |
5525 | ||
5526 | ||
d00edca5 DD |
5527 | /* Return whether NAME is the mangled form of a g++ V3 ABI destructor |
5528 | name. A non-zero return indicates the type of destructor. */ | |
5529 | ||
e61231f1 | 5530 | enum gnu_v3_dtor_kinds |
9334f9c6 | 5531 | is_gnu_v3_mangled_dtor (const char *name) |
e61231f1 | 5532 | { |
d00edca5 DD |
5533 | enum gnu_v3_ctor_kinds ctor_kind; |
5534 | enum gnu_v3_dtor_kinds dtor_kind; | |
e61231f1 | 5535 | |
d00edca5 | 5536 | if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind)) |
585cc78f | 5537 | return (enum gnu_v3_dtor_kinds) 0; |
d00edca5 | 5538 | return dtor_kind; |
e61231f1 JB |
5539 | } |
5540 | ||
d00edca5 | 5541 | #endif /* IN_GLIBCPP_V3 */ |
e61231f1 | 5542 | |
eb383413 L |
5543 | #ifdef STANDALONE_DEMANGLER |
5544 | ||
5545 | #include "getopt.h" | |
d00edca5 DD |
5546 | #include "dyn-string.h" |
5547 | ||
e064c173 | 5548 | static void print_usage (FILE* fp, int exit_value); |
eb383413 | 5549 | |
d00edca5 DD |
5550 | #define IS_ALPHA(CHAR) \ |
5551 | (((CHAR) >= 'a' && (CHAR) <= 'z') \ | |
5552 | || ((CHAR) >= 'A' && (CHAR) <= 'Z')) | |
eb383413 L |
5553 | |
5554 | /* Non-zero if CHAR is a character than can occur in a mangled name. */ | |
5555 | #define is_mangled_char(CHAR) \ | |
74bcd529 DD |
5556 | (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \ |
5557 | || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$') | |
eb383413 L |
5558 | |
5559 | /* The name of this program, as invoked. */ | |
5560 | const char* program_name; | |
5561 | ||
5562 | /* Prints usage summary to FP and then exits with EXIT_VALUE. */ | |
5563 | ||
5564 | static void | |
9334f9c6 | 5565 | print_usage (FILE* fp, int exit_value) |
eb383413 L |
5566 | { |
5567 | fprintf (fp, "Usage: %s [options] [names ...]\n", program_name); | |
74bcd529 | 5568 | fprintf (fp, "Options:\n"); |
eb383413 | 5569 | fprintf (fp, " -h,--help Display this message.\n"); |
6d95373e | 5570 | fprintf (fp, " -p,--no-params Don't display function parameters\n"); |
eb383413 L |
5571 | fprintf (fp, " -v,--verbose Produce verbose demanglings.\n"); |
5572 | fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n"); | |
5573 | ||
5574 | exit (exit_value); | |
5575 | } | |
5576 | ||
5577 | /* Option specification for getopt_long. */ | |
c23795e2 | 5578 | static const struct option long_options[] = |
eb383413 | 5579 | { |
6d95373e DD |
5580 | { "help", no_argument, NULL, 'h' }, |
5581 | { "no-params", no_argument, NULL, 'p' }, | |
5582 | { "verbose", no_argument, NULL, 'v' }, | |
5583 | { NULL, no_argument, NULL, 0 }, | |
eb383413 L |
5584 | }; |
5585 | ||
5586 | /* Main entry for a demangling filter executable. It will demangle | |
5587 | its command line arguments, if any. If none are provided, it will | |
5588 | filter stdin to stdout, replacing any recognized mangled C++ names | |
5589 | with their demangled equivalents. */ | |
5590 | ||
5591 | int | |
9334f9c6 | 5592 | main (int argc, char *argv[]) |
eb383413 | 5593 | { |
eb383413 L |
5594 | int i; |
5595 | int opt_char; | |
d00edca5 | 5596 | int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES; |
eb383413 L |
5597 | |
5598 | /* Use the program name of this program, as invoked. */ | |
5599 | program_name = argv[0]; | |
5600 | ||
5601 | /* Parse options. */ | |
5602 | do | |
5603 | { | |
6d95373e | 5604 | opt_char = getopt_long (argc, argv, "hpv", long_options, NULL); |
eb383413 L |
5605 | switch (opt_char) |
5606 | { | |
5607 | case '?': /* Unrecognized option. */ | |
5608 | print_usage (stderr, 1); | |
5609 | break; | |
5610 | ||
5611 | case 'h': | |
5612 | print_usage (stdout, 0); | |
5613 | break; | |
5614 | ||
6d95373e DD |
5615 | case 'p': |
5616 | options &= ~ DMGL_PARAMS; | |
5617 | break; | |
5618 | ||
eb383413 | 5619 | case 'v': |
d00edca5 | 5620 | options |= DMGL_VERBOSE; |
eb383413 L |
5621 | break; |
5622 | } | |
5623 | } | |
5624 | while (opt_char != -1); | |
5625 | ||
5626 | if (optind == argc) | |
5627 | /* No command line arguments were provided. Filter stdin. */ | |
5628 | { | |
5629 | dyn_string_t mangled = dyn_string_new (3); | |
d00edca5 | 5630 | char *s; |
eb383413 L |
5631 | |
5632 | /* Read all of input. */ | |
5633 | while (!feof (stdin)) | |
5634 | { | |
d00edca5 | 5635 | char c; |
eb383413 L |
5636 | |
5637 | /* Pile characters into mangled until we hit one that can't | |
5638 | occur in a mangled name. */ | |
5639 | c = getchar (); | |
5640 | while (!feof (stdin) && is_mangled_char (c)) | |
5641 | { | |
5642 | dyn_string_append_char (mangled, c); | |
5643 | if (feof (stdin)) | |
5644 | break; | |
5645 | c = getchar (); | |
5646 | } | |
5647 | ||
d00edca5 | 5648 | if (dyn_string_length (mangled) > 0) |
03d5f569 | 5649 | { |
74aee4eb DD |
5650 | #ifdef IN_GLIBCPP_V3 |
5651 | s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL); | |
5652 | #else | |
d00edca5 | 5653 | s = cplus_demangle_v3 (dyn_string_buf (mangled), options); |
74aee4eb | 5654 | #endif |
d00edca5 DD |
5655 | |
5656 | if (s != NULL) | |
5657 | { | |
5658 | fputs (s, stdout); | |
5659 | free (s); | |
5660 | } | |
5661 | else | |
5662 | { | |
5663 | /* It might not have been a mangled name. Print the | |
5664 | original text. */ | |
5665 | fputs (dyn_string_buf (mangled), stdout); | |
5666 | } | |
5667 | ||
5668 | dyn_string_clear (mangled); | |
03d5f569 | 5669 | } |
eb383413 L |
5670 | |
5671 | /* If we haven't hit EOF yet, we've read one character that | |
5672 | can't occur in a mangled name, so print it out. */ | |
5673 | if (!feof (stdin)) | |
5674 | putchar (c); | |
eb383413 L |
5675 | } |
5676 | ||
5677 | dyn_string_delete (mangled); | |
eb383413 L |
5678 | } |
5679 | else | |
5680 | /* Demangle command line arguments. */ | |
5681 | { | |
eb383413 L |
5682 | /* Loop over command line arguments. */ |
5683 | for (i = optind; i < argc; ++i) | |
5684 | { | |
d00edca5 | 5685 | char *s; |
74aee4eb DD |
5686 | #ifdef IN_GLIBCPP_V3 |
5687 | int status; | |
5688 | #endif | |
d00edca5 | 5689 | |
eb383413 | 5690 | /* Attempt to demangle. */ |
74aee4eb DD |
5691 | #ifdef IN_GLIBCPP_V3 |
5692 | s = __cxa_demangle (argv[i], NULL, NULL, &status); | |
5693 | #else | |
d00edca5 | 5694 | s = cplus_demangle_v3 (argv[i], options); |
74aee4eb | 5695 | #endif |
eb383413 L |
5696 | |
5697 | /* If it worked, print the demangled name. */ | |
d00edca5 | 5698 | if (s != NULL) |
03d5f569 | 5699 | { |
d00edca5 DD |
5700 | printf ("%s\n", s); |
5701 | free (s); | |
03d5f569 | 5702 | } |
d00edca5 | 5703 | else |
74aee4eb DD |
5704 | { |
5705 | #ifdef IN_GLIBCPP_V3 | |
5706 | fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status); | |
5707 | #else | |
5708 | fprintf (stderr, "Failed: %s\n", argv[i]); | |
5709 | #endif | |
5710 | } | |
eb383413 | 5711 | } |
eb383413 L |
5712 | } |
5713 | ||
5714 | return 0; | |
5715 | } | |
5716 | ||
5717 | #endif /* STANDALONE_DEMANGLER */ |