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