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