merge from gcc
[deliverable/binutils-gdb.git] / libiberty / cp-demangle.c
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
4
5 This file is part of the libiberty library, which is part of GCC.
6
7 This file is free software; you can redistribute it and/or modify
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
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
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
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdio.h>
36
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
43
44 #include "ansidecl.h"
45 #include "libiberty.h"
46 #include "demangle.h"
47
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
51
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')
61
62 /* The prefix prepended by GCC to an identifier represnting the
63 anonymous namespace. */
64 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
65 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
66 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
67
68 /* Information we keep for operators. */
69
70 struct d_operator_info
71 {
72 /* Mangled name. */
73 const char *code;
74 /* Real name. */
75 const char *name;
76 /* Number of arguments. */
77 int args;
78 };
79
80 /* How to print the value of a builtin type. */
81
82 enum 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
94 };
95
96 /* Information we keep for a builtin type. */
97
98 struct d_builtin_type_info
99 {
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 };
107
108 /* Component types found in mangled names. */
109
110 enum 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
201 };
202
203 /* A component of the mangled name. */
204
205 struct d_comp
206 {
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;
219
220 /* For D_COMP_OPERATOR. */
221 struct
222 {
223 /* Operator. */
224 const struct d_operator_info *op;
225 } s_operator;
226
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;
235
236 /* For D_COMP_CTOR. */
237 struct
238 {
239 enum gnu_v3_ctor_kinds kind;
240 struct d_comp *name;
241 } s_ctor;
242
243 /* For D_COMP_DTOR. */
244 struct
245 {
246 enum gnu_v3_dtor_kinds kind;
247 struct d_comp *name;
248 } s_dtor;
249
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;
261
262 /* For D_COMP_TEMPLATE_PARAM. */
263 struct
264 {
265 long number;
266 } s_number;
267
268 /* For other types. */
269 struct
270 {
271 struct d_comp *left;
272 struct d_comp *right;
273 } s_binary;
274
275 } u;
276 };
277
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
283 struct 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 };
306
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)
312
313 /* A list of templates. This is used while printing. */
314
315 struct 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 };
322
323 /* A list of type modifiers. This is used while printing. */
324
325 struct 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 };
335
336 /* We use this structure to hold information during printing. */
337
338 struct 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 };
356
357 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
358
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)
368
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)
381
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 } \
388 while (0)
389
390 #ifdef CP_DEMANGLE_DEBUG
391 static void d_dump PARAMS ((struct d_comp *, int));
392 #endif
393 static struct d_comp *d_make_empty PARAMS ((struct d_info *,
394 enum d_comp_type));
395 static struct d_comp *d_make_comp PARAMS ((struct d_info *, enum d_comp_type,
396 struct d_comp *, struct d_comp *));
397 static struct d_comp *d_make_name PARAMS ((struct d_info *, const char *,
398 int));
399 static struct d_comp *d_make_builtin_type PARAMS ((struct d_info *,
400 const struct d_builtin_type_info *));
401 static struct d_comp *d_make_operator PARAMS ((struct d_info *,
402 const struct d_operator_info *));
403 static struct d_comp *d_make_extended_operator PARAMS ((struct d_info *,
404 int,
405 struct d_comp *));
406 static struct d_comp *d_make_ctor PARAMS ((struct d_info *,
407 enum gnu_v3_ctor_kinds,
408 struct d_comp *));
409 static struct d_comp *d_make_dtor PARAMS ((struct d_info *,
410 enum gnu_v3_dtor_kinds,
411 struct d_comp *));
412 static struct d_comp *d_make_template_param PARAMS ((struct d_info *, long));
413 static struct d_comp *d_make_sub PARAMS ((struct d_info *, const char *));
414 static struct d_comp *d_mangled_name PARAMS ((struct d_info *));
415 static int has_return_type PARAMS ((struct d_comp *));
416 static int is_ctor_dtor_or_conversion PARAMS ((struct d_comp *));
417 static struct d_comp *d_encoding PARAMS ((struct d_info *, int));
418 static struct d_comp *d_name PARAMS ((struct d_info *));
419 static struct d_comp *d_nested_name PARAMS ((struct d_info *));
420 static struct d_comp *d_prefix PARAMS ((struct d_info *));
421 static struct d_comp *d_unqualified_name PARAMS ((struct d_info *));
422 static struct d_comp *d_source_name PARAMS ((struct d_info *));
423 static long d_number PARAMS ((struct d_info *));
424 static struct d_comp *d_identifier PARAMS ((struct d_info *, int));
425 static struct d_comp *d_operator_name PARAMS ((struct d_info *));
426 static struct d_comp *d_special_name PARAMS ((struct d_info *));
427 static int d_call_offset PARAMS ((struct d_info *, int));
428 static struct d_comp *d_ctor_dtor_name PARAMS ((struct d_info *));
429 static struct d_comp *d_type PARAMS ((struct d_info *));
430 static struct d_comp **d_cv_qualifiers PARAMS ((struct d_info *,
431 struct d_comp **));
432 static struct d_comp *d_function_type PARAMS ((struct d_info *));
433 static struct d_comp *d_bare_function_type PARAMS ((struct d_info *, int));
434 static struct d_comp *d_class_enum_type PARAMS ((struct d_info *));
435 static struct d_comp *d_array_type PARAMS ((struct d_info *));
436 static struct d_comp *d_pointer_to_member_type PARAMS ((struct d_info *));
437 static struct d_comp *d_template_param PARAMS ((struct d_info *));
438 static struct d_comp *d_template_args PARAMS ((struct d_info *));
439 static struct d_comp *d_template_arg PARAMS ((struct d_info *));
440 static struct d_comp *d_expression PARAMS ((struct d_info *));
441 static struct d_comp *d_expr_primary PARAMS ((struct d_info *));
442 static struct d_comp *d_local_name PARAMS ((struct d_info *));
443 static int d_discriminator PARAMS ((struct d_info *));
444 static int d_add_substitution PARAMS ((struct d_info *, struct d_comp *));
445 static struct d_comp *d_substitution PARAMS ((struct d_info *));
446 static void d_print_resize PARAMS ((struct d_print_info *, size_t));
447 static void d_print_append_char PARAMS ((struct d_print_info *, int));
448 static void d_print_append_buffer PARAMS ((struct d_print_info *, const char *,
449 size_t));
450 static void d_print_error PARAMS ((struct d_print_info *));
451 static char *d_print PARAMS ((int, const struct d_comp *, size_t *));
452 static void d_print_comp PARAMS ((struct d_print_info *,
453 const struct d_comp *));
454 static void d_print_identifier PARAMS ((struct d_print_info *, const char *,
455 int));
456 static void d_print_mod_list PARAMS ((struct d_print_info *,
457 struct d_print_mod *));
458 static void d_print_mod PARAMS ((struct d_print_info *,
459 const struct d_comp *));
460 static void d_print_function_type PARAMS ((struct d_print_info *,
461 const struct d_comp *,
462 struct d_print_mod *));
463 static void d_print_array_type PARAMS ((struct d_print_info *,
464 const struct d_comp *,
465 struct d_print_mod *));
466 static void d_print_expr_op PARAMS ((struct d_print_info *,
467 const struct d_comp *));
468 static void d_print_cast PARAMS ((struct d_print_info *,
469 const struct d_comp *));
470 static int d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
471 static char *d_demangle PARAMS ((const char *, int, size_t *));
472
473 #ifdef CP_DEMANGLE_DEBUG
474
475 static void
476 d_dump (dc, indent)
477 struct d_comp *dc;
478 int indent;
479 {
480 int i;
481
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;
630 }
631
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
640 static struct d_comp *
641 d_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
657 static struct d_comp *
658 d_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;
730 }
731
732 p = d_make_empty (di, type);
733 if (p != NULL)
734 {
735 p->u.s_binary.left = left;
736 p->u.s_binary.right = right;
737 }
738 return p;
739 }
740
741 /* Add a new name component. */
742
743 static struct d_comp *
744 d_make_name (di, s, len)
745 struct d_info *di;
746 const char *s;
747 int len;
748 {
749 struct d_comp *p;
750
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;
756 }
757 return p;
758 }
759
760 /* Add a new builtin type component. */
761
762 static struct d_comp *
763 d_make_builtin_type (di, type)
764 struct d_info *di;
765 const struct d_builtin_type_info *type;
766 {
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 }
774
775 /* Add a new operator component. */
776
777 static struct d_comp *
778 d_make_operator (di, op)
779 struct d_info *di;
780 const struct d_operator_info *op;
781 {
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;
788 }
789
790 /* Add a new extended operator component. */
791
792 static struct d_comp *
793 d_make_extended_operator (di, args, name)
794 struct d_info *di;
795 int args;
796 struct d_comp *name;
797 {
798 struct d_comp *p;
799
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;
807 }
808
809 /* Add a new constructor component. */
810
811 static struct d_comp *
812 d_make_ctor (di, kind, name)
813 struct d_info *di;
814 enum gnu_v3_ctor_kinds kind;
815 struct d_comp *name;
816 {
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;
826 }
827
828 /* Add a new destructor component. */
829
830 static struct d_comp *
831 d_make_dtor (di, kind, name)
832 struct d_info *di;
833 enum gnu_v3_dtor_kinds kind;
834 struct d_comp *name;
835 {
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;
845 }
846
847 /* Add a new template parameter. */
848
849 static struct d_comp *
850 d_make_template_param (di, i)
851 struct d_info *di;
852 long i;
853 {
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;
860 }
861
862 /* Add a new standard substitution component. */
863
864 static struct d_comp *
865 d_make_sub (di, name)
866 struct d_info *di;
867 const char *name;
868 {
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;
875 }
876
877 /* <mangled-name> ::= _Z <encoding> */
878
879 static struct d_comp *
880 d_mangled_name (di)
881 struct d_info *di;
882 {
883 if (d_next_char (di) != '_')
884 return NULL;
885 if (d_next_char (di) != 'Z')
886 return NULL;
887 return d_encoding (di, 1);
888 }
889
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. */
898
899 static int
900 has_return_type (dc)
901 struct d_comp *dc;
902 {
903 if (dc == NULL)
904 return 0;
905 switch (dc->type)
906 {
907 default:
908 return 0;
909 case D_COMP_TEMPLATE:
910 return ! is_ctor_dtor_or_conversion (d_left (dc));
911 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));
916 }
917 }
918
919 /* Return whether a name is a constructor, a destructor, or a
920 conversion operator. */
921
922 static int
923 is_ctor_dtor_or_conversion (dc)
924 struct d_comp *dc;
925 {
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 }
939 }
940
941 /* <encoding> ::= <(function) name> <bare-function-type>
942 ::= <(data) name>
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. */
949
950 static struct d_comp *
951 d_encoding (di, top_level)
952 struct d_info *di;
953 int top_level;
954 {
955 char peek = d_peek_char (di);
956
957 if (peek == 'G' || peek == 'T')
958 return d_special_name (di);
959 else
960 {
961 struct d_comp *dc;
962
963 dc = d_name (di);
964 peek = d_peek_char (di);
965 if (peek == '\0'
966 || peek == 'E'
967 || (top_level && (di->options & DMGL_PARAMS) == 0))
968 return dc;
969 return d_make_comp (di, D_COMP_TYPED_NAME, dc,
970 d_bare_function_type (di, has_return_type (dc)));
971 }
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>
981
982 <unscoped-template-name> ::= <unscoped-name>
983 ::= <substitution>
984 */
985
986 static struct d_comp *
987 d_name (di)
988 struct d_info *di;
989 {
990 char peek = d_peek_char (di);
991 struct d_comp *dc;
992
993 switch (peek)
994 {
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')
1044 {
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));
1051 }
1052 return dc;
1053 }
1054 }
1055
1056 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1057 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1058 */
1059
1060 static struct d_comp *
1061 d_nested_name (di)
1062 struct d_info *di;
1063 {
1064 struct d_comp *ret;
1065 struct d_comp **pret;
1066
1067 if (d_next_char (di) != 'N')
1068 return NULL;
1069
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;
1077
1078 if (d_next_char (di) != 'E')
1079 return NULL;
1080
1081 return ret;
1082 }
1083
1084 /* <prefix> ::= <prefix> <unqualified-name>
1085 ::= <template-prefix> <template-args>
1086 ::= <template-param>
1087 ::=
1088 ::= <substitution>
1089
1090 <template-prefix> ::= <prefix> <(template) unqualified-name>
1091 ::= <template-param>
1092 ::= <substitution>
1093 */
1094
1095 static struct d_comp *
1096 d_prefix (di)
1097 struct d_info *di;
1098 {
1099 struct d_comp *ret = NULL;
1100
1101 while (1)
1102 {
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. */
1114
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;
1139 else
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 }
1147 }
1148 }
1149
1150 /* <unqualified-name> ::= <operator-name>
1151 ::= <ctor-dtor-name>
1152 ::= <source-name>
1153 */
1154
1155 static struct d_comp *
1156 d_unqualified_name (di)
1157 struct d_info *di;
1158 {
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
1169 return NULL;
1170 }
1171
1172 /* <source-name> ::= <(positive length) number> <identifier> */
1173
1174 static struct d_comp *
1175 d_source_name (di)
1176 struct d_info *di;
1177 {
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;
1187 }
1188
1189 /* number ::= [n] <(non-negative decimal integer)> */
1190
1191 static long
1192 d_number (di)
1193 struct d_info *di;
1194 {
1195 int sign;
1196 char peek;
1197 long ret;
1198
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 }
1207
1208 ret = 0;
1209 while (1)
1210 {
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);
1216 }
1217 }
1218
1219 /* identifier ::= <(unqualified source code identifier)> */
1220
1221 static struct d_comp *
1222 d_identifier (di, len)
1223 struct d_info *di;
1224 int len;
1225 {
1226 const char *name;
1227
1228 name = d_str (di);
1229 d_advance (di, len);
1230
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)
1237 {
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);
1245 }
1246
1247 return d_make_name (di, name, len);
1248 }
1249
1250 /* operator_name ::= many different two character encodings.
1251 ::= cv <type>
1252 ::= v <digit> <source-name>
1253 */
1254
1255 static 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 };
1307
1308 static struct d_comp *
1309 d_operator_name (di)
1310 struct d_info *di;
1311 {
1312 char c1;
1313 char c2;
1314
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
1322 {
1323 int low = 0;
1324 int high = sizeof (d_operators) / sizeof (d_operators[0]);
1325
1326 while (1)
1327 {
1328 int i;
1329 const struct d_operator_info *p;
1330
1331 i = low + (high - low) / 2;
1332 p = d_operators + i;
1333
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 }
1345 }
1346
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 */
1360
1361 static struct d_comp *
1362 d_special_name (di)
1363 struct d_info *di;
1364 {
1365 char c;
1366
1367 c = d_next_char (di);
1368 if (c == 'T')
1369 {
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);
1380
1381 case 'h':
1382 if (! d_call_offset (di, 'h'))
1383 return NULL;
1384 return d_make_comp (di, D_COMP_THUNK, d_encoding (di, 0), NULL);
1385
1386 case 'v':
1387 if (! d_call_offset (di, 'v'))
1388 return NULL;
1389 return d_make_comp (di, D_COMP_VIRTUAL_THUNK, d_encoding (di, 0),
1390 NULL);
1391
1392 case 'c':
1393 if (! d_call_offset (di, '\0'))
1394 return NULL;
1395 if (! d_call_offset (di, '\0'))
1396 return NULL;
1397 return d_make_comp (di, D_COMP_COVARIANT_THUNK, d_encoding (di, 0),
1398 NULL);
1399
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 }
1418
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);
1423
1424 default:
1425 return NULL;
1426 }
1427 }
1428 else if (c == 'G')
1429 {
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 }
1441 }
1442 else
1443 return NULL;
1444 }
1445
1446 /* <call-offset> ::= h <nv-offset> _
1447 ::= v <v-offset> _
1448
1449 <nv-offset> ::= <(offset) number>
1450
1451 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1452
1453 The C parameter, if not '\0', is a character we just read which is
1454 the start of the <call-offset>.
1455
1456 We don't display the offset information anywhere. FIXME: We should
1457 display it in verbose mode. */
1458
1459 static int
1460 d_call_offset (di, c)
1461 struct d_info *di;
1462 int c;
1463 {
1464 long offset;
1465 long virtual_offset;
1466
1467 if (c == '\0')
1468 c = d_next_char (di);
1469
1470 if (c == 'h')
1471 offset = d_number (di);
1472 else if (c == 'v')
1473 {
1474 offset = d_number (di);
1475 if (d_next_char (di) != '_')
1476 return 0;
1477 virtual_offset = d_number (di);
1478 }
1479 else
1480 return 0;
1481
1482 if (d_next_char (di) != '_')
1483 return 0;
1484
1485 return 1;
1486 }
1487
1488 /* <ctor-dtor-name> ::= C1
1489 ::= C2
1490 ::= C3
1491 ::= D0
1492 ::= D1
1493 ::= D2
1494 */
1495
1496 static struct d_comp *
1497 d_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 }
1543
1544 default:
1545 return NULL;
1546 }
1547 }
1548
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 */
1567
1568 static 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 };
1597
1598 static struct d_comp *
1599 d_type (di)
1600 struct d_info *di;
1601 {
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;
1621
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 }
1628
1629 can_subst = 1;
1630
1631 switch (peek)
1632 {
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':
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);
1649 break;
1650
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':
1654 case 'Z':
1655 ret = d_class_enum_type (di);
1656 break;
1657
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')
1669 {
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));
1676 }
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;
1684
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 }
1710 break;
1711
1712 case 'P':
1713 d_advance (di, 1);
1714 ret = d_make_comp (di, D_COMP_POINTER, d_type (di), NULL);
1715 break;
1716
1717 case 'R':
1718 d_advance (di, 1);
1719 ret = d_make_comp (di, D_COMP_REFERENCE, d_type (di), NULL);
1720 break;
1721
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;
1731
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);
1736 break;
1737
1738 default:
1739 return NULL;
1740 }
1741
1742 if (can_subst)
1743 {
1744 if (! d_add_substitution (di, ret))
1745 return NULL;
1746 }
1747
1748 return ret;
1749 }
1750
1751 /* <CV-qualifiers> ::= [r] [V] [K] */
1752
1753 static struct d_comp **
1754 d_cv_qualifiers (di, pret)
1755 struct d_info *di;
1756 struct d_comp **pret;
1757 {
1758 char peek;
1759
1760 peek = d_peek_char (di);
1761 while (peek == 'r' || peek == 'V' || peek == 'K')
1762 {
1763 enum d_comp_type t;
1764
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;
1772
1773 *pret = d_make_comp (di, t, NULL, NULL);
1774 if (*pret == NULL)
1775 return NULL;
1776 pret = &d_left (*pret);
1777
1778 peek = d_peek_char (di);
1779 }
1780
1781 return pret;
1782 }
1783
1784 /* <function-type> ::= F [Y] <bare-function-type> E */
1785
1786 static struct d_comp *
1787 d_function_type (di)
1788 struct d_info *di;
1789 {
1790 struct d_comp *ret;
1791
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 }
1805
1806 /* <bare-function-type> ::= <type>+ */
1807
1808 static struct d_comp *
1809 d_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;
1816
1817 return_type = NULL;
1818 tl = NULL;
1819 ptl = &tl;
1820 while (1)
1821 {
1822 char peek;
1823 struct d_comp *type;
1824
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)
1832 {
1833 return_type = type;
1834 has_return_type = 0;
1835 }
1836 else
1837 {
1838 *ptl = d_make_comp (di, D_COMP_ARGLIST, type, NULL);
1839 ptl = &d_right (*ptl);
1840 }
1841 }
1842
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;
1848
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;
1854
1855 return d_make_comp (di, D_COMP_FUNCTION_TYPE, return_type, tl);
1856 }
1857
1858 /* <class-enum-type> ::= <name> */
1859
1860 static struct d_comp *
1861 d_class_enum_type (di)
1862 struct d_info *di;
1863 {
1864 return d_name (di);
1865 }
1866
1867 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1868 ::= A [<(dimension) expression>] _ <(element) type>
1869 */
1870
1871 static struct d_comp *
1872 d_array_type (di)
1873 struct d_info *di;
1874 {
1875 char peek;
1876 struct d_comp *dim;
1877
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))
1885 {
1886 const char *s;
1887
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);
1896 }
1897 else
1898 {
1899 dim = d_expression (di);
1900 if (dim == NULL)
1901 return NULL;
1902 }
1903
1904 if (d_next_char (di) != '_')
1905 return NULL;
1906
1907 return d_make_comp (di, D_COMP_ARRAY_TYPE, dim, d_type (di));
1908 }
1909
1910 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
1911
1912 static struct d_comp *
1913 d_pointer_to_member_type (di)
1914 struct d_info *di;
1915 {
1916 struct d_comp *cl;
1917 struct d_comp *mem;
1918 struct d_comp **pmem;
1919
1920 if (d_next_char (di) != 'M')
1921 return NULL;
1922
1923 cl = d_type (di);
1924
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(). */
1937
1938 pmem = d_cv_qualifiers (di, &mem);
1939 *pmem = d_type (di);
1940
1941 return d_make_comp (di, D_COMP_PTRMEM_TYPE, cl, mem);
1942 }
1943
1944 /* <template-param> ::= T_
1945 ::= T <(parameter-2 non-negative) number> _
1946 */
1947
1948 static struct d_comp *
1949 d_template_param (di)
1950 struct d_info *di;
1951 {
1952 long param;
1953
1954 if (d_next_char (di) != 'T')
1955 return NULL;
1956
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 }
1966
1967 if (d_next_char (di) != '_')
1968 return NULL;
1969
1970 return d_make_template_param (di, param);
1971 }
1972
1973 /* <template-args> ::= I <template-arg>+ E */
1974
1975 static struct d_comp *
1976 d_template_args (di)
1977 struct d_info *di;
1978 {
1979 struct d_comp *hold_last_name;
1980 struct d_comp *al;
1981 struct d_comp **pal;
1982
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;
1987
1988 if (d_next_char (di) != 'I')
1989 return NULL;
1990
1991 al = NULL;
1992 pal = &al;
1993 while (1)
1994 {
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')
2005 {
2006 d_advance (di, 1);
2007 break;
2008 }
2009 }
2010
2011 di->last_name = hold_last_name;
2012
2013 return al;
2014 }
2015
2016 /* <template-arg> ::= <type>
2017 ::= X <expression> E
2018 ::= <expr-primary>
2019 */
2020
2021 static struct d_comp *
2022 d_template_arg (di)
2023 struct d_info *di;
2024 {
2025 struct d_comp *ret;
2026
2027 switch (d_peek_char (di))
2028 {
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;
2035
2036 case 'L':
2037 return d_expr_primary (di);
2038
2039 default:
2040 return d_type (di);
2041 }
2042 }
2043
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
2054 static struct d_comp *
2055 d_expression (di)
2056 struct d_info *di;
2057 {
2058 char peek;
2059
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')
2066 {
2067 struct d_comp *type;
2068 struct d_comp *name;
2069
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)));
2079 }
2080 else
2081 {
2082 struct d_comp *op;
2083 int args;
2084
2085 op = d_operator_name (di);
2086 if (op == NULL)
2087 return NULL;
2088
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));
2092
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 }
2138 }
2139 }
2140
2141 /* <expr-primary> ::= L <type> <(value) number> E
2142 ::= L <type> <(value) float> E
2143 ::= L <mangled-name> E
2144 */
2145
2146 static struct d_comp *
2147 d_expr_primary (di)
2148 struct d_info *di;
2149 {
2150 struct d_comp *ret;
2151
2152 if (d_next_char (di) != 'L')
2153 return NULL;
2154 if (d_peek_char (di) == '_')
2155 ret = d_mangled_name (di);
2156 else
2157 {
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;
2183 }
2184
2185 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2186 ::= Z <(function) encoding> E s [<discriminator>]
2187 */
2188
2189 static struct d_comp *
2190 d_local_name (di)
2191 struct d_info *di;
2192 {
2193 struct d_comp *function;
2194
2195 if (d_next_char (di) != 'Z')
2196 return NULL;
2197
2198 function = d_encoding (di, 0);
2199
2200 if (d_next_char (di) != 'E')
2201 return NULL;
2202
2203 if (d_peek_char (di) == 's')
2204 {
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));
2211 }
2212 else
2213 {
2214 struct d_comp *name;
2215
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);
2220 }
2221 }
2222
2223 /* <discriminator> ::= _ <(non-negative) number>
2224
2225 We demangle the discriminator, but we don't print it out. FIXME:
2226 We should print it out in verbose mode. */
2227
2228 static int
2229 d_discriminator (di)
2230 struct d_info *di;
2231 {
2232 long discrim;
2233
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 }
2242
2243 /* Add a new substitution. */
2244
2245 static int
2246 d_add_substitution (di, dc)
2247 struct d_info *di;
2248 struct d_comp *dc;
2249 {
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 */
2267
2268 static struct d_comp *
2269 d_substitution (di)
2270 struct d_info *di;
2271 {
2272 char c;
2273
2274 if (d_next_char (di) != 'S')
2275 return NULL;
2276
2277 c = d_next_char (di);
2278 if (c == '_' || IS_DIGIT (c) || (c >= 'A' && c <= 'Z'))
2279 {
2280 int id;
2281
2282 id = 0;
2283 if (c != '_')
2284 {
2285 do
2286 {
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);
2294 }
2295 while (c != '_');
2296
2297 ++id;
2298 }
2299
2300 if (id >= di->next_sub)
2301 return NULL;
2302
2303 return di->subs[id];
2304 }
2305 else
2306 {
2307 switch (c)
2308 {
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;
2331 }
2332 }
2333 }
2334
2335 /* Resize the print buffer. */
2336
2337 static void
2338 d_print_resize (dpi, add)
2339 struct d_print_info *dpi;
2340 size_t add;
2341 {
2342 size_t need;
2343
2344 need = dpi->len + add;
2345 while (need > dpi->alc)
2346 {
2347 size_t newalc;
2348 char *newbuf;
2349
2350 newalc = dpi->alc * 2;
2351 newbuf = realloc (dpi->buf, newalc);
2352 if (newbuf == NULL)
2353 {
2354 free (dpi->buf);
2355 dpi->buf = NULL;
2356 dpi->allocation_failure = 1;
2357 return;
2358 }
2359 dpi->buf = newbuf;
2360 dpi->alc = newalc;
2361 }
2362 }
2363
2364 /* Append a character to the print buffer. */
2365
2366 static void
2367 d_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 }
2379
2380 dpi->buf[dpi->len] = c;
2381 ++dpi->len;
2382 }
2383 }
2384
2385 /* Append a buffer to the print buffer. */
2386
2387 static void
2388 d_print_append_buffer (dpi, s, l)
2389 struct d_print_info *dpi;
2390 const char *s;
2391 size_t l;
2392 {
2393 if (dpi->buf != NULL)
2394 {
2395 if (dpi->len + l > dpi->alc)
2396 {
2397 d_print_resize (dpi, l);
2398 if (dpi->buf == NULL)
2399 return;
2400 }
2401
2402 memcpy (dpi->buf + dpi->len, s, l);
2403 dpi->len += l;
2404 }
2405 }
2406
2407 /* Indicate that an error occurred during printing. */
2408
2409 static void
2410 d_print_error (dpi)
2411 struct d_print_info *dpi;
2412 {
2413 free (dpi->buf);
2414 dpi->buf = NULL;
2415 }
2416
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. */
2421
2422 static char *
2423 d_print (options, dc, palc)
2424 int options;
2425 const struct d_comp *dc;
2426 size_t *palc;
2427 {
2428 struct d_print_info dpi;
2429
2430 dpi.options = options;
2431
2432 dpi.alc = 64;
2433 dpi.buf = malloc (dpi.alc);
2434 if (dpi.buf == NULL)
2435 {
2436 *palc = 1;
2437 return NULL;
2438 }
2439
2440 dpi.len = 0;
2441 dpi.templates = NULL;
2442 dpi.modifiers = NULL;
2443
2444 dpi.allocation_failure = 0;
2445
2446 d_print_comp (&dpi, dc);
2447
2448 d_append_char (&dpi, '\0');
2449
2450 if (dpi.buf != NULL)
2451 *palc = dpi.alc;
2452 else
2453 *palc = dpi.allocation_failure;
2454
2455 return dpi.buf;
2456 }
2457
2458 /* Subroutine to handle components. */
2459
2460 static void
2461 d_print_comp (dpi, dc)
2462 struct d_print_info *dpi;
2463 const struct d_comp *dc;
2464 {
2465 if (dc == NULL)
2466 {
2467 d_print_error (dpi);
2468 return;
2469 }
2470 if (d_print_saw_error (dpi))
2471 return;
2472
2473 switch (dc->type)
2474 {
2475 case D_COMP_NAME:
2476 d_print_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2477 return;
2478
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;
2484
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 }
2517
2518 d_print_comp (dpi, d_right (dc));
2519
2520 if (typed_name->type == D_COMP_TEMPLATE)
2521 dpi->templates = dpt.next;
2522
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 }
2530
2531 dpi->modifiers = dpm.next;
2532
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 }
2543
2544 return;
2545 }
2546
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;
2563
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 }
2588
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. */
2593
2594 hold_dpt = dpi->templates;
2595 dpi->templates = hold_dpt->next;
2596
2597 d_print_comp (dpi, d_left (a));
2598
2599 dpi->templates = hold_dpt;
2600
2601 return;
2602 }
2603
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;
2690
2691 dpm.next = dpi->modifiers;
2692 dpi->modifiers = &dpm;
2693 dpm.mod = dc;
2694 dpm.printed = 0;
2695
2696 d_print_comp (dpi, d_left (dc));
2697
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);
2702
2703 dpi->modifiers = dpm.next;
2704
2705 return;
2706 }
2707
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;
2714
2715 case D_COMP_VENDOR_TYPE:
2716 d_print_comp (dpi, d_left (dc));
2717 return;
2718
2719 case D_COMP_FUNCTION_TYPE:
2720 {
2721 if (d_left (dc) != NULL)
2722 {
2723 struct d_print_mod dpm;
2724
2725 /* We must pass this type down as a modifier in order to
2726 print it in the right location. */
2727
2728 dpm.next = dpi->modifiers;
2729 dpi->modifiers = &dpm;
2730 dpm.mod = dc;
2731 dpm.printed = 0;
2732
2733 d_print_comp (dpi, d_left (dc));
2734
2735 dpi->modifiers = dpm.next;
2736
2737 if (dpm.printed)
2738 return;
2739
2740 d_append_char (dpi, ' ');
2741 }
2742
2743 d_print_function_type (dpi, dc, dpi->modifiers);
2744
2745 return;
2746 }
2747
2748 case D_COMP_ARRAY_TYPE:
2749 {
2750 struct d_print_mod dpm;
2751
2752 /* We must pass this type down as a modifier in order to print
2753 multi-dimensional arrays correctly. */
2754
2755 dpm.next = dpi->modifiers;
2756 dpi->modifiers = &dpm;
2757 dpm.mod = dc;
2758 dpm.printed = 0;
2759
2760 d_print_comp (dpi, d_right (dc));
2761
2762 dpi->modifiers = dpm.next;
2763
2764 if (dpm.printed)
2765 return;
2766
2767 d_print_array_type (dpi, dc, dpi->modifiers);
2768
2769 return;
2770 }
2771
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 }
2803
2804 dpi->modifiers = dpm.next;
2805
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 }
2816
2817 return;
2818 }
2819
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;
2829
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 }
2841
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;
2846
2847 case D_COMP_CAST:
2848 d_append_string (dpi, "operator ");
2849 d_print_cast (dpi, dc);
2850 return;
2851
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
2856 {
2857 d_append_string (dpi, "((");
2858 d_print_cast (dpi, d_left (dc));
2859 d_append_char (dpi, ')');
2860 }
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)
2870 {
2871 d_print_error (dpi);
2872 return;
2873 }
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;
2934
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;
2952
2953 default:
2954 break;
2955 }
2956 }
2957
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;
2963
2964 default:
2965 d_print_error (dpi);
2966 return;
2967 }
2968 }
2969
2970 /* Print an identifier. */
2971
2972 static void
2973 d_print_identifier (dpi, name, len)
2974 struct d_print_info *dpi;
2975 const char *name;
2976 int len;
2977 {
2978 if ((dpi->options & DMGL_JAVA) == 0)
2979 d_append_buffer (dpi, name, len);
2980 else
2981 {
2982 const char *p;
2983 const char *end;
2984
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)
2991 {
2992 if (end - p > 3
2993 && p[0] == '_'
2994 && p[1] == '_'
2995 && p[2] == 'U')
2996 {
2997 unsigned long c;
2998 const char *q;
2999
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 }
3025
3026 d_append_char (dpi, *p);
3027 }
3028 }
3029 }
3030
3031 /* Print a list of modifiers. */
3032
3033 static void
3034 d_print_mod_list (dpi, mods)
3035 struct d_print_info *dpi;
3036 struct d_print_mod *mods;
3037 {
3038 if (mods == NULL || mods->printed || d_print_saw_error (dpi))
3039 return;
3040
3041 if (mods->mod->type == D_COMP_FUNCTION_TYPE)
3042 {
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 }
3053
3054 mods->printed = 1;
3055
3056 d_print_mod (dpi, mods->mod);
3057
3058 d_print_mod_list (dpi, mods->next);
3059 }
3060
3061 /* Print a modifier. */
3062
3063 static void
3064 d_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 }
3113
3114 /* Print a function type, except for the return type. */
3115
3116 static void
3117 d_print_function_type (dpi, dc, mods)
3118 struct d_print_info *dpi;
3119 const struct d_comp *dc;
3120 struct d_print_mod *mods;
3121 {
3122 if (mods != NULL)
3123 {
3124 int need_paren;
3125 int saw_mod;
3126 struct d_print_mod *p;
3127
3128 need_paren = 0;
3129 saw_mod = 0;
3130 for (p = mods; p != NULL; p = p->next)
3131 {
3132 if (p->printed)
3133 break;
3134
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 }
3155
3156 if (d_left (dc) != NULL && ! saw_mod)
3157 need_paren = 1;
3158
3159 if (need_paren)
3160 d_append_char (dpi, '(');
3161
3162 d_print_mod_list (dpi, mods);
3163
3164 if (need_paren)
3165 d_append_char (dpi, ')');
3166 }
3167
3168 d_append_char (dpi, '(');
3169
3170 if (d_right (dc) != NULL)
3171 d_print_comp (dpi, d_right (dc));
3172
3173 d_append_char (dpi, ')');
3174 }
3175
3176 /* Print an array type, except for the element type. */
3177
3178 static void
3179 d_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;
3185
3186 need_space = 1;
3187 if (mods != NULL)
3188 {
3189 int need_paren;
3190 struct d_print_mod *p;
3191
3192 need_paren = 0;
3193 for (p = mods; p != NULL; p = p->next)
3194 {
3195 if (p->printed)
3196 break;
3197
3198 if (p->mod->type == D_COMP_ARRAY_TYPE)
3199 {
3200 need_space = 0;
3201 break;
3202 }
3203 else
3204 {
3205 need_paren = 1;
3206 need_space = 1;
3207 break;
3208 }
3209 }
3210
3211 if (need_paren)
3212 d_append_string (dpi, " (");
3213
3214 d_print_mod_list (dpi, mods);
3215
3216 if (need_paren)
3217 d_append_char (dpi, ')');
3218 }
3219
3220 if (need_space)
3221 d_append_char (dpi, ' ');
3222
3223 d_append_char (dpi, '[');
3224
3225 if (d_left (dc) != NULL)
3226 d_print_comp (dpi, d_left (dc));
3227
3228 d_append_char (dpi, ']');
3229 }
3230
3231 /* Print an operator in an expression. */
3232
3233 static void
3234 d_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);
3242 }
3243
3244 /* Print a cast. */
3245
3246 static void
3247 d_print_cast (dpi, dc)
3248 struct d_print_info *dpi;
3249 const struct d_comp *dc;
3250 {
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;
3256
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. */
3261
3262 dpt.next = dpi->templates;
3263 dpi->templates = &dpt;
3264 dpt.template = d_left (dc);
3265
3266 d_print_comp (dpi, d_left (d_left (dc)));
3267
3268 dpi->templates = dpt.next;
3269
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, '>');
3277 }
3278 }
3279
3280 /* Initialize the information structure we use to pass around
3281 information. */
3282
3283 static int
3284 d_init_info (mangled, options, len, di)
3285 const char *mangled;
3286 int options;
3287 size_t len;
3288 struct d_info *di;
3289 {
3290 di->s = mangled;
3291 di->options = options;
3292
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)
3314 {
3315 if (di->comps != NULL)
3316 free (di->comps);
3317 if (di->subs != NULL)
3318 free (di->subs);
3319 return 0;
3320 }
3321
3322 return 1;
3323 }
3324
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. */
3331
3332 static char *
3333 d_demangle (mangled, options, palc)
3334 const char* mangled;
3335 int options;
3336 size_t *palc;
3337 {
3338 size_t len;
3339 int type;
3340 struct d_info di;
3341 struct d_comp *dc;
3342 char *ret;
3343
3344 *palc = 0;
3345
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;
3356
3357 r = malloc (40 + len - 11);
3358 if (r == NULL)
3359 *palc = 1;
3360 else
3361 {
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);
3367 }
3368 return r;
3369 }
3370 else
3371 {
3372 if ((options & DMGL_TYPES) == 0)
3373 return NULL;
3374 type = 1;
3375 }
3376
3377 if (! d_init_info (mangled, options, len, &di))
3378 {
3379 *palc = 1;
3380 return NULL;
3381 }
3382
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;
3397
3398 ret = NULL;
3399 if (dc != NULL)
3400 ret = d_print (options, dc, palc);
3401
3402 free (di.comps);
3403
3404 return ret;
3405 }
3406
3407 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3408
3409 extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
3410
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.
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
3419 is placed in a region of memory allocated with malloc.
3420
3421 If LENGTH is non-NULL, the length of the buffer conaining the
3422 demangled name, is placed in *LENGTH.
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
3426 responsible for deallocating this memory using free.
3427
3428 *STATUS is set to one of the following values:
3429 0: The demangling operation succeeded.
3430 -1: A memory allocation failure occurred.
3431 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3432 -3: One of the arguments is invalid.
3433
3434 The demangling is performed using the C++ ABI mangling rules, with
3435 GNU extensions. */
3436
3437 char *
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 {
3444 char *demangled;
3445 size_t alc;
3446
3447 if (status == NULL)
3448 return NULL;
3449
3450 if (mangled_name == NULL)
3451 {
3452 *status = -3;
3453 return NULL;
3454 }
3455
3456 if (output_buffer != NULL && length == NULL)
3457 {
3458 *status = -3;
3459 return NULL;
3460 }
3461
3462 demangled = d_demangle (mangled_name, DMGL_TYPES, &alc);
3463
3464 if (demangled == NULL)
3465 {
3466 if (alc == 1)
3467 *status = -1;
3468 else
3469 *status = -2;
3470 return NULL;
3471 }
3472
3473 if (output_buffer == NULL)
3474 {
3475 if (length != NULL)
3476 *length = alc;
3477 }
3478 else
3479 {
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 }
3491 }
3492
3493 *status = 0;
3494
3495 return demangled;
3496 }
3497
3498 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3499
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. */
3503
3504 char *
3505 cplus_demangle_v3 (mangled, options)
3506 const char* mangled;
3507 int options;
3508 {
3509 size_t alc;
3510
3511 return d_demangle (mangled, options, &alc);
3512 }
3513
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
3521 char *
3522 java_demangle_v3 (mangled)
3523 const char* mangled;
3524 {
3525 size_t alc;
3526 char *demangled;
3527 int nesting;
3528 char *from;
3529 char *to;
3530
3531 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
3532
3533 if (demangled == NULL)
3534 return NULL;
3535
3536 nesting = 0;
3537 from = demangled;
3538 to = from;
3539 while (*from != '\0')
3540 {
3541 if (strncmp (from, "JArray<", 7) == 0)
3542 {
3543 from += 7;
3544 ++nesting;
3545 }
3546 else if (nesting > 0 && *from == '>')
3547 {
3548 while (to > demangled && to[-1] == ' ')
3549 --to;
3550 *to++ = '[';
3551 *to++ = ']';
3552 --nesting;
3553 ++from;
3554 }
3555 else
3556 *to++ = *from++;
3557 }
3558
3559 *to = '\0';
3560
3561 return demangled;
3562 }
3563
3564 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3565
3566 #ifndef IN_GLIBCPP_V3
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
3572 static int
3573 is_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;
3577 {
3578 struct d_info di;
3579 struct d_comp *dc;
3580
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))
3585 return 0;
3586
3587 dc = d_mangled_name (&di);
3588
3589 if (dc == NULL)
3590 return 0;
3591
3592 while (dc != NULL)
3593 {
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 }
3616 }
3617
3618 return 0;
3619 }
3620
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. */
3623
3624 enum gnu_v3_ctor_kinds
3625 is_gnu_v3_mangled_ctor (name)
3626 const char *name;
3627 {
3628 enum gnu_v3_ctor_kinds ctor_kind;
3629 enum gnu_v3_dtor_kinds dtor_kind;
3630
3631 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
3632 return (enum gnu_v3_ctor_kinds) 0;
3633 return ctor_kind;
3634 }
3635
3636
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
3640 enum gnu_v3_dtor_kinds
3641 is_gnu_v3_mangled_dtor (name)
3642 const char *name;
3643 {
3644 enum gnu_v3_ctor_kinds ctor_kind;
3645 enum gnu_v3_dtor_kinds dtor_kind;
3646
3647 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
3648 return (enum gnu_v3_dtor_kinds) 0;
3649 return dtor_kind;
3650 }
3651
3652 #endif /* IN_GLIBCPP_V3 */
3653
3654 #ifdef STANDALONE_DEMANGLER
3655
3656 #include "getopt.h"
3657 #include "dyn-string.h"
3658
3659 static void print_usage PARAMS ((FILE* fp, int exit_value));
3660
3661 #define IS_ALPHA(CHAR) \
3662 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3663 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
3664
3665 /* Non-zero if CHAR is a character than can occur in a mangled name. */
3666 #define is_mangled_char(CHAR) \
3667 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3668 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3669
3670 /* The name of this program, as invoked. */
3671 const char* program_name;
3672
3673 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
3674
3675 static void
3676 print_usage (fp, exit_value)
3677 FILE* fp;
3678 int exit_value;
3679 {
3680 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
3681 fprintf (fp, "Options:\n");
3682 fprintf (fp, " -h,--help Display this message.\n");
3683 fprintf (fp, " -p,--no-params Don't display function parameters\n");
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. */
3691 static const struct option long_options[] =
3692 {
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 },
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
3704 int
3705 main (argc, argv)
3706 int argc;
3707 char *argv[];
3708 {
3709 int i;
3710 int opt_char;
3711 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
3712
3713 /* Use the program name of this program, as invoked. */
3714 program_name = argv[0];
3715
3716 /* Parse options. */
3717 do
3718 {
3719 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
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
3730 case 'p':
3731 options &= ~ DMGL_PARAMS;
3732 break;
3733
3734 case 'v':
3735 options |= DMGL_VERBOSE;
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);
3745 char *s;
3746
3747 /* Read all of input. */
3748 while (!feof (stdin))
3749 {
3750 char c;
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
3763 if (dyn_string_length (mangled) > 0)
3764 {
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);
3780 }
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);
3786 }
3787
3788 dyn_string_delete (mangled);
3789 }
3790 else
3791 /* Demangle command line arguments. */
3792 {
3793 /* Loop over command line arguments. */
3794 for (i = optind; i < argc; ++i)
3795 {
3796 char *s;
3797
3798 /* Attempt to demangle. */
3799 s = cplus_demangle_v3 (argv[i], options);
3800
3801 /* If it worked, print the demangled name. */
3802 if (s != NULL)
3803 {
3804 printf ("%s\n", s);
3805 free (s);
3806 }
3807 else
3808 fprintf (stderr, "Failed: %s\n", argv[i]);
3809 }
3810 }
3811
3812 return 0;
3813 }
3814
3815 #endif /* STANDALONE_DEMANGLER */
This page took 0.108805 seconds and 5 git commands to generate.