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