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