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