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