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