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