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