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