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