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