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