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