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