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