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