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