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