1 #include <string.h>
2 #include <stdlib.h>
3 #define IN_LIBGCC2
4
5 /* Demangler for g++ V3 ABI.
6 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
7 Written by Ian Lance Taylor <ian@wasabisystems.com>.
8
9 This file is part of the libiberty library, which is part of GCC.
10
11 This file is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 In addition to the permissions in the GNU General Public License, the
17 Free Software Foundation gives you unlimited permission to link the
18 compiled version of this file into combinations with other programs,
19 and to distribute those combinations without any restriction coming
20 from the use of this file. (The General Public License restrictions
21 do apply in other respects; for example, they cover modification of
22 the file, and distribution when not linked into a combined
23 executable.)
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
33 */
34
35 /* This code implements a demangler for the g++ V3 ABI. The ABI is
36 described on this web page:
37 http://www.codesourcery.com/cxx-abi/abi.html#mangling
38
39 This code was written while looking at the demangler written by
40 Alex Samuel <samuel@codesourcery.com>.
41
42 This code first pulls the mangled name apart into a list of
43 components, and then walks the list generating the demangled
44 name.
45
46 This file will normally define the following functions, q.v.:
47 char *cplus_demangle_v3(const char *mangled, int options)
48 char *java_demangle_v3(const char *mangled)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
51
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
57 and these functions defined in this file:
58 cplus_demangle_fill_name
59 cplus_demangle_fill_extended_operator
60 cplus_demangle_fill_ctor
61 cplus_demangle_fill_dtor
62 cplus_demangle_print
63 and other functions defined in the file cp-demint.c.
64
65 This file also defines some other functions and variables which are
66 only to be used by the file cp-demint.c.
67
68 Preprocessor macros you can define while compiling this file:
69
70 IN_LIBGCC2
71 If defined, this file defines the following function, q.v.:
72 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
73 int *status)
74 instead of cplus_demangle_v3() and java_demangle_v3().
75
76 IN_GLIBCPP_V3
77 If defined, this file defines only __cxa_demangle(), and no other
78 publically visible functions or variables.
79
80 STANDALONE_DEMANGLER
81 If defined, this file defines a main() function which demangles
82 any arguments, or, if none, demangles stdin.
83
84 CP_DEMANGLE_DEBUG
85 If defined, turns on debugging mode, which prints information on
86 stdout about the mangled string. This is not generally useful.
87 */
88
89 #ifdef HAVE_CONFIG_H
90 #include "config.h"
91 #endif
92
93 #include <stdio.h>
94
95 #ifdef HAVE_STDLIB_H
96 #include <stdlib.h>
97 #endif
98 #ifdef HAVE_STRING_H
99 #include <string.h>
100 #endif
101
102 #include "ansidecl.h"
103 #include "libiberty.h"
104 #include "demangle.h"
105 #include "cp-demangle.h"
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 /* APPLE LOCAL begin mainline 2007-05-09 5173149 */ \
1062 case 'L':
1063 return d_unqualified_name (di);
1064
1065 /* APPLE LOCAL end mainline 2007-05-09 5173149 */ \
1066 case 'S':
1067 {
1068 int subst;
1069
1070 if (d_peek_next_char (di) != 't')
1071 {
1072 dc = d_substitution (di, 0);
1073 subst = 1;
1074 }
1075 else
1076 {
1077 d_advance (di, 2);
1078 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1079 d_make_name (di, "std", 3),
1080 d_unqualified_name (di));
1081 di->expansion += 3;
1082 subst = 0;
1083 }
1084
1085 if (d_peek_char (di) != 'I')
1086 {
1087 /* The grammar does not permit this case to occur if we
1088 called d_substitution() above (i.e., subst == 1). We
1089 don't bother to check. */
1090 }
1091 else
1092 {
1093 /* This is <template-args>, which means that we just saw
1094 <unscoped-template-name>, which is a substitution
1095 candidate if we didn't just get it from a
1096 substitution. */
1097 if (! subst)
1098 {
1099 if (! d_add_substitution (di, dc))
1100 return NULL;
1101 }
1102 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1103 d_template_args (di));
1104 }
1105
1106 return dc;
1107 }
1108
1109 default:
1110 dc = d_unqualified_name (di);
1111 if (d_peek_char (di) == 'I')
1112 {
1113 /* This is <template-args>, which means that we just saw
1114 <unscoped-template-name>, which is a substitution
1115 candidate. */
1116 if (! d_add_substitution (di, dc))
1117 return NULL;
1118 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1119 d_template_args (di));
1120 }
1121 return dc;
1122 }
1123 }
1124
1125 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1126 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1127 */
1128
1129 static struct demangle_component *
d_nested_name(struct d_info * di)1130 d_nested_name (struct d_info *di)
1131 {
1132 struct demangle_component *ret;
1133 struct demangle_component **pret;
1134
1135 if (d_next_char (di) != 'N')
1136 return NULL;
1137
1138 pret = d_cv_qualifiers (di, &ret, 1);
1139 if (pret == NULL)
1140 return NULL;
1141
1142 *pret = d_prefix (di);
1143 if (*pret == NULL)
1144 return NULL;
1145
1146 if (d_next_char (di) != 'E')
1147 return NULL;
1148
1149 return ret;
1150 }
1151
1152 /* <prefix> ::= <prefix> <unqualified-name>
1153 ::= <template-prefix> <template-args>
1154 ::= <template-param>
1155 ::=
1156 ::= <substitution>
1157
1158 <template-prefix> ::= <prefix> <(template) unqualified-name>
1159 ::= <template-param>
1160 ::= <substitution>
1161 */
1162
1163 static struct demangle_component *
d_prefix(struct d_info * di)1164 d_prefix (struct d_info *di)
1165 {
1166 struct demangle_component *ret = NULL;
1167
1168 while (1)
1169 {
1170 char peek;
1171 enum demangle_component_type comb_type;
1172 struct demangle_component *dc;
1173
1174 peek = d_peek_char (di);
1175 if (peek == '\0')
1176 return NULL;
1177
1178 /* The older code accepts a <local-name> here, but I don't see
1179 that in the grammar. The older code does not accept a
1180 <template-param> here. */
1181
1182 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1183 if (IS_DIGIT (peek)
1184 || IS_LOWER (peek)
1185 || peek == 'C'
1186 /* APPLE LOCAL begin mainline 2007-05-09 5173149 */ \
1187 || peek == 'D'
1188 || peek == 'L')
1189 /* APPLE LOCAL end mainline 2007-05-09 5173149 */ \
1190 dc = d_unqualified_name (di);
1191 else if (peek == 'S')
1192 dc = d_substitution (di, 1);
1193 else if (peek == 'I')
1194 {
1195 if (ret == NULL)
1196 return NULL;
1197 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1198 dc = d_template_args (di);
1199 }
1200 else if (peek == 'T')
1201 dc = d_template_param (di);
1202 else if (peek == 'E')
1203 return ret;
1204 else
1205 return NULL;
1206
1207 if (ret == NULL)
1208 ret = dc;
1209 else
1210 ret = d_make_comp (di, comb_type, ret, dc);
1211
1212 if (peek != 'S' && d_peek_char (di) != 'E')
1213 {
1214 if (! d_add_substitution (di, ret))
1215 return NULL;
1216 }
1217 }
1218 }
1219
1220 /* <unqualified-name> ::= <operator-name>
1221 ::= <ctor-dtor-name>
1222 ::= <source-name>
1223 APPLE LOCAL begin mainline 2007-05-09 5173149
1224 ::= <local-source-name>
1225
1226 <local-source-name> ::= L <source-name> <discriminator>
1227 APPLE LOCAL end mainline 2007-05-09 5173149
1228 */
1229
1230 static struct demangle_component *
d_unqualified_name(struct d_info * di)1231 d_unqualified_name (struct d_info *di)
1232 {
1233 char peek;
1234
1235 peek = d_peek_char (di);
1236 if (IS_DIGIT (peek))
1237 return d_source_name (di);
1238 else if (IS_LOWER (peek))
1239 {
1240 struct demangle_component *ret;
1241
1242 ret = d_operator_name (di);
1243 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1244 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1245 return ret;
1246 }
1247 else if (peek == 'C' || peek == 'D')
1248 return d_ctor_dtor_name (di);
1249 /* APPLE LOCAL begin mainline 2007-05-09 5173149 */ \
1250 else if (peek == 'L')
1251 {
1252 struct demangle_component * ret;
1253
1254 d_advance (di, 1);
1255
1256 ret = d_source_name (di);
1257 if (ret == NULL)
1258 return NULL;
1259 if (! d_discriminator (di))
1260 return NULL;
1261 return ret;
1262 }
1263 /* APPLE LOCAL end mainline 2007-05-09 5173149 */ \
1264 else
1265 return NULL;
1266 }
1267
1268 /* <source-name> ::= <(positive length) number> <identifier> */
1269
1270 static struct demangle_component *
d_source_name(struct d_info * di)1271 d_source_name (struct d_info *di)
1272 {
1273 long len;
1274 struct demangle_component *ret;
1275
1276 len = d_number (di);
1277 if (len <= 0)
1278 return NULL;
1279 ret = d_identifier (di, len);
1280 di->last_name = ret;
1281 return ret;
1282 }
1283
1284 /* number ::= [n] <(non-negative decimal integer)> */
1285
1286 static long
d_number(struct d_info * di)1287 d_number (struct d_info *di)
1288 {
1289 int negative;
1290 char peek;
1291 long ret;
1292
1293 negative = 0;
1294 peek = d_peek_char (di);
1295 if (peek == 'n')
1296 {
1297 negative = 1;
1298 d_advance (di, 1);
1299 peek = d_peek_char (di);
1300 }
1301
1302 ret = 0;
1303 while (1)
1304 {
1305 if (! IS_DIGIT (peek))
1306 {
1307 if (negative)
1308 ret = - ret;
1309 return ret;
1310 }
1311 ret = ret * 10 + peek - '0';
1312 d_advance (di, 1);
1313 peek = d_peek_char (di);
1314 }
1315 }
1316
1317 /* identifier ::= <(unqualified source code identifier)> */
1318
1319 static struct demangle_component *
d_identifier(struct d_info * di,int len)1320 d_identifier (struct d_info *di, int len)
1321 {
1322 const char *name;
1323
1324 name = d_str (di);
1325
1326 if (di->send - name < len)
1327 return NULL;
1328
1329 d_advance (di, len);
1330
1331 /* A Java mangled name may have a trailing '$' if it is a C++
1332 keyword. This '$' is not included in the length count. We just
1333 ignore the '$'. */
1334 if ((di->options & DMGL_JAVA) != 0
1335 && d_peek_char (di) == '$')
1336 d_advance (di, 1);
1337
1338 /* Look for something which looks like a gcc encoding of an
1339 anonymous namespace, and replace it with a more user friendly
1340 name. */
1341 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1342 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1343 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1344 {
1345 const char *s;
1346
1347 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1348 if ((*s == '.' || *s == '_' || *s == '$')
1349 && s[1] == 'N')
1350 {
1351 di->expansion -= len - sizeof "(anonymous namespace)";
1352 return d_make_name (di, "(anonymous namespace)",
1353 sizeof "(anonymous namespace)" - 1);
1354 }
1355 }
1356
1357 return d_make_name (di, name, len);
1358 }
1359
1360 /* operator_name ::= many different two character encodings.
1361 ::= cv <type>
1362 ::= v <digit> <source-name>
1363 */
1364
1365 #define NL(s) s, (sizeof s) - 1
1366
1367 CP_STATIC_IF_GLIBCPP_V3
1368 const struct demangle_operator_info cplus_demangle_operators[] =
1369 {
1370 { "aN", NL ("&="), 2 },
1371 { "aS", NL ("="), 2 },
1372 { "aa", NL ("&&"), 2 },
1373 { "ad", NL ("&"), 1 },
1374 { "an", NL ("&"), 2 },
1375 { "cl", NL ("()"), 0 },
1376 { "cm", NL (","), 2 },
1377 { "co", NL ("~"), 1 },
1378 { "dV", NL ("/="), 2 },
1379 { "da", NL ("delete[]"), 1 },
1380 { "de", NL ("*"), 1 },
1381 { "dl", NL ("delete"), 1 },
1382 { "dv", NL ("/"), 2 },
1383 { "eO", NL ("^="), 2 },
1384 { "eo", NL ("^"), 2 },
1385 { "eq", NL ("=="), 2 },
1386 { "ge", NL (">="), 2 },
1387 { "gt", NL (">"), 2 },
1388 { "ix", NL ("[]"), 2 },
1389 { "lS", NL ("<<="), 2 },
1390 { "le", NL ("<="), 2 },
1391 { "ls", NL ("<<"), 2 },
1392 { "lt", NL ("<"), 2 },
1393 { "mI", NL ("-="), 2 },
1394 { "mL", NL ("*="), 2 },
1395 { "mi", NL ("-"), 2 },
1396 { "ml", NL ("*"), 2 },
1397 { "mm", NL ("--"), 1 },
1398 { "na", NL ("new[]"), 1 },
1399 { "ne", NL ("!="), 2 },
1400 { "ng", NL ("-"), 1 },
1401 { "nt", NL ("!"), 1 },
1402 { "nw", NL ("new"), 1 },
1403 { "oR", NL ("|="), 2 },
1404 { "oo", NL ("||"), 2 },
1405 { "or", NL ("|"), 2 },
1406 { "pL", NL ("+="), 2 },
1407 { "pl", NL ("+"), 2 },
1408 { "pm", NL ("->*"), 2 },
1409 { "pp", NL ("++"), 1 },
1410 { "ps", NL ("+"), 1 },
1411 { "pt", NL ("->"), 2 },
1412 { "qu", NL ("?"), 3 },
1413 { "rM", NL ("%="), 2 },
1414 { "rS", NL (">>="), 2 },
1415 { "rm", NL ("%"), 2 },
1416 { "rs", NL (">>"), 2 },
1417 { "st", NL ("sizeof "), 1 },
1418 { "sz", NL ("sizeof "), 1 },
1419 { NULL, NULL, 0, 0 }
1420 };
1421
1422 static struct demangle_component *
d_operator_name(struct d_info * di)1423 d_operator_name (struct d_info *di)
1424 {
1425 char c1;
1426 char c2;
1427
1428 c1 = d_next_char (di);
1429 c2 = d_next_char (di);
1430 if (c1 == 'v' && IS_DIGIT (c2))
1431 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1432 else if (c1 == 'c' && c2 == 'v')
1433 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1434 cplus_demangle_type (di), NULL);
1435 else
1436 {
1437 /* LOW is the inclusive lower bound. */
1438 int low = 0;
1439 /* HIGH is the exclusive upper bound. We subtract one to ignore
1440 the sentinel at the end of the array. */
1441 int high = ((sizeof (cplus_demangle_operators)
1442 / sizeof (cplus_demangle_operators[0]))
1443 - 1);
1444
1445 while (1)
1446 {
1447 int i;
1448 const struct demangle_operator_info *p;
1449
1450 i = low + (high - low) / 2;
1451 p = cplus_demangle_operators + i;
1452
1453 if (c1 == p->code[0] && c2 == p->code[1])
1454 return d_make_operator (di, p);
1455
1456 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1457 high = i;
1458 else
1459 low = i + 1;
1460 if (low == high)
1461 return NULL;
1462 }
1463 }
1464 }
1465
1466 /* <special-name> ::= TV <type>
1467 ::= TT <type>
1468 ::= TI <type>
1469 ::= TS <type>
1470 ::= GV <(object) name>
1471 ::= T <call-offset> <(base) encoding>
1472 ::= Tc <call-offset> <call-offset> <(base) encoding>
1473 Also g++ extensions:
1474 ::= TC <type> <(offset) number> _ <(base) type>
1475 ::= TF <type>
1476 ::= TJ <type>
1477 ::= GR <name>
1478 ::= GA <encoding>
1479 */
1480
1481 static struct demangle_component *
d_special_name(struct d_info * di)1482 d_special_name (struct d_info *di)
1483 {
1484 char c;
1485
1486 di->expansion += 20;
1487 c = d_next_char (di);
1488 if (c == 'T')
1489 {
1490 switch (d_next_char (di))
1491 {
1492 case 'V':
1493 di->expansion -= 5;
1494 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1495 cplus_demangle_type (di), NULL);
1496 case 'T':
1497 di->expansion -= 10;
1498 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1499 cplus_demangle_type (di), NULL);
1500 case 'I':
1501 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1502 cplus_demangle_type (di), NULL);
1503 case 'S':
1504 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1505 cplus_demangle_type (di), NULL);
1506
1507 case 'h':
1508 if (! d_call_offset (di, 'h'))
1509 return NULL;
1510 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1511 d_encoding (di, 0), NULL);
1512
1513 case 'v':
1514 if (! d_call_offset (di, 'v'))
1515 return NULL;
1516 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1517 d_encoding (di, 0), NULL);
1518
1519 case 'c':
1520 if (! d_call_offset (di, '\0'))
1521 return NULL;
1522 if (! d_call_offset (di, '\0'))
1523 return NULL;
1524 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1525 d_encoding (di, 0), NULL);
1526
1527 case 'C':
1528 {
1529 struct demangle_component *derived_type;
1530 long offset;
1531 struct demangle_component *base_type;
1532
1533 derived_type = cplus_demangle_type (di);
1534 offset = d_number (di);
1535 if (offset < 0)
1536 return NULL;
1537 if (d_next_char (di) != '_')
1538 return NULL;
1539 base_type = cplus_demangle_type (di);
1540 /* We don't display the offset. FIXME: We should display
1541 it in verbose mode. */
1542 di->expansion += 5;
1543 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1544 base_type, derived_type);
1545 }
1546
1547 case 'F':
1548 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1549 cplus_demangle_type (di), NULL);
1550 case 'J':
1551 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1552 cplus_demangle_type (di), NULL);
1553
1554 default:
1555 return NULL;
1556 }
1557 }
1558 else if (c == 'G')
1559 {
1560 switch (d_next_char (di))
1561 {
1562 case 'V':
1563 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1564
1565 case 'R':
1566 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1567 NULL);
1568
1569 case 'A':
1570 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1571 d_encoding (di, 0), NULL);
1572
1573 default:
1574 return NULL;
1575 }
1576 }
1577 else
1578 return NULL;
1579 }
1580
1581 /* <call-offset> ::= h <nv-offset> _
1582 ::= v <v-offset> _
1583
1584 <nv-offset> ::= <(offset) number>
1585
1586 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1587
1588 The C parameter, if not '\0', is a character we just read which is
1589 the start of the <call-offset>.
1590
1591 We don't display the offset information anywhere. FIXME: We should
1592 display it in verbose mode. */
1593
1594 static int
d_call_offset(struct d_info * di,int c)1595 d_call_offset (struct d_info *di, int c)
1596 {
1597 if (c == '\0')
1598 c = d_next_char (di);
1599
1600 if (c == 'h')
1601 d_number (di);
1602 else if (c == 'v')
1603 {
1604 d_number (di);
1605 if (d_next_char (di) != '_')
1606 return 0;
1607 d_number (di);
1608 }
1609 else
1610 return 0;
1611
1612 if (d_next_char (di) != '_')
1613 return 0;
1614
1615 return 1;
1616 }
1617
1618 /* <ctor-dtor-name> ::= C1
1619 ::= C2
1620 ::= C3
1621 ::= D0
1622 ::= D1
1623 ::= D2
1624 */
1625
1626 static struct demangle_component *
d_ctor_dtor_name(struct d_info * di)1627 d_ctor_dtor_name (struct d_info *di)
1628 {
1629 if (di->last_name != NULL)
1630 {
1631 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1632 di->expansion += di->last_name->u.s_name.len;
1633 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1634 di->expansion += di->last_name->u.s_string.len;
1635 }
1636 switch (d_next_char (di))
1637 {
1638 case 'C':
1639 {
1640 enum gnu_v3_ctor_kinds kind;
1641
1642 switch (d_next_char (di))
1643 {
1644 case '1':
1645 kind = gnu_v3_complete_object_ctor;
1646 break;
1647 case '2':
1648 kind = gnu_v3_base_object_ctor;
1649 break;
1650 case '3':
1651 kind = gnu_v3_complete_object_allocating_ctor;
1652 break;
1653 default:
1654 return NULL;
1655 }
1656 return d_make_ctor (di, kind, di->last_name);
1657 }
1658
1659 case 'D':
1660 {
1661 enum gnu_v3_dtor_kinds kind;
1662
1663 switch (d_next_char (di))
1664 {
1665 case '0':
1666 kind = gnu_v3_deleting_dtor;
1667 break;
1668 case '1':
1669 kind = gnu_v3_complete_object_dtor;
1670 break;
1671 case '2':
1672 kind = gnu_v3_base_object_dtor;
1673 break;
1674 default:
1675 return NULL;
1676 }
1677 return d_make_dtor (di, kind, di->last_name);
1678 }
1679
1680 default:
1681 return NULL;
1682 }
1683 }
1684
1685 /* <type> ::= <builtin-type>
1686 ::= <function-type>
1687 ::= <class-enum-type>
1688 ::= <array-type>
1689 ::= <pointer-to-member-type>
1690 ::= <template-param>
1691 ::= <template-template-param> <template-args>
1692 ::= <substitution>
1693 ::= <CV-qualifiers> <type>
1694 ::= P <type>
1695 ::= R <type>
1696 ::= C <type>
1697 ::= G <type>
1698 ::= U <source-name> <type>
1699
1700 <builtin-type> ::= various one letter codes
1701 ::= u <source-name>
1702 */
1703
1704 CP_STATIC_IF_GLIBCPP_V3
1705 const struct demangle_builtin_type_info
1706 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1707 {
1708 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1709 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1710 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1711 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1712 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1713 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1714 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1715 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1716 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1717 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1718 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1719 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1720 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1721 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1722 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1723 D_PRINT_DEFAULT },
1724 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1725 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1726 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1727 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1728 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1729 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1730 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1731 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1732 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1733 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1734 D_PRINT_UNSIGNED_LONG_LONG },
1735 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1736 };
1737
1738 CP_STATIC_IF_GLIBCPP_V3
1739 struct demangle_component *
cplus_demangle_type(struct d_info * di)1740 cplus_demangle_type (struct d_info *di)
1741 {
1742 char peek;
1743 struct demangle_component *ret;
1744 int can_subst;
1745
1746 /* The ABI specifies that when CV-qualifiers are used, the base type
1747 is substitutable, and the fully qualified type is substitutable,
1748 but the base type with a strict subset of the CV-qualifiers is
1749 not substitutable. The natural recursive implementation of the
1750 CV-qualifiers would cause subsets to be substitutable, so instead
1751 we pull them all off now.
1752
1753 FIXME: The ABI says that order-insensitive vendor qualifiers
1754 should be handled in the same way, but we have no way to tell
1755 which vendor qualifiers are order-insensitive and which are
1756 order-sensitive. So we just assume that they are all
1757 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1758 __vector, and it treats it as order-sensitive when mangling
1759 names. */
1760
1761 peek = d_peek_char (di);
1762 if (peek == 'r' || peek == 'V' || peek == 'K')
1763 {
1764 struct demangle_component **pret;
1765
1766 pret = d_cv_qualifiers (di, &ret, 0);
1767 if (pret == NULL)
1768 return NULL;
1769 *pret = cplus_demangle_type (di);
1770 if (! d_add_substitution (di, ret))
1771 return NULL;
1772 return ret;
1773 }
1774
1775 can_subst = 1;
1776
1777 switch (peek)
1778 {
1779 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1780 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1781 case 'o': case 's': case 't':
1782 case 'v': case 'w': case 'x': case 'y': case 'z':
1783 ret = d_make_builtin_type (di,
1784 &cplus_demangle_builtin_types[peek - 'a']);
1785 di->expansion += ret->u.s_builtin.type->len;
1786 can_subst = 0;
1787 d_advance (di, 1);
1788 break;
1789
1790 case 'u':
1791 d_advance (di, 1);
1792 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1793 d_source_name (di), NULL);
1794 break;
1795
1796 case 'F':
1797 ret = d_function_type (di);
1798 break;
1799
1800 case '0': case '1': case '2': case '3': case '4':
1801 case '5': case '6': case '7': case '8': case '9':
1802 case 'N':
1803 case 'Z':
1804 ret = d_class_enum_type (di);
1805 break;
1806
1807 case 'A':
1808 ret = d_array_type (di);
1809 break;
1810
1811 case 'M':
1812 ret = d_pointer_to_member_type (di);
1813 break;
1814
1815 case 'T':
1816 ret = d_template_param (di);
1817 if (d_peek_char (di) == 'I')
1818 {
1819 /* This is <template-template-param> <template-args>. The
1820 <template-template-param> part is a substitution
1821 candidate. */
1822 if (! d_add_substitution (di, ret))
1823 return NULL;
1824 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1825 d_template_args (di));
1826 }
1827 break;
1828
1829 case 'S':
1830 /* If this is a special substitution, then it is the start of
1831 <class-enum-type>. */
1832 {
1833 char peek_next;
1834
1835 peek_next = d_peek_next_char (di);
1836 if (IS_DIGIT (peek_next)
1837 || peek_next == '_'
1838 || IS_UPPER (peek_next))
1839 {
1840 ret = d_substitution (di, 0);
1841 /* The substituted name may have been a template name and
1842 may be followed by tepmlate args. */
1843 if (d_peek_char (di) == 'I')
1844 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1845 d_template_args (di));
1846 else
1847 can_subst = 0;
1848 }
1849 else
1850 {
1851 ret = d_class_enum_type (di);
1852 /* If the substitution was a complete type, then it is not
1853 a new substitution candidate. However, if the
1854 substitution was followed by template arguments, then
1855 the whole thing is a substitution candidate. */
1856 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1857 can_subst = 0;
1858 }
1859 }
1860 break;
1861
1862 case 'P':
1863 d_advance (di, 1);
1864 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1865 cplus_demangle_type (di), NULL);
1866 break;
1867
1868 case 'R':
1869 d_advance (di, 1);
1870 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1871 cplus_demangle_type (di), NULL);
1872 break;
1873
1874 case 'C':
1875 d_advance (di, 1);
1876 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1877 cplus_demangle_type (di), NULL);
1878 break;
1879
1880 case 'G':
1881 d_advance (di, 1);
1882 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1883 cplus_demangle_type (di), NULL);
1884 break;
1885
1886 case 'U':
1887 d_advance (di, 1);
1888 ret = d_source_name (di);
1889 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1890 cplus_demangle_type (di), ret);
1891 break;
1892
1893 default:
1894 return NULL;
1895 }
1896
1897 if (can_subst)
1898 {
1899 if (! d_add_substitution (di, ret))
1900 return NULL;
1901 }
1902
1903 return ret;
1904 }
1905
1906 /* <CV-qualifiers> ::= [r] [V] [K] */
1907
1908 static struct demangle_component **
d_cv_qualifiers(struct d_info * di,struct demangle_component ** pret,int member_fn)1909 d_cv_qualifiers (struct d_info *di,
1910 struct demangle_component **pret, int member_fn)
1911 {
1912 char peek;
1913
1914 peek = d_peek_char (di);
1915 while (peek == 'r' || peek == 'V' || peek == 'K')
1916 {
1917 enum demangle_component_type t;
1918
1919 d_advance (di, 1);
1920 if (peek == 'r')
1921 {
1922 t = (member_fn
1923 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1924 : DEMANGLE_COMPONENT_RESTRICT);
1925 di->expansion += sizeof "restrict";
1926 }
1927 else if (peek == 'V')
1928 {
1929 t = (member_fn
1930 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1931 : DEMANGLE_COMPONENT_VOLATILE);
1932 di->expansion += sizeof "volatile";
1933 }
1934 else
1935 {
1936 t = (member_fn
1937 ? DEMANGLE_COMPONENT_CONST_THIS
1938 : DEMANGLE_COMPONENT_CONST);
1939 di->expansion += sizeof "const";
1940 }
1941
1942 *pret = d_make_comp (di, t, NULL, NULL);
1943 if (*pret == NULL)
1944 return NULL;
1945 pret = &d_left (*pret);
1946
1947 peek = d_peek_char (di);
1948 }
1949
1950 return pret;
1951 }
1952
1953 /* <function-type> ::= F [Y] <bare-function-type> E */
1954
1955 static struct demangle_component *
d_function_type(struct d_info * di)1956 d_function_type (struct d_info *di)
1957 {
1958 struct demangle_component *ret;
1959
1960 if (d_next_char (di) != 'F')
1961 return NULL;
1962 if (d_peek_char (di) == 'Y')
1963 {
1964 /* Function has C linkage. We don't print this information.
1965 FIXME: We should print it in verbose mode. */
1966 d_advance (di, 1);
1967 }
1968 ret = d_bare_function_type (di, 1);
1969 if (d_next_char (di) != 'E')
1970 return NULL;
1971 return ret;
1972 }
1973
1974 /* <bare-function-type> ::= [J]<type>+ */
1975
1976 static struct demangle_component *
d_bare_function_type(struct d_info * di,int has_return_type)1977 d_bare_function_type (struct d_info *di, int has_return_type)
1978 {
1979 struct demangle_component *return_type;
1980 struct demangle_component *tl;
1981 struct demangle_component **ptl;
1982 char peek;
1983
1984 /* Detect special qualifier indicating that the first argument
1985 is the return type. */
1986 peek = d_peek_char (di);
1987 if (peek == 'J')
1988 {
1989 d_advance (di, 1);
1990 has_return_type = 1;
1991 }
1992
1993 return_type = NULL;
1994 tl = NULL;
1995 ptl = &tl;
1996 while (1)
1997 {
1998 struct demangle_component *type;
1999
2000 peek = d_peek_char (di);
2001 if (peek == '\0' || peek == 'E')
2002 break;
2003 type = cplus_demangle_type (di);
2004 if (type == NULL)
2005 return NULL;
2006 if (has_return_type)
2007 {
2008 return_type = type;
2009 has_return_type = 0;
2010 }
2011 else
2012 {
2013 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2014 if (*ptl == NULL)
2015 return NULL;
2016 ptl = &d_right (*ptl);
2017 }
2018 }
2019
2020 /* There should be at least one parameter type besides the optional
2021 return type. A function which takes no arguments will have a
2022 single parameter type void. */
2023 if (tl == NULL)
2024 return NULL;
2025
2026 /* If we have a single parameter type void, omit it. */
2027 if (d_right (tl) == NULL
2028 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2029 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2030 {
2031 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2032 tl = NULL;
2033 }
2034
2035 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2036 }
2037
2038 /* <class-enum-type> ::= <name> */
2039
2040 static struct demangle_component *
d_class_enum_type(struct d_info * di)2041 d_class_enum_type (struct d_info *di)
2042 {
2043 return d_name (di);
2044 }
2045
2046 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2047 ::= A [<(dimension) expression>] _ <(element) type>
2048 */
2049
2050 static struct demangle_component *
d_array_type(struct d_info * di)2051 d_array_type (struct d_info *di)
2052 {
2053 char peek;
2054 struct demangle_component *dim;
2055
2056 if (d_next_char (di) != 'A')
2057 return NULL;
2058
2059 peek = d_peek_char (di);
2060 if (peek == '_')
2061 dim = NULL;
2062 else if (IS_DIGIT (peek))
2063 {
2064 const char *s;
2065
2066 s = d_str (di);
2067 do
2068 {
2069 d_advance (di, 1);
2070 peek = d_peek_char (di);
2071 }
2072 while (IS_DIGIT (peek));
2073 dim = d_make_name (di, s, d_str (di) - s);
2074 if (dim == NULL)
2075 return NULL;
2076 }
2077 else
2078 {
2079 dim = d_expression (di);
2080 if (dim == NULL)
2081 return NULL;
2082 }
2083
2084 if (d_next_char (di) != '_')
2085 return NULL;
2086
2087 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2088 cplus_demangle_type (di));
2089 }
2090
2091 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2092
2093 static struct demangle_component *
d_pointer_to_member_type(struct d_info * di)2094 d_pointer_to_member_type (struct d_info *di)
2095 {
2096 struct demangle_component *cl;
2097 struct demangle_component *mem;
2098 struct demangle_component **pmem;
2099
2100 if (d_next_char (di) != 'M')
2101 return NULL;
2102
2103 cl = cplus_demangle_type (di);
2104
2105 /* The ABI specifies that any type can be a substitution source, and
2106 that M is followed by two types, and that when a CV-qualified
2107 type is seen both the base type and the CV-qualified types are
2108 substitution sources. The ABI also specifies that for a pointer
2109 to a CV-qualified member function, the qualifiers are attached to
2110 the second type. Given the grammar, a plain reading of the ABI
2111 suggests that both the CV-qualified member function and the
2112 non-qualified member function are substitution sources. However,
2113 g++ does not work that way. g++ treats only the CV-qualified
2114 member function as a substitution source. FIXME. So to work
2115 with g++, we need to pull off the CV-qualifiers here, in order to
2116 avoid calling add_substitution() in cplus_demangle_type(). But
2117 for a CV-qualified member which is not a function, g++ does
2118 follow the ABI, so we need to handle that case here by calling
2119 d_add_substitution ourselves. */
2120
2121 pmem = d_cv_qualifiers (di, &mem, 1);
2122 if (pmem == NULL)
2123 return NULL;
2124 *pmem = cplus_demangle_type (di);
2125
2126 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2127 {
2128 if (! d_add_substitution (di, mem))
2129 return NULL;
2130 }
2131
2132 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2133 }
2134
2135 /* <template-param> ::= T_
2136 ::= T <(parameter-2 non-negative) number> _
2137 */
2138
2139 static struct demangle_component *
d_template_param(struct d_info * di)2140 d_template_param (struct d_info *di)
2141 {
2142 long param;
2143
2144 if (d_next_char (di) != 'T')
2145 return NULL;
2146
2147 if (d_peek_char (di) == '_')
2148 param = 0;
2149 else
2150 {
2151 param = d_number (di);
2152 if (param < 0)
2153 return NULL;
2154 param += 1;
2155 }
2156
2157 if (d_next_char (di) != '_')
2158 return NULL;
2159
2160 ++di->did_subs;
2161
2162 return d_make_template_param (di, param);
2163 }
2164
2165 /* <template-args> ::= I <template-arg>+ E */
2166
2167 static struct demangle_component *
d_template_args(struct d_info * di)2168 d_template_args (struct d_info *di)
2169 {
2170 struct demangle_component *hold_last_name;
2171 struct demangle_component *al;
2172 struct demangle_component **pal;
2173
2174 /* Preserve the last name we saw--don't let the template arguments
2175 clobber it, as that would give us the wrong name for a subsequent
2176 constructor or destructor. */
2177 hold_last_name = di->last_name;
2178
2179 if (d_next_char (di) != 'I')
2180 return NULL;
2181
2182 al = NULL;
2183 pal = &al;
2184 while (1)
2185 {
2186 struct demangle_component *a;
2187
2188 a = d_template_arg (di);
2189 if (a == NULL)
2190 return NULL;
2191
2192 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2193 if (*pal == NULL)
2194 return NULL;
2195 pal = &d_right (*pal);
2196
2197 if (d_peek_char (di) == 'E')
2198 {
2199 d_advance (di, 1);
2200 break;
2201 }
2202 }
2203
2204 di->last_name = hold_last_name;
2205
2206 return al;
2207 }
2208
2209 /* <template-arg> ::= <type>
2210 ::= X <expression> E
2211 ::= <expr-primary>
2212 */
2213
2214 static struct demangle_component *
d_template_arg(struct d_info * di)2215 d_template_arg (struct d_info *di)
2216 {
2217 struct demangle_component *ret;
2218
2219 switch (d_peek_char (di))
2220 {
2221 case 'X':
2222 d_advance (di, 1);
2223 ret = d_expression (di);
2224 if (d_next_char (di) != 'E')
2225 return NULL;
2226 return ret;
2227
2228 case 'L':
2229 return d_expr_primary (di);
2230
2231 default:
2232 return cplus_demangle_type (di);
2233 }
2234 }
2235
2236 /* <expression> ::= <(unary) operator-name> <expression>
2237 ::= <(binary) operator-name> <expression> <expression>
2238 ::= <(trinary) operator-name> <expression> <expression> <expression>
2239 ::= st <type>
2240 ::= <template-param>
2241 ::= sr <type> <unqualified-name>
2242 ::= sr <type> <unqualified-name> <template-args>
2243 ::= <expr-primary>
2244 */
2245
2246 static struct demangle_component *
d_expression(struct d_info * di)2247 d_expression (struct d_info *di)
2248 {
2249 char peek;
2250
2251 peek = d_peek_char (di);
2252 if (peek == 'L')
2253 return d_expr_primary (di);
2254 else if (peek == 'T')
2255 return d_template_param (di);
2256 else if (peek == 's' && d_peek_next_char (di) == 'r')
2257 {
2258 struct demangle_component *type;
2259 struct demangle_component *name;
2260
2261 d_advance (di, 2);
2262 type = cplus_demangle_type (di);
2263 name = d_unqualified_name (di);
2264 if (d_peek_char (di) != 'I')
2265 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2266 else
2267 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2268 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2269 d_template_args (di)));
2270 }
2271 else
2272 {
2273 struct demangle_component *op;
2274 int args;
2275
2276 op = d_operator_name (di);
2277 if (op == NULL)
2278 return NULL;
2279
2280 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2281 di->expansion += op->u.s_operator.op->len - 2;
2282
2283 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2284 && strcmp (op->u.s_operator.op->code, "st") == 0)
2285 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2286 cplus_demangle_type (di));
2287
2288 switch (op->type)
2289 {
2290 default:
2291 return NULL;
2292 case DEMANGLE_COMPONENT_OPERATOR:
2293 args = op->u.s_operator.op->args;
2294 break;
2295 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2296 args = op->u.s_extended_operator.args;
2297 break;
2298 case DEMANGLE_COMPONENT_CAST:
2299 args = 1;
2300 break;
2301 }
2302
2303 switch (args)
2304 {
2305 case 1:
2306 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2307 d_expression (di));
2308 case 2:
2309 {
2310 struct demangle_component *left;
2311
2312 left = d_expression (di);
2313 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2314 d_make_comp (di,
2315 DEMANGLE_COMPONENT_BINARY_ARGS,
2316 left,
2317 d_expression (di)));
2318 }
2319 case 3:
2320 {
2321 struct demangle_component *first;
2322 struct demangle_component *second;
2323
2324 first = d_expression (di);
2325 second = d_expression (di);
2326 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2327 d_make_comp (di,
2328 DEMANGLE_COMPONENT_TRINARY_ARG1,
2329 first,
2330 d_make_comp (di,
2331 DEMANGLE_COMPONENT_TRINARY_ARG2,
2332 second,
2333 d_expression (di))));
2334 }
2335 default:
2336 return NULL;
2337 }
2338 }
2339 }
2340
2341 /* <expr-primary> ::= L <type> <(value) number> E
2342 ::= L <type> <(value) float> E
2343 ::= L <mangled-name> E
2344 */
2345
2346 static struct demangle_component *
d_expr_primary(struct d_info * di)2347 d_expr_primary (struct d_info *di)
2348 {
2349 struct demangle_component *ret;
2350
2351 if (d_next_char (di) != 'L')
2352 return NULL;
2353 if (d_peek_char (di) == '_')
2354 ret = cplus_demangle_mangled_name (di, 0);
2355 else
2356 {
2357 struct demangle_component *type;
2358 enum demangle_component_type t;
2359 const char *s;
2360
2361 type = cplus_demangle_type (di);
2362 if (type == NULL)
2363 return NULL;
2364
2365 /* If we have a type we know how to print, we aren't going to
2366 print the type name itself. */
2367 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2368 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2369 di->expansion -= type->u.s_builtin.type->len;
2370
2371 /* Rather than try to interpret the literal value, we just
2372 collect it as a string. Note that it's possible to have a
2373 floating point literal here. The ABI specifies that the
2374 format of such literals is machine independent. That's fine,
2375 but what's not fine is that versions of g++ up to 3.2 with
2376 -fabi-version=1 used upper case letters in the hex constant,
2377 and dumped out gcc's internal representation. That makes it
2378 hard to tell where the constant ends, and hard to dump the
2379 constant in any readable form anyhow. We don't attempt to
2380 handle these cases. */
2381
2382 t = DEMANGLE_COMPONENT_LITERAL;
2383 if (d_peek_char (di) == 'n')
2384 {
2385 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2386 d_advance (di, 1);
2387 }
2388 s = d_str (di);
2389 while (d_peek_char (di) != 'E')
2390 {
2391 if (d_peek_char (di) == '\0')
2392 return NULL;
2393 d_advance (di, 1);
2394 }
2395 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2396 }
2397 if (d_next_char (di) != 'E')
2398 return NULL;
2399 return ret;
2400 }
2401
2402 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2403 ::= Z <(function) encoding> E s [<discriminator>]
2404 */
2405
2406 static struct demangle_component *
d_local_name(struct d_info * di)2407 d_local_name (struct d_info *di)
2408 {
2409 struct demangle_component *function;
2410
2411 if (d_next_char (di) != 'Z')
2412 return NULL;
2413
2414 function = d_encoding (di, 0);
2415
2416 if (d_next_char (di) != 'E')
2417 return NULL;
2418
2419 if (d_peek_char (di) == 's')
2420 {
2421 d_advance (di, 1);
2422 if (! d_discriminator (di))
2423 return NULL;
2424 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2425 d_make_name (di, "string literal",
2426 sizeof "string literal" - 1));
2427 }
2428 else
2429 {
2430 struct demangle_component *name;
2431
2432 name = d_name (di);
2433 if (! d_discriminator (di))
2434 return NULL;
2435 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2436 }
2437 }
2438
2439 /* <discriminator> ::= _ <(non-negative) number>
2440
2441 We demangle the discriminator, but we don't print it out. FIXME:
2442 We should print it out in verbose mode. */
2443
2444 static int
d_discriminator(struct d_info * di)2445 d_discriminator (struct d_info *di)
2446 {
2447 long discrim;
2448
2449 if (d_peek_char (di) != '_')
2450 return 1;
2451 d_advance (di, 1);
2452 discrim = d_number (di);
2453 if (discrim < 0)
2454 return 0;
2455 return 1;
2456 }
2457
2458 /* Add a new substitution. */
2459
2460 static int
d_add_substitution(struct d_info * di,struct demangle_component * dc)2461 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2462 {
2463 if (dc == NULL)
2464 return 0;
2465 if (di->next_sub >= di->num_subs)
2466 return 0;
2467 di->subs[di->next_sub] = dc;
2468 ++di->next_sub;
2469 return 1;
2470 }
2471
2472 /* <substitution> ::= S <seq-id> _
2473 ::= S_
2474 ::= St
2475 ::= Sa
2476 ::= Sb
2477 ::= Ss
2478 ::= Si
2479 ::= So
2480 ::= Sd
2481
2482 If PREFIX is non-zero, then this type is being used as a prefix in
2483 a qualified name. In this case, for the standard substitutions, we
2484 need to check whether we are being used as a prefix for a
2485 constructor or destructor, and return a full template name.
2486 Otherwise we will get something like std::iostream::~iostream()
2487 which does not correspond particularly well to any function which
2488 actually appears in the source.
2489 */
2490
2491 static const struct d_standard_sub_info standard_subs[] =
2492 {
2493 { 't', NL ("std"),
2494 NL ("std"),
2495 NULL, 0 },
2496 { 'a', NL ("std::allocator"),
2497 NL ("std::allocator"),
2498 NL ("allocator") },
2499 { 'b', NL ("std::basic_string"),
2500 NL ("std::basic_string"),
2501 NL ("basic_string") },
2502 { 's', NL ("std::string"),
2503 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2504 NL ("basic_string") },
2505 { 'i', NL ("std::istream"),
2506 NL ("std::basic_istream<char, std::char_traits<char> >"),
2507 NL ("basic_istream") },
2508 { 'o', NL ("std::ostream"),
2509 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2510 NL ("basic_ostream") },
2511 { 'd', NL ("std::iostream"),
2512 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2513 NL ("basic_iostream") }
2514 };
2515
2516 static struct demangle_component *
d_substitution(struct d_info * di,int prefix)2517 d_substitution (struct d_info *di, int prefix)
2518 {
2519 char c;
2520
2521 if (d_next_char (di) != 'S')
2522 return NULL;
2523
2524 c = d_next_char (di);
2525 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2526 {
2527 int id;
2528
2529 id = 0;
2530 if (c != '_')
2531 {
2532 do
2533 {
2534 if (IS_DIGIT (c))
2535 id = id * 36 + c - '0';
2536 else if (IS_UPPER (c))
2537 id = id * 36 + c - 'A' + 10;
2538 else
2539 return NULL;
2540 if (id < 0)
2541 return NULL;
2542 c = d_next_char (di);
2543 }
2544 while (c != '_');
2545
2546 ++id;
2547 }
2548
2549 if (id >= di->next_sub)
2550 return NULL;
2551
2552 ++di->did_subs;
2553
2554 return di->subs[id];
2555 }
2556 else
2557 {
2558 int verbose;
2559 const struct d_standard_sub_info *p;
2560 const struct d_standard_sub_info *pend;
2561
2562 verbose = (di->options & DMGL_VERBOSE) != 0;
2563 if (! verbose && prefix)
2564 {
2565 char peek;
2566
2567 peek = d_peek_char (di);
2568 if (peek == 'C' || peek == 'D')
2569 verbose = 1;
2570 }
2571
2572 pend = (&standard_subs[0]
2573 + sizeof standard_subs / sizeof standard_subs[0]);
2574 for (p = &standard_subs[0]; p < pend; ++p)
2575 {
2576 if (c == p->code)
2577 {
2578 const char *s;
2579 int len;
2580
2581 if (p->set_last_name != NULL)
2582 di->last_name = d_make_sub (di, p->set_last_name,
2583 p->set_last_name_len);
2584 if (verbose)
2585 {
2586 s = p->full_expansion;
2587 len = p->full_len;
2588 }
2589 else
2590 {
2591 s = p->simple_expansion;
2592 len = p->simple_len;
2593 }
2594 di->expansion += len;
2595 return d_make_sub (di, s, len);
2596 }
2597 }
2598
2599 return NULL;
2600 }
2601 }
2602
2603 /* Resize the print buffer. */
2604
2605 static void
d_print_resize(struct d_print_info * dpi,size_t add)2606 d_print_resize (struct d_print_info *dpi, size_t add)
2607 {
2608 size_t need;
2609
2610 if (dpi->buf == NULL)
2611 return;
2612 need = dpi->len + add;
2613 while (need > dpi->alc)
2614 {
2615 size_t newalc;
2616 char *newbuf;
2617
2618 newalc = dpi->alc * 2;
2619 newbuf = (char *) realloc (dpi->buf, newalc);
2620 if (newbuf == NULL)
2621 {
2622 free (dpi->buf);
2623 dpi->buf = NULL;
2624 dpi->allocation_failure = 1;
2625 return;
2626 }
2627 dpi->buf = newbuf;
2628 dpi->alc = newalc;
2629 }
2630 }
2631
2632 /* Append a character to the print buffer. */
2633
2634 static void
d_print_append_char(struct d_print_info * dpi,int c)2635 d_print_append_char (struct d_print_info *dpi, int c)
2636 {
2637 if (dpi->buf != NULL)
2638 {
2639 if (dpi->len >= dpi->alc)
2640 {
2641 d_print_resize (dpi, 1);
2642 if (dpi->buf == NULL)
2643 return;
2644 }
2645
2646 dpi->buf[dpi->len] = c;
2647 ++dpi->len;
2648 }
2649 }
2650
2651 /* Append a buffer to the print buffer. */
2652
2653 static void
d_print_append_buffer(struct d_print_info * dpi,const char * s,size_t l)2654 d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2655 {
2656 if (dpi->buf != NULL)
2657 {
2658 if (dpi->len + l > dpi->alc)
2659 {
2660 d_print_resize (dpi, l);
2661 if (dpi->buf == NULL)
2662 return;
2663 }
2664
2665 memcpy (dpi->buf + dpi->len, s, l);
2666 dpi->len += l;
2667 }
2668 }
2669
2670 /* Indicate that an error occurred during printing. */
2671
2672 static void
d_print_error(struct d_print_info * dpi)2673 d_print_error (struct d_print_info *dpi)
2674 {
2675 free (dpi->buf);
2676 dpi->buf = NULL;
2677 }
2678
2679 /* Turn components into a human readable string. OPTIONS is the
2680 options bits passed to the demangler. DC is the tree to print.
2681 ESTIMATE is a guess at the length of the result. This returns a
2682 string allocated by malloc, or NULL on error. On success, this
2683 sets *PALC to the size of the allocated buffer. On failure, this
2684 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2685 failure. */
2686
2687 CP_STATIC_IF_GLIBCPP_V3
2688 char *
cplus_demangle_print(int options,const struct demangle_component * dc,int estimate,size_t * palc)2689 cplus_demangle_print (int options, const struct demangle_component *dc,
2690 int estimate, size_t *palc)
2691 {
2692 struct d_print_info dpi;
2693
2694 dpi.options = options;
2695
2696 dpi.alc = estimate + 1;
2697 dpi.buf = (char *) malloc (dpi.alc);
2698 if (dpi.buf == NULL)
2699 {
2700 *palc = 1;
2701 return NULL;
2702 }
2703
2704 dpi.len = 0;
2705 dpi.templates = NULL;
2706 dpi.modifiers = NULL;
2707
2708 dpi.allocation_failure = 0;
2709
2710 d_print_comp (&dpi, dc);
2711
2712 d_append_char (&dpi, '\0');
2713
2714 if (dpi.buf != NULL)
2715 *palc = dpi.alc;
2716 else
2717 *palc = dpi.allocation_failure;
2718
2719 return dpi.buf;
2720 }
2721
2722 /* Subroutine to handle components. */
2723
2724 static void
d_print_comp(struct d_print_info * dpi,const struct demangle_component * dc)2725 d_print_comp (struct d_print_info *dpi,
2726 const struct demangle_component *dc)
2727 {
2728 if (dc == NULL)
2729 {
2730 d_print_error (dpi);
2731 return;
2732 }
2733 if (d_print_saw_error (dpi))
2734 return;
2735
2736 switch (dc->type)
2737 {
2738 case DEMANGLE_COMPONENT_NAME:
2739 if ((dpi->options & DMGL_JAVA) == 0)
2740 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2741 else
2742 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2743 return;
2744
2745 case DEMANGLE_COMPONENT_QUAL_NAME:
2746 case DEMANGLE_COMPONENT_LOCAL_NAME:
2747 d_print_comp (dpi, d_left (dc));
2748 if ((dpi->options & DMGL_JAVA) == 0)
2749 d_append_string_constant (dpi, "::");
2750 else
2751 d_append_char (dpi, '.');
2752 d_print_comp (dpi, d_right (dc));
2753 return;
2754
2755 case DEMANGLE_COMPONENT_TYPED_NAME:
2756 {
2757 struct d_print_mod *hold_modifiers;
2758 struct demangle_component *typed_name;
2759 struct d_print_mod adpm[4];
2760 unsigned int i;
2761 struct d_print_template dpt;
2762
2763 /* Pass the name down to the type so that it can be printed in
2764 the right place for the type. We also have to pass down
2765 any CV-qualifiers, which apply to the this parameter. */
2766 hold_modifiers = dpi->modifiers;
2767 i = 0;
2768 typed_name = d_left (dc);
2769 while (typed_name != NULL)
2770 {
2771 if (i >= sizeof adpm / sizeof adpm[0])
2772 {
2773 d_print_error (dpi);
2774 return;
2775 }
2776
2777 adpm[i].next = dpi->modifiers;
2778 dpi->modifiers = &adpm[i];
2779 adpm[i].mod = typed_name;
2780 adpm[i].printed = 0;
2781 adpm[i].templates = dpi->templates;
2782 ++i;
2783
2784 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2785 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2786 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2787 break;
2788
2789 typed_name = d_left (typed_name);
2790 }
2791
2792 /* If typed_name is a template, then it applies to the
2793 function type as well. */
2794 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2795 {
2796 dpt.next = dpi->templates;
2797 dpi->templates = &dpt;
2798 dpt.template_decl = typed_name;
2799 }
2800
2801 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2802 there may be CV-qualifiers on its right argument which
2803 really apply here; this happens when parsing a class which
2804 is local to a function. */
2805 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2806 {
2807 struct demangle_component *local_name;
2808
2809 local_name = d_right (typed_name);
2810 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2811 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2812 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2813 {
2814 if (i >= sizeof adpm / sizeof adpm[0])
2815 {
2816 d_print_error (dpi);
2817 return;
2818 }
2819
2820 adpm[i] = adpm[i - 1];
2821 adpm[i].next = &adpm[i - 1];
2822 dpi->modifiers = &adpm[i];
2823
2824 adpm[i - 1].mod = local_name;
2825 adpm[i - 1].printed = 0;
2826 adpm[i - 1].templates = dpi->templates;
2827 ++i;
2828
2829 local_name = d_left (local_name);
2830 }
2831 }
2832
2833 d_print_comp (dpi, d_right (dc));
2834
2835 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2836 dpi->templates = dpt.next;
2837
2838 /* If the modifiers didn't get printed by the type, print them
2839 now. */
2840 while (i > 0)
2841 {
2842 --i;
2843 if (! adpm[i].printed)
2844 {
2845 d_append_char (dpi, ' ');
2846 d_print_mod (dpi, adpm[i].mod);
2847 }
2848 }
2849
2850 dpi->modifiers = hold_modifiers;
2851
2852 return;
2853 }
2854
2855 case DEMANGLE_COMPONENT_TEMPLATE:
2856 {
2857 struct d_print_mod *hold_dpm;
2858
2859 /* Don't push modifiers into a template definition. Doing so
2860 could give the wrong definition for a template argument.
2861 Instead, treat the template essentially as a name. */
2862
2863 hold_dpm = dpi->modifiers;
2864 dpi->modifiers = NULL;
2865
2866 d_print_comp (dpi, d_left (dc));
2867 if (d_last_char (dpi) == '<')
2868 d_append_char (dpi, ' ');
2869 d_append_char (dpi, '<');
2870 d_print_comp (dpi, d_right (dc));
2871 /* Avoid generating two consecutive '>' characters, to avoid
2872 the C++ syntactic ambiguity. */
2873 if (d_last_char (dpi) == '>')
2874 d_append_char (dpi, ' ');
2875 d_append_char (dpi, '>');
2876
2877 dpi->modifiers = hold_dpm;
2878
2879 return;
2880 }
2881
2882 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2883 {
2884 long i;
2885 struct demangle_component *a;
2886 struct d_print_template *hold_dpt;
2887
2888 if (dpi->templates == NULL)
2889 {
2890 d_print_error (dpi);
2891 return;
2892 }
2893 i = dc->u.s_number.number;
2894 for (a = d_right (dpi->templates->template_decl);
2895 a != NULL;
2896 a = d_right (a))
2897 {
2898 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2899 {
2900 d_print_error (dpi);
2901 return;
2902 }
2903 if (i <= 0)
2904 break;
2905 --i;
2906 }
2907 if (i != 0 || a == NULL)
2908 {
2909 d_print_error (dpi);
2910 return;
2911 }
2912
2913 /* While processing this parameter, we need to pop the list of
2914 templates. This is because the template parameter may
2915 itself be a reference to a parameter of an outer
2916 template. */
2917
2918 hold_dpt = dpi->templates;
2919 dpi->templates = hold_dpt->next;
2920
2921 d_print_comp (dpi, d_left (a));
2922
2923 dpi->templates = hold_dpt;
2924
2925 return;
2926 }
2927
2928 case DEMANGLE_COMPONENT_CTOR:
2929 d_print_comp (dpi, dc->u.s_ctor.name);
2930 return;
2931
2932 case DEMANGLE_COMPONENT_DTOR:
2933 d_append_char (dpi, '~');
2934 d_print_comp (dpi, dc->u.s_dtor.name);
2935 return;
2936
2937 case DEMANGLE_COMPONENT_VTABLE:
2938 d_append_string_constant (dpi, "vtable for ");
2939 d_print_comp (dpi, d_left (dc));
2940 return;
2941
2942 case DEMANGLE_COMPONENT_VTT:
2943 d_append_string_constant (dpi, "VTT for ");
2944 d_print_comp (dpi, d_left (dc));
2945 return;
2946
2947 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2948 d_append_string_constant (dpi, "construction vtable for ");
2949 d_print_comp (dpi, d_left (dc));
2950 d_append_string_constant (dpi, "-in-");
2951 d_print_comp (dpi, d_right (dc));
2952 return;
2953
2954 case DEMANGLE_COMPONENT_TYPEINFO:
2955 d_append_string_constant (dpi, "typeinfo for ");
2956 d_print_comp (dpi, d_left (dc));
2957 return;
2958
2959 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
2960 d_append_string_constant (dpi, "typeinfo name for ");
2961 d_print_comp (dpi, d_left (dc));
2962 return;
2963
2964 case DEMANGLE_COMPONENT_TYPEINFO_FN:
2965 d_append_string_constant (dpi, "typeinfo fn for ");
2966 d_print_comp (dpi, d_left (dc));
2967 return;
2968
2969 case DEMANGLE_COMPONENT_THUNK:
2970 d_append_string_constant (dpi, "non-virtual thunk to ");
2971 d_print_comp (dpi, d_left (dc));
2972 return;
2973
2974 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
2975 d_append_string_constant (dpi, "virtual thunk to ");
2976 d_print_comp (dpi, d_left (dc));
2977 return;
2978
2979 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
2980 d_append_string_constant (dpi, "covariant return thunk to ");
2981 d_print_comp (dpi, d_left (dc));
2982 return;
2983
2984 case DEMANGLE_COMPONENT_JAVA_CLASS:
2985 d_append_string_constant (dpi, "java Class for ");
2986 d_print_comp (dpi, d_left (dc));
2987 return;
2988
2989 case DEMANGLE_COMPONENT_GUARD:
2990 d_append_string_constant (dpi, "guard variable for ");
2991 d_print_comp (dpi, d_left (dc));
2992 return;
2993
2994 case DEMANGLE_COMPONENT_REFTEMP:
2995 d_append_string_constant (dpi, "reference temporary for ");
2996 d_print_comp (dpi, d_left (dc));
2997 return;
2998
2999 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3000 d_append_string_constant (dpi, "hidden alias for ");
3001 d_print_comp (dpi, d_left (dc));
3002 return;
3003
3004 case DEMANGLE_COMPONENT_SUB_STD:
3005 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3006 return;
3007
3008 case DEMANGLE_COMPONENT_RESTRICT:
3009 case DEMANGLE_COMPONENT_VOLATILE:
3010 case DEMANGLE_COMPONENT_CONST:
3011 {
3012 struct d_print_mod *pdpm;
3013
3014 /* When printing arrays, it's possible to have cases where the
3015 same CV-qualifier gets pushed on the stack multiple times.
3016 We only need to print it once. */
3017
3018 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3019 {
3020 if (! pdpm->printed)
3021 {
3022 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3023 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3024 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3025 break;
3026 if (pdpm->mod->type == dc->type)
3027 {
3028 d_print_comp (dpi, d_left (dc));
3029 return;
3030 }
3031 }
3032 }
3033 }
3034 /* Fall through. */
3035 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3036 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3037 case DEMANGLE_COMPONENT_CONST_THIS:
3038 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3039 case DEMANGLE_COMPONENT_POINTER:
3040 case DEMANGLE_COMPONENT_REFERENCE:
3041 case DEMANGLE_COMPONENT_COMPLEX:
3042 case DEMANGLE_COMPONENT_IMAGINARY:
3043 {
3044 /* We keep a list of modifiers on the stack. */
3045 struct d_print_mod dpm;
3046
3047 dpm.next = dpi->modifiers;
3048 dpi->modifiers = &dpm;
3049 dpm.mod = dc;
3050 dpm.printed = 0;
3051 dpm.templates = dpi->templates;
3052
3053 d_print_comp (dpi, d_left (dc));
3054
3055 /* If the modifier didn't get printed by the type, print it
3056 now. */
3057 if (! dpm.printed)
3058 d_print_mod (dpi, dc);
3059
3060 dpi->modifiers = dpm.next;
3061
3062 return;
3063 }
3064
3065 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3066 if ((dpi->options & DMGL_JAVA) == 0)
3067 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3068 dc->u.s_builtin.type->len);
3069 else
3070 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3071 dc->u.s_builtin.type->java_len);
3072 return;
3073
3074 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3075 d_print_comp (dpi, d_left (dc));
3076 return;
3077
3078 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3079 {
3080 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3081 d_print_function_type (dpi, dc, dpi->modifiers);
3082
3083 /* Print return type if present */
3084 if (d_left (dc) != NULL)
3085 {
3086 struct d_print_mod dpm;
3087
3088 /* We must pass this type down as a modifier in order to
3089 print it in the right location. */
3090 dpm.next = dpi->modifiers;
3091 dpi->modifiers = &dpm;
3092 dpm.mod = dc;
3093 dpm.printed = 0;
3094 dpm.templates = dpi->templates;
3095
3096 d_print_comp (dpi, d_left (dc));
3097
3098 dpi->modifiers = dpm.next;
3099
3100 if (dpm.printed)
3101 return;
3102
3103 /* In standard prefix notation, there is a space between the
3104 return type and the function signature. */
3105 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3106 d_append_char (dpi, ' ');
3107 }
3108
3109 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3110 d_print_function_type (dpi, dc, dpi->modifiers);
3111
3112 return;
3113 }
3114
3115 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3116 {
3117 struct d_print_mod *hold_modifiers;
3118 struct d_print_mod adpm[4];
3119 unsigned int i;
3120 struct d_print_mod *pdpm;
3121
3122 /* We must pass this type down as a modifier in order to print
3123 multi-dimensional arrays correctly. If the array itself is
3124 CV-qualified, we act as though the element type were
3125 CV-qualified. We do this by copying the modifiers down
3126 rather than fiddling pointers, so that we don't wind up
3127 with a d_print_mod higher on the stack pointing into our
3128 stack frame after we return. */
3129
3130 hold_modifiers = dpi->modifiers;
3131
3132 adpm[0].next = hold_modifiers;
3133 dpi->modifiers = &adpm[0];
3134 adpm[0].mod = dc;
3135 adpm[0].printed = 0;
3136 adpm[0].templates = dpi->templates;
3137
3138 i = 1;
3139 pdpm = hold_modifiers;
3140 while (pdpm != NULL
3141 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3142 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3143 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3144 {
3145 if (! pdpm->printed)
3146 {
3147 if (i >= sizeof adpm / sizeof adpm[0])
3148 {
3149 d_print_error (dpi);
3150 return;
3151 }
3152
3153 adpm[i] = *pdpm;
3154 adpm[i].next = dpi->modifiers;
3155 dpi->modifiers = &adpm[i];
3156 pdpm->printed = 1;
3157 ++i;
3158 }
3159
3160 pdpm = pdpm->next;
3161 }
3162
3163 d_print_comp (dpi, d_right (dc));
3164
3165 dpi->modifiers = hold_modifiers;
3166
3167 if (adpm[0].printed)
3168 return;
3169
3170 while (i > 1)
3171 {
3172 --i;
3173 d_print_mod (dpi, adpm[i].mod);
3174 }
3175
3176 d_print_array_type (dpi, dc, dpi->modifiers);
3177
3178 return;
3179 }
3180
3181 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3182 {
3183 struct d_print_mod dpm;
3184
3185 dpm.next = dpi->modifiers;
3186 dpi->modifiers = &dpm;
3187 dpm.mod = dc;
3188 dpm.printed = 0;
3189 dpm.templates = dpi->templates;
3190
3191 d_print_comp (dpi, d_right (dc));
3192
3193 /* If the modifier didn't get printed by the type, print it
3194 now. */
3195 if (! dpm.printed)
3196 {
3197 d_append_char (dpi, ' ');
3198 d_print_comp (dpi, d_left (dc));
3199 d_append_string_constant (dpi, "::*");
3200 }
3201
3202 dpi->modifiers = dpm.next;
3203
3204 return;
3205 }
3206
3207 case DEMANGLE_COMPONENT_ARGLIST:
3208 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3209 d_print_comp (dpi, d_left (dc));
3210 if (d_right (dc) != NULL)
3211 {
3212 d_append_string_constant (dpi, ", ");
3213 d_print_comp (dpi, d_right (dc));
3214 }
3215 return;
3216
3217 case DEMANGLE_COMPONENT_OPERATOR:
3218 {
3219 char c;
3220
3221 d_append_string_constant (dpi, "operator");
3222 c = dc->u.s_operator.op->name[0];
3223 if (IS_LOWER (c))
3224 d_append_char (dpi, ' ');
3225 d_append_buffer (dpi, dc->u.s_operator.op->name,
3226 dc->u.s_operator.op->len);
3227 return;
3228 }
3229
3230 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3231 d_append_string_constant (dpi, "operator ");
3232 d_print_comp (dpi, dc->u.s_extended_operator.name);
3233 return;
3234
3235 case DEMANGLE_COMPONENT_CAST:
3236 d_append_string_constant (dpi, "operator ");
3237 d_print_cast (dpi, dc);
3238 return;
3239
3240 case DEMANGLE_COMPONENT_UNARY:
3241 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3242 d_print_expr_op (dpi, d_left (dc));
3243 else
3244 {
3245 d_append_char (dpi, '(');
3246 d_print_cast (dpi, d_left (dc));
3247 d_append_char (dpi, ')');
3248 }
3249 d_append_char (dpi, '(');
3250 d_print_comp (dpi, d_right (dc));
3251 d_append_char (dpi, ')');
3252 return;
3253
3254 case DEMANGLE_COMPONENT_BINARY:
3255 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3256 {
3257 d_print_error (dpi);
3258 return;
3259 }
3260
3261 /* We wrap an expression which uses the greater-than operator in
3262 an extra layer of parens so that it does not get confused
3263 with the '>' which ends the template parameters. */
3264 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3265 && d_left (dc)->u.s_operator.op->len == 1
3266 && d_left (dc)->u.s_operator.op->name[0] == '>')
3267 d_append_char (dpi, '(');
3268
3269 d_append_char (dpi, '(');
3270 d_print_comp (dpi, d_left (d_right (dc)));
3271 d_append_string_constant (dpi, ") ");
3272 d_print_expr_op (dpi, d_left (dc));
3273 d_append_string_constant (dpi, " (");
3274 d_print_comp (dpi, d_right (d_right (dc)));
3275 d_append_char (dpi, ')');
3276
3277 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3278 && d_left (dc)->u.s_operator.op->len == 1
3279 && d_left (dc)->u.s_operator.op->name[0] == '>')
3280 d_append_char (dpi, ')');
3281
3282 return;
3283
3284 case DEMANGLE_COMPONENT_BINARY_ARGS:
3285 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3286 d_print_error (dpi);
3287 return;
3288
3289 case DEMANGLE_COMPONENT_TRINARY:
3290 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3291 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3292 {
3293 d_print_error (dpi);
3294 return;
3295 }
3296 d_append_char (dpi, '(');
3297 d_print_comp (dpi, d_left (d_right (dc)));
3298 d_append_string_constant (dpi, ") ");
3299 d_print_expr_op (dpi, d_left (dc));
3300 d_append_string_constant (dpi, " (");
3301 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3302 d_append_string_constant (dpi, ") : (");
3303 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3304 d_append_char (dpi, ')');
3305 return;
3306
3307 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3308 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3309 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3310 d_print_error (dpi);
3311 return;
3312
3313 case DEMANGLE_COMPONENT_LITERAL:
3314 case DEMANGLE_COMPONENT_LITERAL_NEG:
3315 {
3316 enum d_builtin_type_print tp;
3317
3318 /* For some builtin types, produce simpler output. */
3319 tp = D_PRINT_DEFAULT;
3320 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3321 {
3322 tp = d_left (dc)->u.s_builtin.type->print;
3323 switch (tp)
3324 {
3325 case D_PRINT_INT:
3326 case D_PRINT_UNSIGNED:
3327 case D_PRINT_LONG:
3328 case D_PRINT_UNSIGNED_LONG:
3329 case D_PRINT_LONG_LONG:
3330 case D_PRINT_UNSIGNED_LONG_LONG:
3331 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3332 {
3333 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3334 d_append_char (dpi, '-');
3335 d_print_comp (dpi, d_right (dc));
3336 switch (tp)
3337 {
3338 default:
3339 break;
3340 case D_PRINT_UNSIGNED:
3341 d_append_char (dpi, 'u');
3342 break;
3343 case D_PRINT_LONG:
3344 d_append_char (dpi, 'l');
3345 break;
3346 case D_PRINT_UNSIGNED_LONG:
3347 d_append_string_constant (dpi, "ul");
3348 break;
3349 case D_PRINT_LONG_LONG:
3350 d_append_string_constant (dpi, "ll");
3351 break;
3352 case D_PRINT_UNSIGNED_LONG_LONG:
3353 d_append_string_constant (dpi, "ull");
3354 break;
3355 }
3356 return;
3357 }
3358 break;
3359
3360 case D_PRINT_BOOL:
3361 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3362 && d_right (dc)->u.s_name.len == 1
3363 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3364 {
3365 switch (d_right (dc)->u.s_name.s[0])
3366 {
3367 case '0':
3368 d_append_string_constant (dpi, "false");
3369 return;
3370 case '1':
3371 d_append_string_constant (dpi, "true");
3372 return;
3373 default:
3374 break;
3375 }
3376 }
3377 break;
3378
3379 default:
3380 break;
3381 }
3382 }
3383
3384 d_append_char (dpi, '(');
3385 d_print_comp (dpi, d_left (dc));
3386 d_append_char (dpi, ')');
3387 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3388 d_append_char (dpi, '-');
3389 if (tp == D_PRINT_FLOAT)
3390 d_append_char (dpi, '[');
3391 d_print_comp (dpi, d_right (dc));
3392 if (tp == D_PRINT_FLOAT)
3393 d_append_char (dpi, ']');
3394 }
3395 return;
3396
3397 default:
3398 d_print_error (dpi);
3399 return;
3400 }
3401 }
3402
3403 /* Print a Java dentifier. For Java we try to handle encoded extended
3404 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3405 so we don't it for C++. Characters are encoded as
3406 __U<hex-char>+_. */
3407
3408 static void
d_print_java_identifier(struct d_print_info * dpi,const char * name,int len)3409 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3410 {
3411 const char *p;
3412 const char *end;
3413
3414 end = name + len;
3415 for (p = name; p < end; ++p)
3416 {
3417 if (end - p > 3
3418 && p[0] == '_'
3419 && p[1] == '_'
3420 && p[2] == 'U')
3421 {
3422 unsigned long c;
3423 const char *q;
3424
3425 c = 0;
3426 for (q = p + 3; q < end; ++q)
3427 {
3428 int dig;
3429
3430 if (IS_DIGIT (*q))
3431 dig = *q - '0';
3432 else if (*q >= 'A' && *q <= 'F')
3433 dig = *q - 'A' + 10;
3434 else if (*q >= 'a' && *q <= 'f')
3435 dig = *q - 'a' + 10;
3436 else
3437 break;
3438
3439 c = c * 16 + dig;
3440 }
3441 /* If the Unicode character is larger than 256, we don't try
3442 to deal with it here. FIXME. */
3443 if (q < end && *q == '_' && c < 256)
3444 {
3445 d_append_char (dpi, c);
3446 p = q;
3447 continue;
3448 }
3449 }
3450
3451 d_append_char (dpi, *p);
3452 }
3453 }
3454
3455 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3456 qualifiers on this after printing a function. */
3457
3458 static void
d_print_mod_list(struct d_print_info * dpi,struct d_print_mod * mods,int suffix)3459 d_print_mod_list (struct d_print_info *dpi,
3460 struct d_print_mod *mods, int suffix)
3461 {
3462 struct d_print_template *hold_dpt;
3463
3464 if (mods == NULL || d_print_saw_error (dpi))
3465 return;
3466
3467 if (mods->printed
3468 || (! suffix
3469 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3470 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3471 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3472 {
3473 d_print_mod_list (dpi, mods->next, suffix);
3474 return;
3475 }
3476
3477 mods->printed = 1;
3478
3479 hold_dpt = dpi->templates;
3480 dpi->templates = mods->templates;
3481
3482 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3483 {
3484 d_print_function_type (dpi, mods->mod, mods->next);
3485 dpi->templates = hold_dpt;
3486 return;
3487 }
3488 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3489 {
3490 d_print_array_type (dpi, mods->mod, mods->next);
3491 dpi->templates = hold_dpt;
3492 return;
3493 }
3494 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3495 {
3496 struct d_print_mod *hold_modifiers;
3497 struct demangle_component *dc;
3498
3499 /* When this is on the modifier stack, we have pulled any
3500 qualifiers off the right argument already. Otherwise, we
3501 print it as usual, but don't let the left argument see any
3502 modifiers. */
3503
3504 hold_modifiers = dpi->modifiers;
3505 dpi->modifiers = NULL;
3506 d_print_comp (dpi, d_left (mods->mod));
3507 dpi->modifiers = hold_modifiers;
3508
3509 if ((dpi->options & DMGL_JAVA) == 0)
3510 d_append_string_constant (dpi, "::");
3511 else
3512 d_append_char (dpi, '.');
3513
3514 dc = d_right (mods->mod);
3515 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3516 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3517 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3518 dc = d_left (dc);
3519
3520 d_print_comp (dpi, dc);
3521
3522 dpi->templates = hold_dpt;
3523 return;
3524 }
3525
3526 d_print_mod (dpi, mods->mod);
3527
3528 dpi->templates = hold_dpt;
3529
3530 d_print_mod_list (dpi, mods->next, suffix);
3531 }
3532
3533 /* Print a modifier. */
3534
3535 static void
d_print_mod(struct d_print_info * dpi,const struct demangle_component * mod)3536 d_print_mod (struct d_print_info *dpi,
3537 const struct demangle_component *mod)
3538 {
3539 switch (mod->type)
3540 {
3541 case DEMANGLE_COMPONENT_RESTRICT:
3542 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3543 d_append_string_constant (dpi, " restrict");
3544 return;
3545 case DEMANGLE_COMPONENT_VOLATILE:
3546 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3547 d_append_string_constant (dpi, " volatile");
3548 return;
3549 case DEMANGLE_COMPONENT_CONST:
3550 case DEMANGLE_COMPONENT_CONST_THIS:
3551 d_append_string_constant (dpi, " const");
3552 return;
3553 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3554 d_append_char (dpi, ' ');
3555 d_print_comp (dpi, d_right (mod));
3556 return;
3557 case DEMANGLE_COMPONENT_POINTER:
3558 /* There is no pointer symbol in Java. */
3559 if ((dpi->options & DMGL_JAVA) == 0)
3560 d_append_char (dpi, '*');
3561 return;
3562 case DEMANGLE_COMPONENT_REFERENCE:
3563 d_append_char (dpi, '&');
3564 return;
3565 case DEMANGLE_COMPONENT_COMPLEX:
3566 d_append_string_constant (dpi, "complex ");
3567 return;
3568 case DEMANGLE_COMPONENT_IMAGINARY:
3569 d_append_string_constant (dpi, "imaginary ");
3570 return;
3571 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3572 if (d_last_char (dpi) != '(')
3573 d_append_char (dpi, ' ');
3574 d_print_comp (dpi, d_left (mod));
3575 d_append_string_constant (dpi, "::*");
3576 return;
3577 case DEMANGLE_COMPONENT_TYPED_NAME:
3578 d_print_comp (dpi, d_left (mod));
3579 return;
3580 default:
3581 /* Otherwise, we have something that won't go back on the
3582 modifier stack, so we can just print it. */
3583 d_print_comp (dpi, mod);
3584 return;
3585 }
3586 }
3587
3588 /* Print a function type, except for the return type. */
3589
3590 static void
d_print_function_type(struct d_print_info * dpi,const struct demangle_component * dc,struct d_print_mod * mods)3591 d_print_function_type (struct d_print_info *dpi,
3592 const struct demangle_component *dc,
3593 struct d_print_mod *mods)
3594 {
3595 int need_paren;
3596 int saw_mod;
3597 int need_space;
3598 struct d_print_mod *p;
3599 struct d_print_mod *hold_modifiers;
3600
3601 need_paren = 0;
3602 saw_mod = 0;
3603 need_space = 0;
3604 for (p = mods; p != NULL; p = p->next)
3605 {
3606 if (p->printed)
3607 break;
3608
3609 saw_mod = 1;
3610 switch (p->mod->type)
3611 {
3612 case DEMANGLE_COMPONENT_POINTER:
3613 case DEMANGLE_COMPONENT_REFERENCE:
3614 need_paren = 1;
3615 break;
3616 case DEMANGLE_COMPONENT_RESTRICT:
3617 case DEMANGLE_COMPONENT_VOLATILE:
3618 case DEMANGLE_COMPONENT_CONST:
3619 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3620 case DEMANGLE_COMPONENT_COMPLEX:
3621 case DEMANGLE_COMPONENT_IMAGINARY:
3622 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3623 need_space = 1;
3624 need_paren = 1;
3625 break;
3626 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3627 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3628 case DEMANGLE_COMPONENT_CONST_THIS:
3629 break;
3630 default:
3631 break;
3632 }
3633 if (need_paren)
3634 break;
3635 }
3636
3637 if (d_left (dc) != NULL && ! saw_mod)
3638 need_paren = 1;
3639
3640 if (need_paren)
3641 {
3642 if (! need_space)
3643 {
3644 if (d_last_char (dpi) != '('
3645 && d_last_char (dpi) != '*')
3646 need_space = 1;
3647 }
3648 if (need_space && d_last_char (dpi) != ' ')
3649 d_append_char (dpi, ' ');
3650 d_append_char (dpi, '(');
3651 }
3652
3653 hold_modifiers = dpi->modifiers;
3654 dpi->modifiers = NULL;
3655
3656 d_print_mod_list (dpi, mods, 0);
3657
3658 if (need_paren)
3659 d_append_char (dpi, ')');
3660
3661 d_append_char (dpi, '(');
3662
3663 if (d_right (dc) != NULL)
3664 d_print_comp (dpi, d_right (dc));
3665
3666 d_append_char (dpi, ')');
3667
3668 d_print_mod_list (dpi, mods, 1);
3669
3670 dpi->modifiers = hold_modifiers;
3671 }
3672
3673 /* Print an array type, except for the element type. */
3674
3675 static void
d_print_array_type(struct d_print_info * dpi,const struct demangle_component * dc,struct d_print_mod * mods)3676 d_print_array_type (struct d_print_info *dpi,
3677 const struct demangle_component *dc,
3678 struct d_print_mod *mods)
3679 {
3680 int need_space;
3681
3682 need_space = 1;
3683 if (mods != NULL)
3684 {
3685 int need_paren;
3686 struct d_print_mod *p;
3687
3688 need_paren = 0;
3689 for (p = mods; p != NULL; p = p->next)
3690 {
3691 if (! p->printed)
3692 {
3693 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3694 {
3695 need_space = 0;
3696 break;
3697 }
3698 else
3699 {
3700 need_paren = 1;
3701 need_space = 1;
3702 break;
3703 }
3704 }
3705 }
3706
3707 if (need_paren)
3708 d_append_string_constant (dpi, " (");
3709
3710 d_print_mod_list (dpi, mods, 0);
3711
3712 if (need_paren)
3713 d_append_char (dpi, ')');
3714 }
3715
3716 if (need_space)
3717 d_append_char (dpi, ' ');
3718
3719 d_append_char (dpi, '[');
3720
3721 if (d_left (dc) != NULL)
3722 d_print_comp (dpi, d_left (dc));
3723
3724 d_append_char (dpi, ']');
3725 }
3726
3727 /* Print an operator in an expression. */
3728
3729 static void
d_print_expr_op(struct d_print_info * dpi,const struct demangle_component * dc)3730 d_print_expr_op (struct d_print_info *dpi,
3731 const struct demangle_component *dc)
3732 {
3733 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3734 d_append_buffer (dpi, dc->u.s_operator.op->name,
3735 dc->u.s_operator.op->len);
3736 else
3737 d_print_comp (dpi, dc);
3738 }
3739
3740 /* Print a cast. */
3741
3742 static void
d_print_cast(struct d_print_info * dpi,const struct demangle_component * dc)3743 d_print_cast (struct d_print_info *dpi,
3744 const struct demangle_component *dc)
3745 {
3746 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3747 d_print_comp (dpi, d_left (dc));
3748 else
3749 {
3750 struct d_print_mod *hold_dpm;
3751 struct d_print_template dpt;
3752
3753 /* It appears that for a templated cast operator, we need to put
3754 the template parameters in scope for the operator name, but
3755 not for the parameters. The effect is that we need to handle
3756 the template printing here. */
3757
3758 hold_dpm = dpi->modifiers;
3759 dpi->modifiers = NULL;
3760
3761 dpt.next = dpi->templates;
3762 dpi->templates = &dpt;
3763 dpt.template_decl = d_left (dc);
3764
3765 d_print_comp (dpi, d_left (d_left (dc)));
3766
3767 dpi->templates = dpt.next;
3768
3769 if (d_last_char (dpi) == '<')
3770 d_append_char (dpi, ' ');
3771 d_append_char (dpi, '<');
3772 d_print_comp (dpi, d_right (d_left (dc)));
3773 /* Avoid generating two consecutive '>' characters, to avoid
3774 the C++ syntactic ambiguity. */
3775 if (d_last_char (dpi) == '>')
3776 d_append_char (dpi, ' ');
3777 d_append_char (dpi, '>');
3778
3779 dpi->modifiers = hold_dpm;
3780 }
3781 }
3782
3783 /* Initialize the information structure we use to pass around
3784 information. */
3785
3786 CP_STATIC_IF_GLIBCPP_V3
3787 void
cplus_demangle_init_info(const char * mangled,int options,size_t len,struct d_info * di)3788 cplus_demangle_init_info (const char *mangled, int options, size_t len,
3789 struct d_info *di)
3790 {
3791 di->s = mangled;
3792 di->send = mangled + len;
3793 di->options = options;
3794
3795 di->n = mangled;
3796
3797 /* We can not need more components than twice the number of chars in
3798 the mangled string. Most components correspond directly to
3799 chars, but the ARGLIST types are exceptions. */
3800 di->num_comps = 2 * len;
3801 di->next_comp = 0;
3802
3803 /* Similarly, we can not need more substitutions than there are
3804 chars in the mangled string. */
3805 di->num_subs = len;
3806 di->next_sub = 0;
3807 di->did_subs = 0;
3808
3809 di->last_name = NULL;
3810
3811 di->expansion = 0;
3812 }
3813
3814 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3815 name, return a buffer allocated with malloc holding the demangled
3816 name. OPTIONS is the usual libiberty demangler options. On
3817 success, this sets *PALC to the allocated size of the returned
3818 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3819 a memory allocation failure. On failure, this returns NULL. */
3820
3821 static char *
d_demangle(const char * mangled,int options,size_t * palc)3822 d_demangle (const char* mangled, int options, size_t *palc)
3823 {
3824 size_t len;
3825 int type;
3826 struct d_info di;
3827 struct demangle_component *dc;
3828 int estimate;
3829 char *ret;
3830
3831 *palc = 0;
3832
3833 len = strlen (mangled);
3834
3835 if (mangled[0] == '_' && mangled[1] == 'Z')
3836 type = 0;
3837 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3838 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3839 && (mangled[9] == 'D' || mangled[9] == 'I')
3840 && mangled[10] == '_')
3841 {
3842 char *r;
3843
3844 r = (char *) malloc (40 + len - 11);
3845 if (r == NULL)
3846 *palc = 1;
3847 else
3848 {
3849 if (mangled[9] == 'I')
3850 strcpy (r, "global constructors keyed to ");
3851 else
3852 strcpy (r, "global destructors keyed to ");
3853 strcat (r, mangled + 11);
3854 }
3855 return r;
3856 }
3857 else
3858 {
3859 if ((options & DMGL_TYPES) == 0)
3860 return NULL;
3861 type = 1;
3862 }
3863
3864 cplus_demangle_init_info (mangled, options, len, &di);
3865
3866 {
3867 #ifdef CP_DYNAMIC_ARRAYS
3868 __extension__ struct demangle_component comps[di.num_comps];
3869 __extension__ struct demangle_component *subs[di.num_subs];
3870
3871 di.comps = &comps[0];
3872 di.subs = &subs[0];
3873 #else
3874 di.comps = ((struct demangle_component *)
3875 malloc (di.num_comps * sizeof (struct demangle_component)));
3876 di.subs = ((struct demangle_component **)
3877 malloc (di.num_subs * sizeof (struct demangle_component *)));
3878 if (di.comps == NULL || di.subs == NULL)
3879 {
3880 if (di.comps != NULL)
3881 free (di.comps);
3882 if (di.subs != NULL)
3883 free (di.subs);
3884 *palc = 1;
3885 return NULL;
3886 }
3887 #endif
3888
3889 if (! type)
3890 dc = cplus_demangle_mangled_name (&di, 1);
3891 else
3892 dc = cplus_demangle_type (&di);
3893
3894 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3895 mangled string, then we didn't successfully demangle it. If
3896 DMGL_PARAMS is not set, we didn't look at the trailing
3897 parameters. */
3898 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3899 dc = NULL;
3900
3901 #ifdef CP_DEMANGLE_DEBUG
3902 if (dc == NULL)
3903 printf ("failed demangling\n");
3904 else
3905 d_dump (dc, 0);
3906 #endif
3907
3908 /* We try to guess the length of the demangled string, to minimize
3909 calls to realloc during demangling. */
3910 estimate = len + di.expansion + 10 * di.did_subs;
3911 estimate += estimate / 8;
3912
3913 ret = NULL;
3914 if (dc != NULL)
3915 ret = cplus_demangle_print (options, dc, estimate, palc);
3916
3917 #ifndef CP_DYNAMIC_ARRAYS
3918 free (di.comps);
3919 free (di.subs);
3920 #endif
3921
3922 #ifdef CP_DEMANGLE_DEBUG
3923 if (ret != NULL)
3924 {
3925 int rlen;
3926
3927 rlen = strlen (ret);
3928 if (rlen > 2 * estimate)
3929 printf ("*** Length %d much greater than estimate %d\n",
3930 rlen, estimate);
3931 else if (rlen > estimate)
3932 printf ("*** Length %d greater than estimate %d\n",
3933 rlen, estimate);
3934 else if (rlen < estimate / 2)
3935 printf ("*** Length %d much less than estimate %d\n",
3936 rlen, estimate);
3937 }
3938 #endif
3939 }
3940
3941 return ret;
3942 }
3943
3944 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3945
3946 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3947
3948 /* ia64 ABI-mandated entry point in the C++ runtime library for
3949 performing demangling. MANGLED_NAME is a NUL-terminated character
3950 string containing the name to be demangled.
3951
3952 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3953 *LENGTH bytes, into which the demangled name is stored. If
3954 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3955 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3956 is placed in a region of memory allocated with malloc.
3957
3958 If LENGTH is non-NULL, the length of the buffer conaining the
3959 demangled name, is placed in *LENGTH.
3960
3961 The return value is a pointer to the start of the NUL-terminated
3962 demangled name, or NULL if the demangling fails. The caller is
3963 responsible for deallocating this memory using free.
3964
3965 *STATUS is set to one of the following values:
3966 0: The demangling operation succeeded.
3967 -1: A memory allocation failure occurred.
3968 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3969 -3: One of the arguments is invalid.
3970
3971 The demangling is performed using the C++ ABI mangling rules, with
3972 GNU extensions. */
3973
3974 char *
__cxa_demangle(const char * mangled_name,char * output_buffer,size_t * length,int * status)3975 __cxa_demangle (const char *mangled_name, char *output_buffer,
3976 size_t *length, int *status)
3977 {
3978 char *demangled;
3979 size_t alc;
3980
3981 if (mangled_name == NULL)
3982 {
3983 if (status != NULL)
3984 *status = -3;
3985 return NULL;
3986 }
3987
3988 if (output_buffer != NULL && length == NULL)
3989 {
3990 if (status != NULL)
3991 *status = -3;
3992 return NULL;
3993 }
3994
3995 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
3996
3997 if (demangled == NULL)
3998 {
3999 if (status != NULL)
4000 {
4001 if (alc == 1)
4002 *status = -1;
4003 else
4004 *status = -2;
4005 }
4006 return NULL;
4007 }
4008
4009 if (output_buffer == NULL)
4010 {
4011 if (length != NULL)
4012 *length = alc;
4013 }
4014 else
4015 {
4016 if (strlen (demangled) < *length)
4017 {
4018 strcpy (output_buffer, demangled);
4019 free (demangled);
4020 demangled = output_buffer;
4021 }
4022 else
4023 {
4024 free (output_buffer);
4025 *length = alc;
4026 }
4027 }
4028
4029 if (status != NULL)
4030 *status = 0;
4031
4032 return demangled;
4033 }
4034
4035 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4036
4037 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4038 mangled name, return a buffer allocated with malloc holding the
4039 demangled name. Otherwise, return NULL. */
4040
4041 char *
cplus_demangle_v3(const char * mangled,int options)4042 cplus_demangle_v3 (const char* mangled, int options)
4043 {
4044 size_t alc;
4045
4046 return d_demangle (mangled, options, &alc);
4047 }
4048
4049 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4050 conventions, but the output formatting is a little different.
4051 This instructs the C++ demangler not to emit pointer characters ("*"), and
4052 to use Java's namespace separator symbol ("." instead of "::"). It then
4053 does an additional pass over the demangled output to replace instances
4054 of JArray<TYPE> with TYPE[]. */
4055
4056 char *
java_demangle_v3(const char * mangled)4057 java_demangle_v3 (const char* mangled)
4058 {
4059 size_t alc;
4060 char *demangled;
4061 int nesting;
4062 char *from;
4063 char *to;
4064
4065 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4066 &alc);
4067
4068 if (demangled == NULL)
4069 return NULL;
4070
4071 nesting = 0;
4072 from = demangled;
4073 to = from;
4074 while (*from != '\0')
4075 {
4076 if (strncmp (from, "JArray<", 7) == 0)
4077 {
4078 from += 7;
4079 ++nesting;
4080 }
4081 else if (nesting > 0 && *from == '>')
4082 {
4083 while (to > demangled && to[-1] == ' ')
4084 --to;
4085 *to++ = '[';
4086 *to++ = ']';
4087 --nesting;
4088 ++from;
4089 }
4090 else
4091 *to++ = *from++;
4092 }
4093
4094 *to = '\0';
4095
4096 return demangled;
4097 }
4098
4099 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4100
4101 #ifndef IN_GLIBCPP_V3
4102
4103 /* Demangle a string in order to find out whether it is a constructor
4104 or destructor. Return non-zero on success. Set *CTOR_KIND and
4105 *DTOR_KIND appropriately. */
4106
4107 static int
is_ctor_or_dtor(const char * mangled,enum gnu_v3_ctor_kinds * ctor_kind,enum gnu_v3_dtor_kinds * dtor_kind)4108 is_ctor_or_dtor (const char *mangled,
4109 enum gnu_v3_ctor_kinds *ctor_kind,
4110 enum gnu_v3_dtor_kinds *dtor_kind)
4111 {
4112 struct d_info di;
4113 struct demangle_component *dc;
4114 int ret;
4115
4116 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4117 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4118
4119 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4120
4121 {
4122 #ifdef CP_DYNAMIC_ARRAYS
4123 __extension__ struct demangle_component comps[di.num_comps];
4124 __extension__ struct demangle_component *subs[di.num_subs];
4125
4126 di.comps = &comps[0];
4127 di.subs = &subs[0];
4128 #else
4129 di.comps = ((struct demangle_component *)
4130 malloc (di.num_comps * sizeof (struct demangle_component)));
4131 di.subs = ((struct demangle_component **)
4132 malloc (di.num_subs * sizeof (struct demangle_component *)));
4133 if (di.comps == NULL || di.subs == NULL)
4134 {
4135 if (di.comps != NULL)
4136 free (di.comps);
4137 if (di.subs != NULL)
4138 free (di.subs);
4139 return 0;
4140 }
4141 #endif
4142
4143 dc = cplus_demangle_mangled_name (&di, 1);
4144
4145 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4146 to demangle the entire string. */
4147
4148 ret = 0;
4149 while (dc != NULL)
4150 {
4151 switch (dc->type)
4152 {
4153 default:
4154 dc = NULL;
4155 break;
4156 case DEMANGLE_COMPONENT_TYPED_NAME:
4157 case DEMANGLE_COMPONENT_TEMPLATE:
4158 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4159 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4160 case DEMANGLE_COMPONENT_CONST_THIS:
4161 dc = d_left (dc);
4162 break;
4163 case DEMANGLE_COMPONENT_QUAL_NAME:
4164 case DEMANGLE_COMPONENT_LOCAL_NAME:
4165 dc = d_right (dc);
4166 break;
4167 case DEMANGLE_COMPONENT_CTOR:
4168 *ctor_kind = dc->u.s_ctor.kind;
4169 ret = 1;
4170 dc = NULL;
4171 break;
4172 case DEMANGLE_COMPONENT_DTOR:
4173 *dtor_kind = dc->u.s_dtor.kind;
4174 ret = 1;
4175 dc = NULL;
4176 break;
4177 }
4178 }
4179
4180 #ifndef CP_DYNAMIC_ARRAYS
4181 free (di.subs);
4182 free (di.comps);
4183 #endif
4184 }
4185
4186 return ret;
4187 }
4188
4189 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4190 name. A non-zero return indicates the type of constructor. */
4191
4192 enum gnu_v3_ctor_kinds
is_gnu_v3_mangled_ctor(const char * name)4193 is_gnu_v3_mangled_ctor (const char *name)
4194 {
4195 enum gnu_v3_ctor_kinds ctor_kind;
4196 enum gnu_v3_dtor_kinds dtor_kind;
4197
4198 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4199 return (enum gnu_v3_ctor_kinds) 0;
4200 return ctor_kind;
4201 }
4202
4203
4204 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4205 name. A non-zero return indicates the type of destructor. */
4206
4207 enum gnu_v3_dtor_kinds
is_gnu_v3_mangled_dtor(const char * name)4208 is_gnu_v3_mangled_dtor (const char *name)
4209 {
4210 enum gnu_v3_ctor_kinds ctor_kind;
4211 enum gnu_v3_dtor_kinds dtor_kind;
4212
4213 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4214 return (enum gnu_v3_dtor_kinds) 0;
4215 return dtor_kind;
4216 }
4217
4218 #endif /* IN_GLIBCPP_V3 */
4219
main(int argc,char ** argv)4220 int main(int argc, char **argv)
4221 {
4222 int status;
4223 printf("*%s*\n", __cxa_demangle(argv[1], 0, 0, &status));
4224 return 0;
4225 }
4226
4227