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