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