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