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