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