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