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