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