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