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