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