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