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