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