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