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