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

source code of libiberty/cp-demangle.c