* expread.y (abs_decl): Accept '&' and '&' abs_decl.
[deliverable/binutils-gdb.git] / gdb / cplus-dem.c
CommitLineData
dd3b648e
RP
1/* Demangler for GNU C++
2 Copyright (C) 1989 Free Software Foundation, Inc.
3 written by James Clark (jjc@jclark.uucp)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19/* This is for g++ 1.36.1 (November 6 version). It will probably
20 require changes for any other version.
21
22 Modified for g++ 1.36.2 (November 18 version). */
23
24/* This file exports one function
25
26 char *cplus_demangle (const char *name, int mode)
27
28 If NAME is a mangled function name produced by GNU C++, then
29 a pointer to a malloced string giving a C++ representation
30 of the name will be returned; otherwise NULL will be returned.
31 It is the caller's responsibility to free the string which
32 is returned.
33
34 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
35 Otherwise they are not.
36 If MODE >= 0, parameters are emitted; otherwise not.
37
38 For example,
39
40 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
41 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
42 cplus_demangle ("foo__1Ai", -1) => "A::foo"
43
44 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
45 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
46 cplus_demangle ("foo__1Afe", -1) => "A::foo"
47
48 This file imports xmalloc and xrealloc, which are like malloc and
49 realloc except that they generate a fatal error if there is no
50 available memory. */
51
52/* define this if names don't start with _ */
53/* #define nounderscore 1 */
54
f88e7af8
JK
55/* This is '$' on systems where the assembler can deal with that.
56 Where the assembler can't, it's '.' (but on many systems '.' is
57 used for other things). */
58#if !defined (CPLUS_MARKER)
59#define CPLUS_MARKER '$'
60#endif
61
dd3b648e
RP
62#include <stdio.h>
63#include <ctype.h>
64
65#ifdef USG
66#include <memory.h>
67#include <string.h>
68#else
69#include <strings.h>
70#define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
71#define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
72#define strchr index
73#define strrchr rindex
74#endif
75
76#ifndef __STDC__
77#define const
78#endif
79
80#ifdef __STDC__
81extern char *cplus_demangle (const char *type, int mode);
82#else
83extern char *cplus_demangle ();
84#endif
85
86#ifdef __STDC__
87extern char *xmalloc (int);
88extern char *xrealloc (char *, int);
89extern void free (char *);
90#else
91extern char *xmalloc ();
92extern char *xrealloc ();
93extern void free ();
94#endif
95
96static char **typevec = 0;
97static int ntypes = 0;
98static int typevec_size = 0;
99
100static struct {
101 const char *in;
102 const char *out;
103} optable[] = {
104 "new", " new",
105 "delete", " delete",
106 "ne", "!=",
107 "eq", "==",
108 "ge", ">=",
109 "gt", ">",
110 "le", "<=",
111 "lt", "<",
112 "plus", "+",
113 "minus", "-",
114 "mult", "*",
115 "convert", "+", /* unary + */
116 "negate", "-", /* unary - */
117 "trunc_mod", "%",
118 "trunc_div", "/",
119 "truth_andif", "&&",
120 "truth_orif", "||",
121 "truth_not", "!",
122 "postincrement", "++",
123 "postdecrement", "--",
124 "bit_ior", "|",
125 "bit_xor", "^",
126 "bit_and", "&",
127 "bit_not", "~",
128 "call", "()",
129 "cond", "?:",
130 "alshift", "<<",
131 "arshift", ">>",
132 "component", "->",
133 "indirect", "*",
134 "method_call", "->()",
135 "addr", "&", /* unary & */
136 "array", "[]",
137 "nop", "", /* for operator= */
138};
139
140/* Beware: these aren't '\0' terminated. */
141
142typedef struct {
143 char *b; /* pointer to start of string */
144 char *p; /* pointer after last character */
145 char *e; /* pointer after end of allocated space */
146} string;
147
148#ifdef __STDC__
149static void string_need (string *s, int n);
150static void string_delete (string *s);
151static void string_init (string *s);
152static void string_clear (string *s);
153static int string_empty (string *s);
154static void string_append (string *p, const char *s);
155static void string_appends (string *p, string *s);
156static void string_appendn (string *p, const char *s, int n);
157static void string_prepend (string *p, const char *s);
158#if 0
159static void string_prepends (string *p, string *s);
160#endif
161static void string_prependn (string *p, const char *s, int n);
162static int get_count (const char **type, int *count);
163static int do_args (const char **type, string *decl, int arg_mode);
164static int do_type (const char **type, string *result, int arg_mode);
165static int do_arg (const char **type, string *result, int arg_mode);
166static void munge_function_name (string *name, int arg_mode);
167static void remember_type (const char *type, int len);
168#else
169static void string_need ();
170static void string_delete ();
171static void string_init ();
172static void string_clear ();
173static int string_empty ();
174static void string_append ();
175static void string_appends ();
176static void string_appendn ();
177static void string_prepend ();
178#if 0
179static void string_prepends ();
180#endif
181static void string_prependn ();
182static int get_count ();
183static int do_args ();
184static int do_type ();
185static int do_arg ();
186static int do_args ();
187static void munge_function_name ();
188static void remember_type ();
189#endif
190
191char *
192cplus_demangle (type, arg_mode)
193 const char *type;
194 int arg_mode;
195{
196 string decl;
197 int n;
198 int success = 0;
199 int constructor = 0;
200 int const_flag = 0;
201 int i;
202 const char *p;
203#ifndef LONGERNAMES
204 const char *premangle;
205#endif
206
207# define print_ansi_qualifiers (arg_mode > 0)
208# define print_arg_types (arg_mode >= 0)
209
210 if (type == NULL || *type == '\0')
211 return NULL;
212#ifndef nounderscore
213 if (*type++ != '_')
214 return NULL;
215#endif
216 p = type;
217 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
218 p++;
219 if (*p == '\0')
220 {
221 /* destructor */
f88e7af8 222 if (type[0] == '_' && type[1] == CPLUS_MARKER && type[2] == '_')
dd3b648e
RP
223 {
224 int n = (strlen (type) - 3)*2 + 3 + 2 + 1;
225 char *tem = (char *) xmalloc (n);
226 strcpy (tem, type + 3);
227 strcat (tem, "::~");
228 strcat (tem, type + 3);
229 strcat (tem, "()");
230 return tem;
231 }
232 /* static data member */
f88e7af8 233 if (*type != '_' && (p = strchr (type, CPLUS_MARKER)) != NULL)
dd3b648e
RP
234 {
235 int n = strlen (type) + 2;
236 char *tem = (char *) xmalloc (n);
237 memcpy (tem, type, p - type);
238 strcpy (tem + (p - type), "::");
239 strcpy (tem + (p - type) + 2, p + 1);
240 return tem;
241 }
242 /* virtual table "_vt$" */
f88e7af8 243 if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == CPLUS_MARKER)
dd3b648e
RP
244 {
245 int n = strlen (type + 4) + 14 + 1;
246 char *tem = (char *) xmalloc (n);
247 strcpy (tem, type + 4);
248 strcat (tem, " virtual table");
249 return tem;
250 }
251 return NULL;
252 }
253
254 string_init (&decl);
255
256 if (p == type)
257 {
258 if (!isdigit (p[2]))
259 {
260 string_delete (&decl);
261 return NULL;
262 }
263 constructor = 1;
264 }
265 else
266 {
267 string_appendn (&decl, type, p - type);
268 munge_function_name (&decl, arg_mode);
269 }
270 p += 2;
271
272#ifndef LONGERNAMES
273 premangle = p;
274#endif
275 switch (*p)
276 {
277 case 'C':
278 /* a const member function */
279 if (!isdigit (p[1]))
280 {
281 string_delete (&decl);
282 return NULL;
283 }
284 p += 1;
285 const_flag = 1;
286 /* fall through */
287 case '0':
288 case '1':
289 case '2':
290 case '3':
291 case '4':
292 case '5':
293 case '6':
294 case '7':
295 case '8':
296 case '9':
297 n = 0;
298 do
299 {
300 n *= 10;
301 n += *p - '0';
302 p += 1;
303 }
304 while (isdigit (*p));
305 if (strlen (p) < n)
306 {
307 string_delete (&decl);
308 return NULL;
309 }
310 if (constructor)
311 {
312 string_appendn (&decl, p, n);
313 string_append (&decl, "::");
314 string_appendn (&decl, p, n);
315 }
316 else
317 {
318 string_prepend (&decl, "::");
319 string_prependn (&decl, p, n);
320 }
321 p += n;
322#ifndef LONGERNAMES
323 remember_type (premangle, p - premangle);
324#endif
325 success = do_args (&p, &decl, arg_mode);
326 if (const_flag && print_arg_types)
327 string_append (&decl, " const");
328 break;
329 case 'F':
330 p += 1;
331 success = do_args (&p, &decl, arg_mode);
332 break;
333 }
334
335 for (i = 0; i < ntypes; i++)
336 if (typevec[i] != NULL)
337 free (typevec[i]);
338 ntypes = 0;
339 if (typevec != NULL)
340 {
341 free ((char *)typevec);
342 typevec = NULL;
343 typevec_size = 0;
344 }
345
346 if (success)
347 {
348 string_appendn (&decl, "", 1);
349 return decl.b;
350 }
351 else
352 {
353 string_delete (&decl);
354 return NULL;
355 }
356}
357
358static int
359get_count (type, count)
360 const char **type;
361 int *count;
362{
363 if (!isdigit (**type))
364 return 0;
365 *count = **type - '0';
366 *type += 1;
367 /* see flush_repeats in cplus-method.c */
368 if (isdigit (**type))
369 {
370 const char *p = *type;
371 int n = *count;
372 do
373 {
374 n *= 10;
375 n += *p - '0';
376 p += 1;
377 }
378 while (isdigit (*p));
379 if (*p == '_')
380 {
381 *type = p + 1;
382 *count = n;
383 }
384 }
385 return 1;
386}
387
388/* result will be initialised here; it will be freed on failure */
389
390static int
391do_type (type, result, arg_mode)
392 const char **type;
393 string *result;
394 int arg_mode;
395{
396 int n;
397 int done;
398 int non_empty = 0;
399 int success;
400 string decl;
401 const char *remembered_type;
402
403 string_init (&decl);
404 string_init (result);
405
406 done = 0;
407 success = 1;
408 while (success && !done)
409 {
410 int member;
411 switch (**type)
412 {
413 case 'P':
414 *type += 1;
415 string_prepend (&decl, "*");
416 break;
417
418 case 'R':
419 *type += 1;
420 string_prepend (&decl, "&");
421 break;
422
423 case 'T':
424 *type += 1;
425 if (!get_count (type, &n) || n >= ntypes)
426 success = 0;
427 else
428 {
429 remembered_type = typevec[n];
430 type = &remembered_type;
431 }
432 break;
433
434 case 'F':
435 *type += 1;
436 if (!string_empty (&decl) && decl.b[0] == '*')
437 {
438 string_prepend (&decl, "(");
439 string_append (&decl, ")");
440 }
441 if (!do_args (type, &decl, arg_mode) || **type != '_')
442 success = 0;
443 else
444 *type += 1;
445 break;
446
447 case 'M':
448 case 'O':
449 {
450 int constp = 0;
451 int volatilep = 0;
452
453 member = **type == 'M';
454 *type += 1;
455 if (!isdigit (**type))
456 {
457 success = 0;
458 break;
459 }
460 n = 0;
461 do
462 {
463 n *= 10;
464 n += **type - '0';
465 *type += 1;
466 }
467 while (isdigit (**type));
468 if (strlen (*type) < n)
469 {
470 success = 0;
471 break;
472 }
473 string_append (&decl, ")");
474 string_prepend (&decl, "::");
475 string_prependn (&decl, *type, n);
476 string_prepend (&decl, "(");
477 *type += n;
478 if (member)
479 {
480 if (**type == 'C')
481 {
482 *type += 1;
483 constp = 1;
484 }
485 if (**type == 'V')
486 {
487 *type += 1;
488 volatilep = 1;
489 }
490 if (*(*type)++ != 'F')
491 {
492 success = 0;
493 break;
494 }
495 }
496 if ((member && !do_args (type, &decl, arg_mode)) || **type != '_')
497 {
498 success = 0;
499 break;
500 }
501 *type += 1;
502 if (! print_ansi_qualifiers)
503 break;
504 if (constp)
505 {
506 if (non_empty)
507 string_append (&decl, " ");
508 else
509 non_empty = 1;
510 string_append (&decl, "const");
511 }
512 if (volatilep)
513 {
514 if (non_empty)
515 string_append (&decl, " ");
516 else
517 non_empty = 1;
518 string_append (&decl, "volatile");
519 }
520 break;
521 }
522
523 case 'C':
524 if ((*type)[1] == 'P')
525 {
526 *type += 1;
527 if (print_ansi_qualifiers)
528 {
529 if (!string_empty (&decl))
530 string_prepend (&decl, " ");
531 string_prepend (&decl, "const");
532 }
533 break;
534 }
535
536 /* fall through */
537 default:
538 done = 1;
539 break;
540 }
541 }
542
543 done = 0;
544 non_empty = 0;
545 while (success && !done)
546 {
547 switch (**type)
548 {
549 case 'C':
550 *type += 1;
551 if (print_ansi_qualifiers)
552 {
553 if (non_empty)
554 string_append (result, " ");
555 else
556 non_empty = 1;
557 string_append (result, "const");
558 }
559 break;
560 case 'U':
561 *type += 1;
562 if (non_empty)
563 string_append (result, " ");
564 else
565 non_empty = 1;
566 string_append (result, "unsigned");
567 break;
568 case 'V':
569 *type += 1;
570 if (print_ansi_qualifiers)
571 {
572 if (non_empty)
573 string_append (result, " ");
574 else
575 non_empty = 1;
576 string_append (result, "volatile");
577 }
578 break;
579 default:
580 done = 1;
581 break;
582 }
583 }
584
585 if (success)
586 switch (**type)
587 {
588 case '\0':
589 case '_':
590 break;
591 case 'v':
592 *type += 1;
593 if (non_empty)
594 string_append (result, " ");
595 string_append (result, "void");
596 break;
597 case 'x':
598 *type += 1;
599 if (non_empty)
600 string_append (result, " ");
601 string_append (result, "long long");
602 break;
603 case 'l':
604 *type += 1;
605 if (non_empty)
606 string_append (result, " ");
607 string_append (result, "long");
608 break;
609 case 'i':
610 *type += 1;
611 if (non_empty)
612 string_append (result, " ");
613 string_append (result, "int");
614 break;
615 case 's':
616 *type += 1;
617 if (non_empty)
618 string_append (result, " ");
619 string_append (result, "short");
620 break;
621 case 'c':
622 *type += 1;
623 if (non_empty)
624 string_append (result, " ");
625 string_append (result, "char");
626 break;
627 case 'r':
628 *type += 1;
629 if (non_empty)
630 string_append (result, " ");
631 string_append (result, "long double");
632 break;
633 case 'd':
634 *type += 1;
635 if (non_empty)
636 string_append (result, " ");
637 string_append (result, "double");
638 break;
639 case 'f':
640 *type += 1;
641 if (non_empty)
642 string_append (result, " ");
643 string_append (result, "float");
644 break;
645 case 'G':
646 *type += 1;
647 if (!isdigit (**type))
648 {
649 success = 0;
650 break;
651 }
652 /* fall through */
653 case '0':
654 case '1':
655 case '2':
656 case '3':
657 case '4':
658 case '5':
659 case '6':
660 case '7':
661 case '8':
662 case '9':
663 n = 0;
664 do
665 {
666 n *= 10;
667 n += **type - '0';
668 *type += 1;
669 }
670 while (isdigit (**type));
671 if (strlen (*type) < n)
672 {
673 success = 0;
674 break;
675 }
676 if (non_empty)
677 string_append (result, " ");
678 string_appendn (result, *type, n);
679 *type += n;
680 break;
681 default:
682 success = 0;
683 break;
684 }
685
686 if (success)
687 {
688 if (!string_empty (&decl))
689 {
690 string_append (result, " ");
691 string_appends (result, &decl);
692 }
693 string_delete (&decl);
694 return 1;
695 }
696 else
697 {
698 string_delete (&decl);
699 string_delete (result);
700 return 0;
701 }
702}
703
704/* `result' will be initialised in do_type; it will be freed on failure */
705
706static int
707do_arg (type, result, arg_mode)
708 const char **type;
709 string *result;
710 int arg_mode;
711{
712 const char *start = *type;
713
714 if (!do_type (type, result, arg_mode))
715 return 0;
716 remember_type (start, *type - start);
717 return 1;
718}
719
720static void
721remember_type (start, len)
722 const char *start;
723 int len;
724{
725 char *tem;
726
727 if (ntypes >= typevec_size)
728 {
729 if (typevec_size == 0)
730 {
731 typevec_size = 3;
732 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
733 }
734 else
735 {
736 typevec_size *= 2;
737 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
738 }
739 }
740 tem = (char *) xmalloc (len + 1);
741 memcpy (tem, start, len);
742 tem[len] = '\0';
743 typevec[ntypes++] = tem;
744}
745
746/* `decl' must be already initialised, usually non-empty;
747 it won't be freed on failure */
748
749static int
750do_args (type, decl, arg_mode)
751 const char **type;
752 string *decl;
753 int arg_mode;
754{
755 string arg;
756 int need_comma = 0;
757
758 if (print_arg_types)
759 string_append (decl, "(");
760
761 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
762 {
763 if (**type == 'N')
764 {
765 int r;
766 int t;
767 *type += 1;
768 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
769 return 0;
770 while (--r >= 0)
771 {
772 const char *tem = typevec[t];
773 if (need_comma && print_arg_types)
774 string_append (decl, ", ");
775 if (!do_arg (&tem, &arg, arg_mode))
776 return 0;
777 if (print_arg_types)
778 string_appends (decl, &arg);
779 string_delete (&arg);
780 need_comma = 1;
781 }
782 }
783 else
784 {
785 if (need_comma & print_arg_types)
786 string_append (decl, ", ");
787 if (!do_arg (type, &arg, arg_mode))
788 return 0;
789 if (print_arg_types)
790 string_appends (decl, &arg);
791 string_delete (&arg);
792 need_comma = 1;
793 }
794 }
795
796 if (**type == 'v')
797 *type += 1;
798 else if (**type == 'e')
799 {
800 *type += 1;
801 if (print_arg_types)
802 {
803 if (need_comma)
804 string_append (decl, ",");
805 string_append (decl, "...");
806 }
807 }
808
809 if (print_arg_types)
810 string_append (decl, ")");
811 return 1;
812}
813
814static void
815munge_function_name (name, arg_mode)
816 string *name;
817 int arg_mode;
818{
819 if (!string_empty (name) && name->p - name->b >= 3
f88e7af8 820 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == CPLUS_MARKER)
dd3b648e
RP
821 {
822 int i;
823 /* see if it's an assignment expression */
824 if (name->p - name->b >= 10 /* op$assign_ */
825 && memcmp (name->b + 3, "assign_", 7) == 0)
826 {
827 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
828 {
829 int len = name->p - name->b - 10;
830 if (strlen (optable[i].in) == len
831 && memcmp (optable[i].in, name->b + 10, len) == 0)
832 {
833 string_clear (name);
834 string_append (name, "operator");
835 string_append (name, optable[i].out);
836 string_append (name, "=");
837 return;
838 }
839 }
840 }
841 else
842 {
843 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
844 {
845 int len = name->p - name->b - 3;
846 if (strlen (optable[i].in) == len
847 && memcmp (optable[i].in, name->b + 3, len) == 0)
848 {
849 string_clear (name);
850 string_append (name, "operator");
851 string_append (name, optable[i].out);
852 return;
853 }
854 }
855 }
856 return;
857 }
858 else if (!string_empty (name) && name->p - name->b >= 5
859 && memcmp (name->b, "type$", 5) == 0)
860 {
861 /* type conversion operator */
862 string type;
863 const char *tem = name->b + 5;
864 if (do_type (&tem, &type, arg_mode))
865 {
866 string_clear (name);
867 string_append (name, "operator ");
868 string_appends (name, &type);
869 string_delete (&type);
870 return;
871 }
872 }
873}
874
875/* a mini string-handling package */
876
877static void
878string_need (s, n)
879 string *s;
880 int n;
881{
882 if (s->b == NULL)
883 {
884 if (n < 32)
885 n = 32;
886 s->p = s->b = (char *) xmalloc (n);
887 s->e = s->b + n;
888 }
889 else if (s->e - s->p < n)
890 {
891 int tem = s->p - s->b;
892 n += tem;
893 n *= 2;
894 s->b = (char *) xrealloc (s->b, n);
895 s->p = s->b + tem;
896 s->e = s->b + n;
897 }
898}
899
900static void
901string_delete (s)
902 string *s;
903{
904 if (s->b != NULL)
905 {
906 free (s->b);
907 s->b = s->e = s->p = NULL;
908 }
909}
910
911static void
912string_init (s)
913 string *s;
914{
915 s->b = s->p = s->e = NULL;
916}
917
918static void
919string_clear (s)
920 string *s;
921{
922 s->p = s->b;
923}
924
925static int
926string_empty (s)
927 string *s;
928{
929 return s->b == s->p;
930}
931
932static void
933string_append (p, s)
934 string *p;
935 const char *s;
936{
937 int n;
938 if (s == NULL || *s == '\0')
939 return;
940 n = strlen (s);
941 string_need (p, n);
942 memcpy (p->p, s, n);
943 p->p += n;
944}
945
946static void
947string_appends (p, s)
948 string *p, *s;
949{
950 int n;
951 if (s->b == s->p)
952 return;
953 n = s->p - s->b;
954 string_need (p, n);
955 memcpy (p->p, s->b, n);
956 p->p += n;
957}
958
959static void
960string_appendn (p, s, n)
961 string *p;
962 const char *s;
963 int n;
964{
965 if (n == 0)
966 return;
967 string_need (p, n);
968 memcpy (p->p, s, n);
969 p->p += n;
970}
971
972static void
973string_prepend (p, s)
974 string *p;
975 const char *s;
976{
977 if (s == NULL || *s == '\0')
978 return;
979 string_prependn (p, s, strlen (s));
980}
981
982#if 0
983static void
984string_prepends (p, s)
985 string *p, *s;
986{
987 if (s->b == s->p)
988 return;
989 string_prependn (p, s->b, s->p - s->b);
990}
991#endif
992
993static void
994string_prependn (p, s, n)
995 string *p;
996 const char *s;
997 int n;
998{
999 char *q;
1000
1001 if (n == 0)
1002 return;
1003 string_need (p, n);
1004 for (q = p->p - 1; q >= p->b; q--)
1005 q[n] = q[0];
1006 memcpy (p->b, s, n);
1007 p->p += n;
1008}
This page took 0.060459 seconds and 4 git commands to generate.