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