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