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