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