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