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