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

source code of libiberty/cp-demangle.c