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