* defs.h: Incorporate param.h. All users changed.
[deliverable/binutils-gdb.git] / gdb / cplus-dem.c
CommitLineData
dd3b648e 1/* Demangler for GNU C++
7d9884b9 2 Copyright 1989, 1991 Free Software Foundation, Inc.
dd3b648e
RP
3 written by James Clark (jjc@jclark.uucp)
4
572acbbe
MT
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.
99a7de40 9
572acbbe
MT
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.
99a7de40 14
572acbbe
MT
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. */
dd3b648e 18
7d9884b9 19/* This is for g++ 1.95.03 (November 13 verison). */
dd3b648e
RP
20
21/* This file exports one function
22
23 char *cplus_demangle (const char *name, int mode)
572acbbe 24
dd3b648e
RP
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
e1ce8aa5
JK
55/* GDB-specific, FIXME. */
56#include "defs.h"
e1ce8aa5 57
dd3b648e
RP
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
8ffd75c8
JG
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
dd3b648e
RP
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__
2cb3be2c
JK
87/* GDB prototypes these as void* in defs.h, so we better too, at least
88 as long as we're including defs.h. */
89extern void *xmalloc (int);
90extern void *xrealloc (char *, int);
d11c44f1 91extern void free (void *);
dd3b648e
RP
92#else
93extern char *xmalloc ();
94extern char *xrealloc ();
95extern void free ();
96#endif
97
98static char **typevec = 0;
99static int ntypes = 0;
100static int typevec_size = 0;
101
ef98d5ac 102const static struct optable {
dd3b648e
RP
103 const char *in;
104 const char *out;
572acbbe 105 int ansi;
dd3b648e 106} optable[] = {
572acbbe
MT
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=) */
dd3b648e
RP
180};
181
182/* Beware: these aren't '\0' terminated. */
183
ef98d5ac 184typedef struct string {
dd3b648e
RP
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__
191static void string_need (string *s, int n);
192static void string_delete (string *s);
193static void string_init (string *s);
194static void string_clear (string *s);
195static int string_empty (string *s);
196static void string_append (string *p, const char *s);
197static void string_appends (string *p, string *s);
198static void string_appendn (string *p, const char *s, int n);
199static void string_prepend (string *p, const char *s);
200#if 0
201static void string_prepends (string *p, string *s);
202#endif
203static void string_prependn (string *p, const char *s, int n);
204static int get_count (const char **type, int *count);
205static int do_args (const char **type, string *decl, int arg_mode);
206static int do_type (const char **type, string *result, int arg_mode);
207static int do_arg (const char **type, string *result, int arg_mode);
208static void munge_function_name (string *name, int arg_mode);
209static void remember_type (const char *type, int len);
210#else
211static void string_need ();
212static void string_delete ();
213static void string_init ();
214static void string_clear ();
215static int string_empty ();
216static void string_append ();
217static void string_appends ();
218static void string_appendn ();
219static void string_prepend ();
220#if 0
221static void string_prepends ();
222#endif
223static void string_prependn ();
224static int get_count ();
225static int do_args ();
226static int do_type ();
227static int do_arg ();
228static int do_args ();
229static void munge_function_name ();
230static void remember_type ();
231#endif
232
55838914 233/* Takes operator name as e.g. "++" and returns mangled
572acbbe
MT
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. */
55838914 238char *
572acbbe 239cplus_mangle_opname (opname, arg_mode)
55838914 240 char *opname;
572acbbe 241 int arg_mode;
55838914
JK
242{
243 int i, len = strlen (opname);
55838914 244
572acbbe
MT
245 if (arg_mode != 0 && arg_mode != 1)
246 error ("invalid arg_mode");
247
55838914
JK
248 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
249 {
250 if (strlen (optable[i].out) == len
572acbbe 251 && arg_mode == optable[i].ansi
55838914 252 && memcmp (optable[i].out, opname, len) == 0)
e1ce8aa5 253 return (char *)optable[i].in;
55838914 254 }
55838914
JK
255 return 0;
256}
257
dd3b648e
RP
258char *
259cplus_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;
4c53d9ca
DHW
267 int destructor = 0;
268 int static_type = 0;
dd3b648e
RP
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 */
f88e7af8 291 if (type[0] == '_' && type[1] == CPLUS_MARKER && type[2] == '_')
dd3b648e 292 {
572acbbe
MT
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;
dd3b648e
RP
301 }
302 /* static data member */
572acbbe 303 if (*type != '_' && (p = strchr (type, CPLUS_MARKER)) != NULL)
dd3b648e 304 {
572acbbe
MT
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;
dd3b648e 311 }
572acbbe 312 /* virtual table "_vt$" */
f88e7af8 313 if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == CPLUS_MARKER)
dd3b648e 314 {
572acbbe
MT
315 int n = strlen (type + 4) + 14 + 1;
316 char *tem = (char *) xmalloc (n);
dd3b648e
RP
317 strcpy (tem, type + 4);
318 strcat (tem, " virtual table");
319 return tem;
320 }
321 return NULL;
322 }
323
324 string_init (&decl);
325
4c53d9ca
DHW
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)
dd3b648e 335 {
572acbbe 336 if (!isdigit (p[2]) && ('t' != p[2]))
dd3b648e 337 {
572acbbe
MT
338 p += 1;
339 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
340 p++;
341 string_appendn (&decl, type, p - type);
342 *(decl.p) = '\0';
343 munge_function_name (&decl, 1);
344 if (decl.b[0] == '_')
345 {
346 string_delete (&decl);
347 return NULL;
348 }
349 else
350 p += 2;
351 }
352 else
353 {
354 constructor = 1;
355 p += 2;
dd3b648e 356 }
dd3b648e
RP
357 }
358 else
359 {
360 string_appendn (&decl, type, p - type);
572acbbe 361 *(decl.p) = '\0';
dd3b648e 362 munge_function_name (&decl, arg_mode);
4c53d9ca 363 p += 2;
dd3b648e 364 }
dd3b648e
RP
365
366#ifndef LONGERNAMES
367 premangle = p;
368#endif
369 switch (*p)
370 {
371 case 'C':
372 /* a const member function */
373 if (!isdigit (p[1]))
374 {
375 string_delete (&decl);
376 return NULL;
377 }
378 p += 1;
379 const_flag = 1;
380 /* fall through */
381 case '0':
382 case '1':
383 case '2':
384 case '3':
385 case '4':
386 case '5':
387 case '6':
388 case '7':
389 case '8':
390 case '9':
391 n = 0;
392 do
393 {
394 n *= 10;
395 n += *p - '0';
396 p += 1;
397 }
398 while (isdigit (*p));
399 if (strlen (p) < n)
400 {
401 string_delete (&decl);
402 return NULL;
403 }
4c53d9ca 404 if (constructor || destructor)
dd3b648e
RP
405 {
406 string_appendn (&decl, p, n);
407 string_append (&decl, "::");
4c53d9ca
DHW
408 if (destructor)
409 string_append(&decl, "~");
dd3b648e
RP
410 string_appendn (&decl, p, n);
411 }
412 else
413 {
414 string_prepend (&decl, "::");
415 string_prependn (&decl, p, n);
416 }
417 p += n;
418#ifndef LONGERNAMES
419 remember_type (premangle, p - premangle);
420#endif
4c53d9ca
DHW
421 if (static_type)
422 {
423 string_append(&decl, p+1);
424 p += strlen(p);
425 success = 1;
426 }
427 else
428 success = do_args (&p, &decl, arg_mode);
dd3b648e
RP
429 if (const_flag && print_arg_types)
430 string_append (&decl, " const");
431 break;
432 case 'F':
433 p += 1;
434 success = do_args (&p, &decl, arg_mode);
435 break;
4c53d9ca
DHW
436 /* template additions */
437 case 't':
438 p += 1;
439 {
440 int r, i;
441 int non_empty = 0;
442 string tname;
443 string trawname;
444
445 string temp;
446 int need_comma = 0;
447
448 string_init(&tname);
449 string_init(&trawname);
450
451 /* get template name */
452 if (!get_count (&p, &r))
453 return 0;
454 string_appendn (&tname, p, r);
455 string_appendn (&trawname, p, r);
456 string_appendn (&trawname, "", 1);
457 p += r;
458 string_append (&tname, "<");
459 /* get size of template parameter list */
460 if (!get_count (&p, &r))
461 return 0;
462 for (i = 0; i < r; i++)
463 {
464 if (need_comma)
465 string_append (&tname, ", ");
466 /* Z for type parameters */
467 if (*p == 'Z')
468 {
469 p += 1;
470
471 success = do_type (&p, &temp, arg_mode);
472 string_appendn (&temp, "", 1);
473 if (success)
474 string_append (&tname, temp.b);
475 string_delete(&temp);
476 if (!success)
477 break;
478 }
479 /* otherwise, value parameter */
480 else
481 {
482 const char *old_p = p;
483 int is_pointer = 0;
484 int is_real = 0;
485 int is_integral = 0;
486 int done = 0;
487
488 success = do_type (&p, &temp, arg_mode);
489 string_appendn (&temp, "", 1);
490 if (success)
491 string_append (&tname, temp.b);
492 string_delete(&temp);
493 if (!success)
494 break;
495 string_append (&tname, "=");
496 while (*old_p && !done)
497 {
498 switch (*old_p)
499 {
500 case 'P':
501 case 'R':
502 done = is_pointer = 1;
503 break;
504 case 'C': /* const */
505 case 'U': /* unsigned */
506 case 'V': /* volatile */
507 case 'F': /* function */
508 case 'M': /* member function */
509 case 'O': /* ??? */
510 old_p++;
511 continue;
512 case 'Q': /* repetition of following */
513 case 'T': /* remembered type */
514 abort();
515 break;
516 case 'v': /* void */
517 abort();
518 break;
519 case 'x': /* long long */
520 case 'l': /* long */
521 case 'i': /* int */
522 case 's': /* short */
523 case 'c': /* char */
524 done = is_integral = 1;
525 break;
526 case 'r': /* long double */
527 case 'd': /* double */
528 case 'f': /* float */
529 done = is_real = 1;
530 break;
531 default:
532 abort();
533 }
534 }
535 if (is_integral)
536 {
537 if (*p == 'm')
538 {
539 string_appendn (&tname, "-", 1);
540 p++;
541 }
542 while (isdigit (*p))
543 {
544 string_appendn (&tname, p, 1);
545 p++;
546 }
547 }
548 else if (is_real)
549 {
550 if (*p == 'm')
551 {
552 string_appendn (&tname, "-", 1);
553 p++;
554 }
555 while (isdigit (*p))
556 {
557 string_appendn (&tname, p, 1);
558 p++;
559 }
560 if (*p == '.') /* fraction */
561 {
562 string_appendn (&tname, ".", 1);
563 p++;
564 while (isdigit (*p))
565 {
566 string_appendn (&tname, p, 1);
567 p++;
568 }
569 }
570 if (*p == 'e') /* exponent */
571 {
572 string_appendn (&tname, "e", 1);
573 p++;
574 while (isdigit (*p))
575 {
576 string_appendn (&tname, p, 1);
577 p++;
578 }
579 }
580 }
581 else if (is_pointer)
582 {
583 int symbol_len;
584
585 if (!get_count (&p, &symbol_len))
586 {
587 success = 0;
588 break;
589 }
590 string_appendn (&tname, p, symbol_len);
591 p += symbol_len;
592 }
593 }
594 need_comma = 1;
595 }
596 string_append (&tname, ">::");
597 if (destructor)
598 string_append(&tname, "~");
599 if (constructor || destructor) {
600 string_append (&tname, trawname.b);
601 }
602 string_delete(&trawname);
603
604 if (!success) {
605 string_delete(&tname);
606 return 0;
607 }
608 string_prepend (&decl, tname.b);
609 string_delete(&tname);
610
611 if (static_type)
612 {
613 string_append(&decl, p+1);
614 p += strlen(p);
615 success = 1;
616 }
617 else
618 success = do_args (&p, &decl, arg_mode);
619 break;
620 }
dd3b648e
RP
621 }
622
623 for (i = 0; i < ntypes; i++)
624 if (typevec[i] != NULL)
625 free (typevec[i]);
626 ntypes = 0;
627 if (typevec != NULL)
628 {
629 free ((char *)typevec);
630 typevec = NULL;
631 typevec_size = 0;
632 }
633
634 if (success)
635 {
636 string_appendn (&decl, "", 1);
637 return decl.b;
638 }
639 else
640 {
641 string_delete (&decl);
642 return NULL;
643 }
644}
645
646static int
647get_count (type, count)
648 const char **type;
649 int *count;
650{
651 if (!isdigit (**type))
652 return 0;
653 *count = **type - '0';
654 *type += 1;
655 /* see flush_repeats in cplus-method.c */
656 if (isdigit (**type))
657 {
658 const char *p = *type;
659 int n = *count;
660 do
661 {
662 n *= 10;
663 n += *p - '0';
664 p += 1;
665 }
666 while (isdigit (*p));
667 if (*p == '_')
668 {
669 *type = p + 1;
670 *count = n;
671 }
672 }
673 return 1;
674}
675
676/* result will be initialised here; it will be freed on failure */
677
678static int
679do_type (type, result, arg_mode)
680 const char **type;
681 string *result;
682 int arg_mode;
683{
684 int n;
685 int done;
686 int non_empty = 0;
687 int success;
688 string decl;
689 const char *remembered_type;
690
691 string_init (&decl);
692 string_init (result);
693
694 done = 0;
695 success = 1;
696 while (success && !done)
697 {
698 int member;
699 switch (**type)
700 {
55838914
JK
701 case 'Q':
702 n = (*type)[1] - '0';
703 if (n < 0 || n > 9)
704 success = 0;
705 *type += 2;
706 while (n-- > 0)
e1ce8aa5 707 do_type (type, result, arg_mode);
55838914
JK
708 break;
709
dd3b648e
RP
710 case 'P':
711 *type += 1;
712 string_prepend (&decl, "*");
713 break;
714
715 case 'R':
716 *type += 1;
717 string_prepend (&decl, "&");
718 break;
719
720 case 'T':
721 *type += 1;
722 if (!get_count (type, &n) || n >= ntypes)
723 success = 0;
724 else
725 {
726 remembered_type = typevec[n];
727 type = &remembered_type;
728 }
729 break;
730
731 case 'F':
732 *type += 1;
733 if (!string_empty (&decl) && decl.b[0] == '*')
734 {
735 string_prepend (&decl, "(");
736 string_append (&decl, ")");
737 }
738 if (!do_args (type, &decl, arg_mode) || **type != '_')
739 success = 0;
740 else
741 *type += 1;
742 break;
743
744 case 'M':
745 case 'O':
746 {
747 int constp = 0;
748 int volatilep = 0;
749
750 member = **type == 'M';
751 *type += 1;
752 if (!isdigit (**type))
753 {
754 success = 0;
755 break;
756 }
757 n = 0;
758 do
759 {
760 n *= 10;
761 n += **type - '0';
762 *type += 1;
763 }
764 while (isdigit (**type));
765 if (strlen (*type) < n)
766 {
767 success = 0;
768 break;
769 }
770 string_append (&decl, ")");
771 string_prepend (&decl, "::");
772 string_prependn (&decl, *type, n);
773 string_prepend (&decl, "(");
774 *type += n;
775 if (member)
776 {
777 if (**type == 'C')
778 {
779 *type += 1;
780 constp = 1;
781 }
782 if (**type == 'V')
783 {
784 *type += 1;
785 volatilep = 1;
786 }
787 if (*(*type)++ != 'F')
788 {
789 success = 0;
790 break;
791 }
792 }
793 if ((member && !do_args (type, &decl, arg_mode)) || **type != '_')
794 {
795 success = 0;
796 break;
797 }
798 *type += 1;
799 if (! print_ansi_qualifiers)
800 break;
801 if (constp)
802 {
803 if (non_empty)
804 string_append (&decl, " ");
805 else
806 non_empty = 1;
807 string_append (&decl, "const");
808 }
809 if (volatilep)
810 {
811 if (non_empty)
812 string_append (&decl, " ");
813 else
814 non_empty = 1;
815 string_append (&decl, "volatile");
816 }
817 break;
818 }
819
820 case 'C':
821 if ((*type)[1] == 'P')
822 {
823 *type += 1;
824 if (print_ansi_qualifiers)
825 {
826 if (!string_empty (&decl))
827 string_prepend (&decl, " ");
828 string_prepend (&decl, "const");
829 }
830 break;
831 }
832
833 /* fall through */
834 default:
835 done = 1;
836 break;
837 }
838 }
839
840 done = 0;
841 non_empty = 0;
842 while (success && !done)
843 {
844 switch (**type)
845 {
846 case 'C':
847 *type += 1;
848 if (print_ansi_qualifiers)
849 {
850 if (non_empty)
851 string_append (result, " ");
852 else
853 non_empty = 1;
854 string_append (result, "const");
855 }
856 break;
857 case 'U':
858 *type += 1;
859 if (non_empty)
860 string_append (result, " ");
861 else
862 non_empty = 1;
863 string_append (result, "unsigned");
864 break;
865 case 'V':
866 *type += 1;
867 if (print_ansi_qualifiers)
868 {
869 if (non_empty)
870 string_append (result, " ");
871 else
872 non_empty = 1;
873 string_append (result, "volatile");
874 }
875 break;
876 default:
877 done = 1;
878 break;
879 }
880 }
881
882 if (success)
883 switch (**type)
884 {
885 case '\0':
886 case '_':
887 break;
888 case 'v':
889 *type += 1;
890 if (non_empty)
891 string_append (result, " ");
892 string_append (result, "void");
893 break;
894 case 'x':
895 *type += 1;
896 if (non_empty)
897 string_append (result, " ");
898 string_append (result, "long long");
899 break;
900 case 'l':
901 *type += 1;
902 if (non_empty)
903 string_append (result, " ");
904 string_append (result, "long");
905 break;
906 case 'i':
907 *type += 1;
908 if (non_empty)
909 string_append (result, " ");
910 string_append (result, "int");
911 break;
912 case 's':
913 *type += 1;
914 if (non_empty)
915 string_append (result, " ");
916 string_append (result, "short");
917 break;
918 case 'c':
919 *type += 1;
920 if (non_empty)
921 string_append (result, " ");
922 string_append (result, "char");
923 break;
924 case 'r':
925 *type += 1;
926 if (non_empty)
927 string_append (result, " ");
928 string_append (result, "long double");
929 break;
930 case 'd':
931 *type += 1;
932 if (non_empty)
933 string_append (result, " ");
934 string_append (result, "double");
935 break;
936 case 'f':
937 *type += 1;
938 if (non_empty)
939 string_append (result, " ");
940 string_append (result, "float");
941 break;
942 case 'G':
943 *type += 1;
944 if (!isdigit (**type))
945 {
946 success = 0;
947 break;
948 }
949 /* fall through */
950 case '0':
951 case '1':
952 case '2':
953 case '3':
954 case '4':
955 case '5':
956 case '6':
957 case '7':
958 case '8':
959 case '9':
960 n = 0;
961 do
962 {
963 n *= 10;
964 n += **type - '0';
965 *type += 1;
966 }
967 while (isdigit (**type));
968 if (strlen (*type) < n)
969 {
970 success = 0;
971 break;
972 }
973 if (non_empty)
974 string_append (result, " ");
975 string_appendn (result, *type, n);
976 *type += n;
977 break;
978 default:
979 success = 0;
980 break;
981 }
982
983 if (success)
984 {
985 if (!string_empty (&decl))
986 {
987 string_append (result, " ");
988 string_appends (result, &decl);
989 }
990 string_delete (&decl);
991 return 1;
992 }
993 else
994 {
995 string_delete (&decl);
996 string_delete (result);
997 return 0;
998 }
999}
1000
1001/* `result' will be initialised in do_type; it will be freed on failure */
1002
1003static int
1004do_arg (type, result, arg_mode)
1005 const char **type;
1006 string *result;
1007 int arg_mode;
1008{
1009 const char *start = *type;
1010
1011 if (!do_type (type, result, arg_mode))
1012 return 0;
1013 remember_type (start, *type - start);
1014 return 1;
1015}
1016
1017static void
1018remember_type (start, len)
1019 const char *start;
1020 int len;
1021{
1022 char *tem;
1023
1024 if (ntypes >= typevec_size)
1025 {
1026 if (typevec_size == 0)
1027 {
1028 typevec_size = 3;
1029 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
1030 }
1031 else
1032 {
1033 typevec_size *= 2;
1034 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
1035 }
1036 }
1037 tem = (char *) xmalloc (len + 1);
1038 memcpy (tem, start, len);
1039 tem[len] = '\0';
1040 typevec[ntypes++] = tem;
1041}
1042
1043/* `decl' must be already initialised, usually non-empty;
1044 it won't be freed on failure */
1045
1046static int
1047do_args (type, decl, arg_mode)
1048 const char **type;
1049 string *decl;
1050 int arg_mode;
1051{
1052 string arg;
1053 int need_comma = 0;
1054
1055 if (print_arg_types)
1056 string_append (decl, "(");
1057
1058 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
1059 {
1060 if (**type == 'N')
1061 {
1062 int r;
1063 int t;
1064 *type += 1;
1065 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
1066 return 0;
1067 while (--r >= 0)
1068 {
1069 const char *tem = typevec[t];
1070 if (need_comma && print_arg_types)
1071 string_append (decl, ", ");
1072 if (!do_arg (&tem, &arg, arg_mode))
1073 return 0;
1074 if (print_arg_types)
1075 string_appends (decl, &arg);
1076 string_delete (&arg);
1077 need_comma = 1;
1078 }
1079 }
1080 else
1081 {
1082 if (need_comma & print_arg_types)
1083 string_append (decl, ", ");
1084 if (!do_arg (type, &arg, arg_mode))
1085 return 0;
1086 if (print_arg_types)
1087 string_appends (decl, &arg);
1088 string_delete (&arg);
1089 need_comma = 1;
1090 }
1091 }
1092
1093 if (**type == 'v')
1094 *type += 1;
1095 else if (**type == 'e')
1096 {
1097 *type += 1;
1098 if (print_arg_types)
1099 {
1100 if (need_comma)
1101 string_append (decl, ",");
1102 string_append (decl, "...");
1103 }
1104 }
1105
1106 if (print_arg_types)
1107 string_append (decl, ")");
1108 return 1;
1109}
1110
1111static void
1112munge_function_name (name, arg_mode)
1113 string *name;
1114 int arg_mode;
1115{
572acbbe
MT
1116 if (string_empty (name))
1117 return;
1118
1119 if (name->p - name->b >= 3
f88e7af8 1120 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == CPLUS_MARKER)
dd3b648e
RP
1121 {
1122 int i;
1123 /* see if it's an assignment expression */
1124 if (name->p - name->b >= 10 /* op$assign_ */
1125 && memcmp (name->b + 3, "assign_", 7) == 0)
1126 {
1127 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1128 {
1129 int len = name->p - name->b - 10;
1130 if (strlen (optable[i].in) == len
1131 && memcmp (optable[i].in, name->b + 10, len) == 0)
1132 {
1133 string_clear (name);
1134 string_append (name, "operator");
1135 string_append (name, optable[i].out);
1136 string_append (name, "=");
1137 return;
1138 }
1139 }
1140 }
1141 else
1142 {
1143 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1144 {
1145 int len = name->p - name->b - 3;
1146 if (strlen (optable[i].in) == len
1147 && memcmp (optable[i].in, name->b + 3, len) == 0)
1148 {
1149 string_clear (name);
1150 string_append (name, "operator");
1151 string_append (name, optable[i].out);
1152 return;
1153 }
1154 }
1155 }
1156 return;
1157 }
572acbbe 1158 else if (name->p - name->b >= 5 && memcmp (name->b, "type$", 5) == 0)
dd3b648e
RP
1159 {
1160 /* type conversion operator */
1161 string type;
1162 const char *tem = name->b + 5;
1163 if (do_type (&tem, &type, arg_mode))
1164 {
1165 string_clear (name);
1166 string_append (name, "operator ");
1167 string_appends (name, &type);
1168 string_delete (&type);
1169 return;
1170 }
1171 }
572acbbe
MT
1172 /* ANSI. */
1173 else if (name->b[2] == 'o' && name->b[3] == 'p')
1174 {
1175 /* type conversion operator. */
1176 string type;
1177 const char *tem = name->b + 4;
1a18a185 1178 if (do_type (&tem, &type, arg_mode))
572acbbe
MT
1179 {
1180 string_clear (name);
1181 string_append (name, "operator ");
1182 string_appends (name, &type);
1183 string_delete (&type);
1184 return;
1185 }
1186 }
1187 else if (name->b[0] == '_' && name->b[1] == '_'
1188 && name->b[2] >= 'a' && name->b[2] <= 'z'
1189 && name->b[3] >= 'a' && name->b[3] <= 'z')
1190 {
1191 int i;
1192
1193 if (name->b[4] == '\0')
1194 {
1195 /* Operator. */
1196 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1197 {
1198 if (strlen (optable[i].in) == 2
1199 && memcmp (optable[i].in, name->b + 2, 2) == 0)
1200 {
1201 string_clear (name);
1202 string_append (name, "operator");
1203 string_append (name, optable[i].out);
1204 return;
1205 }
1206 }
1207 }
1208 else
1209 {
1210 if (name->b[2] != 'a' || name->b[5] != '\0')
1211 return;
1212 /* Assignment. */
1213 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1214 {
1215 if (strlen (optable[i].in) == 3
1216 && memcmp (optable[i].in, name->b + 2, 3) == 0)
1217 {
1218 string_clear (name);
1219 string_append (name, "operator");
1220 string_append (name, optable[i].out);
1221 return;
1222 }
1223 }
1224 }
1225 }
dd3b648e
RP
1226}
1227
1228/* a mini string-handling package */
1229
1230static void
1231string_need (s, n)
1232 string *s;
1233 int n;
1234{
1235 if (s->b == NULL)
1236 {
1237 if (n < 32)
1238 n = 32;
1239 s->p = s->b = (char *) xmalloc (n);
1240 s->e = s->b + n;
1241 }
1242 else if (s->e - s->p < n)
1243 {
1244 int tem = s->p - s->b;
1245 n += tem;
1246 n *= 2;
1247 s->b = (char *) xrealloc (s->b, n);
1248 s->p = s->b + tem;
1249 s->e = s->b + n;
1250 }
1251}
1252
1253static void
1254string_delete (s)
1255 string *s;
1256{
1257 if (s->b != NULL)
1258 {
1259 free (s->b);
1260 s->b = s->e = s->p = NULL;
1261 }
1262}
1263
1264static void
1265string_init (s)
1266 string *s;
1267{
1268 s->b = s->p = s->e = NULL;
1269}
1270
1271static void
1272string_clear (s)
1273 string *s;
1274{
1275 s->p = s->b;
1276}
1277
1278static int
1279string_empty (s)
1280 string *s;
1281{
1282 return s->b == s->p;
1283}
1284
1285static void
1286string_append (p, s)
1287 string *p;
1288 const char *s;
1289{
1290 int n;
1291 if (s == NULL || *s == '\0')
1292 return;
1293 n = strlen (s);
1294 string_need (p, n);
1295 memcpy (p->p, s, n);
1296 p->p += n;
1297}
1298
1299static void
1300string_appends (p, s)
1301 string *p, *s;
1302{
1303 int n;
1304 if (s->b == s->p)
1305 return;
1306 n = s->p - s->b;
1307 string_need (p, n);
1308 memcpy (p->p, s->b, n);
1309 p->p += n;
1310}
1311
1312static void
1313string_appendn (p, s, n)
1314 string *p;
1315 const char *s;
1316 int n;
1317{
1318 if (n == 0)
1319 return;
1320 string_need (p, n);
1321 memcpy (p->p, s, n);
1322 p->p += n;
1323}
1324
1325static void
1326string_prepend (p, s)
1327 string *p;
1328 const char *s;
1329{
1330 if (s == NULL || *s == '\0')
1331 return;
1332 string_prependn (p, s, strlen (s));
1333}
1334
1335#if 0
1336static void
1337string_prepends (p, s)
1338 string *p, *s;
1339{
1340 if (s->b == s->p)
1341 return;
1342 string_prependn (p, s->b, s->p - s->b);
1343}
1344#endif
1345
1346static void
1347string_prependn (p, s, n)
1348 string *p;
1349 const char *s;
1350 int n;
1351{
1352 char *q;
1353
1354 if (n == 0)
1355 return;
1356 string_need (p, n);
1357 for (q = p->p - 1; q >= p->b; q--)
1358 q[n] = q[0];
1359 memcpy (p->b, s, n);
1360 p->p += n;
1361}
This page took 0.092112 seconds and 4 git commands to generate.