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