1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97: #if defined (_AIX) && !defined (__GNUC__)
98: #pragma alloca
99: #endif
100:
101: #ifdef HAVE_CONFIG_H
102: #include "config.h"
103: #endif
104:
105: #include <stdio.h>
106:
107: #ifdef HAVE_STDLIB_H
108: #include <stdlib.h>
109: #endif
110: #ifdef HAVE_STRING_H
111: #include <string.h>
112: #endif
113:
114: #ifdef HAVE_ALLOCA_H
115: # include <alloca.h>
116: #else
117: # ifndef alloca
118: # ifdef __GNUC__
119: # define alloca __builtin_alloca
120: # else
121: extern char *alloca ();
122: # endif
123: # endif
124: #endif
125:
126: #include "ansidecl.h"
127: #include "libiberty.h"
128: #include "demangle.h"
129: #include "cp-demangle.h"
130:
131:
132:
133:
134:
135: #ifdef IN_GLIBCPP_V3
136:
137: #define CP_STATIC_IF_GLIBCPP_V3 static
138:
139: #define cplus_demangle_fill_name d_fill_name
140: static int d_fill_name (struct demangle_component *, const char *, int);
141:
142: #define cplus_demangle_fill_extended_operator d_fill_extended_operator
143: static int
144: d_fill_extended_operator (struct demangle_component *, int,
145: struct demangle_component *);
146:
147: #define cplus_demangle_fill_ctor d_fill_ctor
148: static int
149: d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
150: struct demangle_component *);
151:
152: #define cplus_demangle_fill_dtor d_fill_dtor
153: static int
154: d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
155: struct demangle_component *);
156:
157: #define cplus_demangle_mangled_name d_mangled_name
158: static struct demangle_component *d_mangled_name (struct d_info *, int);
159:
160: #define cplus_demangle_type d_type
161: static struct demangle_component *d_type (struct d_info *);
162:
163: #define cplus_demangle_print d_print
164: static char *d_print (int, const struct demangle_component *, int, size_t *);
165:
166: #define cplus_demangle_print_callback d_print_callback
167: static int d_print_callback (int, const struct demangle_component *,
168: demangle_callbackref, void *);
169:
170: #define cplus_demangle_init_info d_init_info
171: static void d_init_info (const char *, int, size_t, struct d_info *);
172:
173: #else
174: #define CP_STATIC_IF_GLIBCPP_V3
175: #endif
176:
177:
178:
179: #ifdef __GNUC__
180: #define CP_DYNAMIC_ARRAYS
181: #else
182: #ifdef __STDC__
183: #ifdef __STDC_VERSION__
184: #if __STDC_VERSION__ >= 199901L
185: #define CP_DYNAMIC_ARRAYS
186: #endif
187: #endif
188: #endif
189: #endif
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200: #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
201: #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
202: #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
203:
204:
205:
206: #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
207: #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
208: (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
209:
210:
211:
212: struct d_standard_sub_info
213: {
214:
215: char code;
216:
217: const char *simple_expansion;
218:
219: int simple_len;
220:
221:
222: const char *full_expansion;
223:
224: int full_len;
225:
226:
227:
228: const char *set_last_name;
229:
230: int set_last_name_len;
231: };
232:
233:
234:
235: #define d_left(dc) ((dc)->u.s_binary.left)
236: #define d_right(dc) ((dc)->u.s_binary.right)
237:
238:
239:
240: struct d_print_template
241: {
242:
243: struct d_print_template *next;
244:
245: const struct demangle_component *template_decl;
246: };
247:
248:
249:
250: struct d_print_mod
251: {
252:
253:
254: struct d_print_mod *next;
255:
256: const struct demangle_component *mod;
257:
258: int printed;
259:
260: struct d_print_template *templates;
261: };
262:
263:
264:
265: struct d_growable_string
266: {
267:
268: char *buf;
269:
270: size_t len;
271:
272: size_t alc;
273:
274: int allocation_failure;
275: };
276:
277: enum { D_PRINT_BUFFER_LENGTH = 256 };
278: struct d_print_info
279: {
280:
281: int options;
282:
283:
284: char buf[D_PRINT_BUFFER_LENGTH];
285:
286: size_t len;
287:
288:
289: char last_char;
290:
291: demangle_callbackref callback;
292:
293: void *opaque;
294:
295: struct d_print_template *templates;
296:
297:
298: struct d_print_mod *modifiers;
299:
300: int demangle_failure;
301: };
302:
303: #ifdef CP_DEMANGLE_DEBUG
304: static void d_dump (struct demangle_component *, int);
305: #endif
306:
307: static struct demangle_component *
308: d_make_empty (struct d_info *);
309:
310: static struct demangle_component *
311: d_make_comp (struct d_info *, enum demangle_component_type,
312: struct demangle_component *,
313: struct demangle_component *);
314:
315: static struct demangle_component *
316: d_make_name (struct d_info *, const char *, int);
317:
318: static struct demangle_component *
319: d_make_builtin_type (struct d_info *,
320: const struct demangle_builtin_type_info *);
321:
322: static struct demangle_component *
323: d_make_operator (struct d_info *,
324: const struct demangle_operator_info *);
325:
326: static struct demangle_component *
327: d_make_extended_operator (struct d_info *, int,
328: struct demangle_component *);
329:
330: static struct demangle_component *
331: d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
332: struct demangle_component *);
333:
334: static struct demangle_component *
335: d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
336: struct demangle_component *);
337:
338: static struct demangle_component *
339: d_make_template_param (struct d_info *, long);
340:
341: static struct demangle_component *
342: d_make_sub (struct d_info *, const char *, int);
343:
344: static int
345: has_return_type (struct demangle_component *);
346:
347: static int
348: is_ctor_dtor_or_conversion (struct demangle_component *);
349:
350: static struct demangle_component *d_encoding (struct d_info *, int);
351:
352: static struct demangle_component *d_name (struct d_info *);
353:
354: static struct demangle_component *d_nested_name (struct d_info *);
355:
356: static struct demangle_component *d_prefix (struct d_info *);
357:
358: static struct demangle_component *d_unqualified_name (struct d_info *);
359:
360: static struct demangle_component *d_source_name (struct d_info *);
361:
362: static long d_number (struct d_info *);
363:
364: static struct demangle_component *d_identifier (struct d_info *, int);
365:
366: static struct demangle_component *d_operator_name (struct d_info *);
367:
368: static struct demangle_component *d_special_name (struct d_info *);
369:
370: static int d_call_offset (struct d_info *, int);
371:
372: static struct demangle_component *d_ctor_dtor_name (struct d_info *);
373:
374: static struct demangle_component **
375: d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
376:
377: static struct demangle_component *
378: d_function_type (struct d_info *);
379:
380: static struct demangle_component *
381: d_bare_function_type (struct d_info *, int);
382:
383: static struct demangle_component *
384: d_class_enum_type (struct d_info *);
385:
386: static struct demangle_component *d_array_type (struct d_info *);
387:
388: static struct demangle_component *
389: d_pointer_to_member_type (struct d_info *);
390:
391: static struct demangle_component *
392: d_template_param (struct d_info *);
393:
394: static struct demangle_component *d_template_args (struct d_info *);
395:
396: static struct demangle_component *
397: d_template_arg (struct d_info *);
398:
399: static struct demangle_component *d_expression (struct d_info *);
400:
401: static struct demangle_component *d_expr_primary (struct d_info *);
402:
403: static struct demangle_component *d_local_name (struct d_info *);
404:
405: static int d_discriminator (struct d_info *);
406:
407: static int
408: d_add_substitution (struct d_info *, struct demangle_component *);
409:
410: static struct demangle_component *d_substitution (struct d_info *, int);
411:
412: static void d_growable_string_init (struct d_growable_string *, size_t);
413:
414: static inline void
415: d_growable_string_resize (struct d_growable_string *, size_t);
416:
417: static inline void
418: d_growable_string_append_buffer (struct d_growable_string *,
419: const char *, size_t);
420: static void
421: d_growable_string_callback_adapter (const char *, size_t, void *);
422:
423: static void
424: d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
425:
426: static inline void d_print_error (struct d_print_info *);
427:
428: static inline int d_print_saw_error (struct d_print_info *);
429:
430: static inline void d_print_flush (struct d_print_info *);
431:
432: static inline void d_append_char (struct d_print_info *, char);
433:
434: static inline void d_append_buffer (struct d_print_info *,
435: const char *, size_t);
436:
437: static inline void d_append_string (struct d_print_info *, const char *);
438:
439: static inline char d_last_char (struct d_print_info *);
440:
441: static void
442: d_print_comp (struct d_print_info *, const struct demangle_component *);
443:
444: static void
445: d_print_java_identifier (struct d_print_info *, const char *, int);
446:
447: static void
448: d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
449:
450: static void
451: d_print_mod (struct d_print_info *, const struct demangle_component *);
452:
453: static void
454: d_print_function_type (struct d_print_info *,
455: const struct demangle_component *,
456: struct d_print_mod *);
457:
458: static void
459: d_print_array_type (struct d_print_info *,
460: const struct demangle_component *,
461: struct d_print_mod *);
462:
463: static void
464: d_print_expr_op (struct d_print_info *, const struct demangle_component *);
465:
466: static void
467: d_print_cast (struct d_print_info *, const struct demangle_component *);
468:
469: static int d_demangle_callback (const char *, int,
470: demangle_callbackref, void *);
471: static char *d_demangle (const char *, int, size_t *);
472:
473: #ifdef CP_DEMANGLE_DEBUG
474:
475: static void
476: d_dump (struct demangle_component *dc, int indent)
477: {
478: int i;
479:
480: if (dc == NULL)
481: {
482: if (indent == 0)
483: printf ("failed demangling\n");
484: return;
485: }
486:
487: for (i = 0; i < indent; ++i)
488: putchar (' ');
489:
490: switch (dc->type)
491: {
492: case DEMANGLE_COMPONENT_NAME:
493: printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
494: return;
495: case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
496: printf ("template parameter %ld\n", dc->u.s_number.number);
497: return;
498: case DEMANGLE_COMPONENT_CTOR:
499: printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
500: d_dump (dc->u.s_ctor.name, indent + 2);
501: return;
502: case DEMANGLE_COMPONENT_DTOR:
503: printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
504: d_dump (dc->u.s_dtor.name, indent + 2);
505: return;
506: case DEMANGLE_COMPONENT_SUB_STD:
507: printf ("standard substitution %s\n", dc->u.s_string.string);
508: return;
509: case DEMANGLE_COMPONENT_BUILTIN_TYPE:
510: printf ("builtin type %s\n", dc->u.s_builtin.type->name);
511: return;
512: case DEMANGLE_COMPONENT_OPERATOR:
513: printf ("operator %s\n", dc->u.s_operator.op->name);
514: return;
515: case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
516: printf ("extended operator with %d args\n",
517: dc->u.s_extended_operator.args);
518: d_dump (dc->u.s_extended_operator.name, indent + 2);
519: return;
520:
521: case DEMANGLE_COMPONENT_QUAL_NAME:
522: printf ("qualified name\n");
523: break;
524: case DEMANGLE_COMPONENT_LOCAL_NAME:
525: printf ("local name\n");
526: break;
527: case DEMANGLE_COMPONENT_TYPED_NAME:
528: printf ("typed name\n");
529: break;
530: case DEMANGLE_COMPONENT_TEMPLATE:
531: printf ("template\n");
532: break;
533: case DEMANGLE_COMPONENT_VTABLE:
534: printf ("vtable\n");
535: break;
536: case DEMANGLE_COMPONENT_VTT:
537: printf ("VTT\n");
538: break;
539: case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
540: printf ("construction vtable\n");