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