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