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