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