2007-03-15 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / include / demangle.h
blob360538cf397c2f71c6be24086540d72671f59e8a
1 /* Defs for interface to demanglers.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007 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. */
38 #define DMGL_RET_POSTFIX (1 << 5) /* Print function return types (when
39 present) after function signature */
41 #define DMGL_AUTO (1 << 8)
42 #define DMGL_GNU (1 << 9)
43 #define DMGL_LUCID (1 << 10)
44 #define DMGL_ARM (1 << 11)
45 #define DMGL_HP (1 << 12) /* For the HP aCC compiler;
46 same as ARM except for
47 template arguments, etc. */
48 #define DMGL_EDG (1 << 13)
49 #define DMGL_GNU_V3 (1 << 14)
50 #define DMGL_GNAT (1 << 15)
52 /* If none of these are set, use 'current_demangling_style' as the default. */
53 #define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT)
55 /* Enumeration of possible demangling styles.
57 Lucid and ARM styles are still kept logically distinct, even though
58 they now both behave identically. The resulting style is actual the
59 union of both. I.E. either style recognizes both "__pt__" and "__rf__"
60 for operator "->", even though the first is lucid style and the second
61 is ARM style. (FIXME?) */
63 extern enum demangling_styles
65 no_demangling = -1,
66 unknown_demangling = 0,
67 auto_demangling = DMGL_AUTO,
68 gnu_demangling = DMGL_GNU,
69 lucid_demangling = DMGL_LUCID,
70 arm_demangling = DMGL_ARM,
71 hp_demangling = DMGL_HP,
72 edg_demangling = DMGL_EDG,
73 gnu_v3_demangling = DMGL_GNU_V3,
74 java_demangling = DMGL_JAVA,
75 gnat_demangling = DMGL_GNAT
76 } current_demangling_style;
78 /* Define string names for the various demangling styles. */
80 #define NO_DEMANGLING_STYLE_STRING "none"
81 #define AUTO_DEMANGLING_STYLE_STRING "auto"
82 #define GNU_DEMANGLING_STYLE_STRING "gnu"
83 #define LUCID_DEMANGLING_STYLE_STRING "lucid"
84 #define ARM_DEMANGLING_STYLE_STRING "arm"
85 #define HP_DEMANGLING_STYLE_STRING "hp"
86 #define EDG_DEMANGLING_STYLE_STRING "edg"
87 #define GNU_V3_DEMANGLING_STYLE_STRING "gnu-v3"
88 #define JAVA_DEMANGLING_STYLE_STRING "java"
89 #define GNAT_DEMANGLING_STYLE_STRING "gnat"
91 /* Some macros to test what demangling style is active. */
93 #define CURRENT_DEMANGLING_STYLE current_demangling_style
94 #define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
95 #define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
96 #define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
97 #define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
98 #define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
99 #define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
100 #define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
101 #define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
102 #define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
104 /* Provide information about the available demangle styles. This code is
105 pulled from gdb into libiberty because it is useful to binutils also. */
107 extern const struct demangler_engine
109 const char *const demangling_style_name;
110 const enum demangling_styles demangling_style;
111 const char *const demangling_style_doc;
112 } libiberty_demanglers[];
114 extern char *
115 cplus_demangle (const char *mangled, int options);
117 extern int
118 cplus_demangle_opname (const char *opname, char *result, int options);
120 extern const char *
121 cplus_mangle_opname (const char *opname, int options);
123 /* Note: This sets global state. FIXME if you care about multi-threading. */
125 extern void
126 set_cplus_marker_for_demangling (int ch);
128 extern enum demangling_styles
129 cplus_demangle_set_style (enum demangling_styles style);
131 extern enum demangling_styles
132 cplus_demangle_name_to_style (const char *name);
134 /* Callback typedef for allocation-less demangler interfaces. */
135 typedef void (*demangle_callbackref) (const char *, size_t, void *);
137 /* V3 ABI demangling entry points, defined in cp-demangle.c. Callback
138 variants return non-zero on success, zero on error. char* variants
139 return a string allocated by malloc on success, NULL on error. */
140 extern int
141 cplus_demangle_v3_callback (const char *mangled, int options,
142 demangle_callbackref callback, void *opaque);
144 extern char*
145 cplus_demangle_v3 (const char *mangled, int options);
147 extern int
148 java_demangle_v3_callback (const char *mangled,
149 demangle_callbackref callback, void *opaque);
151 extern char*
152 java_demangle_v3 (const char *mangled);
154 enum gnu_v3_ctor_kinds {
155 gnu_v3_complete_object_ctor = 1,
156 gnu_v3_base_object_ctor,
157 gnu_v3_complete_object_allocating_ctor
160 /* Return non-zero iff NAME is the mangled form of a constructor name
161 in the G++ V3 ABI demangling style. Specifically, return an `enum
162 gnu_v3_ctor_kinds' value indicating what kind of constructor
163 it is. */
164 extern enum gnu_v3_ctor_kinds
165 is_gnu_v3_mangled_ctor (const char *name);
168 enum gnu_v3_dtor_kinds {
169 gnu_v3_deleting_dtor = 1,
170 gnu_v3_complete_object_dtor,
171 gnu_v3_base_object_dtor
174 /* Return non-zero iff NAME is the mangled form of a destructor name
175 in the G++ V3 ABI demangling style. Specifically, return an `enum
176 gnu_v3_dtor_kinds' value, indicating what kind of destructor
177 it is. */
178 extern enum gnu_v3_dtor_kinds
179 is_gnu_v3_mangled_dtor (const char *name);
181 /* The V3 demangler works in two passes. The first pass builds a tree
182 representation of the mangled name, and the second pass turns the
183 tree representation into a demangled string. Here we define an
184 interface to permit a caller to build their own tree
185 representation, which they can pass to the demangler to get a
186 demangled string. This can be used to canonicalize user input into
187 something which the demangler might output. It could also be used
188 by other demanglers in the future. */
190 /* These are the component types which may be found in the tree. Many
191 component types have one or two subtrees, referred to as left and
192 right (a component type with only one subtree puts it in the left
193 subtree). */
195 enum demangle_component_type
197 /* A name, with a length and a pointer to a string. */
198 DEMANGLE_COMPONENT_NAME,
199 /* A qualified name. The left subtree is a class or namespace or
200 some such thing, and the right subtree is a name qualified by
201 that class. */
202 DEMANGLE_COMPONENT_QUAL_NAME,
203 /* A local name. The left subtree describes a function, and the
204 right subtree is a name which is local to that function. */
205 DEMANGLE_COMPONENT_LOCAL_NAME,
206 /* A typed name. The left subtree is a name, and the right subtree
207 describes that name as a function. */
208 DEMANGLE_COMPONENT_TYPED_NAME,
209 /* A template. The left subtree is a template name, and the right
210 subtree is a template argument list. */
211 DEMANGLE_COMPONENT_TEMPLATE,
212 /* A template parameter. This holds a number, which is the template
213 parameter index. */
214 DEMANGLE_COMPONENT_TEMPLATE_PARAM,
215 /* A constructor. This holds a name and the kind of
216 constructor. */
217 DEMANGLE_COMPONENT_CTOR,
218 /* A destructor. This holds a name and the kind of destructor. */
219 DEMANGLE_COMPONENT_DTOR,
220 /* A vtable. This has one subtree, the type for which this is a
221 vtable. */
222 DEMANGLE_COMPONENT_VTABLE,
223 /* A VTT structure. This has one subtree, the type for which this
224 is a VTT. */
225 DEMANGLE_COMPONENT_VTT,
226 /* A construction vtable. The left subtree is the type for which
227 this is a vtable, and the right subtree is the derived type for
228 which this vtable is built. */
229 DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
230 /* A typeinfo structure. This has one subtree, the type for which
231 this is the tpeinfo structure. */
232 DEMANGLE_COMPONENT_TYPEINFO,
233 /* A typeinfo name. This has one subtree, the type for which this
234 is the typeinfo name. */
235 DEMANGLE_COMPONENT_TYPEINFO_NAME,
236 /* A typeinfo function. This has one subtree, the type for which
237 this is the tpyeinfo function. */
238 DEMANGLE_COMPONENT_TYPEINFO_FN,
239 /* A thunk. This has one subtree, the name for which this is a
240 thunk. */
241 DEMANGLE_COMPONENT_THUNK,
242 /* A virtual thunk. This has one subtree, the name for which this
243 is a virtual thunk. */
244 DEMANGLE_COMPONENT_VIRTUAL_THUNK,
245 /* A covariant thunk. This has one subtree, the name for which this
246 is a covariant thunk. */
247 DEMANGLE_COMPONENT_COVARIANT_THUNK,
248 /* A Java class. This has one subtree, the type. */
249 DEMANGLE_COMPONENT_JAVA_CLASS,
250 /* A guard variable. This has one subtree, the name for which this
251 is a guard variable. */
252 DEMANGLE_COMPONENT_GUARD,
253 /* A reference temporary. This has one subtree, the name for which
254 this is a temporary. */
255 DEMANGLE_COMPONENT_REFTEMP,
256 /* A hidden alias. This has one subtree, the encoding for which it
257 is providing alternative linkage. */
258 DEMANGLE_COMPONENT_HIDDEN_ALIAS,
259 /* A standard substitution. This holds the name of the
260 substitution. */
261 DEMANGLE_COMPONENT_SUB_STD,
262 /* The restrict qualifier. The one subtree is the type which is
263 being qualified. */
264 DEMANGLE_COMPONENT_RESTRICT,
265 /* The volatile qualifier. The one subtree is the type which is
266 being qualified. */
267 DEMANGLE_COMPONENT_VOLATILE,
268 /* The const qualifier. The one subtree is the type which is being
269 qualified. */
270 DEMANGLE_COMPONENT_CONST,
271 /* The restrict qualifier modifying a member function. The one
272 subtree is the type which is being qualified. */
273 DEMANGLE_COMPONENT_RESTRICT_THIS,
274 /* The volatile qualifier modifying a member function. The one
275 subtree is the type which is being qualified. */
276 DEMANGLE_COMPONENT_VOLATILE_THIS,
277 /* The const qualifier modifying a member function. The one subtree
278 is the type which is being qualified. */
279 DEMANGLE_COMPONENT_CONST_THIS,
280 /* A vendor qualifier. The left subtree is the type which is being
281 qualified, and the right subtree is the name of the
282 qualifier. */
283 DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
284 /* A pointer. The one subtree is the type which is being pointed
285 to. */
286 DEMANGLE_COMPONENT_POINTER,
287 /* A reference. The one subtree is the type which is being
288 referenced. */
289 DEMANGLE_COMPONENT_REFERENCE,
290 /* A complex type. The one subtree is the base type. */
291 DEMANGLE_COMPONENT_COMPLEX,
292 /* An imaginary type. The one subtree is the base type. */
293 DEMANGLE_COMPONENT_IMAGINARY,
294 /* A builtin type. This holds the builtin type information. */
295 DEMANGLE_COMPONENT_BUILTIN_TYPE,
296 /* A vendor's builtin type. This holds the name of the type. */
297 DEMANGLE_COMPONENT_VENDOR_TYPE,
298 /* A function type. The left subtree is the return type. The right
299 subtree is a list of ARGLIST nodes. Either or both may be
300 NULL. */
301 DEMANGLE_COMPONENT_FUNCTION_TYPE,
302 /* An array type. The left subtree is the dimension, which may be
303 NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
304 expression. The right subtree is the element type. */
305 DEMANGLE_COMPONENT_ARRAY_TYPE,
306 /* A pointer to member type. The left subtree is the class type,
307 and the right subtree is the member type. CV-qualifiers appear
308 on the latter. */
309 DEMANGLE_COMPONENT_PTRMEM_TYPE,
310 /* An argument list. The left subtree is the current argument, and
311 the right subtree is either NULL or another ARGLIST node. */
312 DEMANGLE_COMPONENT_ARGLIST,
313 /* A template argument list. The left subtree is the current
314 template argument, and the right subtree is either NULL or
315 another TEMPLATE_ARGLIST node. */
316 DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
317 /* An operator. This holds information about a standard
318 operator. */
319 DEMANGLE_COMPONENT_OPERATOR,
320 /* An extended operator. This holds the number of arguments, and
321 the name of the extended operator. */
322 DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
323 /* A typecast, represented as a unary operator. The one subtree is
324 the type to which the argument should be cast. */
325 DEMANGLE_COMPONENT_CAST,
326 /* A unary expression. The left subtree is the operator, and the
327 right subtree is the single argument. */
328 DEMANGLE_COMPONENT_UNARY,
329 /* A binary expression. The left subtree is the operator, and the
330 right subtree is a BINARY_ARGS. */
331 DEMANGLE_COMPONENT_BINARY,
332 /* Arguments to a binary expression. The left subtree is the first
333 argument, and the right subtree is the second argument. */
334 DEMANGLE_COMPONENT_BINARY_ARGS,
335 /* A trinary expression. The left subtree is the operator, and the
336 right subtree is a TRINARY_ARG1. */
337 DEMANGLE_COMPONENT_TRINARY,
338 /* Arguments to a trinary expression. The left subtree is the first
339 argument, and the right subtree is a TRINARY_ARG2. */
340 DEMANGLE_COMPONENT_TRINARY_ARG1,
341 /* More arguments to a trinary expression. The left subtree is the
342 second argument, and the right subtree is the third argument. */
343 DEMANGLE_COMPONENT_TRINARY_ARG2,
344 /* A literal. The left subtree is the type, and the right subtree
345 is the value, represented as a DEMANGLE_COMPONENT_NAME. */
346 DEMANGLE_COMPONENT_LITERAL,
347 /* A negative literal. Like LITERAL, but the value is negated.
348 This is a minor hack: the NAME used for LITERAL points directly
349 to the mangled string, but since negative numbers are mangled
350 using 'n' instead of '-', we want a way to indicate a negative
351 number which involves neither modifying the mangled string nor
352 allocating a new copy of the literal in memory. */
353 DEMANGLE_COMPONENT_LITERAL_NEG
356 /* Types which are only used internally. */
358 struct demangle_operator_info;
359 struct demangle_builtin_type_info;
361 /* A node in the tree representation is an instance of a struct
362 demangle_component. Note that the field names of the struct are
363 not well protected against macros defined by the file including
364 this one. We can fix this if it ever becomes a problem. */
366 struct demangle_component
368 /* The type of this component. */
369 enum demangle_component_type type;
371 union
373 /* For DEMANGLE_COMPONENT_NAME. */
374 struct
376 /* A pointer to the name (which need not NULL terminated) and
377 its length. */
378 const char *s;
379 int len;
380 } s_name;
382 /* For DEMANGLE_COMPONENT_OPERATOR. */
383 struct
385 /* Operator. */
386 const struct demangle_operator_info *op;
387 } s_operator;
389 /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
390 struct
392 /* Number of arguments. */
393 int args;
394 /* Name. */
395 struct demangle_component *name;
396 } s_extended_operator;
398 /* For DEMANGLE_COMPONENT_CTOR. */
399 struct
401 /* Kind of constructor. */
402 enum gnu_v3_ctor_kinds kind;
403 /* Name. */
404 struct demangle_component *name;
405 } s_ctor;
407 /* For DEMANGLE_COMPONENT_DTOR. */
408 struct
410 /* Kind of destructor. */
411 enum gnu_v3_dtor_kinds kind;
412 /* Name. */
413 struct demangle_component *name;
414 } s_dtor;
416 /* For DEMANGLE_COMPONENT_BUILTIN_TYPE. */
417 struct
419 /* Builtin type. */
420 const struct demangle_builtin_type_info *type;
421 } s_builtin;
423 /* For DEMANGLE_COMPONENT_SUB_STD. */
424 struct
426 /* Standard substitution string. */
427 const char* string;
428 /* Length of string. */
429 int len;
430 } s_string;
432 /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM. */
433 struct
435 /* Template parameter index. */
436 long number;
437 } s_number;
439 /* For other types. */
440 struct
442 /* Left (or only) subtree. */
443 struct demangle_component *left;
444 /* Right subtree. */
445 struct demangle_component *right;
446 } s_binary;
448 } u;
451 /* People building mangled trees are expected to allocate instances of
452 struct demangle_component themselves. They can then call one of
453 the following functions to fill them in. */
455 /* Fill in most component types with a left subtree and a right
456 subtree. Returns non-zero on success, zero on failure, such as an
457 unrecognized or inappropriate component type. */
459 extern int
460 cplus_demangle_fill_component (struct demangle_component *fill,
461 enum demangle_component_type,
462 struct demangle_component *left,
463 struct demangle_component *right);
465 /* Fill in a DEMANGLE_COMPONENT_NAME. Returns non-zero on success,
466 zero for bad arguments. */
468 extern int
469 cplus_demangle_fill_name (struct demangle_component *fill,
470 const char *, int);
472 /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
473 builtin type (e.g., "int", etc.). Returns non-zero on success,
474 zero if the type is not recognized. */
476 extern int
477 cplus_demangle_fill_builtin_type (struct demangle_component *fill,
478 const char *type_name);
480 /* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
481 operator and the number of arguments which it takes (the latter is
482 used to disambiguate operators which can be both binary and unary,
483 such as '-'). Returns non-zero on success, zero if the operator is
484 not recognized. */
486 extern int
487 cplus_demangle_fill_operator (struct demangle_component *fill,
488 const char *opname, int args);
490 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
491 number of arguments and the name. Returns non-zero on success,
492 zero for bad arguments. */
494 extern int
495 cplus_demangle_fill_extended_operator (struct demangle_component *fill,
496 int numargs,
497 struct demangle_component *nm);
499 /* Fill in a DEMANGLE_COMPONENT_CTOR. Returns non-zero on success,
500 zero for bad arguments. */
502 extern int
503 cplus_demangle_fill_ctor (struct demangle_component *fill,
504 enum gnu_v3_ctor_kinds kind,
505 struct demangle_component *name);
507 /* Fill in a DEMANGLE_COMPONENT_DTOR. Returns non-zero on success,
508 zero for bad arguments. */
510 extern int
511 cplus_demangle_fill_dtor (struct demangle_component *fill,
512 enum gnu_v3_dtor_kinds kind,
513 struct demangle_component *name);
515 /* This function translates a mangled name into a struct
516 demangle_component tree. The first argument is the mangled name.
517 The second argument is DMGL_* options. This returns a pointer to a
518 tree on success, or NULL on failure. On success, the third
519 argument is set to a block of memory allocated by malloc. This
520 block should be passed to free when the tree is no longer
521 needed. */
523 extern struct demangle_component *
524 cplus_demangle_v3_components (const char *mangled, int options, void **mem);
526 /* This function takes a struct demangle_component tree and returns
527 the corresponding demangled string. The first argument is DMGL_*
528 options. The second is the tree to demangle. The third is a guess
529 at the length of the demangled string, used to initially allocate
530 the return buffer. The fourth is a pointer to a size_t. On
531 success, this function returns a buffer allocated by malloc(), and
532 sets the size_t pointed to by the fourth argument to the size of
533 the allocated buffer (not the length of the returned string). On
534 failure, this function returns NULL, and sets the size_t pointed to
535 by the fourth argument to 0 for an invalid tree, or to 1 for a
536 memory allocation error. */
538 extern char *
539 cplus_demangle_print (int options,
540 const struct demangle_component *tree,
541 int estimated_length,
542 size_t *p_allocated_size);
544 /* This function takes a struct demangle_component tree and passes back
545 a demangled string in one or more calls to a callback function.
546 The first argument is DMGL_* options. The second is the tree to
547 demangle. The third is a pointer to a callback function; on each call
548 this receives an element of the demangled string, its length, and an
549 opaque value. The fourth is the opaque value passed to the callback.
550 The callback is called once or more to return the full demangled
551 string. The demangled element string is always nul-terminated, though
552 its length is also provided for convenience. In contrast to
553 cplus_demangle_print(), this function does not allocate heap memory
554 to grow output strings (except perhaps where alloca() is implemented
555 by malloc()), and so is normally safe for use where the heap has been
556 corrupted. On success, this function returns 1; on failure, 0. */
558 extern int
559 cplus_demangle_print_callback (int options,
560 const struct demangle_component *tree,
561 demangle_callbackref callback, void *opaque);
563 #ifdef __cplusplus
565 #endif /* __cplusplus */
567 #endif /* DEMANGLE_H */