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