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