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