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