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