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