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