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