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