]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/cplus-dem.c
Sync libiberty sources with gcc mainline.
[thirdparty/binutils-gdb.git] / libiberty / cplus-dem.c
CommitLineData
252b5132 1/* Demangler for GNU C++
74bcd529 2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
59e11e17 3 2000, 2001, 2002, 2003, 2004, 2010 Free Software Foundation, Inc.
252b5132
RH
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
7
8This file is part of the libiberty library.
9Libiberty is free software; you can redistribute it and/or
10modify it under the terms of the GNU Library General Public
11License as published by the Free Software Foundation; either
12version 2 of the License, or (at your option) any later version.
13
a01fefeb
DD
14In addition to the permissions in the GNU Library General Public
15License, the Free Software Foundation gives you unlimited permission
16to link the compiled version of this file into combinations with other
17programs, and to distribute those combinations without any restriction
18coming from the use of this file. (The Library Public License
19restrictions do apply in other respects; for example, they cover
20modification of the file, and distribution when not linked into a
21combined executable.)
22
252b5132
RH
23Libiberty is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26Library General Public License for more details.
27
28You should have received a copy of the GNU Library General Public
29License along with libiberty; see the file COPYING.LIB. If
979c05d3
NC
30not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
31Boston, MA 02110-1301, USA. */
252b5132
RH
32
33/* This file exports two functions; cplus_mangle_opname and cplus_demangle.
34
35 This file imports xmalloc and xrealloc, which are like malloc and
36 realloc except that they generate a fatal error if there is no
37 available memory. */
38
39/* This file lives in both GCC and libiberty. When making changes, please
40 try not to break either. */
41
42#ifdef HAVE_CONFIG_H
43#include "config.h"
44#endif
45
ac424eb3
DD
46#include "safe-ctype.h"
47
252b5132
RH
48#include <sys/types.h>
49#include <string.h>
50#include <stdio.h>
51
52#ifdef HAVE_STDLIB_H
53#include <stdlib.h>
54#else
6b357a9d
DD
55void * malloc ();
56void * realloc ();
252b5132
RH
57#endif
58
fa3fcee7
NC
59#ifdef HAVE_LIMITS_H
60#include <limits.h>
61#endif
62#ifndef INT_MAX
63# define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
64#endif
65
252b5132
RH
66#include <demangle.h>
67#undef CURRENT_DEMANGLING_STYLE
68#define CURRENT_DEMANGLING_STYLE work->options
69
70#include "libiberty.h"
71
0c0a36a4
ILT
72#define min(X,Y) (((X) < (Y)) ? (X) : (Y))
73
74/* A value at least one greater than the maximum number of characters
75 that will be output when using the `%d' format with `printf'. */
76#define INTBUF_SIZE 32
77
9334f9c6 78extern void fancy_abort (void) ATTRIBUTE_NORETURN;
0c0a36a4 79
252b5132
RH
80/* In order to allow a single demangler executable to demangle strings
81 using various common values of CPLUS_MARKER, as well as any specific
82 one set at compile time, we maintain a string containing all the
83 commonly used ones, and check to see if the marker we are looking for
84 is in that string. CPLUS_MARKER is usually '$' on systems where the
85 assembler can deal with that. Where the assembler can't, it's usually
86 '.' (but on many systems '.' is used for other things). We put the
87 current defined CPLUS_MARKER first (which defaults to '$'), followed
88 by the next most common value, followed by an explicit '$' in case
89 the value of CPLUS_MARKER is not '$'.
90
91 We could avoid this if we could just get g++ to tell us what the actual
92 cplus marker character is as part of the debug information, perhaps by
93 ensuring that it is the character that terminates the gcc<n>_compiled
94 marker symbol (FIXME). */
95
96#if !defined (CPLUS_MARKER)
97#define CPLUS_MARKER '$'
98#endif
99
e49a569c 100enum demangling_styles current_demangling_style = auto_demangling;
252b5132
RH
101
102static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
103
104static char char_str[2] = { '\000', '\000' };
105
106void
9334f9c6 107set_cplus_marker_for_demangling (int ch)
252b5132
RH
108{
109 cplus_markers[0] = ch;
110}
111
112typedef struct string /* Beware: these aren't required to be */
113{ /* '\0' terminated. */
114 char *b; /* pointer to start of string */
115 char *p; /* pointer after last character */
116 char *e; /* pointer after end of allocated space */
117} string;
118
119/* Stuff that is shared between sub-routines.
120 Using a shared structure allows cplus_demangle to be reentrant. */
121
122struct work_stuff
123{
124 int options;
125 char **typevec;
126 char **ktypevec;
127 char **btypevec;
128 int numk;
129 int numb;
130 int ksize;
131 int bsize;
132 int ntypes;
133 int typevec_size;
134 int constructor;
135 int destructor;
136 int static_type; /* A static member function */
137 int temp_start; /* index in demangled to start of template args */
138 int type_quals; /* The type qualifiers. */
139 int dllimported; /* Symbol imported from a PE DLL */
140 char **tmpl_argvec; /* Template function arguments. */
141 int ntmpl_args; /* The number of template function arguments. */
142 int forgetting_types; /* Nonzero if we are not remembering the types
143 we see. */
144 string* previous_argument; /* The last function argument demangled. */
145 int nrepeats; /* The number of times to repeat the previous
146 argument. */
41f225de
NC
147 int *proctypevec; /* Indices of currently processed remembered typevecs. */
148 int proctypevec_size;
149 int nproctypes;
252b5132
RH
150};
151
152#define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
153#define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
154
155static const struct optable
156{
e6450fe5
DD
157 const char *const in;
158 const char *const out;
159 const int flags;
252b5132
RH
160} optable[] = {
161 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
162 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
163 {"new", " new", 0}, /* old (1.91, and 1.x) */
164 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
165 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
166 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
167 {"as", "=", DMGL_ANSI}, /* ansi */
168 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
169 {"eq", "==", DMGL_ANSI}, /* old, ansi */
170 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
171 {"gt", ">", DMGL_ANSI}, /* old, ansi */
172 {"le", "<=", DMGL_ANSI}, /* old, ansi */
173 {"lt", "<", DMGL_ANSI}, /* old, ansi */
174 {"plus", "+", 0}, /* old */
175 {"pl", "+", DMGL_ANSI}, /* ansi */
176 {"apl", "+=", DMGL_ANSI}, /* ansi */
177 {"minus", "-", 0}, /* old */
178 {"mi", "-", DMGL_ANSI}, /* ansi */
179 {"ami", "-=", DMGL_ANSI}, /* ansi */
180 {"mult", "*", 0}, /* old */
181 {"ml", "*", DMGL_ANSI}, /* ansi */
182 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
183 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
184 {"convert", "+", 0}, /* old (unary +) */
185 {"negate", "-", 0}, /* old (unary -) */
186 {"trunc_mod", "%", 0}, /* old */
187 {"md", "%", DMGL_ANSI}, /* ansi */
188 {"amd", "%=", DMGL_ANSI}, /* ansi */
189 {"trunc_div", "/", 0}, /* old */
190 {"dv", "/", DMGL_ANSI}, /* ansi */
191 {"adv", "/=", DMGL_ANSI}, /* ansi */
192 {"truth_andif", "&&", 0}, /* old */
193 {"aa", "&&", DMGL_ANSI}, /* ansi */
194 {"truth_orif", "||", 0}, /* old */
195 {"oo", "||", DMGL_ANSI}, /* ansi */
196 {"truth_not", "!", 0}, /* old */
197 {"nt", "!", DMGL_ANSI}, /* ansi */
198 {"postincrement","++", 0}, /* old */
199 {"pp", "++", DMGL_ANSI}, /* ansi */
200 {"postdecrement","--", 0}, /* old */
201 {"mm", "--", DMGL_ANSI}, /* ansi */
202 {"bit_ior", "|", 0}, /* old */
203 {"or", "|", DMGL_ANSI}, /* ansi */
204 {"aor", "|=", DMGL_ANSI}, /* ansi */
205 {"bit_xor", "^", 0}, /* old */
206 {"er", "^", DMGL_ANSI}, /* ansi */
207 {"aer", "^=", DMGL_ANSI}, /* ansi */
208 {"bit_and", "&", 0}, /* old */
209 {"ad", "&", DMGL_ANSI}, /* ansi */
210 {"aad", "&=", DMGL_ANSI}, /* ansi */
211 {"bit_not", "~", 0}, /* old */
212 {"co", "~", DMGL_ANSI}, /* ansi */
213 {"call", "()", 0}, /* old */
214 {"cl", "()", DMGL_ANSI}, /* ansi */
215 {"alshift", "<<", 0}, /* old */
216 {"ls", "<<", DMGL_ANSI}, /* ansi */
217 {"als", "<<=", DMGL_ANSI}, /* ansi */
218 {"arshift", ">>", 0}, /* old */
219 {"rs", ">>", DMGL_ANSI}, /* ansi */
220 {"ars", ">>=", DMGL_ANSI}, /* ansi */
221 {"component", "->", 0}, /* old */
222 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
223 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
224 {"indirect", "*", 0}, /* old */
225 {"method_call", "->()", 0}, /* old */
226 {"addr", "&", 0}, /* old (unary &) */
227 {"array", "[]", 0}, /* old */
228 {"vc", "[]", DMGL_ANSI}, /* ansi */
229 {"compound", ", ", 0}, /* old */
230 {"cm", ", ", DMGL_ANSI}, /* ansi */
231 {"cond", "?:", 0}, /* old */
232 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
233 {"max", ">?", 0}, /* old */
234 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
235 {"min", "<?", 0}, /* old */
236 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
237 {"nop", "", 0}, /* old (for operator=) */
238 {"rm", "->*", DMGL_ANSI}, /* ansi */
239 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
240};
241
242/* These values are used to indicate the various type varieties.
243 They are all non-zero so that they can be used as `success'
244 values. */
245typedef enum type_kind_t
246{
247 tk_none,
248 tk_pointer,
249 tk_reference,
fa3fcee7 250 tk_rvalue_reference,
252b5132
RH
251 tk_integral,
252 tk_bool,
253 tk_char,
254 tk_real
255} type_kind_t;
256
e6450fe5 257const struct demangler_engine libiberty_demanglers[] =
eb383413 258{
2da4c07f
RH
259 {
260 NO_DEMANGLING_STYLE_STRING,
261 no_demangling,
262 "Demangling disabled"
263 }
264 ,
eb383413
L
265 {
266 AUTO_DEMANGLING_STYLE_STRING,
267 auto_demangling,
268 "Automatic selection based on executable"
269 }
270 ,
271 {
272 GNU_DEMANGLING_STYLE_STRING,
273 gnu_demangling,
274 "GNU (g++) style demangling"
275 }
276 ,
277 {
278 LUCID_DEMANGLING_STYLE_STRING,
279 lucid_demangling,
280 "Lucid (lcc) style demangling"
281 }
282 ,
283 {
284 ARM_DEMANGLING_STYLE_STRING,
285 arm_demangling,
286 "ARM style demangling"
287 }
288 ,
289 {
290 HP_DEMANGLING_STYLE_STRING,
291 hp_demangling,
292 "HP (aCC) style demangling"
293 }
294 ,
295 {
296 EDG_DEMANGLING_STYLE_STRING,
297 edg_demangling,
298 "EDG style demangling"
299 }
300 ,
301 {
e49a569c
DD
302 GNU_V3_DEMANGLING_STYLE_STRING,
303 gnu_v3_demangling,
304 "GNU (g++) V3 ABI-style demangling"
eb383413
L
305 }
306 ,
16e85745
HPN
307 {
308 JAVA_DEMANGLING_STYLE_STRING,
309 java_demangling,
310 "Java style demangling"
311 }
312 ,
313 {
314 GNAT_DEMANGLING_STYLE_STRING,
315 gnat_demangling,
316 "GNAT style demangling"
317 }
318 ,
b55f9678
IB
319 {
320 DLANG_DEMANGLING_STYLE_STRING,
321 dlang_demangling,
322 "DLANG style demangling"
323 }
324 ,
eb383413
L
325 {
326 NULL, unknown_demangling, NULL
327 }
328};
329
252b5132 330#define STRING_EMPTY(str) ((str) -> b == (str) -> p)
252b5132
RH
331#define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
332 string_append(str, " ");}
333#define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
334
335/* The scope separator appropriate for the language being demangled. */
336
337#define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
338
339#define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
340#define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
341
342/* Prototypes for local functions */
343
9334f9c6 344static void delete_work_stuff (struct work_stuff *);
e8865c28 345
9334f9c6 346static void delete_non_B_K_work_stuff (struct work_stuff *);
e8865c28 347
9334f9c6 348static char *mop_up (struct work_stuff *, string *, int);
252b5132 349
9334f9c6 350static void squangle_mop_up (struct work_stuff *);
252b5132 351
9334f9c6 352static void work_stuff_copy_to_from (struct work_stuff *, struct work_stuff *);
e8865c28 353
252b5132
RH
354#if 0
355static int
9334f9c6 356demangle_method_args (struct work_stuff *, const char **, string *);
252b5132
RH
357#endif
358
359static char *
9334f9c6 360internal_cplus_demangle (struct work_stuff *, const char *);
252b5132
RH
361
362static int
9334f9c6
DD
363demangle_template_template_parm (struct work_stuff *work,
364 const char **, string *);
252b5132
RH
365
366static int
9334f9c6
DD
367demangle_template (struct work_stuff *work, const char **, string *,
368 string *, int, int);
252b5132
RH
369
370static int
9334f9c6
DD
371arm_pt (struct work_stuff *, const char *, int, const char **,
372 const char **);
252b5132
RH
373
374static int
9334f9c6 375demangle_class_name (struct work_stuff *, const char **, string *);
252b5132
RH
376
377static int
9334f9c6
DD
378demangle_qualified (struct work_stuff *, const char **, string *,
379 int, int);
252b5132 380
9334f9c6 381static int demangle_class (struct work_stuff *, const char **, string *);
252b5132 382
9334f9c6 383static int demangle_fund_type (struct work_stuff *, const char **, string *);
252b5132 384
9334f9c6 385static int demangle_signature (struct work_stuff *, const char **, string *);
252b5132 386
9334f9c6 387static int demangle_prefix (struct work_stuff *, const char **, string *);
252b5132 388
9334f9c6 389static int gnu_special (struct work_stuff *, const char **, string *);
252b5132 390
9334f9c6 391static int arm_special (const char **, string *);
252b5132 392
9334f9c6 393static void string_need (string *, int);
252b5132 394
9334f9c6 395static void string_delete (string *);
252b5132
RH
396
397static void
9334f9c6 398string_init (string *);
252b5132 399
9334f9c6 400static void string_clear (string *);
252b5132
RH
401
402#if 0
9334f9c6 403static int string_empty (string *);
252b5132
RH
404#endif
405
9334f9c6 406static void string_append (string *, const char *);
252b5132 407
9334f9c6 408static void string_appends (string *, string *);
252b5132 409
9334f9c6 410static void string_appendn (string *, const char *, int);
252b5132 411
9334f9c6 412static void string_prepend (string *, const char *);
252b5132 413
9334f9c6 414static void string_prependn (string *, const char *, int);
252b5132 415
9334f9c6 416static void string_append_template_idx (string *, int);
0c0a36a4 417
9334f9c6 418static int get_count (const char **, int *);
252b5132 419
9334f9c6 420static int consume_count (const char **);
252b5132 421
9334f9c6 422static int consume_count_with_underscores (const char**);
252b5132 423
9334f9c6 424static int demangle_args (struct work_stuff *, const char **, string *);
252b5132 425
9334f9c6 426static int demangle_nested_args (struct work_stuff*, const char**, string*);
252b5132 427
9334f9c6 428static int do_type (struct work_stuff *, const char **, string *);
252b5132 429
9334f9c6 430static int do_arg (struct work_stuff *, const char **, string *);
252b5132 431
b4b41ceb 432static int
9334f9c6
DD
433demangle_function_name (struct work_stuff *, const char **, string *,
434 const char *);
252b5132 435
e8865c28 436static int
9334f9c6
DD
437iterate_demangle_function (struct work_stuff *,
438 const char **, string *, const char *);
e8865c28 439
9334f9c6 440static void remember_type (struct work_stuff *, const char *, int);
252b5132 441
41f225de
NC
442static void push_processed_type (struct work_stuff *, int);
443
444static void pop_processed_type (struct work_stuff *);
445
9334f9c6 446static void remember_Btype (struct work_stuff *, const char *, int, int);
252b5132 447
9334f9c6 448static int register_Btype (struct work_stuff *);
252b5132 449
9334f9c6 450static void remember_Ktype (struct work_stuff *, const char *, int);
252b5132 451
9334f9c6 452static void forget_types (struct work_stuff *);
252b5132 453
9334f9c6 454static void forget_B_and_K_types (struct work_stuff *);
252b5132 455
9334f9c6 456static void string_prepends (string *, string *);
252b5132
RH
457
458static int
9334f9c6
DD
459demangle_template_value_parm (struct work_stuff*, const char**,
460 string*, type_kind_t);
252b5132
RH
461
462static int
9334f9c6 463do_hpacc_template_const_value (struct work_stuff *, const char **, string *);
252b5132
RH
464
465static int
9334f9c6 466do_hpacc_template_literal (struct work_stuff *, const char **, string *);
252b5132 467
9334f9c6 468static int snarf_numeric_literal (const char **, string *);
252b5132
RH
469
470/* There is a TYPE_QUAL value for each type qualifier. They can be
471 combined by bitwise-or to form the complete set of qualifiers for a
472 type. */
473
474#define TYPE_UNQUALIFIED 0x0
475#define TYPE_QUAL_CONST 0x1
476#define TYPE_QUAL_VOLATILE 0x2
477#define TYPE_QUAL_RESTRICT 0x4
478
9334f9c6 479static int code_for_qualifier (int);
252b5132 480
9334f9c6 481static const char* qualifier_string (int);
252b5132 482
9334f9c6 483static const char* demangle_qualifier (int);
252b5132 484
9334f9c6
DD
485static int demangle_expression (struct work_stuff *, const char **, string *,
486 type_kind_t);
0c0a36a4
ILT
487
488static int
9334f9c6 489demangle_integral_value (struct work_stuff *, const char **, string *);
0c0a36a4
ILT
490
491static int
9334f9c6 492demangle_real_value (struct work_stuff *, const char **, string *);
0c0a36a4
ILT
493
494static void
9334f9c6 495demangle_arm_hp_template (struct work_stuff *, const char **, int, string *);
0c0a36a4
ILT
496
497static void
9334f9c6 498recursively_demangle (struct work_stuff *, const char **, string *, int);
0c0a36a4 499
252b5132
RH
500/* Translate count to integer, consuming tokens in the process.
501 Conversion terminates on the first non-digit character.
502
503 Trying to consume something that isn't a count results in no
504 consumption of input and a return of -1.
505
506 Overflow consumes the rest of the digits, and returns -1. */
507
508static int
9334f9c6 509consume_count (const char **type)
252b5132
RH
510{
511 int count = 0;
512
ac424eb3 513 if (! ISDIGIT ((unsigned char)**type))
252b5132
RH
514 return -1;
515
ac424eb3 516 while (ISDIGIT ((unsigned char)**type))
252b5132
RH
517 {
518 count *= 10;
519
520 /* Check for overflow.
521 We assume that count is represented using two's-complement;
522 no power of two is divisible by ten, so if an overflow occurs
523 when multiplying by ten, the result will not be a multiple of
524 ten. */
525 if ((count % 10) != 0)
526 {
ac424eb3 527 while (ISDIGIT ((unsigned char) **type))
252b5132
RH
528 (*type)++;
529 return -1;
530 }
531
532 count += **type - '0';
533 (*type)++;
534 }
535
ae533646
DD
536 if (count < 0)
537 count = -1;
538
252b5132
RH
539 return (count);
540}
541
542
543/* Like consume_count, but for counts that are preceded and followed
544 by '_' if they are greater than 10. Also, -1 is returned for
545 failure, since 0 can be a valid value. */
546
547static int
9334f9c6 548consume_count_with_underscores (const char **mangled)
252b5132
RH
549{
550 int idx;
551
552 if (**mangled == '_')
553 {
554 (*mangled)++;
ac424eb3 555 if (!ISDIGIT ((unsigned char)**mangled))
252b5132
RH
556 return -1;
557
558 idx = consume_count (mangled);
559 if (**mangled != '_')
560 /* The trailing underscore was missing. */
561 return -1;
562
563 (*mangled)++;
564 }
565 else
566 {
567 if (**mangled < '0' || **mangled > '9')
568 return -1;
569
570 idx = **mangled - '0';
571 (*mangled)++;
572 }
573
574 return idx;
575}
576
577/* C is the code for a type-qualifier. Return the TYPE_QUAL
578 corresponding to this qualifier. */
579
580static int
9334f9c6 581code_for_qualifier (int c)
252b5132
RH
582{
583 switch (c)
584 {
585 case 'C':
586 return TYPE_QUAL_CONST;
587
588 case 'V':
589 return TYPE_QUAL_VOLATILE;
590
591 case 'u':
592 return TYPE_QUAL_RESTRICT;
593
594 default:
595 break;
596 }
597
598 /* C was an invalid qualifier. */
599 abort ();
600}
601
602/* Return the string corresponding to the qualifiers given by
603 TYPE_QUALS. */
604
605static const char*
9334f9c6 606qualifier_string (int type_quals)
252b5132
RH
607{
608 switch (type_quals)
609 {
610 case TYPE_UNQUALIFIED:
611 return "";
612
613 case TYPE_QUAL_CONST:
614 return "const";
615
616 case TYPE_QUAL_VOLATILE:
617 return "volatile";
618
619 case TYPE_QUAL_RESTRICT:
620 return "__restrict";
621
622 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
623 return "const volatile";
624
625 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
626 return "const __restrict";
627
628 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
629 return "volatile __restrict";
630
631 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
632 return "const volatile __restrict";
633
634 default:
635 break;
636 }
637
638 /* TYPE_QUALS was an invalid qualifier set. */
639 abort ();
640}
641
642/* C is the code for a type-qualifier. Return the string
643 corresponding to this qualifier. This function should only be
644 called with a valid qualifier code. */
645
646static const char*
9334f9c6 647demangle_qualifier (int c)
252b5132
RH
648{
649 return qualifier_string (code_for_qualifier (c));
650}
651
652int
9334f9c6 653cplus_demangle_opname (const char *opname, char *result, int options)
252b5132
RH
654{
655 int len, len1, ret;
656 string type;
657 struct work_stuff work[1];
658 const char *tem;
659
660 len = strlen(opname);
661 result[0] = '\0';
662 ret = 0;
663 memset ((char *) work, 0, sizeof (work));
664 work->options = options;
665
666 if (opname[0] == '_' && opname[1] == '_'
667 && opname[2] == 'o' && opname[3] == 'p')
668 {
669 /* ANSI. */
670 /* type conversion operator. */
671 tem = opname + 4;
672 if (do_type (work, &tem, &type))
673 {
674 strcat (result, "operator ");
675 strncat (result, type.b, type.p - type.b);
676 string_delete (&type);
677 ret = 1;
678 }
679 }
680 else if (opname[0] == '_' && opname[1] == '_'
ac424eb3
DD
681 && ISLOWER((unsigned char)opname[2])
682 && ISLOWER((unsigned char)opname[3]))
252b5132
RH
683 {
684 if (opname[4] == '\0')
685 {
686 /* Operator. */
687 size_t i;
74bcd529 688 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
689 {
690 if (strlen (optable[i].in) == 2
691 && memcmp (optable[i].in, opname + 2, 2) == 0)
692 {
693 strcat (result, "operator");
694 strcat (result, optable[i].out);
695 ret = 1;
696 break;
697 }
698 }
699 }
700 else
701 {
702 if (opname[2] == 'a' && opname[5] == '\0')
703 {
704 /* Assignment. */
705 size_t i;
74bcd529 706 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
707 {
708 if (strlen (optable[i].in) == 3
709 && memcmp (optable[i].in, opname + 2, 3) == 0)
710 {
711 strcat (result, "operator");
712 strcat (result, optable[i].out);
713 ret = 1;
714 break;
715 }
716 }
717 }
718 }
719 }
720 else if (len >= 3
721 && opname[0] == 'o'
722 && opname[1] == 'p'
723 && strchr (cplus_markers, opname[2]) != NULL)
724 {
725 /* see if it's an assignment expression */
726 if (len >= 10 /* op$assign_ */
727 && memcmp (opname + 3, "assign_", 7) == 0)
728 {
729 size_t i;
74bcd529 730 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
731 {
732 len1 = len - 10;
733 if ((int) strlen (optable[i].in) == len1
734 && memcmp (optable[i].in, opname + 10, len1) == 0)
735 {
736 strcat (result, "operator");
737 strcat (result, optable[i].out);
738 strcat (result, "=");
739 ret = 1;
740 break;
741 }
742 }
743 }
744 else
745 {
746 size_t i;
74bcd529 747 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
748 {
749 len1 = len - 3;
750 if ((int) strlen (optable[i].in) == len1
751 && memcmp (optable[i].in, opname + 3, len1) == 0)
752 {
753 strcat (result, "operator");
754 strcat (result, optable[i].out);
755 ret = 1;
756 break;
757 }
758 }
759 }
760 }
761 else if (len >= 5 && memcmp (opname, "type", 4) == 0
762 && strchr (cplus_markers, opname[4]) != NULL)
763 {
764 /* type conversion operator */
765 tem = opname + 5;
766 if (do_type (work, &tem, &type))
767 {
768 strcat (result, "operator ");
769 strncat (result, type.b, type.p - type.b);
770 string_delete (&type);
771 ret = 1;
772 }
773 }
774 squangle_mop_up (work);
775 return ret;
776
777}
eb383413 778
252b5132
RH
779/* Takes operator name as e.g. "++" and returns mangled
780 operator name (e.g. "postincrement_expr"), or NULL if not found.
781
782 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
783 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
784
785const char *
9334f9c6 786cplus_mangle_opname (const char *opname, int options)
252b5132
RH
787{
788 size_t i;
789 int len;
790
791 len = strlen (opname);
74bcd529 792 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
793 {
794 if ((int) strlen (optable[i].out) == len
795 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
796 && memcmp (optable[i].out, opname, len) == 0)
797 return optable[i].in;
798 }
799 return (0);
800}
801
eb383413
L
802/* Add a routine to set the demangling style to be sure it is valid and
803 allow for any demangler initialization that maybe necessary. */
804
805enum demangling_styles
9334f9c6 806cplus_demangle_set_style (enum demangling_styles style)
eb383413 807{
e6450fe5 808 const struct demangler_engine *demangler = libiberty_demanglers;
eb383413
L
809
810 for (; demangler->demangling_style != unknown_demangling; ++demangler)
811 if (style == demangler->demangling_style)
812 {
813 current_demangling_style = style;
814 return current_demangling_style;
815 }
816
817 return unknown_demangling;
818}
819
820/* Do string name to style translation */
821
822enum demangling_styles
9334f9c6 823cplus_demangle_name_to_style (const char *name)
eb383413 824{
e6450fe5 825 const struct demangler_engine *demangler = libiberty_demanglers;
eb383413
L
826
827 for (; demangler->demangling_style != unknown_demangling; ++demangler)
828 if (strcmp (name, demangler->demangling_style_name) == 0)
829 return demangler->demangling_style;
830
831 return unknown_demangling;
832}
833
252b5132
RH
834/* char *cplus_demangle (const char *mangled, int options)
835
836 If MANGLED is a mangled function name produced by GNU C++, then
5d852400 837 a pointer to a @code{malloc}ed string giving a C++ representation
252b5132
RH
838 of the name will be returned; otherwise NULL will be returned.
839 It is the caller's responsibility to free the string which
840 is returned.
841
842 The OPTIONS arg may contain one or more of the following bits:
843
844 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
845 included.
846 DMGL_PARAMS Function parameters are included.
847
848 For example,
849
850 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
851 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
852 cplus_demangle ("foo__1Ai", 0) => "A::foo"
853
854 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
855 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
856 cplus_demangle ("foo__1Afe", 0) => "A::foo"
857
858 Note that any leading underscores, or other such characters prepended by
859 the compilation system, are presumed to have already been stripped from
860 MANGLED. */
861
862char *
9334f9c6 863cplus_demangle (const char *mangled, int options)
252b5132
RH
864{
865 char *ret;
866 struct work_stuff work[1];
2da4c07f
RH
867
868 if (current_demangling_style == no_demangling)
869 return xstrdup (mangled);
870
252b5132 871 memset ((char *) work, 0, sizeof (work));
f1775526
HPN
872 work->options = options;
873 if ((work->options & DMGL_STYLE_MASK) == 0)
874 work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
252b5132 875
e49a569c
DD
876 /* The V3 ABI demangling is implemented elsewhere. */
877 if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
878 {
44354ae1 879 ret = cplus_demangle_v3 (mangled, work->options);
e49a569c
DD
880 if (ret || GNU_V3_DEMANGLING)
881 return ret;
882 }
eb383413 883
bc9bf259
DD
884 if (JAVA_DEMANGLING)
885 {
886 ret = java_demangle_v3 (mangled);
887 if (ret)
888 return ret;
889 }
890
16e85745 891 if (GNAT_DEMANGLING)
6d390a9e 892 return ada_demangle (mangled, options);
16e85745 893
b55f9678
IB
894 if (DLANG_DEMANGLING)
895 {
896 ret = dlang_demangle (mangled, options);
897 if (ret)
898 return ret;
899 }
900
252b5132
RH
901 ret = internal_cplus_demangle (work, mangled);
902 squangle_mop_up (work);
903 return (ret);
904}
905
6d390a9e 906/* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
252b5132 907
6d390a9e 908char *
9334f9c6 909ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
16e85745 910{
16e85745
HPN
911 int len0;
912 const char* p;
6d390a9e 913 char *d;
d0f0b69d 914 char *demangled;
16e85745 915
6d390a9e 916 /* Discard leading _ada_, which is used for library level subprograms. */
16e85745 917 if (strncmp (mangled, "_ada_", 5) == 0)
6d390a9e
DD
918 mangled += 5;
919
920 /* All ada unit names are lower-case. */
921 if (!ISLOWER (mangled[0]))
922 goto unknown;
923
924 /* Most of the demangling will trivially remove chars. Operator names
925 may add one char but because they are always preceeded by '__' which is
6a44904e
DD
926 replaced by '.', they eventually never expand the size.
927 A few special names such as '___elabs' add a few chars (at most 7), but
928 they occur only once. */
929 len0 = strlen (mangled) + 7 + 1;
6d390a9e 930 demangled = XNEWVEC (char, len0);
16e85745 931
6d390a9e
DD
932 d = demangled;
933 p = mangled;
934 while (1)
16e85745 935 {
6a44904e 936 /* An entity names is expected. */
6d390a9e
DD
937 if (ISLOWER (*p))
938 {
6a44904e 939 /* An identifier, which is always lower case. */
6d390a9e
DD
940 do
941 *d++ = *p++;
942 while (ISLOWER(*p) || ISDIGIT (*p)
943 || (p[0] == '_' && (ISLOWER (p[1]) || ISDIGIT (p[1]))));
944 }
945 else if (p[0] == 'O')
946 {
6a44904e 947 /* An operator name. */
6d390a9e
DD
948 static const char * const operators[][2] =
949 {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
950 {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
951 {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
952 {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
953 {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
954 {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
955 {"Oexpon", "**"}, {NULL, NULL}};
956 int k;
957
6a44904e 958 for (k = 0; operators[k][0] != NULL; k++)
6d390a9e 959 {
6a44904e
DD
960 size_t slen = strlen (operators[k][0]);
961 if (strncmp (p, operators[k][0], slen) == 0)
6d390a9e 962 {
6a44904e
DD
963 p += slen;
964 slen = strlen (operators[k][1]);
6d390a9e 965 *d++ = '"';
6a44904e
DD
966 memcpy (d, operators[k][1], slen);
967 d += slen;
6d390a9e
DD
968 *d++ = '"';
969 break;
970 }
971 }
972 /* Operator not found. */
6a44904e 973 if (operators[k][0] == NULL)
6d390a9e
DD
974 goto unknown;
975 }
16e85745 976 else
6d390a9e
DD
977 {
978 /* Not a GNAT encoding. */
979 goto unknown;
980 }
981
6a44904e
DD
982 /* The name can be directly followed by some uppercase letters. */
983 if (p[0] == 'T' && p[1] == 'K')
984 {
985 /* Task stuff. */
986 if (p[2] == 'B' && p[3] == 0)
987 {
988 /* Subprogram for task body. */
989 break;
990 }
991 else if (p[2] == '_' && p[3] == '_')
992 {
993 /* Inner declarations in a task. */
994 p += 4;
995 *d++ = '.';
996 continue;
997 }
998 else
999 goto unknown;
1000 }
1001 if (p[0] == 'E' && p[1] == 0)
1002 {
1003 /* Exception name. */
1004 goto unknown;
1005 }
1006 if ((p[0] == 'P' || p[0] == 'N') && p[1] == 0)
1007 {
1008 /* Protected type subprogram. */
1009 break;
1010 }
1011 if ((*p == 'N' || *p == 'S') && p[1] == 0)
1012 {
1013 /* Enumerated type name table. */
1014 goto unknown;
1015 }
1016 if (p[0] == 'X')
1017 {
1018 /* Body nested. */
1019 p++;
1020 while (p[0] == 'n' || p[0] == 'b')
1021 p++;
1022 }
1023 if (p[0] == 'S' && p[1] != 0 && (p[2] == '_' || p[2] == 0))
1024 {
1025 /* Stream operations. */
1026 const char *name;
1027 switch (p[1])
1028 {
1029 case 'R':
1030 name = "'Read";
1031 break;
1032 case 'W':
1033 name = "'Write";
1034 break;
1035 case 'I':
1036 name = "'Input";
1037 break;
1038 case 'O':
1039 name = "'Output";
1040 break;
1041 default:
1042 goto unknown;
1043 }
1044 p += 2;
1045 strcpy (d, name);
1046 d += strlen (name);
1047 }
1048 else if (p[0] == 'D')
1049 {
1050 /* Controlled type operation. */
1051 const char *name;
1052 switch (p[1])
1053 {
1054 case 'F':
1055 name = ".Finalize";
1056 break;
1057 case 'A':
1058 name = ".Adjust";
1059 break;
1060 default:
1061 goto unknown;
1062 }
1063 strcpy (d, name);
1064 d += strlen (name);
1065 break;
1066 }
1067
6d390a9e
DD
1068 if (p[0] == '_')
1069 {
1070 /* Separator. */
1071 if (p[1] == '_')
1072 {
1073 /* Standard separator. Handled first. */
1074 p += 2;
6a44904e 1075
6d390a9e
DD
1076 if (ISDIGIT (*p))
1077 {
6a44904e 1078 /* Overloading number. */
6d390a9e
DD
1079 do
1080 p++;
1081 while (ISDIGIT (*p) || (p[0] == '_' && ISDIGIT (p[1])));
6a44904e
DD
1082 if (*p == 'X')
1083 {
1084 p++;
1085 while (p[0] == 'n' || p[0] == 'b')
1086 p++;
1087 }
6d390a9e 1088 }
6a44904e 1089 else if (p[0] == '_' && p[1] != '_')
6d390a9e 1090 {
6a44904e
DD
1091 /* Special names. */
1092 static const char * const special[][2] = {
1093 { "_elabb", "'Elab_Body" },
1094 { "_elabs", "'Elab_Spec" },
1095 { "_size", "'Size" },
1096 { "_alignment", "'Alignment" },
1097 { "_assign", ".\":=\"" },
1098 { NULL, NULL }
1099 };
1100 int k;
1101
1102 for (k = 0; special[k][0] != NULL; k++)
1103 {
1104 size_t slen = strlen (special[k][0]);
1105 if (strncmp (p, special[k][0], slen) == 0)
1106 {
1107 p += slen;
1108 slen = strlen (special[k][1]);
1109 memcpy (d, special[k][1], slen);
1110 d += slen;
1111 break;
1112 }
1113 }
1114 if (special[k][0] != NULL)
1115 break;
1116 else
1117 goto unknown;
6d390a9e
DD
1118 }
1119 else
1120 {
1121 *d++ = '.';
1122 continue;
1123 }
1124 }
1125 else if (p[1] == 'B' || p[1] == 'E')
1126 {
1127 /* Entry Body or barrier Evaluation. */
1128 p += 2;
1129 while (ISDIGIT (*p))
1130 p++;
1131 if (p[0] == 's' && p[1] == 0)
1132 break;
1133 else
1134 goto unknown;
1135 }
1136 else
1137 goto unknown;
1138 }
1139
6d390a9e
DD
1140 if (p[0] == '.' && ISDIGIT (p[1]))
1141 {
1142 /* Nested subprogram. */
1143 p += 2;
1144 while (ISDIGIT (*p))
1145 p++;
1146 }
1147 if (*p == 0)
1148 {
1149 /* End of mangled name. */
1150 break;
1151 }
16e85745 1152 else
6d390a9e 1153 goto unknown;
16e85745 1154 }
6d390a9e
DD
1155 *d = 0;
1156 return demangled;
16e85745 1157
6d390a9e
DD
1158 unknown:
1159 len0 = strlen (mangled);
1160 demangled = XNEWVEC (char, len0 + 3);
0d3deb8d 1161
16e85745 1162 if (mangled[0] == '<')
d0f0b69d 1163 strcpy (demangled, mangled);
16e85745
HPN
1164 else
1165 sprintf (demangled, "<%s>", mangled);
1166
1167 return demangled;
1168}
1169
252b5132
RH
1170/* This function performs most of what cplus_demangle use to do, but
1171 to be able to demangle a name with a B, K or n code, we need to
1172 have a longer term memory of what types have been seen. The original
25dfe0ac 1173 now initializes and cleans up the squangle code info, while internal
252b5132
RH
1174 calls go directly to this routine to avoid resetting that info. */
1175
1176static char *
9334f9c6 1177internal_cplus_demangle (struct work_stuff *work, const char *mangled)
252b5132
RH
1178{
1179
1180 string decl;
1181 int success = 0;
1182 char *demangled = NULL;
f1775526 1183 int s1, s2, s3, s4;
252b5132
RH
1184 s1 = work->constructor;
1185 s2 = work->destructor;
1186 s3 = work->static_type;
1187 s4 = work->type_quals;
1188 work->constructor = work->destructor = 0;
1189 work->type_quals = TYPE_UNQUALIFIED;
1190 work->dllimported = 0;
1191
1192 if ((mangled != NULL) && (*mangled != '\0'))
1193 {
1194 string_init (&decl);
1195
1196 /* First check to see if gnu style demangling is active and if the
1197 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1198 recognize one of the gnu special forms rather than looking for a
1199 standard prefix. In particular, don't worry about whether there
1200 is a "__" string in the mangled string. Consider "_$_5__foo" for
1201 example. */
1202
1203 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
1204 {
1205 success = gnu_special (work, &mangled, &decl);
0aaa9a3a
AB
1206 if (!success)
1207 {
1208 delete_work_stuff (work);
1209 string_delete (&decl);
1210 }
252b5132
RH
1211 }
1212 if (!success)
1213 {
1214 success = demangle_prefix (work, &mangled, &decl);
1215 }
1216 if (success && (*mangled != '\0'))
1217 {
1218 success = demangle_signature (work, &mangled, &decl);
1219 }
1220 if (work->constructor == 2)
1221 {
1222 string_prepend (&decl, "global constructors keyed to ");
1223 work->constructor = 0;
1224 }
1225 else if (work->destructor == 2)
1226 {
1227 string_prepend (&decl, "global destructors keyed to ");
1228 work->destructor = 0;
1229 }
1230 else if (work->dllimported == 1)
1231 {
1232 string_prepend (&decl, "import stub for ");
1233 work->dllimported = 0;
1234 }
1235 demangled = mop_up (work, &decl, success);
1236 }
1237 work->constructor = s1;
1238 work->destructor = s2;
1239 work->static_type = s3;
1240 work->type_quals = s4;
f1775526 1241 return demangled;
252b5132
RH
1242}
1243
1244
1245/* Clear out and squangling related storage */
1246static void
9334f9c6 1247squangle_mop_up (struct work_stuff *work)
252b5132
RH
1248{
1249 /* clean up the B and K type mangling types. */
1250 forget_B_and_K_types (work);
1251 if (work -> btypevec != NULL)
1252 {
1253 free ((char *) work -> btypevec);
0aaa9a3a 1254 work->btypevec = NULL;
fa3fcee7 1255 work->bsize = 0;
252b5132
RH
1256 }
1257 if (work -> ktypevec != NULL)
1258 {
1259 free ((char *) work -> ktypevec);
0aaa9a3a 1260 work->ktypevec = NULL;
fa3fcee7 1261 work->ksize = 0;
252b5132
RH
1262 }
1263}
1264
252b5132 1265
e8865c28
DB
1266/* Copy the work state and storage. */
1267
1268static void
9334f9c6 1269work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from)
252b5132 1270{
e8865c28
DB
1271 int i;
1272
1273 delete_work_stuff (to);
1274
1275 /* Shallow-copy scalars. */
1276 memcpy (to, from, sizeof (*to));
1277
1278 /* Deep-copy dynamic storage. */
1279 if (from->typevec_size)
abf6a75b 1280 to->typevec = XNEWVEC (char *, from->typevec_size);
e8865c28
DB
1281
1282 for (i = 0; i < from->ntypes; i++)
1283 {
1284 int len = strlen (from->typevec[i]) + 1;
1285
abf6a75b 1286 to->typevec[i] = XNEWVEC (char, len);
e8865c28
DB
1287 memcpy (to->typevec[i], from->typevec[i], len);
1288 }
1289
1290 if (from->ksize)
abf6a75b 1291 to->ktypevec = XNEWVEC (char *, from->ksize);
e8865c28
DB
1292
1293 for (i = 0; i < from->numk; i++)
1294 {
1295 int len = strlen (from->ktypevec[i]) + 1;
1296
abf6a75b 1297 to->ktypevec[i] = XNEWVEC (char, len);
e8865c28
DB
1298 memcpy (to->ktypevec[i], from->ktypevec[i], len);
1299 }
252b5132 1300
e8865c28 1301 if (from->bsize)
abf6a75b 1302 to->btypevec = XNEWVEC (char *, from->bsize);
e8865c28
DB
1303
1304 for (i = 0; i < from->numb; i++)
1305 {
1306 int len = strlen (from->btypevec[i]) + 1;
1307
abf6a75b 1308 to->btypevec[i] = XNEWVEC (char , len);
e8865c28
DB
1309 memcpy (to->btypevec[i], from->btypevec[i], len);
1310 }
1311
41f225de
NC
1312 if (from->proctypevec)
1313 to->proctypevec =
1314 XDUPVEC (int, from->proctypevec, from->proctypevec_size);
1315
e8865c28 1316 if (from->ntmpl_args)
abf6a75b 1317 to->tmpl_argvec = XNEWVEC (char *, from->ntmpl_args);
e8865c28
DB
1318
1319 for (i = 0; i < from->ntmpl_args; i++)
1320 {
1321 int len = strlen (from->tmpl_argvec[i]) + 1;
1322
abf6a75b 1323 to->tmpl_argvec[i] = XNEWVEC (char, len);
e8865c28
DB
1324 memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
1325 }
1326
1327 if (from->previous_argument)
1328 {
abf6a75b 1329 to->previous_argument = XNEW (string);
e8865c28
DB
1330 string_init (to->previous_argument);
1331 string_appends (to->previous_argument, from->previous_argument);
1332 }
1333}
1334
1335
1336/* Delete dynamic stuff in work_stuff that is not to be re-used. */
1337
1338static void
9334f9c6 1339delete_non_B_K_work_stuff (struct work_stuff *work)
e8865c28 1340{
252b5132
RH
1341 /* Discard the remembered types, if any. */
1342
1343 forget_types (work);
41f225de 1344 if (work->typevec != NULL)
252b5132 1345 {
41f225de
NC
1346 free ((char *) work->typevec);
1347 work->typevec = NULL;
1348 work->typevec_size = 0;
1349 }
1350 if (work->proctypevec != NULL)
1351 {
1352 free (work->proctypevec);
1353 work->proctypevec = NULL;
1354 work->proctypevec_size = 0;
252b5132
RH
1355 }
1356 if (work->tmpl_argvec)
1357 {
1358 int i;
1359
1360 for (i = 0; i < work->ntmpl_args; i++)
4ff224cb 1361 free ((char*) work->tmpl_argvec[i]);
252b5132
RH
1362
1363 free ((char*) work->tmpl_argvec);
1364 work->tmpl_argvec = NULL;
1365 }
1366 if (work->previous_argument)
1367 {
1368 string_delete (work->previous_argument);
1369 free ((char*) work->previous_argument);
1370 work->previous_argument = NULL;
1371 }
e8865c28
DB
1372}
1373
1374
1375/* Delete all dynamic storage in work_stuff. */
1376static void
9334f9c6 1377delete_work_stuff (struct work_stuff *work)
e8865c28
DB
1378{
1379 delete_non_B_K_work_stuff (work);
1380 squangle_mop_up (work);
1381}
1382
1383
1384/* Clear out any mangled storage */
1385
1386static char *
9334f9c6 1387mop_up (struct work_stuff *work, string *declp, int success)
e8865c28
DB
1388{
1389 char *demangled = NULL;
1390
1391 delete_non_B_K_work_stuff (work);
252b5132
RH
1392
1393 /* If demangling was successful, ensure that the demangled string is null
1394 terminated and return it. Otherwise, free the demangling decl. */
1395
1396 if (!success)
1397 {
1398 string_delete (declp);
1399 }
1400 else
1401 {
1402 string_appendn (declp, "", 1);
f1775526 1403 demangled = declp->b;
252b5132
RH
1404 }
1405 return (demangled);
1406}
1407
1408/*
1409
1410LOCAL FUNCTION
1411
1412 demangle_signature -- demangle the signature part of a mangled name
1413
1414SYNOPSIS
1415
1416 static int
1417 demangle_signature (struct work_stuff *work, const char **mangled,
1418 string *declp);
1419
1420DESCRIPTION
1421
1422 Consume and demangle the signature portion of the mangled name.
1423
1424 DECLP is the string where demangled output is being built. At
1425 entry it contains the demangled root name from the mangled name
1426 prefix. I.E. either a demangled operator name or the root function
1427 name. In some special cases, it may contain nothing.
1428
1429 *MANGLED points to the current unconsumed location in the mangled
1430 name. As tokens are consumed and demangling is performed, the
1431 pointer is updated to continuously point at the next token to
1432 be consumed.
1433
1434 Demangling GNU style mangled names is nasty because there is no
1435 explicit token that marks the start of the outermost function
1436 argument list. */
1437
1438static int
9334f9c6
DD
1439demangle_signature (struct work_stuff *work,
1440 const char **mangled, string *declp)
252b5132
RH
1441{
1442 int success = 1;
1443 int func_done = 0;
1444 int expect_func = 0;
1445 int expect_return_type = 0;
1446 const char *oldmangled = NULL;
1447 string trawname;
1448 string tname;
1449
1450 while (success && (**mangled != '\0'))
1451 {
1452 switch (**mangled)
1453 {
1454 case 'Q':
1455 oldmangled = *mangled;
1456 success = demangle_qualified (work, mangled, declp, 1, 0);
1457 if (success)
1458 remember_type (work, oldmangled, *mangled - oldmangled);
1459 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1460 expect_func = 1;
1461 oldmangled = NULL;
1462 break;
1463
1464 case 'K':
1465 oldmangled = *mangled;
1466 success = demangle_qualified (work, mangled, declp, 1, 0);
1467 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1468 {
1469 expect_func = 1;
1470 }
1471 oldmangled = NULL;
1472 break;
1473
1474 case 'S':
1475 /* Static member function */
1476 if (oldmangled == NULL)
1477 {
1478 oldmangled = *mangled;
1479 }
1480 (*mangled)++;
1481 work -> static_type = 1;
1482 break;
1483
1484 case 'C':
1485 case 'V':
1486 case 'u':
1487 work->type_quals |= code_for_qualifier (**mangled);
1488
1489 /* a qualified member function */
1490 if (oldmangled == NULL)
1491 oldmangled = *mangled;
1492 (*mangled)++;
1493 break;
1494
1495 case 'L':
1496 /* Local class name follows after "Lnnn_" */
1497 if (HP_DEMANGLING)
1498 {
1499 while (**mangled && (**mangled != '_'))
1500 (*mangled)++;
1501 if (!**mangled)
1502 success = 0;
1503 else
1504 (*mangled)++;
1505 }
1506 else
1507 success = 0;
1508 break;
1509
1510 case '0': case '1': case '2': case '3': case '4':
1511 case '5': case '6': case '7': case '8': case '9':
1512 if (oldmangled == NULL)
1513 {
1514 oldmangled = *mangled;
1515 }
1516 work->temp_start = -1; /* uppermost call to demangle_class */
1517 success = demangle_class (work, mangled, declp);
1518 if (success)
1519 {
1520 remember_type (work, oldmangled, *mangled - oldmangled);
1521 }
1522 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1523 {
1524 /* EDG and others will have the "F", so we let the loop cycle
1525 if we are looking at one. */
1526 if (**mangled != 'F')
1527 expect_func = 1;
1528 }
1529 oldmangled = NULL;
1530 break;
1531
1532 case 'B':
1533 {
1534 string s;
1535 success = do_type (work, mangled, &s);
1536 if (success)
1537 {
1538 string_append (&s, SCOPE_STRING (work));
1539 string_prepends (declp, &s);
fcd7c7c9 1540 string_delete (&s);
252b5132
RH
1541 }
1542 oldmangled = NULL;
1543 expect_func = 1;
1544 }
1545 break;
1546
1547 case 'F':
1548 /* Function */
1549 /* ARM/HP style demangling includes a specific 'F' character after
1550 the class name. For GNU style, it is just implied. So we can
1551 safely just consume any 'F' at this point and be compatible
1552 with either style. */
1553
1554 oldmangled = NULL;
1555 func_done = 1;
1556 (*mangled)++;
1557
1558 /* For lucid/ARM/HP style we have to forget any types we might
1559 have remembered up to this point, since they were not argument
1560 types. GNU style considers all types seen as available for
1561 back references. See comment in demangle_args() */
1562
1563 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1564 {
1565 forget_types (work);
1566 }
1567 success = demangle_args (work, mangled, declp);
1568 /* After picking off the function args, we expect to either
1569 find the function return type (preceded by an '_') or the
1570 end of the string. */
1571 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1572 {
1573 ++(*mangled);
1574 /* At this level, we do not care about the return type. */
1575 success = do_type (work, mangled, &tname);
1576 string_delete (&tname);
1577 }
1578
1579 break;
1580
1581 case 't':
1582 /* G++ Template */
1583 string_init(&trawname);
1584 string_init(&tname);
1585 if (oldmangled == NULL)
1586 {
1587 oldmangled = *mangled;
1588 }
1589 success = demangle_template (work, mangled, &tname,
1590 &trawname, 1, 1);
1591 if (success)
1592 {
1593 remember_type (work, oldmangled, *mangled - oldmangled);
1594 }
1595 string_append (&tname, SCOPE_STRING (work));
1596
1597 string_prepends(declp, &tname);
1598 if (work -> destructor & 1)
1599 {
1600 string_prepend (&trawname, "~");
1601 string_appends (declp, &trawname);
1602 work->destructor -= 1;
1603 }
1604 if ((work->constructor & 1) || (work->destructor & 1))
1605 {
1606 string_appends (declp, &trawname);
1607 work->constructor -= 1;
1608 }
1609 string_delete(&trawname);
1610 string_delete(&tname);
1611 oldmangled = NULL;
1612 expect_func = 1;
1613 break;
1614
1615 case '_':
e8865c28 1616 if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
252b5132
RH
1617 {
1618 /* Read the return type. */
1619 string return_type;
252b5132
RH
1620
1621 (*mangled)++;
1622 success = do_type (work, mangled, &return_type);
1623 APPEND_BLANK (&return_type);
1624
1625 string_prepends (declp, &return_type);
1626 string_delete (&return_type);
1627 break;
1628 }
1629 else
1630 /* At the outermost level, we cannot have a return type specified,
1631 so if we run into another '_' at this point we are dealing with
1632 a mangled name that is either bogus, or has been mangled by
1633 some algorithm we don't know how to deal with. So just
1634 reject the entire demangling. */
1635 /* However, "_nnn" is an expected suffix for alternate entry point
1636 numbered nnn for a function, with HP aCC, so skip over that
1637 without reporting failure. pai/1997-09-04 */
1638 if (HP_DEMANGLING)
1639 {
1640 (*mangled)++;
ac424eb3 1641 while (**mangled && ISDIGIT ((unsigned char)**mangled))
252b5132
RH
1642 (*mangled)++;
1643 }
1644 else
1645 success = 0;
1646 break;
1647
1648 case 'H':
e8865c28 1649 if (AUTO_DEMANGLING || GNU_DEMANGLING)
252b5132
RH
1650 {
1651 /* A G++ template function. Read the template arguments. */
1652 success = demangle_template (work, mangled, declp, 0, 0,
1653 0);
1654 if (!(work->constructor & 1))
1655 expect_return_type = 1;
1656 (*mangled)++;
1657 break;
1658 }
1659 else
1660 /* fall through */
1661 {;}
1662
1663 default:
1664 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1665 {
1666 /* Assume we have stumbled onto the first outermost function
1667 argument token, and start processing args. */
1668 func_done = 1;
1669 success = demangle_args (work, mangled, declp);
1670 }
1671 else
1672 {
1673 /* Non-GNU demanglers use a specific token to mark the start
1674 of the outermost function argument tokens. Typically 'F',
1675 for ARM/HP-demangling, for example. So if we find something
1676 we are not prepared for, it must be an error. */
1677 success = 0;
1678 }
1679 break;
1680 }
1681 /*
1682 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1683 */
1684 {
1685 if (success && expect_func)
1686 {
1687 func_done = 1;
1688 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1689 {
1690 forget_types (work);
1691 }
1692 success = demangle_args (work, mangled, declp);
1693 /* Since template include the mangling of their return types,
1694 we must set expect_func to 0 so that we don't try do
1695 demangle more arguments the next time we get here. */
1696 expect_func = 0;
1697 }
1698 }
1699 }
1700 if (success && !func_done)
1701 {
1702 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1703 {
1704 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1705 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1706 first case, and need to ensure that the '(void)' gets added to
1707 the current declp. Note that with ARM/HP, the first case
1708 represents the name of a static data member 'foo::bar',
1709 which is in the current declp, so we leave it alone. */
1710 success = demangle_args (work, mangled, declp);
1711 }
1712 }
1713 if (success && PRINT_ARG_TYPES)
1714 {
1715 if (work->static_type)
1716 string_append (declp, " static");
1717 if (work->type_quals != TYPE_UNQUALIFIED)
1718 {
1719 APPEND_BLANK (declp);
1720 string_append (declp, qualifier_string (work->type_quals));
1721 }
1722 }
1723
1724 return (success);
1725}
1726
1727#if 0
1728
1729static int
9334f9c6
DD
1730demangle_method_args (struct work_stuff *work, const char **mangled,
1731 string *declp)
252b5132
RH
1732{
1733 int success = 0;
1734
1735 if (work -> static_type)
1736 {
1737 string_append (declp, *mangled + 1);
1738 *mangled += strlen (*mangled);
1739 success = 1;
1740 }
1741 else
1742 {
1743 success = demangle_args (work, mangled, declp);
1744 }
1745 return (success);
1746}
1747
1748#endif
1749
1750static int
9334f9c6
DD
1751demangle_template_template_parm (struct work_stuff *work,
1752 const char **mangled, string *tname)
252b5132
RH
1753{
1754 int i;
1755 int r;
1756 int need_comma = 0;
1757 int success = 1;
1758 string temp;
1759
1760 string_append (tname, "template <");
1761 /* get size of template parameter list */
1762 if (get_count (mangled, &r))
1763 {
1764 for (i = 0; i < r; i++)
1765 {
1766 if (need_comma)
1767 {
1768 string_append (tname, ", ");
1769 }
1770
1771 /* Z for type parameters */
1772 if (**mangled == 'Z')
1773 {
1774 (*mangled)++;
1775 string_append (tname, "class");
1776 }
1777 /* z for template parameters */
1778 else if (**mangled == 'z')
1779 {
1780 (*mangled)++;
1781 success =
1782 demangle_template_template_parm (work, mangled, tname);
1783 if (!success)
1784 {
1785 break;
1786 }
1787 }
1788 else
1789 {
1790 /* temp is initialized in do_type */
1791 success = do_type (work, mangled, &temp);
1792 if (success)
1793 {
1794 string_appends (tname, &temp);
1795 }
1796 string_delete(&temp);
1797 if (!success)
1798 {
1799 break;
1800 }
1801 }
1802 need_comma = 1;
1803 }
1804
1805 }
1806 if (tname->p[-1] == '>')
1807 string_append (tname, " ");
1808 string_append (tname, "> class");
1809 return (success);
1810}
1811
1812static int
9334f9c6
DD
1813demangle_expression (struct work_stuff *work, const char **mangled,
1814 string *s, type_kind_t tk)
252b5132 1815{
0c0a36a4 1816 int need_operator = 0;
252b5132
RH
1817 int success;
1818
0c0a36a4
ILT
1819 success = 1;
1820 string_appendn (s, "(", 1);
1821 (*mangled)++;
1822 while (success && **mangled != 'W' && **mangled != '\0')
252b5132 1823 {
0c0a36a4 1824 if (need_operator)
252b5132 1825 {
0c0a36a4
ILT
1826 size_t i;
1827 size_t len;
252b5132 1828
0c0a36a4 1829 success = 0;
252b5132 1830
0c0a36a4 1831 len = strlen (*mangled);
252b5132 1832
74bcd529 1833 for (i = 0; i < ARRAY_SIZE (optable); ++i)
0c0a36a4
ILT
1834 {
1835 size_t l = strlen (optable[i].in);
252b5132 1836
0c0a36a4
ILT
1837 if (l <= len
1838 && memcmp (optable[i].in, *mangled, l) == 0)
1839 {
1840 string_appendn (s, " ", 1);
1841 string_append (s, optable[i].out);
1842 string_appendn (s, " ", 1);
1843 success = 1;
1844 (*mangled) += l;
1845 break;
252b5132 1846 }
252b5132 1847 }
252b5132 1848
0c0a36a4
ILT
1849 if (!success)
1850 break;
252b5132 1851 }
252b5132 1852 else
0c0a36a4
ILT
1853 need_operator = 1;
1854
1855 success = demangle_template_value_parm (work, mangled, s, tk);
252b5132 1856 }
0c0a36a4
ILT
1857
1858 if (**mangled != 'W')
1859 success = 0;
1860 else
1861 {
1862 string_appendn (s, ")", 1);
1863 (*mangled)++;
1864 }
1865
1866 return success;
1867}
1868
1869static int
9334f9c6
DD
1870demangle_integral_value (struct work_stuff *work,
1871 const char **mangled, string *s)
0c0a36a4
ILT
1872{
1873 int success;
1874
1875 if (**mangled == 'E')
1876 success = demangle_expression (work, mangled, s, tk_integral);
252b5132
RH
1877 else if (**mangled == 'Q' || **mangled == 'K')
1878 success = demangle_qualified (work, mangled, s, 0, 1);
1879 else
1880 {
0c0a36a4
ILT
1881 int value;
1882
e8865c28
DB
1883 /* By default, we let the number decide whether we shall consume an
1884 underscore. */
6f848550 1885 int multidigit_without_leading_underscore = 0;
e8865c28
DB
1886 int leave_following_underscore = 0;
1887
252b5132
RH
1888 success = 0;
1889
d8cbbf95
DD
1890 if (**mangled == '_')
1891 {
1892 if (mangled[0][1] == 'm')
1893 {
1894 /* Since consume_count_with_underscores does not handle the
1895 `m'-prefix we must do it here, using consume_count and
1896 adjusting underscores: we have to consume the underscore
1897 matching the prepended one. */
1898 multidigit_without_leading_underscore = 1;
1899 string_appendn (s, "-", 1);
1900 (*mangled) += 2;
1901 }
1902 else
1903 {
1904 /* Do not consume a following underscore;
1905 consume_count_with_underscores will consume what
1906 should be consumed. */
1907 leave_following_underscore = 1;
1908 }
e8865c28 1909 }
6f848550
DD
1910 else
1911 {
d8cbbf95
DD
1912 /* Negative numbers are indicated with a leading `m'. */
1913 if (**mangled == 'm')
1914 {
1915 string_appendn (s, "-", 1);
1916 (*mangled)++;
1917 }
6f848550
DD
1918 /* Since consume_count_with_underscores does not handle
1919 multi-digit numbers that do not start with an underscore,
1920 and this number can be an integer template parameter,
1921 we have to call consume_count. */
1922 multidigit_without_leading_underscore = 1;
1923 /* These multi-digit numbers never end on an underscore,
1924 so if there is one then don't eat it. */
1925 leave_following_underscore = 1;
1926 }
e8865c28
DB
1927
1928 /* We must call consume_count if we expect to remove a trailing
1929 underscore, since consume_count_with_underscores expects
1930 the leading underscore (that we consumed) if it is to handle
1931 multi-digit numbers. */
6f848550 1932 if (multidigit_without_leading_underscore)
e8865c28
DB
1933 value = consume_count (mangled);
1934 else
1935 value = consume_count_with_underscores (mangled);
0c0a36a4 1936
0c0a36a4
ILT
1937 if (value != -1)
1938 {
1939 char buf[INTBUF_SIZE];
1940 sprintf (buf, "%d", value);
1941 string_append (s, buf);
1942
e8865c28
DB
1943 /* Numbers not otherwise delimited, might have an underscore
1944 appended as a delimeter, which we should skip.
1945
1946 ??? This used to always remove a following underscore, which
1947 is wrong. If other (arbitrary) cases are followed by an
1948 underscore, we need to do something more radical. */
1949
6f848550 1950 if ((value > 9 || multidigit_without_leading_underscore)
e8865c28
DB
1951 && ! leave_following_underscore
1952 && **mangled == '_')
0c0a36a4
ILT
1953 (*mangled)++;
1954
1955 /* All is well. */
1956 success = 1;
1957 }
d8cbbf95 1958 }
0c0a36a4
ILT
1959
1960 return success;
1961}
1962
1963/* Demangle the real value in MANGLED. */
1964
1965static int
9334f9c6
DD
1966demangle_real_value (struct work_stuff *work,
1967 const char **mangled, string *s)
0c0a36a4
ILT
1968{
1969 if (**mangled == 'E')
1970 return demangle_expression (work, mangled, s, tk_real);
1971
1972 if (**mangled == 'm')
1973 {
1974 string_appendn (s, "-", 1);
1975 (*mangled)++;
1976 }
ac424eb3 1977 while (ISDIGIT ((unsigned char)**mangled))
0c0a36a4
ILT
1978 {
1979 string_appendn (s, *mangled, 1);
1980 (*mangled)++;
1981 }
1982 if (**mangled == '.') /* fraction */
1983 {
1984 string_appendn (s, ".", 1);
1985 (*mangled)++;
ac424eb3 1986 while (ISDIGIT ((unsigned char)**mangled))
0c0a36a4
ILT
1987 {
1988 string_appendn (s, *mangled, 1);
1989 (*mangled)++;
1990 }
1991 }
1992 if (**mangled == 'e') /* exponent */
1993 {
1994 string_appendn (s, "e", 1);
1995 (*mangled)++;
ac424eb3 1996 while (ISDIGIT ((unsigned char)**mangled))
252b5132
RH
1997 {
1998 string_appendn (s, *mangled, 1);
1999 (*mangled)++;
252b5132
RH
2000 }
2001 }
2002
0c0a36a4 2003 return 1;
252b5132
RH
2004}
2005
2006static int
9334f9c6
DD
2007demangle_template_value_parm (struct work_stuff *work, const char **mangled,
2008 string *s, type_kind_t tk)
252b5132
RH
2009{
2010 int success = 1;
2011
2012 if (**mangled == 'Y')
2013 {
2014 /* The next argument is a template parameter. */
2015 int idx;
2016
2017 (*mangled)++;
2018 idx = consume_count_with_underscores (mangled);
2019 if (idx == -1
2020 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2021 || consume_count_with_underscores (mangled) == -1)
2022 return -1;
2023 if (work->tmpl_argvec)
2024 string_append (s, work->tmpl_argvec[idx]);
2025 else
0c0a36a4 2026 string_append_template_idx (s, idx);
252b5132
RH
2027 }
2028 else if (tk == tk_integral)
2029 success = demangle_integral_value (work, mangled, s);
2030 else if (tk == tk_char)
2031 {
2032 char tmp[2];
2033 int val;
2034 if (**mangled == 'm')
2035 {
2036 string_appendn (s, "-", 1);
2037 (*mangled)++;
2038 }
2039 string_appendn (s, "'", 1);
2040 val = consume_count(mangled);
2041 if (val <= 0)
2042 success = 0;
2043 else
2044 {
2045 tmp[0] = (char)val;
2046 tmp[1] = '\0';
2047 string_appendn (s, &tmp[0], 1);
2048 string_appendn (s, "'", 1);
2049 }
2050 }
2051 else if (tk == tk_bool)
2052 {
2053 int val = consume_count (mangled);
2054 if (val == 0)
2055 string_appendn (s, "false", 5);
2056 else if (val == 1)
2057 string_appendn (s, "true", 4);
2058 else
2059 success = 0;
2060 }
2061 else if (tk == tk_real)
0c0a36a4 2062 success = demangle_real_value (work, mangled, s);
fa3fcee7
NC
2063 else if (tk == tk_pointer || tk == tk_reference
2064 || tk == tk_rvalue_reference)
252b5132 2065 {
0c0a36a4
ILT
2066 if (**mangled == 'Q')
2067 success = demangle_qualified (work, mangled, s,
2068 /*isfuncname=*/0,
2069 /*append=*/1);
252b5132
RH
2070 else
2071 {
0c0a36a4 2072 int symbol_len = consume_count (mangled);
fa3fcee7
NC
2073 if (symbol_len == -1
2074 || symbol_len > (long) strlen (*mangled))
0c0a36a4
ILT
2075 return -1;
2076 if (symbol_len == 0)
2077 string_appendn (s, "0", 1);
2078 else
252b5132 2079 {
abf6a75b 2080 char *p = XNEWVEC (char, symbol_len + 1), *q;
0c0a36a4
ILT
2081 strncpy (p, *mangled, symbol_len);
2082 p [symbol_len] = '\0';
2083 /* We use cplus_demangle here, rather than
2084 internal_cplus_demangle, because the name of the entity
2085 mangled here does not make use of any of the squangling
2086 or type-code information we have built up thus far; it is
2087 mangled independently. */
2088 q = cplus_demangle (p, work->options);
2089 if (tk == tk_pointer)
2090 string_appendn (s, "&", 1);
2091 /* FIXME: Pointer-to-member constants should get a
2092 qualifying class name here. */
2093 if (q)
2094 {
2095 string_append (s, q);
2096 free (q);
2097 }
2098 else
2099 string_append (s, p);
2100 free (p);
252b5132 2101 }
0c0a36a4 2102 *mangled += symbol_len;
252b5132 2103 }
252b5132
RH
2104 }
2105
2106 return success;
2107}
2108
2109/* Demangle the template name in MANGLED. The full name of the
2110 template (e.g., S<int>) is placed in TNAME. The name without the
2111 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2112 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2113 not a function template. If both IS_TYPE and REMEMBER are nonzero,
e8865c28 2114 the template is remembered in the list of back-referenceable
252b5132
RH
2115 types. */
2116
2117static int
9334f9c6
DD
2118demangle_template (struct work_stuff *work, const char **mangled,
2119 string *tname, string *trawname,
2120 int is_type, int remember)
252b5132
RH
2121{
2122 int i;
2123 int r;
2124 int need_comma = 0;
2125 int success = 0;
252b5132
RH
2126 int is_java_array = 0;
2127 string temp;
252b5132
RH
2128
2129 (*mangled)++;
2130 if (is_type)
2131 {
252b5132
RH
2132 /* get template name */
2133 if (**mangled == 'z')
2134 {
2135 int idx;
2136 (*mangled)++;
2137 (*mangled)++;
2138
2139 idx = consume_count_with_underscores (mangled);
2140 if (idx == -1
2141 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2142 || consume_count_with_underscores (mangled) == -1)
2143 return (0);
2144
2145 if (work->tmpl_argvec)
2146 {
2147 string_append (tname, work->tmpl_argvec[idx]);
2148 if (trawname)
2149 string_append (trawname, work->tmpl_argvec[idx]);
2150 }
2151 else
2152 {
0c0a36a4 2153 string_append_template_idx (tname, idx);
252b5132 2154 if (trawname)
0c0a36a4 2155 string_append_template_idx (trawname, idx);
252b5132
RH
2156 }
2157 }
2158 else
2159 {
2160 if ((r = consume_count (mangled)) <= 0
2161 || (int) strlen (*mangled) < r)
2162 {
2163 return (0);
2164 }
2165 is_java_array = (work -> options & DMGL_JAVA)
2166 && strncmp (*mangled, "JArray1Z", 8) == 0;
2167 if (! is_java_array)
2168 {
2169 string_appendn (tname, *mangled, r);
2170 }
2171 if (trawname)
2172 string_appendn (trawname, *mangled, r);
2173 *mangled += r;
2174 }
2175 }
2176 if (!is_java_array)
2177 string_append (tname, "<");
2178 /* get size of template parameter list */
2179 if (!get_count (mangled, &r))
2180 {
2181 return (0);
2182 }
2183 if (!is_type)
2184 {
2185 /* Create an array for saving the template argument values. */
abf6a75b 2186 work->tmpl_argvec = XNEWVEC (char *, r);
252b5132
RH
2187 work->ntmpl_args = r;
2188 for (i = 0; i < r; i++)
2189 work->tmpl_argvec[i] = 0;
2190 }
2191 for (i = 0; i < r; i++)
2192 {
2193 if (need_comma)
2194 {
2195 string_append (tname, ", ");
2196 }
2197 /* Z for type parameters */
2198 if (**mangled == 'Z')
2199 {
2200 (*mangled)++;
2201 /* temp is initialized in do_type */
2202 success = do_type (work, mangled, &temp);
2203 if (success)
2204 {
2205 string_appends (tname, &temp);
2206
2207 if (!is_type)
2208 {
2209 /* Save the template argument. */
2210 int len = temp.p - temp.b;
abf6a75b 2211 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
252b5132
RH
2212 memcpy (work->tmpl_argvec[i], temp.b, len);
2213 work->tmpl_argvec[i][len] = '\0';
2214 }
2215 }
2216 string_delete(&temp);
2217 if (!success)
2218 {
2219 break;
2220 }
2221 }
2222 /* z for template parameters */
2223 else if (**mangled == 'z')
2224 {
2225 int r2;
2226 (*mangled)++;
2227 success = demangle_template_template_parm (work, mangled, tname);
2228
2229 if (success
2230 && (r2 = consume_count (mangled)) > 0
2231 && (int) strlen (*mangled) >= r2)
2232 {
2233 string_append (tname, " ");
2234 string_appendn (tname, *mangled, r2);
2235 if (!is_type)
2236 {
2237 /* Save the template argument. */
2238 int len = r2;
abf6a75b 2239 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
252b5132
RH
2240 memcpy (work->tmpl_argvec[i], *mangled, len);
2241 work->tmpl_argvec[i][len] = '\0';
2242 }
2243 *mangled += r2;
2244 }
2245 if (!success)
2246 {
2247 break;
2248 }
2249 }
2250 else
2251 {
2252 string param;
2253 string* s;
2254
2255 /* otherwise, value parameter */
2256
2257 /* temp is initialized in do_type */
2258 success = do_type (work, mangled, &temp);
2259 string_delete(&temp);
2260 if (!success)
2261 break;
2262
2263 if (!is_type)
2264 {
2265 s = &param;
2266 string_init (s);
2267 }
2268 else
2269 s = tname;
2270
2271 success = demangle_template_value_parm (work, mangled, s,
2272 (type_kind_t) success);
2273
2274 if (!success)
2275 {
2276 if (!is_type)
2277 string_delete (s);
2278 success = 0;
2279 break;
2280 }
2281
2282 if (!is_type)
2283 {
2284 int len = s->p - s->b;
abf6a75b 2285 work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
252b5132
RH
2286 memcpy (work->tmpl_argvec[i], s->b, len);
2287 work->tmpl_argvec[i][len] = '\0';
2288
2289 string_appends (tname, s);
2290 string_delete (s);
2291 }
2292 }
2293 need_comma = 1;
2294 }
2295 if (is_java_array)
2296 {
2297 string_append (tname, "[]");
2298 }
2299 else
2300 {
2301 if (tname->p[-1] == '>')
2302 string_append (tname, " ");
2303 string_append (tname, ">");
2304 }
2305
2306 if (is_type && remember)
eec65ede
JB
2307 {
2308 const int bindex = register_Btype (work);
2309 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
2310 }
252b5132
RH
2311
2312 /*
2313 if (work -> static_type)
2314 {
2315 string_append (declp, *mangled + 1);
2316 *mangled += strlen (*mangled);
2317 success = 1;
2318 }
2319 else
2320 {
2321 success = demangle_args (work, mangled, declp);
2322 }
2323 }
2324 */
2325 return (success);
2326}
2327
2328static int
9334f9c6
DD
2329arm_pt (struct work_stuff *work, const char *mangled,
2330 int n, const char **anchor, const char **args)
252b5132
RH
2331{
2332 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2333 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
20d54542 2334 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
252b5132
RH
2335 {
2336 int len;
2337 *args = *anchor + 6;
2338 len = consume_count (args);
2339 if (len == -1)
2340 return 0;
2341 if (*args + len == mangled + n && **args == '_')
2342 {
2343 ++*args;
2344 return 1;
2345 }
2346 }
2347 if (AUTO_DEMANGLING || EDG_DEMANGLING)
2348 {
20d54542
DD
2349 if ((*anchor = strstr (mangled, "__tm__"))
2350 || (*anchor = strstr (mangled, "__ps__"))
2351 || (*anchor = strstr (mangled, "__pt__")))
252b5132
RH
2352 {
2353 int len;
2354 *args = *anchor + 6;
2355 len = consume_count (args);
2356 if (len == -1)
2357 return 0;
2358 if (*args + len == mangled + n && **args == '_')
2359 {
2360 ++*args;
2361 return 1;
2362 }
2363 }
20d54542 2364 else if ((*anchor = strstr (mangled, "__S")))
252b5132
RH
2365 {
2366 int len;
2367 *args = *anchor + 3;
2368 len = consume_count (args);
2369 if (len == -1)
2370 return 0;
2371 if (*args + len == mangled + n && **args == '_')
2372 {
2373 ++*args;
2374 return 1;
2375 }
2376 }
2377 }
2378
2379 return 0;
2380}
2381
2382static void
9334f9c6
DD
2383demangle_arm_hp_template (struct work_stuff *work, const char **mangled,
2384 int n, string *declp)
252b5132
RH
2385{
2386 const char *p;
2387 const char *args;
2388 const char *e = *mangled + n;
2389 string arg;
2390
2391 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2392 template args */
2393 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
2394 {
2395 char *start_spec_args = NULL;
820542c9 2396 int hold_options;
252b5132
RH
2397
2398 /* First check for and omit template specialization pseudo-arguments,
2399 such as in "Spec<#1,#1.*>" */
2400 start_spec_args = strchr (*mangled, '<');
2401 if (start_spec_args && (start_spec_args - *mangled < n))
2402 string_appendn (declp, *mangled, start_spec_args - *mangled);
2403 else
2404 string_appendn (declp, *mangled, n);
2405 (*mangled) += n + 1;
2406 string_init (&arg);
2407 if (work->temp_start == -1) /* non-recursive call */
2408 work->temp_start = declp->p - declp->b;
820542c9
DD
2409
2410 /* We want to unconditionally demangle parameter types in
2411 template parameters. */
2412 hold_options = work->options;
2413 work->options |= DMGL_PARAMS;
2414
252b5132
RH
2415 string_append (declp, "<");
2416 while (1)
2417 {
fcd7c7c9 2418 string_delete (&arg);
252b5132
RH
2419 switch (**mangled)
2420 {
2421 case 'T':
2422 /* 'T' signals a type parameter */
2423 (*mangled)++;
2424 if (!do_type (work, mangled, &arg))
2425 goto hpacc_template_args_done;
2426 break;
2427
2428 case 'U':
2429 case 'S':
2430 /* 'U' or 'S' signals an integral value */
2431 if (!do_hpacc_template_const_value (work, mangled, &arg))
2432 goto hpacc_template_args_done;
2433 break;
2434
2435 case 'A':
2436 /* 'A' signals a named constant expression (literal) */
2437 if (!do_hpacc_template_literal (work, mangled, &arg))
2438 goto hpacc_template_args_done;
2439 break;
2440
2441 default:
2442 /* Today, 1997-09-03, we have only the above types
2443 of template parameters */
2444 /* FIXME: maybe this should fail and return null */
2445 goto hpacc_template_args_done;
2446 }
2447 string_appends (declp, &arg);
2448 /* Check if we're at the end of template args.
2449 0 if at end of static member of template class,
2450 _ if done with template args for a function */
2451 if ((**mangled == '\000') || (**mangled == '_'))
2452 break;
2453 else
2454 string_append (declp, ",");
2455 }
2456 hpacc_template_args_done:
2457 string_append (declp, ">");
2458 string_delete (&arg);
2459 if (**mangled == '_')
2460 (*mangled)++;
820542c9 2461 work->options = hold_options;
252b5132
RH
2462 return;
2463 }
2464 /* ARM template? (Also handles HP cfront extensions) */
2465 else if (arm_pt (work, *mangled, n, &p, &args))
2466 {
820542c9 2467 int hold_options;
252b5132
RH
2468 string type_str;
2469
2470 string_init (&arg);
2471 string_appendn (declp, *mangled, p - *mangled);
2472 if (work->temp_start == -1) /* non-recursive call */
2473 work->temp_start = declp->p - declp->b;
820542c9
DD
2474
2475 /* We want to unconditionally demangle parameter types in
2476 template parameters. */
2477 hold_options = work->options;
2478 work->options |= DMGL_PARAMS;
2479
252b5132
RH
2480 string_append (declp, "<");
2481 /* should do error checking here */
2482 while (args < e) {
fcd7c7c9 2483 string_delete (&arg);
252b5132
RH
2484
2485 /* Check for type or literal here */
2486 switch (*args)
2487 {
2488 /* HP cfront extensions to ARM for template args */
2489 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2490 /* FIXME: We handle only numeric literals for HP cfront */
2491 case 'X':
2492 /* A typed constant value follows */
2493 args++;
2494 if (!do_type (work, &args, &type_str))
2495 goto cfront_template_args_done;
2496 string_append (&arg, "(");
2497 string_appends (&arg, &type_str);
fcd7c7c9 2498 string_delete (&type_str);
252b5132
RH
2499 string_append (&arg, ")");
2500 if (*args != 'L')
2501 goto cfront_template_args_done;
2502 args++;
2503 /* Now snarf a literal value following 'L' */
2504 if (!snarf_numeric_literal (&args, &arg))
2505 goto cfront_template_args_done;
2506 break;
2507
2508 case 'L':
2509 /* Snarf a literal following 'L' */
2510 args++;
2511 if (!snarf_numeric_literal (&args, &arg))
2512 goto cfront_template_args_done;
2513 break;
2514 default:
2515 /* Not handling other HP cfront stuff */
59ea9fe7
DD
2516 {
2517 const char* old_args = args;
2518 if (!do_type (work, &args, &arg))
2519 goto cfront_template_args_done;
2520
2521 /* Fail if we didn't make any progress: prevent infinite loop. */
2522 if (args == old_args)
820542c9
DD
2523 {
2524 work->options = hold_options;
2525 return;
2526 }
59ea9fe7 2527 }
252b5132
RH
2528 }
2529 string_appends (declp, &arg);
2530 string_append (declp, ",");
2531 }
2532 cfront_template_args_done:
2533 string_delete (&arg);
2534 if (args >= e)
2535 --declp->p; /* remove extra comma */
2536 string_append (declp, ">");
820542c9 2537 work->options = hold_options;
252b5132
RH
2538 }
2539 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2540 && (*mangled)[9] == 'N'
2541 && (*mangled)[8] == (*mangled)[10]
2542 && strchr (cplus_markers, (*mangled)[8]))
2543 {
2544 /* A member of the anonymous namespace. */
2545 string_append (declp, "{anonymous}");
2546 }
2547 else
2548 {
2549 if (work->temp_start == -1) /* non-recursive call only */
2550 work->temp_start = 0; /* disable in recursive calls */
2551 string_appendn (declp, *mangled, n);
2552 }
2553 *mangled += n;
2554}
2555
2556/* Extract a class name, possibly a template with arguments, from the
2557 mangled string; qualifiers, local class indicators, etc. have
2558 already been dealt with */
2559
2560static int
9334f9c6
DD
2561demangle_class_name (struct work_stuff *work, const char **mangled,
2562 string *declp)
252b5132
RH
2563{
2564 int n;
2565 int success = 0;
2566
2567 n = consume_count (mangled);
2568 if (n == -1)
2569 return 0;
2570 if ((int) strlen (*mangled) >= n)
2571 {
2572 demangle_arm_hp_template (work, mangled, n, declp);
2573 success = 1;
2574 }
2575
2576 return (success);
2577}
2578
2579/*
2580
2581LOCAL FUNCTION
2582
2583 demangle_class -- demangle a mangled class sequence
2584
2585SYNOPSIS
2586
2587 static int
2588 demangle_class (struct work_stuff *work, const char **mangled,
2589 strint *declp)
2590
2591DESCRIPTION
2592
2593 DECLP points to the buffer into which demangling is being done.
2594
2595 *MANGLED points to the current token to be demangled. On input,
2596 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2597 On exit, it points to the next token after the mangled class on
2598 success, or the first unconsumed token on failure.
2599
2600 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2601 we are demangling a constructor or destructor. In this case
2602 we prepend "class::class" or "class::~class" to DECLP.
2603
2604 Otherwise, we prepend "class::" to the current DECLP.
2605
2606 Reset the constructor/destructor flags once they have been
2607 "consumed". This allows demangle_class to be called later during
2608 the same demangling, to do normal class demangling.
2609
2610 Returns 1 if demangling is successful, 0 otherwise.
2611
2612*/
2613
2614static int
9334f9c6 2615demangle_class (struct work_stuff *work, const char **mangled, string *declp)
252b5132
RH
2616{
2617 int success = 0;
2618 int btype;
2619 string class_name;
2620 char *save_class_name_end = 0;
2621
2622 string_init (&class_name);
2623 btype = register_Btype (work);
2624 if (demangle_class_name (work, mangled, &class_name))
2625 {
2626 save_class_name_end = class_name.p;
2627 if ((work->constructor & 1) || (work->destructor & 1))
2628 {
2629 /* adjust so we don't include template args */
2630 if (work->temp_start && (work->temp_start != -1))
2631 {
2632 class_name.p = class_name.b + work->temp_start;
2633 }
2634 string_prepends (declp, &class_name);
2635 if (work -> destructor & 1)
2636 {
2637 string_prepend (declp, "~");
2638 work -> destructor -= 1;
2639 }
2640 else
2641 {
2642 work -> constructor -= 1;
2643 }
2644 }
2645 class_name.p = save_class_name_end;
2646 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2647 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2648 string_prepend (declp, SCOPE_STRING (work));
2649 string_prepends (declp, &class_name);
2650 success = 1;
2651 }
2652 string_delete (&class_name);
2653 return (success);
2654}
2655
e8865c28
DB
2656
2657/* Called when there's a "__" in the mangled name, with `scan' pointing to
2658 the rightmost guess.
2659
2660 Find the correct "__"-sequence where the function name ends and the
2661 signature starts, which is ambiguous with GNU mangling.
2662 Call demangle_signature here, so we can make sure we found the right
2663 one; *mangled will be consumed so caller will not make further calls to
2664 demangle_signature. */
2665
2666static int
9334f9c6
DD
2667iterate_demangle_function (struct work_stuff *work, const char **mangled,
2668 string *declp, const char *scan)
e8865c28
DB
2669{
2670 const char *mangle_init = *mangled;
2671 int success = 0;
2672 string decl_init;
2673 struct work_stuff work_init;
2674
2675 if (*(scan + 2) == '\0')
2676 return 0;
2677
2678 /* Do not iterate for some demangling modes, or if there's only one
2679 "__"-sequence. This is the normal case. */
2680 if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
20d54542 2681 || strstr (scan + 2, "__") == NULL)
b4b41ceb 2682 return demangle_function_name (work, mangled, declp, scan);
e8865c28
DB
2683
2684 /* Save state so we can restart if the guess at the correct "__" was
2685 wrong. */
2686 string_init (&decl_init);
2687 string_appends (&decl_init, declp);
2688 memset (&work_init, 0, sizeof work_init);
2689 work_stuff_copy_to_from (&work_init, work);
2690
2691 /* Iterate over occurrences of __, allowing names and types to have a
2692 "__" sequence in them. We must start with the first (not the last)
2693 occurrence, since "__" most often occur between independent mangled
2694 parts, hence starting at the last occurence inside a signature
2695 might get us a "successful" demangling of the signature. */
2696
2697 while (scan[2])
2698 {
b4b41ceb
BE
2699 if (demangle_function_name (work, mangled, declp, scan))
2700 {
2701 success = demangle_signature (work, mangled, declp);
2702 if (success)
2703 break;
2704 }
e8865c28
DB
2705
2706 /* Reset demangle state for the next round. */
2707 *mangled = mangle_init;
2708 string_clear (declp);
2709 string_appends (declp, &decl_init);
2710 work_stuff_copy_to_from (work, &work_init);
2711
2712 /* Leave this underscore-sequence. */
2713 scan += 2;
2714
2715 /* Scan for the next "__" sequence. */
2716 while (*scan && (scan[0] != '_' || scan[1] != '_'))
2717 scan++;
2718
2719 /* Move to last "__" in this sequence. */
2720 while (*scan && *scan == '_')
2721 scan++;
2722 scan -= 2;
2723 }
2724
2725 /* Delete saved state. */
2726 delete_work_stuff (&work_init);
2727 string_delete (&decl_init);
2728
2729 return success;
2730}
2731
252b5132
RH
2732/*
2733
2734LOCAL FUNCTION
2735
2736 demangle_prefix -- consume the mangled name prefix and find signature
2737
2738SYNOPSIS
2739
2740 static int
2741 demangle_prefix (struct work_stuff *work, const char **mangled,
2742 string *declp);
2743
2744DESCRIPTION
2745
2746 Consume and demangle the prefix of the mangled name.
e8865c28
DB
2747 While processing the function name root, arrange to call
2748 demangle_signature if the root is ambiguous.
252b5132
RH
2749
2750 DECLP points to the string buffer into which demangled output is
2751 placed. On entry, the buffer is empty. On exit it contains
2752 the root function name, the demangled operator name, or in some
2753 special cases either nothing or the completely demangled result.
2754
2755 MANGLED points to the current pointer into the mangled name. As each
2756 token of the mangled name is consumed, it is updated. Upon entry
2757 the current mangled name pointer points to the first character of
2758 the mangled name. Upon exit, it should point to the first character
2759 of the signature if demangling was successful, or to the first
2760 unconsumed character if demangling of the prefix was unsuccessful.
2761
2762 Returns 1 on success, 0 otherwise.
2763 */
2764
2765static int
9334f9c6
DD
2766demangle_prefix (struct work_stuff *work, const char **mangled,
2767 string *declp)
252b5132
RH
2768{
2769 int success = 1;
2770 const char *scan;
2771 int i;
2772
2773 if (strlen(*mangled) > 6
2774 && (strncmp(*mangled, "_imp__", 6) == 0
2775 || strncmp(*mangled, "__imp_", 6) == 0))
2776 {
2777 /* it's a symbol imported from a PE dynamic library. Check for both
2778 new style prefix _imp__ and legacy __imp_ used by older versions
2779 of dlltool. */
2780 (*mangled) += 6;
2781 work->dllimported = 1;
2782 }
2783 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2784 {
2785 char *marker = strchr (cplus_markers, (*mangled)[8]);
2786 if (marker != NULL && *marker == (*mangled)[10])
2787 {
2788 if ((*mangled)[9] == 'D')
2789 {
2790 /* it's a GNU global destructor to be executed at program exit */
2791 (*mangled) += 11;
2792 work->destructor = 2;
2793 if (gnu_special (work, mangled, declp))
2794 return success;
2795 }
2796 else if ((*mangled)[9] == 'I')
2797 {
2798 /* it's a GNU global constructor to be executed at program init */
2799 (*mangled) += 11;
2800 work->constructor = 2;
2801 if (gnu_special (work, mangled, declp))
2802 return success;
2803 }
2804 }
2805 }
2806 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2807 {
2808 /* it's a ARM global destructor to be executed at program exit */
2809 (*mangled) += 7;
2810 work->destructor = 2;
2811 }
2812 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2813 {
2814 /* it's a ARM global constructor to be executed at program initial */
2815 (*mangled) += 7;
2816 work->constructor = 2;
2817 }
2818
2819 /* This block of code is a reduction in strength time optimization
2820 of:
20d54542 2821 scan = strstr (*mangled, "__"); */
252b5132
RH
2822
2823 {
2824 scan = *mangled;
2825
2826 do {
2827 scan = strchr (scan, '_');
2828 } while (scan != NULL && *++scan != '_');
2829
2830 if (scan != NULL) --scan;
2831 }
2832
2833 if (scan != NULL)
2834 {
2835 /* We found a sequence of two or more '_', ensure that we start at
2836 the last pair in the sequence. */
2837 i = strspn (scan, "_");
2838 if (i > 2)
2839 {
2840 scan += (i - 2);
2841 }
2842 }
2843
2844 if (scan == NULL)
2845 {
2846 success = 0;
2847 }
2848 else if (work -> static_type)
2849 {
ac424eb3 2850 if (!ISDIGIT ((unsigned char)scan[0]) && (scan[0] != 't'))
252b5132
RH
2851 {
2852 success = 0;
2853 }
2854 }
2855 else if ((scan == *mangled)
ac424eb3 2856 && (ISDIGIT ((unsigned char)scan[2]) || (scan[2] == 'Q')
252b5132
RH
2857 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2858 {
2859 /* The ARM says nothing about the mangling of local variables.
2860 But cfront mangles local variables by prepending __<nesting_level>
2861 to them. As an extension to ARM demangling we handle this case. */
2862 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
ac424eb3 2863 && ISDIGIT ((unsigned char)scan[2]))
252b5132
RH
2864 {
2865 *mangled = scan + 2;
2866 consume_count (mangled);
2867 string_append (declp, *mangled);
2868 *mangled += strlen (*mangled);
2869 success = 1;
2870 }
2871 else
2872 {
2873 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2874 names like __Q2_3foo3bar for nested type names. So don't accept
2875 this style of constructor for cfront demangling. A GNU
2876 style member-template constructor starts with 'H'. */
2877 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2878 work -> constructor += 1;
2879 *mangled = scan + 2;
2880 }
2881 }
2882 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2883 {
2884 /* Cfront-style parameterized type. Handled later as a signature. */
2885 success = 1;
2886
2887 /* ARM template? */
2888 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2889 }
2890 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2891 || (scan[2] == 'p' && scan[3] == 's')
2892 || (scan[2] == 'p' && scan[3] == 't')))
2893 {
2894 /* EDG-style parameterized type. Handled later as a signature. */
2895 success = 1;
2896
2897 /* EDG template? */
2898 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2899 }
ac424eb3 2900 else if ((scan == *mangled) && !ISDIGIT ((unsigned char)scan[2])
252b5132
RH
2901 && (scan[2] != 't'))
2902 {
2903 /* Mangled name starts with "__". Skip over any leading '_' characters,
2904 then find the next "__" that separates the prefix from the signature.
2905 */
2906 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2907 || (arm_special (mangled, declp) == 0))
2908 {
2909 while (*scan == '_')
2910 {
2911 scan++;
2912 }
20d54542 2913 if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
252b5132
RH
2914 {
2915 /* No separator (I.E. "__not_mangled"), or empty signature
2916 (I.E. "__not_mangled_either__") */
2917 success = 0;
2918 }
2919 else
e8865c28 2920 return iterate_demangle_function (work, mangled, declp, scan);
252b5132
RH
2921 }
2922 }
2923 else if (*(scan + 2) != '\0')
2924 {
2925 /* Mangled name does not start with "__" but does have one somewhere
2926 in there with non empty stuff after it. Looks like a global
e8865c28
DB
2927 function name. Iterate over all "__":s until the right
2928 one is found. */
2929 return iterate_demangle_function (work, mangled, declp, scan);
252b5132
RH
2930 }
2931 else
2932 {
2933 /* Doesn't look like a mangled name */
2934 success = 0;
2935 }
2936
2937 if (!success && (work->constructor == 2 || work->destructor == 2))
2938 {
2939 string_append (declp, *mangled);
2940 *mangled += strlen (*mangled);
2941 success = 1;
2942 }
2943 return (success);
2944}
2945
2946/*
2947
2948LOCAL FUNCTION
2949
2950 gnu_special -- special handling of gnu mangled strings
2951
2952SYNOPSIS
2953
2954 static int
2955 gnu_special (struct work_stuff *work, const char **mangled,
2956 string *declp);
2957
2958
2959DESCRIPTION
2960
2961 Process some special GNU style mangling forms that don't fit
2962 the normal pattern. For example:
2963
2964 _$_3foo (destructor for class foo)
2965 _vt$foo (foo virtual table)
2966 _vt$foo$bar (foo::bar virtual table)
2967 __vt_foo (foo virtual table, new style with thunks)
2968 _3foo$varname (static data member)
2969 _Q22rs2tu$vw (static data member)
2970 __t6vector1Zii (constructor with template)
2971 __thunk_4__$_7ostream (virtual function thunk)
2972 */
2973
2974static int
9334f9c6 2975gnu_special (struct work_stuff *work, const char **mangled, string *declp)
252b5132
RH
2976{
2977 int n;
2978 int success = 1;
2979 const char *p;
2980
2981 if ((*mangled)[0] == '_'
2982 && strchr (cplus_markers, (*mangled)[1]) != NULL
2983 && (*mangled)[2] == '_')
2984 {
2985 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2986 (*mangled) += 3;
2987 work -> destructor += 1;
2988 }
2989 else if ((*mangled)[0] == '_'
2990 && (((*mangled)[1] == '_'
2991 && (*mangled)[2] == 'v'
2992 && (*mangled)[3] == 't'
2993 && (*mangled)[4] == '_')
2994 || ((*mangled)[1] == 'v'
2995 && (*mangled)[2] == 't'
2996 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2997 {
2998 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2999 and create the decl. Note that we consume the entire mangled
3000 input string, which means that demangle_signature has no work
3001 to do. */
3002 if ((*mangled)[2] == 'v')
3003 (*mangled) += 5; /* New style, with thunks: "__vt_" */
3004 else
3005 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
3006 while (**mangled != '\0')
3007 {
3008 switch (**mangled)
3009 {
3010 case 'Q':
3011 case 'K':
3012 success = demangle_qualified (work, mangled, declp, 0, 1);
3013 break;
3014 case 't':
3015 success = demangle_template (work, mangled, declp, 0, 1,
3016 1);
3017 break;
3018 default:
ac424eb3 3019 if (ISDIGIT((unsigned char)*mangled[0]))
252b5132
RH
3020 {
3021 n = consume_count(mangled);
3022 /* We may be seeing a too-large size, or else a
3023 ".<digits>" indicating a static local symbol. In
3024 any case, declare victory and move on; *don't* try
3025 to use n to allocate. */
3026 if (n > (int) strlen (*mangled))
3027 {
3028 success = 1;
3029 break;
3030 }
fa3fcee7
NC
3031 else if (n == -1)
3032 {
3033 success = 0;
3034 break;
3035 }
252b5132
RH
3036 }
3037 else
3038 {
3039 n = strcspn (*mangled, cplus_markers);
3040 }
3041 string_appendn (declp, *mangled, n);
3042 (*mangled) += n;
3043 }
3044
3045 p = strpbrk (*mangled, cplus_markers);
3046 if (success && ((p == NULL) || (p == *mangled)))
3047 {
3048 if (p != NULL)
3049 {
3050 string_append (declp, SCOPE_STRING (work));
3051 (*mangled)++;
3052 }
3053 }
3054 else
3055 {
3056 success = 0;
3057 break;
3058 }
3059 }
3060 if (success)
3061 string_append (declp, " virtual table");
3062 }
3063 else if ((*mangled)[0] == '_'
3064 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
3065 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
3066 {
3067 /* static data member, "_3foo$varname" for example */
3068 (*mangled)++;
3069 switch (**mangled)
3070 {
3071 case 'Q':
3072 case 'K':
3073 success = demangle_qualified (work, mangled, declp, 0, 1);
3074 break;
3075 case 't':
3076 success = demangle_template (work, mangled, declp, 0, 1, 1);
3077 break;
3078 default:
3079 n = consume_count (mangled);
0c0a36a4 3080 if (n < 0 || n > (long) strlen (*mangled))
252b5132
RH
3081 {
3082 success = 0;
3083 break;
3084 }
d3e85005
HPN
3085
3086 if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
3087 && (*mangled)[9] == 'N'
3088 && (*mangled)[8] == (*mangled)[10]
3089 && strchr (cplus_markers, (*mangled)[8]))
3090 {
3091 /* A member of the anonymous namespace. There's information
3092 about what identifier or filename it was keyed to, but
3093 it's just there to make the mangled name unique; we just
3094 step over it. */
3095 string_append (declp, "{anonymous}");
3096 (*mangled) += n;
3097
3098 /* Now p points to the marker before the N, so we need to
3099 update it to the first marker after what we consumed. */
3100 p = strpbrk (*mangled, cplus_markers);
3101 break;
3102 }
3103
252b5132
RH
3104 string_appendn (declp, *mangled, n);
3105 (*mangled) += n;
3106 }
3107 if (success && (p == *mangled))
3108 {
3109 /* Consumed everything up to the cplus_marker, append the
3110 variable name. */
3111 (*mangled)++;
3112 string_append (declp, SCOPE_STRING (work));
3113 n = strlen (*mangled);
3114 string_appendn (declp, *mangled, n);
3115 (*mangled) += n;
3116 }
3117 else
3118 {
3119 success = 0;
3120 }
3121 }
3122 else if (strncmp (*mangled, "__thunk_", 8) == 0)
3123 {
3124 int delta;
3125
3126 (*mangled) += 8;
3127 delta = consume_count (mangled);
3128 if (delta == -1)
3129 success = 0;
3130 else
3131 {
3132 char *method = internal_cplus_demangle (work, ++*mangled);
3133
3134 if (method)
3135 {
3136 char buf[50];
3137 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
3138 string_append (declp, buf);
3139 string_append (declp, method);
3140 free (method);
3141 n = strlen (*mangled);
3142 (*mangled) += n;
3143 }
3144 else
3145 {
3146 success = 0;
3147 }
3148 }
3149 }
3150 else if (strncmp (*mangled, "__t", 3) == 0
3151 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
3152 {
3153 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
3154 (*mangled) += 4;
3155 switch (**mangled)
3156 {
3157 case 'Q':
3158 case 'K':
3159 success = demangle_qualified (work, mangled, declp, 0, 1);
3160 break;
3161 case 't':
3162 success = demangle_template (work, mangled, declp, 0, 1, 1);
3163 break;
3164 default:
1dc349e5 3165 success = do_type (work, mangled, declp);
252b5132
RH
3166 break;
3167 }
3168 if (success && **mangled != '\0')
3169 success = 0;
3170 if (success)
3171 string_append (declp, p);
3172 }
3173 else
3174 {
3175 success = 0;
3176 }
3177 return (success);
3178}
3179
3180static void
9334f9c6
DD
3181recursively_demangle(struct work_stuff *work, const char **mangled,
3182 string *result, int namelength)
252b5132
RH
3183{
3184 char * recurse = (char *)NULL;
3185 char * recurse_dem = (char *)NULL;
3186
abf6a75b 3187 recurse = XNEWVEC (char, namelength + 1);
252b5132
RH
3188 memcpy (recurse, *mangled, namelength);
3189 recurse[namelength] = '\000';
3190
3191 recurse_dem = cplus_demangle (recurse, work->options);
3192
3193 if (recurse_dem)
3194 {
3195 string_append (result, recurse_dem);
3196 free (recurse_dem);
3197 }
3198 else
3199 {
3200 string_appendn (result, *mangled, namelength);
3201 }
3202 free (recurse);
3203 *mangled += namelength;
3204}
3205
3206/*
3207
3208LOCAL FUNCTION
3209
3210 arm_special -- special handling of ARM/lucid mangled strings
3211
3212SYNOPSIS
3213
3214 static int
3215 arm_special (const char **mangled,
3216 string *declp);
3217
3218
3219DESCRIPTION
3220
3221 Process some special ARM style mangling forms that don't fit
3222 the normal pattern. For example:
3223
3224 __vtbl__3foo (foo virtual table)
3225 __vtbl__3foo__3bar (bar::foo virtual table)
3226
3227 */
3228
3229static int
9334f9c6 3230arm_special (const char **mangled, string *declp)
252b5132
RH
3231{
3232 int n;
3233 int success = 1;
3234 const char *scan;
3235
3236 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
3237 {
3238 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3239 and create the decl. Note that we consume the entire mangled
3240 input string, which means that demangle_signature has no work
3241 to do. */
3242 scan = *mangled + ARM_VTABLE_STRLEN;
3243 while (*scan != '\0') /* first check it can be demangled */
3244 {
3245 n = consume_count (&scan);
3246 if (n == -1)
3247 {
3248 return (0); /* no good */
3249 }
3250 scan += n;
3251 if (scan[0] == '_' && scan[1] == '_')
3252 {
3253 scan += 2;
3254 }
3255 }
3256 (*mangled) += ARM_VTABLE_STRLEN;
3257 while (**mangled != '\0')
3258 {
3259 n = consume_count (mangled);
3260 if (n == -1
0c0a36a4 3261 || n > (long) strlen (*mangled))
252b5132
RH
3262 return 0;
3263 string_prependn (declp, *mangled, n);
3264 (*mangled) += n;
3265 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
3266 {
3267 string_prepend (declp, "::");
3268 (*mangled) += 2;
3269 }
3270 }
3271 string_append (declp, " virtual table");
3272 }
3273 else
3274 {
3275 success = 0;
3276 }
3277 return (success);
3278}
3279
3280/*
3281
3282LOCAL FUNCTION
3283
3284 demangle_qualified -- demangle 'Q' qualified name strings
3285
3286SYNOPSIS
3287
3288 static int
3289 demangle_qualified (struct work_stuff *, const char *mangled,
3290 string *result, int isfuncname, int append);
3291
3292DESCRIPTION
3293
3294 Demangle a qualified name, such as "Q25Outer5Inner" which is
3295 the mangled form of "Outer::Inner". The demangled output is
3296 prepended or appended to the result string according to the
3297 state of the append flag.
3298
3299 If isfuncname is nonzero, then the qualified name we are building
3300 is going to be used as a member function name, so if it is a
3301 constructor or destructor function, append an appropriate
3302 constructor or destructor name. I.E. for the above example,
3303 the result for use as a constructor is "Outer::Inner::Inner"
3304 and the result for use as a destructor is "Outer::Inner::~Inner".
3305
3306BUGS
3307
3308 Numeric conversion is ASCII dependent (FIXME).
3309
3310 */
3311
3312static int
9334f9c6
DD
3313demangle_qualified (struct work_stuff *work, const char **mangled,
3314 string *result, int isfuncname, int append)
252b5132
RH
3315{
3316 int qualifiers = 0;
3317 int success = 1;
252b5132
RH
3318 char num[2];
3319 string temp;
3320 string last_name;
3321 int bindex = register_Btype (work);
3322
3323 /* We only make use of ISFUNCNAME if the entity is a constructor or
3324 destructor. */
3325 isfuncname = (isfuncname
3326 && ((work->constructor & 1) || (work->destructor & 1)));
3327
3328 string_init (&temp);
3329 string_init (&last_name);
3330
3331 if ((*mangled)[0] == 'K')
3332 {
3333 /* Squangling qualified name reuse */
3334 int idx;
3335 (*mangled)++;
3336 idx = consume_count_with_underscores (mangled);
3337 if (idx == -1 || idx >= work -> numk)
3338 success = 0;
3339 else
3340 string_append (&temp, work -> ktypevec[idx]);
3341 }
3342 else
3343 switch ((*mangled)[1])
3344 {
3345 case '_':
3346 /* GNU mangled name with more than 9 classes. The count is preceded
3347 by an underscore (to distinguish it from the <= 9 case) and followed
3348 by an underscore. */
0c0a36a4
ILT
3349 (*mangled)++;
3350 qualifiers = consume_count_with_underscores (mangled);
3351 if (qualifiers == -1)
252b5132 3352 success = 0;
252b5132
RH
3353 break;
3354
3355 case '1':
3356 case '2':
3357 case '3':
3358 case '4':
3359 case '5':
3360 case '6':
3361 case '7':
3362 case '8':
3363 case '9':
3364 /* The count is in a single digit. */
3365 num[0] = (*mangled)[1];
3366 num[1] = '\0';
3367 qualifiers = atoi (num);
3368
3369 /* If there is an underscore after the digit, skip it. This is
3370 said to be for ARM-qualified names, but the ARM makes no
3371 mention of such an underscore. Perhaps cfront uses one. */
3372 if ((*mangled)[2] == '_')
3373 {
3374 (*mangled)++;
3375 }
3376 (*mangled) += 2;
3377 break;
3378
3379 case '0':
3380 default:
3381 success = 0;
3382 }
3383
3384 if (!success)
3385 return success;
3386
3387 /* Pick off the names and collect them in the temp buffer in the order
3388 in which they are found, separated by '::'. */
3389
3390 while (qualifiers-- > 0)
3391 {
3392 int remember_K = 1;
3393 string_clear (&last_name);
3394
3395 if (*mangled[0] == '_')
3396 (*mangled)++;
3397
3398 if (*mangled[0] == 't')
3399 {
3400 /* Here we always append to TEMP since we will want to use
3401 the template name without the template parameters as a
3402 constructor or destructor name. The appropriate
3403 (parameter-less) value is returned by demangle_template
3404 in LAST_NAME. We do not remember the template type here,
3405 in order to match the G++ mangling algorithm. */
3406 success = demangle_template(work, mangled, &temp,
3407 &last_name, 1, 0);
3408 if (!success)
3409 break;
3410 }
3411 else if (*mangled[0] == 'K')
3412 {
3413 int idx;
3414 (*mangled)++;
3415 idx = consume_count_with_underscores (mangled);
3416 if (idx == -1 || idx >= work->numk)
3417 success = 0;
3418 else
3419 string_append (&temp, work->ktypevec[idx]);
3420 remember_K = 0;
3421
3422 if (!success) break;
3423 }
3424 else
3425 {
3426 if (EDG_DEMANGLING)
3427 {
3428 int namelength;
3429 /* Now recursively demangle the qualifier
3430 * This is necessary to deal with templates in
3431 * mangling styles like EDG */
3432 namelength = consume_count (mangled);
3433 if (namelength == -1)
3434 {
3435 success = 0;
3436 break;
3437 }
3438 recursively_demangle(work, mangled, &temp, namelength);
3439 }
3440 else
3441 {
fcd7c7c9 3442 string_delete (&last_name);
252b5132
RH
3443 success = do_type (work, mangled, &last_name);
3444 if (!success)
3445 break;
3446 string_appends (&temp, &last_name);
3447 }
3448 }
3449
3450 if (remember_K)
3451 remember_Ktype (work, temp.b, LEN_STRING (&temp));
3452
3453 if (qualifiers > 0)
3454 string_append (&temp, SCOPE_STRING (work));
3455 }
3456
3457 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
3458
3459 /* If we are using the result as a function name, we need to append
3460 the appropriate '::' separated constructor or destructor name.
3461 We do this here because this is the most convenient place, where
3462 we already have a pointer to the name and the length of the name. */
3463
3464 if (isfuncname)
3465 {
3466 string_append (&temp, SCOPE_STRING (work));
3467 if (work -> destructor & 1)
3468 string_append (&temp, "~");
3469 string_appends (&temp, &last_name);
3470 }
3471
3472 /* Now either prepend the temp buffer to the result, or append it,
3473 depending upon the state of the append flag. */
3474
3475 if (append)
3476 string_appends (result, &temp);
3477 else
3478 {
3479 if (!STRING_EMPTY (result))
3480 string_append (&temp, SCOPE_STRING (work));
3481 string_prepends (result, &temp);
3482 }
3483
3484 string_delete (&last_name);
3485 string_delete (&temp);
3486 return (success);
3487}
3488
3489/*
3490
3491LOCAL FUNCTION
3492
3493 get_count -- convert an ascii count to integer, consuming tokens
3494
3495SYNOPSIS
3496
3497 static int
3498 get_count (const char **type, int *count)
3499
3500DESCRIPTION
3501
3502 Assume that *type points at a count in a mangled name; set
3503 *count to its value, and set *type to the next character after
3504 the count. There are some weird rules in effect here.
3505
3506 If *type does not point at a string of digits, return zero.
3507
3508 If *type points at a string of digits followed by an
3509 underscore, set *count to their value as an integer, advance
3510 *type to point *after the underscore, and return 1.
3511
3512 If *type points at a string of digits not followed by an
3513 underscore, consume only the first digit. Set *count to its
3514 value as an integer, leave *type pointing after that digit,
3515 and return 1.
3516
3517 The excuse for this odd behavior: in the ARM and HP demangling
3518 styles, a type can be followed by a repeat count of the form
3519 `Nxy', where:
3520
3521 `x' is a single digit specifying how many additional copies
3522 of the type to append to the argument list, and
3523
3524 `y' is one or more digits, specifying the zero-based index of
3525 the first repeated argument in the list. Yes, as you're
3526 unmangling the name you can figure this out yourself, but
3527 it's there anyway.
3528
3529 So, for example, in `bar__3fooFPiN51', the first argument is a
3530 pointer to an integer (`Pi'), and then the next five arguments
3531 are the same (`N5'), and the first repeat is the function's
3532 second argument (`1').
3533*/
3534
3535static int
9334f9c6 3536get_count (const char **type, int *count)
252b5132
RH
3537{
3538 const char *p;
3539 int n;
3540
ac424eb3 3541 if (!ISDIGIT ((unsigned char)**type))
0c0a36a4 3542 return (0);
252b5132
RH
3543 else
3544 {
3545 *count = **type - '0';
3546 (*type)++;
ac424eb3 3547 if (ISDIGIT ((unsigned char)**type))
252b5132
RH
3548 {
3549 p = *type;
3550 n = *count;
3551 do
3552 {
3553 n *= 10;
3554 n += *p - '0';
3555 p++;
3556 }
ac424eb3 3557 while (ISDIGIT ((unsigned char)*p));
252b5132
RH
3558 if (*p == '_')
3559 {
3560 *type = p + 1;
3561 *count = n;
3562 }
3563 }
3564 }
3565 return (1);
3566}
3567
3568/* RESULT will be initialised here; it will be freed on failure. The
3569 value returned is really a type_kind_t. */
3570
3571static int
9334f9c6 3572do_type (struct work_stuff *work, const char **mangled, string *result)
252b5132
RH
3573{
3574 int n;
41f225de
NC
3575 int i;
3576 int is_proctypevec;
252b5132
RH
3577 int done;
3578 int success;
3579 string decl;
3580 const char *remembered_type;
3581 int type_quals;
252b5132
RH
3582 type_kind_t tk = tk_none;
3583
252b5132
RH
3584 string_init (&decl);
3585 string_init (result);
3586
3587 done = 0;
3588 success = 1;
41f225de 3589 is_proctypevec = 0;
252b5132
RH
3590 while (success && !done)
3591 {
3592 int member;
3593 switch (**mangled)
3594 {
3595
3596 /* A pointer type */
3597 case 'P':
3598 case 'p':
3599 (*mangled)++;
3600 if (! (work -> options & DMGL_JAVA))
3601 string_prepend (&decl, "*");
3602 if (tk == tk_none)
3603 tk = tk_pointer;
3604 break;
3605
3606 /* A reference type */
3607 case 'R':
3608 (*mangled)++;
3609 string_prepend (&decl, "&");
3610 if (tk == tk_none)
3611 tk = tk_reference;
3612 break;
3613
fa3fcee7
NC
3614 /* An rvalue reference type */
3615 case 'O':
3616 (*mangled)++;
3617 string_prepend (&decl, "&&");
3618 if (tk == tk_none)
3619 tk = tk_rvalue_reference;
3620 break;
3621
252b5132
RH
3622 /* An array */
3623 case 'A':
3624 {
3625 ++(*mangled);
3626 if (!STRING_EMPTY (&decl)
3627 && (decl.b[0] == '*' || decl.b[0] == '&'))
3628 {
3629 string_prepend (&decl, "(");
3630 string_append (&decl, ")");
3631 }
3632 string_append (&decl, "[");
3633 if (**mangled != '_')
3634 success = demangle_template_value_parm (work, mangled, &decl,
3635 tk_integral);
3636 if (**mangled == '_')
3637 ++(*mangled);
3638 string_append (&decl, "]");
3639 break;
3640 }
3641
3642 /* A back reference to a previously seen type */
3643 case 'T':
3644 (*mangled)++;
fa3fcee7 3645 if (!get_count (mangled, &n) || n < 0 || n >= work -> ntypes)
252b5132
RH
3646 {
3647 success = 0;
3648 }
3649 else
41f225de
NC
3650 for (i = 0; i < work->nproctypes; i++)
3651 if (work -> proctypevec [i] == n)
3652 success = 0;
3653
3654 if (success)
3655 {
3656 is_proctypevec = 1;
3657 push_processed_type (work, n);
3658 remembered_type = work->typevec[n];
252b5132
RH
3659 mangled = &remembered_type;
3660 }
3661 break;
3662
3663 /* A function */
3664 case 'F':
3665 (*mangled)++;
3666 if (!STRING_EMPTY (&decl)
3667 && (decl.b[0] == '*' || decl.b[0] == '&'))
3668 {
3669 string_prepend (&decl, "(");
3670 string_append (&decl, ")");
3671 }
3672 /* After picking off the function args, we expect to either find the
3673 function return type (preceded by an '_') or the end of the
3674 string. */
3675 if (!demangle_nested_args (work, mangled, &decl)
3676 || (**mangled != '_' && **mangled != '\0'))
3677 {
3678 success = 0;
3679 break;
3680 }
3681 if (success && (**mangled == '_'))
3682 (*mangled)++;
3683 break;
3684
3685 case 'M':
252b5132
RH
3686 {
3687 type_quals = TYPE_UNQUALIFIED;
3688
3689 member = **mangled == 'M';
3690 (*mangled)++;
252b5132
RH
3691
3692 string_append (&decl, ")");
0c0a36a4
ILT
3693
3694 /* We don't need to prepend `::' for a qualified name;
3695 demangle_qualified will do that for us. */
3696 if (**mangled != 'Q')
3697 string_prepend (&decl, SCOPE_STRING (work));
3698
ac424eb3 3699 if (ISDIGIT ((unsigned char)**mangled))
252b5132
RH
3700 {
3701 n = consume_count (mangled);
3702 if (n == -1
3703 || (int) strlen (*mangled) < n)
3704 {
3705 success = 0;
3706 break;
3707 }
3708 string_prependn (&decl, *mangled, n);
3709 *mangled += n;
3710 }
0c0a36a4
ILT
3711 else if (**mangled == 'X' || **mangled == 'Y')
3712 {
3713 string temp;
3714 do_type (work, mangled, &temp);
3715 string_prepends (&decl, &temp);
fcd7c7c9 3716 string_delete (&temp);
0c0a36a4
ILT
3717 }
3718 else if (**mangled == 't')
252b5132
RH
3719 {
3720 string temp;
3721 string_init (&temp);
3722 success = demangle_template (work, mangled, &temp,
3723 NULL, 1, 1);
3724 if (success)
3725 {
3726 string_prependn (&decl, temp.b, temp.p - temp.b);
fcd7c7c9 3727 string_delete (&temp);
252b5132
RH
3728 }
3729 else
173373c6
AB
3730 {
3731 string_delete (&temp);
3732 break;
3733 }
252b5132 3734 }
0c0a36a4
ILT
3735 else if (**mangled == 'Q')
3736 {
3737 success = demangle_qualified (work, mangled, &decl,
3738 /*isfuncnam=*/0,
3739 /*append=*/0);
3740 if (!success)
3741 break;
3742 }
3743 else
3744 {
3745 success = 0;
3746 break;
3747 }
3748
252b5132
RH
3749 string_prepend (&decl, "(");
3750 if (member)
3751 {
3752 switch (**mangled)
3753 {
3754 case 'C':
3755 case 'V':
3756 case 'u':
3757 type_quals |= code_for_qualifier (**mangled);
3758 (*mangled)++;
3759 break;
3760
3761 default:
3762 break;
3763 }
3764
3765 if (*(*mangled)++ != 'F')
3766 {
3767 success = 0;
3768 break;
3769 }
3770 }
3771 if ((member && !demangle_nested_args (work, mangled, &decl))
3772 || **mangled != '_')
3773 {
3774 success = 0;
3775 break;
3776 }
3777 (*mangled)++;
3778 if (! PRINT_ANSI_QUALIFIERS)
3779 {
3780 break;
3781 }
3782 if (type_quals != TYPE_UNQUALIFIED)
3783 {
3784 APPEND_BLANK (&decl);
3785 string_append (&decl, qualifier_string (type_quals));
3786 }
3787 break;
3788 }
3789 case 'G':
3790 (*mangled)++;
3791 break;
3792
3793 case 'C':
3794 case 'V':
3795 case 'u':
3796 if (PRINT_ANSI_QUALIFIERS)
3797 {
3798 if (!STRING_EMPTY (&decl))
3799 string_prepend (&decl, " ");
3800
3801 string_prepend (&decl, demangle_qualifier (**mangled));
3802 }
3803 (*mangled)++;
3804 break;
3805 /*
3806 }
3807 */
3808
3809 /* fall through */
3810 default:
3811 done = 1;
3812 break;
3813 }
3814 }
3815
3816 if (success) switch (**mangled)
3817 {
3818 /* A qualified name, such as "Outer::Inner". */
3819 case 'Q':
3820 case 'K':
3821 {
3822 success = demangle_qualified (work, mangled, result, 0, 1);
3823 break;
3824 }
3825
3826 /* A back reference to a previously seen squangled type */
3827 case 'B':
3828 (*mangled)++;
fa3fcee7 3829 if (!get_count (mangled, &n) || n < 0 || n >= work -> numb)
252b5132
RH
3830 success = 0;
3831 else
3832 string_append (result, work->btypevec[n]);
3833 break;
3834
3835 case 'X':
3836 case 'Y':
3837 /* A template parm. We substitute the corresponding argument. */
3838 {
3839 int idx;
3840
3841 (*mangled)++;
3842 idx = consume_count_with_underscores (mangled);
3843
3844 if (idx == -1
3845 || (work->tmpl_argvec && idx >= work->ntmpl_args)
3846 || consume_count_with_underscores (mangled) == -1)
3847 {
3848 success = 0;
3849 break;
3850 }
3851
3852 if (work->tmpl_argvec)
3853 string_append (result, work->tmpl_argvec[idx]);
3854 else
0c0a36a4 3855 string_append_template_idx (result, idx);
252b5132
RH
3856
3857 success = 1;
3858 }
3859 break;
3860
3861 default:
3862 success = demangle_fund_type (work, mangled, result);
3863 if (tk == tk_none)
3864 tk = (type_kind_t) success;
3865 break;
3866 }
3867
3868 if (success)
3869 {
3870 if (!STRING_EMPTY (&decl))
3871 {
3872 string_append (result, " ");
3873 string_appends (result, &decl);
3874 }
3875 }
3876 else
3877 string_delete (result);
3878 string_delete (&decl);
3879
41f225de
NC
3880 if (is_proctypevec)
3881 pop_processed_type (work);
3882
252b5132
RH
3883 if (success)
3884 /* Assume an integral type, if we're not sure. */
3885 return (int) ((tk == tk_none) ? tk_integral : tk);
3886 else
3887 return 0;
3888}
3889
3890/* Given a pointer to a type string that represents a fundamental type
3891 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3892 string in which the demangled output is being built in RESULT, and
3893 the WORK structure, decode the types and add them to the result.
3894
3895 For example:
3896
3897 "Ci" => "const int"
3898 "Sl" => "signed long"
3899 "CUs" => "const unsigned short"
3900
3901 The value returned is really a type_kind_t. */
3902
3903static int
9334f9c6
DD
3904demangle_fund_type (struct work_stuff *work,
3905 const char **mangled, string *result)
252b5132
RH
3906{
3907 int done = 0;
3908 int success = 1;
f6b66e90 3909 char buf[INTBUF_SIZE + 5 /* 'int%u_t' */];
9adf30b2 3910 unsigned int dec = 0;
252b5132
RH
3911 type_kind_t tk = tk_integral;
3912
252b5132
RH
3913 /* First pick off any type qualifiers. There can be more than one. */
3914
3915 while (!done)
3916 {
3917 switch (**mangled)
3918 {
3919 case 'C':
3920 case 'V':
3921 case 'u':
3922 if (PRINT_ANSI_QUALIFIERS)
3923 {
3924 if (!STRING_EMPTY (result))
3925 string_prepend (result, " ");
3926 string_prepend (result, demangle_qualifier (**mangled));
3927 }
3928 (*mangled)++;
3929 break;
3930 case 'U':
3931 (*mangled)++;
3932 APPEND_BLANK (result);
3933 string_append (result, "unsigned");
3934 break;
3935 case 'S': /* signed char only */
3936 (*mangled)++;
3937 APPEND_BLANK (result);
3938 string_append (result, "signed");
3939 break;
3940 case 'J':
3941 (*mangled)++;
3942 APPEND_BLANK (result);
3943 string_append (result, "__complex");
3944 break;
3945 default:
3946 done = 1;
3947 break;
3948 }
3949 }
3950
3951 /* Now pick off the fundamental type. There can be only one. */
3952
3953 switch (**mangled)
3954 {
3955 case '\0':
3956 case '_':
3957 break;
3958 case 'v':
3959 (*mangled)++;
3960 APPEND_BLANK (result);
3961 string_append (result, "void");
3962 break;
3963 case 'x':
3964 (*mangled)++;
3965 APPEND_BLANK (result);
3966 string_append (result, "long long");
3967 break;
3968 case 'l':
3969 (*mangled)++;
3970 APPEND_BLANK (result);
3971 string_append (result, "long");
3972 break;
3973 case 'i':
3974 (*mangled)++;
3975 APPEND_BLANK (result);
3976 string_append (result, "int");
3977 break;
3978 case 's':
3979 (*mangled)++;
3980 APPEND_BLANK (result);
3981 string_append (result, "short");
3982 break;
3983 case 'b':
3984 (*mangled)++;
3985 APPEND_BLANK (result);
3986 string_append (result, "bool");
3987 tk = tk_bool;
3988 break;
3989 case 'c':
3990 (*mangled)++;
3991 APPEND_BLANK (result);
3992 string_append (result, "char");
3993 tk = tk_char;
3994 break;
3995 case 'w':
3996 (*mangled)++;
3997 APPEND_BLANK (result);
3998 string_append (result, "wchar_t");
3999 tk = tk_char;
4000 break;
4001 case 'r':
4002 (*mangled)++;
4003 APPEND_BLANK (result);
4004 string_append (result, "long double");
4005 tk = tk_real;
4006 break;
4007 case 'd':
4008 (*mangled)++;
4009 APPEND_BLANK (result);
4010 string_append (result, "double");
4011 tk = tk_real;
4012 break;
4013 case 'f':
4014 (*mangled)++;
4015 APPEND_BLANK (result);
4016 string_append (result, "float");
4017 tk = tk_real;
4018 break;
4019 case 'G':
4020 (*mangled)++;
ac424eb3 4021 if (!ISDIGIT ((unsigned char)**mangled))
252b5132
RH
4022 {
4023 success = 0;
4024 break;
4025 }
4026 case 'I':
0c0a36a4 4027 (*mangled)++;
252b5132
RH
4028 if (**mangled == '_')
4029 {
4030 int i;
0c0a36a4 4031 (*mangled)++;
252b5132 4032 for (i = 0;
0c0a36a4
ILT
4033 i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
4034 (*mangled)++, i++)
252b5132
RH
4035 buf[i] = **mangled;
4036 if (**mangled != '_')
4037 {
4038 success = 0;
4039 break;
4040 }
4041 buf[i] = '\0';
0c0a36a4 4042 (*mangled)++;
252b5132
RH
4043 }
4044 else
4045 {
4046 strncpy (buf, *mangled, 2);
4047 buf[2] = '\0';
0c0a36a4 4048 *mangled += min (strlen (*mangled), 2);
252b5132
RH
4049 }
4050 sscanf (buf, "%x", &dec);
9adf30b2 4051 sprintf (buf, "int%u_t", dec);
252b5132
RH
4052 APPEND_BLANK (result);
4053 string_append (result, buf);
4054 break;
4055
4056 /* fall through */
4057 /* An explicit type, such as "6mytype" or "7integer" */
4058 case '0':
4059 case '1':
4060 case '2':
4061 case '3':
4062 case '4':
4063 case '5':
4064 case '6':
4065 case '7':
4066 case '8':
4067 case '9':
4068 {
4069 int bindex = register_Btype (work);
4070 string btype;
4071 string_init (&btype);
4072 if (demangle_class_name (work, mangled, &btype)) {
4073 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
4074 APPEND_BLANK (result);
4075 string_appends (result, &btype);
4076 }
4077 else
4078 success = 0;
4079 string_delete (&btype);
4080 break;
4081 }
4082 case 't':
4083 {
fcd7c7c9
DD
4084 string btype;
4085 string_init (&btype);
252b5132
RH
4086 success = demangle_template (work, mangled, &btype, 0, 1, 1);
4087 string_appends (result, &btype);
fcd7c7c9 4088 string_delete (&btype);
252b5132
RH
4089 break;
4090 }
4091 default:
4092 success = 0;
4093 break;
4094 }
4095
4096 return success ? ((int) tk) : 0;
4097}
4098
4099
4100/* Handle a template's value parameter for HP aCC (extension from ARM)
4101 **mangled points to 'S' or 'U' */
4102
4103static int
9334f9c6
DD
4104do_hpacc_template_const_value (struct work_stuff *work ATTRIBUTE_UNUSED,
4105 const char **mangled, string *result)
252b5132
RH
4106{
4107 int unsigned_const;
4108
4109 if (**mangled != 'U' && **mangled != 'S')
4110 return 0;
4111
4112 unsigned_const = (**mangled == 'U');
4113
4114 (*mangled)++;
4115
4116 switch (**mangled)
4117 {
4118 case 'N':
4119 string_append (result, "-");
4120 /* fall through */
4121 case 'P':
4122 (*mangled)++;
4123 break;
4124 case 'M':
4125 /* special case for -2^31 */
4126 string_append (result, "-2147483648");
4127 (*mangled)++;
4128 return 1;
4129 default:
4130 return 0;
4131 }
4132
4133 /* We have to be looking at an integer now */
ac424eb3 4134 if (!(ISDIGIT ((unsigned char)**mangled)))
252b5132
RH
4135 return 0;
4136
4137 /* We only deal with integral values for template
4138 parameters -- so it's OK to look only for digits */
ac424eb3 4139 while (ISDIGIT ((unsigned char)**mangled))
252b5132
RH
4140 {
4141 char_str[0] = **mangled;
4142 string_append (result, char_str);
4143 (*mangled)++;
4144 }
4145
4146 if (unsigned_const)
4147 string_append (result, "U");
4148
4149 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4150 with L or LL suffixes. pai/1997-09-03 */
4151
4152 return 1; /* success */
4153}
4154
4155/* Handle a template's literal parameter for HP aCC (extension from ARM)
4156 **mangled is pointing to the 'A' */
4157
4158static int
9334f9c6
DD
4159do_hpacc_template_literal (struct work_stuff *work, const char **mangled,
4160 string *result)
252b5132
RH
4161{
4162 int literal_len = 0;
4163 char * recurse;
4164 char * recurse_dem;
4165
4166 if (**mangled != 'A')
4167 return 0;
4168
4169 (*mangled)++;
4170
4171 literal_len = consume_count (mangled);
4172
fa3fcee7
NC
4173 if (literal_len <= 0
4174 || literal_len > (long) strlen (*mangled))
252b5132
RH
4175 return 0;
4176
4177 /* Literal parameters are names of arrays, functions, etc. and the
4178 canonical representation uses the address operator */
4179 string_append (result, "&");
4180
4181 /* Now recursively demangle the literal name */
abf6a75b 4182 recurse = XNEWVEC (char, literal_len + 1);
252b5132
RH
4183 memcpy (recurse, *mangled, literal_len);
4184 recurse[literal_len] = '\000';
4185
4186 recurse_dem = cplus_demangle (recurse, work->options);
4187
4188 if (recurse_dem)
4189 {
4190 string_append (result, recurse_dem);
4191 free (recurse_dem);
4192 }
4193 else
4194 {
4195 string_appendn (result, *mangled, literal_len);
4196 }
4197 (*mangled) += literal_len;
4198 free (recurse);
4199
4200 return 1;
4201}
4202
4203static int
9334f9c6 4204snarf_numeric_literal (const char **args, string *arg)
252b5132
RH
4205{
4206 if (**args == '-')
4207 {
4208 char_str[0] = '-';
4209 string_append (arg, char_str);
4210 (*args)++;
4211 }
4212 else if (**args == '+')
4213 (*args)++;
4214
ac424eb3 4215 if (!ISDIGIT ((unsigned char)**args))
252b5132
RH
4216 return 0;
4217
ac424eb3 4218 while (ISDIGIT ((unsigned char)**args))
252b5132
RH
4219 {
4220 char_str[0] = **args;
4221 string_append (arg, char_str);
4222 (*args)++;
4223 }
4224
4225 return 1;
4226}
4227
4228/* Demangle the next argument, given by MANGLED into RESULT, which
4229 *should be an uninitialized* string. It will be initialized here,
4230 and free'd should anything go wrong. */
4231
4232static int
9334f9c6 4233do_arg (struct work_stuff *work, const char **mangled, string *result)
252b5132
RH
4234{
4235 /* Remember where we started so that we can record the type, for
4236 non-squangling type remembering. */
4237 const char *start = *mangled;
4238
4239 string_init (result);
4240
4241 if (work->nrepeats > 0)
4242 {
4243 --work->nrepeats;
4244
4245 if (work->previous_argument == 0)
4246 return 0;
4247
4248 /* We want to reissue the previous type in this argument list. */
4249 string_appends (result, work->previous_argument);
4250 return 1;
4251 }
4252
4253 if (**mangled == 'n')
4254 {
4255 /* A squangling-style repeat. */
4256 (*mangled)++;
4257 work->nrepeats = consume_count(mangled);
4258
4259 if (work->nrepeats <= 0)
4260 /* This was not a repeat count after all. */
4261 return 0;
4262
4263 if (work->nrepeats > 9)
4264 {
4265 if (**mangled != '_')
4266 /* The repeat count should be followed by an '_' in this
4267 case. */
4268 return 0;
4269 else
4270 (*mangled)++;
4271 }
4272
4273 /* Now, the repeat is all set up. */
4274 return do_arg (work, mangled, result);
4275 }
4276
4277 /* Save the result in WORK->previous_argument so that we can find it
4278 if it's repeated. Note that saving START is not good enough: we
4279 do not want to add additional types to the back-referenceable
4280 type vector when processing a repeated type. */
4281 if (work->previous_argument)
fcd7c7c9 4282 string_delete (work->previous_argument);
252b5132 4283 else
abf6a75b 4284 work->previous_argument = XNEW (string);
252b5132
RH
4285
4286 if (!do_type (work, mangled, work->previous_argument))
4287 return 0;
4288
4289 string_appends (result, work->previous_argument);
4290
4291 remember_type (work, start, *mangled - start);
4292 return 1;
4293}
4294
41f225de
NC
4295static void
4296push_processed_type (struct work_stuff *work, int typevec_index)
4297{
4298 if (work->nproctypes >= work->proctypevec_size)
4299 {
4300 if (!work->proctypevec_size)
4301 {
4302 work->proctypevec_size = 4;
4303 work->proctypevec = XNEWVEC (int, work->proctypevec_size);
4304 }
4305 else
4306 {
4307 if (work->proctypevec_size < 16)
4308 /* Double when small. */
4309 work->proctypevec_size *= 2;
4310 else
4311 {
4312 /* Grow slower when large. */
4313 if (work->proctypevec_size > (INT_MAX / 3) * 2)
4314 xmalloc_failed (INT_MAX);
4315 work->proctypevec_size = (work->proctypevec_size * 3 / 2);
4316 }
4317 work->proctypevec
4318 = XRESIZEVEC (int, work->proctypevec, work->proctypevec_size);
4319 }
4320 }
4321 work->proctypevec [work->nproctypes++] = typevec_index;
4322}
4323
4324static void
4325pop_processed_type (struct work_stuff *work)
4326{
4327 work->nproctypes--;
4328}
4329
252b5132 4330static void
9334f9c6 4331remember_type (struct work_stuff *work, const char *start, int len)
252b5132
RH
4332{
4333 char *tem;
4334
4335 if (work->forgetting_types)
4336 return;
4337
4338 if (work -> ntypes >= work -> typevec_size)
4339 {
4340 if (work -> typevec_size == 0)
4341 {
4342 work -> typevec_size = 3;
abf6a75b 4343 work -> typevec = XNEWVEC (char *, work->typevec_size);
252b5132
RH
4344 }
4345 else
4346 {
fa3fcee7
NC
4347 if (work -> typevec_size > INT_MAX / 2)
4348 xmalloc_failed (INT_MAX);
252b5132
RH
4349 work -> typevec_size *= 2;
4350 work -> typevec
abf6a75b 4351 = XRESIZEVEC (char *, work->typevec, work->typevec_size);
252b5132
RH
4352 }
4353 }
abf6a75b 4354 tem = XNEWVEC (char, len + 1);
252b5132
RH
4355 memcpy (tem, start, len);
4356 tem[len] = '\0';
4357 work -> typevec[work -> ntypes++] = tem;
4358}
4359
4360
4361/* Remember a K type class qualifier. */
4362static void
9334f9c6 4363remember_Ktype (struct work_stuff *work, const char *start, int len)
252b5132
RH
4364{
4365 char *tem;
4366
4367 if (work -> numk >= work -> ksize)
4368 {
4369 if (work -> ksize == 0)
4370 {
4371 work -> ksize = 5;
abf6a75b 4372 work -> ktypevec = XNEWVEC (char *, work->ksize);
252b5132
RH
4373 }
4374 else
4375 {
fa3fcee7
NC
4376 if (work -> ksize > INT_MAX / 2)
4377 xmalloc_failed (INT_MAX);
252b5132
RH
4378 work -> ksize *= 2;
4379 work -> ktypevec
abf6a75b 4380 = XRESIZEVEC (char *, work->ktypevec, work->ksize);
252b5132
RH
4381 }
4382 }
abf6a75b 4383 tem = XNEWVEC (char, len + 1);
252b5132
RH
4384 memcpy (tem, start, len);
4385 tem[len] = '\0';
4386 work -> ktypevec[work -> numk++] = tem;
4387}
4388
4389/* Register a B code, and get an index for it. B codes are registered
4390 as they are seen, rather than as they are completed, so map<temp<char> >
4391 registers map<temp<char> > as B0, and temp<char> as B1 */
4392
4393static int
9334f9c6 4394register_Btype (struct work_stuff *work)
252b5132
RH
4395{
4396 int ret;
4397
4398 if (work -> numb >= work -> bsize)
4399 {
4400 if (work -> bsize == 0)
4401 {
4402 work -> bsize = 5;
abf6a75b 4403 work -> btypevec = XNEWVEC (char *, work->bsize);
252b5132
RH
4404 }
4405 else
4406 {
fa3fcee7
NC
4407 if (work -> bsize > INT_MAX / 2)
4408 xmalloc_failed (INT_MAX);
252b5132
RH
4409 work -> bsize *= 2;
4410 work -> btypevec
abf6a75b 4411 = XRESIZEVEC (char *, work->btypevec, work->bsize);
252b5132
RH
4412 }
4413 }
4414 ret = work -> numb++;
4415 work -> btypevec[ret] = NULL;
4416 return(ret);
4417}
4418
4419/* Store a value into a previously registered B code type. */
4420
4421static void
9334f9c6
DD
4422remember_Btype (struct work_stuff *work, const char *start,
4423 int len, int index)
252b5132
RH
4424{
4425 char *tem;
4426
abf6a75b 4427 tem = XNEWVEC (char, len + 1);
252b5132
RH
4428 memcpy (tem, start, len);
4429 tem[len] = '\0';
4430 work -> btypevec[index] = tem;
4431}
4432
4433/* Lose all the info related to B and K type codes. */
4434static void
9334f9c6 4435forget_B_and_K_types (struct work_stuff *work)
252b5132
RH
4436{
4437 int i;
4438
4439 while (work -> numk > 0)
4440 {
4441 i = --(work -> numk);
4442 if (work -> ktypevec[i] != NULL)
4443 {
4444 free (work -> ktypevec[i]);
4445 work -> ktypevec[i] = NULL;
4446 }
4447 }
4448
4449 while (work -> numb > 0)
4450 {
4451 i = --(work -> numb);
4452 if (work -> btypevec[i] != NULL)
4453 {
4454 free (work -> btypevec[i]);
4455 work -> btypevec[i] = NULL;
4456 }
4457 }
4458}
4459/* Forget the remembered types, but not the type vector itself. */
4460
4461static void
9334f9c6 4462forget_types (struct work_stuff *work)
252b5132
RH
4463{
4464 int i;
4465
4466 while (work -> ntypes > 0)
4467 {
4468 i = --(work -> ntypes);
4469 if (work -> typevec[i] != NULL)
4470 {
4471 free (work -> typevec[i]);
4472 work -> typevec[i] = NULL;
4473 }
4474 }
4475}
4476
4477/* Process the argument list part of the signature, after any class spec
4478 has been consumed, as well as the first 'F' character (if any). For
4479 example:
4480
4481 "__als__3fooRT0" => process "RT0"
4482 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4483
4484 DECLP must be already initialised, usually non-empty. It won't be freed
4485 on failure.
4486
4487 Note that g++ differs significantly from ARM and lucid style mangling
4488 with regards to references to previously seen types. For example, given
4489 the source fragment:
4490
4491 class foo {
4492 public:
4493 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4494 };
4495
4496 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4497 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4498
4499 g++ produces the names:
4500
4501 __3fooiRT0iT2iT2
4502 foo__FiR3fooiT1iT1
4503
4504 while lcc (and presumably other ARM style compilers as well) produces:
4505
4506 foo__FiR3fooT1T2T1T2
4507 __ct__3fooFiR3fooT1T2T1T2
4508
4509 Note that g++ bases its type numbers starting at zero and counts all
4510 previously seen types, while lucid/ARM bases its type numbers starting
4511 at one and only considers types after it has seen the 'F' character
4512 indicating the start of the function args. For lucid/ARM style, we
4513 account for this difference by discarding any previously seen types when
4514 we see the 'F' character, and subtracting one from the type number
4515 reference.
4516
4517 */
4518
4519static int
9334f9c6
DD
4520demangle_args (struct work_stuff *work, const char **mangled,
4521 string *declp)
252b5132
RH
4522{
4523 string arg;
4524 int need_comma = 0;
4525 int r;
4526 int t;
4527 const char *tem;
4528 char temptype;
4529
4530 if (PRINT_ARG_TYPES)
4531 {
4532 string_append (declp, "(");
4533 if (**mangled == '\0')
4534 {
4535 string_append (declp, "void");
4536 }
4537 }
4538
4539 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
4540 || work->nrepeats > 0)
4541 {
4542 if ((**mangled == 'N') || (**mangled == 'T'))
4543 {
4544 temptype = *(*mangled)++;
4545
4546 if (temptype == 'N')
4547 {
4548 if (!get_count (mangled, &r))
4549 {
4550 return (0);
4551 }
4552 }
4553 else
4554 {
4555 r = 1;
4556 }
4557 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
4558 {
4559 /* If we have 10 or more types we might have more than a 1 digit
4560 index so we'll have to consume the whole count here. This
4561 will lose if the next thing is a type name preceded by a
4562 count but it's impossible to demangle that case properly
4563 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4564 Pc, ...)" or "(..., type12, char *, ...)" */
4565 if ((t = consume_count(mangled)) <= 0)
4566 {
4567 return (0);
4568 }
4569 }
4570 else
4571 {
4572 if (!get_count (mangled, &t))
4573 {
4574 return (0);
4575 }
4576 }
4577 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4578 {
4579 t--;
4580 }
4581 /* Validate the type index. Protect against illegal indices from
4582 malformed type strings. */
4583 if ((t < 0) || (t >= work -> ntypes))
4584 {
4585 return (0);
4586 }
4587 while (work->nrepeats > 0 || --r >= 0)
4588 {
4589 tem = work -> typevec[t];
4590 if (need_comma && PRINT_ARG_TYPES)
4591 {
4592 string_append (declp, ", ");
4593 }
41f225de 4594 push_processed_type (work, t);
252b5132
RH
4595 if (!do_arg (work, &tem, &arg))
4596 {
41f225de 4597 pop_processed_type (work);
252b5132
RH
4598 return (0);
4599 }
41f225de 4600 pop_processed_type (work);
252b5132
RH
4601 if (PRINT_ARG_TYPES)
4602 {
4603 string_appends (declp, &arg);
4604 }
4605 string_delete (&arg);
4606 need_comma = 1;
4607 }
4608 }
4609 else
4610 {
4611 if (need_comma && PRINT_ARG_TYPES)
4612 string_append (declp, ", ");
4613 if (!do_arg (work, mangled, &arg))
4614 return (0);
4615 if (PRINT_ARG_TYPES)
4616 string_appends (declp, &arg);
4617 string_delete (&arg);
4618 need_comma = 1;
4619 }
4620 }
4621
4622 if (**mangled == 'e')
4623 {
4624 (*mangled)++;
4625 if (PRINT_ARG_TYPES)
4626 {
4627 if (need_comma)
4628 {
4629 string_append (declp, ",");
4630 }
4631 string_append (declp, "...");
4632 }
4633 }
4634
4635 if (PRINT_ARG_TYPES)
4636 {
4637 string_append (declp, ")");
4638 }
4639 return (1);
4640}
4641
4642/* Like demangle_args, but for demangling the argument lists of function
4643 and method pointers or references, not top-level declarations. */
4644
4645static int
9334f9c6
DD
4646demangle_nested_args (struct work_stuff *work, const char **mangled,
4647 string *declp)
252b5132
RH
4648{
4649 string* saved_previous_argument;
4650 int result;
4651 int saved_nrepeats;
4652
4653 /* The G++ name-mangling algorithm does not remember types on nested
4654 argument lists, unless -fsquangling is used, and in that case the
4655 type vector updated by remember_type is not used. So, we turn
4656 off remembering of types here. */
4657 ++work->forgetting_types;
4658
4659 /* For the repeat codes used with -fsquangling, we must keep track of
4660 the last argument. */
4661 saved_previous_argument = work->previous_argument;
4662 saved_nrepeats = work->nrepeats;
4663 work->previous_argument = 0;
4664 work->nrepeats = 0;
4665
4666 /* Actually demangle the arguments. */
4667 result = demangle_args (work, mangled, declp);
4668
4669 /* Restore the previous_argument field. */
4670 if (work->previous_argument)
fcd7c7c9
DD
4671 {
4672 string_delete (work->previous_argument);
4673 free ((char *) work->previous_argument);
4674 }
252b5132
RH
4675 work->previous_argument = saved_previous_argument;
4676 --work->forgetting_types;
4677 work->nrepeats = saved_nrepeats;
4678
4679 return result;
4680}
4681
b4b41ceb
BE
4682/* Returns 1 if a valid function name was found or 0 otherwise. */
4683
4684static int
9334f9c6
DD
4685demangle_function_name (struct work_stuff *work, const char **mangled,
4686 string *declp, const char *scan)
252b5132
RH
4687{
4688 size_t i;
4689 string type;
4690 const char *tem;
4691
4692 string_appendn (declp, (*mangled), scan - (*mangled));
4693 string_need (declp, 1);
4694 *(declp -> p) = '\0';
4695
4696 /* Consume the function name, including the "__" separating the name
4697 from the signature. We are guaranteed that SCAN points to the
4698 separator. */
4699
4700 (*mangled) = scan + 2;
4701 /* We may be looking at an instantiation of a template function:
4702 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4703 following _F marks the start of the function arguments. Handle
4704 the template arguments first. */
4705
4706 if (HP_DEMANGLING && (**mangled == 'X'))
4707 {
4708 demangle_arm_hp_template (work, mangled, 0, declp);
4709 /* This leaves MANGLED pointing to the 'F' marking func args */
4710 }
4711
4712 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4713 {
4714
4715 /* See if we have an ARM style constructor or destructor operator.
4716 If so, then just record it, clear the decl, and return.
4717 We can't build the actual constructor/destructor decl until later,
4718 when we recover the class name from the signature. */
4719
4720 if (strcmp (declp -> b, "__ct") == 0)
4721 {
4722 work -> constructor += 1;
4723 string_clear (declp);
b4b41ceb 4724 return 1;
252b5132
RH
4725 }
4726 else if (strcmp (declp -> b, "__dt") == 0)
4727 {
4728 work -> destructor += 1;
4729 string_clear (declp);
b4b41ceb 4730 return 1;
252b5132
RH
4731 }
4732 }
4733
4734 if (declp->p - declp->b >= 3
4735 && declp->b[0] == 'o'
4736 && declp->b[1] == 'p'
4737 && strchr (cplus_markers, declp->b[2]) != NULL)
4738 {
4739 /* see if it's an assignment expression */
4740 if (declp->p - declp->b >= 10 /* op$assign_ */
4741 && memcmp (declp->b + 3, "assign_", 7) == 0)
4742 {
74bcd529 4743 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
4744 {
4745 int len = declp->p - declp->b - 10;
4746 if ((int) strlen (optable[i].in) == len
4747 && memcmp (optable[i].in, declp->b + 10, len) == 0)
4748 {
4749 string_clear (declp);
4750 string_append (declp, "operator");
4751 string_append (declp, optable[i].out);
4752 string_append (declp, "=");
4753 break;
4754 }
4755 }
4756 }
4757 else
4758 {
74bcd529 4759 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
4760 {
4761 int len = declp->p - declp->b - 3;
4762 if ((int) strlen (optable[i].in) == len
4763 && memcmp (optable[i].in, declp->b + 3, len) == 0)
4764 {
4765 string_clear (declp);
4766 string_append (declp, "operator");
4767 string_append (declp, optable[i].out);
4768 break;
4769 }
4770 }
4771 }
4772 }
4773 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4774 && strchr (cplus_markers, declp->b[4]) != NULL)
4775 {
4776 /* type conversion operator */
4777 tem = declp->b + 5;
4778 if (do_type (work, &tem, &type))
4779 {
4780 string_clear (declp);
4781 string_append (declp, "operator ");
4782 string_appends (declp, &type);
4783 string_delete (&type);
4784 }
4785 }
4786 else if (declp->b[0] == '_' && declp->b[1] == '_'
4787 && declp->b[2] == 'o' && declp->b[3] == 'p')
4788 {
4789 /* ANSI. */
4790 /* type conversion operator. */
4791 tem = declp->b + 4;
4792 if (do_type (work, &tem, &type))
4793 {
4794 string_clear (declp);
4795 string_append (declp, "operator ");
4796 string_appends (declp, &type);
4797 string_delete (&type);
4798 }
4799 }
4800 else if (declp->b[0] == '_' && declp->b[1] == '_'
ac424eb3
DD
4801 && ISLOWER((unsigned char)declp->b[2])
4802 && ISLOWER((unsigned char)declp->b[3]))
252b5132
RH
4803 {
4804 if (declp->b[4] == '\0')
4805 {
4806 /* Operator. */
74bcd529 4807 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
4808 {
4809 if (strlen (optable[i].in) == 2
4810 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4811 {
4812 string_clear (declp);
4813 string_append (declp, "operator");
4814 string_append (declp, optable[i].out);
4815 break;
4816 }
4817 }
4818 }
4819 else
4820 {
4821 if (declp->b[2] == 'a' && declp->b[5] == '\0')
4822 {
4823 /* Assignment. */
74bcd529 4824 for (i = 0; i < ARRAY_SIZE (optable); i++)
252b5132
RH
4825 {
4826 if (strlen (optable[i].in) == 3
4827 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4828 {
4829 string_clear (declp);
4830 string_append (declp, "operator");
4831 string_append (declp, optable[i].out);
4832 break;
4833 }
4834 }
4835 }
4836 }
4837 }
b4b41ceb
BE
4838
4839 /* If a function name was obtained but it's not valid, we were not
4840 successful. */
4841 if (LEN_STRING (declp) == 1 && declp->b[0] == '.')
4842 return 0;
4843 else
4844 return 1;
252b5132
RH
4845}
4846
4847/* a mini string-handling package */
4848
4849static void
9334f9c6 4850string_need (string *s, int n)
252b5132
RH
4851{
4852 int tem;
4853
4854 if (s->b == NULL)
4855 {
4856 if (n < 32)
4857 {
4858 n = 32;
4859 }
abf6a75b 4860 s->p = s->b = XNEWVEC (char, n);
252b5132
RH
4861 s->e = s->b + n;
4862 }
4863 else if (s->e - s->p < n)
4864 {
4865 tem = s->p - s->b;
fa3fcee7
NC
4866 if (n > INT_MAX / 2 - tem)
4867 xmalloc_failed (INT_MAX);
252b5132
RH
4868 n += tem;
4869 n *= 2;
abf6a75b 4870 s->b = XRESIZEVEC (char, s->b, n);
252b5132
RH
4871 s->p = s->b + tem;
4872 s->e = s->b + n;
4873 }
4874}
4875
4876static void
9334f9c6 4877string_delete (string *s)
252b5132
RH
4878{
4879 if (s->b != NULL)
4880 {
4881 free (s->b);
4882 s->b = s->e = s->p = NULL;
4883 }
4884}
4885
4886static void
9334f9c6 4887string_init (string *s)
252b5132
RH
4888{
4889 s->b = s->p = s->e = NULL;
4890}
4891
4892static void
9334f9c6 4893string_clear (string *s)
252b5132
RH
4894{
4895 s->p = s->b;
4896}
4897
4898#if 0
4899
4900static int
9334f9c6 4901string_empty (string *s)
252b5132
RH
4902{
4903 return (s->b == s->p);
4904}
4905
4906#endif
4907
4908static void
9334f9c6 4909string_append (string *p, const char *s)
252b5132
RH
4910{
4911 int n;
4912 if (s == NULL || *s == '\0')
4913 return;
4914 n = strlen (s);
4915 string_need (p, n);
4916 memcpy (p->p, s, n);
4917 p->p += n;
4918}
4919
4920static void
9334f9c6 4921string_appends (string *p, string *s)
252b5132
RH
4922{
4923 int n;
4924
4925 if (s->b != s->p)
4926 {
4927 n = s->p - s->b;
4928 string_need (p, n);
4929 memcpy (p->p, s->b, n);
4930 p->p += n;
4931 }
4932}
4933
4934static void
9334f9c6 4935string_appendn (string *p, const char *s, int n)
252b5132
RH
4936{
4937 if (n != 0)
4938 {
4939 string_need (p, n);
4940 memcpy (p->p, s, n);
4941 p->p += n;
4942 }
4943}
4944
4945static void
9334f9c6 4946string_prepend (string *p, const char *s)
252b5132
RH
4947{
4948 if (s != NULL && *s != '\0')
4949 {
4950 string_prependn (p, s, strlen (s));
4951 }
4952}
4953
4954static void
9334f9c6 4955string_prepends (string *p, string *s)
252b5132
RH
4956{
4957 if (s->b != s->p)
4958 {
4959 string_prependn (p, s->b, s->p - s->b);
4960 }
4961}
4962
4963static void
9334f9c6 4964string_prependn (string *p, const char *s, int n)
252b5132
RH
4965{
4966 char *q;
4967
4968 if (n != 0)
4969 {
4970 string_need (p, n);
4971 for (q = p->p - 1; q >= p->b; q--)
4972 {
4973 q[n] = q[0];
4974 }
4975 memcpy (p->b, s, n);
4976 p->p += n;
4977 }
4978}
4979
0c0a36a4 4980static void
9334f9c6 4981string_append_template_idx (string *s, int idx)
0c0a36a4
ILT
4982{
4983 char buf[INTBUF_SIZE + 1 /* 'T' */];
4984 sprintf(buf, "T%d", idx);
4985 string_append (s, buf);
4986}