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