Initial revision
[deliverable/binutils-gdb.git] / gdb / cplus-dem.c
1 /* Demangler for GNU C++
2 Copyright 1989, 1991 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 2, 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.95.03 (November 13 verison). */
20
21 /* This file exports one function
22
23 char *cplus_demangle (const char *name, int mode)
24
25 If NAME is a mangled function name produced by GNU C++, then
26 a pointer to a malloced string giving a C++ representation
27 of the name will be returned; otherwise NULL will be returned.
28 It is the caller's responsibility to free the string which
29 is returned.
30
31 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
32 Otherwise they are not.
33 If MODE >= 0, parameters are emitted; otherwise not.
34
35 For example,
36
37 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
38 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
39 cplus_demangle ("foo__1Ai", -1) => "A::foo"
40
41 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
42 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
43 cplus_demangle ("foo__1Afe", -1) => "A::foo"
44
45 This file imports xmalloc and xrealloc, which are like malloc and
46 realloc except that they generate a fatal error if there is no
47 available memory. */
48
49 /* define this if names don't start with _ */
50 /* #define nounderscore 1 */
51
52 #include <stdio.h>
53 #include <ctype.h>
54
55 /* GDB-specific, FIXME. */
56 #include "defs.h"
57
58 #ifdef USG
59 #include <memory.h>
60 #include <string.h>
61 #else
62 #include <strings.h>
63 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
64 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
65 #define strchr index
66 #define strrchr rindex
67 #endif
68
69 /* This is '$' on systems where the assembler can deal with that.
70 Where the assembler can't, it's '.' (but on many systems '.' is
71 used for other things). */
72 #if !defined (CPLUS_MARKER)
73 #define CPLUS_MARKER '$'
74 #endif
75
76 #ifndef __STDC__
77 #define const
78 #endif
79
80 #ifdef __STDC__
81 extern char *cplus_demangle (const char *type, int mode);
82 #else
83 extern char *cplus_demangle ();
84 #endif
85
86 #ifdef __STDC__
87 /* GDB prototypes these as void* in defs.h, so we better too, at least
88 as long as we're including defs.h. */
89 extern void *xmalloc (int);
90 extern void *xrealloc (char *, int);
91 extern void free (void *);
92 #else
93 extern char *xmalloc ();
94 extern char *xrealloc ();
95 extern void free ();
96 #endif
97
98 static char **typevec = 0;
99 static int ntypes = 0;
100 static int typevec_size = 0;
101
102 const static struct optable {
103 const char *in;
104 const char *out;
105 int ansi;
106 } optable[] = {
107 "nw", " new", 1, /* new (1.92, ansi) */
108 "dl", " delete", 1, /* new (1.92, ansi) */
109 "new", " new", 0, /* old (1.91, and 1.x) */
110 "delete", " delete", 0, /* old (1.91, and 1.x) */
111 "as", "=", 1, /* ansi */
112 "ne", "!=", 1, /* old, ansi */
113 "eq", "==", 1, /* old, ansi */
114 "ge", ">=", 1, /* old, ansi */
115 "gt", ">", 1, /* old, ansi */
116 "le", "<=", 1, /* old, ansi */
117 "lt", "<", 1, /* old, ansi */
118 "plus", "+", 0, /* old */
119 "pl", "+", 1, /* ansi */
120 "apl", "+=", 1, /* ansi */
121 "minus", "-", 0, /* old */
122 "mi", "-", 1, /* ansi */
123 "ami", "-=", 1, /* ansi */
124 "mult", "*", 0, /* old */
125 "ml", "*", 1, /* ansi */
126 "aml", "*=", 1, /* ansi */
127 "convert", "+", 0, /* old (unary +) */
128 "negate", "-", 0, /* old (unary -) */
129 "trunc_mod", "%", 0, /* old */
130 "md", "%", 1, /* ansi */
131 "amd", "%=", 1, /* ansi */
132 "trunc_div", "/", 0, /* old */
133 "dv", "/", 1, /* ansi */
134 "adv", "/=", 1, /* ansi */
135 "truth_andif", "&&", 0, /* old */
136 "aa", "&&", 1, /* ansi */
137 "truth_orif", "||", 0, /* old */
138 "oo", "||", 1, /* ansi */
139 "truth_not", "!", 0, /* old */
140 "nt", "!", 1, /* ansi */
141 "postincrement", "++", 0, /* old */
142 "pp", "++", 1, /* ansi */
143 "postdecrement", "--", 0, /* old */
144 "mm", "--", 1, /* ansi */
145 "bit_ior", "|", 0, /* old */
146 "or", "|", 1, /* ansi */
147 "aor", "|=", 1, /* ansi */
148 "bit_xor", "^", 0, /* old */
149 "er", "^", 1, /* ansi */
150 "aer", "^=", 1, /* ansi */
151 "bit_and", "&", 0, /* old */
152 "ad", "&", 1, /* ansi */
153 "aad", "&=", 1, /* ansi */
154 "bit_not", "~", 0, /* old */
155 "co", "~", 1, /* ansi */
156 "call", "()", 0, /* old */
157 "cl", "()", 1, /* ansi */
158 "alshift", "<<", 0, /* old */
159 "ls", "<<", 1, /* ansi */
160 "als", "<<=", 1, /* ansi */
161 "arshift", ">>", 0, /* old */
162 "rs", ">>", 1, /* ansi */
163 "ars", ">>=", 1, /* ansi */
164 "component", "->", 0, /* old */
165 "rf", "->", 1, /* ansi */
166 "indirect", "*", 0, /* old */
167 "method_call", "->()", 0, /* old */
168 "addr", "&", 0, /* old (unary &) */
169 "array", "[]", 0, /* old */
170 "vc", "[]", 1, /* ansi */
171 "compound", ",", 0, /* old */
172 "cm", ",", 1, /* ansi */
173 "cond", "?:", 0, /* old */
174 "cn", "?:", 1, /* psuedo-ansi */
175 "max", ">?", 0, /* old */
176 "mx", ">?", 1, /* psuedo-ansi */
177 "min", "<?", 0, /* old */
178 "mn", "<?", 1, /* psuedo-ansi */
179 "nop", "", 0, /* old (for operator=) */
180 };
181
182 /* Beware: these aren't '\0' terminated. */
183
184 typedef struct string {
185 char *b; /* pointer to start of string */
186 char *p; /* pointer after last character */
187 char *e; /* pointer after end of allocated space */
188 } string;
189
190 #ifdef __STDC__
191 static void string_need (string *s, int n);
192 static void string_delete (string *s);
193 static void string_init (string *s);
194 static void string_clear (string *s);
195 static int string_empty (string *s);
196 static void string_append (string *p, const char *s);
197 static void string_appends (string *p, string *s);
198 static void string_appendn (string *p, const char *s, int n);
199 static void string_prepend (string *p, const char *s);
200 #if 0
201 static void string_prepends (string *p, string *s);
202 #endif
203 static void string_prependn (string *p, const char *s, int n);
204 static int get_count (const char **type, int *count);
205 static int do_args (const char **type, string *decl, int arg_mode);
206 static int do_type (const char **type, string *result, int arg_mode);
207 static int do_arg (const char **type, string *result, int arg_mode);
208 static void munge_function_name (string *name, int arg_mode);
209 static void remember_type (const char *type, int len);
210 #else
211 static void string_need ();
212 static void string_delete ();
213 static void string_init ();
214 static void string_clear ();
215 static int string_empty ();
216 static void string_append ();
217 static void string_appends ();
218 static void string_appendn ();
219 static void string_prepend ();
220 #if 0
221 static void string_prepends ();
222 #endif
223 static void string_prependn ();
224 static int get_count ();
225 static int do_args ();
226 static int do_type ();
227 static int do_arg ();
228 static int do_args ();
229 static void munge_function_name ();
230 static void remember_type ();
231 #endif
232
233 /* Takes operator name as e.g. "++" and returns mangled
234 operator name (e.g. "postincrement_expr"), or NULL if not found.
235
236 If ARG_MODE == 1, return the ANSI name;
237 if ARG_MODE == 0 return the old GNU name. */
238 char *
239 cplus_mangle_opname (opname, arg_mode)
240 char *opname;
241 int arg_mode;
242 {
243 int i, len = strlen (opname);
244
245 if (arg_mode != 0 && arg_mode != 1)
246 error ("invalid arg_mode");
247
248 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
249 {
250 if (strlen (optable[i].out) == len
251 && arg_mode == optable[i].ansi
252 && memcmp (optable[i].out, opname, len) == 0)
253 return (char *)optable[i].in;
254 }
255 return 0;
256 }
257
258 char *
259 cplus_demangle (type, arg_mode)
260 const char *type;
261 int arg_mode;
262 {
263 string decl;
264 int n;
265 int success = 0;
266 int constructor = 0;
267 int destructor = 0;
268 int static_type = 0;
269 int const_flag = 0;
270 int i;
271 const char *p;
272 #ifndef LONGERNAMES
273 const char *premangle;
274 #endif
275
276 # define print_ansi_qualifiers (arg_mode > 0)
277 # define print_arg_types (arg_mode >= 0)
278
279 if (type == NULL || *type == '\0')
280 return NULL;
281 #ifndef nounderscore
282 if (*type++ != '_')
283 return NULL;
284 #endif
285 p = type;
286 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
287 p++;
288 if (*p == '\0')
289 {
290 /* destructor */
291 if (type[0] == '_' && type[1] == CPLUS_MARKER && type[2] == '_')
292 {
293 int n = (strlen (type) - 3)*2 + 3 + 2 + 1;
294 char *tem = (char *) xmalloc (n);
295 strcpy (tem, type + 3);
296 strcat (tem, "::~");
297 strcat (tem, type + 3);
298 if (print_arg_types)
299 strcat (tem, "()");
300 return tem;
301 }
302 /* static data member */
303 if (*type != '_' && (p = strchr (type, CPLUS_MARKER)) != NULL)
304 {
305 int n = strlen (type) + 2;
306 char *tem = (char *) xmalloc (n);
307 memcpy (tem, type, p - type);
308 strcpy (tem + (p - type), "::");
309 strcpy (tem + (p - type) + 2, p + 1);
310 return tem;
311 }
312 /* virtual table "_vt$" */
313 if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == CPLUS_MARKER)
314 {
315 int n = strlen (type + 4) + 14 + 1;
316 char *tem = (char *) xmalloc (n);
317 strcpy (tem, type + 4);
318 strcat (tem, " virtual table");
319 return tem;
320 }
321 return NULL;
322 }
323
324 string_init (&decl);
325
326 if (static_type)
327 {
328 if (!isdigit (p[0]) && ('t' != p[0]))
329 {
330 string_delete (&decl);
331 return NULL;
332 }
333 }
334 else if (p == type)
335 {
336 if (!isdigit (p[2]) && ('t' != p[2]))
337 {
338 p += 1;
339 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
340 p++;
341 string_appendn (&decl, type, p - type);
342 string_need (&decl, 1);
343 *(decl.p) = '\0';
344 munge_function_name (&decl, 1);
345 if (decl.b[0] == '_')
346 {
347 string_delete (&decl);
348 return NULL;
349 }
350 else
351 p += 2;
352 }
353 else
354 {
355 constructor = 1;
356 p += 2;
357 }
358 }
359 else
360 {
361 string_appendn (&decl, type, p - type);
362 string_need (&decl, 1);
363 *(decl.p) = '\0';
364 munge_function_name (&decl, arg_mode);
365 p += 2;
366 }
367
368 #ifndef LONGERNAMES
369 premangle = p;
370 #endif
371 switch (*p)
372 {
373 case 'C':
374 /* a const member function */
375 if (!isdigit (p[1]))
376 {
377 string_delete (&decl);
378 return NULL;
379 }
380 p += 1;
381 const_flag = 1;
382 /* fall through */
383 case '0':
384 case '1':
385 case '2':
386 case '3':
387 case '4':
388 case '5':
389 case '6':
390 case '7':
391 case '8':
392 case '9':
393 n = 0;
394 do
395 {
396 n *= 10;
397 n += *p - '0';
398 p += 1;
399 }
400 while (isdigit (*p));
401 if (strlen (p) < n)
402 {
403 string_delete (&decl);
404 return NULL;
405 }
406 if (constructor || destructor)
407 {
408 string_appendn (&decl, p, n);
409 string_append (&decl, "::");
410 if (destructor)
411 string_append(&decl, "~");
412 string_appendn (&decl, p, n);
413 }
414 else
415 {
416 string_prepend (&decl, "::");
417 string_prependn (&decl, p, n);
418 }
419 p += n;
420 #ifndef LONGERNAMES
421 remember_type (premangle, p - premangle);
422 #endif
423 if (static_type)
424 {
425 string_append(&decl, p+1);
426 p += strlen(p);
427 success = 1;
428 }
429 else
430 success = do_args (&p, &decl, arg_mode);
431 if (const_flag && print_arg_types)
432 string_append (&decl, " const");
433 break;
434 case 'F':
435 p += 1;
436 success = do_args (&p, &decl, arg_mode);
437 break;
438 /* template additions */
439 case 't':
440 p += 1;
441 {
442 int r, i;
443 int non_empty = 0;
444 string tname;
445 string trawname;
446
447 string temp;
448 int need_comma = 0;
449
450 string_init(&tname);
451 string_init(&trawname);
452
453 /* get template name */
454 if (!get_count (&p, &r))
455 return 0;
456 string_appendn (&tname, p, r);
457 string_appendn (&trawname, p, r);
458 string_appendn (&trawname, "", 1);
459 p += r;
460 string_append (&tname, "<");
461 /* get size of template parameter list */
462 if (!get_count (&p, &r))
463 return 0;
464 for (i = 0; i < r; i++)
465 {
466 if (need_comma)
467 string_append (&tname, ", ");
468 /* Z for type parameters */
469 if (*p == 'Z')
470 {
471 p += 1;
472
473 success = do_type (&p, &temp, arg_mode);
474 string_appendn (&temp, "", 1);
475 if (success)
476 string_append (&tname, temp.b);
477 string_delete(&temp);
478 if (!success)
479 break;
480 }
481 /* otherwise, value parameter */
482 else
483 {
484 const char *old_p = p;
485 int is_pointer = 0;
486 int is_real = 0;
487 int is_integral = 0;
488 int done = 0;
489
490 success = do_type (&p, &temp, arg_mode);
491 string_appendn (&temp, "", 1);
492 if (success)
493 string_append (&tname, temp.b);
494 string_delete(&temp);
495 if (!success)
496 break;
497 string_append (&tname, "=");
498 while (*old_p && !done)
499 {
500 switch (*old_p)
501 {
502 case 'P':
503 case 'R':
504 done = is_pointer = 1;
505 break;
506 case 'C': /* const */
507 case 'U': /* unsigned */
508 case 'V': /* volatile */
509 case 'F': /* function */
510 case 'M': /* member function */
511 case 'O': /* ??? */
512 old_p++;
513 continue;
514 case 'Q': /* repetition of following */
515 case 'T': /* remembered type */
516 abort();
517 break;
518 case 'v': /* void */
519 abort();
520 break;
521 case 'x': /* long long */
522 case 'l': /* long */
523 case 'i': /* int */
524 case 's': /* short */
525 case 'c': /* char */
526 done = is_integral = 1;
527 break;
528 case 'r': /* long double */
529 case 'd': /* double */
530 case 'f': /* float */
531 done = is_real = 1;
532 break;
533 default:
534 abort();
535 }
536 }
537 if (is_integral)
538 {
539 if (*p == 'm')
540 {
541 string_appendn (&tname, "-", 1);
542 p++;
543 }
544 while (isdigit (*p))
545 {
546 string_appendn (&tname, p, 1);
547 p++;
548 }
549 }
550 else if (is_real)
551 {
552 if (*p == 'm')
553 {
554 string_appendn (&tname, "-", 1);
555 p++;
556 }
557 while (isdigit (*p))
558 {
559 string_appendn (&tname, p, 1);
560 p++;
561 }
562 if (*p == '.') /* fraction */
563 {
564 string_appendn (&tname, ".", 1);
565 p++;
566 while (isdigit (*p))
567 {
568 string_appendn (&tname, p, 1);
569 p++;
570 }
571 }
572 if (*p == 'e') /* exponent */
573 {
574 string_appendn (&tname, "e", 1);
575 p++;
576 while (isdigit (*p))
577 {
578 string_appendn (&tname, p, 1);
579 p++;
580 }
581 }
582 }
583 else if (is_pointer)
584 {
585 int symbol_len;
586
587 if (!get_count (&p, &symbol_len))
588 {
589 success = 0;
590 break;
591 }
592 string_appendn (&tname, p, symbol_len);
593 p += symbol_len;
594 }
595 }
596 need_comma = 1;
597 }
598 string_append (&tname, ">::");
599 if (destructor)
600 string_append(&tname, "~");
601 if (constructor || destructor) {
602 string_append (&tname, trawname.b);
603 }
604 string_delete(&trawname);
605
606 if (!success) {
607 string_delete(&tname);
608 return 0;
609 }
610 string_prepend (&decl, tname.b);
611 string_delete(&tname);
612
613 if (static_type)
614 {
615 string_append(&decl, p+1);
616 p += strlen(p);
617 success = 1;
618 }
619 else
620 success = do_args (&p, &decl, arg_mode);
621 break;
622 }
623 }
624
625 for (i = 0; i < ntypes; i++)
626 if (typevec[i] != NULL)
627 free (typevec[i]);
628 ntypes = 0;
629 if (typevec != NULL)
630 {
631 free ((char *)typevec);
632 typevec = NULL;
633 typevec_size = 0;
634 }
635
636 if (success)
637 {
638 string_appendn (&decl, "", 1);
639 return decl.b;
640 }
641 else
642 {
643 string_delete (&decl);
644 return NULL;
645 }
646 }
647
648 static int
649 get_count (type, count)
650 const char **type;
651 int *count;
652 {
653 if (!isdigit (**type))
654 return 0;
655 *count = **type - '0';
656 *type += 1;
657 /* see flush_repeats in cplus-method.c */
658 if (isdigit (**type))
659 {
660 const char *p = *type;
661 int n = *count;
662 do
663 {
664 n *= 10;
665 n += *p - '0';
666 p += 1;
667 }
668 while (isdigit (*p));
669 if (*p == '_')
670 {
671 *type = p + 1;
672 *count = n;
673 }
674 }
675 return 1;
676 }
677
678 /* result will be initialised here; it will be freed on failure */
679
680 static int
681 do_type (type, result, arg_mode)
682 const char **type;
683 string *result;
684 int arg_mode;
685 {
686 int n;
687 int done;
688 int non_empty = 0;
689 int success;
690 string decl;
691 const char *remembered_type;
692
693 string_init (&decl);
694 string_init (result);
695
696 done = 0;
697 success = 1;
698 while (success && !done)
699 {
700 int member;
701 switch (**type)
702 {
703 case 'Q':
704 n = (*type)[1] - '0';
705 if (n < 0 || n > 9)
706 success = 0;
707 *type += 2;
708 while (n-- > 0)
709 do_type (type, result, arg_mode);
710 break;
711
712 case 'P':
713 *type += 1;
714 string_prepend (&decl, "*");
715 break;
716
717 case 'R':
718 *type += 1;
719 string_prepend (&decl, "&");
720 break;
721
722 case 'T':
723 *type += 1;
724 if (!get_count (type, &n) || n >= ntypes)
725 success = 0;
726 else
727 {
728 remembered_type = typevec[n];
729 type = &remembered_type;
730 }
731 break;
732
733 case 'F':
734 *type += 1;
735 if (!string_empty (&decl) && decl.b[0] == '*')
736 {
737 string_prepend (&decl, "(");
738 string_append (&decl, ")");
739 }
740 if (!do_args (type, &decl, arg_mode) || **type != '_')
741 success = 0;
742 else
743 *type += 1;
744 break;
745
746 case 'M':
747 case 'O':
748 {
749 int constp = 0;
750 int volatilep = 0;
751
752 member = **type == 'M';
753 *type += 1;
754 if (!isdigit (**type))
755 {
756 success = 0;
757 break;
758 }
759 n = 0;
760 do
761 {
762 n *= 10;
763 n += **type - '0';
764 *type += 1;
765 }
766 while (isdigit (**type));
767 if (strlen (*type) < n)
768 {
769 success = 0;
770 break;
771 }
772 string_append (&decl, ")");
773 string_prepend (&decl, "::");
774 string_prependn (&decl, *type, n);
775 string_prepend (&decl, "(");
776 *type += n;
777 if (member)
778 {
779 if (**type == 'C')
780 {
781 *type += 1;
782 constp = 1;
783 }
784 if (**type == 'V')
785 {
786 *type += 1;
787 volatilep = 1;
788 }
789 if (*(*type)++ != 'F')
790 {
791 success = 0;
792 break;
793 }
794 }
795 if ((member && !do_args (type, &decl, arg_mode)) || **type != '_')
796 {
797 success = 0;
798 break;
799 }
800 *type += 1;
801 if (! print_ansi_qualifiers)
802 break;
803 if (constp)
804 {
805 if (non_empty)
806 string_append (&decl, " ");
807 else
808 non_empty = 1;
809 string_append (&decl, "const");
810 }
811 if (volatilep)
812 {
813 if (non_empty)
814 string_append (&decl, " ");
815 else
816 non_empty = 1;
817 string_append (&decl, "volatile");
818 }
819 break;
820 }
821
822 case 'C':
823 if ((*type)[1] == 'P')
824 {
825 *type += 1;
826 if (print_ansi_qualifiers)
827 {
828 if (!string_empty (&decl))
829 string_prepend (&decl, " ");
830 string_prepend (&decl, "const");
831 }
832 break;
833 }
834
835 /* fall through */
836 default:
837 done = 1;
838 break;
839 }
840 }
841
842 done = 0;
843 non_empty = 0;
844 while (success && !done)
845 {
846 switch (**type)
847 {
848 case 'C':
849 *type += 1;
850 if (print_ansi_qualifiers)
851 {
852 if (non_empty)
853 string_append (result, " ");
854 else
855 non_empty = 1;
856 string_append (result, "const");
857 }
858 break;
859 case 'U':
860 *type += 1;
861 if (non_empty)
862 string_append (result, " ");
863 else
864 non_empty = 1;
865 string_append (result, "unsigned");
866 break;
867 case 'V':
868 *type += 1;
869 if (print_ansi_qualifiers)
870 {
871 if (non_empty)
872 string_append (result, " ");
873 else
874 non_empty = 1;
875 string_append (result, "volatile");
876 }
877 break;
878 default:
879 done = 1;
880 break;
881 }
882 }
883
884 if (success)
885 switch (**type)
886 {
887 case '\0':
888 case '_':
889 break;
890 case 'v':
891 *type += 1;
892 if (non_empty)
893 string_append (result, " ");
894 string_append (result, "void");
895 break;
896 case 'x':
897 *type += 1;
898 if (non_empty)
899 string_append (result, " ");
900 string_append (result, "long long");
901 break;
902 case 'l':
903 *type += 1;
904 if (non_empty)
905 string_append (result, " ");
906 string_append (result, "long");
907 break;
908 case 'i':
909 *type += 1;
910 if (non_empty)
911 string_append (result, " ");
912 string_append (result, "int");
913 break;
914 case 's':
915 *type += 1;
916 if (non_empty)
917 string_append (result, " ");
918 string_append (result, "short");
919 break;
920 case 'c':
921 *type += 1;
922 if (non_empty)
923 string_append (result, " ");
924 string_append (result, "char");
925 break;
926 case 'r':
927 *type += 1;
928 if (non_empty)
929 string_append (result, " ");
930 string_append (result, "long double");
931 break;
932 case 'd':
933 *type += 1;
934 if (non_empty)
935 string_append (result, " ");
936 string_append (result, "double");
937 break;
938 case 'f':
939 *type += 1;
940 if (non_empty)
941 string_append (result, " ");
942 string_append (result, "float");
943 break;
944 case 'G':
945 *type += 1;
946 if (!isdigit (**type))
947 {
948 success = 0;
949 break;
950 }
951 /* fall through */
952 case '0':
953 case '1':
954 case '2':
955 case '3':
956 case '4':
957 case '5':
958 case '6':
959 case '7':
960 case '8':
961 case '9':
962 n = 0;
963 do
964 {
965 n *= 10;
966 n += **type - '0';
967 *type += 1;
968 }
969 while (isdigit (**type));
970 if (strlen (*type) < n)
971 {
972 success = 0;
973 break;
974 }
975 if (non_empty)
976 string_append (result, " ");
977 string_appendn (result, *type, n);
978 *type += n;
979 break;
980 default:
981 success = 0;
982 break;
983 }
984
985 if (success)
986 {
987 if (!string_empty (&decl))
988 {
989 string_append (result, " ");
990 string_appends (result, &decl);
991 }
992 string_delete (&decl);
993 return 1;
994 }
995 else
996 {
997 string_delete (&decl);
998 string_delete (result);
999 return 0;
1000 }
1001 }
1002
1003 /* `result' will be initialised in do_type; it will be freed on failure */
1004
1005 static int
1006 do_arg (type, result, arg_mode)
1007 const char **type;
1008 string *result;
1009 int arg_mode;
1010 {
1011 const char *start = *type;
1012
1013 if (!do_type (type, result, arg_mode))
1014 return 0;
1015 remember_type (start, *type - start);
1016 return 1;
1017 }
1018
1019 static void
1020 remember_type (start, len)
1021 const char *start;
1022 int len;
1023 {
1024 char *tem;
1025
1026 if (ntypes >= typevec_size)
1027 {
1028 if (typevec_size == 0)
1029 {
1030 typevec_size = 3;
1031 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
1032 }
1033 else
1034 {
1035 typevec_size *= 2;
1036 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
1037 }
1038 }
1039 tem = (char *) xmalloc (len + 1);
1040 memcpy (tem, start, len);
1041 tem[len] = '\0';
1042 typevec[ntypes++] = tem;
1043 }
1044
1045 /* `decl' must be already initialised, usually non-empty;
1046 it won't be freed on failure */
1047
1048 static int
1049 do_args (type, decl, arg_mode)
1050 const char **type;
1051 string *decl;
1052 int arg_mode;
1053 {
1054 string arg;
1055 int need_comma = 0;
1056
1057 if (print_arg_types)
1058 string_append (decl, "(");
1059
1060 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
1061 {
1062 if (**type == 'N')
1063 {
1064 int r;
1065 int t;
1066 *type += 1;
1067 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
1068 return 0;
1069 while (--r >= 0)
1070 {
1071 const char *tem = typevec[t];
1072 if (need_comma && print_arg_types)
1073 string_append (decl, ", ");
1074 if (!do_arg (&tem, &arg, arg_mode))
1075 return 0;
1076 if (print_arg_types)
1077 string_appends (decl, &arg);
1078 string_delete (&arg);
1079 need_comma = 1;
1080 }
1081 }
1082 else
1083 {
1084 if (need_comma & print_arg_types)
1085 string_append (decl, ", ");
1086 if (!do_arg (type, &arg, arg_mode))
1087 return 0;
1088 if (print_arg_types)
1089 string_appends (decl, &arg);
1090 string_delete (&arg);
1091 need_comma = 1;
1092 }
1093 }
1094
1095 if (**type == 'v')
1096 *type += 1;
1097 else if (**type == 'e')
1098 {
1099 *type += 1;
1100 if (print_arg_types)
1101 {
1102 if (need_comma)
1103 string_append (decl, ",");
1104 string_append (decl, "...");
1105 }
1106 }
1107
1108 if (print_arg_types)
1109 string_append (decl, ")");
1110 return 1;
1111 }
1112
1113 static void
1114 munge_function_name (name, arg_mode)
1115 string *name;
1116 int arg_mode;
1117 {
1118 if (string_empty (name))
1119 return;
1120
1121 if (name->p - name->b >= 3
1122 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == CPLUS_MARKER)
1123 {
1124 int i;
1125 /* see if it's an assignment expression */
1126 if (name->p - name->b >= 10 /* op$assign_ */
1127 && memcmp (name->b + 3, "assign_", 7) == 0)
1128 {
1129 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1130 {
1131 int len = name->p - name->b - 10;
1132 if (strlen (optable[i].in) == len
1133 && memcmp (optable[i].in, name->b + 10, len) == 0)
1134 {
1135 string_clear (name);
1136 string_append (name, "operator");
1137 string_append (name, optable[i].out);
1138 string_append (name, "=");
1139 return;
1140 }
1141 }
1142 }
1143 else
1144 {
1145 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1146 {
1147 int len = name->p - name->b - 3;
1148 if (strlen (optable[i].in) == len
1149 && memcmp (optable[i].in, name->b + 3, len) == 0)
1150 {
1151 string_clear (name);
1152 string_append (name, "operator");
1153 string_append (name, optable[i].out);
1154 return;
1155 }
1156 }
1157 }
1158 return;
1159 }
1160 else if (name->p - name->b >= 5 && memcmp (name->b, "type$", 5) == 0)
1161 {
1162 /* type conversion operator */
1163 string type;
1164 const char *tem = name->b + 5;
1165 if (do_type (&tem, &type, arg_mode))
1166 {
1167 string_clear (name);
1168 string_append (name, "operator ");
1169 string_appends (name, &type);
1170 string_delete (&type);
1171 return;
1172 }
1173 }
1174 /* ANSI. */
1175 else if (name->b[2] == 'o' && name->b[3] == 'p')
1176 {
1177 /* type conversion operator. */
1178 string type;
1179 const char *tem = name->b + 4;
1180 if (do_type (&tem, &type, arg_mode))
1181 {
1182 string_clear (name);
1183 string_append (name, "operator ");
1184 string_appends (name, &type);
1185 string_delete (&type);
1186 return;
1187 }
1188 }
1189 else if (name->b[0] == '_' && name->b[1] == '_'
1190 && name->b[2] >= 'a' && name->b[2] <= 'z'
1191 && name->b[3] >= 'a' && name->b[3] <= 'z')
1192 {
1193 int i;
1194
1195 if (name->b[4] == '\0')
1196 {
1197 /* Operator. */
1198 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1199 {
1200 if (strlen (optable[i].in) == 2
1201 && memcmp (optable[i].in, name->b + 2, 2) == 0)
1202 {
1203 string_clear (name);
1204 string_append (name, "operator");
1205 string_append (name, optable[i].out);
1206 return;
1207 }
1208 }
1209 }
1210 else
1211 {
1212 if (name->b[2] != 'a' || name->b[5] != '\0')
1213 return;
1214 /* Assignment. */
1215 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1216 {
1217 if (strlen (optable[i].in) == 3
1218 && memcmp (optable[i].in, name->b + 2, 3) == 0)
1219 {
1220 string_clear (name);
1221 string_append (name, "operator");
1222 string_append (name, optable[i].out);
1223 return;
1224 }
1225 }
1226 }
1227 }
1228 }
1229
1230 /* a mini string-handling package */
1231
1232 static void
1233 string_need (s, n)
1234 string *s;
1235 int n;
1236 {
1237 if (s->b == NULL)
1238 {
1239 if (n < 32)
1240 n = 32;
1241 s->p = s->b = (char *) xmalloc (n);
1242 s->e = s->b + n;
1243 }
1244 else if (s->e - s->p < n)
1245 {
1246 int tem = s->p - s->b;
1247 n += tem;
1248 n *= 2;
1249 s->b = (char *) xrealloc (s->b, n);
1250 s->p = s->b + tem;
1251 s->e = s->b + n;
1252 }
1253 }
1254
1255 static void
1256 string_delete (s)
1257 string *s;
1258 {
1259 if (s->b != NULL)
1260 {
1261 free (s->b);
1262 s->b = s->e = s->p = NULL;
1263 }
1264 }
1265
1266 static void
1267 string_init (s)
1268 string *s;
1269 {
1270 s->b = s->p = s->e = NULL;
1271 }
1272
1273 static void
1274 string_clear (s)
1275 string *s;
1276 {
1277 s->p = s->b;
1278 }
1279
1280 static int
1281 string_empty (s)
1282 string *s;
1283 {
1284 return s->b == s->p;
1285 }
1286
1287 static void
1288 string_append (p, s)
1289 string *p;
1290 const char *s;
1291 {
1292 int n;
1293 if (s == NULL || *s == '\0')
1294 return;
1295 n = strlen (s);
1296 string_need (p, n);
1297 memcpy (p->p, s, n);
1298 p->p += n;
1299 }
1300
1301 static void
1302 string_appends (p, s)
1303 string *p, *s;
1304 {
1305 int n;
1306 if (s->b == s->p)
1307 return;
1308 n = s->p - s->b;
1309 string_need (p, n);
1310 memcpy (p->p, s->b, n);
1311 p->p += n;
1312 }
1313
1314 static void
1315 string_appendn (p, s, n)
1316 string *p;
1317 const char *s;
1318 int n;
1319 {
1320 if (n == 0)
1321 return;
1322 string_need (p, n);
1323 memcpy (p->p, s, n);
1324 p->p += n;
1325 }
1326
1327 static void
1328 string_prepend (p, s)
1329 string *p;
1330 const char *s;
1331 {
1332 if (s == NULL || *s == '\0')
1333 return;
1334 string_prependn (p, s, strlen (s));
1335 }
1336
1337 #if 0
1338 static void
1339 string_prepends (p, s)
1340 string *p, *s;
1341 {
1342 if (s->b == s->p)
1343 return;
1344 string_prependn (p, s->b, s->p - s->b);
1345 }
1346 #endif
1347
1348 static void
1349 string_prependn (p, s, n)
1350 string *p;
1351 const char *s;
1352 int n;
1353 {
1354 char *q;
1355
1356 if (n == 0)
1357 return;
1358 string_need (p, n);
1359 for (q = p->p - 1; q >= p->b; q--)
1360 q[n] = q[0];
1361 memcpy (p->b, s, n);
1362 p->p += n;
1363 }
This page took 0.058266 seconds and 4 git commands to generate.