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