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