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