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