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