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