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