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