merge from gcc
[deliverable/binutils-gdb.git] / libiberty / cp-demangle.c
CommitLineData
d00edca5
DD
1/* Demangler for g++ V3 ABI.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
eb383413 4
9ad1aa29 5 This file is part of the libiberty library, which is part of GCC.
74bcd529 6
9ad1aa29 7 This file is free software; you can redistribute it and/or modify
eb383413
L
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
35efcd67
DD
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
20
eb383413
L
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29*/
30
eb383413
L
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
d00edca5 35#include <stdio.h>
b1233257 36
eb383413
L
37#ifdef HAVE_STDLIB_H
38#include <stdlib.h>
39#endif
eb383413
L
40#ifdef HAVE_STRING_H
41#include <string.h>
42#endif
43
44#include "ansidecl.h"
45#include "libiberty.h"
eb383413
L
46#include "demangle.h"
47
d00edca5
DD
48/* This code implements a demangler for the g++ V3 ABI. The ABI is
49 described on this web page:
50 http://www.codesourcery.com/cxx-abi/abi.html#mangling
eb383413 51
d00edca5
DD
52 This code was written while looking at the demangler written by
53 Alex Samuel <samuel@codesourcery.com>.
54
55 This code first pulls the mangled name apart into a list of
56 components, and then walks the list generating the demangled
57 name. */
58
59/* Avoid pulling in the ctype tables for this simple usage. */
60#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
03d5f569 61
74bcd529
DD
62/* The prefix prepended by GCC to an identifier represnting the
63 anonymous namespace. */
64#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
d00edca5
DD
65#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
66 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
74bcd529 67
d00edca5 68/* Information we keep for operators. */
eb383413 69
d00edca5 70struct d_operator_info
eb383413 71{
d00edca5
DD
72 /* Mangled name. */
73 const char *code;
74 /* Real name. */
75 const char *name;
76 /* Number of arguments. */
77 int args;
78};
59666b35 79
d00edca5 80/* How to print the value of a builtin type. */
59666b35 81
d00edca5
DD
82enum d_builtin_type_print
83{
84 /* Print as (type)val. */
85 D_PRINT_DEFAULT,
86 /* Print as integer. */
87 D_PRINT_INT,
88 /* Print as long, with trailing `l'. */
89 D_PRINT_LONG,
90 /* Print as bool. */
91 D_PRINT_BOOL,
92 /* Print in usual way, but here to detect void. */
93 D_PRINT_VOID
eb383413
L
94};
95
d00edca5 96/* Information we keep for a builtin type. */
eb383413 97
d00edca5 98struct d_builtin_type_info
eb383413 99{
d00edca5
DD
100 /* Type name. */
101 const char *name;
102 /* Type name when using Java. */
103 const char *java_name;
104 /* How to print a value of this type. */
105 enum d_builtin_type_print print;
106};
eb383413 107
d00edca5
DD
108/* Component types found in mangled names. */
109
110enum d_comp_type
111{
112 /* A name. */
113 D_COMP_NAME,
114 /* A qualified name. */
115 D_COMP_QUAL_NAME,
116 /* A typed name. */
117 D_COMP_TYPED_NAME,
118 /* A template. */
119 D_COMP_TEMPLATE,
120 /* A template parameter. */
121 D_COMP_TEMPLATE_PARAM,
122 /* A constructor. */
123 D_COMP_CTOR,
124 /* A destructor. */
125 D_COMP_DTOR,
126 /* A vtable. */
127 D_COMP_VTABLE,
128 /* A VTT structure. */
129 D_COMP_VTT,
130 /* A construction vtable. */
131 D_COMP_CONSTRUCTION_VTABLE,
132 /* A typeinfo structure. */
133 D_COMP_TYPEINFO,
134 /* A typeinfo name. */
135 D_COMP_TYPEINFO_NAME,
136 /* A typeinfo function. */
137 D_COMP_TYPEINFO_FN,
138 /* A thunk. */
139 D_COMP_THUNK,
140 /* A virtual thunk. */
141 D_COMP_VIRTUAL_THUNK,
142 /* A covariant thunk. */
143 D_COMP_COVARIANT_THUNK,
144 /* A Java class. */
145 D_COMP_JAVA_CLASS,
146 /* A guard variable. */
147 D_COMP_GUARD,
148 /* A reference temporary. */
149 D_COMP_REFTEMP,
150 /* A standard substitution. */
151 D_COMP_SUB_STD,
152 /* The restrict qualifier. */
153 D_COMP_RESTRICT,
154 /* The volatile qualifier. */
155 D_COMP_VOLATILE,
156 /* The const qualifier. */
157 D_COMP_CONST,
158 /* A vendor qualifier. */
159 D_COMP_VENDOR_TYPE_QUAL,
160 /* A pointer. */
161 D_COMP_POINTER,
162 /* A reference. */
163 D_COMP_REFERENCE,
164 /* A complex type. */
165 D_COMP_COMPLEX,
166 /* An imaginary type. */
167 D_COMP_IMAGINARY,
168 /* A builtin type. */
169 D_COMP_BUILTIN_TYPE,
170 /* A vendor's builtin type. */
171 D_COMP_VENDOR_TYPE,
172 /* A function type. */
173 D_COMP_FUNCTION_TYPE,
174 /* An array type. */
175 D_COMP_ARRAY_TYPE,
176 /* A pointer to member type. */
177 D_COMP_PTRMEM_TYPE,
178 /* An argument list. */
179 D_COMP_ARGLIST,
180 /* A template argument list. */
181 D_COMP_TEMPLATE_ARGLIST,
182 /* An operator. */
183 D_COMP_OPERATOR,
184 /* An extended operator. */
185 D_COMP_EXTENDED_OPERATOR,
186 /* A typecast. */
187 D_COMP_CAST,
188 /* A unary expression. */
189 D_COMP_UNARY,
190 /* A binary expression. */
191 D_COMP_BINARY,
192 /* Arguments to a binary expression. */
193 D_COMP_BINARY_ARGS,
194 /* A trinary expression. */
195 D_COMP_TRINARY,
196 /* Arguments to a trinary expression. */
197 D_COMP_TRINARY_ARG1,
198 D_COMP_TRINARY_ARG2,
199 /* A literal. */
200 D_COMP_LITERAL
eb383413
L
201};
202
d00edca5 203/* A component of the mangled name. */
eb383413 204
d00edca5 205struct d_comp
eb383413 206{
d00edca5
DD
207 /* The type of this component. */
208 enum d_comp_type type;
209 union
210 {
211 /* For D_COMP_NAME. */
212 struct
213 {
214 /* A pointer to the name (not NULL terminated) and it's
215 length. */
216 const char *s;
217 int len;
218 } s_name;
eb383413 219
d00edca5
DD
220 /* For D_COMP_OPERATOR. */
221 struct
222 {
223 /* Operator. */
224 const struct d_operator_info *op;
225 } s_operator;
eb383413 226
d00edca5
DD
227 /* For D_COMP_EXTENDED_OPERATOR. */
228 struct
229 {
230 /* Number of arguments. */
231 int args;
232 /* Name. */
233 struct d_comp *name;
234 } s_extended_operator;
eb383413 235
d00edca5
DD
236 /* For D_COMP_CTOR. */
237 struct
238 {
239 enum gnu_v3_ctor_kinds kind;
240 struct d_comp *name;
241 } s_ctor;
eb383413 242
d00edca5
DD
243 /* For D_COMP_DTOR. */
244 struct
245 {
246 enum gnu_v3_dtor_kinds kind;
247 struct d_comp *name;
248 } s_dtor;
eb383413 249
d00edca5
DD
250 /* For D_COMP_BUILTIN_TYPE. */
251 struct
252 {
253 const struct d_builtin_type_info *type;
254 } s_builtin;
255
256 /* For D_COMP_SUB_STD. */
257 struct
258 {
259 const char* string;
260 } s_string;
eb383413 261
d00edca5
DD
262 /* For D_COMP_TEMPLATE_PARAM. */
263 struct
264 {
265 long number;
266 } s_number;
eb383413 267
d00edca5
DD
268 /* For other types. */
269 struct
270 {
271 struct d_comp *left;
272 struct d_comp *right;
273 } s_binary;
eb383413 274
d00edca5
DD
275 } u;
276};
eb383413 277
d00edca5
DD
278#define d_left(dc) ((dc)->u.s_binary.left)
279#define d_right(dc) ((dc)->u.s_binary.right)
280
281/* The information structure we pass around. */
282
283struct d_info
284{
285 /* The string we are demangling. */
286 const char *s;
287 /* The options passed to the demangler. */
288 int options;
289 /* The next character in the string to consider. */
290 const char *n;
291 /* The array of components. */
292 struct d_comp *comps;
293 /* The index of the next available component. */
294 int next_comp;
295 /* The number of available component structures. */
296 int num_comps;
297 /* The array of substitutions. */
298 struct d_comp **subs;
299 /* The index of the next substitution. */
300 int next_sub;
301 /* The number of available entries in the subs array. */
302 int num_subs;
303 /* The last name we saw, for constructors and destructors. */
304 struct d_comp *last_name;
305};
eb383413 306
d00edca5
DD
307#define d_peek_char(di) (*((di)->n))
308#define d_peek_next_char(di) ((di)->n[1])
309#define d_advance(di, i) ((di)->n += (i))
310#define d_next_char(di) (*((di)->n++))
311#define d_str(di) ((di)->n)
eb383413 312
d00edca5 313/* A list of templates. This is used while printing. */
eb383413 314
d00edca5
DD
315struct d_print_template
316{
317 /* Next template on the list. */
318 struct d_print_template *next;
319 /* This template. */
320 const struct d_comp *template;
321};
eb383413 322
d00edca5 323/* A list of type modifiers. This is used while printing. */
eb383413 324
d00edca5
DD
325struct d_print_mod
326{
327 /* Next modifier on the list. These are in the reverse of the order
328 in which they appeared in the mangled string. */
329 struct d_print_mod *next;
330 /* The modifier. */
331 const struct d_comp *mod;
332 /* Whether this modifier was printed. */
333 int printed;
331c3da2
DD
334 /* The list of templates which applies to this modifier. */
335 struct d_print_template *templates;
d00edca5 336};
eb383413 337
d00edca5
DD
338/* We use this structure to hold information during printing. */
339
340struct d_print_info
341{
342 /* The options passed to the demangler. */
343 int options;
344 /* Buffer holding the result. */
345 char *buf;
346 /* Current length of data in buffer. */
347 size_t len;
348 /* Allocated size of buffer. */
349 size_t alc;
350 /* The current list of templates, if any. */
351 struct d_print_template *templates;
352 /* The current list of modifiers (e.g., pointer, reference, etc.),
353 if any. */
354 struct d_print_mod *modifiers;
355 /* Set to 1 if we had a memory allocation failure. */
356 int allocation_failure;
357};
e61231f1 358
d00edca5 359#define d_print_saw_error(dpi) ((dpi)->buf == NULL)
e61231f1 360
d00edca5
DD
361#define d_append_char(dpi, c) \
362 do \
363 { \
364 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
365 (dpi)->buf[(dpi)->len++] = (c); \
366 else \
367 d_print_append_char ((dpi), (c)); \
368 } \
369 while (0)
e61231f1 370
d00edca5
DD
371#define d_append_buffer(dpi, s, l) \
372 do \
373 { \
374 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
375 { \
376 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
377 (dpi)->len += l; \
378 } \
379 else \
380 d_print_append_buffer ((dpi), (s), (l)); \
381 } \
382 while (0)
eb383413 383
d00edca5
DD
384#define d_append_string(dpi, s) \
385 do \
386 { \
387 size_t d_append_string_len = strlen (s); \
388 d_append_buffer ((dpi), (s), d_append_string_len); \
389 } \
03d5f569
JM
390 while (0)
391
eb383413 392#ifdef CP_DEMANGLE_DEBUG
d00edca5 393static void d_dump PARAMS ((struct d_comp *, int));
eb383413 394#endif
d00edca5
DD
395static struct d_comp *d_make_empty PARAMS ((struct d_info *,
396 enum d_comp_type));
397static struct d_comp *d_make_comp PARAMS ((struct d_info *, enum d_comp_type,
398 struct d_comp *, struct d_comp *));
399static struct d_comp *d_make_name PARAMS ((struct d_info *, const char *,
400 int));
401static struct d_comp *d_make_builtin_type PARAMS ((struct d_info *,
402 const struct d_builtin_type_info *));
403static struct d_comp *d_make_operator PARAMS ((struct d_info *,
404 const struct d_operator_info *));
405static struct d_comp *d_make_extended_operator PARAMS ((struct d_info *,
406 int,
407 struct d_comp *));
408static struct d_comp *d_make_ctor PARAMS ((struct d_info *,
409 enum gnu_v3_ctor_kinds,
410 struct d_comp *));
411static struct d_comp *d_make_dtor PARAMS ((struct d_info *,
412 enum gnu_v3_dtor_kinds,
413 struct d_comp *));
414static struct d_comp *d_make_template_param PARAMS ((struct d_info *, long));
415static struct d_comp *d_make_sub PARAMS ((struct d_info *, const char *));
331c3da2 416static struct d_comp *d_mangled_name PARAMS ((struct d_info *, int));
d00edca5
DD
417static int has_return_type PARAMS ((struct d_comp *));
418static int is_ctor_dtor_or_conversion PARAMS ((struct d_comp *));
6d95373e 419static struct d_comp *d_encoding PARAMS ((struct d_info *, int));
d00edca5
DD
420static struct d_comp *d_name PARAMS ((struct d_info *));
421static struct d_comp *d_nested_name PARAMS ((struct d_info *));
422static struct d_comp *d_prefix PARAMS ((struct d_info *));
423static struct d_comp *d_unqualified_name PARAMS ((struct d_info *));
424static struct d_comp *d_source_name PARAMS ((struct d_info *));
425static long d_number PARAMS ((struct d_info *));
426static struct d_comp *d_identifier PARAMS ((struct d_info *, int));
427static struct d_comp *d_operator_name PARAMS ((struct d_info *));
428static struct d_comp *d_special_name PARAMS ((struct d_info *));
429static int d_call_offset PARAMS ((struct d_info *, int));
430static struct d_comp *d_ctor_dtor_name PARAMS ((struct d_info *));
431static struct d_comp *d_type PARAMS ((struct d_info *));
432static struct d_comp **d_cv_qualifiers PARAMS ((struct d_info *,
433 struct d_comp **));
434static struct d_comp *d_function_type PARAMS ((struct d_info *));
435static struct d_comp *d_bare_function_type PARAMS ((struct d_info *, int));
436static struct d_comp *d_class_enum_type PARAMS ((struct d_info *));
437static struct d_comp *d_array_type PARAMS ((struct d_info *));
438static struct d_comp *d_pointer_to_member_type PARAMS ((struct d_info *));
439static struct d_comp *d_template_param PARAMS ((struct d_info *));
440static struct d_comp *d_template_args PARAMS ((struct d_info *));
441static struct d_comp *d_template_arg PARAMS ((struct d_info *));
442static struct d_comp *d_expression PARAMS ((struct d_info *));
443static struct d_comp *d_expr_primary PARAMS ((struct d_info *));
444static struct d_comp *d_local_name PARAMS ((struct d_info *));
445static int d_discriminator PARAMS ((struct d_info *));
446static int d_add_substitution PARAMS ((struct d_info *, struct d_comp *));
447static struct d_comp *d_substitution PARAMS ((struct d_info *));
448static void d_print_resize PARAMS ((struct d_print_info *, size_t));
449static void d_print_append_char PARAMS ((struct d_print_info *, int));
450static void d_print_append_buffer PARAMS ((struct d_print_info *, const char *,
451 size_t));
452static void d_print_error PARAMS ((struct d_print_info *));
453static char *d_print PARAMS ((int, const struct d_comp *, size_t *));
454static void d_print_comp PARAMS ((struct d_print_info *,
455 const struct d_comp *));
456static void d_print_identifier PARAMS ((struct d_print_info *, const char *,
457 int));
458static void d_print_mod_list PARAMS ((struct d_print_info *,
459 struct d_print_mod *));
460static void d_print_mod PARAMS ((struct d_print_info *,
461 const struct d_comp *));
462static void d_print_function_type PARAMS ((struct d_print_info *,
463 const struct d_comp *,
464 struct d_print_mod *));
465static void d_print_array_type PARAMS ((struct d_print_info *,
466 const struct d_comp *,
467 struct d_print_mod *));
468static void d_print_expr_op PARAMS ((struct d_print_info *,
469 const struct d_comp *));
470static void d_print_cast PARAMS ((struct d_print_info *,
471 const struct d_comp *));
472static int d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
473static char *d_demangle PARAMS ((const char *, int, size_t *));
474
eb383413 475#ifdef CP_DEMANGLE_DEBUG
d00edca5
DD
476
477static void
478d_dump (dc, indent)
479 struct d_comp *dc;
480 int indent;
eb383413
L
481{
482 int i;
eb383413 483
d00edca5
DD
484 if (dc == NULL)
485 return;
486
487 for (i = 0; i < indent; ++i)
488 putchar (' ');
489
490 switch (dc->type)
491 {
492 case D_COMP_NAME:
493 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
494 return;
495 case D_COMP_TEMPLATE_PARAM:
496 printf ("template parameter %ld\n", dc->u.s_number.number);
497 return;
498 case D_COMP_CTOR:
499 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
500 d_dump (dc->u.s_ctor.name, indent + 2);
501 return;
502 case D_COMP_DTOR:
503 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
504 d_dump (dc->u.s_dtor.name, indent + 2);
505 return;
506 case D_COMP_SUB_STD:
507 printf ("standard substitution %s\n", dc->u.s_string.string);
508 return;
509 case D_COMP_BUILTIN_TYPE:
510 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
511 return;
512 case D_COMP_OPERATOR:
513 printf ("operator %s\n", dc->u.s_operator.op->name);
514 return;
515 case D_COMP_EXTENDED_OPERATOR:
516 printf ("extended operator with %d args\n",
517 dc->u.s_extended_operator.args);
518 d_dump (dc->u.s_extended_operator.name, indent + 2);
519 return;
520
521 case D_COMP_QUAL_NAME:
522 printf ("qualified name\n");
523 break;
524 case D_COMP_TYPED_NAME:
525 printf ("typed name\n");
526 break;
527 case D_COMP_TEMPLATE:
528 printf ("template\n");
529 break;
530 case D_COMP_VTABLE:
531 printf ("vtable\n");
532 break;
533 case D_COMP_VTT:
534 printf ("VTT\n");
535 break;
536 case D_COMP_CONSTRUCTION_VTABLE:
537 printf ("construction vtable\n");
538 break;
539 case D_COMP_TYPEINFO:
540 printf ("typeinfo\n");
541 break;
542 case D_COMP_TYPEINFO_NAME:
543 printf ("typeinfo name\n");
544 break;
545 case D_COMP_TYPEINFO_FN:
546 printf ("typeinfo function\n");
547 break;
548 case D_COMP_THUNK:
549 printf ("thunk\n");
550 break;
551 case D_COMP_VIRTUAL_THUNK:
552 printf ("virtual thunk\n");
553 break;
554 case D_COMP_COVARIANT_THUNK:
555 printf ("covariant thunk\n");
556 break;
557 case D_COMP_JAVA_CLASS:
558 printf ("java class\n");
559 break;
560 case D_COMP_GUARD:
561 printf ("guard\n");
562 break;
563 case D_COMP_REFTEMP:
564 printf ("reference temporary\n");
565 break;
566 case D_COMP_RESTRICT:
567 printf ("restrict\n");
568 break;
569 case D_COMP_VOLATILE:
570 printf ("volatile\n");
571 break;
572 case D_COMP_CONST:
573 printf ("const\n");
574 break;
575 case D_COMP_VENDOR_TYPE_QUAL:
576 printf ("vendor type qualifier\n");
577 break;
578 case D_COMP_POINTER:
579 printf ("pointer\n");
580 break;
581 case D_COMP_REFERENCE:
582 printf ("reference\n");
583 break;
584 case D_COMP_COMPLEX:
585 printf ("complex\n");
586 break;
587 case D_COMP_IMAGINARY:
588 printf ("imaginary\n");
589 break;
590 case D_COMP_VENDOR_TYPE:
591 printf ("vendor type\n");
592 break;
593 case D_COMP_FUNCTION_TYPE:
594 printf ("function type\n");
595 break;
596 case D_COMP_ARRAY_TYPE:
597 printf ("array type\n");
598 break;
599 case D_COMP_PTRMEM_TYPE:
600 printf ("pointer to member type\n");
601 break;
602 case D_COMP_ARGLIST:
603 printf ("argument list\n");
604 break;
605 case D_COMP_TEMPLATE_ARGLIST:
606 printf ("template argument list\n");
607 break;
608 case D_COMP_CAST:
609 printf ("cast\n");
610 break;
611 case D_COMP_UNARY:
612 printf ("unary operator\n");
613 break;
614 case D_COMP_BINARY:
615 printf ("binary operator\n");
616 break;
617 case D_COMP_BINARY_ARGS:
618 printf ("binary operator arguments\n");
619 break;
620 case D_COMP_TRINARY:
621 printf ("trinary operator\n");
622 break;
623 case D_COMP_TRINARY_ARG1:
624 printf ("trinary operator arguments 1\n");
625 break;
626 case D_COMP_TRINARY_ARG2:
627 printf ("trinary operator arguments 1\n");
628 break;
629 case D_COMP_LITERAL:
630 printf ("literal\n");
631 break;
eb383413
L
632 }
633
d00edca5
DD
634 d_dump (d_left (dc), indent + 2);
635 d_dump (d_right (dc), indent + 2);
636}
637
638#endif /* CP_DEMANGLE_DEBUG */
639
640/* Add a new component. */
641
642static struct d_comp *
643d_make_empty (di, type)
644 struct d_info *di;
645 enum d_comp_type type;
646{
647 struct d_comp *p;
648
649 if (di->next_comp >= di->num_comps)
650 return NULL;
651 p = &di->comps[di->next_comp];
652 p->type = type;
653 ++di->next_comp;
654 return p;
655}
656
657/* Add a new generic component. */
658
659static struct d_comp *
660d_make_comp (di, type, left, right)
661 struct d_info *di;
662 enum d_comp_type type;
663 struct d_comp *left;
664 struct d_comp *right;
665{
666 struct d_comp *p;
667
668 /* We check for errors here. A typical error would be a NULL return
331c3da2
DD
669 from a subroutine. We catch those here, and return NULL
670 upward. */
d00edca5
DD
671 switch (type)
672 {
673 /* These types require two parameters. */
674 case D_COMP_QUAL_NAME:
675 case D_COMP_TYPED_NAME:
676 case D_COMP_TEMPLATE:
677 case D_COMP_VENDOR_TYPE_QUAL:
678 case D_COMP_PTRMEM_TYPE:
679 case D_COMP_UNARY:
680 case D_COMP_BINARY:
681 case D_COMP_BINARY_ARGS:
682 case D_COMP_TRINARY:
683 case D_COMP_TRINARY_ARG1:
684 case D_COMP_TRINARY_ARG2:
685 case D_COMP_LITERAL:
686 if (left == NULL || right == NULL)
687 return NULL;
688 break;
689
690 /* These types only require one parameter. */
691 case D_COMP_VTABLE:
692 case D_COMP_VTT:
693 case D_COMP_CONSTRUCTION_VTABLE:
694 case D_COMP_TYPEINFO:
695 case D_COMP_TYPEINFO_NAME:
696 case D_COMP_TYPEINFO_FN:
697 case D_COMP_THUNK:
698 case D_COMP_VIRTUAL_THUNK:
699 case D_COMP_COVARIANT_THUNK:
700 case D_COMP_JAVA_CLASS:
701 case D_COMP_GUARD:
702 case D_COMP_REFTEMP:
703 case D_COMP_POINTER:
704 case D_COMP_REFERENCE:
705 case D_COMP_COMPLEX:
706 case D_COMP_IMAGINARY:
707 case D_COMP_VENDOR_TYPE:
708 case D_COMP_ARGLIST:
709 case D_COMP_TEMPLATE_ARGLIST:
710 case D_COMP_CAST:
711 if (left == NULL)
712 return NULL;
713 break;
714
715 /* This needs a right parameter, but the left parameter can be
716 empty. */
717 case D_COMP_ARRAY_TYPE:
718 if (right == NULL)
719 return NULL;
720 break;
721
722 /* These are allowed to have no parameters--in some cases they
723 will be filled in later. */
724 case D_COMP_FUNCTION_TYPE:
725 case D_COMP_RESTRICT:
726 case D_COMP_VOLATILE:
727 case D_COMP_CONST:
728 break;
729
730 /* Other types should not be seen here. */
731 default:
732 return NULL;
eb383413 733 }
d00edca5
DD
734
735 p = d_make_empty (di, type);
736 if (p != NULL)
eb383413 737 {
d00edca5
DD
738 p->u.s_binary.left = left;
739 p->u.s_binary.right = right;
eb383413 740 }
d00edca5
DD
741 return p;
742}
eb383413 743
d00edca5 744/* Add a new name component. */
03d5f569 745
d00edca5
DD
746static struct d_comp *
747d_make_name (di, s, len)
748 struct d_info *di;
749 const char *s;
750 int len;
751{
752 struct d_comp *p;
03d5f569 753
d00edca5
DD
754 p = d_make_empty (di, D_COMP_NAME);
755 if (p != NULL)
756 {
757 p->u.s_name.s = s;
758 p->u.s_name.len = len;
eb383413 759 }
d00edca5 760 return p;
eb383413
L
761}
762
d00edca5 763/* Add a new builtin type component. */
eb383413 764
d00edca5
DD
765static struct d_comp *
766d_make_builtin_type (di, type)
767 struct d_info *di;
768 const struct d_builtin_type_info *type;
eb383413 769{
d00edca5
DD
770 struct d_comp *p;
771
331c3da2
DD
772 if (type == NULL)
773 return NULL;
d00edca5
DD
774 p = d_make_empty (di, D_COMP_BUILTIN_TYPE);
775 if (p != NULL)
776 p->u.s_builtin.type = type;
777 return p;
778}
eb383413 779
d00edca5 780/* Add a new operator component. */
eb383413 781
d00edca5
DD
782static struct d_comp *
783d_make_operator (di, op)
784 struct d_info *di;
785 const struct d_operator_info *op;
eb383413 786{
d00edca5
DD
787 struct d_comp *p;
788
789 p = d_make_empty (di, D_COMP_OPERATOR);
790 if (p != NULL)
791 p->u.s_operator.op = op;
792 return p;
eb383413
L
793}
794
d00edca5 795/* Add a new extended operator component. */
eb383413 796
d00edca5
DD
797static struct d_comp *
798d_make_extended_operator (di, args, name)
799 struct d_info *di;
800 int args;
801 struct d_comp *name;
eb383413 802{
d00edca5 803 struct d_comp *p;
03d5f569 804
331c3da2
DD
805 if (name == NULL)
806 return NULL;
d00edca5
DD
807 p = d_make_empty (di, D_COMP_EXTENDED_OPERATOR);
808 if (p != NULL)
809 {
810 p->u.s_extended_operator.args = args;
811 p->u.s_extended_operator.name = name;
812 }
813 return p;
eb383413
L
814}
815
d00edca5 816/* Add a new constructor component. */
eb383413 817
d00edca5
DD
818static struct d_comp *
819d_make_ctor (di, kind, name)
820 struct d_info *di;
821 enum gnu_v3_ctor_kinds kind;
822 struct d_comp *name;
eb383413 823{
d00edca5
DD
824 struct d_comp *p;
825
331c3da2
DD
826 if (name == NULL)
827 return NULL;
d00edca5
DD
828 p = d_make_empty (di, D_COMP_CTOR);
829 if (p != NULL)
830 {
831 p->u.s_ctor.kind = kind;
832 p->u.s_ctor.name = name;
833 }
834 return p;
eb383413
L
835}
836
d00edca5 837/* Add a new destructor component. */
eb383413 838
d00edca5
DD
839static struct d_comp *
840d_make_dtor (di, kind, name)
841 struct d_info *di;
842 enum gnu_v3_dtor_kinds kind;
843 struct d_comp *name;
eb383413 844{
d00edca5
DD
845 struct d_comp *p;
846
331c3da2
DD
847 if (name == NULL)
848 return NULL;
d00edca5
DD
849 p = d_make_empty (di, D_COMP_DTOR);
850 if (p != NULL)
851 {
852 p->u.s_dtor.kind = kind;
853 p->u.s_dtor.name = name;
854 }
855 return p;
eb383413
L
856}
857
d00edca5 858/* Add a new template parameter. */
59666b35 859
d00edca5
DD
860static struct d_comp *
861d_make_template_param (di, i)
862 struct d_info *di;
863 long i;
59666b35 864{
d00edca5
DD
865 struct d_comp *p;
866
867 p = d_make_empty (di, D_COMP_TEMPLATE_PARAM);
868 if (p != NULL)
869 p->u.s_number.number = i;
870 return p;
59666b35
DD
871}
872
d00edca5 873/* Add a new standard substitution component. */
59666b35 874
d00edca5
DD
875static struct d_comp *
876d_make_sub (di, name)
877 struct d_info *di;
878 const char *name;
59666b35 879{
d00edca5
DD
880 struct d_comp *p;
881
882 p = d_make_empty (di, D_COMP_SUB_STD);
883 if (p != NULL)
884 p->u.s_string.string = name;
885 return p;
59666b35
DD
886}
887
331c3da2
DD
888/* <mangled-name> ::= _Z <encoding>
889
890 TOP_LEVEL is non-zero when called at the top level. */
59666b35 891
d00edca5 892static struct d_comp *
331c3da2 893d_mangled_name (di, top_level)
d00edca5 894 struct d_info *di;
331c3da2 895 int top_level;
59666b35 896{
d00edca5
DD
897 if (d_next_char (di) != '_')
898 return NULL;
899 if (d_next_char (di) != 'Z')
900 return NULL;
331c3da2 901 return d_encoding (di, top_level);
59666b35
DD
902}
903
d00edca5
DD
904/* Return whether a function should have a return type. The argument
905 is the function name, which may be qualified in various ways. The
906 rules are that template functions have return types with some
907 exceptions, function types which are not part of a function name
908 mangling have return types with some exceptions, and non-template
909 function names do not have return types. The exceptions are that
910 constructors, destructors, and conversion operators do not have
911 return types. */
59666b35
DD
912
913static int
d00edca5
DD
914has_return_type (dc)
915 struct d_comp *dc;
59666b35 916{
d00edca5
DD
917 if (dc == NULL)
918 return 0;
919 switch (dc->type)
920 {
921 default:
922 return 0;
923 case D_COMP_TEMPLATE:
924 return ! is_ctor_dtor_or_conversion (d_left (dc));
54a962d9
DD
925 case D_COMP_RESTRICT:
926 case D_COMP_VOLATILE:
927 case D_COMP_CONST:
928 case D_COMP_VENDOR_TYPE_QUAL:
929 return has_return_type (d_left (dc));
d00edca5 930 }
59666b35
DD
931}
932
d00edca5
DD
933/* Return whether a name is a constructor, a destructor, or a
934 conversion operator. */
eb383413
L
935
936static int
d00edca5
DD
937is_ctor_dtor_or_conversion (dc)
938 struct d_comp *dc;
eb383413 939{
d00edca5
DD
940 if (dc == NULL)
941 return 0;
942 switch (dc->type)
943 {
944 default:
945 return 0;
946 case D_COMP_QUAL_NAME:
947 return is_ctor_dtor_or_conversion (d_right (dc));
948 case D_COMP_CTOR:
949 case D_COMP_DTOR:
950 case D_COMP_CAST:
951 return 1;
952 }
eb383413
L
953}
954
d00edca5
DD
955/* <encoding> ::= <(function) name> <bare-function-type>
956 ::= <(data) name>
6d95373e
DD
957 ::= <special-name>
958
959 TOP_LEVEL is non-zero when called at the top level, in which case
960 if DMGL_PARAMS is not set we do not demangle the function
961 parameters. We only set this at the top level, because otherwise
962 we would not correctly demangle names in local scopes. */
eb383413 963
d00edca5 964static struct d_comp *
6d95373e 965d_encoding (di, top_level)
d00edca5 966 struct d_info *di;
6d95373e 967 int top_level;
eb383413 968{
d00edca5 969 char peek = d_peek_char (di);
03d5f569 970
d00edca5
DD
971 if (peek == 'G' || peek == 'T')
972 return d_special_name (di);
973 else
03d5f569 974 {
d00edca5
DD
975 struct d_comp *dc;
976
977 dc = d_name (di);
331c3da2
DD
978
979 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
980 {
981 /* Strip off any initial CV-qualifiers, as they really apply
982 to the `this' parameter, and they were not output by the
983 v2 demangler without DMGL_PARAMS. */
984 while (dc->type == D_COMP_RESTRICT
985 || dc->type == D_COMP_VOLATILE
986 || dc->type == D_COMP_CONST)
987 dc = d_left (dc);
988 return dc;
989 }
990
d00edca5 991 peek = d_peek_char (di);
331c3da2 992 if (peek == '\0' || peek == 'E')
d00edca5
DD
993 return dc;
994 return d_make_comp (di, D_COMP_TYPED_NAME, dc,
995 d_bare_function_type (di, has_return_type (dc)));
03d5f569 996 }
d00edca5
DD
997}
998
999/* <name> ::= <nested-name>
1000 ::= <unscoped-name>
1001 ::= <unscoped-template-name> <template-args>
1002 ::= <local-name>
1003
1004 <unscoped-name> ::= <unqualified-name>
1005 ::= St <unqualified-name>
eb383413 1006
d00edca5
DD
1007 <unscoped-template-name> ::= <unscoped-name>
1008 ::= <substitution>
1009*/
1010
1011static struct d_comp *
1012d_name (di)
1013 struct d_info *di;
1014{
1015 char peek = d_peek_char (di);
1016 struct d_comp *dc;
1017
1018 switch (peek)
eb383413 1019 {
d00edca5
DD
1020 case 'N':
1021 return d_nested_name (di);
1022
1023 case 'Z':
1024 return d_local_name (di);
1025
1026 case 'S':
1027 {
1028 int subst;
1029
1030 if (d_peek_next_char (di) != 't')
1031 {
1032 dc = d_substitution (di);
1033 subst = 1;
1034 }
1035 else
1036 {
1037 d_advance (di, 2);
1038 dc = d_make_comp (di, D_COMP_QUAL_NAME, d_make_name (di, "std", 3),
1039 d_unqualified_name (di));
1040 subst = 0;
1041 }
1042
1043 if (d_peek_char (di) != 'I')
1044 {
1045 /* The grammar does not permit this case to occur if we
1046 called d_substitution() above (i.e., subst == 1). We
1047 don't bother to check. */
1048 }
1049 else
1050 {
1051 /* This is <template-args>, which means that we just saw
1052 <unscoped-template-name>, which is a substitution
1053 candidate if we didn't just get it from a
1054 substitution. */
1055 if (! subst)
1056 {
1057 if (! d_add_substitution (di, dc))
1058 return NULL;
1059 }
1060 dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1061 }
1062
1063 return dc;
1064 }
1065
1066 default:
1067 dc = d_unqualified_name (di);
1068 if (d_peek_char (di) == 'I')
03d5f569 1069 {
d00edca5
DD
1070 /* This is <template-args>, which means that we just saw
1071 <unscoped-template-name>, which is a substitution
1072 candidate. */
1073 if (! d_add_substitution (di, dc))
1074 return NULL;
1075 dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
03d5f569 1076 }
d00edca5 1077 return dc;
eb383413 1078 }
d00edca5 1079}
eb383413 1080
d00edca5
DD
1081/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1082 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1083*/
eb383413 1084
d00edca5
DD
1085static struct d_comp *
1086d_nested_name (di)
1087 struct d_info *di;
1088{
1089 struct d_comp *ret;
1090 struct d_comp **pret;
03d5f569 1091
d00edca5
DD
1092 if (d_next_char (di) != 'N')
1093 return NULL;
eb383413 1094
d00edca5
DD
1095 pret = d_cv_qualifiers (di, &ret);
1096 if (pret == NULL)
1097 return NULL;
1098
1099 *pret = d_prefix (di);
1100 if (*pret == NULL)
1101 return NULL;
eb383413 1102
d00edca5 1103 if (d_next_char (di) != 'E')
eb383413
L
1104 return NULL;
1105
d00edca5 1106 return ret;
eb383413
L
1107}
1108
d00edca5
DD
1109/* <prefix> ::= <prefix> <unqualified-name>
1110 ::= <template-prefix> <template-args>
1111 ::= <template-param>
1112 ::=
1113 ::= <substitution>
eb383413 1114
d00edca5
DD
1115 <template-prefix> ::= <prefix> <(template) unqualified-name>
1116 ::= <template-param>
1117 ::= <substitution>
1118*/
1119
1120static struct d_comp *
1121d_prefix (di)
1122 struct d_info *di;
eb383413 1123{
d00edca5 1124 struct d_comp *ret = NULL;
eb383413 1125
d00edca5 1126 while (1)
eb383413 1127 {
d00edca5
DD
1128 char peek;
1129 enum d_comp_type comb_type;
1130 struct d_comp *dc;
1131
1132 peek = d_peek_char (di);
1133 if (peek == '\0')
1134 return NULL;
1135
1136 /* The older code accepts a <local-name> here, but I don't see
1137 that in the grammar. The older code does not accept a
1138 <template-param> here. */
eb383413 1139
d00edca5
DD
1140 comb_type = D_COMP_QUAL_NAME;
1141 if (IS_DIGIT (peek)
1142 || (peek >= 'a' && peek <= 'z')
1143 || peek == 'C'
1144 || peek == 'D')
1145 dc = d_unqualified_name (di);
1146 else if (peek == 'S')
1147 dc = d_substitution (di);
1148 else if (peek == 'I')
1149 {
1150 if (ret == NULL)
1151 return NULL;
1152 comb_type = D_COMP_TEMPLATE;
1153 dc = d_template_args (di);
1154 }
1155 else if (peek == 'T')
1156 dc = d_template_param (di);
1157 else if (peek == 'E')
1158 return ret;
1159 else
1160 return NULL;
1161
1162 if (ret == NULL)
1163 ret = dc;
eb383413 1164 else
d00edca5
DD
1165 ret = d_make_comp (di, comb_type, ret, dc);
1166
1167 if (peek != 'S' && d_peek_char (di) != 'E')
1168 {
1169 if (! d_add_substitution (di, ret))
1170 return NULL;
1171 }
eb383413
L
1172 }
1173}
1174
d00edca5
DD
1175/* <unqualified-name> ::= <operator-name>
1176 ::= <ctor-dtor-name>
1177 ::= <source-name>
1178*/
eb383413 1179
d00edca5
DD
1180static struct d_comp *
1181d_unqualified_name (di)
1182 struct d_info *di;
eb383413 1183{
d00edca5
DD
1184 char peek;
1185
1186 peek = d_peek_char (di);
1187 if (IS_DIGIT (peek))
1188 return d_source_name (di);
1189 else if (peek >= 'a' && peek <= 'z')
1190 return d_operator_name (di);
1191 else if (peek == 'C' || peek == 'D')
1192 return d_ctor_dtor_name (di);
1193 else
03d5f569 1194 return NULL;
eb383413
L
1195}
1196
d00edca5 1197/* <source-name> ::= <(positive length) number> <identifier> */
eb383413 1198
d00edca5
DD
1199static struct d_comp *
1200d_source_name (di)
1201 struct d_info *di;
eb383413 1202{
d00edca5
DD
1203 long len;
1204 struct d_comp *ret;
1205
1206 len = d_number (di);
1207 if (len <= 0)
1208 return NULL;
1209 ret = d_identifier (di, len);
1210 di->last_name = ret;
1211 return ret;
eb383413
L
1212}
1213
d00edca5 1214/* number ::= [n] <(non-negative decimal integer)> */
eb383413 1215
d00edca5
DD
1216static long
1217d_number (di)
1218 struct d_info *di;
eb383413 1219{
d00edca5
DD
1220 int sign;
1221 char peek;
1222 long ret;
eb383413 1223
d00edca5
DD
1224 sign = 1;
1225 peek = d_peek_char (di);
1226 if (peek == 'n')
1227 {
1228 sign = -1;
1229 d_advance (di, 1);
1230 peek = d_peek_char (di);
1231 }
eb383413 1232
d00edca5
DD
1233 ret = 0;
1234 while (1)
eb383413 1235 {
d00edca5
DD
1236 if (! IS_DIGIT (peek))
1237 return ret * sign;
1238 ret = ret * 10 + peek - '0';
1239 d_advance (di, 1);
1240 peek = d_peek_char (di);
eb383413 1241 }
eb383413
L
1242}
1243
d00edca5 1244/* identifier ::= <(unqualified source code identifier)> */
eb383413 1245
d00edca5
DD
1246static struct d_comp *
1247d_identifier (di, len)
1248 struct d_info *di;
1249 int len;
eb383413 1250{
d00edca5 1251 const char *name;
eb383413 1252
d00edca5
DD
1253 name = d_str (di);
1254 d_advance (di, len);
eb383413 1255
d00edca5
DD
1256 /* Look for something which looks like a gcc encoding of an
1257 anonymous namespace, and replace it with a more user friendly
1258 name. */
1259 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1260 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1261 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
eb383413 1262 {
d00edca5
DD
1263 const char *s;
1264
1265 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1266 if ((*s == '.' || *s == '_' || *s == '$')
1267 && s[1] == 'N')
1268 return d_make_name (di, "(anonymous namespace)",
1269 sizeof "(anonymous namespace)" - 1);
eb383413 1270 }
d00edca5
DD
1271
1272 return d_make_name (di, name, len);
eb383413
L
1273}
1274
d00edca5
DD
1275/* operator_name ::= many different two character encodings.
1276 ::= cv <type>
1277 ::= v <digit> <source-name>
1278*/
eb383413 1279
d00edca5
DD
1280static const struct d_operator_info d_operators[] =
1281{
1282 { "aN", "&=", 2 },
1283 { "aS", "=", 2 },
1284 { "aa", "&&", 2 },
1285 { "ad", "&", 1 },
1286 { "an", "&", 2 },
1287 { "cl", "()", 0 },
1288 { "cm", ",", 2 },
1289 { "co", "~", 1 },
1290 { "dV", "/=", 2 },
1291 { "da", "delete[]", 1 },
1292 { "de", "*", 1 },
1293 { "dl", "delete", 1 },
1294 { "dv", "/", 2 },
1295 { "eO", "^=", 2 },
1296 { "eo", "^", 2 },
1297 { "eq", "==", 2 },
1298 { "ge", ">=", 2 },
1299 { "gt", ">", 2 },
1300 { "ix", "[]", 2 },
1301 { "lS", "<<=", 2 },
1302 { "le", "<=", 2 },
1303 { "ls", "<<", 2 },
1304 { "lt", "<", 2 },
1305 { "mI", "-=", 2 },
1306 { "mL", "*=", 2 },
1307 { "mi", "-", 2 },
1308 { "ml", "*", 2 },
1309 { "mm", "--", 1 },
1310 { "na", "new[]", 1 },
1311 { "ne", "!=", 2 },
1312 { "ng", "-", 1 },
1313 { "nt", "!", 1 },
1314 { "nw", "new", 1 },
1315 { "oR", "|=", 2 },
1316 { "oo", "||", 2 },
1317 { "or", "|", 2 },
1318 { "pL", "+=", 2 },
1319 { "pl", "+", 2 },
1320 { "pm", "->*", 2 },
1321 { "pp", "++", 1 },
1322 { "ps", "+", 1 },
1323 { "pt", "->", 2 },
1324 { "qu", "?", 3 },
1325 { "rM", "%=", 2 },
1326 { "rS", ">>=", 2 },
1327 { "rm", "%", 2 },
1328 { "rs", ">>", 2 },
1329 { "st", "sizeof ", 1 },
1330 { "sz", "sizeof ", 1 }
1331};
eb383413 1332
d00edca5
DD
1333static struct d_comp *
1334d_operator_name (di)
1335 struct d_info *di;
eb383413 1336{
d00edca5
DD
1337 char c1;
1338 char c2;
eb383413 1339
d00edca5
DD
1340 c1 = d_next_char (di);
1341 c2 = d_next_char (di);
1342 if (c1 == 'v' && IS_DIGIT (c2))
1343 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1344 else if (c1 == 'c' && c2 == 'v')
1345 return d_make_comp (di, D_COMP_CAST, d_type (di), NULL);
1346 else
eb383413 1347 {
d00edca5
DD
1348 int low = 0;
1349 int high = sizeof (d_operators) / sizeof (d_operators[0]);
eb383413 1350
d00edca5
DD
1351 while (1)
1352 {
1353 int i;
1354 const struct d_operator_info *p;
eb383413 1355
d00edca5
DD
1356 i = low + (high - low) / 2;
1357 p = d_operators + i;
eb383413 1358
d00edca5
DD
1359 if (c1 == p->code[0] && c2 == p->code[1])
1360 return d_make_operator (di, p);
1361
1362 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1363 high = i;
1364 else
1365 low = i + 1;
1366 if (low == high)
1367 return NULL;
1368 }
1369 }
eb383413
L
1370}
1371
d00edca5
DD
1372/* <special-name> ::= TV <type>
1373 ::= TT <type>
1374 ::= TI <type>
1375 ::= TS <type>
1376 ::= GV <(object) name>
1377 ::= T <call-offset> <(base) encoding>
1378 ::= Tc <call-offset> <call-offset> <(base) encoding>
1379 Also g++ extensions:
1380 ::= TC <type> <(offset) number> _ <(base) type>
1381 ::= TF <type>
1382 ::= TJ <type>
1383 ::= GR <name>
1384*/
eb383413 1385
d00edca5
DD
1386static struct d_comp *
1387d_special_name (di)
1388 struct d_info *di;
eb383413 1389{
d00edca5 1390 char c;
eb383413 1391
d00edca5
DD
1392 c = d_next_char (di);
1393 if (c == 'T')
03d5f569 1394 {
d00edca5
DD
1395 switch (d_next_char (di))
1396 {
1397 case 'V':
1398 return d_make_comp (di, D_COMP_VTABLE, d_type (di), NULL);
1399 case 'T':
1400 return d_make_comp (di, D_COMP_VTT, d_type (di), NULL);
1401 case 'I':
1402 return d_make_comp (di, D_COMP_TYPEINFO, d_type (di), NULL);
1403 case 'S':
1404 return d_make_comp (di, D_COMP_TYPEINFO_NAME, d_type (di), NULL);
eb383413 1405
d00edca5
DD
1406 case 'h':
1407 if (! d_call_offset (di, 'h'))
1408 return NULL;
6d95373e 1409 return d_make_comp (di, D_COMP_THUNK, d_encoding (di, 0), NULL);
eb383413 1410
d00edca5
DD
1411 case 'v':
1412 if (! d_call_offset (di, 'v'))
1413 return NULL;
6d95373e 1414 return d_make_comp (di, D_COMP_VIRTUAL_THUNK, d_encoding (di, 0),
d00edca5 1415 NULL);
eb383413 1416
d00edca5
DD
1417 case 'c':
1418 if (! d_call_offset (di, '\0'))
1419 return NULL;
1420 if (! d_call_offset (di, '\0'))
1421 return NULL;
6d95373e 1422 return d_make_comp (di, D_COMP_COVARIANT_THUNK, d_encoding (di, 0),
d00edca5 1423 NULL);
eb383413 1424
d00edca5
DD
1425 case 'C':
1426 {
1427 struct d_comp *derived_type;
1428 long offset;
1429 struct d_comp *base_type;
1430
1431 derived_type = d_type (di);
1432 offset = d_number (di);
1433 if (offset < 0)
1434 return NULL;
1435 if (d_next_char (di) != '_')
1436 return NULL;
1437 base_type = d_type (di);
1438 /* We don't display the offset. FIXME: We should display
1439 it in verbose mode. */
1440 return d_make_comp (di, D_COMP_CONSTRUCTION_VTABLE, base_type,
1441 derived_type);
1442 }
eb383413 1443
d00edca5
DD
1444 case 'F':
1445 return d_make_comp (di, D_COMP_TYPEINFO_FN, d_type (di), NULL);
1446 case 'J':
1447 return d_make_comp (di, D_COMP_JAVA_CLASS, d_type (di), NULL);
eb383413 1448
d00edca5
DD
1449 default:
1450 return NULL;
1451 }
eb383413 1452 }
d00edca5 1453 else if (c == 'G')
eb383413 1454 {
d00edca5
DD
1455 switch (d_next_char (di))
1456 {
1457 case 'V':
1458 return d_make_comp (di, D_COMP_GUARD, d_name (di), NULL);
1459
1460 case 'R':
1461 return d_make_comp (di, D_COMP_REFTEMP, d_name (di), NULL);
1462
1463 default:
1464 return NULL;
1465 }
eb383413 1466 }
d00edca5
DD
1467 else
1468 return NULL;
eb383413
L
1469}
1470
d00edca5
DD
1471/* <call-offset> ::= h <nv-offset> _
1472 ::= v <v-offset> _
eb383413 1473
d00edca5 1474 <nv-offset> ::= <(offset) number>
eb383413 1475
d00edca5 1476 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
eb383413 1477
d00edca5
DD
1478 The C parameter, if not '\0', is a character we just read which is
1479 the start of the <call-offset>.
eb383413 1480
d00edca5
DD
1481 We don't display the offset information anywhere. FIXME: We should
1482 display it in verbose mode. */
eb383413 1483
d00edca5
DD
1484static int
1485d_call_offset (di, c)
1486 struct d_info *di;
1487 int c;
eb383413 1488{
d00edca5
DD
1489 long offset;
1490 long virtual_offset;
eb383413 1491
d00edca5
DD
1492 if (c == '\0')
1493 c = d_next_char (di);
eb383413 1494
d00edca5
DD
1495 if (c == 'h')
1496 offset = d_number (di);
1497 else if (c == 'v')
eb383413 1498 {
d00edca5
DD
1499 offset = d_number (di);
1500 if (d_next_char (di) != '_')
1501 return 0;
1502 virtual_offset = d_number (di);
eb383413 1503 }
d00edca5
DD
1504 else
1505 return 0;
eb383413 1506
d00edca5
DD
1507 if (d_next_char (di) != '_')
1508 return 0;
eb383413 1509
d00edca5 1510 return 1;
eb383413
L
1511}
1512
d00edca5
DD
1513/* <ctor-dtor-name> ::= C1
1514 ::= C2
1515 ::= C3
1516 ::= D0
1517 ::= D1
1518 ::= D2
1519*/
1520
1521static struct d_comp *
1522d_ctor_dtor_name (di)
1523 struct d_info *di;
1524{
1525 switch (d_next_char (di))
1526 {
1527 case 'C':
1528 {
1529 enum gnu_v3_ctor_kinds kind;
1530
1531 switch (d_next_char (di))
1532 {
1533 case '1':
1534 kind = gnu_v3_complete_object_ctor;
1535 break;
1536 case '2':
1537 kind = gnu_v3_base_object_ctor;
1538 break;
1539 case '3':
1540 kind = gnu_v3_complete_object_allocating_ctor;
1541 break;
1542 default:
1543 return NULL;
1544 }
1545 return d_make_ctor (di, kind, di->last_name);
1546 }
1547
1548 case 'D':
1549 {
1550 enum gnu_v3_dtor_kinds kind;
1551
1552 switch (d_next_char (di))
1553 {
1554 case '0':
1555 kind = gnu_v3_deleting_dtor;
1556 break;
1557 case '1':
1558 kind = gnu_v3_complete_object_dtor;
1559 break;
1560 case '2':
1561 kind = gnu_v3_base_object_dtor;
1562 break;
1563 default:
1564 return NULL;
1565 }
1566 return d_make_dtor (di, kind, di->last_name);
1567 }
eb383413 1568
d00edca5
DD
1569 default:
1570 return NULL;
1571 }
1572}
eb383413 1573
d00edca5
DD
1574/* <type> ::= <builtin-type>
1575 ::= <function-type>
1576 ::= <class-enum-type>
1577 ::= <array-type>
1578 ::= <pointer-to-member-type>
1579 ::= <template-param>
1580 ::= <template-template-param> <template-args>
1581 ::= <substitution>
1582 ::= <CV-qualifiers> <type>
1583 ::= P <type>
1584 ::= R <type>
1585 ::= C <type>
1586 ::= G <type>
1587 ::= U <source-name> <type>
1588
1589 <builtin-type> ::= various one letter codes
1590 ::= u <source-name>
1591*/
eb383413 1592
d00edca5
DD
1593static const struct d_builtin_type_info d_builtin_types[26] =
1594{
1595 /* a */ { "signed char", "signed char", D_PRINT_INT },
1596 /* b */ { "bool", "boolean", D_PRINT_BOOL },
1597 /* c */ { "char", "byte", D_PRINT_INT },
1598 /* d */ { "double", "double", D_PRINT_DEFAULT },
1599 /* e */ { "long double", "long double", D_PRINT_DEFAULT },
1600 /* f */ { "float", "float", D_PRINT_DEFAULT },
1601 /* g */ { "__float128", "__float128", D_PRINT_DEFAULT },
1602 /* h */ { "unsigned char", "unsigned char", D_PRINT_INT },
1603 /* i */ { "int", "int", D_PRINT_INT },
1604 /* j */ { "unsigned int", "unsigned", D_PRINT_INT },
1605 /* k */ { NULL, NULL, D_PRINT_DEFAULT },
1606 /* l */ { "long", "long", D_PRINT_LONG },
1607 /* m */ { "unsigned long", "unsigned long", D_PRINT_LONG },
1608 /* n */ { "__int128", "__int128", D_PRINT_DEFAULT },
1609 /* o */ { "unsigned __int128", "unsigned __int128", D_PRINT_DEFAULT },
1610 /* p */ { NULL, NULL, D_PRINT_DEFAULT },
1611 /* q */ { NULL, NULL, D_PRINT_DEFAULT },
1612 /* r */ { NULL, NULL, D_PRINT_DEFAULT },
1613 /* s */ { "short", "short", D_PRINT_INT },
1614 /* t */ { "unsigned short", "unsigned short", D_PRINT_INT },
1615 /* u */ { NULL, NULL, D_PRINT_DEFAULT },
1616 /* v */ { "void", "void", D_PRINT_VOID },
1617 /* w */ { "wchar_t", "char", D_PRINT_INT },
1618 /* x */ { "long long", "long", D_PRINT_DEFAULT },
1619 /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT },
1620 /* z */ { "...", "...", D_PRINT_DEFAULT },
1621};
eb383413 1622
d00edca5
DD
1623static struct d_comp *
1624d_type (di)
1625 struct d_info *di;
eb383413 1626{
d00edca5
DD
1627 char peek;
1628 struct d_comp *ret;
1629 int can_subst;
1630
1631 /* The ABI specifies that when CV-qualifiers are used, the base type
1632 is substitutable, and the fully qualified type is substitutable,
1633 but the base type with a strict subset of the CV-qualifiers is
1634 not substitutable. The natural recursive implementation of the
1635 CV-qualifiers would cause subsets to be substitutable, so instead
1636 we pull them all off now.
1637
331c3da2
DD
1638 FIXME: The ABI says that order-insensitive vendor qualifiers
1639 should be handled in the same way, but we have no way to tell
1640 which vendor qualifiers are order-insensitive and which are
1641 order-sensitive. So we just assume that they are all
1642 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1643 __vector, and it treats it as order-sensitive when mangling
1644 names. */
d00edca5
DD
1645
1646 peek = d_peek_char (di);
1647 if (peek == 'r' || peek == 'V' || peek == 'K')
1648 {
1649 struct d_comp **pret;
74bcd529 1650
d00edca5 1651 pret = d_cv_qualifiers (di, &ret);
331c3da2
DD
1652 if (pret == NULL)
1653 return NULL;
d00edca5
DD
1654 *pret = d_type (di);
1655 if (! d_add_substitution (di, ret))
1656 return NULL;
1657 return ret;
1658 }
eb383413 1659
d00edca5 1660 can_subst = 1;
eb383413 1661
74bcd529 1662 switch (peek)
eb383413 1663 {
d00edca5
DD
1664 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1665 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1666 case 'o': case 's': case 't':
1667 case 'v': case 'w': case 'x': case 'y': case 'z':
d00edca5
DD
1668 ret = d_make_builtin_type (di, &d_builtin_types[peek - 'a']);
1669 can_subst = 0;
1670 d_advance (di, 1);
1671 break;
1672
1673 case 'u':
1674 d_advance (di, 1);
1675 ret = d_make_comp (di, D_COMP_VENDOR_TYPE, d_source_name (di), NULL);
1676 break;
1677
1678 case 'F':
1679 ret = d_function_type (di);
eb383413
L
1680 break;
1681
d00edca5
DD
1682 case '0': case '1': case '2': case '3': case '4':
1683 case '5': case '6': case '7': case '8': case '9':
1684 case 'N':
eb383413 1685 case 'Z':
d00edca5 1686 ret = d_class_enum_type (di);
eb383413
L
1687 break;
1688
d00edca5
DD
1689 case 'A':
1690 ret = d_array_type (di);
1691 break;
1692
1693 case 'M':
1694 ret = d_pointer_to_member_type (di);
1695 break;
1696
1697 case 'T':
1698 ret = d_template_param (di);
1699 if (d_peek_char (di) == 'I')
03d5f569 1700 {
d00edca5
DD
1701 /* This is <template-template-param> <template-args>. The
1702 <template-template-param> part is a substitution
1703 candidate. */
1704 if (! d_add_substitution (di, ret))
1705 return NULL;
1706 ret = d_make_comp (di, D_COMP_TEMPLATE, ret, d_template_args (di));
03d5f569 1707 }
d00edca5
DD
1708 break;
1709
1710 case 'S':
1711 /* If this is a special substitution, then it is the start of
1712 <class-enum-type>. */
1713 {
1714 char peek_next;
74bcd529 1715
d00edca5
DD
1716 peek_next = d_peek_next_char (di);
1717 if (IS_DIGIT (peek_next)
1718 || peek_next == '_'
1719 || (peek_next >= 'A' && peek_next <= 'Z'))
1720 {
1721 ret = d_substitution (di);
1722 /* The substituted name may have been a template name and
1723 may be followed by tepmlate args. */
1724 if (d_peek_char (di) == 'I')
1725 ret = d_make_comp (di, D_COMP_TEMPLATE, ret,
1726 d_template_args (di));
1727 else
1728 can_subst = 0;
1729 }
1730 else
1731 {
1732 ret = d_class_enum_type (di);
1733 /* If the substitution was a complete type, then it is not
1734 a new substitution candidate. However, if the
1735 substitution was followed by template arguments, then
1736 the whole thing is a substitution candidate. */
331c3da2 1737 if (ret != NULL && ret->type == D_COMP_SUB_STD)
d00edca5
DD
1738 can_subst = 0;
1739 }
1740 }
eb383413
L
1741 break;
1742
d00edca5
DD
1743 case 'P':
1744 d_advance (di, 1);
1745 ret = d_make_comp (di, D_COMP_POINTER, d_type (di), NULL);
1746 break;
eb383413 1747
d00edca5
DD
1748 case 'R':
1749 d_advance (di, 1);
1750 ret = d_make_comp (di, D_COMP_REFERENCE, d_type (di), NULL);
1751 break;
eb383413 1752
d00edca5
DD
1753 case 'C':
1754 d_advance (di, 1);
1755 ret = d_make_comp (di, D_COMP_COMPLEX, d_type (di), NULL);
1756 break;
1757
1758 case 'G':
1759 d_advance (di, 1);
1760 ret = d_make_comp (di, D_COMP_IMAGINARY, d_type (di), NULL);
1761 break;
eb383413 1762
d00edca5
DD
1763 case 'U':
1764 d_advance (di, 1);
1765 ret = d_source_name (di);
1766 ret = d_make_comp (di, D_COMP_VENDOR_TYPE_QUAL, d_type (di), ret);
eb383413 1767 break;
d00edca5
DD
1768
1769 default:
1770 return NULL;
eb383413
L
1771 }
1772
d00edca5
DD
1773 if (can_subst)
1774 {
1775 if (! d_add_substitution (di, ret))
1776 return NULL;
1777 }
eb383413 1778
d00edca5
DD
1779 return ret;
1780}
eb383413 1781
d00edca5 1782/* <CV-qualifiers> ::= [r] [V] [K] */
eb383413 1783
d00edca5
DD
1784static struct d_comp **
1785d_cv_qualifiers (di, pret)
1786 struct d_info *di;
1787 struct d_comp **pret;
eb383413
L
1788{
1789 char peek;
1790
d00edca5
DD
1791 peek = d_peek_char (di);
1792 while (peek == 'r' || peek == 'V' || peek == 'K')
eb383413 1793 {
d00edca5 1794 enum d_comp_type t;
59666b35 1795
d00edca5
DD
1796 d_advance (di, 1);
1797 if (peek == 'r')
1798 t = D_COMP_RESTRICT;
1799 else if (peek == 'V')
1800 t = D_COMP_VOLATILE;
1801 else
1802 t = D_COMP_CONST;
eb383413 1803
d00edca5
DD
1804 *pret = d_make_comp (di, t, NULL, NULL);
1805 if (*pret == NULL)
1806 return NULL;
1807 pret = &d_left (*pret);
eb383413 1808
d00edca5
DD
1809 peek = d_peek_char (di);
1810 }
eb383413 1811
d00edca5
DD
1812 return pret;
1813}
eb383413 1814
d00edca5 1815/* <function-type> ::= F [Y] <bare-function-type> E */
eb383413 1816
d00edca5
DD
1817static struct d_comp *
1818d_function_type (di)
1819 struct d_info *di;
eb383413 1820{
d00edca5 1821 struct d_comp *ret;
eb383413 1822
d00edca5
DD
1823 if (d_next_char (di) != 'F')
1824 return NULL;
1825 if (d_peek_char (di) == 'Y')
1826 {
1827 /* Function has C linkage. We don't print this information.
1828 FIXME: We should print it in verbose mode. */
1829 d_advance (di, 1);
1830 }
1831 ret = d_bare_function_type (di, 1);
1832 if (d_next_char (di) != 'E')
1833 return NULL;
1834 return ret;
1835}
74bcd529 1836
d00edca5 1837/* <bare-function-type> ::= <type>+ */
eb383413 1838
d00edca5
DD
1839static struct d_comp *
1840d_bare_function_type (di, has_return_type)
1841 struct d_info *di;
1842 int has_return_type;
1843{
1844 struct d_comp *return_type;
1845 struct d_comp *tl;
1846 struct d_comp **ptl;
eb383413 1847
d00edca5
DD
1848 return_type = NULL;
1849 tl = NULL;
1850 ptl = &tl;
eb383413
L
1851 while (1)
1852 {
1853 char peek;
d00edca5 1854 struct d_comp *type;
eb383413 1855
d00edca5
DD
1856 peek = d_peek_char (di);
1857 if (peek == '\0' || peek == 'E')
1858 break;
1859 type = d_type (di);
1860 if (type == NULL)
1861 return NULL;
1862 if (has_return_type)
eb383413 1863 {
d00edca5
DD
1864 return_type = type;
1865 has_return_type = 0;
eb383413 1866 }
d00edca5 1867 else
eb383413 1868 {
d00edca5 1869 *ptl = d_make_comp (di, D_COMP_ARGLIST, type, NULL);
331c3da2
DD
1870 if (*ptl == NULL)
1871 return NULL;
d00edca5 1872 ptl = &d_right (*ptl);
eb383413 1873 }
eb383413 1874 }
eb383413 1875
d00edca5
DD
1876 /* There should be at least one parameter type besides the optional
1877 return type. A function which takes no arguments will have a
1878 single parameter type void. */
1879 if (tl == NULL)
1880 return NULL;
eb383413 1881
d00edca5
DD
1882 /* If we have a single parameter type void, omit it. */
1883 if (d_right (tl) == NULL
1884 && d_left (tl)->type == D_COMP_BUILTIN_TYPE
1885 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
1886 tl = NULL;
eb383413 1887
d00edca5
DD
1888 return d_make_comp (di, D_COMP_FUNCTION_TYPE, return_type, tl);
1889}
eb383413 1890
d00edca5 1891/* <class-enum-type> ::= <name> */
eb383413 1892
d00edca5
DD
1893static struct d_comp *
1894d_class_enum_type (di)
1895 struct d_info *di;
1896{
1897 return d_name (di);
1898}
74bcd529 1899
d00edca5
DD
1900/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1901 ::= A [<(dimension) expression>] _ <(element) type>
1902*/
74bcd529 1903
d00edca5
DD
1904static struct d_comp *
1905d_array_type (di)
1906 struct d_info *di;
1907{
1908 char peek;
1909 struct d_comp *dim;
74bcd529 1910
d00edca5
DD
1911 if (d_next_char (di) != 'A')
1912 return NULL;
1913
1914 peek = d_peek_char (di);
1915 if (peek == '_')
1916 dim = NULL;
1917 else if (IS_DIGIT (peek))
74bcd529 1918 {
d00edca5 1919 const char *s;
74bcd529 1920
d00edca5
DD
1921 s = d_str (di);
1922 do
1923 {
1924 d_advance (di, 1);
1925 peek = d_peek_char (di);
1926 }
1927 while (IS_DIGIT (peek));
1928 dim = d_make_name (di, s, d_str (di) - s);
331c3da2
DD
1929 if (dim == NULL)
1930 return NULL;
74bcd529 1931 }
eb383413 1932 else
d00edca5
DD
1933 {
1934 dim = d_expression (di);
1935 if (dim == NULL)
1936 return NULL;
1937 }
eb383413 1938
d00edca5
DD
1939 if (d_next_char (di) != '_')
1940 return NULL;
eb383413 1941
d00edca5
DD
1942 return d_make_comp (di, D_COMP_ARRAY_TYPE, dim, d_type (di));
1943}
eb383413 1944
d00edca5 1945/* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
eb383413 1946
d00edca5
DD
1947static struct d_comp *
1948d_pointer_to_member_type (di)
1949 struct d_info *di;
eb383413 1950{
d00edca5
DD
1951 struct d_comp *cl;
1952 struct d_comp *mem;
1953 struct d_comp **pmem;
eb383413 1954
d00edca5
DD
1955 if (d_next_char (di) != 'M')
1956 return NULL;
eb383413 1957
d00edca5 1958 cl = d_type (di);
eb383413 1959
d00edca5
DD
1960 /* The ABI specifies that any type can be a substitution source, and
1961 that M is followed by two types, and that when a CV-qualified
1962 type is seen both the base type and the CV-qualified types are
1963 substitution sources. The ABI also specifies that for a pointer
1964 to a CV-qualified member function, the qualifiers are attached to
1965 the second type. Given the grammar, a plain reading of the ABI
1966 suggests that both the CV-qualified member function and the
1967 non-qualified member function are substitution sources. However,
1968 g++ does not work that way. g++ treats only the CV-qualified
1969 member function as a substitution source. FIXME. So to work
1970 with g++, we need to pull off the CV-qualifiers here, in order to
1971 avoid calling add_substitution() in d_type(). */
eb383413 1972
d00edca5 1973 pmem = d_cv_qualifiers (di, &mem);
331c3da2
DD
1974 if (pmem == NULL)
1975 return NULL;
d00edca5 1976 *pmem = d_type (di);
eb383413 1977
d00edca5 1978 return d_make_comp (di, D_COMP_PTRMEM_TYPE, cl, mem);
eb383413
L
1979}
1980
d00edca5
DD
1981/* <template-param> ::= T_
1982 ::= T <(parameter-2 non-negative) number> _
1983*/
eb383413 1984
d00edca5
DD
1985static struct d_comp *
1986d_template_param (di)
1987 struct d_info *di;
eb383413 1988{
d00edca5 1989 long param;
eb383413 1990
d00edca5
DD
1991 if (d_next_char (di) != 'T')
1992 return NULL;
eb383413 1993
d00edca5
DD
1994 if (d_peek_char (di) == '_')
1995 param = 0;
1996 else
1997 {
1998 param = d_number (di);
1999 if (param < 0)
2000 return NULL;
2001 param += 1;
2002 }
03d5f569 2003
d00edca5
DD
2004 if (d_next_char (di) != '_')
2005 return NULL;
eb383413 2006
d00edca5 2007 return d_make_template_param (di, param);
eb383413
L
2008}
2009
d00edca5
DD
2010/* <template-args> ::= I <template-arg>+ E */
2011
2012static struct d_comp *
2013d_template_args (di)
2014 struct d_info *di;
eb383413 2015{
d00edca5
DD
2016 struct d_comp *hold_last_name;
2017 struct d_comp *al;
2018 struct d_comp **pal;
eb383413 2019
d00edca5
DD
2020 /* Preserve the last name we saw--don't let the template arguments
2021 clobber it, as that would give us the wrong name for a subsequent
2022 constructor or destructor. */
2023 hold_last_name = di->last_name;
eb383413 2024
d00edca5
DD
2025 if (d_next_char (di) != 'I')
2026 return NULL;
eb383413 2027
d00edca5
DD
2028 al = NULL;
2029 pal = &al;
eb383413
L
2030 while (1)
2031 {
d00edca5
DD
2032 struct d_comp *a;
2033
2034 a = d_template_arg (di);
2035 if (a == NULL)
2036 return NULL;
2037
2038 *pal = d_make_comp (di, D_COMP_TEMPLATE_ARGLIST, a, NULL);
331c3da2
DD
2039 if (*pal == NULL)
2040 return NULL;
d00edca5
DD
2041 pal = &d_right (*pal);
2042
2043 if (d_peek_char (di) == 'E')
03d5f569 2044 {
d00edca5
DD
2045 d_advance (di, 1);
2046 break;
03d5f569 2047 }
eb383413
L
2048 }
2049
d00edca5
DD
2050 di->last_name = hold_last_name;
2051
2052 return al;
eb383413
L
2053}
2054
d00edca5
DD
2055/* <template-arg> ::= <type>
2056 ::= X <expression> E
2057 ::= <expr-primary>
2058*/
eb383413 2059
d00edca5
DD
2060static struct d_comp *
2061d_template_arg (di)
2062 struct d_info *di;
eb383413 2063{
d00edca5 2064 struct d_comp *ret;
03d5f569 2065
d00edca5 2066 switch (d_peek_char (di))
eb383413 2067 {
d00edca5
DD
2068 case 'X':
2069 d_advance (di, 1);
2070 ret = d_expression (di);
2071 if (d_next_char (di) != 'E')
2072 return NULL;
2073 return ret;
b851d07b 2074
d00edca5
DD
2075 case 'L':
2076 return d_expr_primary (di);
eb383413 2077
d00edca5
DD
2078 default:
2079 return d_type (di);
74bcd529 2080 }
eb383413
L
2081}
2082
d00edca5
DD
2083/* <expression> ::= <(unary) operator-name> <expression>
2084 ::= <(binary) operator-name> <expression> <expression>
2085 ::= <(trinary) operator-name> <expression> <expression> <expression>
2086 ::= st <type>
2087 ::= <template-param>
2088 ::= sr <type> <unqualified-name>
2089 ::= sr <type> <unqualified-name> <template-args>
2090 ::= <expr-primary>
2091*/
2092
2093static struct d_comp *
2094d_expression (di)
2095 struct d_info *di;
eb383413 2096{
d00edca5 2097 char peek;
eb383413 2098
d00edca5
DD
2099 peek = d_peek_char (di);
2100 if (peek == 'L')
2101 return d_expr_primary (di);
2102 else if (peek == 'T')
2103 return d_template_param (di);
2104 else if (peek == 's' && d_peek_next_char (di) == 'r')
eb383413 2105 {
d00edca5
DD
2106 struct d_comp *type;
2107 struct d_comp *name;
eb383413 2108
d00edca5
DD
2109 d_advance (di, 2);
2110 type = d_type (di);
2111 name = d_unqualified_name (di);
2112 if (d_peek_char (di) != 'I')
2113 return d_make_comp (di, D_COMP_QUAL_NAME, type, name);
2114 else
2115 return d_make_comp (di, D_COMP_QUAL_NAME, type,
2116 d_make_comp (di, D_COMP_TEMPLATE, name,
2117 d_template_args (di)));
793011ca 2118 }
d00edca5 2119 else
eb383413 2120 {
d00edca5
DD
2121 struct d_comp *op;
2122 int args;
eb383413 2123
d00edca5
DD
2124 op = d_operator_name (di);
2125 if (op == NULL)
2126 return NULL;
eb383413 2127
d00edca5
DD
2128 if (op->type == D_COMP_OPERATOR
2129 && strcmp (op->u.s_operator.op->code, "st") == 0)
2130 return d_make_comp (di, D_COMP_UNARY, op, d_type (di));
eb383413 2131
d00edca5
DD
2132 switch (op->type)
2133 {
2134 default:
2135 return NULL;
2136 case D_COMP_OPERATOR:
2137 args = op->u.s_operator.op->args;
2138 break;
2139 case D_COMP_EXTENDED_OPERATOR:
2140 args = op->u.s_extended_operator.args;
2141 break;
2142 case D_COMP_CAST:
2143 args = 1;
2144 break;
2145 }
2146
2147 switch (args)
2148 {
2149 case 1:
2150 return d_make_comp (di, D_COMP_UNARY, op, d_expression (di));
2151 case 2:
2152 {
2153 struct d_comp *left;
2154
2155 left = d_expression (di);
2156 return d_make_comp (di, D_COMP_BINARY, op,
2157 d_make_comp (di, D_COMP_BINARY_ARGS, left,
2158 d_expression (di)));
2159 }
2160 case 3:
2161 {
2162 struct d_comp *first;
2163 struct d_comp *second;
2164
2165 first = d_expression (di);
2166 second = d_expression (di);
2167 return d_make_comp (di, D_COMP_TRINARY, op,
2168 d_make_comp (di, D_COMP_TRINARY_ARG1, first,
2169 d_make_comp (di,
2170 D_COMP_TRINARY_ARG2,
2171 second,
2172 d_expression (di))));
2173 }
2174 default:
2175 return NULL;
2176 }
eb383413
L
2177 }
2178}
2179
d00edca5
DD
2180/* <expr-primary> ::= L <type> <(value) number> E
2181 ::= L <type> <(value) float> E
2182 ::= L <mangled-name> E
2183*/
74bcd529 2184
d00edca5
DD
2185static struct d_comp *
2186d_expr_primary (di)
2187 struct d_info *di;
74bcd529 2188{
d00edca5 2189 struct d_comp *ret;
74bcd529 2190
d00edca5
DD
2191 if (d_next_char (di) != 'L')
2192 return NULL;
2193 if (d_peek_char (di) == '_')
331c3da2 2194 ret = d_mangled_name (di, 0);
d00edca5 2195 else
74bcd529 2196 {
d00edca5
DD
2197 struct d_comp *type;
2198 const char *s;
2199
2200 type = d_type (di);
2201
2202 /* Rather than try to interpret the literal value, we just
2203 collect it as a string. Note that it's possible to have a
2204 floating point literal here. The ABI specifies that the
2205 format of such literals is machine independent. That's fine,
2206 but what's not fine is that versions of g++ up to 3.2 with
2207 -fabi-version=1 used upper case letters in the hex constant,
2208 and dumped out gcc's internal representation. That makes it
2209 hard to tell where the constant ends, and hard to dump the
2210 constant in any readable form anyhow. We don't attempt to
2211 handle these cases. */
2212
2213 s = d_str (di);
2214 while (d_peek_char (di) != 'E')
2215 d_advance (di, 1);
2216 ret = d_make_comp (di, D_COMP_LITERAL, type,
2217 d_make_name (di, s, d_str (di) - s));
2218 }
2219 if (d_next_char (di) != 'E')
2220 return NULL;
2221 return ret;
74bcd529
DD
2222}
2223
d00edca5
DD
2224/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2225 ::= Z <(function) encoding> E s [<discriminator>]
2226*/
74bcd529 2227
d00edca5
DD
2228static struct d_comp *
2229d_local_name (di)
2230 struct d_info *di;
74bcd529 2231{
d00edca5 2232 struct d_comp *function;
74bcd529 2233
d00edca5
DD
2234 if (d_next_char (di) != 'Z')
2235 return NULL;
74bcd529 2236
6d95373e 2237 function = d_encoding (di, 0);
74bcd529 2238
d00edca5
DD
2239 if (d_next_char (di) != 'E')
2240 return NULL;
74bcd529 2241
d00edca5 2242 if (d_peek_char (di) == 's')
74bcd529 2243 {
d00edca5
DD
2244 d_advance (di, 1);
2245 if (! d_discriminator (di))
2246 return NULL;
2247 return d_make_comp (di, D_COMP_QUAL_NAME, function,
2248 d_make_name (di, "string literal",
2249 sizeof "string literal" - 1));
74bcd529 2250 }
d00edca5 2251 else
74bcd529 2252 {
d00edca5 2253 struct d_comp *name;
74bcd529 2254
d00edca5
DD
2255 name = d_name (di);
2256 if (! d_discriminator (di))
2257 return NULL;
2258 return d_make_comp (di, D_COMP_QUAL_NAME, function, name);
74bcd529 2259 }
74bcd529
DD
2260}
2261
d00edca5 2262/* <discriminator> ::= _ <(non-negative) number>
eb383413 2263
d00edca5
DD
2264 We demangle the discriminator, but we don't print it out. FIXME:
2265 We should print it out in verbose mode. */
74bcd529 2266
d00edca5
DD
2267static int
2268d_discriminator (di)
2269 struct d_info *di;
2270{
2271 long discrim;
74bcd529 2272
d00edca5
DD
2273 if (d_peek_char (di) != '_')
2274 return 1;
2275 d_advance (di, 1);
2276 discrim = d_number (di);
2277 if (discrim < 0)
2278 return 0;
2279 return 1;
2280}
eb383413 2281
d00edca5 2282/* Add a new substitution. */
eb383413 2283
d00edca5
DD
2284static int
2285d_add_substitution (di, dc)
2286 struct d_info *di;
2287 struct d_comp *dc;
eb383413 2288{
331c3da2
DD
2289 if (dc == NULL)
2290 return 0;
d00edca5
DD
2291 if (di->next_sub >= di->num_subs)
2292 return 0;
2293 di->subs[di->next_sub] = dc;
2294 ++di->next_sub;
2295 return 1;
2296}
2297
2298/* <substitution> ::= S <seq-id> _
2299 ::= S_
2300 ::= St
2301 ::= Sa
2302 ::= Sb
2303 ::= Ss
2304 ::= Si
2305 ::= So
2306 ::= Sd
2307*/
eb383413 2308
d00edca5
DD
2309static struct d_comp *
2310d_substitution (di)
2311 struct d_info *di;
2312{
2313 char c;
eb383413 2314
d00edca5
DD
2315 if (d_next_char (di) != 'S')
2316 return NULL;
e7e9b069 2317
d00edca5
DD
2318 c = d_next_char (di);
2319 if (c == '_' || IS_DIGIT (c) || (c >= 'A' && c <= 'Z'))
eb383413 2320 {
d00edca5 2321 int id;
eb383413 2322
d00edca5
DD
2323 id = 0;
2324 if (c != '_')
eb383413 2325 {
d00edca5 2326 do
eb383413 2327 {
d00edca5
DD
2328 if (IS_DIGIT (c))
2329 id = id * 36 + c - '0';
2330 else if (c >= 'A' && c <= 'Z')
2331 id = id * 36 + c - 'A' + 10;
2332 else
2333 return NULL;
2334 c = d_next_char (di);
eb383413 2335 }
d00edca5 2336 while (c != '_');
eb383413 2337
d00edca5 2338 ++id;
eb383413 2339 }
eb383413 2340
d00edca5
DD
2341 if (id >= di->next_sub)
2342 return NULL;
eb383413 2343
d00edca5 2344 return di->subs[id];
eb383413 2345 }
d00edca5 2346 else
eb383413 2347 {
d00edca5 2348 switch (c)
e61231f1 2349 {
d00edca5
DD
2350 case 't':
2351 return d_make_sub (di, "std");
2352 case 'a':
2353 di->last_name = d_make_sub (di, "allocator");
2354 return d_make_sub (di, "std::allocator");
2355 case 'b':
2356 di->last_name = d_make_sub (di, "basic_string");
2357 return d_make_sub (di, "std::basic_string");
2358 case 's':
2359 di->last_name = d_make_sub (di, "string");
2360 return d_make_sub (di, "std::string");
2361 case 'i':
2362 di->last_name = d_make_sub (di, "istream");
2363 return d_make_sub (di, "std::istream");
2364 case 'o':
2365 di->last_name = d_make_sub (di, "ostream");
2366 return d_make_sub (di, "std::ostream");
2367 case 'd':
2368 di->last_name = d_make_sub (di, "iostream");
2369 return d_make_sub (di, "std::iostream");
2370 default:
2371 return NULL;
eb383413
L
2372 }
2373 }
eb383413
L
2374}
2375
d00edca5 2376/* Resize the print buffer. */
eb383413 2377
d00edca5
DD
2378static void
2379d_print_resize (dpi, add)
2380 struct d_print_info *dpi;
2381 size_t add;
2382{
2383 size_t need;
eb383413 2384
331c3da2
DD
2385 if (dpi->buf == NULL)
2386 return;
d00edca5
DD
2387 need = dpi->len + add;
2388 while (need > dpi->alc)
eb383413 2389 {
d00edca5
DD
2390 size_t newalc;
2391 char *newbuf;
59666b35 2392
d00edca5
DD
2393 newalc = dpi->alc * 2;
2394 newbuf = realloc (dpi->buf, newalc);
2395 if (newbuf == NULL)
0976f6a7 2396 {
d00edca5
DD
2397 free (dpi->buf);
2398 dpi->buf = NULL;
2399 dpi->allocation_failure = 1;
2400 return;
eb383413 2401 }
d00edca5
DD
2402 dpi->buf = newbuf;
2403 dpi->alc = newalc;
eb383413 2404 }
d00edca5 2405}
0976f6a7 2406
d00edca5 2407/* Append a character to the print buffer. */
0976f6a7 2408
d00edca5
DD
2409static void
2410d_print_append_char (dpi, c)
2411 struct d_print_info *dpi;
2412 int c;
2413{
2414 if (dpi->buf != NULL)
2415 {
2416 if (dpi->len >= dpi->alc)
2417 {
2418 d_print_resize (dpi, 1);
2419 if (dpi->buf == NULL)
2420 return;
2421 }
0976f6a7 2422
d00edca5
DD
2423 dpi->buf[dpi->len] = c;
2424 ++dpi->len;
74bcd529 2425 }
eb383413
L
2426}
2427
d00edca5
DD
2428/* Append a buffer to the print buffer. */
2429
2430static void
2431d_print_append_buffer (dpi, s, l)
2432 struct d_print_info *dpi;
2433 const char *s;
2434 size_t l;
eb383413 2435{
d00edca5 2436 if (dpi->buf != NULL)
eb383413 2437 {
d00edca5 2438 if (dpi->len + l > dpi->alc)
eb383413 2439 {
d00edca5
DD
2440 d_print_resize (dpi, l);
2441 if (dpi->buf == NULL)
2442 return;
eb383413 2443 }
eb383413 2444
d00edca5
DD
2445 memcpy (dpi->buf + dpi->len, s, l);
2446 dpi->len += l;
2447 }
eb383413
L
2448}
2449
d00edca5 2450/* Indicate that an error occurred during printing. */
eb383413 2451
d00edca5
DD
2452static void
2453d_print_error (dpi)
2454 struct d_print_info *dpi;
bc9bf259 2455{
d00edca5
DD
2456 free (dpi->buf);
2457 dpi->buf = NULL;
2458}
bc9bf259 2459
d00edca5
DD
2460/* Turn components into a human readable string. Returns a string
2461 allocated by malloc, or NULL on error. On success, this sets *PALC
2462 to the size of the allocated buffer. On failure, this sets *PALC
2463 to 0 for a bad parse, or to 1 for a memory allocation failure. */
eb383413 2464
d00edca5
DD
2465static char *
2466d_print (options, dc, palc)
2467 int options;
2468 const struct d_comp *dc;
2469 size_t *palc;
2470{
2471 struct d_print_info dpi;
eb383413 2472
d00edca5 2473 dpi.options = options;
eb383413 2474
d00edca5
DD
2475 dpi.alc = 64;
2476 dpi.buf = malloc (dpi.alc);
2477 if (dpi.buf == NULL)
eb383413 2478 {
d00edca5
DD
2479 *palc = 1;
2480 return NULL;
eb383413 2481 }
eb383413 2482
d00edca5
DD
2483 dpi.len = 0;
2484 dpi.templates = NULL;
2485 dpi.modifiers = NULL;
eb383413 2486
d00edca5 2487 dpi.allocation_failure = 0;
eb383413 2488
d00edca5 2489 d_print_comp (&dpi, dc);
eb383413 2490
d00edca5 2491 d_append_char (&dpi, '\0');
eb383413 2492
d00edca5
DD
2493 if (dpi.buf != NULL)
2494 *palc = dpi.alc;
2495 else
2496 *palc = dpi.allocation_failure;
eb383413 2497
d00edca5 2498 return dpi.buf;
eb383413
L
2499}
2500
d00edca5 2501/* Subroutine to handle components. */
eb383413 2502
d00edca5
DD
2503static void
2504d_print_comp (dpi, dc)
2505 struct d_print_info *dpi;
2506 const struct d_comp *dc;
eb383413 2507{
d00edca5 2508 if (dc == NULL)
eb383413 2509 {
d00edca5
DD
2510 d_print_error (dpi);
2511 return;
eb383413 2512 }
d00edca5
DD
2513 if (d_print_saw_error (dpi))
2514 return;
eb383413 2515
d00edca5 2516 switch (dc->type)
eb383413 2517 {
d00edca5
DD
2518 case D_COMP_NAME:
2519 d_print_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2520 return;
eb383413 2521
d00edca5
DD
2522 case D_COMP_QUAL_NAME:
2523 d_print_comp (dpi, d_left (dc));
2524 d_append_string (dpi, (dpi->options & DMGL_JAVA) == 0 ? "::" : ".");
2525 d_print_comp (dpi, d_right (dc));
2526 return;
eb383413 2527
d00edca5
DD
2528 case D_COMP_TYPED_NAME:
2529 {
2530 const struct d_comp *typed_name;
2531 struct d_print_mod dpm;
2532 struct d_print_template dpt;
2533
2534 /* Pass the name down to the type so that it can be printed in
2535 the right place for the type. If the name has
2536 CV-qualifiers, they are really method qualifiers; pull them
2537 off now and print them after everything else. Note that we
2538 don't handle D_COMP_VENDOR_TYPE_QUAL here; it's not
2539 accepted by d_cv_qualifiers() either. */
2540 typed_name = d_left (dc);
2541 while (typed_name != NULL
2542 && (typed_name->type == D_COMP_RESTRICT
2543 || typed_name->type == D_COMP_VOLATILE
2544 || typed_name->type == D_COMP_CONST))
2545 typed_name = d_left (typed_name);
2546
2547 dpm.next = dpi->modifiers;
2548 dpi->modifiers = &dpm;
2549 dpm.mod = typed_name;
2550 dpm.printed = 0;
331c3da2 2551 dpm.templates = dpi->templates;
d00edca5
DD
2552
2553 /* If typed_name is a template, then it applies to the
2554 function type as well. */
2555 if (typed_name->type == D_COMP_TEMPLATE)
2556 {
2557 dpt.next = dpi->templates;
2558 dpi->templates = &dpt;
2559 dpt.template = typed_name;
2560 }
eb383413 2561
d00edca5 2562 d_print_comp (dpi, d_right (dc));
74bcd529 2563
d00edca5
DD
2564 if (typed_name->type == D_COMP_TEMPLATE)
2565 dpi->templates = dpt.next;
eb383413 2566
d00edca5
DD
2567 /* If the modifier didn't get printed by the type, print it
2568 now. */
2569 if (! dpm.printed)
2570 {
2571 d_append_char (dpi, ' ');
2572 d_print_comp (dpi, typed_name);
2573 }
eb383413 2574
d00edca5 2575 dpi->modifiers = dpm.next;
eb383413 2576
d00edca5
DD
2577 /* Now print any CV-qualifiers on the type. */
2578 typed_name = d_left (dc);
2579 while (typed_name != NULL
2580 && (typed_name->type == D_COMP_RESTRICT
2581 || typed_name->type == D_COMP_VOLATILE
2582 || typed_name->type == D_COMP_CONST))
2583 {
2584 d_print_mod (dpi, typed_name);
2585 typed_name = d_left (typed_name);
2586 }
eb383413 2587
d00edca5
DD
2588 return;
2589 }
eb383413 2590
d00edca5 2591 case D_COMP_TEMPLATE:
331c3da2
DD
2592 {
2593 struct d_print_mod *hold_dpm;
2594
2595 /* Don't push modifiers into a template definition. Doing so
2596 could give the wrong definition for a template argument.
2597 Instead, treat the template essentially as a name. */
2598
2599 hold_dpm = dpi->modifiers;
2600 dpi->modifiers = NULL;
2601
2602 d_print_comp (dpi, d_left (dc));
2603 d_append_char (dpi, '<');
2604 d_print_comp (dpi, d_right (dc));
2605 /* Avoid generating two consecutive '>' characters, to avoid
2606 the C++ syntactic ambiguity. */
2607 if (dpi->buf != NULL && dpi->buf[dpi->len - 1] == '>')
2608 d_append_char (dpi, ' ');
2609 d_append_char (dpi, '>');
2610
2611 dpi->modifiers = hold_dpm;
2612
2613 return;
2614 }
d00edca5
DD
2615
2616 case D_COMP_TEMPLATE_PARAM:
2617 {
2618 long i;
2619 struct d_comp *a;
2620 struct d_print_template *hold_dpt;
eb383413 2621
d00edca5
DD
2622 if (dpi->templates == NULL)
2623 {
2624 d_print_error (dpi);
2625 return;
2626 }
2627 i = dc->u.s_number.number;
2628 for (a = d_right (dpi->templates->template);
2629 a != NULL;
2630 a = d_right (a))
2631 {
2632 if (a->type != D_COMP_TEMPLATE_ARGLIST)
2633 {
2634 d_print_error (dpi);
2635 return;
2636 }
2637 if (i <= 0)
2638 break;
2639 --i;
2640 }
2641 if (i != 0 || a == NULL)
2642 {
2643 d_print_error (dpi);
2644 return;
2645 }
59666b35 2646
d00edca5
DD
2647 /* While processing this parameter, we need to pop the list of
2648 templates. This is because the template parameter may
2649 itself be a reference to a parameter of an outer
2650 template. */
59666b35 2651
d00edca5
DD
2652 hold_dpt = dpi->templates;
2653 dpi->templates = hold_dpt->next;
eb383413 2654
d00edca5 2655 d_print_comp (dpi, d_left (a));
03d5f569 2656
d00edca5 2657 dpi->templates = hold_dpt;
59666b35 2658
d00edca5
DD
2659 return;
2660 }
eb383413 2661
d00edca5
DD
2662 case D_COMP_CTOR:
2663 d_print_comp (dpi, dc->u.s_ctor.name);
2664 return;
2665
2666 case D_COMP_DTOR:
2667 d_append_char (dpi, '~');
2668 d_print_comp (dpi, dc->u.s_dtor.name);
2669 return;
2670
2671 case D_COMP_VTABLE:
2672 d_append_string (dpi, "vtable for ");
2673 d_print_comp (dpi, d_left (dc));
2674 return;
2675
2676 case D_COMP_VTT:
2677 d_append_string (dpi, "VTT for ");
2678 d_print_comp (dpi, d_left (dc));
2679 return;
2680
2681 case D_COMP_CONSTRUCTION_VTABLE:
2682 d_append_string (dpi, "construction vtable for ");
2683 d_print_comp (dpi, d_left (dc));
2684 d_append_string (dpi, "-in-");
2685 d_print_comp (dpi, d_right (dc));
2686 return;
2687
2688 case D_COMP_TYPEINFO:
2689 d_append_string (dpi, "typeinfo for ");
2690 d_print_comp (dpi, d_left (dc));
2691 return;
2692
2693 case D_COMP_TYPEINFO_NAME:
2694 d_append_string (dpi, "typeinfo name for ");
2695 d_print_comp (dpi, d_left (dc));
2696 return;
2697
2698 case D_COMP_TYPEINFO_FN:
2699 d_append_string (dpi, "typeinfo fn for ");
2700 d_print_comp (dpi, d_left (dc));
2701 return;
2702
2703 case D_COMP_THUNK:
2704 d_append_string (dpi, "non-virtual thunk to ");
2705 d_print_comp (dpi, d_left (dc));
2706 return;
2707
2708 case D_COMP_VIRTUAL_THUNK:
2709 d_append_string (dpi, "virtual thunk to ");
2710 d_print_comp (dpi, d_left (dc));
2711 return;
2712
2713 case D_COMP_COVARIANT_THUNK:
2714 d_append_string (dpi, "covariant return thunk to ");
2715 d_print_comp (dpi, d_left (dc));
2716 return;
2717
2718 case D_COMP_JAVA_CLASS:
2719 d_append_string (dpi, "java Class for ");
2720 d_print_comp (dpi, d_left (dc));
2721 return;
2722
2723 case D_COMP_GUARD:
2724 d_append_string (dpi, "guard variable for ");
2725 d_print_comp (dpi, d_left (dc));
2726 return;
2727
2728 case D_COMP_REFTEMP:
2729 d_append_string (dpi, "reference temporary for ");
2730 d_print_comp (dpi, d_left (dc));
2731 return;
2732
2733 case D_COMP_SUB_STD:
2734 d_append_string (dpi, dc->u.s_string.string);
2735 return;
2736
2737 case D_COMP_RESTRICT:
2738 case D_COMP_VOLATILE:
2739 case D_COMP_CONST:
2740 case D_COMP_VENDOR_TYPE_QUAL:
2741 case D_COMP_POINTER:
2742 case D_COMP_REFERENCE:
2743 case D_COMP_COMPLEX:
2744 case D_COMP_IMAGINARY:
2745 {
2746 /* We keep a list of modifiers on the stack. */
2747 struct d_print_mod dpm;
eb383413 2748
d00edca5
DD
2749 dpm.next = dpi->modifiers;
2750 dpi->modifiers = &dpm;
2751 dpm.mod = dc;
2752 dpm.printed = 0;
331c3da2 2753 dpm.templates = dpi->templates;
eb383413 2754
d00edca5 2755 d_print_comp (dpi, d_left (dc));
59666b35 2756
d00edca5
DD
2757 /* If the modifier didn't get printed by the type, print it
2758 now. */
2759 if (! dpm.printed)
2760 d_print_mod (dpi, dc);
eb383413 2761
d00edca5 2762 dpi->modifiers = dpm.next;
eb383413 2763
d00edca5
DD
2764 return;
2765 }
eb383413 2766
d00edca5
DD
2767 case D_COMP_BUILTIN_TYPE:
2768 if ((dpi->options & DMGL_JAVA) == 0)
2769 d_append_string (dpi, dc->u.s_builtin.type->name);
2770 else
2771 d_append_string (dpi, dc->u.s_builtin.type->java_name);
2772 return;
eb383413 2773
d00edca5
DD
2774 case D_COMP_VENDOR_TYPE:
2775 d_print_comp (dpi, d_left (dc));
2776 return;
eb383413 2777
d00edca5
DD
2778 case D_COMP_FUNCTION_TYPE:
2779 {
2780 if (d_left (dc) != NULL)
2781 {
2782 struct d_print_mod dpm;
eb383413 2783
d00edca5
DD
2784 /* We must pass this type down as a modifier in order to
2785 print it in the right location. */
eb383413 2786
d00edca5
DD
2787 dpm.next = dpi->modifiers;
2788 dpi->modifiers = &dpm;
2789 dpm.mod = dc;
2790 dpm.printed = 0;
331c3da2 2791 dpm.templates = dpi->templates;
eb383413 2792
d00edca5 2793 d_print_comp (dpi, d_left (dc));
eb383413 2794
d00edca5 2795 dpi->modifiers = dpm.next;
eb383413 2796
d00edca5
DD
2797 if (dpm.printed)
2798 return;
eb383413 2799
d00edca5
DD
2800 d_append_char (dpi, ' ');
2801 }
eb383413 2802
d00edca5 2803 d_print_function_type (dpi, dc, dpi->modifiers);
03d5f569 2804
d00edca5
DD
2805 return;
2806 }
eb383413 2807
d00edca5
DD
2808 case D_COMP_ARRAY_TYPE:
2809 {
2810 struct d_print_mod dpm;
eb383413 2811
d00edca5
DD
2812 /* We must pass this type down as a modifier in order to print
2813 multi-dimensional arrays correctly. */
03d5f569 2814
d00edca5
DD
2815 dpm.next = dpi->modifiers;
2816 dpi->modifiers = &dpm;
2817 dpm.mod = dc;
2818 dpm.printed = 0;
331c3da2 2819 dpm.templates = dpi->templates;
eb383413 2820
d00edca5 2821 d_print_comp (dpi, d_right (dc));
eb383413 2822
d00edca5 2823 dpi->modifiers = dpm.next;
eb383413 2824
d00edca5
DD
2825 if (dpm.printed)
2826 return;
eb383413 2827
d00edca5 2828 d_print_array_type (dpi, dc, dpi->modifiers);
eb383413 2829
d00edca5
DD
2830 return;
2831 }
eb383413 2832
d00edca5
DD
2833 case D_COMP_PTRMEM_TYPE:
2834 {
2835 const struct d_comp *target_type;
2836 struct d_print_mod dpm;
2837
2838 /* Pass the name down to the type so that it can be printed in
2839 the right place for the type. If the type has
2840 CV-qualifiers, they are really method qualifiers; pull them
2841 off now and print them after everything else. */
2842 target_type = d_right (dc);
2843 while (target_type != NULL
2844 && (target_type->type == D_COMP_RESTRICT
2845 || target_type->type == D_COMP_VOLATILE
2846 || target_type->type == D_COMP_CONST))
2847 target_type = d_left (target_type);
2848
2849 dpm.next = dpi->modifiers;
2850 dpi->modifiers = &dpm;
2851 dpm.mod = dc;
2852 dpm.printed = 0;
331c3da2 2853 dpm.templates = dpi->templates;
d00edca5
DD
2854
2855 d_print_comp (dpi, target_type);
2856
2857 /* If the modifier didn't get printed by the type, print it
2858 now. */
2859 if (! dpm.printed)
2860 {
2861 d_append_char (dpi, ' ');
2862 d_print_comp (dpi, d_left (dc));
2863 d_append_string (dpi, "::*");
2864 }
eb383413 2865
d00edca5 2866 dpi->modifiers = dpm.next;
eb383413 2867
d00edca5
DD
2868 /* Now print any CV-qualifiers on the type. */
2869 target_type = d_right (dc);
2870 while (target_type != NULL
2871 && (target_type->type == D_COMP_RESTRICT
2872 || target_type->type == D_COMP_VOLATILE
2873 || target_type->type == D_COMP_CONST))
2874 {
2875 d_print_mod (dpi, target_type);
2876 target_type = d_left (target_type);
2877 }
eb383413 2878
d00edca5
DD
2879 return;
2880 }
eb383413 2881
d00edca5
DD
2882 case D_COMP_ARGLIST:
2883 case D_COMP_TEMPLATE_ARGLIST:
2884 d_print_comp (dpi, d_left (dc));
2885 if (d_right (dc) != NULL)
2886 {
2887 d_append_string (dpi, ", ");
2888 d_print_comp (dpi, d_right (dc));
2889 }
2890 return;
eb383413 2891
d00edca5
DD
2892 case D_COMP_OPERATOR:
2893 {
2894 char c;
2895
2896 d_append_string (dpi, "operator");
2897 c = dc->u.s_operator.op->name[0];
2898 if (c >= 'a' && c <= 'z')
2899 d_append_char (dpi, ' ');
2900 d_append_string (dpi, dc->u.s_operator.op->name);
2901 return;
2902 }
eb383413 2903
d00edca5
DD
2904 case D_COMP_EXTENDED_OPERATOR:
2905 d_append_string (dpi, "operator ");
2906 d_print_comp (dpi, dc->u.s_extended_operator.name);
2907 return;
eb383413 2908
d00edca5
DD
2909 case D_COMP_CAST:
2910 d_append_string (dpi, "operator ");
2911 d_print_cast (dpi, dc);
2912 return;
eb383413 2913
d00edca5
DD
2914 case D_COMP_UNARY:
2915 if (d_left (dc)->type != D_COMP_CAST)
2916 d_print_expr_op (dpi, d_left (dc));
2917 else
eb383413 2918 {
d00edca5
DD
2919 d_append_string (dpi, "((");
2920 d_print_cast (dpi, d_left (dc));
2921 d_append_char (dpi, ')');
eb383413 2922 }
d00edca5
DD
2923 d_append_char (dpi, '(');
2924 d_print_comp (dpi, d_right (dc));
2925 d_append_char (dpi, ')');
2926 if (d_left (dc)->type == D_COMP_CAST)
2927 d_append_char (dpi, ')');
2928 return;
2929
2930 case D_COMP_BINARY:
2931 if (d_right (dc)->type != D_COMP_BINARY_ARGS)
eb383413 2932 {
d00edca5
DD
2933 d_print_error (dpi);
2934 return;
eb383413 2935 }
d00edca5
DD
2936 d_append_char (dpi, '(');
2937 d_print_comp (dpi, d_left (d_right (dc)));
2938 d_append_string (dpi, ") ");
2939 d_print_expr_op (dpi, d_left (dc));
2940 d_append_string (dpi, " (");
2941 d_print_comp (dpi, d_right (d_right (dc)));
2942 d_append_char (dpi, ')');
2943 return;
2944
2945 case D_COMP_BINARY_ARGS:
2946 /* We should only see this as part of D_COMP_BINARY. */
2947 d_print_error (dpi);
2948 return;
2949
2950 case D_COMP_TRINARY:
2951 if (d_right (dc)->type != D_COMP_TRINARY_ARG1
2952 || d_right (d_right (dc))->type != D_COMP_TRINARY_ARG2)
2953 {
2954 d_print_error (dpi);
2955 return;
2956 }
2957 d_append_char (dpi, '(');
2958 d_print_comp (dpi, d_left (d_right (dc)));
2959 d_append_string (dpi, ") ");
2960 d_print_expr_op (dpi, d_left (dc));
2961 d_append_string (dpi, " (");
2962 d_print_comp (dpi, d_left (d_right (d_right (dc))));
2963 d_append_string (dpi, ") : (");
2964 d_print_comp (dpi, d_right (d_right (d_right (dc))));
2965 d_append_char (dpi, ')');
2966 return;
2967
2968 case D_COMP_TRINARY_ARG1:
2969 case D_COMP_TRINARY_ARG2:
2970 /* We should only see these are part of D_COMP_TRINARY. */
2971 d_print_error (dpi);
2972 return;
2973
2974 case D_COMP_LITERAL:
2975 /* For some builtin types, produce simpler output. */
2976 if (d_left (dc)->type == D_COMP_BUILTIN_TYPE)
2977 {
2978 switch (d_left (dc)->u.s_builtin.type->print)
2979 {
2980 case D_PRINT_INT:
2981 if (d_right (dc)->type == D_COMP_NAME)
2982 {
2983 d_print_comp (dpi, d_right (dc));
2984 return;
2985 }
2986 break;
2987
2988 case D_PRINT_LONG:
2989 if (d_right (dc)->type == D_COMP_NAME)
2990 {
2991 d_print_comp (dpi, d_right (dc));
2992 d_append_char (dpi, 'l');
2993 return;
2994 }
2995 break;
eb383413 2996
d00edca5
DD
2997 case D_PRINT_BOOL:
2998 if (d_right (dc)->type == D_COMP_NAME
2999 && d_right (dc)->u.s_name.len == 1)
3000 {
3001 switch (d_right (dc)->u.s_name.s[0])
3002 {
3003 case '0':
3004 d_append_string (dpi, "false");
3005 return;
3006 case '1':
3007 d_append_string (dpi, "true");
3008 return;
3009 default:
3010 break;
3011 }
3012 }
3013 break;
03d5f569 3014
d00edca5
DD
3015 default:
3016 break;
3017 }
3018 }
eb383413 3019
d00edca5
DD
3020 d_append_char (dpi, '(');
3021 d_print_comp (dpi, d_left (dc));
3022 d_append_char (dpi, ')');
3023 d_print_comp (dpi, d_right (dc));
3024 return;
eb383413 3025
d00edca5
DD
3026 default:
3027 d_print_error (dpi);
3028 return;
3029 }
eb383413
L
3030}
3031
d00edca5 3032/* Print an identifier. */
eb383413 3033
d00edca5
DD
3034static void
3035d_print_identifier (dpi, name, len)
3036 struct d_print_info *dpi;
3037 const char *name;
3038 int len;
eb383413 3039{
d00edca5
DD
3040 if ((dpi->options & DMGL_JAVA) == 0)
3041 d_append_buffer (dpi, name, len);
3042 else
eb383413 3043 {
d00edca5
DD
3044 const char *p;
3045 const char *end;
eb383413 3046
d00edca5
DD
3047 /* For Java we try to handle encoded extended Unicode
3048 characters. The C++ ABI doesn't mention Unicode encoding, so
3049 we don't it for C++. Characters are encoded as
3050 __U<hex-char>+_. */
3051 end = name + len;
3052 for (p = name; p < end; ++p)
eb383413 3053 {
d00edca5
DD
3054 if (end - p > 3
3055 && p[0] == '_'
3056 && p[1] == '_'
3057 && p[2] == 'U')
3058 {
3059 unsigned long c;
3060 const char *q;
eb383413 3061
d00edca5
DD
3062 c = 0;
3063 for (q = p + 3; q < end; ++q)
3064 {
3065 int dig;
3066
3067 if (*q >= '0' && *q <= '9')
3068 dig = *q - '0';
3069 else if (*q >= 'A' && *q <= 'F')
3070 dig = *q - 'A' + 10;
3071 else if (*q >= 'a' && *q <= 'f')
3072 dig = *q - 'a' + 10;
3073 else
3074 break;
3075
3076 c = c * 16 + dig;
3077 }
3078 /* If the Unicode character is larger than 256, we don't
3079 try to deal with it here. FIXME. */
3080 if (q < end && *q == '_' && c < 256)
3081 {
3082 d_append_char (dpi, c);
3083 p = q;
3084 continue;
3085 }
3086 }
eb383413 3087
d00edca5
DD
3088 d_append_char (dpi, *p);
3089 }
eb383413 3090 }
eb383413
L
3091}
3092
d00edca5 3093/* Print a list of modifiers. */
eb383413 3094
d00edca5
DD
3095static void
3096d_print_mod_list (dpi, mods)
3097 struct d_print_info *dpi;
3098 struct d_print_mod *mods;
eb383413 3099{
331c3da2
DD
3100 struct d_print_template *hold_dpt;
3101
d00edca5
DD
3102 if (mods == NULL || mods->printed || d_print_saw_error (dpi))
3103 return;
eb383413 3104
331c3da2
DD
3105 mods->printed = 1;
3106
3107 hold_dpt = dpi->templates;
3108 dpi->templates = mods->templates;
3109
d00edca5 3110 if (mods->mod->type == D_COMP_FUNCTION_TYPE)
eb383413 3111 {
d00edca5 3112 d_print_function_type (dpi, mods->mod, mods->next);
331c3da2 3113 dpi->templates = hold_dpt;
d00edca5
DD
3114 return;
3115 }
3116 else if (mods->mod->type == D_COMP_ARRAY_TYPE)
3117 {
d00edca5 3118 d_print_array_type (dpi, mods->mod, mods->next);
331c3da2 3119 dpi->templates = hold_dpt;
d00edca5
DD
3120 return;
3121 }
eb383413 3122
d00edca5 3123 d_print_mod (dpi, mods->mod);
eb383413 3124
331c3da2
DD
3125 dpi->templates = hold_dpt;
3126
d00edca5 3127 d_print_mod_list (dpi, mods->next);
eb383413 3128}
331c3da2 3129
d00edca5 3130/* Print a modifier. */
eb383413 3131
d00edca5
DD
3132static void
3133d_print_mod (dpi, mod)
3134 struct d_print_info *dpi;
3135 const struct d_comp *mod;
3136{
3137 switch (mod->type)
3138 {
3139 case D_COMP_RESTRICT:
3140 d_append_string (dpi, " restrict");
3141 return;
3142 case D_COMP_VOLATILE:
3143 d_append_string (dpi, " volatile");
3144 return;
3145 case D_COMP_CONST:
3146 d_append_string (dpi, " const");
3147 return;
3148 case D_COMP_VENDOR_TYPE_QUAL:
3149 d_append_char (dpi, ' ');
3150 d_print_comp (dpi, d_right (mod));
3151 return;
3152 case D_COMP_POINTER:
3153 /* There is no pointer symbol in Java. */
3154 if ((dpi->options & DMGL_JAVA) == 0)
3155 d_append_char (dpi, '*');
3156 return;
3157 case D_COMP_REFERENCE:
3158 d_append_char (dpi, '&');
3159 return;
3160 case D_COMP_COMPLEX:
3161 d_append_string (dpi, "complex ");
3162 return;
3163 case D_COMP_IMAGINARY:
3164 d_append_string (dpi, "imaginary ");
3165 return;
3166 case D_COMP_PTRMEM_TYPE:
331c3da2 3167 if (dpi->buf != NULL && dpi->buf[dpi->len - 1] != '(')
d00edca5
DD
3168 d_append_char (dpi, ' ');
3169 d_print_comp (dpi, d_left (mod));
3170 d_append_string (dpi, "::*");
3171 return;
3172 case D_COMP_TYPED_NAME:
3173 d_print_comp (dpi, d_left (mod));
3174 return;
3175 default:
3176 /* Otherwise, we have something that won't go back on the
3177 modifier stack, so we can just print it. */
3178 d_print_comp (dpi, mod);
3179 return;
3180 }
3181}
eb383413 3182
d00edca5 3183/* Print a function type, except for the return type. */
eb383413 3184
d00edca5
DD
3185static void
3186d_print_function_type (dpi, dc, mods)
3187 struct d_print_info *dpi;
3188 const struct d_comp *dc;
3189 struct d_print_mod *mods;
eb383413 3190{
331c3da2
DD
3191 int need_paren;
3192 int saw_mod;
3193 struct d_print_mod *p;
3194
3195 need_paren = 0;
3196 saw_mod = 0;
3197 for (p = mods; p != NULL; p = p->next)
d00edca5 3198 {
331c3da2
DD
3199 if (p->printed)
3200 break;
eb383413 3201
331c3da2
DD
3202 saw_mod = 1;
3203 switch (p->mod->type)
d00edca5 3204 {
331c3da2
DD
3205 case D_COMP_RESTRICT:
3206 case D_COMP_VOLATILE:
3207 case D_COMP_CONST:
3208 case D_COMP_VENDOR_TYPE_QUAL:
3209 case D_COMP_POINTER:
3210 case D_COMP_REFERENCE:
3211 case D_COMP_COMPLEX:
3212 case D_COMP_IMAGINARY:
3213 case D_COMP_PTRMEM_TYPE:
3214 need_paren = 1;
3215 break;
3216 default:
3217 break;
d00edca5 3218 }
331c3da2
DD
3219 if (need_paren)
3220 break;
3221 }
eb383413 3222
331c3da2
DD
3223 if (d_left (dc) != NULL && ! saw_mod)
3224 need_paren = 1;
eb383413 3225
331c3da2
DD
3226 if (need_paren)
3227 d_append_char (dpi, '(');
eb383413 3228
331c3da2 3229 d_print_mod_list (dpi, mods);
eb383413 3230
331c3da2
DD
3231 if (need_paren)
3232 d_append_char (dpi, ')');
eb383413 3233
d00edca5 3234 d_append_char (dpi, '(');
eb383413 3235
d00edca5
DD
3236 if (d_right (dc) != NULL)
3237 d_print_comp (dpi, d_right (dc));
eb383413 3238
d00edca5
DD
3239 d_append_char (dpi, ')');
3240}
eb383413 3241
d00edca5 3242/* Print an array type, except for the element type. */
eb383413 3243
d00edca5
DD
3244static void
3245d_print_array_type (dpi, dc, mods)
3246 struct d_print_info *dpi;
3247 const struct d_comp *dc;
3248 struct d_print_mod *mods;
3249{
3250 int need_space;
eb383413 3251
d00edca5
DD
3252 need_space = 1;
3253 if (mods != NULL)
eb383413 3254 {
d00edca5
DD
3255 int need_paren;
3256 struct d_print_mod *p;
03d5f569 3257
d00edca5
DD
3258 need_paren = 0;
3259 for (p = mods; p != NULL; p = p->next)
eb383413 3260 {
d00edca5
DD
3261 if (p->printed)
3262 break;
eb383413 3263
d00edca5 3264 if (p->mod->type == D_COMP_ARRAY_TYPE)
eb383413 3265 {
d00edca5
DD
3266 need_space = 0;
3267 break;
eb383413
L
3268 }
3269 else
3270 {
d00edca5
DD
3271 need_paren = 1;
3272 need_space = 1;
3273 break;
eb383413 3274 }
d00edca5 3275 }
eb383413 3276
d00edca5
DD
3277 if (need_paren)
3278 d_append_string (dpi, " (");
eb383413 3279
d00edca5 3280 d_print_mod_list (dpi, mods);
eb383413 3281
d00edca5
DD
3282 if (need_paren)
3283 d_append_char (dpi, ')');
3284 }
eb383413 3285
d00edca5
DD
3286 if (need_space)
3287 d_append_char (dpi, ' ');
03d5f569 3288
d00edca5 3289 d_append_char (dpi, '[');
03d5f569 3290
d00edca5
DD
3291 if (d_left (dc) != NULL)
3292 d_print_comp (dpi, d_left (dc));
eb383413 3293
d00edca5
DD
3294 d_append_char (dpi, ']');
3295}
eb383413 3296
d00edca5 3297/* Print an operator in an expression. */
eb383413 3298
d00edca5
DD
3299static void
3300d_print_expr_op (dpi, dc)
3301 struct d_print_info *dpi;
3302 const struct d_comp *dc;
3303{
3304 if (dc->type == D_COMP_OPERATOR)
3305 d_append_string (dpi, dc->u.s_operator.op->name);
3306 else
3307 d_print_comp (dpi, dc);
eb383413
L
3308}
3309
d00edca5 3310/* Print a cast. */
eb383413 3311
d00edca5
DD
3312static void
3313d_print_cast (dpi, dc)
3314 struct d_print_info *dpi;
3315 const struct d_comp *dc;
eb383413 3316{
d00edca5
DD
3317 if (d_left (dc)->type != D_COMP_TEMPLATE)
3318 d_print_comp (dpi, d_left (dc));
3319 else
3320 {
331c3da2 3321 struct d_print_mod *hold_dpm;
d00edca5 3322 struct d_print_template dpt;
0976f6a7 3323
d00edca5
DD
3324 /* It appears that for a templated cast operator, we need to put
3325 the template parameters in scope for the operator name, but
3326 not for the parameters. The effect is that we need to handle
24afc00d 3327 the template printing here. */
eb383413 3328
331c3da2
DD
3329 hold_dpm = dpi->modifiers;
3330 dpi->modifiers = NULL;
3331
d00edca5
DD
3332 dpt.next = dpi->templates;
3333 dpi->templates = &dpt;
3334 dpt.template = d_left (dc);
0976f6a7 3335
d00edca5 3336 d_print_comp (dpi, d_left (d_left (dc)));
0976f6a7 3337
d00edca5 3338 dpi->templates = dpt.next;
eb383413 3339
d00edca5
DD
3340 d_append_char (dpi, '<');
3341 d_print_comp (dpi, d_right (d_left (dc)));
3342 /* Avoid generating two consecutive '>' characters, to avoid
3343 the C++ syntactic ambiguity. */
331c3da2 3344 if (dpi->buf != NULL && dpi->buf[dpi->len - 1] == '>')
d00edca5
DD
3345 d_append_char (dpi, ' ');
3346 d_append_char (dpi, '>');
331c3da2
DD
3347
3348 dpi->modifiers = hold_dpm;
eb383413 3349 }
d00edca5
DD
3350}
3351
3352/* Initialize the information structure we use to pass around
3353 information. */
3354
3355static int
3356d_init_info (mangled, options, len, di)
3357 const char *mangled;
3358 int options;
3359 size_t len;
3360 struct d_info *di;
eb383413 3361{
d00edca5
DD
3362 di->s = mangled;
3363 di->options = options;
eb383413 3364
d00edca5
DD
3365 di->n = mangled;
3366
3367 /* We can not need more components than twice the number of chars in
3368 the mangled string. Most components correspond directly to
3369 chars, but the ARGLIST types are exceptions. */
3370 di->num_comps = 2 * len;
3371 di->comps = (struct d_comp *) malloc (di->num_comps
3372 * sizeof (struct d_comp));
3373 di->next_comp = 0;
3374
3375 /* Similarly, we can not need more substitutions than there are
331c3da2
DD
3376 chars in the mangled string. */
3377 di->num_subs = len;
d00edca5
DD
3378 di->subs = (struct d_comp **) malloc (di->num_subs
3379 * sizeof (struct d_comp *));
3380 di->next_sub = 0;
3381
3382 di->last_name = NULL;
3383
3384 if (di->comps == NULL || di->subs == NULL)
eb383413 3385 {
d00edca5
DD
3386 if (di->comps != NULL)
3387 free (di->comps);
3388 if (di->subs != NULL)
3389 free (di->subs);
3390 return 0;
eb383413
L
3391 }
3392
d00edca5 3393 return 1;
eb383413
L
3394}
3395
d00edca5
DD
3396/* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3397 name, return a buffer allocated with malloc holding the demangled
3398 name. OPTIONS is the usual libiberty demangler options. On
3399 success, this sets *PALC to the allocated size of the returned
3400 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3401 a memory allocation failure. On failure, this returns NULL. */
eb383413 3402
d00edca5
DD
3403static char *
3404d_demangle (mangled, options, palc)
3405 const char* mangled;
3406 int options;
3407 size_t *palc;
eb383413 3408{
d00edca5
DD
3409 size_t len;
3410 int type;
3411 struct d_info di;
3412 struct d_comp *dc;
3413 char *ret;
eb383413 3414
d00edca5 3415 *palc = 0;
eb383413 3416
d00edca5
DD
3417 len = strlen (mangled);
3418
3419 if (mangled[0] == '_' && mangled[1] == 'Z')
3420 type = 0;
3421 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3422 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3423 && (mangled[9] == 'D' || mangled[9] == 'I')
3424 && mangled[10] == '_')
3425 {
3426 char *r;
eb383413 3427
d00edca5
DD
3428 r = malloc (40 + len - 11);
3429 if (r == NULL)
3430 *palc = 1;
3431 else
eb383413 3432 {
d00edca5
DD
3433 if (mangled[9] == 'I')
3434 strcpy (r, "global constructors keyed to ");
3435 else
3436 strcpy (r, "global destructors keyed to ");
3437 strcat (r, mangled + 11);
eb383413 3438 }
d00edca5 3439 return r;
eb383413
L
3440 }
3441 else
3442 {
d00edca5
DD
3443 if ((options & DMGL_TYPES) == 0)
3444 return NULL;
3445 type = 1;
eb383413
L
3446 }
3447
d00edca5 3448 if (! d_init_info (mangled, options, len, &di))
03d5f569 3449 {
d00edca5
DD
3450 *palc = 1;
3451 return NULL;
03d5f569
JM
3452 }
3453
d00edca5 3454 if (! type)
331c3da2 3455 dc = d_mangled_name (&di, 1);
d00edca5
DD
3456 else
3457 dc = d_type (&di);
3458
24afc00d
DD
3459 /* If we didn't consume the entire mangled string, then we didn't
3460 successfully demangle it. */
3461 if (d_peek_char (&di) != '\0')
3462 dc = NULL;
3463
d00edca5
DD
3464#ifdef CP_DEMANGLE_DEBUG
3465 if (dc == NULL)
3466 printf ("failed demangling\n");
3467 else
3468 d_dump (dc, 0);
3469#endif
3470
3471 free (di.subs);
3472 di.subs = NULL;
03d5f569 3473
d00edca5
DD
3474 ret = NULL;
3475 if (dc != NULL)
3476 ret = d_print (options, dc, palc);
03d5f569 3477
d00edca5 3478 free (di.comps);
03d5f569 3479
d00edca5 3480 return ret;
eb383413
L
3481}
3482
0c4460bb 3483#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
d00edca5 3484
03d5f569
JM
3485extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
3486
d00edca5
DD
3487/* ia64 ABI-mandated entry point in the C++ runtime library for
3488 performing demangling. MANGLED_NAME is a NUL-terminated character
3489 string containing the name to be demangled.
03d5f569
JM
3490
3491 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3492 *LENGTH bytes, into which the demangled name is stored. If
3493 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3494 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
d00edca5 3495 is placed in a region of memory allocated with malloc.
03d5f569
JM
3496
3497 If LENGTH is non-NULL, the length of the buffer conaining the
d00edca5 3498 demangled name, is placed in *LENGTH.
03d5f569
JM
3499
3500 The return value is a pointer to the start of the NUL-terminated
3501 demangled name, or NULL if the demangling fails. The caller is
d00edca5 3502 responsible for deallocating this memory using free.
03d5f569
JM
3503
3504 *STATUS is set to one of the following values:
3505 0: The demangling operation succeeded.
d00edca5 3506 -1: A memory allocation failure occurred.
03d5f569
JM
3507 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3508 -3: One of the arguments is invalid.
3509
d00edca5 3510 The demangling is performed using the C++ ABI mangling rules, with
03d5f569
JM
3511 GNU extensions. */
3512
3513char *
3514__cxa_demangle (mangled_name, output_buffer, length, status)
3515 const char *mangled_name;
3516 char *output_buffer;
3517 size_t *length;
3518 int *status;
3519{
d00edca5
DD
3520 char *demangled;
3521 size_t alc;
03d5f569
JM
3522
3523 if (status == NULL)
3524 return NULL;
3525
d00edca5
DD
3526 if (mangled_name == NULL)
3527 {
03d5f569
JM
3528 *status = -3;
3529 return NULL;
3530 }
03d5f569 3531
d00edca5 3532 if (output_buffer != NULL && length == NULL)
03d5f569 3533 {
d00edca5
DD
3534 *status = -3;
3535 return NULL;
03d5f569 3536 }
d00edca5
DD
3537
3538 demangled = d_demangle (mangled_name, DMGL_TYPES, &alc);
3539
3540 if (demangled == NULL)
03d5f569 3541 {
d00edca5
DD
3542 if (alc == 1)
3543 *status = -1;
3544 else
3545 *status = -2;
03d5f569
JM
3546 return NULL;
3547 }
d00edca5
DD
3548
3549 if (output_buffer == NULL)
3550 {
3551 if (length != NULL)
3552 *length = alc;
3553 }
03d5f569 3554 else
03d5f569 3555 {
d00edca5
DD
3556 if (strlen (demangled) < *length)
3557 {
3558 strcpy (output_buffer, demangled);
3559 free (demangled);
3560 demangled = output_buffer;
3561 }
3562 else
3563 {
3564 free (output_buffer);
3565 *length = alc;
3566 }
03d5f569 3567 }
d00edca5
DD
3568
3569 *status = 0;
3570
3571 return demangled;
03d5f569
JM
3572}
3573
0c4460bb 3574#else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
03d5f569 3575
d00edca5
DD
3576/* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3577 mangled name, return a buffer allocated with malloc holding the
3578 demangled name. Otherwise, return NULL. */
eb383413
L
3579
3580char *
44354ae1 3581cplus_demangle_v3 (mangled, options)
eb383413 3582 const char* mangled;
44354ae1 3583 int options;
eb383413 3584{
d00edca5 3585 size_t alc;
849ee224 3586
d00edca5 3587 return d_demangle (mangled, options, &alc);
eb383413
L
3588}
3589
bc9bf259
DD
3590/* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3591 conventions, but the output formatting is a little different.
3592 This instructs the C++ demangler not to emit pointer characters ("*"), and
3593 to use Java's namespace separator symbol ("." instead of "::"). It then
3594 does an additional pass over the demangled output to replace instances
3595 of JArray<TYPE> with TYPE[]. */
3596
3597char *
3598java_demangle_v3 (mangled)
3599 const char* mangled;
3600{
d00edca5
DD
3601 size_t alc;
3602 char *demangled;
3603 int nesting;
3604 char *from;
3605 char *to;
3606
51948b34 3607 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
d00edca5
DD
3608
3609 if (demangled == NULL)
3610 return NULL;
3611
3612 nesting = 0;
3613 from = demangled;
3614 to = from;
3615 while (*from != '\0')
bc9bf259 3616 {
d00edca5
DD
3617 if (strncmp (from, "JArray<", 7) == 0)
3618 {
3619 from += 7;
bc9bf259 3620 ++nesting;
bc9bf259 3621 }
d00edca5
DD
3622 else if (nesting > 0 && *from == '>')
3623 {
3624 while (to > demangled && to[-1] == ' ')
3625 --to;
3626 *to++ = '[';
3627 *to++ = ']';
bc9bf259 3628 --nesting;
d00edca5 3629 ++from;
bc9bf259
DD
3630 }
3631 else
d00edca5 3632 *to++ = *from++;
bc9bf259
DD
3633 }
3634
d00edca5 3635 *to = '\0';
f2160d2b 3636
d00edca5 3637 return demangled;
bc9bf259
DD
3638}
3639
0c4460bb 3640#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
03d5f569 3641
2a9dffbf 3642#ifndef IN_GLIBCPP_V3
d00edca5
DD
3643
3644/* Demangle a string in order to find out whether it is a constructor
3645 or destructor. Return non-zero on success. Set *CTOR_KIND and
3646 *DTOR_KIND appropriately. */
3647
3648static int
3649is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
3650 const char *mangled;
3651 enum gnu_v3_ctor_kinds *ctor_kind;
3652 enum gnu_v3_dtor_kinds *dtor_kind;
e61231f1 3653{
d00edca5
DD
3654 struct d_info di;
3655 struct d_comp *dc;
e61231f1 3656
d00edca5
DD
3657 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
3658 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
3659
3660 if (! d_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di))
e61231f1
JB
3661 return 0;
3662
331c3da2 3663 dc = d_mangled_name (&di, 1);
d00edca5 3664
24afc00d 3665 if (dc == NULL || d_peek_char (&di) != '\0')
d00edca5 3666 return 0;
e61231f1 3667
d00edca5 3668 while (dc != NULL)
e61231f1 3669 {
d00edca5
DD
3670 switch (dc->type)
3671 {
3672 default:
3673 return 0;
3674 case D_COMP_TYPED_NAME:
3675 case D_COMP_TEMPLATE:
3676 case D_COMP_RESTRICT:
3677 case D_COMP_VOLATILE:
3678 case D_COMP_CONST:
3679 case D_COMP_VENDOR_TYPE_QUAL:
3680 dc = d_left (dc);
3681 break;
3682 case D_COMP_QUAL_NAME:
3683 dc = d_right (dc);
3684 break;
3685 case D_COMP_CTOR:
3686 *ctor_kind = dc->u.s_ctor.kind;
3687 return 1;
3688 case D_COMP_DTOR:
3689 *dtor_kind = dc->u.s_dtor.kind;
3690 return 1;
3691 }
e61231f1
JB
3692 }
3693
e61231f1
JB
3694 return 0;
3695}
3696
d00edca5
DD
3697/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3698 name. A non-zero return indicates the type of constructor. */
e61231f1 3699
e61231f1 3700enum gnu_v3_ctor_kinds
457161bf
DD
3701is_gnu_v3_mangled_ctor (name)
3702 const char *name;
e61231f1 3703{
d00edca5
DD
3704 enum gnu_v3_ctor_kinds ctor_kind;
3705 enum gnu_v3_dtor_kinds dtor_kind;
e61231f1 3706
d00edca5 3707 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
585cc78f 3708 return (enum gnu_v3_ctor_kinds) 0;
d00edca5 3709 return ctor_kind;
e61231f1
JB
3710}
3711
3712
d00edca5
DD
3713/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3714 name. A non-zero return indicates the type of destructor. */
3715
e61231f1 3716enum gnu_v3_dtor_kinds
457161bf
DD
3717is_gnu_v3_mangled_dtor (name)
3718 const char *name;
e61231f1 3719{
d00edca5
DD
3720 enum gnu_v3_ctor_kinds ctor_kind;
3721 enum gnu_v3_dtor_kinds dtor_kind;
e61231f1 3722
d00edca5 3723 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
585cc78f 3724 return (enum gnu_v3_dtor_kinds) 0;
d00edca5 3725 return dtor_kind;
e61231f1
JB
3726}
3727
d00edca5 3728#endif /* IN_GLIBCPP_V3 */
e61231f1 3729
eb383413
L
3730#ifdef STANDALONE_DEMANGLER
3731
3732#include "getopt.h"
d00edca5
DD
3733#include "dyn-string.h"
3734
3735static void print_usage PARAMS ((FILE* fp, int exit_value));
eb383413 3736
d00edca5
DD
3737#define IS_ALPHA(CHAR) \
3738 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3739 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
eb383413
L
3740
3741/* Non-zero if CHAR is a character than can occur in a mangled name. */
3742#define is_mangled_char(CHAR) \
74bcd529
DD
3743 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3744 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
eb383413
L
3745
3746/* The name of this program, as invoked. */
3747const char* program_name;
3748
3749/* Prints usage summary to FP and then exits with EXIT_VALUE. */
3750
3751static void
3752print_usage (fp, exit_value)
3753 FILE* fp;
3754 int exit_value;
3755{
3756 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
74bcd529 3757 fprintf (fp, "Options:\n");
eb383413 3758 fprintf (fp, " -h,--help Display this message.\n");
6d95373e 3759 fprintf (fp, " -p,--no-params Don't display function parameters\n");
eb383413
L
3760 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
3761 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
3762
3763 exit (exit_value);
3764}
3765
3766/* Option specification for getopt_long. */
c23795e2 3767static const struct option long_options[] =
eb383413 3768{
6d95373e
DD
3769 { "help", no_argument, NULL, 'h' },
3770 { "no-params", no_argument, NULL, 'p' },
3771 { "verbose", no_argument, NULL, 'v' },
3772 { NULL, no_argument, NULL, 0 },
eb383413
L
3773};
3774
3775/* Main entry for a demangling filter executable. It will demangle
3776 its command line arguments, if any. If none are provided, it will
3777 filter stdin to stdout, replacing any recognized mangled C++ names
3778 with their demangled equivalents. */
3779
3780int
3781main (argc, argv)
3782 int argc;
3783 char *argv[];
3784{
eb383413
L
3785 int i;
3786 int opt_char;
d00edca5 3787 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
eb383413
L
3788
3789 /* Use the program name of this program, as invoked. */
3790 program_name = argv[0];
3791
3792 /* Parse options. */
3793 do
3794 {
6d95373e 3795 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
eb383413
L
3796 switch (opt_char)
3797 {
3798 case '?': /* Unrecognized option. */
3799 print_usage (stderr, 1);
3800 break;
3801
3802 case 'h':
3803 print_usage (stdout, 0);
3804 break;
3805
6d95373e
DD
3806 case 'p':
3807 options &= ~ DMGL_PARAMS;
3808 break;
3809
eb383413 3810 case 'v':
d00edca5 3811 options |= DMGL_VERBOSE;
eb383413
L
3812 break;
3813 }
3814 }
3815 while (opt_char != -1);
3816
3817 if (optind == argc)
3818 /* No command line arguments were provided. Filter stdin. */
3819 {
3820 dyn_string_t mangled = dyn_string_new (3);
d00edca5 3821 char *s;
eb383413
L
3822
3823 /* Read all of input. */
3824 while (!feof (stdin))
3825 {
d00edca5 3826 char c;
eb383413
L
3827
3828 /* Pile characters into mangled until we hit one that can't
3829 occur in a mangled name. */
3830 c = getchar ();
3831 while (!feof (stdin) && is_mangled_char (c))
3832 {
3833 dyn_string_append_char (mangled, c);
3834 if (feof (stdin))
3835 break;
3836 c = getchar ();
3837 }
3838
d00edca5 3839 if (dyn_string_length (mangled) > 0)
03d5f569 3840 {
d00edca5
DD
3841 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
3842
3843 if (s != NULL)
3844 {
3845 fputs (s, stdout);
3846 free (s);
3847 }
3848 else
3849 {
3850 /* It might not have been a mangled name. Print the
3851 original text. */
3852 fputs (dyn_string_buf (mangled), stdout);
3853 }
3854
3855 dyn_string_clear (mangled);
03d5f569 3856 }
eb383413
L
3857
3858 /* If we haven't hit EOF yet, we've read one character that
3859 can't occur in a mangled name, so print it out. */
3860 if (!feof (stdin))
3861 putchar (c);
eb383413
L
3862 }
3863
3864 dyn_string_delete (mangled);
eb383413
L
3865 }
3866 else
3867 /* Demangle command line arguments. */
3868 {
eb383413
L
3869 /* Loop over command line arguments. */
3870 for (i = optind; i < argc; ++i)
3871 {
d00edca5
DD
3872 char *s;
3873
eb383413 3874 /* Attempt to demangle. */
d00edca5 3875 s = cplus_demangle_v3 (argv[i], options);
eb383413
L
3876
3877 /* If it worked, print the demangled name. */
d00edca5 3878 if (s != NULL)
03d5f569 3879 {
d00edca5
DD
3880 printf ("%s\n", s);
3881 free (s);
03d5f569 3882 }
d00edca5
DD
3883 else
3884 fprintf (stderr, "Failed: %s\n", argv[i]);
eb383413 3885 }
eb383413
L
3886 }
3887
3888 return 0;
3889}
3890
3891#endif /* STANDALONE_DEMANGLER */
This page took 0.438319 seconds and 4 git commands to generate.