2005-08-12 Andreas Krebbel <krebbel1@de.ibm.com>
[official-gcc.git] / include / demangle.h
blob304a4c4e472561b8c5c83274b284f2180ae6bc81
1 /* Defs for interface to demanglers.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
3 2003, 2004 Free Software Foundation, Inc.
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.
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.
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., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
21 #if !defined (DEMANGLE_H)
22 #define DEMANGLE_H
24 #include "libiberty.h"
26 #ifdef __cplusplus
27 extern "C" {
28 #endif /* __cplusplus */
30 /* Options passed to cplus_demangle (in 2nd parameter). */
32 #define DMGL_NO_OPTS 0 /* For readability... */
33 #define DMGL_PARAMS (1 << 0) /* Include function args */
34 #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
35 #define DMGL_JAVA (1 << 2) /* Demangle as Java rather than C++. */
36 #define DMGL_VERBOSE (1 << 3) /* Include implementation details. */
37 #define DMGL_TYPES (1 << 4) /* Also try to demangle type encodings. */
39 #define DMGL_AUTO (1 << 8)
40 #define DMGL_GNU (1 << 9)
41 #define DMGL_LUCID (1 << 10)
42 #define DMGL_ARM (1 << 11)
43 #define DMGL_HP (1 << 12) /* For the HP aCC compiler;
44 same as ARM except for
45 template arguments, etc. */
46 #define DMGL_EDG (1 << 13)
47 #define DMGL_GNU_V3 (1 << 14)
48 #define DMGL_GNAT (1 << 15)
50 /* If none of these are set, use 'current_demangling_style' as the default. */
51 #define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT)
53 /* Enumeration of possible demangling styles.
55 Lucid and ARM styles are still kept logically distinct, even though
56 they now both behave identically. The resulting style is actual the
57 union of both. I.E. either style recognizes both "__pt__" and "__rf__"
58 for operator "->", even though the first is lucid style and the second
59 is ARM style. (FIXME?) */
61 extern enum demangling_styles
63 no_demangling = -1,
64 unknown_demangling = 0,
65 auto_demangling = DMGL_AUTO,
66 gnu_demangling = DMGL_GNU,
67 lucid_demangling = DMGL_LUCID,
68 arm_demangling = DMGL_ARM,
69 hp_demangling = DMGL_HP,
70 edg_demangling = DMGL_EDG,
71 gnu_v3_demangling = DMGL_GNU_V3,
72 java_demangling = DMGL_JAVA,
73 gnat_demangling = DMGL_GNAT
74 } current_demangling_style;
76 /* Define string names for the various demangling styles. */
78 #define NO_DEMANGLING_STYLE_STRING "none"
79 #define AUTO_DEMANGLING_STYLE_STRING "auto"
80 #define GNU_DEMANGLING_STYLE_STRING "gnu"
81 #define LUCID_DEMANGLING_STYLE_STRING "lucid"
82 #define ARM_DEMANGLING_STYLE_STRING "arm"
83 #define HP_DEMANGLING_STYLE_STRING "hp"
84 #define EDG_DEMANGLING_STYLE_STRING "edg"
85 #define GNU_V3_DEMANGLING_STYLE_STRING "gnu-v3"
86 #define JAVA_DEMANGLING_STYLE_STRING "java"
87 #define GNAT_DEMANGLING_STYLE_STRING "gnat"
89 /* Some macros to test what demangling style is active. */
91 #define CURRENT_DEMANGLING_STYLE current_demangling_style
92 #define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
93 #define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
94 #define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
95 #define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
96 #define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
97 #define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
98 #define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
99 #define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
100 #define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
102 /* Provide information about the available demangle styles. This code is
103 pulled from gdb into libiberty because it is useful to binutils also. */
105 extern const struct demangler_engine
107 const char *const demangling_style_name;
108 const enum demangling_styles demangling_style;
109 const char *const demangling_style_doc;
110 } libiberty_demanglers[];
112 extern char *
113 cplus_demangle (const char *mangled, int options);
115 extern int
116 cplus_demangle_opname (const char *opname, char *result, int options);
118 extern const char *
119 cplus_mangle_opname (const char *opname, int options);
121 /* Note: This sets global state. FIXME if you care about multi-threading. */
123 extern void
124 set_cplus_marker_for_demangling (int ch);
126 extern enum demangling_styles
127 cplus_demangle_set_style (enum demangling_styles style);
129 extern enum demangling_styles
130 cplus_demangle_name_to_style (const char *name);
132 /* V3 ABI demangling entry points, defined in cp-demangle.c. */
133 extern char*
134 cplus_demangle_v3 (const char* mangled, int options);
136 extern char*
137 java_demangle_v3 (const char* mangled);
140 enum gnu_v3_ctor_kinds {
141 gnu_v3_complete_object_ctor = 1,
142 gnu_v3_base_object_ctor,
143 gnu_v3_complete_object_allocating_ctor
146 /* Return non-zero iff NAME is the mangled form of a constructor name
147 in the G++ V3 ABI demangling style. Specifically, return an `enum
148 gnu_v3_ctor_kinds' value indicating what kind of constructor
149 it is. */
150 extern enum gnu_v3_ctor_kinds
151 is_gnu_v3_mangled_ctor (const char *name);
154 enum gnu_v3_dtor_kinds {
155 gnu_v3_deleting_dtor = 1,
156 gnu_v3_complete_object_dtor,
157 gnu_v3_base_object_dtor
160 /* Return non-zero iff NAME is the mangled form of a destructor name
161 in the G++ V3 ABI demangling style. Specifically, return an `enum
162 gnu_v3_dtor_kinds' value, indicating what kind of destructor
163 it is. */
164 extern enum gnu_v3_dtor_kinds
165 is_gnu_v3_mangled_dtor (const char *name);
167 /* The V3 demangler works in two passes. The first pass builds a tree
168 representation of the mangled name, and the second pass turns the
169 tree representation into a demangled string. Here we define an
170 interface to permit a caller to build their own tree
171 representation, which they can pass to the demangler to get a
172 demangled string. This can be used to canonicalize user input into
173 something which the demangler might output. It could also be used
174 by other demanglers in the future. */
176 /* These are the component types which may be found in the tree. Many
177 component types have one or two subtrees, referred to as left and
178 right (a component type with only one subtree puts it in the left
179 subtree). */
181 enum demangle_component_type
183 /* A name, with a length and a pointer to a string. */
184 DEMANGLE_COMPONENT_NAME,
185 /* A qualified name. The left subtree is a class or namespace or
186 some such thing, and the right subtree is a name qualified by
187 that class. */
188 DEMANGLE_COMPONENT_QUAL_NAME,
189 /* A local name. The left subtree describes a function, and the
190 right subtree is a name which is local to that function. */
191 DEMANGLE_COMPONENT_LOCAL_NAME,
192 /* A typed name. The left subtree is a name, and the right subtree
193 describes that name as a function. */
194 DEMANGLE_COMPONENT_TYPED_NAME,
195 /* A template. The left subtree is a template name, and the right
196 subtree is a template argument list. */
197 DEMANGLE_COMPONENT_TEMPLATE,
198 /* A template parameter. This holds a number, which is the template
199 parameter index. */
200 DEMANGLE_COMPONENT_TEMPLATE_PARAM,
201 /* A constructor. This holds a name and the kind of
202 constructor. */
203 DEMANGLE_COMPONENT_CTOR,
204 /* A destructor. This holds a name and the kind of destructor. */
205 DEMANGLE_COMPONENT_DTOR,
206 /* A vtable. This has one subtree, the type for which this is a
207 vtable. */
208 DEMANGLE_COMPONENT_VTABLE,
209 /* A VTT structure. This has one subtree, the type for which this
210 is a VTT. */
211 DEMANGLE_COMPONENT_VTT,
212 /* A construction vtable. The left subtree is the type for which
213 this is a vtable, and the right subtree is the derived type for
214 which this vtable is built. */
215 DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
216 /* A typeinfo structure. This has one subtree, the type for which
217 this is the tpeinfo structure. */
218 DEMANGLE_COMPONENT_TYPEINFO,
219 /* A typeinfo name. This has one subtree, the type for which this
220 is the typeinfo name. */
221 DEMANGLE_COMPONENT_TYPEINFO_NAME,
222 /* A typeinfo function. This has one subtree, the type for which
223 this is the tpyeinfo function. */
224 DEMANGLE_COMPONENT_TYPEINFO_FN,
225 /* A thunk. This has one subtree, the name for which this is a
226 thunk. */
227 DEMANGLE_COMPONENT_THUNK,
228 /* A virtual thunk. This has one subtree, the name for which this
229 is a virtual thunk. */
230 DEMANGLE_COMPONENT_VIRTUAL_THUNK,
231 /* A covariant thunk. This has one subtree, the name for which this
232 is a covariant thunk. */
233 DEMANGLE_COMPONENT_COVARIANT_THUNK,
234 /* A Java class. This has one subtree, the type. */
235 DEMANGLE_COMPONENT_JAVA_CLASS,
236 /* A guard variable. This has one subtree, the name for which this
237 is a guard variable. */
238 DEMANGLE_COMPONENT_GUARD,
239 /* A reference temporary. This has one subtree, the name for which
240 this is a temporary. */
241 DEMANGLE_COMPONENT_REFTEMP,
242 /* A hidden alias. This has one subtree, the encoding for which it
243 is providing alternative linkage. */
244 DEMANGLE_COMPONENT_HIDDEN_ALIAS,
245 /* A standard substitution. This holds the name of the
246 substitution. */
247 DEMANGLE_COMPONENT_SUB_STD,
248 /* The restrict qualifier. The one subtree is the type which is
249 being qualified. */
250 DEMANGLE_COMPONENT_RESTRICT,
251 /* The volatile qualifier. The one subtree is the type which is
252 being qualified. */
253 DEMANGLE_COMPONENT_VOLATILE,
254 /* The const qualifier. The one subtree is the type which is being
255 qualified. */
256 DEMANGLE_COMPONENT_CONST,
257 /* The restrict qualifier modifying a member function. The one
258 subtree is the type which is being qualified. */
259 DEMANGLE_COMPONENT_RESTRICT_THIS,
260 /* The volatile qualifier modifying a member function. The one
261 subtree is the type which is being qualified. */
262 DEMANGLE_COMPONENT_VOLATILE_THIS,
263 /* The const qualifier modifying a member function. The one subtree
264 is the type which is being qualified. */
265 DEMANGLE_COMPONENT_CONST_THIS,
266 /* A vendor qualifier. The left subtree is the type which is being
267 qualified, and the right subtree is the name of the
268 qualifier. */
269 DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
270 /* A pointer. The one subtree is the type which is being pointed
271 to. */
272 DEMANGLE_COMPONENT_POINTER,
273 /* A reference. The one subtree is the type which is being
274 referenced. */
275 DEMANGLE_COMPONENT_REFERENCE,
276 /* A complex type. The one subtree is the base type. */
277 DEMANGLE_COMPONENT_COMPLEX,
278 /* An imaginary type. The one subtree is the base type. */
279 DEMANGLE_COMPONENT_IMAGINARY,
280 /* A builtin type. This holds the builtin type information. */
281 DEMANGLE_COMPONENT_BUILTIN_TYPE,
282 /* A vendor's builtin type. This holds the name of the type. */
283 DEMANGLE_COMPONENT_VENDOR_TYPE,
284 /* A function type. The left subtree is the return type. The right
285 subtree is a list of ARGLIST nodes. Either or both may be
286 NULL. */
287 DEMANGLE_COMPONENT_FUNCTION_TYPE,
288 /* An array type. The left subtree is the dimension, which may be
289 NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
290 expression. The right subtree is the element type. */
291 DEMANGLE_COMPONENT_ARRAY_TYPE,
292 /* A pointer to member type. The left subtree is the class type,
293 and the right subtree is the member type. CV-qualifiers appear
294 on the latter. */
295 DEMANGLE_COMPONENT_PTRMEM_TYPE,
296 /* An argument list. The left subtree is the current argument, and
297 the right subtree is either NULL or another ARGLIST node. */
298 DEMANGLE_COMPONENT_ARGLIST,
299 /* A template argument list. The left subtree is the current
300 template argument, and the right subtree is either NULL or
301 another TEMPLATE_ARGLIST node. */
302 DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
303 /* An operator. This holds information about a standard
304 operator. */
305 DEMANGLE_COMPONENT_OPERATOR,
306 /* An extended operator. This holds the number of arguments, and
307 the name of the extended operator. */
308 DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
309 /* A typecast, represented as a unary operator. The one subtree is
310 the type to which the argument should be cast. */
311 DEMANGLE_COMPONENT_CAST,
312 /* A unary expression. The left subtree is the operator, and the
313 right subtree is the single argument. */
314 DEMANGLE_COMPONENT_UNARY,
315 /* A binary expression. The left subtree is the operator, and the
316 right subtree is a BINARY_ARGS. */
317 DEMANGLE_COMPONENT_BINARY,
318 /* Arguments to a binary expression. The left subtree is the first
319 argument, and the right subtree is the second argument. */
320 DEMANGLE_COMPONENT_BINARY_ARGS,
321 /* A trinary expression. The left subtree is the operator, and the
322 right subtree is a TRINARY_ARG1. */
323 DEMANGLE_COMPONENT_TRINARY,
324 /* Arguments to a trinary expression. The left subtree is the first
325 argument, and the right subtree is a TRINARY_ARG2. */
326 DEMANGLE_COMPONENT_TRINARY_ARG1,
327 /* More arguments to a trinary expression. The left subtree is the
328 second argument, and the right subtree is the third argument. */
329 DEMANGLE_COMPONENT_TRINARY_ARG2,
330 /* A literal. The left subtree is the type, and the right subtree
331 is the value, represented as a DEMANGLE_COMPONENT_NAME. */
332 DEMANGLE_COMPONENT_LITERAL,
333 /* A negative literal. Like LITERAL, but the value is negated.
334 This is a minor hack: the NAME used for LITERAL points directly
335 to the mangled string, but since negative numbers are mangled
336 using 'n' instead of '-', we want a way to indicate a negative
337 number which involves neither modifying the mangled string nor
338 allocating a new copy of the literal in memory. */
339 DEMANGLE_COMPONENT_LITERAL_NEG
342 /* Types which are only used internally. */
344 struct demangle_operator_info;
345 struct demangle_builtin_type_info;
347 /* A node in the tree representation is an instance of a struct
348 demangle_component. Note that the field names of the struct are
349 not well protected against macros defined by the file including
350 this one. We can fix this if it ever becomes a problem. */
352 struct demangle_component
354 /* The type of this component. */
355 enum demangle_component_type type;
357 union
359 /* For DEMANGLE_COMPONENT_NAME. */
360 struct
362 /* A pointer to the name (which need not NULL terminated) and
363 its length. */
364 const char *s;
365 int len;
366 } s_name;
368 /* For DEMANGLE_COMPONENT_OPERATOR. */
369 struct
371 /* Operator. */
372 const struct demangle_operator_info *op;
373 } s_operator;
375 /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
376 struct
378 /* Number of arguments. */
379 int args;
380 /* Name. */
381 struct demangle_component *name;
382 } s_extended_operator;
384 /* For DEMANGLE_COMPONENT_CTOR. */
385 struct
387 /* Kind of constructor. */
388 enum gnu_v3_ctor_kinds kind;
389 /* Name. */
390 struct demangle_component *name;
391 } s_ctor;
393 /* For DEMANGLE_COMPONENT_DTOR. */
394 struct
396 /* Kind of destructor. */
397 enum gnu_v3_dtor_kinds kind;
398 /* Name. */
399 struct demangle_component *name;
400 } s_dtor;
402 /* For DEMANGLE_COMPONENT_BUILTIN_TYPE. */
403 struct
405 /* Builtin type. */
406 const struct demangle_builtin_type_info *type;
407 } s_builtin;
409 /* For DEMANGLE_COMPONENT_SUB_STD. */
410 struct
412 /* Standard substitution string. */
413 const char* string;
414 /* Length of string. */
415 int len;
416 } s_string;
418 /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM. */
419 struct
421 /* Template parameter index. */
422 long number;
423 } s_number;
425 /* For other types. */
426 struct
428 /* Left (or only) subtree. */
429 struct demangle_component *left;
430 /* Right subtree. */
431 struct demangle_component *right;
432 } s_binary;
434 } u;
437 /* People building mangled trees are expected to allocate instances of
438 struct demangle_component themselves. They can then call one of
439 the following functions to fill them in. */
441 /* Fill in most component types with a left subtree and a right
442 subtree. Returns non-zero on success, zero on failure, such as an
443 unrecognized or inappropriate component type. */
445 extern int
446 cplus_demangle_fill_component (struct demangle_component *fill,
447 enum demangle_component_type,
448 struct demangle_component *left,
449 struct demangle_component *right);
451 /* Fill in a DEMANGLE_COMPONENT_NAME. Returns non-zero on success,
452 zero for bad arguments. */
454 extern int
455 cplus_demangle_fill_name (struct demangle_component *fill,
456 const char *, int);
458 /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
459 builtin type (e.g., "int", etc.). Returns non-zero on success,
460 zero if the type is not recognized. */
462 extern int
463 cplus_demangle_fill_builtin_type (struct demangle_component *fill,
464 const char *type_name);
466 /* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
467 operator and the number of arguments which it takes (the latter is
468 used to disambiguate operators which can be both binary and unary,
469 such as '-'). Returns non-zero on success, zero if the operator is
470 not recognized. */
472 extern int
473 cplus_demangle_fill_operator (struct demangle_component *fill,
474 const char *opname, int args);
476 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
477 number of arguments and the name. Returns non-zero on success,
478 zero for bad arguments. */
480 extern int
481 cplus_demangle_fill_extended_operator (struct demangle_component *fill,
482 int numargs,
483 struct demangle_component *nm);
485 /* Fill in a DEMANGLE_COMPONENT_CTOR. Returns non-zero on success,
486 zero for bad arguments. */
488 extern int
489 cplus_demangle_fill_ctor (struct demangle_component *fill,
490 enum gnu_v3_ctor_kinds kind,
491 struct demangle_component *name);
493 /* Fill in a DEMANGLE_COMPONENT_DTOR. Returns non-zero on success,
494 zero for bad arguments. */
496 extern int
497 cplus_demangle_fill_dtor (struct demangle_component *fill,
498 enum gnu_v3_dtor_kinds kind,
499 struct demangle_component *name);
501 /* This function translates a mangled name into a struct
502 demangle_component tree. The first argument is the mangled name.
503 The second argument is DMGL_* options. This returns a pointer to a
504 tree on success, or NULL on failure. On success, the third
505 argument is set to a block of memory allocated by malloc. This
506 block should be passed to free when the tree is no longer
507 needed. */
509 extern struct demangle_component *
510 cplus_demangle_v3_components (const char *mangled, int options, void **mem);
512 /* This function takes a struct demangle_component tree and returns
513 the corresponding demangled string. The first argument is DMGL_*
514 options. The second is the tree to demangle. The third is a guess
515 at the length of the demangled string, used to initially allocate
516 the return buffer. The fourth is a pointer to a size_t. On
517 success, this function returns a buffer allocated by malloc(), and
518 sets the size_t pointed to by the fourth argument to the size of
519 the allocated buffer (not the length of the returned string). On
520 failure, this function returns NULL, and sets the size_t pointed to
521 by the fourth argument to 0 for an invalid tree, or to 1 for a
522 memory allocation error. */
524 extern char *
525 cplus_demangle_print (int options,
526 const struct demangle_component *tree,
527 int estimated_length,
528 size_t *p_allocated_size);
530 #ifdef __cplusplus
532 #endif /* __cplusplus */
534 #endif /* DEMANGLE_H */