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