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