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