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

source code of libiberty/cp-demangle.c