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 "libiberty.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif /* __cplusplus */
29
30/* Options passed to cplus_demangle (in 2nd parameter). */
31
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
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)
49
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)
52
53/* Enumeration of possible demangling styles.
54
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?) */
60
61extern enum demangling_styles
62{
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;
75
76/* Define string names for the various demangling styles. */
77
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"
88
89/* Some macros to test what demangling style is active. */
90
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)
101
102/* Provide information about the available demangle styles. This code is
103   pulled from gdb into libiberty because it is useful to binutils also.  */
104
105extern const struct demangler_engine
106{
107  const char *const demangling_style_name;
108  const enum demangling_styles demangling_style;
109  const char *const demangling_style_doc;
110} libiberty_demanglers[];
111
112extern char *
113cplus_demangle PARAMS ((const char *mangled, int options));
114
115extern int
116cplus_demangle_opname PARAMS ((const char *opname, char *result, int options));
117
118extern const char *
119cplus_mangle_opname PARAMS ((const char *opname, int options));
120
121/* Note: This sets global state.  FIXME if you care about multi-threading. */
122
123extern void
124set_cplus_marker_for_demangling PARAMS ((int ch));
125
126extern enum demangling_styles
127cplus_demangle_set_style PARAMS ((enum demangling_styles style));
128
129extern enum demangling_styles
130cplus_demangle_name_to_style PARAMS ((const char *name));
131
132/* V3 ABI demangling entry points, defined in cp-demangle.c.  */
133extern char*
134cplus_demangle_v3 PARAMS ((const char* mangled, int options));
135
136extern char*
137java_demangle_v3 PARAMS ((const char* mangled));
138
139
140enum gnu_v3_ctor_kinds {
141  gnu_v3_complete_object_ctor = 1,
142  gnu_v3_base_object_ctor,
143  gnu_v3_complete_object_allocating_ctor
144};
145
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.  */
150extern enum gnu_v3_ctor_kinds
151	is_gnu_v3_mangled_ctor PARAMS ((const char *name));
152
153
154enum gnu_v3_dtor_kinds {
155  gnu_v3_deleting_dtor = 1,
156  gnu_v3_complete_object_dtor,
157  gnu_v3_base_object_dtor
158};
159
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.  */
164extern enum gnu_v3_dtor_kinds
165	is_gnu_v3_mangled_dtor PARAMS ((const char *name));
166
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.  */
175
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).  */
180
181enum demangle_component_type
182{
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 standard substitution.  This holds the name of the
243     substitution.  */
244  DEMANGLE_COMPONENT_SUB_STD,
245  /* The restrict qualifier.  The one subtree is the type which is
246     being qualified.  */
247  DEMANGLE_COMPONENT_RESTRICT,
248  /* The volatile qualifier.  The one subtree is the type which is
249     being qualified.  */
250  DEMANGLE_COMPONENT_VOLATILE,
251  /* The const qualifier.  The one subtree is the type which is being
252     qualified.  */
253  DEMANGLE_COMPONENT_CONST,
254  /* The restrict qualifier modifying a member function.  The one
255     subtree is the type which is being qualified.  */
256  DEMANGLE_COMPONENT_RESTRICT_THIS,
257  /* The volatile qualifier modifying a member function.  The one
258     subtree is the type which is being qualified.  */
259  DEMANGLE_COMPONENT_VOLATILE_THIS,
260  /* The const qualifier modifying a member function.  The one subtree
261     is the type which is being qualified.  */
262  DEMANGLE_COMPONENT_CONST_THIS,
263  /* A vendor qualifier.  The left subtree is the type which is being
264     qualified, and the right subtree is the name of the
265     qualifier.  */
266  DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
267  /* A pointer.  The one subtree is the type which is being pointed
268     to.  */
269  DEMANGLE_COMPONENT_POINTER,
270  /* A reference.  The one subtree is the type which is being
271     referenced.  */
272  DEMANGLE_COMPONENT_REFERENCE,
273  /* A complex type.  The one subtree is the base type.  */
274  DEMANGLE_COMPONENT_COMPLEX,
275  /* An imaginary type.  The one subtree is the base type.  */
276  DEMANGLE_COMPONENT_IMAGINARY,
277  /* A builtin type.  This holds the builtin type information.  */
278  DEMANGLE_COMPONENT_BUILTIN_TYPE,
279  /* A vendor's builtin type.  This holds the name of the type.  */
280  DEMANGLE_COMPONENT_VENDOR_TYPE,
281  /* A function type.  The left subtree is the return type.  The right
282     subtree is a list of ARGLIST nodes.  Either or both may be
283     NULL.  */
284  DEMANGLE_COMPONENT_FUNCTION_TYPE,
285  /* An array type.  The left subtree is the dimension, which may be
286     NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
287     expression.  The right subtree is the element type.  */
288  DEMANGLE_COMPONENT_ARRAY_TYPE,
289  /* A pointer to member type.  The left subtree is the class type,
290     and the right subtree is the member type.  CV-qualifiers appear
291     on the latter.  */
292  DEMANGLE_COMPONENT_PTRMEM_TYPE,
293  /* An argument list.  The left subtree is the current argument, and
294     the right subtree is either NULL or another ARGLIST node.  */
295  DEMANGLE_COMPONENT_ARGLIST,
296  /* A template argument list.  The left subtree is the current
297     template argument, and the right subtree is either NULL or
298     another TEMPLATE_ARGLIST node.  */
299  DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
300  /* An operator.  This holds information about a standard
301     operator.  */
302  DEMANGLE_COMPONENT_OPERATOR,
303  /* An extended operator.  This holds the number of arguments, and
304     the name of the extended operator.  */
305  DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
306  /* A typecast, represented as a unary operator.  The one subtree is
307     the type to which the argument should be cast.  */
308  DEMANGLE_COMPONENT_CAST,
309  /* A unary expression.  The left subtree is the operator, and the
310     right subtree is the single argument.  */
311  DEMANGLE_COMPONENT_UNARY,
312  /* A binary expression.  The left subtree is the operator, and the
313     right subtree is a BINARY_ARGS.  */
314  DEMANGLE_COMPONENT_BINARY,
315  /* Arguments to a binary expression.  The left subtree is the first
316     argument, and the right subtree is the second argument.  */
317  DEMANGLE_COMPONENT_BINARY_ARGS,
318  /* A trinary expression.  The left subtree is the operator, and the
319     right subtree is a TRINARY_ARG1.  */
320  DEMANGLE_COMPONENT_TRINARY,
321  /* Arguments to a trinary expression.  The left subtree is the first
322     argument, and the right subtree is a TRINARY_ARG2.  */
323  DEMANGLE_COMPONENT_TRINARY_ARG1,
324  /* More arguments to a trinary expression.  The left subtree is the
325     second argument, and the right subtree is the third argument.  */
326  DEMANGLE_COMPONENT_TRINARY_ARG2,
327  /* A literal.  The left subtree is the type, and the right subtree
328     is the value, represented as a DEMANGLE_COMPONENT_NAME.  */
329  DEMANGLE_COMPONENT_LITERAL,
330  /* A negative literal.  Like LITERAL, but the value is negated.
331     This is a minor hack: the NAME used for LITERAL points directly
332     to the mangled string, but since negative numbers are mangled
333     using 'n' instead of '-', we want a way to indicate a negative
334     number which involves neither modifying the mangled string nor
335     allocating a new copy of the literal in memory.  */
336  DEMANGLE_COMPONENT_LITERAL_NEG
337};
338
339/* Types which are only used internally.  */
340
341struct demangle_operator_info;
342struct demangle_builtin_type_info;
343
344/* A node in the tree representation is an instance of a struct
345   demangle_component.  Note that the field names of the struct are
346   not well protected against macros defined by the file including
347   this one.  We can fix this if it ever becomes a problem.  */
348
349struct demangle_component
350{
351  /* The type of this component.  */
352  enum demangle_component_type type;
353
354  union
355  {
356    /* For DEMANGLE_COMPONENT_NAME.  */
357    struct
358    {
359      /* A pointer to the name (which need not NULL terminated) and
360	 its length.  */
361      const char *s;
362      int len;
363    } s_name;
364
365    /* For DEMANGLE_COMPONENT_OPERATOR.  */
366    struct
367    {
368      /* Operator.  */
369      const struct demangle_operator_info *op;
370    } s_operator;
371
372    /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
373    struct
374    {
375      /* Number of arguments.  */
376      int args;
377      /* Name.  */
378      struct demangle_component *name;
379    } s_extended_operator;
380
381    /* For DEMANGLE_COMPONENT_CTOR.  */
382    struct
383    {
384      /* Kind of constructor.  */
385      enum gnu_v3_ctor_kinds kind;
386      /* Name.  */
387      struct demangle_component *name;
388    } s_ctor;
389
390    /* For DEMANGLE_COMPONENT_DTOR.  */
391    struct
392    {
393      /* Kind of destructor.  */
394      enum gnu_v3_dtor_kinds kind;
395      /* Name.  */
396      struct demangle_component *name;
397    } s_dtor;
398
399    /* For DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
400    struct
401    {
402      /* Builtin type.  */
403      const struct demangle_builtin_type_info *type;
404    } s_builtin;
405
406    /* For DEMANGLE_COMPONENT_SUB_STD.  */
407    struct
408    {
409      /* Standard substitution string.  */
410      const char* string;
411      /* Length of string.  */
412      int len;
413    } s_string;
414
415    /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM.  */
416    struct
417    {
418      /* Template parameter index.  */
419      long number;
420    } s_number;
421
422    /* For other types.  */
423    struct
424    {
425      /* Left (or only) subtree.  */
426      struct demangle_component *left;
427      /* Right subtree.  */
428      struct demangle_component *right;
429    } s_binary;
430
431  } u;
432};
433
434/* People building mangled trees are expected to allocate instances of
435   struct demangle_component themselves.  They can then call one of
436   the following functions to fill them in.  */
437
438/* Fill in most component types with a left subtree and a right
439   subtree.  Returns non-zero on success, zero on failure, such as an
440   unrecognized or inappropriate component type.  */
441
442extern int
443cplus_demangle_fill_component PARAMS ((struct demangle_component *fill,
444				       enum demangle_component_type,
445				       struct demangle_component *left,
446				       struct demangle_component *right));
447
448/* Fill in a DEMANGLE_COMPONENT_NAME.  Returns non-zero on success,
449   zero for bad arguments.  */
450
451extern int
452cplus_demangle_fill_name PARAMS ((struct demangle_component *fill,
453				  const char *, int));
454
455/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
456   builtin type (e.g., "int", etc.).  Returns non-zero on success,
457   zero if the type is not recognized.  */
458
459extern int
460cplus_demangle_fill_builtin_type PARAMS ((struct demangle_component *fill,
461					  const char *typename));
462
463/* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
464   operator and the number of arguments which it takes (the latter is
465   used to disambiguate operators which can be both binary and unary,
466   such as '-').  Returns non-zero on success, zero if the operator is
467   not recognized.  */
468
469extern int
470cplus_demangle_fill_operator PARAMS ((struct demangle_component *fill,
471				      const char *opname, int args));
472
473/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
474   number of arguments and the name.  Returns non-zero on success,
475   zero for bad arguments.  */
476
477extern int
478cplus_demangle_fill_extended_operator PARAMS ((struct demangle_component *fill,
479					       int numargs,
480					       struct demangle_component *nm));
481
482/* Fill in a DEMANGLE_COMPONENT_CTOR.  Returns non-zero on success,
483   zero for bad arguments.  */
484
485extern int
486cplus_demangle_fill_ctor PARAMS ((struct demangle_component *fill,
487				  enum gnu_v3_ctor_kinds kind,
488				  struct demangle_component *name));
489
490/* Fill in a DEMANGLE_COMPONENT_DTOR.  Returns non-zero on success,
491   zero for bad arguments.  */
492
493extern int
494cplus_demangle_fill_dtor PARAMS ((struct demangle_component *fill,
495				  enum gnu_v3_dtor_kinds kind,
496				  struct demangle_component *name));
497
498/* This function translates a mangled name into a struct
499   demangle_component tree.  The first argument is the mangled name.
500   The second argument is DMGL_* options.  This returns a pointer to a
501   tree on success, or NULL on failure.  On success, the third
502   argument is set to a block of memory allocated by malloc.  This
503   block should be passed to free when the tree is no longer
504   needed.  */
505
506extern struct demangle_component *
507cplus_demangle_v3_components PARAMS ((const char *mangled,
508				      int options,
509				      void **mem));
510
511/* This function takes a struct demangle_component tree and returns
512   the corresponding demangled string.  The first argument is DMGL_*
513   options.  The second is the tree to demangle.  The third is a guess
514   at the length of the demangled string, used to initially allocate
515   the return buffer.  The fourth is a pointer to a size_t.  On
516   success, this function returns a buffer allocated by malloc(), and
517   sets the size_t pointed to by the fourth argument to the size of
518   the allocated buffer (not the length of the returned string).  On
519   failure, this function returns NULL, and sets the size_t pointed to
520   by the fourth argument to 0 for an invalid tree, or to 1 for a
521   memory allocation error.  */
522
523extern char *
524cplus_demangle_print PARAMS ((int options,
525			      const struct demangle_component *tree,
526			      int estimated_length,
527			      size_t *p_allocated_size));
528
529#ifdef __cplusplus
530}
531#endif /* __cplusplus */
532
533#endif	/* DEMANGLE_H */
534