1/* Defs for interface to demanglers.
2   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
3   2003, 2004, 2005, 2007, 2008, 2009, 2010 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					   It applies only to the toplevel
50					   function type.  */
51#define DMGL_RET_DROP	 (1 << 6)       /* Suppress printing function return
52					   types, even if present.  It applies
53					   only to the toplevel function type.
54					   */
55
56#define DMGL_AUTO	 (1 << 8)
57#define DMGL_GNU	 (1 << 9)
58#define DMGL_LUCID	 (1 << 10)
59#define DMGL_ARM	 (1 << 11)
60#define DMGL_HP 	 (1 << 12)       /* For the HP aCC compiler;
61                                            same as ARM except for
62                                            template arguments, etc. */
63#define DMGL_EDG	 (1 << 13)
64#define DMGL_GNU_V3	 (1 << 14)
65#define DMGL_GNAT	 (1 << 15)
66#define DMGL_DLANG	 (1 << 16)
67
68/* If none of these are set, use 'current_demangling_style' as the default. */
69#define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT|DMGL_DLANG)
70
71/* Enumeration of possible demangling styles.
72
73   Lucid and ARM styles are still kept logically distinct, even though
74   they now both behave identically.  The resulting style is actual the
75   union of both.  I.E. either style recognizes both "__pt__" and "__rf__"
76   for operator "->", even though the first is lucid style and the second
77   is ARM style. (FIXME?) */
78
79extern enum demangling_styles
80{
81  no_demangling = -1,
82  unknown_demangling = 0,
83  auto_demangling = DMGL_AUTO,
84  gnu_demangling = DMGL_GNU,
85  lucid_demangling = DMGL_LUCID,
86  arm_demangling = DMGL_ARM,
87  hp_demangling = DMGL_HP,
88  edg_demangling = DMGL_EDG,
89  gnu_v3_demangling = DMGL_GNU_V3,
90  java_demangling = DMGL_JAVA,
91  gnat_demangling = DMGL_GNAT,
92  dlang_demangling = DMGL_DLANG
93} current_demangling_style;
94
95/* Define string names for the various demangling styles. */
96
97#define NO_DEMANGLING_STYLE_STRING            "none"
98#define AUTO_DEMANGLING_STYLE_STRING	      "auto"
99#define GNU_DEMANGLING_STYLE_STRING    	      "gnu"
100#define LUCID_DEMANGLING_STYLE_STRING	      "lucid"
101#define ARM_DEMANGLING_STYLE_STRING	      "arm"
102#define HP_DEMANGLING_STYLE_STRING	      "hp"
103#define EDG_DEMANGLING_STYLE_STRING	      "edg"
104#define GNU_V3_DEMANGLING_STYLE_STRING        "gnu-v3"
105#define JAVA_DEMANGLING_STYLE_STRING          "java"
106#define GNAT_DEMANGLING_STYLE_STRING          "gnat"
107#define DLANG_DEMANGLING_STYLE_STRING         "dlang"
108
109/* Some macros to test what demangling style is active. */
110
111#define CURRENT_DEMANGLING_STYLE current_demangling_style
112#define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
113#define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
114#define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
115#define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
116#define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
117#define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
118#define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
119#define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
120#define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
121#define DLANG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_DLANG)
122
123/* Provide information about the available demangle styles. This code is
124   pulled from gdb into libiberty because it is useful to binutils also.  */
125
126extern const struct demangler_engine
127{
128  const char *const demangling_style_name;
129  const enum demangling_styles demangling_style;
130  const char *const demangling_style_doc;
131} libiberty_demanglers[];
132
133extern char *
134cplus_demangle (const char *mangled, int options);
135
136extern int
137cplus_demangle_opname (const char *opname, char *result, int options);
138
139extern const char *
140cplus_mangle_opname (const char *opname, int options);
141
142/* Note: This sets global state.  FIXME if you care about multi-threading. */
143
144extern void
145set_cplus_marker_for_demangling (int ch);
146
147extern enum demangling_styles
148cplus_demangle_set_style (enum demangling_styles style);
149
150extern enum demangling_styles
151cplus_demangle_name_to_style (const char *name);
152
153/* Callback typedef for allocation-less demangler interfaces. */
154typedef void (*demangle_callbackref) (const char *, size_t, void *);
155
156/* V3 ABI demangling entry points, defined in cp-demangle.c.  Callback
157   variants return non-zero on success, zero on error.  char* variants
158   return a string allocated by malloc on success, NULL on error.  */
159extern int
160cplus_demangle_v3_callback (const char *mangled, int options,
161                            demangle_callbackref callback, void *opaque);
162
163extern char*
164cplus_demangle_v3 (const char *mangled, int options);
165
166extern int
167java_demangle_v3_callback (const char *mangled,
168                           demangle_callbackref callback, void *opaque);
169
170extern char*
171java_demangle_v3 (const char *mangled);
172
173char *
174ada_demangle (const char *mangled, int options);
175
176extern char *
177dlang_demangle (const char *mangled, int options);
178
179enum gnu_v3_ctor_kinds {
180  gnu_v3_complete_object_ctor = 1,
181  gnu_v3_base_object_ctor,
182  gnu_v3_complete_object_allocating_ctor,
183  /* These are not part of the V3 ABI.  Unified constructors are generated
184     as a speed-for-space optimization when the -fdeclone-ctor-dtor option
185     is used, and are always internal symbols.  */
186  gnu_v3_unified_ctor,
187  gnu_v3_object_ctor_group
188};
189
190/* Return non-zero iff NAME is the mangled form of a constructor name
191   in the G++ V3 ABI demangling style.  Specifically, return an `enum
192   gnu_v3_ctor_kinds' value indicating what kind of constructor
193   it is.  */
194extern enum gnu_v3_ctor_kinds
195	is_gnu_v3_mangled_ctor (const char *name);
196
197
198enum gnu_v3_dtor_kinds {
199  gnu_v3_deleting_dtor = 1,
200  gnu_v3_complete_object_dtor,
201  gnu_v3_base_object_dtor,
202  /* These are not part of the V3 ABI.  Unified destructors are generated
203     as a speed-for-space optimization when the -fdeclone-ctor-dtor option
204     is used, and are always internal symbols.  */
205  gnu_v3_unified_dtor,
206  gnu_v3_object_dtor_group
207};
208
209/* Return non-zero iff NAME is the mangled form of a destructor name
210   in the G++ V3 ABI demangling style.  Specifically, return an `enum
211   gnu_v3_dtor_kinds' value, indicating what kind of destructor
212   it is.  */
213extern enum gnu_v3_dtor_kinds
214	is_gnu_v3_mangled_dtor (const char *name);
215
216/* The V3 demangler works in two passes.  The first pass builds a tree
217   representation of the mangled name, and the second pass turns the
218   tree representation into a demangled string.  Here we define an
219   interface to permit a caller to build their own tree
220   representation, which they can pass to the demangler to get a
221   demangled string.  This can be used to canonicalize user input into
222   something which the demangler might output.  It could also be used
223   by other demanglers in the future.  */
224
225/* These are the component types which may be found in the tree.  Many
226   component types have one or two subtrees, referred to as left and
227   right (a component type with only one subtree puts it in the left
228   subtree).  */
229
230enum demangle_component_type
231{
232  /* A name, with a length and a pointer to a string.  */
233  DEMANGLE_COMPONENT_NAME,
234  /* A qualified name.  The left subtree is a class or namespace or
235     some such thing, and the right subtree is a name qualified by
236     that class.  */
237  DEMANGLE_COMPONENT_QUAL_NAME,
238  /* A local name.  The left subtree describes a function, and the
239     right subtree is a name which is local to that function.  */
240  DEMANGLE_COMPONENT_LOCAL_NAME,
241  /* A typed name.  The left subtree is a name, and the right subtree
242     describes that name as a function.  */
243  DEMANGLE_COMPONENT_TYPED_NAME,
244  /* A template.  The left subtree is a template name, and the right
245     subtree is a template argument list.  */
246  DEMANGLE_COMPONENT_TEMPLATE,
247  /* A template parameter.  This holds a number, which is the template
248     parameter index.  */
249  DEMANGLE_COMPONENT_TEMPLATE_PARAM,
250  /* A function parameter.  This holds a number, which is the index.  */
251  DEMANGLE_COMPONENT_FUNCTION_PARAM,
252  /* A constructor.  This holds a name and the kind of
253     constructor.  */
254  DEMANGLE_COMPONENT_CTOR,
255  /* A destructor.  This holds a name and the kind of destructor.  */
256  DEMANGLE_COMPONENT_DTOR,
257  /* A vtable.  This has one subtree, the type for which this is a
258     vtable.  */
259  DEMANGLE_COMPONENT_VTABLE,
260  /* A VTT structure.  This has one subtree, the type for which this
261     is a VTT.  */
262  DEMANGLE_COMPONENT_VTT,
263  /* A construction vtable.  The left subtree is the type for which
264     this is a vtable, and the right subtree is the derived type for
265     which this vtable is built.  */
266  DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
267  /* A typeinfo structure.  This has one subtree, the type for which
268     this is the tpeinfo structure.  */
269  DEMANGLE_COMPONENT_TYPEINFO,
270  /* A typeinfo name.  This has one subtree, the type for which this
271     is the typeinfo name.  */
272  DEMANGLE_COMPONENT_TYPEINFO_NAME,
273  /* A typeinfo function.  This has one subtree, the type for which
274     this is the tpyeinfo function.  */
275  DEMANGLE_COMPONENT_TYPEINFO_FN,
276  /* A thunk.  This has one subtree, the name for which this is a
277     thunk.  */
278  DEMANGLE_COMPONENT_THUNK,
279  /* A virtual thunk.  This has one subtree, the name for which this
280     is a virtual thunk.  */
281  DEMANGLE_COMPONENT_VIRTUAL_THUNK,
282  /* A covariant thunk.  This has one subtree, the name for which this
283     is a covariant thunk.  */
284  DEMANGLE_COMPONENT_COVARIANT_THUNK,
285  /* A Java class.  This has one subtree, the type.  */
286  DEMANGLE_COMPONENT_JAVA_CLASS,
287  /* A guard variable.  This has one subtree, the name for which this
288     is a guard variable.  */
289  DEMANGLE_COMPONENT_GUARD,
290  /* The init and wrapper functions for C++11 thread_local variables.  */
291  DEMANGLE_COMPONENT_TLS_INIT,
292  DEMANGLE_COMPONENT_TLS_WRAPPER,
293  /* A reference temporary.  This has one subtree, the name for which
294     this is a temporary.  */
295  DEMANGLE_COMPONENT_REFTEMP,
296  /* A hidden alias.  This has one subtree, the encoding for which it
297     is providing alternative linkage.  */
298  DEMANGLE_COMPONENT_HIDDEN_ALIAS,
299  /* A standard substitution.  This holds the name of the
300     substitution.  */
301  DEMANGLE_COMPONENT_SUB_STD,
302  /* The restrict qualifier.  The one subtree is the type which is
303     being qualified.  */
304  DEMANGLE_COMPONENT_RESTRICT,
305  /* The volatile qualifier.  The one subtree is the type which is
306     being qualified.  */
307  DEMANGLE_COMPONENT_VOLATILE,
308  /* The const qualifier.  The one subtree is the type which is being
309     qualified.  */
310  DEMANGLE_COMPONENT_CONST,
311  /* The restrict qualifier modifying a member function.  The one
312     subtree is the type which is being qualified.  */
313  DEMANGLE_COMPONENT_RESTRICT_THIS,
314  /* The volatile qualifier modifying a member function.  The one
315     subtree is the type which is being qualified.  */
316  DEMANGLE_COMPONENT_VOLATILE_THIS,
317  /* The const qualifier modifying a member function.  The one subtree
318     is the type which is being qualified.  */
319  DEMANGLE_COMPONENT_CONST_THIS,
320  /* C++11 A reference modifying a member function.  The one subtree is the
321     type which is being referenced.  */
322  DEMANGLE_COMPONENT_REFERENCE_THIS,
323  /* C++11: An rvalue reference modifying a member function.  The one
324     subtree is the type which is being referenced.  */
325  DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS,
326  /* A vendor qualifier.  The left subtree is the type which is being
327     qualified, and the right subtree is the name of the
328     qualifier.  */
329  DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
330  /* A pointer.  The one subtree is the type which is being pointed
331     to.  */
332  DEMANGLE_COMPONENT_POINTER,
333  /* A reference.  The one subtree is the type which is being
334     referenced.  */
335  DEMANGLE_COMPONENT_REFERENCE,
336  /* C++0x: An rvalue reference.  The one subtree is the type which is
337     being referenced.  */
338  DEMANGLE_COMPONENT_RVALUE_REFERENCE,
339  /* A complex type.  The one subtree is the base type.  */
340  DEMANGLE_COMPONENT_COMPLEX,
341  /* An imaginary type.  The one subtree is the base type.  */
342  DEMANGLE_COMPONENT_IMAGINARY,
343  /* A builtin type.  This holds the builtin type information.  */
344  DEMANGLE_COMPONENT_BUILTIN_TYPE,
345  /* A vendor's builtin type.  This holds the name of the type.  */
346  DEMANGLE_COMPONENT_VENDOR_TYPE,
347  /* A function type.  The left subtree is the return type.  The right
348     subtree is a list of ARGLIST nodes.  Either or both may be
349     NULL.  */
350  DEMANGLE_COMPONENT_FUNCTION_TYPE,
351  /* An array type.  The left subtree is the dimension, which may be
352     NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
353     expression.  The right subtree is the element type.  */
354  DEMANGLE_COMPONENT_ARRAY_TYPE,
355  /* A pointer to member type.  The left subtree is the class type,
356     and the right subtree is the member type.  CV-qualifiers appear
357     on the latter.  */
358  DEMANGLE_COMPONENT_PTRMEM_TYPE,
359  /* A fixed-point type.  */
360  DEMANGLE_COMPONENT_FIXED_TYPE,
361  /* A vector type.  The left subtree is the number of elements,
362     the right subtree is the element type.  */
363  DEMANGLE_COMPONENT_VECTOR_TYPE,
364  /* An argument list.  The left subtree is the current argument, and
365     the right subtree is either NULL or another ARGLIST node.  */
366  DEMANGLE_COMPONENT_ARGLIST,
367  /* A template argument list.  The left subtree is the current
368     template argument, and the right subtree is either NULL or
369     another TEMPLATE_ARGLIST node.  */
370  DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
371  /* An initializer list.  The left subtree is either an explicit type or
372     NULL, and the right subtree is a DEMANGLE_COMPONENT_ARGLIST.  */
373  DEMANGLE_COMPONENT_INITIALIZER_LIST,
374  /* An operator.  This holds information about a standard
375     operator.  */
376  DEMANGLE_COMPONENT_OPERATOR,
377  /* An extended operator.  This holds the number of arguments, and
378     the name of the extended operator.  */
379  DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
380  /* A typecast, represented as a unary operator.  The one subtree is
381     the type to which the argument should be cast.  */
382  DEMANGLE_COMPONENT_CAST,
383  /* A conversion operator, represented as a unary operator.  The one
384     subtree is the type to which the argument should be converted
385     to.  */
386  DEMANGLE_COMPONENT_CONVERSION,
387  /* A nullary expression.  The left subtree is the operator.  */
388  DEMANGLE_COMPONENT_NULLARY,
389  /* A unary expression.  The left subtree is the operator, and the
390     right subtree is the single argument.  */
391  DEMANGLE_COMPONENT_UNARY,
392  /* A binary expression.  The left subtree is the operator, and the
393     right subtree is a BINARY_ARGS.  */
394  DEMANGLE_COMPONENT_BINARY,
395  /* Arguments to a binary expression.  The left subtree is the first
396     argument, and the right subtree is the second argument.  */
397  DEMANGLE_COMPONENT_BINARY_ARGS,
398  /* A trinary expression.  The left subtree is the operator, and the
399     right subtree is a TRINARY_ARG1.  */
400  DEMANGLE_COMPONENT_TRINARY,
401  /* Arguments to a trinary expression.  The left subtree is the first
402     argument, and the right subtree is a TRINARY_ARG2.  */
403  DEMANGLE_COMPONENT_TRINARY_ARG1,
404  /* More arguments to a trinary expression.  The left subtree is the
405     second argument, and the right subtree is the third argument.  */
406  DEMANGLE_COMPONENT_TRINARY_ARG2,
407  /* A literal.  The left subtree is the type, and the right subtree
408     is the value, represented as a DEMANGLE_COMPONENT_NAME.  */
409  DEMANGLE_COMPONENT_LITERAL,
410  /* A negative literal.  Like LITERAL, but the value is negated.
411     This is a minor hack: the NAME used for LITERAL points directly
412     to the mangled string, but since negative numbers are mangled
413     using 'n' instead of '-', we want a way to indicate a negative
414     number which involves neither modifying the mangled string nor
415     allocating a new copy of the literal in memory.  */
416  DEMANGLE_COMPONENT_LITERAL_NEG,
417  /* A libgcj compiled resource.  The left subtree is the name of the
418     resource.  */
419  DEMANGLE_COMPONENT_JAVA_RESOURCE,
420  /* A name formed by the concatenation of two parts.  The left
421     subtree is the first part and the right subtree the second.  */
422  DEMANGLE_COMPONENT_COMPOUND_NAME,
423  /* A name formed by a single character.  */
424  DEMANGLE_COMPONENT_CHARACTER,
425  /* A number.  */
426  DEMANGLE_COMPONENT_NUMBER,
427  /* A decltype type.  */
428  DEMANGLE_COMPONENT_DECLTYPE,
429  /* Global constructors keyed to name.  */
430  DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS,
431  /* Global destructors keyed to name.  */
432  DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS,
433  /* A lambda closure type.  */
434  DEMANGLE_COMPONENT_LAMBDA,
435  /* A default argument scope.  */
436  DEMANGLE_COMPONENT_DEFAULT_ARG,
437  /* An unnamed type.  */
438  DEMANGLE_COMPONENT_UNNAMED_TYPE,
439  /* A transactional clone.  This has one subtree, the encoding for
440     which it is providing alternative linkage.  */
441  DEMANGLE_COMPONENT_TRANSACTION_CLONE,
442  /* A non-transactional clone entry point.  In the i386/x86_64 abi,
443     the unmangled symbol of a tm_callable becomes a thunk and the
444     non-transactional function version is mangled thus.  */
445  DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
446  /* A pack expansion.  */
447  DEMANGLE_COMPONENT_PACK_EXPANSION,
448  /* A name with an ABI tag.  */
449  DEMANGLE_COMPONENT_TAGGED_NAME,
450  /* A cloned function.  */
451  DEMANGLE_COMPONENT_CLONE
452};
453
454/* Types which are only used internally.  */
455
456struct demangle_operator_info;
457struct demangle_builtin_type_info;
458
459/* A node in the tree representation is an instance of a struct
460   demangle_component.  Note that the field names of the struct are
461   not well protected against macros defined by the file including
462   this one.  We can fix this if it ever becomes a problem.  */
463
464struct demangle_component
465{
466  /* The type of this component.  */
467  enum demangle_component_type type;
468
469  union
470  {
471    /* For DEMANGLE_COMPONENT_NAME.  */
472    struct
473    {
474      /* A pointer to the name (which need not NULL terminated) and
475	 its length.  */
476      const char *s;
477      int len;
478    } s_name;
479
480    /* For DEMANGLE_COMPONENT_OPERATOR.  */
481    struct
482    {
483      /* Operator.  */
484      const struct demangle_operator_info *op;
485    } s_operator;
486
487    /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
488    struct
489    {
490      /* Number of arguments.  */
491      int args;
492      /* Name.  */
493      struct demangle_component *name;
494    } s_extended_operator;
495
496    /* For DEMANGLE_COMPONENT_FIXED_TYPE.  */
497    struct
498    {
499      /* The length, indicated by a C integer type name.  */
500      struct demangle_component *length;
501      /* _Accum or _Fract?  */
502      short accum;
503      /* Saturating or not?  */
504      short sat;
505    } s_fixed;
506
507    /* For DEMANGLE_COMPONENT_CTOR.  */
508    struct
509    {
510      /* Kind of constructor.  */
511      enum gnu_v3_ctor_kinds kind;
512      /* Name.  */
513      struct demangle_component *name;
514    } s_ctor;
515
516    /* For DEMANGLE_COMPONENT_DTOR.  */
517    struct
518    {
519      /* Kind of destructor.  */
520      enum gnu_v3_dtor_kinds kind;
521      /* Name.  */
522      struct demangle_component *name;
523    } s_dtor;
524
525    /* For DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
526    struct
527    {
528      /* Builtin type.  */
529      const struct demangle_builtin_type_info *type;
530    } s_builtin;
531
532    /* For DEMANGLE_COMPONENT_SUB_STD.  */
533    struct
534    {
535      /* Standard substitution string.  */
536      const char* string;
537      /* Length of string.  */
538      int len;
539    } s_string;
540
541    /* For DEMANGLE_COMPONENT_*_PARAM.  */
542    struct
543    {
544      /* Parameter index.  */
545      long number;
546    } s_number;
547
548    /* For DEMANGLE_COMPONENT_CHARACTER.  */
549    struct
550    {
551      int character;
552    } s_character;
553
554    /* For other types.  */
555    struct
556    {
557      /* Left (or only) subtree.  */
558      struct demangle_component *left;
559      /* Right subtree.  */
560      struct demangle_component *right;
561    } s_binary;
562
563    struct
564    {
565      /* subtree, same place as d_left.  */
566      struct demangle_component *sub;
567      /* integer.  */
568      int num;
569    } s_unary_num;
570
571  } u;
572};
573
574/* People building mangled trees are expected to allocate instances of
575   struct demangle_component themselves.  They can then call one of
576   the following functions to fill them in.  */
577
578/* Fill in most component types with a left subtree and a right
579   subtree.  Returns non-zero on success, zero on failure, such as an
580   unrecognized or inappropriate component type.  */
581
582extern int
583cplus_demangle_fill_component (struct demangle_component *fill,
584                               enum demangle_component_type,
585                               struct demangle_component *left,
586                               struct demangle_component *right);
587
588/* Fill in a DEMANGLE_COMPONENT_NAME.  Returns non-zero on success,
589   zero for bad arguments.  */
590
591extern int
592cplus_demangle_fill_name (struct demangle_component *fill,
593                          const char *, int);
594
595/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
596   builtin type (e.g., "int", etc.).  Returns non-zero on success,
597   zero if the type is not recognized.  */
598
599extern int
600cplus_demangle_fill_builtin_type (struct demangle_component *fill,
601                                  const char *type_name);
602
603/* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
604   operator and the number of arguments which it takes (the latter is
605   used to disambiguate operators which can be both binary and unary,
606   such as '-').  Returns non-zero on success, zero if the operator is
607   not recognized.  */
608
609extern int
610cplus_demangle_fill_operator (struct demangle_component *fill,
611                              const char *opname, int args);
612
613/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
614   number of arguments and the name.  Returns non-zero on success,
615   zero for bad arguments.  */
616
617extern int
618cplus_demangle_fill_extended_operator (struct demangle_component *fill,
619                                       int numargs,
620                                       struct demangle_component *nm);
621
622/* Fill in a DEMANGLE_COMPONENT_CTOR.  Returns non-zero on success,
623   zero for bad arguments.  */
624
625extern int
626cplus_demangle_fill_ctor (struct demangle_component *fill,
627                          enum gnu_v3_ctor_kinds kind,
628                          struct demangle_component *name);
629
630/* Fill in a DEMANGLE_COMPONENT_DTOR.  Returns non-zero on success,
631   zero for bad arguments.  */
632
633extern int
634cplus_demangle_fill_dtor (struct demangle_component *fill,
635                          enum gnu_v3_dtor_kinds kind,
636                          struct demangle_component *name);
637
638/* This function translates a mangled name into a struct
639   demangle_component tree.  The first argument is the mangled name.
640   The second argument is DMGL_* options.  This returns a pointer to a
641   tree on success, or NULL on failure.  On success, the third
642   argument is set to a block of memory allocated by malloc.  This
643   block should be passed to free when the tree is no longer
644   needed.  */
645
646extern struct demangle_component *
647cplus_demangle_v3_components (const char *mangled, int options, void **mem);
648
649/* This function takes a struct demangle_component tree and returns
650   the corresponding demangled string.  The first argument is DMGL_*
651   options.  The second is the tree to demangle.  The third is a guess
652   at the length of the demangled string, used to initially allocate
653   the return buffer.  The fourth is a pointer to a size_t.  On
654   success, this function returns a buffer allocated by malloc(), and
655   sets the size_t pointed to by the fourth argument to the size of
656   the allocated buffer (not the length of the returned string).  On
657   failure, this function returns NULL, and sets the size_t pointed to
658   by the fourth argument to 0 for an invalid tree, or to 1 for a
659   memory allocation error.  */
660
661extern char *
662cplus_demangle_print (int options,
663                      const struct demangle_component *tree,
664                      int estimated_length,
665                      size_t *p_allocated_size);
666
667/* This function takes a struct demangle_component tree and passes back
668   a demangled string in one or more calls to a callback function.
669   The first argument is DMGL_* options.  The second is the tree to
670   demangle.  The third is a pointer to a callback function; on each call
671   this receives an element of the demangled string, its length, and an
672   opaque value.  The fourth is the opaque value passed to the callback.
673   The callback is called once or more to return the full demangled
674   string.  The demangled element string is always nul-terminated, though
675   its length is also provided for convenience.  In contrast to
676   cplus_demangle_print(), this function does not allocate heap memory
677   to grow output strings (except perhaps where alloca() is implemented
678   by malloc()), and so is normally safe for use where the heap has been
679   corrupted.  On success, this function returns 1; on failure, 0.  */
680
681extern int
682cplus_demangle_print_callback (int options,
683                               const struct demangle_component *tree,
684                               demangle_callbackref callback, void *opaque);
685
686#ifdef __cplusplus
687}
688#endif /* __cplusplus */
689
690#endif	/* DEMANGLE_H */
691