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