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