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