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