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