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