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