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