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