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