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