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