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