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., 51 Franklin Street - Fifth Floor,
18   Boston, MA 02110-1301, 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#define DMGL_RET_POSTFIX (1 << 5)       /* Print function return types (when
39                                           present) after function signature */
40
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)
51
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)
54
55/* Enumeration of possible demangling styles.
56
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?) */
62
63extern enum demangling_styles
64{
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;
77
78/* Define string names for the various demangling styles. */
79
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"
90
91/* Some macros to test what demangling style is active. */
92
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)
103
104/* Provide information about the available demangle styles. This code is
105   pulled from gdb into libiberty because it is useful to binutils also.  */
106
107extern const struct demangler_engine
108{
109  const char *const demangling_style_name;
110  const enum demangling_styles demangling_style;
111  const char *const demangling_style_doc;
112} libiberty_demanglers[];
113
114extern char *
115cplus_demangle (const char *mangled, int options);
116
117extern int
118cplus_demangle_opname (const char *opname, char *result, int options);
119
120extern const char *
121cplus_mangle_opname (const char *opname, int options);
122
123/* Note: This sets global state.  FIXME if you care about multi-threading. */
124
125extern void
126set_cplus_marker_for_demangling (int ch);
127
128extern enum demangling_styles
129cplus_demangle_set_style (enum demangling_styles style);
130
131extern enum demangling_styles
132cplus_demangle_name_to_style (const char *name);
133
134/* V3 ABI demangling entry points, defined in cp-demangle.c.  */
135extern char*
136cplus_demangle_v3 (const char* mangled, int options);
137
138extern char*
139java_demangle_v3 (const char* mangled);
140
141
142enum gnu_v3_ctor_kinds {
143  gnu_v3_complete_object_ctor = 1,
144  gnu_v3_base_object_ctor,
145  gnu_v3_complete_object_allocating_ctor
146};
147
148/* Return non-zero iff NAME is the mangled form of a constructor name
149   in the G++ V3 ABI demangling style.  Specifically, return an `enum
150   gnu_v3_ctor_kinds' value indicating what kind of constructor
151   it is.  */
152extern enum gnu_v3_ctor_kinds
153	is_gnu_v3_mangled_ctor (const char *name);
154
155
156enum gnu_v3_dtor_kinds {
157  gnu_v3_deleting_dtor = 1,
158  gnu_v3_complete_object_dtor,
159  gnu_v3_base_object_dtor
160};
161
162/* Return non-zero iff NAME is the mangled form of a destructor name
163   in the G++ V3 ABI demangling style.  Specifically, return an `enum
164   gnu_v3_dtor_kinds' value, indicating what kind of destructor
165   it is.  */
166extern enum gnu_v3_dtor_kinds
167	is_gnu_v3_mangled_dtor (const char *name);
168
169/* The V3 demangler works in two passes.  The first pass builds a tree
170   representation of the mangled name, and the second pass turns the
171   tree representation into a demangled string.  Here we define an
172   interface to permit a caller to build their own tree
173   representation, which they can pass to the demangler to get a
174   demangled string.  This can be used to canonicalize user input into
175   something which the demangler might output.  It could also be used
176   by other demanglers in the future.  */
177
178/* These are the component types which may be found in the tree.  Many
179   component types have one or two subtrees, referred to as left and
180   right (a component type with only one subtree puts it in the left
181   subtree).  */
182
183enum demangle_component_type
184{
185  /* A name, with a length and a pointer to a string.  */
186  DEMANGLE_COMPONENT_NAME,
187  /* A qualified name.  The left subtree is a class or namespace or
188     some such thing, and the right subtree is a name qualified by
189     that class.  */
190  DEMANGLE_COMPONENT_QUAL_NAME,
191  /* A local name.  The left subtree describes a function, and the
192     right subtree is a name which is local to that function.  */
193  DEMANGLE_COMPONENT_LOCAL_NAME,
194  /* A typed name.  The left subtree is a name, and the right subtree
195     describes that name as a function.  */
196  DEMANGLE_COMPONENT_TYPED_NAME,
197  /* A template.  The left subtree is a template name, and the right
198     subtree is a template argument list.  */
199  DEMANGLE_COMPONENT_TEMPLATE,
200  /* A template parameter.  This holds a number, which is the template
201     parameter index.  */
202  DEMANGLE_COMPONENT_TEMPLATE_PARAM,
203  /* A constructor.  This holds a name and the kind of
204     constructor.  */
205  DEMANGLE_COMPONENT_CTOR,
206  /* A destructor.  This holds a name and the kind of destructor.  */
207  DEMANGLE_COMPONENT_DTOR,
208  /* A vtable.  This has one subtree, the type for which this is a
209     vtable.  */
210  DEMANGLE_COMPONENT_VTABLE,
211  /* A VTT structure.  This has one subtree, the type for which this
212     is a VTT.  */
213  DEMANGLE_COMPONENT_VTT,
214  /* A construction vtable.  The left subtree is the type for which
215     this is a vtable, and the right subtree is the derived type for
216     which this vtable is built.  */
217  DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
218  /* A typeinfo structure.  This has one subtree, the type for which
219     this is the tpeinfo structure.  */
220  DEMANGLE_COMPONENT_TYPEINFO,
221  /* A typeinfo name.  This has one subtree, the type for which this
222     is the typeinfo name.  */
223  DEMANGLE_COMPONENT_TYPEINFO_NAME,
224  /* A typeinfo function.  This has one subtree, the type for which
225     this is the tpyeinfo function.  */
226  DEMANGLE_COMPONENT_TYPEINFO_FN,
227  /* A thunk.  This has one subtree, the name for which this is a
228     thunk.  */
229  DEMANGLE_COMPONENT_THUNK,
230  /* A virtual thunk.  This has one subtree, the name for which this
231     is a virtual thunk.  */
232  DEMANGLE_COMPONENT_VIRTUAL_THUNK,
233  /* A covariant thunk.  This has one subtree, the name for which this
234     is a covariant thunk.  */
235  DEMANGLE_COMPONENT_COVARIANT_THUNK,
236  /* A Java class.  This has one subtree, the type.  */
237  DEMANGLE_COMPONENT_JAVA_CLASS,
238  /* A guard variable.  This has one subtree, the name for which this
239     is a guard variable.  */
240  DEMANGLE_COMPONENT_GUARD,
241  /* A reference temporary.  This has one subtree, the name for which
242     this is a temporary.  */
243  DEMANGLE_COMPONENT_REFTEMP,
244  /* A hidden alias.  This has one subtree, the encoding for which it
245     is providing alternative linkage.  */
246  DEMANGLE_COMPONENT_HIDDEN_ALIAS,
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
346struct demangle_operator_info;
347struct 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
354struct 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
447extern int
448cplus_demangle_fill_component (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
456extern int
457cplus_demangle_fill_name (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
464extern int
465cplus_demangle_fill_builtin_type (struct demangle_component *fill,
466                                  const char *type_name);
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
474extern int
475cplus_demangle_fill_operator (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
482extern int
483cplus_demangle_fill_extended_operator (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
490extern int
491cplus_demangle_fill_ctor (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
498extern int
499cplus_demangle_fill_dtor (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
511extern struct demangle_component *
512cplus_demangle_v3_components (const char *mangled, int options, void **mem);
513
514/* This function takes a struct demangle_component tree and returns
515   the corresponding demangled string.  The first argument is DMGL_*
516   options.  The second is the tree to demangle.  The third is a guess
517   at the length of the demangled string, used to initially allocate
518   the return buffer.  The fourth is a pointer to a size_t.  On
519   success, this function returns a buffer allocated by malloc(), and
520   sets the size_t pointed to by the fourth argument to the size of
521   the allocated buffer (not the length of the returned string).  On
522   failure, this function returns NULL, and sets the size_t pointed to
523   by the fourth argument to 0 for an invalid tree, or to 1 for a
524   memory allocation error.  */
525
526extern char *
527cplus_demangle_print (int options,
528                      const struct demangle_component *tree,
529                      int estimated_length,
530                      size_t *p_allocated_size);
531
532#ifdef __cplusplus
533}
534#endif /* __cplusplus */
535
536#endif	/* DEMANGLE_H */
537