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