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