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