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