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