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