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