]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/cp-demangle.c
* s390-tdep.c (s390_frame_prev_register): Change type of last
[thirdparty/binutils-gdb.git] / libiberty / cp-demangle.c
CommitLineData
d00edca5 1/* Demangler for g++ V3 ABI.
b31a49d7 2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
d00edca5 3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
eb383413 4
9ad1aa29 5 This file is part of the libiberty library, which is part of GCC.
74bcd529 6
9ad1aa29 7 This file is free software; you can redistribute it and/or modify
eb383413
L
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
35efcd67
DD
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
20
eb383413
L
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
979c05d3 28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
eb383413
L
29*/
30
858b45cf
DD
31/* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
34
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
37
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
40 name.
41
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
47
59727473
DD
48 Also, the interface to the component list is public, and defined in
49 demangle.h. The interface consists of these types, which are
50 defined in demangle.h:
51 enum demangle_component_type
52 struct demangle_component
53 and these functions defined in this file:
54 cplus_demangle_fill_name
55 cplus_demangle_fill_extended_operator
56 cplus_demangle_fill_ctor
57 cplus_demangle_fill_dtor
58 cplus_demangle_print
59 and other functions defined in the file cp-demint.c.
60
61 This file also defines some other functions and variables which are
62 only to be used by the file cp-demint.c.
63
858b45cf
DD
64 Preprocessor macros you can define while compiling this file:
65
66 IN_LIBGCC2
67 If defined, this file defines the following function, q.v.:
68 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
69 int *status)
70 instead of cplus_demangle_v3() and java_demangle_v3().
71
72 IN_GLIBCPP_V3
59727473
DD
73 If defined, this file defines only __cxa_demangle(), and no other
74 publically visible functions or variables.
858b45cf
DD
75
76 STANDALONE_DEMANGLER
77 If defined, this file defines a main() function which demangles
78 any arguments, or, if none, demangles stdin.
79
80 CP_DEMANGLE_DEBUG
81 If defined, turns on debugging mode, which prints information on
82 stdout about the mangled string. This is not generally useful.
83*/
84
eb383413
L
85#ifdef HAVE_CONFIG_H
86#include "config.h"
87#endif
88
d00edca5 89#include <stdio.h>
b1233257 90
eb383413
L
91#ifdef HAVE_STDLIB_H
92#include <stdlib.h>
93#endif
eb383413
L
94#ifdef HAVE_STRING_H
95#include <string.h>
96#endif
97
98#include "ansidecl.h"
99#include "libiberty.h"
eb383413 100#include "demangle.h"
59727473
DD
101#include "cp-demangle.h"
102
103/* If IN_GLIBCPP_V3 is defined, some functions are made static. We
104 also rename them via #define to avoid compiler errors when the
105 static definition conflicts with the extern declaration in a header
106 file. */
107#ifdef IN_GLIBCPP_V3
108
109#define CP_STATIC_IF_GLIBCPP_V3 static
110
111#define cplus_demangle_fill_name d_fill_name
9334f9c6 112static int d_fill_name (struct demangle_component *, const char *, int);
59727473
DD
113
114#define cplus_demangle_fill_extended_operator d_fill_extended_operator
115static int
9334f9c6
DD
116d_fill_extended_operator (struct demangle_component *, int,
117 struct demangle_component *);
59727473
DD
118
119#define cplus_demangle_fill_ctor d_fill_ctor
120static int
9334f9c6
DD
121d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
122 struct demangle_component *);
59727473
DD
123
124#define cplus_demangle_fill_dtor d_fill_dtor
125static int
9334f9c6
DD
126d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
127 struct demangle_component *);
59727473
DD
128
129#define cplus_demangle_mangled_name d_mangled_name
9334f9c6 130static struct demangle_component *d_mangled_name (struct d_info *, int);
59727473
DD
131
132#define cplus_demangle_type d_type
9334f9c6 133static struct demangle_component *d_type (struct d_info *);
59727473
DD
134
135#define cplus_demangle_print d_print
9334f9c6 136static char *d_print (int, const struct demangle_component *, int, size_t *);
59727473
DD
137
138#define cplus_demangle_init_info d_init_info
9334f9c6 139static void d_init_info (const char *, int, size_t, struct d_info *);
59727473
DD
140
141#else /* ! defined(IN_GLIBCPP_V3) */
142#define CP_STATIC_IF_GLIBCPP_V3
143#endif /* ! defined(IN_GLIBCPP_V3) */
eb383413 144
b6fb00c0
DD
145/* See if the compiler supports dynamic arrays. */
146
147#ifdef __GNUC__
148#define CP_DYNAMIC_ARRAYS
149#else
150#ifdef __STDC__
151#ifdef __STDC_VERSION__
152#if __STDC_VERSION__ >= 199901L
153#define CP_DYNAMIC_ARRAYS
154#endif /* __STDC__VERSION >= 199901L */
155#endif /* defined (__STDC_VERSION__) */
156#endif /* defined (__STDC__) */
157#endif /* ! defined (__GNUC__) */
158
858b45cf
DD
159/* We avoid pulling in the ctype tables, to prevent pulling in
160 additional unresolved symbols when this code is used in a library.
161 FIXME: Is this really a valid reason? This comes from the original
162 V3 demangler code.
d00edca5 163
858b45cf
DD
164 As of this writing this file has the following undefined references
165 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
166 strcpy, strcat, strlen. */
d00edca5 167
d00edca5 168#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
858b45cf
DD
169#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
170#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
03d5f569 171
74bcd529
DD
172/* The prefix prepended by GCC to an identifier represnting the
173 anonymous namespace. */
174#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
d00edca5
DD
175#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
176 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
74bcd529 177
97ceaf5b
DD
178/* Information we keep for the standard substitutions. */
179
180struct d_standard_sub_info
181{
182 /* The code for this substitution. */
183 char code;
184 /* The simple string it expands to. */
185 const char *simple_expansion;
b6fb00c0
DD
186 /* The length of the simple expansion. */
187 int simple_len;
97ceaf5b
DD
188 /* The results of a full, verbose, expansion. This is used when
189 qualifying a constructor/destructor, or when in verbose mode. */
190 const char *full_expansion;
b6fb00c0
DD
191 /* The length of the full expansion. */
192 int full_len;
97ceaf5b
DD
193 /* What to set the last_name field of d_info to; NULL if we should
194 not set it. This is only relevant when qualifying a
195 constructor/destructor. */
196 const char *set_last_name;
b6fb00c0
DD
197 /* The length of set_last_name. */
198 int set_last_name_len;
97ceaf5b
DD
199};
200
59727473 201/* Accessors for subtrees of struct demangle_component. */
eb383413 202
d00edca5
DD
203#define d_left(dc) ((dc)->u.s_binary.left)
204#define d_right(dc) ((dc)->u.s_binary.right)
205
d00edca5 206/* A list of templates. This is used while printing. */
eb383413 207
d00edca5
DD
208struct d_print_template
209{
210 /* Next template on the list. */
211 struct d_print_template *next;
212 /* This template. */
59727473 213 const struct demangle_component *template;
d00edca5 214};
eb383413 215
d00edca5 216/* A list of type modifiers. This is used while printing. */
eb383413 217
d00edca5
DD
218struct d_print_mod
219{
220 /* Next modifier on the list. These are in the reverse of the order
221 in which they appeared in the mangled string. */
222 struct d_print_mod *next;
223 /* The modifier. */
59727473 224 const struct demangle_component *mod;
d00edca5
DD
225 /* Whether this modifier was printed. */
226 int printed;
331c3da2
DD
227 /* The list of templates which applies to this modifier. */
228 struct d_print_template *templates;
d00edca5 229};
eb383413 230
d00edca5
DD
231/* We use this structure to hold information during printing. */
232
233struct d_print_info
234{
235 /* The options passed to the demangler. */
236 int options;
237 /* Buffer holding the result. */
238 char *buf;
239 /* Current length of data in buffer. */
240 size_t len;
241 /* Allocated size of buffer. */
242 size_t alc;
243 /* The current list of templates, if any. */
244 struct d_print_template *templates;
245 /* The current list of modifiers (e.g., pointer, reference, etc.),
246 if any. */
247 struct d_print_mod *modifiers;
248 /* Set to 1 if we had a memory allocation failure. */
249 int allocation_failure;
250};
e61231f1 251
d00edca5 252#define d_print_saw_error(dpi) ((dpi)->buf == NULL)
e61231f1 253
d00edca5
DD
254#define d_append_char(dpi, c) \
255 do \
256 { \
257 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
258 (dpi)->buf[(dpi)->len++] = (c); \
259 else \
260 d_print_append_char ((dpi), (c)); \
261 } \
262 while (0)
e61231f1 263
d00edca5
DD
264#define d_append_buffer(dpi, s, l) \
265 do \
266 { \
267 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
268 { \
269 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
270 (dpi)->len += l; \
271 } \
272 else \
273 d_print_append_buffer ((dpi), (s), (l)); \
274 } \
275 while (0)
eb383413 276
b6fb00c0
DD
277#define d_append_string_constant(dpi, s) \
278 d_append_buffer (dpi, (s), sizeof (s) - 1)
03d5f569 279
858b45cf
DD
280#define d_last_char(dpi) \
281 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
282
eb383413 283#ifdef CP_DEMANGLE_DEBUG
9334f9c6 284static void d_dump (struct demangle_component *, int);
eb383413 285#endif
59727473
DD
286
287static struct demangle_component *
9334f9c6 288d_make_empty (struct d_info *);
59727473
DD
289
290static struct demangle_component *
9334f9c6
DD
291d_make_comp (struct d_info *, enum demangle_component_type,
292 struct demangle_component *,
293 struct demangle_component *);
59727473
DD
294
295static struct demangle_component *
9334f9c6 296d_make_name (struct d_info *, const char *, int);
59727473
DD
297
298static struct demangle_component *
9334f9c6
DD
299d_make_builtin_type (struct d_info *,
300 const struct demangle_builtin_type_info *);
59727473
DD
301
302static struct demangle_component *
9334f9c6
DD
303d_make_operator (struct d_info *,
304 const struct demangle_operator_info *);
59727473
DD
305
306static struct demangle_component *
9334f9c6
DD
307d_make_extended_operator (struct d_info *, int,
308 struct demangle_component *);
59727473
DD
309
310static struct demangle_component *
9334f9c6
DD
311d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
312 struct demangle_component *);
59727473
DD
313
314static struct demangle_component *
9334f9c6
DD
315d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
316 struct demangle_component *);
59727473
DD
317
318static struct demangle_component *
9334f9c6 319d_make_template_param (struct d_info *, long);
59727473
DD
320
321static struct demangle_component *
9334f9c6 322d_make_sub (struct d_info *, const char *, int);
59727473
DD
323
324static int
9334f9c6 325has_return_type (struct demangle_component *);
59727473
DD
326
327static int
9334f9c6 328is_ctor_dtor_or_conversion (struct demangle_component *);
59727473 329
9334f9c6 330static struct demangle_component *d_encoding (struct d_info *, int);
59727473 331
9334f9c6 332static struct demangle_component *d_name (struct d_info *);
59727473 333
9334f9c6 334static struct demangle_component *d_nested_name (struct d_info *);
59727473 335
9334f9c6 336static struct demangle_component *d_prefix (struct d_info *);
59727473 337
9334f9c6 338static struct demangle_component *d_unqualified_name (struct d_info *);
59727473 339
9334f9c6 340static struct demangle_component *d_source_name (struct d_info *);
59727473 341
9334f9c6 342static long d_number (struct d_info *);
59727473 343
9334f9c6 344static struct demangle_component *d_identifier (struct d_info *, int);
59727473 345
9334f9c6 346static struct demangle_component *d_operator_name (struct d_info *);
59727473 347
9334f9c6 348static struct demangle_component *d_special_name (struct d_info *);
59727473 349
9334f9c6 350static int d_call_offset (struct d_info *, int);
59727473 351
9334f9c6 352static struct demangle_component *d_ctor_dtor_name (struct d_info *);
59727473
DD
353
354static struct demangle_component **
9334f9c6 355d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
59727473
DD
356
357static struct demangle_component *
9334f9c6 358d_function_type (struct d_info *);
59727473
DD
359
360static struct demangle_component *
9334f9c6 361d_bare_function_type (struct d_info *, int);
59727473
DD
362
363static struct demangle_component *
9334f9c6 364d_class_enum_type (struct d_info *);
59727473 365
9334f9c6 366static struct demangle_component *d_array_type (struct d_info *);
59727473
DD
367
368static struct demangle_component *
9334f9c6 369d_pointer_to_member_type (struct d_info *);
59727473
DD
370
371static struct demangle_component *
9334f9c6 372d_template_param (struct d_info *);
59727473 373
9334f9c6 374static struct demangle_component *d_template_args (struct d_info *);
59727473
DD
375
376static struct demangle_component *
9334f9c6 377d_template_arg (struct d_info *);
59727473 378
9334f9c6 379static struct demangle_component *d_expression (struct d_info *);
59727473 380
9334f9c6 381static struct demangle_component *d_expr_primary (struct d_info *);
59727473 382
9334f9c6 383static struct demangle_component *d_local_name (struct d_info *);
59727473 384
9334f9c6 385static int d_discriminator (struct d_info *);
59727473
DD
386
387static int
9334f9c6 388d_add_substitution (struct d_info *, struct demangle_component *);
59727473 389
9334f9c6 390static struct demangle_component *d_substitution (struct d_info *, int);
59727473 391
9334f9c6 392static void d_print_resize (struct d_print_info *, size_t);
59727473 393
9334f9c6 394static void d_print_append_char (struct d_print_info *, int);
59727473
DD
395
396static void
9334f9c6 397d_print_append_buffer (struct d_print_info *, const char *, size_t);
59727473 398
9334f9c6 399static void d_print_error (struct d_print_info *);
59727473
DD
400
401static void
9334f9c6 402d_print_comp (struct d_print_info *, const struct demangle_component *);
59727473
DD
403
404static void
9334f9c6 405d_print_java_identifier (struct d_print_info *, const char *, int);
59727473
DD
406
407static void
9334f9c6 408d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
59727473
DD
409
410static void
9334f9c6 411d_print_mod (struct d_print_info *, const struct demangle_component *);
59727473
DD
412
413static void
9334f9c6
DD
414d_print_function_type (struct d_print_info *,
415 const struct demangle_component *,
416 struct d_print_mod *);
59727473
DD
417
418static void
9334f9c6
DD
419d_print_array_type (struct d_print_info *,
420 const struct demangle_component *,
421 struct d_print_mod *);
59727473
DD
422
423static void
9334f9c6 424d_print_expr_op (struct d_print_info *, const struct demangle_component *);
59727473
DD
425
426static void
9334f9c6 427d_print_cast (struct d_print_info *, const struct demangle_component *);
59727473 428
9334f9c6 429static char *d_demangle (const char *, int, size_t *);
d00edca5 430
eb383413 431#ifdef CP_DEMANGLE_DEBUG
d00edca5
DD
432
433static void
9334f9c6 434d_dump (struct demangle_component *dc, int indent)
eb383413
L
435{
436 int i;
eb383413 437
d00edca5
DD
438 if (dc == NULL)
439 return;
440
441 for (i = 0; i < indent; ++i)
442 putchar (' ');
443
444 switch (dc->type)
445 {
59727473 446 case DEMANGLE_COMPONENT_NAME:
d00edca5
DD
447 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
448 return;
59727473 449 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
d00edca5
DD
450 printf ("template parameter %ld\n", dc->u.s_number.number);
451 return;
59727473 452 case DEMANGLE_COMPONENT_CTOR:
d00edca5
DD
453 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
454 d_dump (dc->u.s_ctor.name, indent + 2);
455 return;
59727473 456 case DEMANGLE_COMPONENT_DTOR:
d00edca5
DD
457 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
458 d_dump (dc->u.s_dtor.name, indent + 2);
459 return;
59727473 460 case DEMANGLE_COMPONENT_SUB_STD:
d00edca5
DD
461 printf ("standard substitution %s\n", dc->u.s_string.string);
462 return;
59727473 463 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
d00edca5
DD
464 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
465 return;
59727473 466 case DEMANGLE_COMPONENT_OPERATOR:
d00edca5
DD
467 printf ("operator %s\n", dc->u.s_operator.op->name);
468 return;
59727473 469 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
d00edca5
DD
470 printf ("extended operator with %d args\n",
471 dc->u.s_extended_operator.args);
472 d_dump (dc->u.s_extended_operator.name, indent + 2);
473 return;
474
59727473 475 case DEMANGLE_COMPONENT_QUAL_NAME:
d00edca5
DD
476 printf ("qualified name\n");
477 break;
59727473 478 case DEMANGLE_COMPONENT_LOCAL_NAME:
d4edd112
DD
479 printf ("local name\n");
480 break;
59727473 481 case DEMANGLE_COMPONENT_TYPED_NAME:
d00edca5
DD
482 printf ("typed name\n");
483 break;
59727473 484 case DEMANGLE_COMPONENT_TEMPLATE:
d00edca5
DD
485 printf ("template\n");
486 break;
59727473 487 case DEMANGLE_COMPONENT_VTABLE:
d00edca5
DD
488 printf ("vtable\n");
489 break;
59727473 490 case DEMANGLE_COMPONENT_VTT:
d00edca5
DD
491 printf ("VTT\n");
492 break;
59727473 493 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
d00edca5
DD
494 printf ("construction vtable\n");
495 break;
59727473 496 case DEMANGLE_COMPONENT_TYPEINFO:
d00edca5
DD
497 printf ("typeinfo\n");
498 break;
59727473 499 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
d00edca5
DD
500 printf ("typeinfo name\n");
501 break;
59727473 502 case DEMANGLE_COMPONENT_TYPEINFO_FN:
d00edca5
DD
503 printf ("typeinfo function\n");
504 break;
59727473 505 case DEMANGLE_COMPONENT_THUNK:
d00edca5
DD
506 printf ("thunk\n");
507 break;
59727473 508 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
d00edca5
DD
509 printf ("virtual thunk\n");
510 break;
59727473 511 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
d00edca5
DD
512 printf ("covariant thunk\n");
513 break;
59727473 514 case DEMANGLE_COMPONENT_JAVA_CLASS:
d00edca5
DD
515 printf ("java class\n");
516 break;
59727473 517 case DEMANGLE_COMPONENT_GUARD:
d00edca5
DD
518 printf ("guard\n");
519 break;
59727473 520 case DEMANGLE_COMPONENT_REFTEMP:
d00edca5
DD
521 printf ("reference temporary\n");
522 break;
59727473 523 case DEMANGLE_COMPONENT_RESTRICT:
d00edca5
DD
524 printf ("restrict\n");
525 break;
59727473 526 case DEMANGLE_COMPONENT_VOLATILE:
d00edca5
DD
527 printf ("volatile\n");
528 break;
59727473 529 case DEMANGLE_COMPONENT_CONST:
d00edca5
DD
530 printf ("const\n");
531 break;
59727473 532 case DEMANGLE_COMPONENT_RESTRICT_THIS:
858b45cf
DD
533 printf ("restrict this\n");
534 break;
59727473 535 case DEMANGLE_COMPONENT_VOLATILE_THIS:
858b45cf
DD
536 printf ("volatile this\n");
537 break;
59727473 538 case DEMANGLE_COMPONENT_CONST_THIS:
858b45cf
DD
539 printf ("const this\n");
540 break;
59727473 541 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
d00edca5
DD
542 printf ("vendor type qualifier\n");
543 break;
59727473 544 case DEMANGLE_COMPONENT_POINTER:
d00edca5
DD
545 printf ("pointer\n");
546 break;
59727473 547 case DEMANGLE_COMPONENT_REFERENCE:
d00edca5
DD
548 printf ("reference\n");
549 break;
59727473 550 case DEMANGLE_COMPONENT_COMPLEX:
d00edca5
DD
551 printf ("complex\n");
552 break;
59727473 553 case DEMANGLE_COMPONENT_IMAGINARY:
d00edca5
DD
554 printf ("imaginary\n");
555 break;
59727473 556 case DEMANGLE_COMPONENT_VENDOR_TYPE:
d00edca5
DD
557 printf ("vendor type\n");
558 break;
59727473 559 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
d00edca5
DD
560 printf ("function type\n");
561 break;
59727473 562 case DEMANGLE_COMPONENT_ARRAY_TYPE:
d00edca5
DD
563 printf ("array type\n");
564 break;
59727473 565 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
d00edca5
DD
566 printf ("pointer to member type\n");
567 break;
59727473 568 case DEMANGLE_COMPONENT_ARGLIST:
d00edca5
DD
569 printf ("argument list\n");
570 break;
59727473 571 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
d00edca5
DD
572 printf ("template argument list\n");
573 break;
59727473 574 case DEMANGLE_COMPONENT_CAST:
d00edca5
DD
575 printf ("cast\n");
576 break;
59727473 577 case DEMANGLE_COMPONENT_UNARY:
d00edca5
DD
578 printf ("unary operator\n");
579 break;
59727473 580 case DEMANGLE_COMPONENT_BINARY:
d00edca5
DD
581 printf ("binary operator\n");
582 break;
59727473 583 case DEMANGLE_COMPONENT_BINARY_ARGS:
d00edca5
DD
584 printf ("binary operator arguments\n");
585 break;
59727473 586 case DEMANGLE_COMPONENT_TRINARY:
d00edca5
DD
587 printf ("trinary operator\n");
588 break;
59727473 589 case DEMANGLE_COMPONENT_TRINARY_ARG1:
d00edca5
DD
590 printf ("trinary operator arguments 1\n");
591 break;
59727473 592 case DEMANGLE_COMPONENT_TRINARY_ARG2:
d00edca5
DD
593 printf ("trinary operator arguments 1\n");
594 break;
59727473 595 case DEMANGLE_COMPONENT_LITERAL:
d00edca5
DD
596 printf ("literal\n");
597 break;
59727473 598 case DEMANGLE_COMPONENT_LITERAL_NEG:
97ceaf5b
DD
599 printf ("negative literal\n");
600 break;
eb383413
L
601 }
602
d00edca5
DD
603 d_dump (d_left (dc), indent + 2);
604 d_dump (d_right (dc), indent + 2);
605}
606
607#endif /* CP_DEMANGLE_DEBUG */
608
59727473
DD
609/* Fill in a DEMANGLE_COMPONENT_NAME. */
610
611CP_STATIC_IF_GLIBCPP_V3
612int
9334f9c6 613cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
59727473
DD
614{
615 if (p == NULL || s == NULL || len == 0)
616 return 0;
617 p->type = DEMANGLE_COMPONENT_NAME;
618 p->u.s_name.s = s;
619 p->u.s_name.len = len;
620 return 1;
621}
622
623/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
624
625CP_STATIC_IF_GLIBCPP_V3
626int
9334f9c6
DD
627cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
628 struct demangle_component *name)
59727473
DD
629{
630 if (p == NULL || args < 0 || name == NULL)
631 return 0;
632 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
633 p->u.s_extended_operator.args = args;
634 p->u.s_extended_operator.name = name;
635 return 1;
636}
637
638/* Fill in a DEMANGLE_COMPONENT_CTOR. */
639
640CP_STATIC_IF_GLIBCPP_V3
641int
9334f9c6
DD
642cplus_demangle_fill_ctor (struct demangle_component *p,
643 enum gnu_v3_ctor_kinds kind,
644 struct demangle_component *name)
59727473
DD
645{
646 if (p == NULL
647 || name == NULL
648 || (kind < gnu_v3_complete_object_ctor
649 && kind > gnu_v3_complete_object_allocating_ctor))
650 return 0;
651 p->type = DEMANGLE_COMPONENT_CTOR;
652 p->u.s_ctor.kind = kind;
653 p->u.s_ctor.name = name;
654 return 1;
655}
656
657/* Fill in a DEMANGLE_COMPONENT_DTOR. */
658
659CP_STATIC_IF_GLIBCPP_V3
660int
9334f9c6
DD
661cplus_demangle_fill_dtor (struct demangle_component *p,
662 enum gnu_v3_dtor_kinds kind,
663 struct demangle_component *name)
59727473
DD
664{
665 if (p == NULL
666 || name == NULL
667 || (kind < gnu_v3_deleting_dtor
668 && kind > gnu_v3_base_object_dtor))
669 return 0;
670 p->type = DEMANGLE_COMPONENT_DTOR;
671 p->u.s_dtor.kind = kind;
672 p->u.s_dtor.name = name;
673 return 1;
674}
675
d00edca5
DD
676/* Add a new component. */
677
59727473 678static struct demangle_component *
9334f9c6 679d_make_empty (struct d_info *di)
d00edca5 680{
59727473 681 struct demangle_component *p;
d00edca5
DD
682
683 if (di->next_comp >= di->num_comps)
684 return NULL;
685 p = &di->comps[di->next_comp];
d00edca5
DD
686 ++di->next_comp;
687 return p;
688}
689
690/* Add a new generic component. */
691
59727473 692static struct demangle_component *
9334f9c6
DD
693d_make_comp (struct d_info *di, enum demangle_component_type type,
694 struct demangle_component *left,
695 struct demangle_component *right)
d00edca5 696{
59727473 697 struct demangle_component *p;
d00edca5
DD
698
699 /* We check for errors here. A typical error would be a NULL return
331c3da2
DD
700 from a subroutine. We catch those here, and return NULL
701 upward. */
d00edca5
DD
702 switch (type)
703 {
704 /* These types require two parameters. */
59727473
DD
705 case DEMANGLE_COMPONENT_QUAL_NAME:
706 case DEMANGLE_COMPONENT_LOCAL_NAME:
707 case DEMANGLE_COMPONENT_TYPED_NAME:
708 case DEMANGLE_COMPONENT_TEMPLATE:
2d6520ee 709 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
59727473
DD
710 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
711 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
712 case DEMANGLE_COMPONENT_UNARY:
713 case DEMANGLE_COMPONENT_BINARY:
714 case DEMANGLE_COMPONENT_BINARY_ARGS:
715 case DEMANGLE_COMPONENT_TRINARY:
716 case DEMANGLE_COMPONENT_TRINARY_ARG1:
717 case DEMANGLE_COMPONENT_TRINARY_ARG2:
718 case DEMANGLE_COMPONENT_LITERAL:
719 case DEMANGLE_COMPONENT_LITERAL_NEG:
d00edca5
DD
720 if (left == NULL || right == NULL)
721 return NULL;
722 break;
723
724 /* These types only require one parameter. */
59727473
DD
725 case DEMANGLE_COMPONENT_VTABLE:
726 case DEMANGLE_COMPONENT_VTT:
59727473
DD
727 case DEMANGLE_COMPONENT_TYPEINFO:
728 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
729 case DEMANGLE_COMPONENT_TYPEINFO_FN:
730 case DEMANGLE_COMPONENT_THUNK:
731 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
732 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
733 case DEMANGLE_COMPONENT_JAVA_CLASS:
734 case DEMANGLE_COMPONENT_GUARD:
735 case DEMANGLE_COMPONENT_REFTEMP:
736 case DEMANGLE_COMPONENT_POINTER:
737 case DEMANGLE_COMPONENT_REFERENCE:
738 case DEMANGLE_COMPONENT_COMPLEX:
739 case DEMANGLE_COMPONENT_IMAGINARY:
740 case DEMANGLE_COMPONENT_VENDOR_TYPE:
741 case DEMANGLE_COMPONENT_ARGLIST:
742 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
743 case DEMANGLE_COMPONENT_CAST:
d00edca5
DD
744 if (left == NULL)
745 return NULL;
746 break;
747
748 /* This needs a right parameter, but the left parameter can be
749 empty. */
59727473 750 case DEMANGLE_COMPONENT_ARRAY_TYPE:
d00edca5
DD
751 if (right == NULL)
752 return NULL;
753 break;
754
755 /* These are allowed to have no parameters--in some cases they
756 will be filled in later. */
59727473
DD
757 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
758 case DEMANGLE_COMPONENT_RESTRICT:
759 case DEMANGLE_COMPONENT_VOLATILE:
760 case DEMANGLE_COMPONENT_CONST:
761 case DEMANGLE_COMPONENT_RESTRICT_THIS:
762 case DEMANGLE_COMPONENT_VOLATILE_THIS:
763 case DEMANGLE_COMPONENT_CONST_THIS:
d00edca5
DD
764 break;
765
766 /* Other types should not be seen here. */
767 default:
768 return NULL;
eb383413 769 }
d00edca5 770
59727473 771 p = d_make_empty (di);
d00edca5 772 if (p != NULL)
eb383413 773 {
59727473 774 p->type = type;
d00edca5
DD
775 p->u.s_binary.left = left;
776 p->u.s_binary.right = right;
eb383413 777 }
d00edca5
DD
778 return p;
779}
eb383413 780
d00edca5 781/* Add a new name component. */
03d5f569 782
59727473 783static struct demangle_component *
9334f9c6 784d_make_name (struct d_info *di, const char *s, int len)
d00edca5 785{
59727473 786 struct demangle_component *p;
03d5f569 787
59727473
DD
788 p = d_make_empty (di);
789 if (! cplus_demangle_fill_name (p, s, len))
858b45cf 790 return NULL;
d00edca5 791 return p;
eb383413
L
792}
793
d00edca5 794/* Add a new builtin type component. */
eb383413 795
59727473 796static struct demangle_component *
9334f9c6
DD
797d_make_builtin_type (struct d_info *di,
798 const struct demangle_builtin_type_info *type)
eb383413 799{
59727473 800 struct demangle_component *p;
d00edca5 801
331c3da2
DD
802 if (type == NULL)
803 return NULL;
59727473 804 p = d_make_empty (di);
d00edca5 805 if (p != NULL)
59727473
DD
806 {
807 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
808 p->u.s_builtin.type = type;
809 }
d00edca5
DD
810 return p;
811}
eb383413 812
d00edca5 813/* Add a new operator component. */
eb383413 814
59727473 815static struct demangle_component *
9334f9c6 816d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
eb383413 817{
59727473 818 struct demangle_component *p;
d00edca5 819
59727473 820 p = d_make_empty (di);
d00edca5 821 if (p != NULL)
59727473
DD
822 {
823 p->type = DEMANGLE_COMPONENT_OPERATOR;
824 p->u.s_operator.op = op;
825 }
d00edca5 826 return p;
eb383413
L
827}
828
d00edca5 829/* Add a new extended operator component. */
eb383413 830
59727473 831static struct demangle_component *
9334f9c6
DD
832d_make_extended_operator (struct d_info *di, int args,
833 struct demangle_component *name)
eb383413 834{
59727473 835 struct demangle_component *p;
03d5f569 836
59727473
DD
837 p = d_make_empty (di);
838 if (! cplus_demangle_fill_extended_operator (p, args, name))
331c3da2 839 return NULL;
d00edca5 840 return p;
eb383413
L
841}
842
d00edca5 843/* Add a new constructor component. */
eb383413 844
59727473 845static struct demangle_component *
9334f9c6
DD
846d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
847 struct demangle_component *name)
eb383413 848{
59727473 849 struct demangle_component *p;
d00edca5 850
59727473
DD
851 p = d_make_empty (di);
852 if (! cplus_demangle_fill_ctor (p, kind, name))
331c3da2 853 return NULL;
d00edca5 854 return p;
eb383413
L
855}
856
d00edca5 857/* Add a new destructor component. */
eb383413 858
59727473 859static struct demangle_component *
9334f9c6
DD
860d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
861 struct demangle_component *name)
eb383413 862{
59727473 863 struct demangle_component *p;
d00edca5 864
59727473
DD
865 p = d_make_empty (di);
866 if (! cplus_demangle_fill_dtor (p, kind, name))
331c3da2 867 return NULL;
d00edca5 868 return p;
eb383413
L
869}
870
d00edca5 871/* Add a new template parameter. */
59666b35 872
59727473 873static struct demangle_component *
9334f9c6 874d_make_template_param (struct d_info *di, long i)
59666b35 875{
59727473 876 struct demangle_component *p;
d00edca5 877
59727473 878 p = d_make_empty (di);
d00edca5 879 if (p != NULL)
59727473
DD
880 {
881 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
882 p->u.s_number.number = i;
883 }
d00edca5 884 return p;
59666b35
DD
885}
886
d00edca5 887/* Add a new standard substitution component. */
59666b35 888
59727473 889static struct demangle_component *
9334f9c6 890d_make_sub (struct d_info *di, const char *name, int len)
59666b35 891{
59727473 892 struct demangle_component *p;
d00edca5 893
59727473 894 p = d_make_empty (di);
d00edca5 895 if (p != NULL)
b6fb00c0 896 {
59727473 897 p->type = DEMANGLE_COMPONENT_SUB_STD;
b6fb00c0
DD
898 p->u.s_string.string = name;
899 p->u.s_string.len = len;
900 }
d00edca5 901 return p;
59666b35
DD
902}
903
331c3da2
DD
904/* <mangled-name> ::= _Z <encoding>
905
906 TOP_LEVEL is non-zero when called at the top level. */
59666b35 907
59727473
DD
908CP_STATIC_IF_GLIBCPP_V3
909struct demangle_component *
9334f9c6 910cplus_demangle_mangled_name (struct d_info *di, int top_level)
59666b35 911{
d00edca5
DD
912 if (d_next_char (di) != '_')
913 return NULL;
914 if (d_next_char (di) != 'Z')
915 return NULL;
331c3da2 916 return d_encoding (di, top_level);
59666b35
DD
917}
918
d00edca5
DD
919/* Return whether a function should have a return type. The argument
920 is the function name, which may be qualified in various ways. The
921 rules are that template functions have return types with some
922 exceptions, function types which are not part of a function name
923 mangling have return types with some exceptions, and non-template
924 function names do not have return types. The exceptions are that
925 constructors, destructors, and conversion operators do not have
926 return types. */
59666b35
DD
927
928static int
9334f9c6 929has_return_type (struct demangle_component *dc)
59666b35 930{
d00edca5
DD
931 if (dc == NULL)
932 return 0;
933 switch (dc->type)
934 {
935 default:
936 return 0;
59727473 937 case DEMANGLE_COMPONENT_TEMPLATE:
d00edca5 938 return ! is_ctor_dtor_or_conversion (d_left (dc));
59727473
DD
939 case DEMANGLE_COMPONENT_RESTRICT_THIS:
940 case DEMANGLE_COMPONENT_VOLATILE_THIS:
941 case DEMANGLE_COMPONENT_CONST_THIS:
54a962d9 942 return has_return_type (d_left (dc));
d00edca5 943 }
59666b35
DD
944}
945
d00edca5
DD
946/* Return whether a name is a constructor, a destructor, or a
947 conversion operator. */
eb383413
L
948
949static int
9334f9c6 950is_ctor_dtor_or_conversion (struct demangle_component *dc)
eb383413 951{
d00edca5
DD
952 if (dc == NULL)
953 return 0;
954 switch (dc->type)
955 {
956 default:
957 return 0;
59727473
DD
958 case DEMANGLE_COMPONENT_QUAL_NAME:
959 case DEMANGLE_COMPONENT_LOCAL_NAME:
d00edca5 960 return is_ctor_dtor_or_conversion (d_right (dc));
59727473
DD
961 case DEMANGLE_COMPONENT_CTOR:
962 case DEMANGLE_COMPONENT_DTOR:
963 case DEMANGLE_COMPONENT_CAST:
d00edca5
DD
964 return 1;
965 }
eb383413
L
966}
967
d00edca5
DD
968/* <encoding> ::= <(function) name> <bare-function-type>
969 ::= <(data) name>
6d95373e
DD
970 ::= <special-name>
971
972 TOP_LEVEL is non-zero when called at the top level, in which case
973 if DMGL_PARAMS is not set we do not demangle the function
974 parameters. We only set this at the top level, because otherwise
975 we would not correctly demangle names in local scopes. */
eb383413 976
59727473 977static struct demangle_component *
9334f9c6 978d_encoding (struct d_info *di, int top_level)
eb383413 979{
d00edca5 980 char peek = d_peek_char (di);
03d5f569 981
d00edca5
DD
982 if (peek == 'G' || peek == 'T')
983 return d_special_name (di);
984 else
03d5f569 985 {
59727473 986 struct demangle_component *dc;
d00edca5
DD
987
988 dc = d_name (di);
331c3da2
DD
989
990 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
991 {
992 /* Strip off any initial CV-qualifiers, as they really apply
993 to the `this' parameter, and they were not output by the
994 v2 demangler without DMGL_PARAMS. */
59727473
DD
995 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
996 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
997 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
331c3da2 998 dc = d_left (dc);
820542c9 999
59727473
DD
1000 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1001 there may be CV-qualifiers on its right argument which
1002 really apply here; this happens when parsing a class
1003 which is local to a function. */
1004 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
820542c9 1005 {
59727473 1006 struct demangle_component *dcr;
820542c9
DD
1007
1008 dcr = d_right (dc);
59727473
DD
1009 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1010 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1011 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
820542c9
DD
1012 dcr = d_left (dcr);
1013 dc->u.s_binary.right = dcr;
1014 }
1015
331c3da2
DD
1016 return dc;
1017 }
1018
d00edca5 1019 peek = d_peek_char (di);
331c3da2 1020 if (peek == '\0' || peek == 'E')
d00edca5 1021 return dc;
59727473 1022 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
d00edca5 1023 d_bare_function_type (di, has_return_type (dc)));
03d5f569 1024 }
d00edca5
DD
1025}
1026
1027/* <name> ::= <nested-name>
1028 ::= <unscoped-name>
1029 ::= <unscoped-template-name> <template-args>
1030 ::= <local-name>
1031
1032 <unscoped-name> ::= <unqualified-name>
1033 ::= St <unqualified-name>
eb383413 1034
d00edca5
DD
1035 <unscoped-template-name> ::= <unscoped-name>
1036 ::= <substitution>
1037*/
1038
59727473 1039static struct demangle_component *
9334f9c6 1040d_name (struct d_info *di)
d00edca5
DD
1041{
1042 char peek = d_peek_char (di);
59727473 1043 struct demangle_component *dc;
d00edca5
DD
1044
1045 switch (peek)
eb383413 1046 {
d00edca5
DD
1047 case 'N':
1048 return d_nested_name (di);
1049
1050 case 'Z':
1051 return d_local_name (di);
1052
1053 case 'S':
1054 {
1055 int subst;
1056
1057 if (d_peek_next_char (di) != 't')
1058 {
97ceaf5b 1059 dc = d_substitution (di, 0);
d00edca5
DD
1060 subst = 1;
1061 }
1062 else
1063 {
1064 d_advance (di, 2);
59727473
DD
1065 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1066 d_make_name (di, "std", 3),
d00edca5 1067 d_unqualified_name (di));
b6fb00c0 1068 di->expansion += 3;
d00edca5
DD
1069 subst = 0;
1070 }
1071
1072 if (d_peek_char (di) != 'I')
1073 {
1074 /* The grammar does not permit this case to occur if we
1075 called d_substitution() above (i.e., subst == 1). We
1076 don't bother to check. */
1077 }
1078 else
1079 {
1080 /* This is <template-args>, which means that we just saw
1081 <unscoped-template-name>, which is a substitution
1082 candidate if we didn't just get it from a
1083 substitution. */
1084 if (! subst)
1085 {
1086 if (! d_add_substitution (di, dc))
1087 return NULL;
1088 }
59727473
DD
1089 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1090 d_template_args (di));
d00edca5
DD
1091 }
1092
1093 return dc;
1094 }
1095
1096 default:
1097 dc = d_unqualified_name (di);
1098 if (d_peek_char (di) == 'I')
03d5f569 1099 {
d00edca5
DD
1100 /* This is <template-args>, which means that we just saw
1101 <unscoped-template-name>, which is a substitution
1102 candidate. */
1103 if (! d_add_substitution (di, dc))
1104 return NULL;
59727473
DD
1105 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1106 d_template_args (di));
03d5f569 1107 }
d00edca5 1108 return dc;
eb383413 1109 }
d00edca5 1110}
eb383413 1111
d00edca5
DD
1112/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1113 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1114*/
eb383413 1115
59727473 1116static struct demangle_component *
9334f9c6 1117d_nested_name (struct d_info *di)
d00edca5 1118{
59727473
DD
1119 struct demangle_component *ret;
1120 struct demangle_component **pret;
03d5f569 1121
d00edca5
DD
1122 if (d_next_char (di) != 'N')
1123 return NULL;
eb383413 1124
858b45cf 1125 pret = d_cv_qualifiers (di, &ret, 1);
d00edca5
DD
1126 if (pret == NULL)
1127 return NULL;
1128
1129 *pret = d_prefix (di);
1130 if (*pret == NULL)
1131 return NULL;
eb383413 1132
d00edca5 1133 if (d_next_char (di) != 'E')
eb383413
L
1134 return NULL;
1135
d00edca5 1136 return ret;
eb383413
L
1137}
1138
d00edca5
DD
1139/* <prefix> ::= <prefix> <unqualified-name>
1140 ::= <template-prefix> <template-args>
1141 ::= <template-param>
1142 ::=
1143 ::= <substitution>
eb383413 1144
d00edca5
DD
1145 <template-prefix> ::= <prefix> <(template) unqualified-name>
1146 ::= <template-param>
1147 ::= <substitution>
1148*/
1149
59727473 1150static struct demangle_component *
9334f9c6 1151d_prefix (struct d_info *di)
eb383413 1152{
59727473 1153 struct demangle_component *ret = NULL;
eb383413 1154
d00edca5 1155 while (1)
eb383413 1156 {
d00edca5 1157 char peek;
59727473
DD
1158 enum demangle_component_type comb_type;
1159 struct demangle_component *dc;
d00edca5
DD
1160
1161 peek = d_peek_char (di);
1162 if (peek == '\0')
1163 return NULL;
1164
1165 /* The older code accepts a <local-name> here, but I don't see
1166 that in the grammar. The older code does not accept a
1167 <template-param> here. */
eb383413 1168
59727473 1169 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
d00edca5 1170 if (IS_DIGIT (peek)
858b45cf 1171 || IS_LOWER (peek)
d00edca5
DD
1172 || peek == 'C'
1173 || peek == 'D')
1174 dc = d_unqualified_name (di);
1175 else if (peek == 'S')
97ceaf5b 1176 dc = d_substitution (di, 1);
d00edca5
DD
1177 else if (peek == 'I')
1178 {
1179 if (ret == NULL)
1180 return NULL;
59727473 1181 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
d00edca5
DD
1182 dc = d_template_args (di);
1183 }
1184 else if (peek == 'T')
1185 dc = d_template_param (di);
1186 else if (peek == 'E')
1187 return ret;
1188 else
1189 return NULL;
1190
1191 if (ret == NULL)
1192 ret = dc;
eb383413 1193 else
d00edca5
DD
1194 ret = d_make_comp (di, comb_type, ret, dc);
1195
1196 if (peek != 'S' && d_peek_char (di) != 'E')
1197 {
1198 if (! d_add_substitution (di, ret))
1199 return NULL;
1200 }
eb383413
L
1201 }
1202}
1203
d00edca5
DD
1204/* <unqualified-name> ::= <operator-name>
1205 ::= <ctor-dtor-name>
1206 ::= <source-name>
1207*/
eb383413 1208
59727473 1209static struct demangle_component *
9334f9c6 1210d_unqualified_name (struct d_info *di)
eb383413 1211{
d00edca5
DD
1212 char peek;
1213
1214 peek = d_peek_char (di);
1215 if (IS_DIGIT (peek))
1216 return d_source_name (di);
858b45cf 1217 else if (IS_LOWER (peek))
b6fb00c0 1218 {
59727473 1219 struct demangle_component *ret;
b6fb00c0
DD
1220
1221 ret = d_operator_name (di);
59727473 1222 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
b6fb00c0
DD
1223 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1224 return ret;
1225 }
d00edca5
DD
1226 else if (peek == 'C' || peek == 'D')
1227 return d_ctor_dtor_name (di);
1228 else
03d5f569 1229 return NULL;
eb383413
L
1230}
1231
d00edca5 1232/* <source-name> ::= <(positive length) number> <identifier> */
eb383413 1233
59727473 1234static struct demangle_component *
9334f9c6 1235d_source_name (struct d_info *di)
eb383413 1236{
d00edca5 1237 long len;
59727473 1238 struct demangle_component *ret;
d00edca5
DD
1239
1240 len = d_number (di);
1241 if (len <= 0)
1242 return NULL;
1243 ret = d_identifier (di, len);
1244 di->last_name = ret;
1245 return ret;
eb383413
L
1246}
1247
d00edca5 1248/* number ::= [n] <(non-negative decimal integer)> */
eb383413 1249
d00edca5 1250static long
9334f9c6 1251d_number (struct d_info *di)
eb383413 1252{
b6fb00c0 1253 int negative;
d00edca5
DD
1254 char peek;
1255 long ret;
eb383413 1256
b6fb00c0 1257 negative = 0;
d00edca5
DD
1258 peek = d_peek_char (di);
1259 if (peek == 'n')
1260 {
b6fb00c0 1261 negative = 1;
d00edca5
DD
1262 d_advance (di, 1);
1263 peek = d_peek_char (di);
1264 }
eb383413 1265
d00edca5
DD
1266 ret = 0;
1267 while (1)
eb383413 1268 {
d00edca5 1269 if (! IS_DIGIT (peek))
b6fb00c0
DD
1270 {
1271 if (negative)
1272 ret = - ret;
1273 return ret;
1274 }
d00edca5
DD
1275 ret = ret * 10 + peek - '0';
1276 d_advance (di, 1);
1277 peek = d_peek_char (di);
eb383413 1278 }
eb383413
L
1279}
1280
d00edca5 1281/* identifier ::= <(unqualified source code identifier)> */
eb383413 1282
59727473 1283static struct demangle_component *
9334f9c6 1284d_identifier (struct d_info *di, int len)
eb383413 1285{
d00edca5 1286 const char *name;
eb383413 1287
d00edca5 1288 name = d_str (di);
b6fb00c0
DD
1289
1290 if (di->send - name < len)
1291 return NULL;
1292
d00edca5 1293 d_advance (di, len);
eb383413 1294
2730f651
DD
1295 /* A Java mangled name may have a trailing '$' if it is a C++
1296 keyword. This '$' is not included in the length count. We just
1297 ignore the '$'. */
1298 if ((di->options & DMGL_JAVA) != 0
1299 && d_peek_char (di) == '$')
1300 d_advance (di, 1);
1301
d00edca5
DD
1302 /* Look for something which looks like a gcc encoding of an
1303 anonymous namespace, and replace it with a more user friendly
1304 name. */
1305 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1306 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1307 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
eb383413 1308 {
d00edca5
DD
1309 const char *s;
1310
1311 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1312 if ((*s == '.' || *s == '_' || *s == '$')
1313 && s[1] == 'N')
b6fb00c0
DD
1314 {
1315 di->expansion -= len - sizeof "(anonymous namespace)";
1316 return d_make_name (di, "(anonymous namespace)",
1317 sizeof "(anonymous namespace)" - 1);
1318 }
eb383413 1319 }
d00edca5
DD
1320
1321 return d_make_name (di, name, len);
eb383413
L
1322}
1323
d00edca5
DD
1324/* operator_name ::= many different two character encodings.
1325 ::= cv <type>
1326 ::= v <digit> <source-name>
1327*/
eb383413 1328
b6fb00c0
DD
1329#define NL(s) s, (sizeof s) - 1
1330
59727473
DD
1331CP_STATIC_IF_GLIBCPP_V3
1332const struct demangle_operator_info cplus_demangle_operators[] =
d00edca5 1333{
b6fb00c0
DD
1334 { "aN", NL ("&="), 2 },
1335 { "aS", NL ("="), 2 },
1336 { "aa", NL ("&&"), 2 },
1337 { "ad", NL ("&"), 1 },
1338 { "an", NL ("&"), 2 },
1339 { "cl", NL ("()"), 0 },
1340 { "cm", NL (","), 2 },
1341 { "co", NL ("~"), 1 },
1342 { "dV", NL ("/="), 2 },
1343 { "da", NL ("delete[]"), 1 },
1344 { "de", NL ("*"), 1 },
1345 { "dl", NL ("delete"), 1 },
1346 { "dv", NL ("/"), 2 },
1347 { "eO", NL ("^="), 2 },
1348 { "eo", NL ("^"), 2 },
1349 { "eq", NL ("=="), 2 },
1350 { "ge", NL (">="), 2 },
1351 { "gt", NL (">"), 2 },
1352 { "ix", NL ("[]"), 2 },
1353 { "lS", NL ("<<="), 2 },
1354 { "le", NL ("<="), 2 },
1355 { "ls", NL ("<<"), 2 },
1356 { "lt", NL ("<"), 2 },
1357 { "mI", NL ("-="), 2 },
1358 { "mL", NL ("*="), 2 },
1359 { "mi", NL ("-"), 2 },
1360 { "ml", NL ("*"), 2 },
1361 { "mm", NL ("--"), 1 },
1362 { "na", NL ("new[]"), 1 },
1363 { "ne", NL ("!="), 2 },
1364 { "ng", NL ("-"), 1 },
1365 { "nt", NL ("!"), 1 },
1366 { "nw", NL ("new"), 1 },
1367 { "oR", NL ("|="), 2 },
1368 { "oo", NL ("||"), 2 },
1369 { "or", NL ("|"), 2 },
1370 { "pL", NL ("+="), 2 },
1371 { "pl", NL ("+"), 2 },
1372 { "pm", NL ("->*"), 2 },
1373 { "pp", NL ("++"), 1 },
1374 { "ps", NL ("+"), 1 },
1375 { "pt", NL ("->"), 2 },
1376 { "qu", NL ("?"), 3 },
1377 { "rM", NL ("%="), 2 },
1378 { "rS", NL (">>="), 2 },
1379 { "rm", NL ("%"), 2 },
1380 { "rs", NL (">>"), 2 },
1381 { "st", NL ("sizeof "), 1 },
59727473
DD
1382 { "sz", NL ("sizeof "), 1 },
1383 { NULL, NULL, 0, 0 }
d00edca5 1384};
eb383413 1385
59727473 1386static struct demangle_component *
9334f9c6 1387d_operator_name (struct d_info *di)
eb383413 1388{
d00edca5
DD
1389 char c1;
1390 char c2;
eb383413 1391
d00edca5
DD
1392 c1 = d_next_char (di);
1393 c2 = d_next_char (di);
1394 if (c1 == 'v' && IS_DIGIT (c2))
1395 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1396 else if (c1 == 'c' && c2 == 'v')
59727473
DD
1397 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1398 cplus_demangle_type (di), NULL);
d00edca5 1399 else
eb383413 1400 {
59727473 1401 /* LOW is the inclusive lower bound. */
d00edca5 1402 int low = 0;
59727473
DD
1403 /* HIGH is the exclusive upper bound. We subtract one to ignore
1404 the sentinel at the end of the array. */
1405 int high = ((sizeof (cplus_demangle_operators)
1406 / sizeof (cplus_demangle_operators[0]))
1407 - 1);
eb383413 1408
d00edca5
DD
1409 while (1)
1410 {
1411 int i;
59727473 1412 const struct demangle_operator_info *p;
eb383413 1413
d00edca5 1414 i = low + (high - low) / 2;
59727473 1415 p = cplus_demangle_operators + i;
eb383413 1416
d00edca5
DD
1417 if (c1 == p->code[0] && c2 == p->code[1])
1418 return d_make_operator (di, p);
1419
1420 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1421 high = i;
1422 else
1423 low = i + 1;
1424 if (low == high)
1425 return NULL;
1426 }
1427 }
eb383413
L
1428}
1429
d00edca5
DD
1430/* <special-name> ::= TV <type>
1431 ::= TT <type>
1432 ::= TI <type>
1433 ::= TS <type>
1434 ::= GV <(object) name>
1435 ::= T <call-offset> <(base) encoding>
1436 ::= Tc <call-offset> <call-offset> <(base) encoding>
1437 Also g++ extensions:
1438 ::= TC <type> <(offset) number> _ <(base) type>
1439 ::= TF <type>
1440 ::= TJ <type>
1441 ::= GR <name>
1442*/
eb383413 1443
59727473 1444static struct demangle_component *
9334f9c6 1445d_special_name (struct d_info *di)
eb383413 1446{
d00edca5 1447 char c;
eb383413 1448
b6fb00c0 1449 di->expansion += 20;
d00edca5
DD
1450 c = d_next_char (di);
1451 if (c == 'T')
03d5f569 1452 {
d00edca5
DD
1453 switch (d_next_char (di))
1454 {
1455 case 'V':
b6fb00c0 1456 di->expansion -= 5;
59727473
DD
1457 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1458 cplus_demangle_type (di), NULL);
d00edca5 1459 case 'T':
b6fb00c0 1460 di->expansion -= 10;
59727473
DD
1461 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1462 cplus_demangle_type (di), NULL);
d00edca5 1463 case 'I':
59727473
DD
1464 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1465 cplus_demangle_type (di), NULL);
d00edca5 1466 case 'S':
59727473
DD
1467 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1468 cplus_demangle_type (di), NULL);
eb383413 1469
d00edca5
DD
1470 case 'h':
1471 if (! d_call_offset (di, 'h'))
1472 return NULL;
59727473
DD
1473 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1474 d_encoding (di, 0), NULL);
eb383413 1475
d00edca5
DD
1476 case 'v':
1477 if (! d_call_offset (di, 'v'))
1478 return NULL;
59727473
DD
1479 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1480 d_encoding (di, 0), NULL);
eb383413 1481
d00edca5
DD
1482 case 'c':
1483 if (! d_call_offset (di, '\0'))
1484 return NULL;
1485 if (! d_call_offset (di, '\0'))
1486 return NULL;
59727473
DD
1487 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1488 d_encoding (di, 0), NULL);
eb383413 1489
d00edca5
DD
1490 case 'C':
1491 {
59727473 1492 struct demangle_component *derived_type;
d00edca5 1493 long offset;
59727473 1494 struct demangle_component *base_type;
d00edca5 1495
59727473 1496 derived_type = cplus_demangle_type (di);
d00edca5
DD
1497 offset = d_number (di);
1498 if (offset < 0)
1499 return NULL;
1500 if (d_next_char (di) != '_')
1501 return NULL;
59727473 1502 base_type = cplus_demangle_type (di);
d00edca5
DD
1503 /* We don't display the offset. FIXME: We should display
1504 it in verbose mode. */
b6fb00c0 1505 di->expansion += 5;
59727473
DD
1506 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1507 base_type, derived_type);
d00edca5 1508 }
eb383413 1509
d00edca5 1510 case 'F':
59727473
DD
1511 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1512 cplus_demangle_type (di), NULL);
d00edca5 1513 case 'J':
59727473
DD
1514 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1515 cplus_demangle_type (di), NULL);
eb383413 1516
d00edca5
DD
1517 default:
1518 return NULL;
1519 }
eb383413 1520 }
d00edca5 1521 else if (c == 'G')
eb383413 1522 {
d00edca5
DD
1523 switch (d_next_char (di))
1524 {
1525 case 'V':
59727473 1526 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
d00edca5
DD
1527
1528 case 'R':
59727473
DD
1529 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1530 NULL);
d00edca5
DD
1531
1532 default:
1533 return NULL;
1534 }
eb383413 1535 }
d00edca5
DD
1536 else
1537 return NULL;
eb383413
L
1538}
1539
d00edca5
DD
1540/* <call-offset> ::= h <nv-offset> _
1541 ::= v <v-offset> _
eb383413 1542
d00edca5 1543 <nv-offset> ::= <(offset) number>
eb383413 1544
d00edca5 1545 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
eb383413 1546
d00edca5
DD
1547 The C parameter, if not '\0', is a character we just read which is
1548 the start of the <call-offset>.
eb383413 1549
d00edca5
DD
1550 We don't display the offset information anywhere. FIXME: We should
1551 display it in verbose mode. */
eb383413 1552
d00edca5 1553static int
9334f9c6 1554d_call_offset (struct d_info *di, int c)
eb383413 1555{
d00edca5
DD
1556 if (c == '\0')
1557 c = d_next_char (di);
eb383413 1558
d00edca5 1559 if (c == 'h')
eb129e35 1560 d_number (di);
d00edca5 1561 else if (c == 'v')
eb383413 1562 {
eb129e35 1563 d_number (di);
d00edca5
DD
1564 if (d_next_char (di) != '_')
1565 return 0;
eb129e35 1566 d_number (di);
eb383413 1567 }
d00edca5
DD
1568 else
1569 return 0;
eb383413 1570
d00edca5
DD
1571 if (d_next_char (di) != '_')
1572 return 0;
eb383413 1573
d00edca5 1574 return 1;
eb383413
L
1575}
1576
d00edca5
DD
1577/* <ctor-dtor-name> ::= C1
1578 ::= C2
1579 ::= C3
1580 ::= D0
1581 ::= D1
1582 ::= D2
1583*/
1584
59727473 1585static struct demangle_component *
9334f9c6 1586d_ctor_dtor_name (struct d_info *di)
d00edca5 1587{
b6fb00c0
DD
1588 if (di->last_name != NULL)
1589 {
59727473 1590 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
b6fb00c0 1591 di->expansion += di->last_name->u.s_name.len;
59727473 1592 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
b6fb00c0
DD
1593 di->expansion += di->last_name->u.s_string.len;
1594 }
d00edca5
DD
1595 switch (d_next_char (di))
1596 {
1597 case 'C':
1598 {
1599 enum gnu_v3_ctor_kinds kind;
1600
1601 switch (d_next_char (di))
1602 {
1603 case '1':
1604 kind = gnu_v3_complete_object_ctor;
1605 break;
1606 case '2':
1607 kind = gnu_v3_base_object_ctor;
1608 break;
1609 case '3':
1610 kind = gnu_v3_complete_object_allocating_ctor;
1611 break;
1612 default:
1613 return NULL;
1614 }
1615 return d_make_ctor (di, kind, di->last_name);
1616 }
1617
1618 case 'D':
1619 {
1620 enum gnu_v3_dtor_kinds kind;
1621
1622 switch (d_next_char (di))
1623 {
1624 case '0':
1625 kind = gnu_v3_deleting_dtor;
1626 break;
1627 case '1':
1628 kind = gnu_v3_complete_object_dtor;
1629 break;
1630 case '2':
1631 kind = gnu_v3_base_object_dtor;
1632 break;
1633 default:
1634 return NULL;
1635 }
1636 return d_make_dtor (di, kind, di->last_name);
1637 }
eb383413 1638
d00edca5
DD
1639 default:
1640 return NULL;
1641 }
1642}
eb383413 1643
d00edca5
DD
1644/* <type> ::= <builtin-type>
1645 ::= <function-type>
1646 ::= <class-enum-type>
1647 ::= <array-type>
1648 ::= <pointer-to-member-type>
1649 ::= <template-param>
1650 ::= <template-template-param> <template-args>
1651 ::= <substitution>
1652 ::= <CV-qualifiers> <type>
1653 ::= P <type>
1654 ::= R <type>
1655 ::= C <type>
1656 ::= G <type>
1657 ::= U <source-name> <type>
1658
1659 <builtin-type> ::= various one letter codes
1660 ::= u <source-name>
1661*/
eb383413 1662
59727473
DD
1663CP_STATIC_IF_GLIBCPP_V3
1664const struct demangle_builtin_type_info
1665cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
d00edca5 1666{
2d733211 1667 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
b6fb00c0 1668 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2d733211
DD
1669 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1670 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1671 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1672 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1673 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1674 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
b6fb00c0 1675 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2d733211 1676 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
b6fb00c0
DD
1677 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1678 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2d733211 1679 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
b6fb00c0 1680 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2d733211
DD
1681 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1682 D_PRINT_DEFAULT },
b6fb00c0
DD
1683 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1684 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1685 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2d733211
DD
1686 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1687 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
b6fb00c0
DD
1688 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1689 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2d733211
DD
1690 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1691 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1692 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1693 D_PRINT_UNSIGNED_LONG_LONG },
b6fb00c0 1694 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
d00edca5 1695};
eb383413 1696
59727473
DD
1697CP_STATIC_IF_GLIBCPP_V3
1698struct demangle_component *
9334f9c6 1699cplus_demangle_type (struct d_info *di)
eb383413 1700{
d00edca5 1701 char peek;
59727473 1702 struct demangle_component *ret;
d00edca5
DD
1703 int can_subst;
1704
1705 /* The ABI specifies that when CV-qualifiers are used, the base type
1706 is substitutable, and the fully qualified type is substitutable,
1707 but the base type with a strict subset of the CV-qualifiers is
1708 not substitutable. The natural recursive implementation of the
1709 CV-qualifiers would cause subsets to be substitutable, so instead
1710 we pull them all off now.
1711
331c3da2
DD
1712 FIXME: The ABI says that order-insensitive vendor qualifiers
1713 should be handled in the same way, but we have no way to tell
1714 which vendor qualifiers are order-insensitive and which are
1715 order-sensitive. So we just assume that they are all
1716 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1717 __vector, and it treats it as order-sensitive when mangling
1718 names. */
d00edca5
DD
1719
1720 peek = d_peek_char (di);
1721 if (peek == 'r' || peek == 'V' || peek == 'K')
1722 {
59727473 1723 struct demangle_component **pret;
74bcd529 1724
858b45cf 1725 pret = d_cv_qualifiers (di, &ret, 0);
331c3da2
DD
1726 if (pret == NULL)
1727 return NULL;
59727473 1728 *pret = cplus_demangle_type (di);
d00edca5
DD
1729 if (! d_add_substitution (di, ret))
1730 return NULL;
1731 return ret;
1732 }
eb383413 1733
d00edca5 1734 can_subst = 1;
eb383413 1735
74bcd529 1736 switch (peek)
eb383413 1737 {
d00edca5
DD
1738 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1739 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1740 case 'o': case 's': case 't':
1741 case 'v': case 'w': case 'x': case 'y': case 'z':
59727473
DD
1742 ret = d_make_builtin_type (di,
1743 &cplus_demangle_builtin_types[peek - 'a']);
b6fb00c0 1744 di->expansion += ret->u.s_builtin.type->len;
d00edca5
DD
1745 can_subst = 0;
1746 d_advance (di, 1);
1747 break;
1748
1749 case 'u':
1750 d_advance (di, 1);
59727473
DD
1751 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1752 d_source_name (di), NULL);
d00edca5
DD
1753 break;
1754
1755 case 'F':
1756 ret = d_function_type (di);
eb383413
L
1757 break;
1758
d00edca5
DD
1759 case '0': case '1': case '2': case '3': case '4':
1760 case '5': case '6': case '7': case '8': case '9':
1761 case 'N':
eb383413 1762 case 'Z':
d00edca5 1763 ret = d_class_enum_type (di);
eb383413
L
1764 break;
1765
d00edca5
DD
1766 case 'A':
1767 ret = d_array_type (di);
1768 break;
1769
1770 case 'M':
1771 ret = d_pointer_to_member_type (di);
1772 break;
1773
1774 case 'T':
1775 ret = d_template_param (di);
1776 if (d_peek_char (di) == 'I')
03d5f569 1777 {
d00edca5
DD
1778 /* This is <template-template-param> <template-args>. The
1779 <template-template-param> part is a substitution
1780 candidate. */
1781 if (! d_add_substitution (di, ret))
1782 return NULL;
59727473
DD
1783 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1784 d_template_args (di));
03d5f569 1785 }
d00edca5
DD
1786 break;
1787
1788 case 'S':
1789 /* If this is a special substitution, then it is the start of
1790 <class-enum-type>. */
1791 {
1792 char peek_next;
74bcd529 1793
d00edca5
DD
1794 peek_next = d_peek_next_char (di);
1795 if (IS_DIGIT (peek_next)
1796 || peek_next == '_'
858b45cf 1797 || IS_UPPER (peek_next))
d00edca5 1798 {
97ceaf5b 1799 ret = d_substitution (di, 0);
d00edca5
DD
1800 /* The substituted name may have been a template name and
1801 may be followed by tepmlate args. */
1802 if (d_peek_char (di) == 'I')
59727473 1803 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
d00edca5
DD
1804 d_template_args (di));
1805 else
1806 can_subst = 0;
1807 }
1808 else
1809 {
1810 ret = d_class_enum_type (di);
1811 /* If the substitution was a complete type, then it is not
1812 a new substitution candidate. However, if the
1813 substitution was followed by template arguments, then
1814 the whole thing is a substitution candidate. */
59727473 1815 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
d00edca5
DD
1816 can_subst = 0;
1817 }
1818 }
eb383413
L
1819 break;
1820
d00edca5
DD
1821 case 'P':
1822 d_advance (di, 1);
59727473
DD
1823 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1824 cplus_demangle_type (di), NULL);
d00edca5 1825 break;
eb383413 1826
d00edca5
DD
1827 case 'R':
1828 d_advance (di, 1);
59727473
DD
1829 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1830 cplus_demangle_type (di), NULL);
d00edca5 1831 break;
eb383413 1832
d00edca5
DD
1833 case 'C':
1834 d_advance (di, 1);
59727473
DD
1835 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1836 cplus_demangle_type (di), NULL);
d00edca5
DD
1837 break;
1838
1839 case 'G':
1840 d_advance (di, 1);
59727473
DD
1841 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1842 cplus_demangle_type (di), NULL);
d00edca5 1843 break;
eb383413 1844
d00edca5
DD
1845 case 'U':
1846 d_advance (di, 1);
1847 ret = d_source_name (di);
59727473
DD
1848 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1849 cplus_demangle_type (di), ret);
eb383413 1850 break;
d00edca5
DD
1851
1852 default:
1853 return NULL;
eb383413
L
1854 }
1855
d00edca5
DD
1856 if (can_subst)
1857 {
1858 if (! d_add_substitution (di, ret))
1859 return NULL;
1860 }
eb383413 1861
d00edca5
DD
1862 return ret;
1863}
eb383413 1864
d00edca5 1865/* <CV-qualifiers> ::= [r] [V] [K] */
eb383413 1866
59727473 1867static struct demangle_component **
9334f9c6
DD
1868d_cv_qualifiers (struct d_info *di,
1869 struct demangle_component **pret, int member_fn)
eb383413
L
1870{
1871 char peek;
1872
d00edca5
DD
1873 peek = d_peek_char (di);
1874 while (peek == 'r' || peek == 'V' || peek == 'K')
eb383413 1875 {
59727473 1876 enum demangle_component_type t;
59666b35 1877
d00edca5
DD
1878 d_advance (di, 1);
1879 if (peek == 'r')
b6fb00c0 1880 {
59727473
DD
1881 t = (member_fn
1882 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1883 : DEMANGLE_COMPONENT_RESTRICT);
b6fb00c0
DD
1884 di->expansion += sizeof "restrict";
1885 }
d00edca5 1886 else if (peek == 'V')
b6fb00c0 1887 {
59727473
DD
1888 t = (member_fn
1889 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1890 : DEMANGLE_COMPONENT_VOLATILE);
b6fb00c0
DD
1891 di->expansion += sizeof "volatile";
1892 }
d00edca5 1893 else
b6fb00c0 1894 {
59727473
DD
1895 t = (member_fn
1896 ? DEMANGLE_COMPONENT_CONST_THIS
1897 : DEMANGLE_COMPONENT_CONST);
b6fb00c0
DD
1898 di->expansion += sizeof "const";
1899 }
eb383413 1900
d00edca5
DD
1901 *pret = d_make_comp (di, t, NULL, NULL);
1902 if (*pret == NULL)
1903 return NULL;
1904 pret = &d_left (*pret);
eb383413 1905
d00edca5
DD
1906 peek = d_peek_char (di);
1907 }
eb383413 1908
d00edca5
DD
1909 return pret;
1910}
eb383413 1911
d00edca5 1912/* <function-type> ::= F [Y] <bare-function-type> E */
eb383413 1913
59727473 1914static struct demangle_component *
9334f9c6 1915d_function_type (struct d_info *di)
eb383413 1916{
59727473 1917 struct demangle_component *ret;
eb383413 1918
d00edca5
DD
1919 if (d_next_char (di) != 'F')
1920 return NULL;
1921 if (d_peek_char (di) == 'Y')
1922 {
1923 /* Function has C linkage. We don't print this information.
1924 FIXME: We should print it in verbose mode. */
1925 d_advance (di, 1);
1926 }
1927 ret = d_bare_function_type (di, 1);
1928 if (d_next_char (di) != 'E')
1929 return NULL;
1930 return ret;
1931}
74bcd529 1932
d00edca5 1933/* <bare-function-type> ::= <type>+ */
eb383413 1934
59727473 1935static struct demangle_component *
9334f9c6 1936d_bare_function_type (struct d_info *di, int has_return_type)
d00edca5 1937{
59727473
DD
1938 struct demangle_component *return_type;
1939 struct demangle_component *tl;
1940 struct demangle_component **ptl;
eb383413 1941
d00edca5
DD
1942 return_type = NULL;
1943 tl = NULL;
1944 ptl = &tl;
eb383413
L
1945 while (1)
1946 {
1947 char peek;
59727473 1948 struct demangle_component *type;
eb383413 1949
d00edca5
DD
1950 peek = d_peek_char (di);
1951 if (peek == '\0' || peek == 'E')
1952 break;
59727473 1953 type = cplus_demangle_type (di);
d00edca5
DD
1954 if (type == NULL)
1955 return NULL;
1956 if (has_return_type)
eb383413 1957 {
d00edca5
DD
1958 return_type = type;
1959 has_return_type = 0;
eb383413 1960 }
d00edca5 1961 else
eb383413 1962 {
59727473 1963 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
331c3da2
DD
1964 if (*ptl == NULL)
1965 return NULL;
d00edca5 1966 ptl = &d_right (*ptl);
eb383413 1967 }
eb383413 1968 }
eb383413 1969
d00edca5
DD
1970 /* There should be at least one parameter type besides the optional
1971 return type. A function which takes no arguments will have a
1972 single parameter type void. */
1973 if (tl == NULL)
1974 return NULL;
eb383413 1975
d00edca5
DD
1976 /* If we have a single parameter type void, omit it. */
1977 if (d_right (tl) == NULL
59727473 1978 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
d00edca5 1979 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
b6fb00c0
DD
1980 {
1981 di->expansion -= d_left (tl)->u.s_builtin.type->len;
1982 tl = NULL;
1983 }
eb383413 1984
59727473 1985 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
d00edca5 1986}
eb383413 1987
d00edca5 1988/* <class-enum-type> ::= <name> */
eb383413 1989
59727473 1990static struct demangle_component *
9334f9c6 1991d_class_enum_type (struct d_info *di)
d00edca5
DD
1992{
1993 return d_name (di);
1994}
74bcd529 1995
d00edca5
DD
1996/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1997 ::= A [<(dimension) expression>] _ <(element) type>
1998*/
74bcd529 1999
59727473 2000static struct demangle_component *
9334f9c6 2001d_array_type (struct d_info *di)
d00edca5
DD
2002{
2003 char peek;
59727473 2004 struct demangle_component *dim;
74bcd529 2005
d00edca5
DD
2006 if (d_next_char (di) != 'A')
2007 return NULL;
2008
2009 peek = d_peek_char (di);
2010 if (peek == '_')
2011 dim = NULL;
2012 else if (IS_DIGIT (peek))
74bcd529 2013 {
d00edca5 2014 const char *s;
74bcd529 2015
d00edca5
DD
2016 s = d_str (di);
2017 do
2018 {
2019 d_advance (di, 1);
2020 peek = d_peek_char (di);
2021 }
2022 while (IS_DIGIT (peek));
2023 dim = d_make_name (di, s, d_str (di) - s);
331c3da2
DD
2024 if (dim == NULL)
2025 return NULL;
74bcd529 2026 }
eb383413 2027 else
d00edca5
DD
2028 {
2029 dim = d_expression (di);
2030 if (dim == NULL)
2031 return NULL;
2032 }
eb383413 2033
d00edca5
DD
2034 if (d_next_char (di) != '_')
2035 return NULL;
eb383413 2036
59727473
DD
2037 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2038 cplus_demangle_type (di));
d00edca5 2039}
eb383413 2040
d00edca5 2041/* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
eb383413 2042
59727473 2043static struct demangle_component *
9334f9c6 2044d_pointer_to_member_type (struct d_info *di)
eb383413 2045{
59727473
DD
2046 struct demangle_component *cl;
2047 struct demangle_component *mem;
2048 struct demangle_component **pmem;
eb383413 2049
d00edca5
DD
2050 if (d_next_char (di) != 'M')
2051 return NULL;
eb383413 2052
59727473 2053 cl = cplus_demangle_type (di);
eb383413 2054
d00edca5
DD
2055 /* The ABI specifies that any type can be a substitution source, and
2056 that M is followed by two types, and that when a CV-qualified
2057 type is seen both the base type and the CV-qualified types are
2058 substitution sources. The ABI also specifies that for a pointer
2059 to a CV-qualified member function, the qualifiers are attached to
2060 the second type. Given the grammar, a plain reading of the ABI
2061 suggests that both the CV-qualified member function and the
2062 non-qualified member function are substitution sources. However,
2063 g++ does not work that way. g++ treats only the CV-qualified
2064 member function as a substitution source. FIXME. So to work
2065 with g++, we need to pull off the CV-qualifiers here, in order to
59727473 2066 avoid calling add_substitution() in cplus_demangle_type(). */
eb383413 2067
858b45cf 2068 pmem = d_cv_qualifiers (di, &mem, 1);
331c3da2
DD
2069 if (pmem == NULL)
2070 return NULL;
59727473 2071 *pmem = cplus_demangle_type (di);
eb383413 2072
59727473 2073 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
eb383413
L
2074}
2075
d00edca5
DD
2076/* <template-param> ::= T_
2077 ::= T <(parameter-2 non-negative) number> _
2078*/
eb383413 2079
59727473 2080static struct demangle_component *
9334f9c6 2081d_template_param (struct d_info *di)
eb383413 2082{
d00edca5 2083 long param;
eb383413 2084
d00edca5
DD
2085 if (d_next_char (di) != 'T')
2086 return NULL;
eb383413 2087
d00edca5
DD
2088 if (d_peek_char (di) == '_')
2089 param = 0;
2090 else
2091 {
2092 param = d_number (di);
2093 if (param < 0)
2094 return NULL;
2095 param += 1;
2096 }
03d5f569 2097
d00edca5
DD
2098 if (d_next_char (di) != '_')
2099 return NULL;
eb383413 2100
b6fb00c0
DD
2101 ++di->did_subs;
2102
d00edca5 2103 return d_make_template_param (di, param);
eb383413
L
2104}
2105
d00edca5
DD
2106/* <template-args> ::= I <template-arg>+ E */
2107
59727473 2108static struct demangle_component *
9334f9c6 2109d_template_args (struct d_info *di)
eb383413 2110{
59727473
DD
2111 struct demangle_component *hold_last_name;
2112 struct demangle_component *al;
2113 struct demangle_component **pal;
eb383413 2114
d00edca5
DD
2115 /* Preserve the last name we saw--don't let the template arguments
2116 clobber it, as that would give us the wrong name for a subsequent
2117 constructor or destructor. */
2118 hold_last_name = di->last_name;
eb383413 2119
d00edca5
DD
2120 if (d_next_char (di) != 'I')
2121 return NULL;
eb383413 2122
d00edca5
DD
2123 al = NULL;
2124 pal = &al;
eb383413
L
2125 while (1)
2126 {
59727473 2127 struct demangle_component *a;
d00edca5
DD
2128
2129 a = d_template_arg (di);
2130 if (a == NULL)
2131 return NULL;
2132
59727473 2133 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
331c3da2
DD
2134 if (*pal == NULL)
2135 return NULL;
d00edca5
DD
2136 pal = &d_right (*pal);
2137
2138 if (d_peek_char (di) == 'E')
03d5f569 2139 {
d00edca5
DD
2140 d_advance (di, 1);
2141 break;
03d5f569 2142 }
eb383413
L
2143 }
2144
d00edca5
DD
2145 di->last_name = hold_last_name;
2146
2147 return al;
eb383413
L
2148}
2149
d00edca5
DD
2150/* <template-arg> ::= <type>
2151 ::= X <expression> E
2152 ::= <expr-primary>
2153*/
eb383413 2154
59727473 2155static struct demangle_component *
9334f9c6 2156d_template_arg (struct d_info *di)
eb383413 2157{
59727473 2158 struct demangle_component *ret;
03d5f569 2159
d00edca5 2160 switch (d_peek_char (di))
eb383413 2161 {
d00edca5
DD
2162 case 'X':
2163 d_advance (di, 1);
2164 ret = d_expression (di);
2165 if (d_next_char (di) != 'E')
2166 return NULL;
2167 return ret;
b851d07b 2168
d00edca5
DD
2169 case 'L':
2170 return d_expr_primary (di);
eb383413 2171
d00edca5 2172 default:
59727473 2173 return cplus_demangle_type (di);
74bcd529 2174 }
eb383413
L
2175}
2176
d00edca5
DD
2177/* <expression> ::= <(unary) operator-name> <expression>
2178 ::= <(binary) operator-name> <expression> <expression>
2179 ::= <(trinary) operator-name> <expression> <expression> <expression>
2180 ::= st <type>
2181 ::= <template-param>
2182 ::= sr <type> <unqualified-name>
2183 ::= sr <type> <unqualified-name> <template-args>
2184 ::= <expr-primary>
2185*/
2186
59727473 2187static struct demangle_component *
9334f9c6 2188d_expression (struct d_info *di)
eb383413 2189{
d00edca5 2190 char peek;
eb383413 2191
d00edca5
DD
2192 peek = d_peek_char (di);
2193 if (peek == 'L')
2194 return d_expr_primary (di);
2195 else if (peek == 'T')
2196 return d_template_param (di);
2197 else if (peek == 's' && d_peek_next_char (di) == 'r')
eb383413 2198 {
59727473
DD
2199 struct demangle_component *type;
2200 struct demangle_component *name;
eb383413 2201
d00edca5 2202 d_advance (di, 2);
59727473 2203 type = cplus_demangle_type (di);
d00edca5
DD
2204 name = d_unqualified_name (di);
2205 if (d_peek_char (di) != 'I')
59727473 2206 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
d00edca5 2207 else
59727473
DD
2208 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2209 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
d00edca5 2210 d_template_args (di)));
793011ca 2211 }
d00edca5 2212 else
eb383413 2213 {
59727473 2214 struct demangle_component *op;
d00edca5 2215 int args;
eb383413 2216
d00edca5
DD
2217 op = d_operator_name (di);
2218 if (op == NULL)
2219 return NULL;
eb383413 2220
59727473 2221 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
b6fb00c0
DD
2222 di->expansion += op->u.s_operator.op->len - 2;
2223
59727473 2224 if (op->type == DEMANGLE_COMPONENT_OPERATOR
d00edca5 2225 && strcmp (op->u.s_operator.op->code, "st") == 0)
59727473
DD
2226 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2227 cplus_demangle_type (di));
eb383413 2228
d00edca5
DD
2229 switch (op->type)
2230 {
2231 default:
2232 return NULL;
59727473 2233 case DEMANGLE_COMPONENT_OPERATOR:
d00edca5
DD
2234 args = op->u.s_operator.op->args;
2235 break;
59727473 2236 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
d00edca5
DD
2237 args = op->u.s_extended_operator.args;
2238 break;
59727473 2239 case DEMANGLE_COMPONENT_CAST:
d00edca5
DD
2240 args = 1;
2241 break;
2242 }
2243
2244 switch (args)
2245 {
2246 case 1:
59727473
DD
2247 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2248 d_expression (di));
d00edca5
DD
2249 case 2:
2250 {
59727473 2251 struct demangle_component *left;
d00edca5
DD
2252
2253 left = d_expression (di);
59727473
DD
2254 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2255 d_make_comp (di,
2256 DEMANGLE_COMPONENT_BINARY_ARGS,
2257 left,
d00edca5
DD
2258 d_expression (di)));
2259 }
2260 case 3:
2261 {
59727473
DD
2262 struct demangle_component *first;
2263 struct demangle_component *second;
d00edca5
DD
2264
2265 first = d_expression (di);
2266 second = d_expression (di);
59727473
DD
2267 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2268 d_make_comp (di,
2269 DEMANGLE_COMPONENT_TRINARY_ARG1,
2270 first,
d00edca5 2271 d_make_comp (di,
59727473 2272 DEMANGLE_COMPONENT_TRINARY_ARG2,
d00edca5
DD
2273 second,
2274 d_expression (di))));
2275 }
2276 default:
2277 return NULL;
2278 }
eb383413
L
2279 }
2280}
2281
d00edca5
DD
2282/* <expr-primary> ::= L <type> <(value) number> E
2283 ::= L <type> <(value) float> E
2284 ::= L <mangled-name> E
2285*/
74bcd529 2286
59727473 2287static struct demangle_component *
9334f9c6 2288d_expr_primary (struct d_info *di)
74bcd529 2289{
59727473 2290 struct demangle_component *ret;
74bcd529 2291
d00edca5
DD
2292 if (d_next_char (di) != 'L')
2293 return NULL;
2294 if (d_peek_char (di) == '_')
59727473 2295 ret = cplus_demangle_mangled_name (di, 0);
d00edca5 2296 else
74bcd529 2297 {
59727473
DD
2298 struct demangle_component *type;
2299 enum demangle_component_type t;
d00edca5
DD
2300 const char *s;
2301
59727473 2302 type = cplus_demangle_type (di);
a21da8bf
DD
2303 if (type == NULL)
2304 return NULL;
d00edca5 2305
b6fb00c0
DD
2306 /* If we have a type we know how to print, we aren't going to
2307 print the type name itself. */
59727473 2308 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
b6fb00c0
DD
2309 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2310 di->expansion -= type->u.s_builtin.type->len;
2311
d00edca5
DD
2312 /* Rather than try to interpret the literal value, we just
2313 collect it as a string. Note that it's possible to have a
2314 floating point literal here. The ABI specifies that the
2315 format of such literals is machine independent. That's fine,
2316 but what's not fine is that versions of g++ up to 3.2 with
2317 -fabi-version=1 used upper case letters in the hex constant,
2318 and dumped out gcc's internal representation. That makes it
2319 hard to tell where the constant ends, and hard to dump the
2320 constant in any readable form anyhow. We don't attempt to
2321 handle these cases. */
2322
59727473 2323 t = DEMANGLE_COMPONENT_LITERAL;
97ceaf5b
DD
2324 if (d_peek_char (di) == 'n')
2325 {
59727473 2326 t = DEMANGLE_COMPONENT_LITERAL_NEG;
97ceaf5b
DD
2327 d_advance (di, 1);
2328 }
d00edca5
DD
2329 s = d_str (di);
2330 while (d_peek_char (di) != 'E')
2331 d_advance (di, 1);
97ceaf5b 2332 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
d00edca5
DD
2333 }
2334 if (d_next_char (di) != 'E')
2335 return NULL;
2336 return ret;
74bcd529
DD
2337}
2338
d00edca5
DD
2339/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2340 ::= Z <(function) encoding> E s [<discriminator>]
2341*/
74bcd529 2342
59727473 2343static struct demangle_component *
9334f9c6 2344d_local_name (struct d_info *di)
74bcd529 2345{
59727473 2346 struct demangle_component *function;
74bcd529 2347
d00edca5
DD
2348 if (d_next_char (di) != 'Z')
2349 return NULL;
74bcd529 2350
6d95373e 2351 function = d_encoding (di, 0);
74bcd529 2352
d00edca5
DD
2353 if (d_next_char (di) != 'E')
2354 return NULL;
74bcd529 2355
d00edca5 2356 if (d_peek_char (di) == 's')
74bcd529 2357 {
d00edca5
DD
2358 d_advance (di, 1);
2359 if (! d_discriminator (di))
2360 return NULL;
59727473 2361 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
d00edca5
DD
2362 d_make_name (di, "string literal",
2363 sizeof "string literal" - 1));
74bcd529 2364 }
d00edca5 2365 else
74bcd529 2366 {
59727473 2367 struct demangle_component *name;
74bcd529 2368
d00edca5
DD
2369 name = d_name (di);
2370 if (! d_discriminator (di))
2371 return NULL;
59727473 2372 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
74bcd529 2373 }
74bcd529
DD
2374}
2375
d00edca5 2376/* <discriminator> ::= _ <(non-negative) number>
eb383413 2377
d00edca5
DD
2378 We demangle the discriminator, but we don't print it out. FIXME:
2379 We should print it out in verbose mode. */
74bcd529 2380
d00edca5 2381static int
9334f9c6 2382d_discriminator (struct d_info *di)
d00edca5
DD
2383{
2384 long discrim;
74bcd529 2385
d00edca5
DD
2386 if (d_peek_char (di) != '_')
2387 return 1;
2388 d_advance (di, 1);
2389 discrim = d_number (di);
2390 if (discrim < 0)
2391 return 0;
2392 return 1;
2393}
eb383413 2394
d00edca5 2395/* Add a new substitution. */
eb383413 2396
d00edca5 2397static int
9334f9c6 2398d_add_substitution (struct d_info *di, struct demangle_component *dc)
eb383413 2399{
331c3da2
DD
2400 if (dc == NULL)
2401 return 0;
d00edca5
DD
2402 if (di->next_sub >= di->num_subs)
2403 return 0;
2404 di->subs[di->next_sub] = dc;
2405 ++di->next_sub;
2406 return 1;
2407}
2408
2409/* <substitution> ::= S <seq-id> _
2410 ::= S_
2411 ::= St
2412 ::= Sa
2413 ::= Sb
2414 ::= Ss
2415 ::= Si
2416 ::= So
2417 ::= Sd
97ceaf5b
DD
2418
2419 If PREFIX is non-zero, then this type is being used as a prefix in
2420 a qualified name. In this case, for the standard substitutions, we
2421 need to check whether we are being used as a prefix for a
2422 constructor or destructor, and return a full template name.
2423 Otherwise we will get something like std::iostream::~iostream()
2424 which does not correspond particularly well to any function which
2425 actually appears in the source.
d00edca5 2426*/
eb383413 2427
97ceaf5b
DD
2428static const struct d_standard_sub_info standard_subs[] =
2429{
b6fb00c0
DD
2430 { 't', NL ("std"),
2431 NL ("std"),
2432 NULL, 0 },
2433 { 'a', NL ("std::allocator"),
2434 NL ("std::allocator"),
2435 NL ("allocator") },
2436 { 'b', NL ("std::basic_string"),
2437 NL ("std::basic_string"),
2438 NL ("basic_string") },
2439 { 's', NL ("std::string"),
2440 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2441 NL ("basic_string") },
2442 { 'i', NL ("std::istream"),
2443 NL ("std::basic_istream<char, std::char_traits<char> >"),
2444 NL ("basic_istream") },
2445 { 'o', NL ("std::ostream"),
2446 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2447 NL ("basic_ostream") },
2448 { 'd', NL ("std::iostream"),
2449 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2450 NL ("basic_iostream") }
97ceaf5b
DD
2451};
2452
59727473 2453static struct demangle_component *
9334f9c6 2454d_substitution (struct d_info *di, int prefix)
d00edca5
DD
2455{
2456 char c;
eb383413 2457
d00edca5
DD
2458 if (d_next_char (di) != 'S')
2459 return NULL;
e7e9b069 2460
d00edca5 2461 c = d_next_char (di);
858b45cf 2462 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
eb383413 2463 {
d00edca5 2464 int id;
eb383413 2465
d00edca5
DD
2466 id = 0;
2467 if (c != '_')
eb383413 2468 {
d00edca5 2469 do
eb383413 2470 {
d00edca5
DD
2471 if (IS_DIGIT (c))
2472 id = id * 36 + c - '0';
858b45cf 2473 else if (IS_UPPER (c))
d00edca5
DD
2474 id = id * 36 + c - 'A' + 10;
2475 else
2476 return NULL;
2477 c = d_next_char (di);
eb383413 2478 }
d00edca5 2479 while (c != '_');
eb383413 2480
d00edca5 2481 ++id;
eb383413 2482 }
eb383413 2483
d00edca5
DD
2484 if (id >= di->next_sub)
2485 return NULL;
eb383413 2486
b6fb00c0
DD
2487 ++di->did_subs;
2488
d00edca5 2489 return di->subs[id];
eb383413 2490 }
d00edca5 2491 else
eb383413 2492 {
97ceaf5b
DD
2493 int verbose;
2494 const struct d_standard_sub_info *p;
2495 const struct d_standard_sub_info *pend;
2496
2497 verbose = (di->options & DMGL_VERBOSE) != 0;
2498 if (! verbose && prefix)
e61231f1 2499 {
97ceaf5b
DD
2500 char peek;
2501
2502 peek = d_peek_char (di);
2503 if (peek == 'C' || peek == 'D')
2504 verbose = 1;
eb383413 2505 }
97ceaf5b
DD
2506
2507 pend = (&standard_subs[0]
2508 + sizeof standard_subs / sizeof standard_subs[0]);
2509 for (p = &standard_subs[0]; p < pend; ++p)
2510 {
2511 if (c == p->code)
2512 {
b6fb00c0
DD
2513 const char *s;
2514 int len;
2515
97ceaf5b 2516 if (p->set_last_name != NULL)
b6fb00c0
DD
2517 di->last_name = d_make_sub (di, p->set_last_name,
2518 p->set_last_name_len);
97ceaf5b 2519 if (verbose)
b6fb00c0
DD
2520 {
2521 s = p->full_expansion;
2522 len = p->full_len;
2523 }
97ceaf5b 2524 else
b6fb00c0
DD
2525 {
2526 s = p->simple_expansion;
2527 len = p->simple_len;
2528 }
2529 di->expansion += len;
2530 return d_make_sub (di, s, len);
97ceaf5b
DD
2531 }
2532 }
2533
2534 return NULL;
eb383413 2535 }
eb383413
L
2536}
2537
d00edca5 2538/* Resize the print buffer. */
eb383413 2539
d00edca5 2540static void
9334f9c6 2541d_print_resize (struct d_print_info *dpi, size_t add)
d00edca5
DD
2542{
2543 size_t need;
eb383413 2544
331c3da2
DD
2545 if (dpi->buf == NULL)
2546 return;
d00edca5
DD
2547 need = dpi->len + add;
2548 while (need > dpi->alc)
eb383413 2549 {
d00edca5
DD
2550 size_t newalc;
2551 char *newbuf;
59666b35 2552
d00edca5
DD
2553 newalc = dpi->alc * 2;
2554 newbuf = realloc (dpi->buf, newalc);
2555 if (newbuf == NULL)
0976f6a7 2556 {
d00edca5
DD
2557 free (dpi->buf);
2558 dpi->buf = NULL;
2559 dpi->allocation_failure = 1;
2560 return;
eb383413 2561 }
d00edca5
DD
2562 dpi->buf = newbuf;
2563 dpi->alc = newalc;
eb383413 2564 }
d00edca5 2565}
0976f6a7 2566
d00edca5 2567/* Append a character to the print buffer. */
0976f6a7 2568
d00edca5 2569static void
9334f9c6 2570d_print_append_char (struct d_print_info *dpi, int c)
d00edca5
DD
2571{
2572 if (dpi->buf != NULL)
2573 {
2574 if (dpi->len >= dpi->alc)
2575 {
2576 d_print_resize (dpi, 1);
2577 if (dpi->buf == NULL)
2578 return;
2579 }
0976f6a7 2580
d00edca5
DD
2581 dpi->buf[dpi->len] = c;
2582 ++dpi->len;
74bcd529 2583 }
eb383413
L
2584}
2585
d00edca5
DD
2586/* Append a buffer to the print buffer. */
2587
2588static void
9334f9c6 2589d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
eb383413 2590{
d00edca5 2591 if (dpi->buf != NULL)
eb383413 2592 {
d00edca5 2593 if (dpi->len + l > dpi->alc)
eb383413 2594 {
d00edca5
DD
2595 d_print_resize (dpi, l);
2596 if (dpi->buf == NULL)
2597 return;
eb383413 2598 }
eb383413 2599
d00edca5
DD
2600 memcpy (dpi->buf + dpi->len, s, l);
2601 dpi->len += l;
2602 }
eb383413
L
2603}
2604
d00edca5 2605/* Indicate that an error occurred during printing. */
eb383413 2606
d00edca5 2607static void
9334f9c6 2608d_print_error (struct d_print_info *dpi)
bc9bf259 2609{
d00edca5
DD
2610 free (dpi->buf);
2611 dpi->buf = NULL;
2612}
bc9bf259 2613
b6fb00c0
DD
2614/* Turn components into a human readable string. OPTIONS is the
2615 options bits passed to the demangler. DC is the tree to print.
2616 ESTIMATE is a guess at the length of the result. This returns a
2617 string allocated by malloc, or NULL on error. On success, this
2618 sets *PALC to the size of the allocated buffer. On failure, this
2619 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2620 failure. */
eb383413 2621
59727473
DD
2622CP_STATIC_IF_GLIBCPP_V3
2623char *
9334f9c6
DD
2624cplus_demangle_print (int options, const struct demangle_component *dc,
2625 int estimate, size_t *palc)
d00edca5
DD
2626{
2627 struct d_print_info dpi;
eb383413 2628
d00edca5 2629 dpi.options = options;
eb383413 2630
b6fb00c0 2631 dpi.alc = estimate + 1;
d00edca5
DD
2632 dpi.buf = malloc (dpi.alc);
2633 if (dpi.buf == NULL)
eb383413 2634 {
d00edca5
DD
2635 *palc = 1;
2636 return NULL;
eb383413 2637 }
eb383413 2638
d00edca5
DD
2639 dpi.len = 0;
2640 dpi.templates = NULL;
2641 dpi.modifiers = NULL;
eb383413 2642
d00edca5 2643 dpi.allocation_failure = 0;
eb383413 2644
d00edca5 2645 d_print_comp (&dpi, dc);
eb383413 2646
d00edca5 2647 d_append_char (&dpi, '\0');
eb383413 2648
d00edca5
DD
2649 if (dpi.buf != NULL)
2650 *palc = dpi.alc;
2651 else
2652 *palc = dpi.allocation_failure;
eb383413 2653
d00edca5 2654 return dpi.buf;
eb383413
L
2655}
2656
d00edca5 2657/* Subroutine to handle components. */
eb383413 2658
d00edca5 2659static void
9334f9c6
DD
2660d_print_comp (struct d_print_info *dpi,
2661 const struct demangle_component *dc)
eb383413 2662{
d00edca5 2663 if (dc == NULL)
eb383413 2664 {
d00edca5
DD
2665 d_print_error (dpi);
2666 return;
eb383413 2667 }
d00edca5
DD
2668 if (d_print_saw_error (dpi))
2669 return;
eb383413 2670
d00edca5 2671 switch (dc->type)
eb383413 2672 {
59727473 2673 case DEMANGLE_COMPONENT_NAME:
b6fb00c0
DD
2674 if ((dpi->options & DMGL_JAVA) == 0)
2675 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2676 else
2677 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
d00edca5 2678 return;
eb383413 2679
59727473
DD
2680 case DEMANGLE_COMPONENT_QUAL_NAME:
2681 case DEMANGLE_COMPONENT_LOCAL_NAME:
d00edca5 2682 d_print_comp (dpi, d_left (dc));
b6fb00c0
DD
2683 if ((dpi->options & DMGL_JAVA) == 0)
2684 d_append_string_constant (dpi, "::");
2685 else
2686 d_append_char (dpi, '.');
d00edca5
DD
2687 d_print_comp (dpi, d_right (dc));
2688 return;
eb383413 2689
59727473 2690 case DEMANGLE_COMPONENT_TYPED_NAME:
d00edca5 2691 {
858b45cf 2692 struct d_print_mod *hold_modifiers;
59727473 2693 struct demangle_component *typed_name;
858b45cf
DD
2694 struct d_print_mod adpm[4];
2695 unsigned int i;
d00edca5
DD
2696 struct d_print_template dpt;
2697
2698 /* Pass the name down to the type so that it can be printed in
858b45cf
DD
2699 the right place for the type. We also have to pass down
2700 any CV-qualifiers, which apply to the this parameter. */
2701 hold_modifiers = dpi->modifiers;
2702 i = 0;
d00edca5 2703 typed_name = d_left (dc);
858b45cf
DD
2704 while (typed_name != NULL)
2705 {
2706 if (i >= sizeof adpm / sizeof adpm[0])
2707 {
2708 d_print_error (dpi);
2709 return;
2710 }
d00edca5 2711
858b45cf
DD
2712 adpm[i].next = dpi->modifiers;
2713 dpi->modifiers = &adpm[i];
2714 adpm[i].mod = typed_name;
2715 adpm[i].printed = 0;
2716 adpm[i].templates = dpi->templates;
2717 ++i;
2718
59727473
DD
2719 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2720 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2721 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
858b45cf
DD
2722 break;
2723
2724 typed_name = d_left (typed_name);
2725 }
d00edca5
DD
2726
2727 /* If typed_name is a template, then it applies to the
2728 function type as well. */
59727473 2729 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
d00edca5
DD
2730 {
2731 dpt.next = dpi->templates;
2732 dpi->templates = &dpt;
2733 dpt.template = typed_name;
2734 }
eb383413 2735
59727473
DD
2736 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2737 there may be CV-qualifiers on its right argument which
2738 really apply here; this happens when parsing a class which
2739 is local to a function. */
2740 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
d4edd112 2741 {
59727473 2742 struct demangle_component *local_name;
d4edd112
DD
2743
2744 local_name = d_right (typed_name);
59727473
DD
2745 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2746 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2747 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
d4edd112
DD
2748 {
2749 if (i >= sizeof adpm / sizeof adpm[0])
2750 {
2751 d_print_error (dpi);
2752 return;
2753 }
2754
2755 adpm[i] = adpm[i - 1];
2756 adpm[i].next = &adpm[i - 1];
2757 dpi->modifiers = &adpm[i];
2758
2759 adpm[i - 1].mod = local_name;
2760 adpm[i - 1].printed = 0;
2761 adpm[i - 1].templates = dpi->templates;
2762 ++i;
2763
2764 local_name = d_left (local_name);
2765 }
2766 }
2767
d00edca5 2768 d_print_comp (dpi, d_right (dc));
74bcd529 2769
59727473 2770 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
d00edca5 2771 dpi->templates = dpt.next;
eb383413 2772
858b45cf 2773 /* If the modifiers didn't get printed by the type, print them
d00edca5 2774 now. */
858b45cf 2775 while (i > 0)
d00edca5 2776 {
858b45cf
DD
2777 --i;
2778 if (! adpm[i].printed)
2779 {
2780 d_append_char (dpi, ' ');
2781 d_print_mod (dpi, adpm[i].mod);
2782 }
d00edca5 2783 }
eb383413 2784
858b45cf 2785 dpi->modifiers = hold_modifiers;
eb383413 2786
d00edca5
DD
2787 return;
2788 }
eb383413 2789
59727473 2790 case DEMANGLE_COMPONENT_TEMPLATE:
331c3da2
DD
2791 {
2792 struct d_print_mod *hold_dpm;
2793
2794 /* Don't push modifiers into a template definition. Doing so
2795 could give the wrong definition for a template argument.
2796 Instead, treat the template essentially as a name. */
2797
2798 hold_dpm = dpi->modifiers;
2799 dpi->modifiers = NULL;
2800
2801 d_print_comp (dpi, d_left (dc));
858b45cf
DD
2802 if (d_last_char (dpi) == '<')
2803 d_append_char (dpi, ' ');
331c3da2
DD
2804 d_append_char (dpi, '<');
2805 d_print_comp (dpi, d_right (dc));
2806 /* Avoid generating two consecutive '>' characters, to avoid
2807 the C++ syntactic ambiguity. */
858b45cf 2808 if (d_last_char (dpi) == '>')
331c3da2
DD
2809 d_append_char (dpi, ' ');
2810 d_append_char (dpi, '>');
2811
2812 dpi->modifiers = hold_dpm;
2813
2814 return;
2815 }
d00edca5 2816
59727473 2817 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
d00edca5
DD
2818 {
2819 long i;
59727473 2820 struct demangle_component *a;
d00edca5 2821 struct d_print_template *hold_dpt;
eb383413 2822
d00edca5
DD
2823 if (dpi->templates == NULL)
2824 {
2825 d_print_error (dpi);
2826 return;
2827 }
2828 i = dc->u.s_number.number;
2829 for (a = d_right (dpi->templates->template);
2830 a != NULL;
2831 a = d_right (a))
2832 {
59727473 2833 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
d00edca5
DD
2834 {
2835 d_print_error (dpi);
2836 return;
2837 }
2838 if (i <= 0)
2839 break;
2840 --i;
2841 }
2842 if (i != 0 || a == NULL)
2843 {
2844 d_print_error (dpi);
2845 return;
2846 }
59666b35 2847
d00edca5
DD
2848 /* While processing this parameter, we need to pop the list of
2849 templates. This is because the template parameter may
2850 itself be a reference to a parameter of an outer
2851 template. */
59666b35 2852
d00edca5
DD
2853 hold_dpt = dpi->templates;
2854 dpi->templates = hold_dpt->next;
eb383413 2855
d00edca5 2856 d_print_comp (dpi, d_left (a));
03d5f569 2857
d00edca5 2858 dpi->templates = hold_dpt;
59666b35 2859
d00edca5
DD
2860 return;
2861 }
eb383413 2862
59727473 2863 case DEMANGLE_COMPONENT_CTOR:
d00edca5
DD
2864 d_print_comp (dpi, dc->u.s_ctor.name);
2865 return;
2866
59727473 2867 case DEMANGLE_COMPONENT_DTOR:
d00edca5
DD
2868 d_append_char (dpi, '~');
2869 d_print_comp (dpi, dc->u.s_dtor.name);
2870 return;
2871
59727473 2872 case DEMANGLE_COMPONENT_VTABLE:
b6fb00c0 2873 d_append_string_constant (dpi, "vtable for ");
d00edca5
DD
2874 d_print_comp (dpi, d_left (dc));
2875 return;
2876
59727473 2877 case DEMANGLE_COMPONENT_VTT:
b6fb00c0 2878 d_append_string_constant (dpi, "VTT for ");
d00edca5
DD
2879 d_print_comp (dpi, d_left (dc));
2880 return;
2881
59727473 2882 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
b6fb00c0 2883 d_append_string_constant (dpi, "construction vtable for ");
d00edca5 2884 d_print_comp (dpi, d_left (dc));
b6fb00c0 2885 d_append_string_constant (dpi, "-in-");
d00edca5
DD
2886 d_print_comp (dpi, d_right (dc));
2887 return;
2888
59727473 2889 case DEMANGLE_COMPONENT_TYPEINFO:
b6fb00c0 2890 d_append_string_constant (dpi, "typeinfo for ");
d00edca5
DD
2891 d_print_comp (dpi, d_left (dc));
2892 return;
2893
59727473 2894 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
b6fb00c0 2895 d_append_string_constant (dpi, "typeinfo name for ");
d00edca5
DD
2896 d_print_comp (dpi, d_left (dc));
2897 return;
2898
59727473 2899 case DEMANGLE_COMPONENT_TYPEINFO_FN:
b6fb00c0 2900 d_append_string_constant (dpi, "typeinfo fn for ");
d00edca5
DD
2901 d_print_comp (dpi, d_left (dc));
2902 return;
2903
59727473 2904 case DEMANGLE_COMPONENT_THUNK:
b6fb00c0 2905 d_append_string_constant (dpi, "non-virtual thunk to ");
d00edca5
DD
2906 d_print_comp (dpi, d_left (dc));
2907 return;
2908
59727473 2909 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
b6fb00c0 2910 d_append_string_constant (dpi, "virtual thunk to ");
d00edca5
DD
2911 d_print_comp (dpi, d_left (dc));
2912 return;
2913
59727473 2914 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
b6fb00c0 2915 d_append_string_constant (dpi, "covariant return thunk to ");
d00edca5
DD
2916 d_print_comp (dpi, d_left (dc));
2917 return;
2918
59727473 2919 case DEMANGLE_COMPONENT_JAVA_CLASS:
b6fb00c0 2920 d_append_string_constant (dpi, "java Class for ");
d00edca5
DD
2921 d_print_comp (dpi, d_left (dc));
2922 return;
2923
59727473 2924 case DEMANGLE_COMPONENT_GUARD:
b6fb00c0 2925 d_append_string_constant (dpi, "guard variable for ");
d00edca5
DD
2926 d_print_comp (dpi, d_left (dc));
2927 return;
2928
59727473 2929 case DEMANGLE_COMPONENT_REFTEMP:
b6fb00c0 2930 d_append_string_constant (dpi, "reference temporary for ");
d00edca5
DD
2931 d_print_comp (dpi, d_left (dc));
2932 return;
2933
59727473 2934 case DEMANGLE_COMPONENT_SUB_STD:
b6fb00c0 2935 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
d00edca5
DD
2936 return;
2937
59727473
DD
2938 case DEMANGLE_COMPONENT_RESTRICT:
2939 case DEMANGLE_COMPONENT_VOLATILE:
2940 case DEMANGLE_COMPONENT_CONST:
74aee4eb
DD
2941 {
2942 struct d_print_mod *pdpm;
2943
2944 /* When printing arrays, it's possible to have cases where the
2945 same CV-qualifier gets pushed on the stack multiple times.
2946 We only need to print it once. */
2947
2948 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
2949 {
2950 if (! pdpm->printed)
2951 {
2952 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
2953 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
2954 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
2955 break;
2956 if (pdpm->mod->type == dc->type)
2957 {
2958 d_print_comp (dpi, d_left (dc));
2959 return;
2960 }
2961 }
2962 }
2963 }
2964 /* Fall through. */
59727473
DD
2965 case DEMANGLE_COMPONENT_RESTRICT_THIS:
2966 case DEMANGLE_COMPONENT_VOLATILE_THIS:
2967 case DEMANGLE_COMPONENT_CONST_THIS:
2968 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
2969 case DEMANGLE_COMPONENT_POINTER:
2970 case DEMANGLE_COMPONENT_REFERENCE:
2971 case DEMANGLE_COMPONENT_COMPLEX:
2972 case DEMANGLE_COMPONENT_IMAGINARY:
d00edca5
DD
2973 {
2974 /* We keep a list of modifiers on the stack. */
2975 struct d_print_mod dpm;
eb383413 2976
d00edca5
DD
2977 dpm.next = dpi->modifiers;
2978 dpi->modifiers = &dpm;
2979 dpm.mod = dc;
2980 dpm.printed = 0;
331c3da2 2981 dpm.templates = dpi->templates;
eb383413 2982
d00edca5 2983 d_print_comp (dpi, d_left (dc));
59666b35 2984
d00edca5
DD
2985 /* If the modifier didn't get printed by the type, print it
2986 now. */
2987 if (! dpm.printed)
2988 d_print_mod (dpi, dc);
eb383413 2989
d00edca5 2990 dpi->modifiers = dpm.next;
eb383413 2991
d00edca5
DD
2992 return;
2993 }
eb383413 2994
59727473 2995 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
d00edca5 2996 if ((dpi->options & DMGL_JAVA) == 0)
b6fb00c0
DD
2997 d_append_buffer (dpi, dc->u.s_builtin.type->name,
2998 dc->u.s_builtin.type->len);
d00edca5 2999 else
b6fb00c0
DD
3000 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3001 dc->u.s_builtin.type->java_len);
d00edca5 3002 return;
eb383413 3003
59727473 3004 case DEMANGLE_COMPONENT_VENDOR_TYPE:
d00edca5
DD
3005 d_print_comp (dpi, d_left (dc));
3006 return;
eb383413 3007
59727473 3008 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
d00edca5
DD
3009 {
3010 if (d_left (dc) != NULL)
3011 {
3012 struct d_print_mod dpm;
eb383413 3013
d00edca5
DD
3014 /* We must pass this type down as a modifier in order to
3015 print it in the right location. */
eb383413 3016
d00edca5
DD
3017 dpm.next = dpi->modifiers;
3018 dpi->modifiers = &dpm;
3019 dpm.mod = dc;
3020 dpm.printed = 0;
331c3da2 3021 dpm.templates = dpi->templates;
eb383413 3022
d00edca5 3023 d_print_comp (dpi, d_left (dc));
eb383413 3024
d00edca5 3025 dpi->modifiers = dpm.next;
eb383413 3026
d00edca5
DD
3027 if (dpm.printed)
3028 return;
eb383413 3029
d00edca5
DD
3030 d_append_char (dpi, ' ');
3031 }
eb383413 3032
d00edca5 3033 d_print_function_type (dpi, dc, dpi->modifiers);
03d5f569 3034
d00edca5
DD
3035 return;
3036 }
eb383413 3037
59727473 3038 case DEMANGLE_COMPONENT_ARRAY_TYPE:
d00edca5 3039 {
74aee4eb
DD
3040 struct d_print_mod *hold_modifiers;
3041 struct d_print_mod adpm[4];
3042 unsigned int i;
3043 struct d_print_mod *pdpm;
eb383413 3044
d00edca5 3045 /* We must pass this type down as a modifier in order to print
74aee4eb
DD
3046 multi-dimensional arrays correctly. If the array itself is
3047 CV-qualified, we act as though the element type were
3048 CV-qualified. We do this by copying the modifiers down
3049 rather than fiddling pointers, so that we don't wind up
3050 with a d_print_mod higher on the stack pointing into our
3051 stack frame after we return. */
03d5f569 3052
74aee4eb
DD
3053 hold_modifiers = dpi->modifiers;
3054
3055 adpm[0].next = hold_modifiers;
3056 dpi->modifiers = &adpm[0];
3057 adpm[0].mod = dc;
3058 adpm[0].printed = 0;
3059 adpm[0].templates = dpi->templates;
3060
3061 i = 1;
3062 pdpm = hold_modifiers;
3063 while (pdpm != NULL
3064 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3065 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3066 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3067 {
3068 if (! pdpm->printed)
3069 {
3070 if (i >= sizeof adpm / sizeof adpm[0])
3071 {
3072 d_print_error (dpi);
3073 return;
3074 }
3075
3076 adpm[i] = *pdpm;
3077 adpm[i].next = dpi->modifiers;
3078 dpi->modifiers = &adpm[i];
3079 pdpm->printed = 1;
3080 ++i;
3081 }
3082
3083 pdpm = pdpm->next;
3084 }
eb383413 3085
d00edca5 3086 d_print_comp (dpi, d_right (dc));
eb383413 3087
74aee4eb 3088 dpi->modifiers = hold_modifiers;
eb383413 3089
74aee4eb 3090 if (adpm[0].printed)
d00edca5 3091 return;
eb383413 3092
74aee4eb
DD
3093 while (i > 1)
3094 {
3095 --i;
3096 d_print_mod (dpi, adpm[i].mod);
3097 }
3098
d00edca5 3099 d_print_array_type (dpi, dc, dpi->modifiers);
eb383413 3100
d00edca5
DD
3101 return;
3102 }
eb383413 3103
59727473 3104 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
d00edca5 3105 {
d00edca5
DD
3106 struct d_print_mod dpm;
3107
d00edca5
DD
3108 dpm.next = dpi->modifiers;
3109 dpi->modifiers = &dpm;
3110 dpm.mod = dc;
3111 dpm.printed = 0;
331c3da2 3112 dpm.templates = dpi->templates;
d00edca5 3113
858b45cf 3114 d_print_comp (dpi, d_right (dc));
d00edca5
DD
3115
3116 /* If the modifier didn't get printed by the type, print it
3117 now. */
3118 if (! dpm.printed)
3119 {
3120 d_append_char (dpi, ' ');
3121 d_print_comp (dpi, d_left (dc));
b6fb00c0 3122 d_append_string_constant (dpi, "::*");
d00edca5 3123 }
eb383413 3124
d00edca5 3125 dpi->modifiers = dpm.next;
eb383413 3126
d00edca5
DD
3127 return;
3128 }
eb383413 3129
59727473
DD
3130 case DEMANGLE_COMPONENT_ARGLIST:
3131 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
d00edca5
DD
3132 d_print_comp (dpi, d_left (dc));
3133 if (d_right (dc) != NULL)
3134 {
b6fb00c0 3135 d_append_string_constant (dpi, ", ");
d00edca5
DD
3136 d_print_comp (dpi, d_right (dc));
3137 }
3138 return;
eb383413 3139
59727473 3140 case DEMANGLE_COMPONENT_OPERATOR:
d00edca5
DD
3141 {
3142 char c;
3143
b6fb00c0 3144 d_append_string_constant (dpi, "operator");
d00edca5 3145 c = dc->u.s_operator.op->name[0];
858b45cf 3146 if (IS_LOWER (c))
d00edca5 3147 d_append_char (dpi, ' ');
b6fb00c0
DD
3148 d_append_buffer (dpi, dc->u.s_operator.op->name,
3149 dc->u.s_operator.op->len);
d00edca5
DD
3150 return;
3151 }
eb383413 3152
59727473 3153 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
b6fb00c0 3154 d_append_string_constant (dpi, "operator ");
d00edca5
DD
3155 d_print_comp (dpi, dc->u.s_extended_operator.name);
3156 return;
eb383413 3157
59727473 3158 case DEMANGLE_COMPONENT_CAST:
b6fb00c0 3159 d_append_string_constant (dpi, "operator ");
d00edca5
DD
3160 d_print_cast (dpi, dc);
3161 return;
eb383413 3162
59727473
DD
3163 case DEMANGLE_COMPONENT_UNARY:
3164 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
d00edca5
DD
3165 d_print_expr_op (dpi, d_left (dc));
3166 else
eb383413 3167 {
099f84cf 3168 d_append_char (dpi, '(');
d00edca5
DD
3169 d_print_cast (dpi, d_left (dc));
3170 d_append_char (dpi, ')');
eb383413 3171 }
d00edca5
DD
3172 d_append_char (dpi, '(');
3173 d_print_comp (dpi, d_right (dc));
3174 d_append_char (dpi, ')');
d00edca5
DD
3175 return;
3176
59727473
DD
3177 case DEMANGLE_COMPONENT_BINARY:
3178 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
eb383413 3179 {
d00edca5
DD
3180 d_print_error (dpi);
3181 return;
eb383413 3182 }
858b45cf
DD
3183
3184 /* We wrap an expression which uses the greater-than operator in
3185 an extra layer of parens so that it does not get confused
3186 with the '>' which ends the template parameters. */
59727473 3187 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
b6fb00c0
DD
3188 && d_left (dc)->u.s_operator.op->len == 1
3189 && d_left (dc)->u.s_operator.op->name[0] == '>')
858b45cf
DD
3190 d_append_char (dpi, '(');
3191
d00edca5
DD
3192 d_append_char (dpi, '(');
3193 d_print_comp (dpi, d_left (d_right (dc)));
b6fb00c0 3194 d_append_string_constant (dpi, ") ");
d00edca5 3195 d_print_expr_op (dpi, d_left (dc));
b6fb00c0 3196 d_append_string_constant (dpi, " (");
d00edca5
DD
3197 d_print_comp (dpi, d_right (d_right (dc)));
3198 d_append_char (dpi, ')');
858b45cf 3199
59727473 3200 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
b6fb00c0
DD
3201 && d_left (dc)->u.s_operator.op->len == 1
3202 && d_left (dc)->u.s_operator.op->name[0] == '>')
858b45cf
DD
3203 d_append_char (dpi, ')');
3204
d00edca5
DD
3205 return;
3206
59727473
DD
3207 case DEMANGLE_COMPONENT_BINARY_ARGS:
3208 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
d00edca5
DD
3209 d_print_error (dpi);
3210 return;
3211
59727473
DD
3212 case DEMANGLE_COMPONENT_TRINARY:
3213 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3214 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
d00edca5
DD
3215 {
3216 d_print_error (dpi);
3217 return;
3218 }
3219 d_append_char (dpi, '(');
3220 d_print_comp (dpi, d_left (d_right (dc)));
b6fb00c0 3221 d_append_string_constant (dpi, ") ");
d00edca5 3222 d_print_expr_op (dpi, d_left (dc));
b6fb00c0 3223 d_append_string_constant (dpi, " (");
d00edca5 3224 d_print_comp (dpi, d_left (d_right (d_right (dc))));
b6fb00c0 3225 d_append_string_constant (dpi, ") : (");
d00edca5
DD
3226 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3227 d_append_char (dpi, ')');
3228 return;
3229
59727473
DD
3230 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3231 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3232 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
d00edca5
DD
3233 d_print_error (dpi);
3234 return;
3235
59727473
DD
3236 case DEMANGLE_COMPONENT_LITERAL:
3237 case DEMANGLE_COMPONENT_LITERAL_NEG:
2d733211
DD
3238 {
3239 enum d_builtin_type_print tp;
d00edca5 3240
2d733211
DD
3241 /* For some builtin types, produce simpler output. */
3242 tp = D_PRINT_DEFAULT;
3243 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3244 {
3245 tp = d_left (dc)->u.s_builtin.type->print;
3246 switch (tp)
3247 {
3248 case D_PRINT_INT:
3249 case D_PRINT_UNSIGNED:
3250 case D_PRINT_LONG:
3251 case D_PRINT_UNSIGNED_LONG:
3252 case D_PRINT_LONG_LONG:
3253 case D_PRINT_UNSIGNED_LONG_LONG:
3254 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3255 {
3256 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3257 d_append_char (dpi, '-');
3258 d_print_comp (dpi, d_right (dc));
3259 switch (tp)
3260 {
3261 default:
3262 break;
3263 case D_PRINT_UNSIGNED:
3264 d_append_char (dpi, 'u');
3265 break;
3266 case D_PRINT_LONG:
3267 d_append_char (dpi, 'l');
3268 break;
3269 case D_PRINT_UNSIGNED_LONG:
3270 d_append_string_constant (dpi, "ul");
3271 break;
3272 case D_PRINT_LONG_LONG:
3273 d_append_string_constant (dpi, "ll");
3274 break;
3275 case D_PRINT_UNSIGNED_LONG_LONG:
3276 d_append_string_constant (dpi, "ull");
3277 break;
3278 }
3279 return;
3280 }
3281 break;
eb383413 3282
2d733211
DD
3283 case D_PRINT_BOOL:
3284 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3285 && d_right (dc)->u.s_name.len == 1
3286 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3287 {
3288 switch (d_right (dc)->u.s_name.s[0])
3289 {
3290 case '0':
3291 d_append_string_constant (dpi, "false");
3292 return;
3293 case '1':
3294 d_append_string_constant (dpi, "true");
3295 return;
3296 default:
3297 break;
3298 }
3299 }
3300 break;
03d5f569 3301
2d733211
DD
3302 default:
3303 break;
3304 }
3305 }
eb383413 3306
2d733211
DD
3307 d_append_char (dpi, '(');
3308 d_print_comp (dpi, d_left (dc));
3309 d_append_char (dpi, ')');
3310 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3311 d_append_char (dpi, '-');
3312 if (tp == D_PRINT_FLOAT)
3313 d_append_char (dpi, '[');
3314 d_print_comp (dpi, d_right (dc));
3315 if (tp == D_PRINT_FLOAT)
3316 d_append_char (dpi, ']');
3317 }
d00edca5 3318 return;
eb383413 3319
d00edca5
DD
3320 default:
3321 d_print_error (dpi);
3322 return;
3323 }
eb383413
L
3324}
3325
b6fb00c0
DD
3326/* Print a Java dentifier. For Java we try to handle encoded extended
3327 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3328 so we don't it for C++. Characters are encoded as
3329 __U<hex-char>+_. */
eb383413 3330
d00edca5 3331static void
9334f9c6 3332d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
eb383413 3333{
b6fb00c0
DD
3334 const char *p;
3335 const char *end;
eb383413 3336
b6fb00c0
DD
3337 end = name + len;
3338 for (p = name; p < end; ++p)
3339 {
3340 if (end - p > 3
3341 && p[0] == '_'
3342 && p[1] == '_'
3343 && p[2] == 'U')
eb383413 3344 {
b6fb00c0
DD
3345 unsigned long c;
3346 const char *q;
3347
3348 c = 0;
3349 for (q = p + 3; q < end; ++q)
d00edca5 3350 {
b6fb00c0
DD
3351 int dig;
3352
3353 if (IS_DIGIT (*q))
3354 dig = *q - '0';
3355 else if (*q >= 'A' && *q <= 'F')
3356 dig = *q - 'A' + 10;
3357 else if (*q >= 'a' && *q <= 'f')
3358 dig = *q - 'a' + 10;
3359 else
3360 break;
eb383413 3361
b6fb00c0
DD
3362 c = c * 16 + dig;
3363 }
3364 /* If the Unicode character is larger than 256, we don't try
3365 to deal with it here. FIXME. */
3366 if (q < end && *q == '_' && c < 256)
3367 {
3368 d_append_char (dpi, c);
3369 p = q;
3370 continue;
d00edca5 3371 }
d00edca5 3372 }
b6fb00c0
DD
3373
3374 d_append_char (dpi, *p);
eb383413 3375 }
eb383413
L
3376}
3377
858b45cf
DD
3378/* Print a list of modifiers. SUFFIX is 1 if we are printing
3379 qualifiers on this after printing a function. */
eb383413 3380
d00edca5 3381static void
9334f9c6
DD
3382d_print_mod_list (struct d_print_info *dpi,
3383 struct d_print_mod *mods, int suffix)
eb383413 3384{
331c3da2
DD
3385 struct d_print_template *hold_dpt;
3386
858b45cf 3387 if (mods == NULL || d_print_saw_error (dpi))
d00edca5 3388 return;
eb383413 3389
858b45cf
DD
3390 if (mods->printed
3391 || (! suffix
59727473
DD
3392 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3393 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3394 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
858b45cf
DD
3395 {
3396 d_print_mod_list (dpi, mods->next, suffix);
3397 return;
3398 }
3399
331c3da2
DD
3400 mods->printed = 1;
3401
3402 hold_dpt = dpi->templates;
3403 dpi->templates = mods->templates;
3404
59727473 3405 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
eb383413 3406 {
d00edca5 3407 d_print_function_type (dpi, mods->mod, mods->next);
331c3da2 3408 dpi->templates = hold_dpt;
d00edca5
DD
3409 return;
3410 }
59727473 3411 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
d00edca5 3412 {
d00edca5 3413 d_print_array_type (dpi, mods->mod, mods->next);
331c3da2 3414 dpi->templates = hold_dpt;
d00edca5
DD
3415 return;
3416 }
59727473 3417 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
d4edd112
DD
3418 {
3419 struct d_print_mod *hold_modifiers;
59727473 3420 struct demangle_component *dc;
d4edd112
DD
3421
3422 /* When this is on the modifier stack, we have pulled any
3423 qualifiers off the right argument already. Otherwise, we
3424 print it as usual, but don't let the left argument see any
3425 modifiers. */
3426
3427 hold_modifiers = dpi->modifiers;
3428 dpi->modifiers = NULL;
3429 d_print_comp (dpi, d_left (mods->mod));
3430 dpi->modifiers = hold_modifiers;
3431
b6fb00c0
DD
3432 if ((dpi->options & DMGL_JAVA) == 0)
3433 d_append_string_constant (dpi, "::");
3434 else
3435 d_append_char (dpi, '.');
d4edd112
DD
3436
3437 dc = d_right (mods->mod);
59727473
DD
3438 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3439 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3440 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
d4edd112
DD
3441 dc = d_left (dc);
3442
3443 d_print_comp (dpi, dc);
3444
3445 dpi->templates = hold_dpt;
3446 return;
3447 }
eb383413 3448
d00edca5 3449 d_print_mod (dpi, mods->mod);
eb383413 3450
331c3da2
DD
3451 dpi->templates = hold_dpt;
3452
858b45cf 3453 d_print_mod_list (dpi, mods->next, suffix);
eb383413 3454}
331c3da2 3455
d00edca5 3456/* Print a modifier. */
eb383413 3457
d00edca5 3458static void
9334f9c6
DD
3459d_print_mod (struct d_print_info *dpi,
3460 const struct demangle_component *mod)
d00edca5
DD
3461{
3462 switch (mod->type)
3463 {
59727473
DD
3464 case DEMANGLE_COMPONENT_RESTRICT:
3465 case DEMANGLE_COMPONENT_RESTRICT_THIS:
b6fb00c0 3466 d_append_string_constant (dpi, " restrict");
d00edca5 3467 return;
59727473
DD
3468 case DEMANGLE_COMPONENT_VOLATILE:
3469 case DEMANGLE_COMPONENT_VOLATILE_THIS:
b6fb00c0 3470 d_append_string_constant (dpi, " volatile");
d00edca5 3471 return;
59727473
DD
3472 case DEMANGLE_COMPONENT_CONST:
3473 case DEMANGLE_COMPONENT_CONST_THIS:
b6fb00c0 3474 d_append_string_constant (dpi, " const");
d00edca5 3475 return;
59727473 3476 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
d00edca5
DD
3477 d_append_char (dpi, ' ');
3478 d_print_comp (dpi, d_right (mod));
3479 return;
59727473 3480 case DEMANGLE_COMPONENT_POINTER:
d00edca5
DD
3481 /* There is no pointer symbol in Java. */
3482 if ((dpi->options & DMGL_JAVA) == 0)
3483 d_append_char (dpi, '*');
3484 return;
59727473 3485 case DEMANGLE_COMPONENT_REFERENCE:
d00edca5
DD
3486 d_append_char (dpi, '&');
3487 return;
59727473 3488 case DEMANGLE_COMPONENT_COMPLEX:
b6fb00c0 3489 d_append_string_constant (dpi, "complex ");
d00edca5 3490 return;
59727473 3491 case DEMANGLE_COMPONENT_IMAGINARY:
b6fb00c0 3492 d_append_string_constant (dpi, "imaginary ");
d00edca5 3493 return;
59727473 3494 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
858b45cf 3495 if (d_last_char (dpi) != '(')
d00edca5
DD
3496 d_append_char (dpi, ' ');
3497 d_print_comp (dpi, d_left (mod));
b6fb00c0 3498 d_append_string_constant (dpi, "::*");
d00edca5 3499 return;
59727473 3500 case DEMANGLE_COMPONENT_TYPED_NAME:
d00edca5
DD
3501 d_print_comp (dpi, d_left (mod));
3502 return;
3503 default:
3504 /* Otherwise, we have something that won't go back on the
3505 modifier stack, so we can just print it. */
3506 d_print_comp (dpi, mod);
3507 return;
3508 }
3509}
eb383413 3510
d00edca5 3511/* Print a function type, except for the return type. */
eb383413 3512
d00edca5 3513static void
9334f9c6
DD
3514d_print_function_type (struct d_print_info *dpi,
3515 const struct demangle_component *dc,
3516 struct d_print_mod *mods)
eb383413 3517{
331c3da2
DD
3518 int need_paren;
3519 int saw_mod;
2d733211 3520 int need_space;
331c3da2 3521 struct d_print_mod *p;
d4edd112 3522 struct d_print_mod *hold_modifiers;
331c3da2
DD
3523
3524 need_paren = 0;
3525 saw_mod = 0;
2d733211 3526 need_space = 0;
331c3da2 3527 for (p = mods; p != NULL; p = p->next)
d00edca5 3528 {
331c3da2
DD
3529 if (p->printed)
3530 break;
eb383413 3531
331c3da2
DD
3532 saw_mod = 1;
3533 switch (p->mod->type)
d00edca5 3534 {
2d733211
DD
3535 case DEMANGLE_COMPONENT_POINTER:
3536 case DEMANGLE_COMPONENT_REFERENCE:
3537 need_paren = 1;
3538 break;
59727473
DD
3539 case DEMANGLE_COMPONENT_RESTRICT:
3540 case DEMANGLE_COMPONENT_VOLATILE:
3541 case DEMANGLE_COMPONENT_CONST:
3542 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
59727473
DD
3543 case DEMANGLE_COMPONENT_COMPLEX:
3544 case DEMANGLE_COMPONENT_IMAGINARY:
3545 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
2d733211 3546 need_space = 1;
331c3da2
DD
3547 need_paren = 1;
3548 break;
59727473
DD
3549 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3550 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3551 case DEMANGLE_COMPONENT_CONST_THIS:
858b45cf 3552 break;
331c3da2
DD
3553 default:
3554 break;
d00edca5 3555 }
331c3da2
DD
3556 if (need_paren)
3557 break;
3558 }
eb383413 3559
331c3da2
DD
3560 if (d_left (dc) != NULL && ! saw_mod)
3561 need_paren = 1;
eb383413 3562
331c3da2 3563 if (need_paren)
858b45cf 3564 {
2d733211 3565 if (! need_space)
858b45cf 3566 {
2d733211
DD
3567 if (d_last_char (dpi) != '('
3568 && d_last_char (dpi) != '*')
3569 need_space = 1;
858b45cf 3570 }
2d733211
DD
3571 if (need_space && d_last_char (dpi) != ' ')
3572 d_append_char (dpi, ' ');
858b45cf
DD
3573 d_append_char (dpi, '(');
3574 }
eb383413 3575
d4edd112
DD
3576 hold_modifiers = dpi->modifiers;
3577 dpi->modifiers = NULL;
3578
858b45cf 3579 d_print_mod_list (dpi, mods, 0);
eb383413 3580
331c3da2
DD
3581 if (need_paren)
3582 d_append_char (dpi, ')');
eb383413 3583
d00edca5 3584 d_append_char (dpi, '(');
eb383413 3585
d00edca5 3586 if (d_right (dc) != NULL)
d4edd112 3587 d_print_comp (dpi, d_right (dc));
eb383413 3588
d00edca5 3589 d_append_char (dpi, ')');
858b45cf
DD
3590
3591 d_print_mod_list (dpi, mods, 1);
d4edd112
DD
3592
3593 dpi->modifiers = hold_modifiers;
d00edca5 3594}
eb383413 3595
d00edca5 3596/* Print an array type, except for the element type. */
eb383413 3597
d00edca5 3598static void
9334f9c6
DD
3599d_print_array_type (struct d_print_info *dpi,
3600 const struct demangle_component *dc,
3601 struct d_print_mod *mods)
d00edca5
DD
3602{
3603 int need_space;
eb383413 3604
d00edca5
DD
3605 need_space = 1;
3606 if (mods != NULL)
eb383413 3607 {
d00edca5
DD
3608 int need_paren;
3609 struct d_print_mod *p;
03d5f569 3610
d00edca5
DD
3611 need_paren = 0;
3612 for (p = mods; p != NULL; p = p->next)
eb383413 3613 {
74aee4eb 3614 if (! p->printed)
eb383413 3615 {
74aee4eb
DD
3616 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3617 {
3618 need_space = 0;
3619 break;
3620 }
3621 else
3622 {
3623 need_paren = 1;
3624 need_space = 1;
3625 break;
3626 }
eb383413 3627 }
d00edca5 3628 }
eb383413 3629
d00edca5 3630 if (need_paren)
b6fb00c0 3631 d_append_string_constant (dpi, " (");
eb383413 3632
858b45cf 3633 d_print_mod_list (dpi, mods, 0);
eb383413 3634
d00edca5
DD
3635 if (need_paren)
3636 d_append_char (dpi, ')');
3637 }
eb383413 3638
d00edca5
DD
3639 if (need_space)
3640 d_append_char (dpi, ' ');
03d5f569 3641
d00edca5 3642 d_append_char (dpi, '[');
03d5f569 3643
d00edca5
DD
3644 if (d_left (dc) != NULL)
3645 d_print_comp (dpi, d_left (dc));
eb383413 3646
d00edca5
DD
3647 d_append_char (dpi, ']');
3648}
eb383413 3649
d00edca5 3650/* Print an operator in an expression. */
eb383413 3651
d00edca5 3652static void
9334f9c6
DD
3653d_print_expr_op (struct d_print_info *dpi,
3654 const struct demangle_component *dc)
d00edca5 3655{
59727473 3656 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
b6fb00c0
DD
3657 d_append_buffer (dpi, dc->u.s_operator.op->name,
3658 dc->u.s_operator.op->len);
d00edca5
DD
3659 else
3660 d_print_comp (dpi, dc);
eb383413
L
3661}
3662
d00edca5 3663/* Print a cast. */
eb383413 3664
d00edca5 3665static void
9334f9c6
DD
3666d_print_cast (struct d_print_info *dpi,
3667 const struct demangle_component *dc)
eb383413 3668{
59727473 3669 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
d00edca5
DD
3670 d_print_comp (dpi, d_left (dc));
3671 else
3672 {
331c3da2 3673 struct d_print_mod *hold_dpm;
d00edca5 3674 struct d_print_template dpt;
0976f6a7 3675
d00edca5
DD
3676 /* It appears that for a templated cast operator, we need to put
3677 the template parameters in scope for the operator name, but
3678 not for the parameters. The effect is that we need to handle
24afc00d 3679 the template printing here. */
eb383413 3680
331c3da2
DD
3681 hold_dpm = dpi->modifiers;
3682 dpi->modifiers = NULL;
3683
d00edca5
DD
3684 dpt.next = dpi->templates;
3685 dpi->templates = &dpt;
3686 dpt.template = d_left (dc);
0976f6a7 3687
d00edca5 3688 d_print_comp (dpi, d_left (d_left (dc)));
0976f6a7 3689
d00edca5 3690 dpi->templates = dpt.next;
eb383413 3691
858b45cf
DD
3692 if (d_last_char (dpi) == '<')
3693 d_append_char (dpi, ' ');
d00edca5
DD
3694 d_append_char (dpi, '<');
3695 d_print_comp (dpi, d_right (d_left (dc)));
3696 /* Avoid generating two consecutive '>' characters, to avoid
3697 the C++ syntactic ambiguity. */
858b45cf 3698 if (d_last_char (dpi) == '>')
d00edca5
DD
3699 d_append_char (dpi, ' ');
3700 d_append_char (dpi, '>');
331c3da2
DD
3701
3702 dpi->modifiers = hold_dpm;
eb383413 3703 }
d00edca5
DD
3704}
3705
3706/* Initialize the information structure we use to pass around
3707 information. */
3708
59727473
DD
3709CP_STATIC_IF_GLIBCPP_V3
3710void
9334f9c6
DD
3711cplus_demangle_init_info (const char *mangled, int options, size_t len,
3712 struct d_info *di)
eb383413 3713{
d00edca5 3714 di->s = mangled;
b6fb00c0 3715 di->send = mangled + len;
d00edca5 3716 di->options = options;
eb383413 3717
d00edca5
DD
3718 di->n = mangled;
3719
3720 /* We can not need more components than twice the number of chars in
3721 the mangled string. Most components correspond directly to
3722 chars, but the ARGLIST types are exceptions. */
3723 di->num_comps = 2 * len;
d00edca5
DD
3724 di->next_comp = 0;
3725
3726 /* Similarly, we can not need more substitutions than there are
331c3da2
DD
3727 chars in the mangled string. */
3728 di->num_subs = len;
d00edca5 3729 di->next_sub = 0;
b6fb00c0 3730 di->did_subs = 0;
d00edca5
DD
3731
3732 di->last_name = NULL;
3733
b6fb00c0 3734 di->expansion = 0;
eb383413
L
3735}
3736
d00edca5
DD
3737/* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3738 name, return a buffer allocated with malloc holding the demangled
3739 name. OPTIONS is the usual libiberty demangler options. On
3740 success, this sets *PALC to the allocated size of the returned
3741 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3742 a memory allocation failure. On failure, this returns NULL. */
eb383413 3743
d00edca5 3744static char *
9334f9c6 3745d_demangle (const char* mangled, int options, size_t *palc)
eb383413 3746{
d00edca5
DD
3747 size_t len;
3748 int type;
3749 struct d_info di;
59727473 3750 struct demangle_component *dc;
b6fb00c0 3751 int estimate;
d00edca5 3752 char *ret;
eb383413 3753
d00edca5 3754 *palc = 0;
eb383413 3755
d00edca5
DD
3756 len = strlen (mangled);
3757
3758 if (mangled[0] == '_' && mangled[1] == 'Z')
3759 type = 0;
3760 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3761 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3762 && (mangled[9] == 'D' || mangled[9] == 'I')
3763 && mangled[10] == '_')
3764 {
3765 char *r;
eb383413 3766
d00edca5
DD
3767 r = malloc (40 + len - 11);
3768 if (r == NULL)
3769 *palc = 1;
3770 else
eb383413 3771 {
d00edca5
DD
3772 if (mangled[9] == 'I')
3773 strcpy (r, "global constructors keyed to ");
3774 else
3775 strcpy (r, "global destructors keyed to ");
3776 strcat (r, mangled + 11);
eb383413 3777 }
d00edca5 3778 return r;
eb383413
L
3779 }
3780 else
3781 {
d00edca5
DD
3782 if ((options & DMGL_TYPES) == 0)
3783 return NULL;
3784 type = 1;
eb383413
L
3785 }
3786
59727473 3787 cplus_demangle_init_info (mangled, options, len, &di);
03d5f569 3788
b6fb00c0
DD
3789 {
3790#ifdef CP_DYNAMIC_ARRAYS
59727473
DD
3791 __extension__ struct demangle_component comps[di.num_comps];
3792 __extension__ struct demangle_component *subs[di.num_subs];
b6fb00c0
DD
3793
3794 di.comps = &comps[0];
3795 di.subs = &subs[0];
3796#else
59727473
DD
3797 di.comps = ((struct demangle_component *)
3798 malloc (di.num_comps * sizeof (struct demangle_component)));
3799 di.subs = ((struct demangle_component **)
3800 malloc (di.num_subs * sizeof (struct demangle_component *)));
b6fb00c0
DD
3801 if (di.comps == NULL || di.subs == NULL)
3802 {
3803 if (di.comps != NULL)
3804 free (di.comps);
3805 if (di.subs != NULL)
3806 free (di.subs);
3807 *palc = 1;
3808 return NULL;
3809 }
3810#endif
3811
3812 if (! type)
59727473 3813 dc = cplus_demangle_mangled_name (&di, 1);
b6fb00c0 3814 else
59727473 3815 dc = cplus_demangle_type (&di);
d00edca5 3816
b6fb00c0
DD
3817 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3818 mangled string, then we didn't successfully demangle it. If
3819 DMGL_PARAMS is not set, we didn't look at the trailing
3820 parameters. */
3821 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3822 dc = NULL;
24afc00d 3823
d00edca5 3824#ifdef CP_DEMANGLE_DEBUG
b6fb00c0
DD
3825 if (dc == NULL)
3826 printf ("failed demangling\n");
3827 else
3828 d_dump (dc, 0);
d00edca5
DD
3829#endif
3830
b6fb00c0
DD
3831 /* We try to guess the length of the demangled string, to minimize
3832 calls to realloc during demangling. */
3833 estimate = len + di.expansion + 10 * di.did_subs;
3834 estimate += estimate / 8;
03d5f569 3835
b6fb00c0
DD
3836 ret = NULL;
3837 if (dc != NULL)
59727473 3838 ret = cplus_demangle_print (options, dc, estimate, palc);
03d5f569 3839
b6fb00c0
DD
3840#ifndef CP_DYNAMIC_ARRAYS
3841 free (di.comps);
3842 free (di.subs);
3843#endif
3844
3845#ifdef CP_DEMANGLE_DEBUG
3846 if (ret != NULL)
3847 {
3848 int rlen;
3849
3850 rlen = strlen (ret);
3851 if (rlen > 2 * estimate)
3852 printf ("*** Length %d much greater than estimate %d\n",
3853 rlen, estimate);
3854 else if (rlen > estimate)
3855 printf ("*** Length %d greater than estimate %d\n",
3856 rlen, estimate);
3857 else if (rlen < estimate / 2)
3858 printf ("*** Length %d much less than estimate %d\n",
3859 rlen, estimate);
3860 }
3861#endif
3862 }
03d5f569 3863
d00edca5 3864 return ret;
eb383413
L
3865}
3866
0c4460bb 3867#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
d00edca5 3868
9334f9c6 3869extern char *__cxa_demangle (const char *, char *, size_t *, int *);
03d5f569 3870
d00edca5
DD
3871/* ia64 ABI-mandated entry point in the C++ runtime library for
3872 performing demangling. MANGLED_NAME is a NUL-terminated character
3873 string containing the name to be demangled.
03d5f569
JM
3874
3875 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3876 *LENGTH bytes, into which the demangled name is stored. If
3877 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3878 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
d00edca5 3879 is placed in a region of memory allocated with malloc.
03d5f569
JM
3880
3881 If LENGTH is non-NULL, the length of the buffer conaining the
d00edca5 3882 demangled name, is placed in *LENGTH.
03d5f569
JM
3883
3884 The return value is a pointer to the start of the NUL-terminated
3885 demangled name, or NULL if the demangling fails. The caller is
d00edca5 3886 responsible for deallocating this memory using free.
03d5f569
JM
3887
3888 *STATUS is set to one of the following values:
3889 0: The demangling operation succeeded.
d00edca5 3890 -1: A memory allocation failure occurred.
03d5f569
JM
3891 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3892 -3: One of the arguments is invalid.
3893
d00edca5 3894 The demangling is performed using the C++ ABI mangling rules, with
03d5f569
JM
3895 GNU extensions. */
3896
3897char *
9334f9c6
DD
3898__cxa_demangle (const char *mangled_name, char *output_buffer,
3899 size_t *length, int *status)
03d5f569 3900{
d00edca5
DD
3901 char *demangled;
3902 size_t alc;
03d5f569 3903
d00edca5
DD
3904 if (mangled_name == NULL)
3905 {
74aee4eb
DD
3906 if (status != NULL)
3907 *status = -3;
03d5f569
JM
3908 return NULL;
3909 }
03d5f569 3910
d00edca5 3911 if (output_buffer != NULL && length == NULL)
03d5f569 3912 {
74aee4eb
DD
3913 if (status != NULL)
3914 *status = -3;
d00edca5 3915 return NULL;
03d5f569 3916 }
d00edca5 3917
74aee4eb 3918 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
d00edca5
DD
3919
3920 if (demangled == NULL)
03d5f569 3921 {
74aee4eb
DD
3922 if (status != NULL)
3923 {
3924 if (alc == 1)
3925 *status = -1;
3926 else
3927 *status = -2;
3928 }
03d5f569
JM
3929 return NULL;
3930 }
d00edca5
DD
3931
3932 if (output_buffer == NULL)
3933 {
3934 if (length != NULL)
3935 *length = alc;
3936 }
03d5f569 3937 else
03d5f569 3938 {
d00edca5
DD
3939 if (strlen (demangled) < *length)
3940 {
3941 strcpy (output_buffer, demangled);
3942 free (demangled);
3943 demangled = output_buffer;
3944 }
3945 else
3946 {
3947 free (output_buffer);
3948 *length = alc;
3949 }
03d5f569 3950 }
d00edca5 3951
74aee4eb
DD
3952 if (status != NULL)
3953 *status = 0;
d00edca5
DD
3954
3955 return demangled;
03d5f569
JM
3956}
3957
0c4460bb 3958#else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
03d5f569 3959
d00edca5
DD
3960/* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3961 mangled name, return a buffer allocated with malloc holding the
3962 demangled name. Otherwise, return NULL. */
eb383413
L
3963
3964char *
9334f9c6 3965cplus_demangle_v3 (const char* mangled, int options)
eb383413 3966{
d00edca5 3967 size_t alc;
849ee224 3968
d00edca5 3969 return d_demangle (mangled, options, &alc);
eb383413
L
3970}
3971
bc9bf259
DD
3972/* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3973 conventions, but the output formatting is a little different.
3974 This instructs the C++ demangler not to emit pointer characters ("*"), and
3975 to use Java's namespace separator symbol ("." instead of "::"). It then
3976 does an additional pass over the demangled output to replace instances
3977 of JArray<TYPE> with TYPE[]. */
3978
3979char *
9334f9c6 3980java_demangle_v3 (const char* mangled)
bc9bf259 3981{
d00edca5
DD
3982 size_t alc;
3983 char *demangled;
3984 int nesting;
3985 char *from;
3986 char *to;
3987
51948b34 3988 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
d00edca5
DD
3989
3990 if (demangled == NULL)
3991 return NULL;
3992
3993 nesting = 0;
3994 from = demangled;
3995 to = from;
3996 while (*from != '\0')
bc9bf259 3997 {
d00edca5
DD
3998 if (strncmp (from, "JArray<", 7) == 0)
3999 {
4000 from += 7;
bc9bf259 4001 ++nesting;
bc9bf259 4002 }
d00edca5
DD
4003 else if (nesting > 0 && *from == '>')
4004 {
4005 while (to > demangled && to[-1] == ' ')
4006 --to;
4007 *to++ = '[';
4008 *to++ = ']';
bc9bf259 4009 --nesting;
d00edca5 4010 ++from;
bc9bf259
DD
4011 }
4012 else
d00edca5 4013 *to++ = *from++;
bc9bf259
DD
4014 }
4015
d00edca5 4016 *to = '\0';
f2160d2b 4017
d00edca5 4018 return demangled;
bc9bf259
DD
4019}
4020
0c4460bb 4021#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
03d5f569 4022
2a9dffbf 4023#ifndef IN_GLIBCPP_V3
d00edca5
DD
4024
4025/* Demangle a string in order to find out whether it is a constructor
4026 or destructor. Return non-zero on success. Set *CTOR_KIND and
4027 *DTOR_KIND appropriately. */
4028
4029static int
9334f9c6
DD
4030is_ctor_or_dtor (const char *mangled,
4031 enum gnu_v3_ctor_kinds *ctor_kind,
4032 enum gnu_v3_dtor_kinds *dtor_kind)
e61231f1 4033{
d00edca5 4034 struct d_info di;
59727473 4035 struct demangle_component *dc;
858b45cf 4036 int ret;
e61231f1 4037
d00edca5
DD
4038 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4039 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4040
59727473 4041 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
e61231f1 4042
b6fb00c0
DD
4043 {
4044#ifdef CP_DYNAMIC_ARRAYS
59727473
DD
4045 __extension__ struct demangle_component comps[di.num_comps];
4046 __extension__ struct demangle_component *subs[di.num_subs];
b6fb00c0
DD
4047
4048 di.comps = &comps[0];
4049 di.subs = &subs[0];
4050#else
59727473
DD
4051 di.comps = ((struct demangle_component *)
4052 malloc (di.num_comps * sizeof (struct demangle_component)));
4053 di.subs = ((struct demangle_component **)
4054 malloc (di.num_subs * sizeof (struct demangle_component *)));
b6fb00c0
DD
4055 if (di.comps == NULL || di.subs == NULL)
4056 {
4057 if (di.comps != NULL)
4058 free (di.comps);
4059 if (di.subs != NULL)
4060 free (di.subs);
2f9c4058 4061 return 0;
b6fb00c0
DD
4062 }
4063#endif
d00edca5 4064
59727473 4065 dc = cplus_demangle_mangled_name (&di, 1);
d35d0cd4 4066
b6fb00c0
DD
4067 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4068 to demangle the entire string. */
e61231f1 4069
b6fb00c0
DD
4070 ret = 0;
4071 while (dc != NULL)
4072 {
4073 switch (dc->type)
4074 {
4075 default:
4076 dc = NULL;
4077 break;
59727473
DD
4078 case DEMANGLE_COMPONENT_TYPED_NAME:
4079 case DEMANGLE_COMPONENT_TEMPLATE:
4080 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4081 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4082 case DEMANGLE_COMPONENT_CONST_THIS:
b6fb00c0
DD
4083 dc = d_left (dc);
4084 break;
59727473
DD
4085 case DEMANGLE_COMPONENT_QUAL_NAME:
4086 case DEMANGLE_COMPONENT_LOCAL_NAME:
b6fb00c0
DD
4087 dc = d_right (dc);
4088 break;
59727473 4089 case DEMANGLE_COMPONENT_CTOR:
b6fb00c0
DD
4090 *ctor_kind = dc->u.s_ctor.kind;
4091 ret = 1;
4092 dc = NULL;
4093 break;
59727473 4094 case DEMANGLE_COMPONENT_DTOR:
b6fb00c0
DD
4095 *dtor_kind = dc->u.s_dtor.kind;
4096 ret = 1;
4097 dc = NULL;
4098 break;
4099 }
4100 }
4101
4102#ifndef CP_DYNAMIC_ARRAYS
4103 free (di.subs);
4104 free (di.comps);
4105#endif
4106 }
858b45cf
DD
4107
4108 return ret;
e61231f1
JB
4109}
4110
d00edca5
DD
4111/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4112 name. A non-zero return indicates the type of constructor. */
e61231f1 4113
e61231f1 4114enum gnu_v3_ctor_kinds
9334f9c6 4115is_gnu_v3_mangled_ctor (const char *name)
e61231f1 4116{
d00edca5
DD
4117 enum gnu_v3_ctor_kinds ctor_kind;
4118 enum gnu_v3_dtor_kinds dtor_kind;
e61231f1 4119
d00edca5 4120 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
585cc78f 4121 return (enum gnu_v3_ctor_kinds) 0;
d00edca5 4122 return ctor_kind;
e61231f1
JB
4123}
4124
4125
d00edca5
DD
4126/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4127 name. A non-zero return indicates the type of destructor. */
4128
e61231f1 4129enum gnu_v3_dtor_kinds
9334f9c6 4130is_gnu_v3_mangled_dtor (const char *name)
e61231f1 4131{
d00edca5
DD
4132 enum gnu_v3_ctor_kinds ctor_kind;
4133 enum gnu_v3_dtor_kinds dtor_kind;
e61231f1 4134
d00edca5 4135 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
585cc78f 4136 return (enum gnu_v3_dtor_kinds) 0;
d00edca5 4137 return dtor_kind;
e61231f1
JB
4138}
4139
d00edca5 4140#endif /* IN_GLIBCPP_V3 */
e61231f1 4141
eb383413
L
4142#ifdef STANDALONE_DEMANGLER
4143
4144#include "getopt.h"
d00edca5
DD
4145#include "dyn-string.h"
4146
e064c173 4147static void print_usage (FILE* fp, int exit_value);
eb383413 4148
d00edca5
DD
4149#define IS_ALPHA(CHAR) \
4150 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4151 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
eb383413
L
4152
4153/* Non-zero if CHAR is a character than can occur in a mangled name. */
4154#define is_mangled_char(CHAR) \
74bcd529
DD
4155 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4156 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
eb383413
L
4157
4158/* The name of this program, as invoked. */
4159const char* program_name;
4160
4161/* Prints usage summary to FP and then exits with EXIT_VALUE. */
4162
4163static void
9334f9c6 4164print_usage (FILE* fp, int exit_value)
eb383413
L
4165{
4166 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
74bcd529 4167 fprintf (fp, "Options:\n");
eb383413 4168 fprintf (fp, " -h,--help Display this message.\n");
6d95373e 4169 fprintf (fp, " -p,--no-params Don't display function parameters\n");
eb383413
L
4170 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4171 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4172
4173 exit (exit_value);
4174}
4175
4176/* Option specification for getopt_long. */
c23795e2 4177static const struct option long_options[] =
eb383413 4178{
6d95373e
DD
4179 { "help", no_argument, NULL, 'h' },
4180 { "no-params", no_argument, NULL, 'p' },
4181 { "verbose", no_argument, NULL, 'v' },
4182 { NULL, no_argument, NULL, 0 },
eb383413
L
4183};
4184
4185/* Main entry for a demangling filter executable. It will demangle
4186 its command line arguments, if any. If none are provided, it will
4187 filter stdin to stdout, replacing any recognized mangled C++ names
4188 with their demangled equivalents. */
4189
4190int
9334f9c6 4191main (int argc, char *argv[])
eb383413 4192{
eb383413
L
4193 int i;
4194 int opt_char;
d00edca5 4195 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
eb383413
L
4196
4197 /* Use the program name of this program, as invoked. */
4198 program_name = argv[0];
4199
4200 /* Parse options. */
4201 do
4202 {
6d95373e 4203 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
eb383413
L
4204 switch (opt_char)
4205 {
4206 case '?': /* Unrecognized option. */
4207 print_usage (stderr, 1);
4208 break;
4209
4210 case 'h':
4211 print_usage (stdout, 0);
4212 break;
4213
6d95373e
DD
4214 case 'p':
4215 options &= ~ DMGL_PARAMS;
4216 break;
4217
eb383413 4218 case 'v':
d00edca5 4219 options |= DMGL_VERBOSE;
eb383413
L
4220 break;
4221 }
4222 }
4223 while (opt_char != -1);
4224
4225 if (optind == argc)
4226 /* No command line arguments were provided. Filter stdin. */
4227 {
4228 dyn_string_t mangled = dyn_string_new (3);
d00edca5 4229 char *s;
eb383413
L
4230
4231 /* Read all of input. */
4232 while (!feof (stdin))
4233 {
d00edca5 4234 char c;
eb383413
L
4235
4236 /* Pile characters into mangled until we hit one that can't
4237 occur in a mangled name. */
4238 c = getchar ();
4239 while (!feof (stdin) && is_mangled_char (c))
4240 {
4241 dyn_string_append_char (mangled, c);
4242 if (feof (stdin))
4243 break;
4244 c = getchar ();
4245 }
4246
d00edca5 4247 if (dyn_string_length (mangled) > 0)
03d5f569 4248 {
74aee4eb
DD
4249#ifdef IN_GLIBCPP_V3
4250 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4251#else
d00edca5 4252 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
74aee4eb 4253#endif
d00edca5
DD
4254
4255 if (s != NULL)
4256 {
4257 fputs (s, stdout);
4258 free (s);
4259 }
4260 else
4261 {
4262 /* It might not have been a mangled name. Print the
4263 original text. */
4264 fputs (dyn_string_buf (mangled), stdout);
4265 }
4266
4267 dyn_string_clear (mangled);
03d5f569 4268 }
eb383413
L
4269
4270 /* If we haven't hit EOF yet, we've read one character that
4271 can't occur in a mangled name, so print it out. */
4272 if (!feof (stdin))
4273 putchar (c);
eb383413
L
4274 }
4275
4276 dyn_string_delete (mangled);
eb383413
L
4277 }
4278 else
4279 /* Demangle command line arguments. */
4280 {
eb383413
L
4281 /* Loop over command line arguments. */
4282 for (i = optind; i < argc; ++i)
4283 {
d00edca5 4284 char *s;
74aee4eb
DD
4285#ifdef IN_GLIBCPP_V3
4286 int status;
4287#endif
d00edca5 4288
eb383413 4289 /* Attempt to demangle. */
74aee4eb
DD
4290#ifdef IN_GLIBCPP_V3
4291 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4292#else
d00edca5 4293 s = cplus_demangle_v3 (argv[i], options);
74aee4eb 4294#endif
eb383413
L
4295
4296 /* If it worked, print the demangled name. */
d00edca5 4297 if (s != NULL)
03d5f569 4298 {
d00edca5
DD
4299 printf ("%s\n", s);
4300 free (s);
03d5f569 4301 }
d00edca5 4302 else
74aee4eb
DD
4303 {
4304#ifdef IN_GLIBCPP_V3
4305 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4306#else
4307 fprintf (stderr, "Failed: %s\n", argv[i]);
4308#endif
4309 }
eb383413 4310 }
eb383413
L
4311 }
4312
4313 return 0;
4314}
4315
4316#endif /* STANDALONE_DEMANGLER */