133965Sjdp/* Defs for interface to demanglers.
2130561Sobrien   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
3218822Sdim   2003, 2004, 2005, 2007 Free Software Foundation, Inc.
433965Sjdp
5218822Sdim   This program is free software; you can redistribute it and/or
6218822Sdim   modify it under the terms of the GNU Library General Public License
7218822Sdim   as published by the Free Software Foundation; either version 2, or
8218822Sdim   (at your option) any later version.
933965Sjdp
10218822Sdim   In addition to the permissions in the GNU Library General Public
11218822Sdim   License, the Free Software Foundation gives you unlimited
12218822Sdim   permission to link the compiled version of this file into
13218822Sdim   combinations with other programs, and to distribute those
14218822Sdim   combinations without any restriction coming from the use of this
15218822Sdim   file.  (The Library Public License restrictions do apply in other
16218822Sdim   respects; for example, they cover modification of the file, and
17218822Sdim   distribution when not linked into a combined executable.)
1833965Sjdp
19218822Sdim   This program is distributed in the hope that it will be useful, but
20218822Sdim   WITHOUT ANY WARRANTY; without even the implied warranty of
21218822Sdim   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22218822Sdim   Library General Public License for more details.
2333965Sjdp
24218822Sdim   You should have received a copy of the GNU Library General Public
25218822Sdim   License along with this program; if not, write to the Free Software
26218822Sdim   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
27218822Sdim   02110-1301, USA.  */
2833965Sjdp
29218822Sdim
3033965Sjdp#if !defined (DEMANGLE_H)
3133965Sjdp#define DEMANGLE_H
3233965Sjdp
33130561Sobrien#include "libiberty.h"
3433965Sjdp
35130561Sobrien#ifdef __cplusplus
36130561Sobrienextern "C" {
37130561Sobrien#endif /* __cplusplus */
38130561Sobrien
3933965Sjdp/* Options passed to cplus_demangle (in 2nd parameter). */
4033965Sjdp
4168765Sobrien#define DMGL_NO_OPTS	 0		/* For readability... */
4268765Sobrien#define DMGL_PARAMS	 (1 << 0)	/* Include function args */
4368765Sobrien#define DMGL_ANSI	 (1 << 1)	/* Include const, volatile, etc */
4468765Sobrien#define DMGL_JAVA	 (1 << 2)	/* Demangle as Java rather than C++. */
4591041Sobrien#define DMGL_VERBOSE	 (1 << 3)	/* Include implementation details.  */
4691041Sobrien#define DMGL_TYPES	 (1 << 4)	/* Also try to demangle type encodings.  */
47218822Sdim#define DMGL_RET_POSTFIX (1 << 5)       /* Print function return types (when
48218822Sdim                                           present) after function signature */
4933965Sjdp
5068765Sobrien#define DMGL_AUTO	 (1 << 8)
5168765Sobrien#define DMGL_GNU	 (1 << 9)
5268765Sobrien#define DMGL_LUCID	 (1 << 10)
5368765Sobrien#define DMGL_ARM	 (1 << 11)
5468765Sobrien#define DMGL_HP 	 (1 << 12)       /* For the HP aCC compiler;
5568765Sobrien                                            same as ARM except for
5668765Sobrien                                            template arguments, etc. */
5768765Sobrien#define DMGL_EDG	 (1 << 13)
5877298Sobrien#define DMGL_GNU_V3	 (1 << 14)
5977298Sobrien#define DMGL_GNAT	 (1 << 15)
6060484Sobrien
6133965Sjdp/* If none of these are set, use 'current_demangling_style' as the default. */
6277298Sobrien#define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT)
6333965Sjdp
6433965Sjdp/* Enumeration of possible demangling styles.
6533965Sjdp
6633965Sjdp   Lucid and ARM styles are still kept logically distinct, even though
6733965Sjdp   they now both behave identically.  The resulting style is actual the
6833965Sjdp   union of both.  I.E. either style recognizes both "__pt__" and "__rf__"
6933965Sjdp   for operator "->", even though the first is lucid style and the second
7033965Sjdp   is ARM style. (FIXME?) */
7133965Sjdp
7233965Sjdpextern enum demangling_styles
7333965Sjdp{
7489857Sobrien  no_demangling = -1,
7533965Sjdp  unknown_demangling = 0,
7633965Sjdp  auto_demangling = DMGL_AUTO,
7733965Sjdp  gnu_demangling = DMGL_GNU,
7833965Sjdp  lucid_demangling = DMGL_LUCID,
7960484Sobrien  arm_demangling = DMGL_ARM,
8060484Sobrien  hp_demangling = DMGL_HP,
8168765Sobrien  edg_demangling = DMGL_EDG,
8277298Sobrien  gnu_v3_demangling = DMGL_GNU_V3,
8377298Sobrien  java_demangling = DMGL_JAVA,
8477298Sobrien  gnat_demangling = DMGL_GNAT
8533965Sjdp} current_demangling_style;
8633965Sjdp
8733965Sjdp/* Define string names for the various demangling styles. */
8833965Sjdp
8989857Sobrien#define NO_DEMANGLING_STYLE_STRING            "none"
9068765Sobrien#define AUTO_DEMANGLING_STYLE_STRING	      "auto"
9168765Sobrien#define GNU_DEMANGLING_STYLE_STRING    	      "gnu"
9268765Sobrien#define LUCID_DEMANGLING_STYLE_STRING	      "lucid"
9368765Sobrien#define ARM_DEMANGLING_STYLE_STRING	      "arm"
9468765Sobrien#define HP_DEMANGLING_STYLE_STRING	      "hp"
9568765Sobrien#define EDG_DEMANGLING_STYLE_STRING	      "edg"
9677298Sobrien#define GNU_V3_DEMANGLING_STYLE_STRING        "gnu-v3"
9777298Sobrien#define JAVA_DEMANGLING_STYLE_STRING          "java"
9877298Sobrien#define GNAT_DEMANGLING_STYLE_STRING          "gnat"
9933965Sjdp
10033965Sjdp/* Some macros to test what demangling style is active. */
10133965Sjdp
10233965Sjdp#define CURRENT_DEMANGLING_STYLE current_demangling_style
10333965Sjdp#define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
10433965Sjdp#define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
10533965Sjdp#define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
10660484Sobrien#define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
10760484Sobrien#define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
10860484Sobrien#define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
10977298Sobrien#define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
11077298Sobrien#define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
11177298Sobrien#define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
11233965Sjdp
11368765Sobrien/* Provide information about the available demangle styles. This code is
11468765Sobrien   pulled from gdb into libiberty because it is useful to binutils also.  */
11568765Sobrien
11689857Sobrienextern const struct demangler_engine
11768765Sobrien{
11889857Sobrien  const char *const demangling_style_name;
11989857Sobrien  const enum demangling_styles demangling_style;
12089857Sobrien  const char *const demangling_style_doc;
12168765Sobrien} libiberty_demanglers[];
12268765Sobrien
12333965Sjdpextern char *
124218822Sdimcplus_demangle (const char *mangled, int options);
12533965Sjdp
12633965Sjdpextern int
127218822Sdimcplus_demangle_opname (const char *opname, char *result, int options);
12833965Sjdp
12933965Sjdpextern const char *
130218822Sdimcplus_mangle_opname (const char *opname, int options);
13133965Sjdp
13233965Sjdp/* Note: This sets global state.  FIXME if you care about multi-threading. */
13333965Sjdp
13433965Sjdpextern void
135218822Sdimset_cplus_marker_for_demangling (int ch);
13633965Sjdp
13768765Sobrienextern enum demangling_styles
138218822Sdimcplus_demangle_set_style (enum demangling_styles style);
13968765Sobrien
14068765Sobrienextern enum demangling_styles
141218822Sdimcplus_demangle_name_to_style (const char *name);
14268765Sobrien
143218822Sdim/* Callback typedef for allocation-less demangler interfaces. */
144218822Sdimtypedef void (*demangle_callbackref) (const char *, size_t, void *);
145218822Sdim
146218822Sdim/* V3 ABI demangling entry points, defined in cp-demangle.c.  Callback
147218822Sdim   variants return non-zero on success, zero on error.  char* variants
148218822Sdim   return a string allocated by malloc on success, NULL on error.  */
149218822Sdimextern int
150218822Sdimcplus_demangle_v3_callback (const char *mangled, int options,
151218822Sdim                            demangle_callbackref callback, void *opaque);
152218822Sdim
15368765Sobrienextern char*
154218822Sdimcplus_demangle_v3 (const char *mangled, int options);
15568765Sobrien
156218822Sdimextern int
157218822Sdimjava_demangle_v3_callback (const char *mangled,
158218822Sdim                           demangle_callbackref callback, void *opaque);
159218822Sdim
16089857Sobrienextern char*
161218822Sdimjava_demangle_v3 (const char *mangled);
16289857Sobrien
16389857Sobrienenum gnu_v3_ctor_kinds {
16489857Sobrien  gnu_v3_complete_object_ctor = 1,
16589857Sobrien  gnu_v3_base_object_ctor,
16689857Sobrien  gnu_v3_complete_object_allocating_ctor
16789857Sobrien};
16889857Sobrien
16989857Sobrien/* Return non-zero iff NAME is the mangled form of a constructor name
17089857Sobrien   in the G++ V3 ABI demangling style.  Specifically, return an `enum
17189857Sobrien   gnu_v3_ctor_kinds' value indicating what kind of constructor
17289857Sobrien   it is.  */
17389857Sobrienextern enum gnu_v3_ctor_kinds
174218822Sdim	is_gnu_v3_mangled_ctor (const char *name);
17589857Sobrien
17689857Sobrien
17789857Sobrienenum gnu_v3_dtor_kinds {
17889857Sobrien  gnu_v3_deleting_dtor = 1,
17989857Sobrien  gnu_v3_complete_object_dtor,
18089857Sobrien  gnu_v3_base_object_dtor
18189857Sobrien};
18289857Sobrien
18389857Sobrien/* Return non-zero iff NAME is the mangled form of a destructor name
18489857Sobrien   in the G++ V3 ABI demangling style.  Specifically, return an `enum
18589857Sobrien   gnu_v3_dtor_kinds' value, indicating what kind of destructor
18689857Sobrien   it is.  */
18789857Sobrienextern enum gnu_v3_dtor_kinds
188218822Sdim	is_gnu_v3_mangled_dtor (const char *name);
18989857Sobrien
190130561Sobrien/* The V3 demangler works in two passes.  The first pass builds a tree
191130561Sobrien   representation of the mangled name, and the second pass turns the
192130561Sobrien   tree representation into a demangled string.  Here we define an
193130561Sobrien   interface to permit a caller to build their own tree
194130561Sobrien   representation, which they can pass to the demangler to get a
195130561Sobrien   demangled string.  This can be used to canonicalize user input into
196130561Sobrien   something which the demangler might output.  It could also be used
197130561Sobrien   by other demanglers in the future.  */
198130561Sobrien
199130561Sobrien/* These are the component types which may be found in the tree.  Many
200130561Sobrien   component types have one or two subtrees, referred to as left and
201130561Sobrien   right (a component type with only one subtree puts it in the left
202130561Sobrien   subtree).  */
203130561Sobrien
204130561Sobrienenum demangle_component_type
205130561Sobrien{
206130561Sobrien  /* A name, with a length and a pointer to a string.  */
207130561Sobrien  DEMANGLE_COMPONENT_NAME,
208130561Sobrien  /* A qualified name.  The left subtree is a class or namespace or
209130561Sobrien     some such thing, and the right subtree is a name qualified by
210130561Sobrien     that class.  */
211130561Sobrien  DEMANGLE_COMPONENT_QUAL_NAME,
212130561Sobrien  /* A local name.  The left subtree describes a function, and the
213130561Sobrien     right subtree is a name which is local to that function.  */
214130561Sobrien  DEMANGLE_COMPONENT_LOCAL_NAME,
215130561Sobrien  /* A typed name.  The left subtree is a name, and the right subtree
216130561Sobrien     describes that name as a function.  */
217130561Sobrien  DEMANGLE_COMPONENT_TYPED_NAME,
218130561Sobrien  /* A template.  The left subtree is a template name, and the right
219130561Sobrien     subtree is a template argument list.  */
220130561Sobrien  DEMANGLE_COMPONENT_TEMPLATE,
221130561Sobrien  /* A template parameter.  This holds a number, which is the template
222130561Sobrien     parameter index.  */
223130561Sobrien  DEMANGLE_COMPONENT_TEMPLATE_PARAM,
224130561Sobrien  /* A constructor.  This holds a name and the kind of
225130561Sobrien     constructor.  */
226130561Sobrien  DEMANGLE_COMPONENT_CTOR,
227130561Sobrien  /* A destructor.  This holds a name and the kind of destructor.  */
228130561Sobrien  DEMANGLE_COMPONENT_DTOR,
229130561Sobrien  /* A vtable.  This has one subtree, the type for which this is a
230130561Sobrien     vtable.  */
231130561Sobrien  DEMANGLE_COMPONENT_VTABLE,
232130561Sobrien  /* A VTT structure.  This has one subtree, the type for which this
233130561Sobrien     is a VTT.  */
234130561Sobrien  DEMANGLE_COMPONENT_VTT,
235130561Sobrien  /* A construction vtable.  The left subtree is the type for which
236130561Sobrien     this is a vtable, and the right subtree is the derived type for
237130561Sobrien     which this vtable is built.  */
238130561Sobrien  DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
239130561Sobrien  /* A typeinfo structure.  This has one subtree, the type for which
240130561Sobrien     this is the tpeinfo structure.  */
241130561Sobrien  DEMANGLE_COMPONENT_TYPEINFO,
242130561Sobrien  /* A typeinfo name.  This has one subtree, the type for which this
243130561Sobrien     is the typeinfo name.  */
244130561Sobrien  DEMANGLE_COMPONENT_TYPEINFO_NAME,
245130561Sobrien  /* A typeinfo function.  This has one subtree, the type for which
246130561Sobrien     this is the tpyeinfo function.  */
247130561Sobrien  DEMANGLE_COMPONENT_TYPEINFO_FN,
248130561Sobrien  /* A thunk.  This has one subtree, the name for which this is a
249130561Sobrien     thunk.  */
250130561Sobrien  DEMANGLE_COMPONENT_THUNK,
251130561Sobrien  /* A virtual thunk.  This has one subtree, the name for which this
252130561Sobrien     is a virtual thunk.  */
253130561Sobrien  DEMANGLE_COMPONENT_VIRTUAL_THUNK,
254130561Sobrien  /* A covariant thunk.  This has one subtree, the name for which this
255130561Sobrien     is a covariant thunk.  */
256130561Sobrien  DEMANGLE_COMPONENT_COVARIANT_THUNK,
257130561Sobrien  /* A Java class.  This has one subtree, the type.  */
258130561Sobrien  DEMANGLE_COMPONENT_JAVA_CLASS,
259130561Sobrien  /* A guard variable.  This has one subtree, the name for which this
260130561Sobrien     is a guard variable.  */
261130561Sobrien  DEMANGLE_COMPONENT_GUARD,
262130561Sobrien  /* A reference temporary.  This has one subtree, the name for which
263130561Sobrien     this is a temporary.  */
264130561Sobrien  DEMANGLE_COMPONENT_REFTEMP,
265218822Sdim  /* A hidden alias.  This has one subtree, the encoding for which it
266218822Sdim     is providing alternative linkage.  */
267218822Sdim  DEMANGLE_COMPONENT_HIDDEN_ALIAS,
268130561Sobrien  /* A standard substitution.  This holds the name of the
269130561Sobrien     substitution.  */
270130561Sobrien  DEMANGLE_COMPONENT_SUB_STD,
271130561Sobrien  /* The restrict qualifier.  The one subtree is the type which is
272130561Sobrien     being qualified.  */
273130561Sobrien  DEMANGLE_COMPONENT_RESTRICT,
274130561Sobrien  /* The volatile qualifier.  The one subtree is the type which is
275130561Sobrien     being qualified.  */
276130561Sobrien  DEMANGLE_COMPONENT_VOLATILE,
277130561Sobrien  /* The const qualifier.  The one subtree is the type which is being
278130561Sobrien     qualified.  */
279130561Sobrien  DEMANGLE_COMPONENT_CONST,
280130561Sobrien  /* The restrict qualifier modifying a member function.  The one
281130561Sobrien     subtree is the type which is being qualified.  */
282130561Sobrien  DEMANGLE_COMPONENT_RESTRICT_THIS,
283130561Sobrien  /* The volatile qualifier modifying a member function.  The one
284130561Sobrien     subtree is the type which is being qualified.  */
285130561Sobrien  DEMANGLE_COMPONENT_VOLATILE_THIS,
286130561Sobrien  /* The const qualifier modifying a member function.  The one subtree
287130561Sobrien     is the type which is being qualified.  */
288130561Sobrien  DEMANGLE_COMPONENT_CONST_THIS,
289130561Sobrien  /* A vendor qualifier.  The left subtree is the type which is being
290130561Sobrien     qualified, and the right subtree is the name of the
291130561Sobrien     qualifier.  */
292130561Sobrien  DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
293130561Sobrien  /* A pointer.  The one subtree is the type which is being pointed
294130561Sobrien     to.  */
295130561Sobrien  DEMANGLE_COMPONENT_POINTER,
296130561Sobrien  /* A reference.  The one subtree is the type which is being
297130561Sobrien     referenced.  */
298130561Sobrien  DEMANGLE_COMPONENT_REFERENCE,
299130561Sobrien  /* A complex type.  The one subtree is the base type.  */
300130561Sobrien  DEMANGLE_COMPONENT_COMPLEX,
301130561Sobrien  /* An imaginary type.  The one subtree is the base type.  */
302130561Sobrien  DEMANGLE_COMPONENT_IMAGINARY,
303130561Sobrien  /* A builtin type.  This holds the builtin type information.  */
304130561Sobrien  DEMANGLE_COMPONENT_BUILTIN_TYPE,
305130561Sobrien  /* A vendor's builtin type.  This holds the name of the type.  */
306130561Sobrien  DEMANGLE_COMPONENT_VENDOR_TYPE,
307130561Sobrien  /* A function type.  The left subtree is the return type.  The right
308130561Sobrien     subtree is a list of ARGLIST nodes.  Either or both may be
309130561Sobrien     NULL.  */
310130561Sobrien  DEMANGLE_COMPONENT_FUNCTION_TYPE,
311130561Sobrien  /* An array type.  The left subtree is the dimension, which may be
312130561Sobrien     NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
313130561Sobrien     expression.  The right subtree is the element type.  */
314130561Sobrien  DEMANGLE_COMPONENT_ARRAY_TYPE,
315130561Sobrien  /* A pointer to member type.  The left subtree is the class type,
316130561Sobrien     and the right subtree is the member type.  CV-qualifiers appear
317130561Sobrien     on the latter.  */
318130561Sobrien  DEMANGLE_COMPONENT_PTRMEM_TYPE,
319130561Sobrien  /* An argument list.  The left subtree is the current argument, and
320130561Sobrien     the right subtree is either NULL or another ARGLIST node.  */
321130561Sobrien  DEMANGLE_COMPONENT_ARGLIST,
322130561Sobrien  /* A template argument list.  The left subtree is the current
323130561Sobrien     template argument, and the right subtree is either NULL or
324130561Sobrien     another TEMPLATE_ARGLIST node.  */
325130561Sobrien  DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
326130561Sobrien  /* An operator.  This holds information about a standard
327130561Sobrien     operator.  */
328130561Sobrien  DEMANGLE_COMPONENT_OPERATOR,
329130561Sobrien  /* An extended operator.  This holds the number of arguments, and
330130561Sobrien     the name of the extended operator.  */
331130561Sobrien  DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
332130561Sobrien  /* A typecast, represented as a unary operator.  The one subtree is
333130561Sobrien     the type to which the argument should be cast.  */
334130561Sobrien  DEMANGLE_COMPONENT_CAST,
335130561Sobrien  /* A unary expression.  The left subtree is the operator, and the
336130561Sobrien     right subtree is the single argument.  */
337130561Sobrien  DEMANGLE_COMPONENT_UNARY,
338130561Sobrien  /* A binary expression.  The left subtree is the operator, and the
339130561Sobrien     right subtree is a BINARY_ARGS.  */
340130561Sobrien  DEMANGLE_COMPONENT_BINARY,
341130561Sobrien  /* Arguments to a binary expression.  The left subtree is the first
342130561Sobrien     argument, and the right subtree is the second argument.  */
343130561Sobrien  DEMANGLE_COMPONENT_BINARY_ARGS,
344130561Sobrien  /* A trinary expression.  The left subtree is the operator, and the
345130561Sobrien     right subtree is a TRINARY_ARG1.  */
346130561Sobrien  DEMANGLE_COMPONENT_TRINARY,
347130561Sobrien  /* Arguments to a trinary expression.  The left subtree is the first
348130561Sobrien     argument, and the right subtree is a TRINARY_ARG2.  */
349130561Sobrien  DEMANGLE_COMPONENT_TRINARY_ARG1,
350130561Sobrien  /* More arguments to a trinary expression.  The left subtree is the
351130561Sobrien     second argument, and the right subtree is the third argument.  */
352130561Sobrien  DEMANGLE_COMPONENT_TRINARY_ARG2,
353130561Sobrien  /* A literal.  The left subtree is the type, and the right subtree
354130561Sobrien     is the value, represented as a DEMANGLE_COMPONENT_NAME.  */
355130561Sobrien  DEMANGLE_COMPONENT_LITERAL,
356130561Sobrien  /* A negative literal.  Like LITERAL, but the value is negated.
357130561Sobrien     This is a minor hack: the NAME used for LITERAL points directly
358130561Sobrien     to the mangled string, but since negative numbers are mangled
359130561Sobrien     using 'n' instead of '-', we want a way to indicate a negative
360130561Sobrien     number which involves neither modifying the mangled string nor
361130561Sobrien     allocating a new copy of the literal in memory.  */
362130561Sobrien  DEMANGLE_COMPONENT_LITERAL_NEG
363130561Sobrien};
364130561Sobrien
365130561Sobrien/* Types which are only used internally.  */
366130561Sobrien
367130561Sobrienstruct demangle_operator_info;
368130561Sobrienstruct demangle_builtin_type_info;
369130561Sobrien
370130561Sobrien/* A node in the tree representation is an instance of a struct
371130561Sobrien   demangle_component.  Note that the field names of the struct are
372130561Sobrien   not well protected against macros defined by the file including
373130561Sobrien   this one.  We can fix this if it ever becomes a problem.  */
374130561Sobrien
375130561Sobrienstruct demangle_component
376130561Sobrien{
377130561Sobrien  /* The type of this component.  */
378130561Sobrien  enum demangle_component_type type;
379130561Sobrien
380130561Sobrien  union
381130561Sobrien  {
382130561Sobrien    /* For DEMANGLE_COMPONENT_NAME.  */
383130561Sobrien    struct
384130561Sobrien    {
385130561Sobrien      /* A pointer to the name (which need not NULL terminated) and
386130561Sobrien	 its length.  */
387130561Sobrien      const char *s;
388130561Sobrien      int len;
389130561Sobrien    } s_name;
390130561Sobrien
391130561Sobrien    /* For DEMANGLE_COMPONENT_OPERATOR.  */
392130561Sobrien    struct
393130561Sobrien    {
394130561Sobrien      /* Operator.  */
395130561Sobrien      const struct demangle_operator_info *op;
396130561Sobrien    } s_operator;
397130561Sobrien
398130561Sobrien    /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
399130561Sobrien    struct
400130561Sobrien    {
401130561Sobrien      /* Number of arguments.  */
402130561Sobrien      int args;
403130561Sobrien      /* Name.  */
404130561Sobrien      struct demangle_component *name;
405130561Sobrien    } s_extended_operator;
406130561Sobrien
407130561Sobrien    /* For DEMANGLE_COMPONENT_CTOR.  */
408130561Sobrien    struct
409130561Sobrien    {
410130561Sobrien      /* Kind of constructor.  */
411130561Sobrien      enum gnu_v3_ctor_kinds kind;
412130561Sobrien      /* Name.  */
413130561Sobrien      struct demangle_component *name;
414130561Sobrien    } s_ctor;
415130561Sobrien
416130561Sobrien    /* For DEMANGLE_COMPONENT_DTOR.  */
417130561Sobrien    struct
418130561Sobrien    {
419130561Sobrien      /* Kind of destructor.  */
420130561Sobrien      enum gnu_v3_dtor_kinds kind;
421130561Sobrien      /* Name.  */
422130561Sobrien      struct demangle_component *name;
423130561Sobrien    } s_dtor;
424130561Sobrien
425130561Sobrien    /* For DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
426130561Sobrien    struct
427130561Sobrien    {
428130561Sobrien      /* Builtin type.  */
429130561Sobrien      const struct demangle_builtin_type_info *type;
430130561Sobrien    } s_builtin;
431130561Sobrien
432130561Sobrien    /* For DEMANGLE_COMPONENT_SUB_STD.  */
433130561Sobrien    struct
434130561Sobrien    {
435130561Sobrien      /* Standard substitution string.  */
436130561Sobrien      const char* string;
437130561Sobrien      /* Length of string.  */
438130561Sobrien      int len;
439130561Sobrien    } s_string;
440130561Sobrien
441130561Sobrien    /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM.  */
442130561Sobrien    struct
443130561Sobrien    {
444130561Sobrien      /* Template parameter index.  */
445130561Sobrien      long number;
446130561Sobrien    } s_number;
447130561Sobrien
448130561Sobrien    /* For other types.  */
449130561Sobrien    struct
450130561Sobrien    {
451130561Sobrien      /* Left (or only) subtree.  */
452130561Sobrien      struct demangle_component *left;
453130561Sobrien      /* Right subtree.  */
454130561Sobrien      struct demangle_component *right;
455130561Sobrien    } s_binary;
456130561Sobrien
457130561Sobrien  } u;
458130561Sobrien};
459130561Sobrien
460130561Sobrien/* People building mangled trees are expected to allocate instances of
461130561Sobrien   struct demangle_component themselves.  They can then call one of
462130561Sobrien   the following functions to fill them in.  */
463130561Sobrien
464130561Sobrien/* Fill in most component types with a left subtree and a right
465130561Sobrien   subtree.  Returns non-zero on success, zero on failure, such as an
466130561Sobrien   unrecognized or inappropriate component type.  */
467130561Sobrien
468130561Sobrienextern int
469218822Sdimcplus_demangle_fill_component (struct demangle_component *fill,
470218822Sdim                               enum demangle_component_type,
471218822Sdim                               struct demangle_component *left,
472218822Sdim                               struct demangle_component *right);
473130561Sobrien
474130561Sobrien/* Fill in a DEMANGLE_COMPONENT_NAME.  Returns non-zero on success,
475130561Sobrien   zero for bad arguments.  */
476130561Sobrien
477130561Sobrienextern int
478218822Sdimcplus_demangle_fill_name (struct demangle_component *fill,
479218822Sdim                          const char *, int);
480130561Sobrien
481130561Sobrien/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
482130561Sobrien   builtin type (e.g., "int", etc.).  Returns non-zero on success,
483130561Sobrien   zero if the type is not recognized.  */
484130561Sobrien
485130561Sobrienextern int
486218822Sdimcplus_demangle_fill_builtin_type (struct demangle_component *fill,
487218822Sdim                                  const char *type_name);
488130561Sobrien
489130561Sobrien/* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
490130561Sobrien   operator and the number of arguments which it takes (the latter is
491130561Sobrien   used to disambiguate operators which can be both binary and unary,
492130561Sobrien   such as '-').  Returns non-zero on success, zero if the operator is
493130561Sobrien   not recognized.  */
494130561Sobrien
495130561Sobrienextern int
496218822Sdimcplus_demangle_fill_operator (struct demangle_component *fill,
497218822Sdim                              const char *opname, int args);
498130561Sobrien
499130561Sobrien/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
500130561Sobrien   number of arguments and the name.  Returns non-zero on success,
501130561Sobrien   zero for bad arguments.  */
502130561Sobrien
503130561Sobrienextern int
504218822Sdimcplus_demangle_fill_extended_operator (struct demangle_component *fill,
505218822Sdim                                       int numargs,
506218822Sdim                                       struct demangle_component *nm);
507130561Sobrien
508130561Sobrien/* Fill in a DEMANGLE_COMPONENT_CTOR.  Returns non-zero on success,
509130561Sobrien   zero for bad arguments.  */
510130561Sobrien
511130561Sobrienextern int
512218822Sdimcplus_demangle_fill_ctor (struct demangle_component *fill,
513218822Sdim                          enum gnu_v3_ctor_kinds kind,
514218822Sdim                          struct demangle_component *name);
515130561Sobrien
516130561Sobrien/* Fill in a DEMANGLE_COMPONENT_DTOR.  Returns non-zero on success,
517130561Sobrien   zero for bad arguments.  */
518130561Sobrien
519130561Sobrienextern int
520218822Sdimcplus_demangle_fill_dtor (struct demangle_component *fill,
521218822Sdim                          enum gnu_v3_dtor_kinds kind,
522218822Sdim                          struct demangle_component *name);
523130561Sobrien
524130561Sobrien/* This function translates a mangled name into a struct
525130561Sobrien   demangle_component tree.  The first argument is the mangled name.
526130561Sobrien   The second argument is DMGL_* options.  This returns a pointer to a
527130561Sobrien   tree on success, or NULL on failure.  On success, the third
528130561Sobrien   argument is set to a block of memory allocated by malloc.  This
529130561Sobrien   block should be passed to free when the tree is no longer
530130561Sobrien   needed.  */
531130561Sobrien
532130561Sobrienextern struct demangle_component *
533218822Sdimcplus_demangle_v3_components (const char *mangled, int options, void **mem);
534130561Sobrien
535130561Sobrien/* This function takes a struct demangle_component tree and returns
536130561Sobrien   the corresponding demangled string.  The first argument is DMGL_*
537130561Sobrien   options.  The second is the tree to demangle.  The third is a guess
538130561Sobrien   at the length of the demangled string, used to initially allocate
539130561Sobrien   the return buffer.  The fourth is a pointer to a size_t.  On
540130561Sobrien   success, this function returns a buffer allocated by malloc(), and
541130561Sobrien   sets the size_t pointed to by the fourth argument to the size of
542130561Sobrien   the allocated buffer (not the length of the returned string).  On
543130561Sobrien   failure, this function returns NULL, and sets the size_t pointed to
544130561Sobrien   by the fourth argument to 0 for an invalid tree, or to 1 for a
545130561Sobrien   memory allocation error.  */
546130561Sobrien
547130561Sobrienextern char *
548218822Sdimcplus_demangle_print (int options,
549218822Sdim                      const struct demangle_component *tree,
550218822Sdim                      int estimated_length,
551218822Sdim                      size_t *p_allocated_size);
552130561Sobrien
553218822Sdim/* This function takes a struct demangle_component tree and passes back
554218822Sdim   a demangled string in one or more calls to a callback function.
555218822Sdim   The first argument is DMGL_* options.  The second is the tree to
556218822Sdim   demangle.  The third is a pointer to a callback function; on each call
557218822Sdim   this receives an element of the demangled string, its length, and an
558218822Sdim   opaque value.  The fourth is the opaque value passed to the callback.
559218822Sdim   The callback is called once or more to return the full demangled
560218822Sdim   string.  The demangled element string is always nul-terminated, though
561218822Sdim   its length is also provided for convenience.  In contrast to
562218822Sdim   cplus_demangle_print(), this function does not allocate heap memory
563218822Sdim   to grow output strings (except perhaps where alloca() is implemented
564218822Sdim   by malloc()), and so is normally safe for use where the heap has been
565218822Sdim   corrupted.  On success, this function returns 1; on failure, 0.  */
566218822Sdim
567218822Sdimextern int
568218822Sdimcplus_demangle_print_callback (int options,
569218822Sdim                               const struct demangle_component *tree,
570218822Sdim                               demangle_callbackref callback, void *opaque);
571218822Sdim
572130561Sobrien#ifdef __cplusplus
573130561Sobrien}
574130561Sobrien#endif /* __cplusplus */
575130561Sobrien
57633965Sjdp#endif	/* DEMANGLE_H */
577