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