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