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