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