1 /* $MirOS: src/gnu/usr.bin/binutils/include/demangle.h,v 1.4 2005/06/05 21:24:34 tg Exp $ */
2 
3 /* Defs for interface to demanglers.
4    Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
5    2003, 2004, 2005
6    Free Software Foundation, Inc.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22 
23 
24 #if !defined (DEMANGLE_H)
25 #define DEMANGLE_H
26 
27 #include "libiberty.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif /* __cplusplus */
32 
33 /* Options passed to cplus_demangle (in 2nd parameter). */
34 
35 #define DMGL_NO_OPTS	 0		/* For readability... */
36 #define DMGL_PARAMS	 (1 << 0)	/* Include function args */
37 #define DMGL_ANSI	 (1 << 1)	/* Include const, volatile, etc */
38 #define DMGL_JAVA	 (1 << 2)	/* Demangle as Java rather than C++. */
39 #define DMGL_VERBOSE	 (1 << 3)	/* Include implementation details.  */
40 #define DMGL_TYPES	 (1 << 4)	/* Also try to demangle type encodings.  */
41 
42 #define DMGL_AUTO	 (1 << 8)
43 #define DMGL_GNU	 (1 << 9)
44 #define DMGL_LUCID	 (1 << 10)
45 #define DMGL_ARM	 (1 << 11)
46 #define DMGL_HP 	 (1 << 12)       /* For the HP aCC compiler;
47                                             same as ARM except for
48                                             template arguments, etc. */
49 #define DMGL_EDG	 (1 << 13)
50 #define DMGL_GNU_V3	 (1 << 14)
51 #define DMGL_GNAT	 (1 << 15)
52 
53 /* If none of these are set, use 'current_demangling_style' as the default. */
54 #define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT)
55 
56 /* Enumeration of possible demangling styles.
57 
58    Lucid and ARM styles are still kept logically distinct, even though
59    they now both behave identically.  The resulting style is actual the
60    union of both.  I.E. either style recognizes both "__pt__" and "__rf__"
61    for operator "->", even though the first is lucid style and the second
62    is ARM style. (FIXME?) */
63 
64 extern enum demangling_styles
65 {
66   no_demangling = -1,
67   unknown_demangling = 0,
68   auto_demangling = DMGL_AUTO,
69   gnu_demangling = DMGL_GNU,
70   lucid_demangling = DMGL_LUCID,
71   arm_demangling = DMGL_ARM,
72   hp_demangling = DMGL_HP,
73   edg_demangling = DMGL_EDG,
74   gnu_v3_demangling = DMGL_GNU_V3,
75   java_demangling = DMGL_JAVA,
76   gnat_demangling = DMGL_GNAT
77 } current_demangling_style;
78 
79 /* Define string names for the various demangling styles. */
80 
81 #define NO_DEMANGLING_STYLE_STRING            "none"
82 #define AUTO_DEMANGLING_STYLE_STRING	      "auto"
83 #define GNU_DEMANGLING_STYLE_STRING    	      "gnu"
84 #define LUCID_DEMANGLING_STYLE_STRING	      "lucid"
85 #define ARM_DEMANGLING_STYLE_STRING	      "arm"
86 #define HP_DEMANGLING_STYLE_STRING	      "hp"
87 #define EDG_DEMANGLING_STYLE_STRING	      "edg"
88 #define GNU_V3_DEMANGLING_STYLE_STRING        "gnu-v3"
89 #define JAVA_DEMANGLING_STYLE_STRING          "java"
90 #define GNAT_DEMANGLING_STYLE_STRING          "gnat"
91 
92 /* Some macros to test what demangling style is active. */
93 
94 #define CURRENT_DEMANGLING_STYLE current_demangling_style
95 #define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
96 #define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
97 #define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
98 #define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
99 #define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
100 #define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
101 #define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
102 #define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
103 #define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
104 
105 /* Provide information about the available demangle styles. This code is
106    pulled from gdb into libiberty because it is useful to binutils also.  */
107 
108 extern const struct demangler_engine
109 {
110   const char *const demangling_style_name;
111   const enum demangling_styles demangling_style;
112   const char *const demangling_style_doc;
113 } libiberty_demanglers[];
114 
115 extern char *
116 cplus_demangle (const char *mangled, int options);
117 
118 extern int
119 cplus_demangle_opname (const char *opname, char *result, int options,
120                        size_t ressiz);
121 
122 extern const char *
123 cplus_mangle_opname (const char *opname, int options);
124 
125 /* Note: This sets global state.  FIXME if you care about multi-threading. */
126 
127 extern void
128 set_cplus_marker_for_demangling (int ch);
129 
130 extern enum demangling_styles
131 cplus_demangle_set_style (enum demangling_styles style);
132 
133 extern enum demangling_styles
134 cplus_demangle_name_to_style (const char *name);
135 
136 /* V3 ABI demangling entry points, defined in cp-demangle.c.  */
137 extern char*
138 cplus_demangle_v3 (const char* mangled, int options);
139 
140 extern char*
141 java_demangle_v3 (const char* mangled);
142 
143 
144 enum gnu_v3_ctor_kinds {
145   gnu_v3_complete_object_ctor = 1,
146   gnu_v3_base_object_ctor,
147   gnu_v3_complete_object_allocating_ctor
148 };
149 
150 /* Return non-zero iff NAME is the mangled form of a constructor name
151    in the G++ V3 ABI demangling style.  Specifically, return an `enum
152    gnu_v3_ctor_kinds' value indicating what kind of constructor
153    it is.  */
154 extern enum gnu_v3_ctor_kinds
155 	is_gnu_v3_mangled_ctor (const char *name);
156 
157 
158 enum gnu_v3_dtor_kinds {
159   gnu_v3_deleting_dtor = 1,
160   gnu_v3_complete_object_dtor,
161   gnu_v3_base_object_dtor
162 };
163 
164 /* Return non-zero iff NAME is the mangled form of a destructor name
165    in the G++ V3 ABI demangling style.  Specifically, return an `enum
166    gnu_v3_dtor_kinds' value, indicating what kind of destructor
167    it is.  */
168 extern enum gnu_v3_dtor_kinds
169 	is_gnu_v3_mangled_dtor (const char *name);
170 
171 /* The V3 demangler works in two passes.  The first pass builds a tree
172    representation of the mangled name, and the second pass turns the
173    tree representation into a demangled string.  Here we define an
174    interface to permit a caller to build their own tree
175    representation, which they can pass to the demangler to get a
176    demangled string.  This can be used to canonicalize user input into
177    something which the demangler might output.  It could also be used
178    by other demanglers in the future.  */
179 
180 /* These are the component types which may be found in the tree.  Many
181    component types have one or two subtrees, referred to as left and
182    right (a component type with only one subtree puts it in the left
183    subtree).  */
184 
185 enum demangle_component_type
186 {
187   /* A name, with a length and a pointer to a string.  */
188   DEMANGLE_COMPONENT_NAME,
189   /* A qualified name.  The left subtree is a class or namespace or
190      some such thing, and the right subtree is a name qualified by
191      that class.  */
192   DEMANGLE_COMPONENT_QUAL_NAME,
193   /* A local name.  The left subtree describes a function, and the
194      right subtree is a name which is local to that function.  */
195   DEMANGLE_COMPONENT_LOCAL_NAME,
196   /* A typed name.  The left subtree is a name, and the right subtree
197      describes that name as a function.  */
198   DEMANGLE_COMPONENT_TYPED_NAME,
199   /* A template.  The left subtree is a template name, and the right
200      subtree is a template argument list.  */
201   DEMANGLE_COMPONENT_TEMPLATE,
202   /* A template parameter.  This holds a number, which is the template
203      parameter index.  */
204   DEMANGLE_COMPONENT_TEMPLATE_PARAM,
205   /* A constructor.  This holds a name and the kind of
206      constructor.  */
207   DEMANGLE_COMPONENT_CTOR,
208   /* A destructor.  This holds a name and the kind of destructor.  */
209   DEMANGLE_COMPONENT_DTOR,
210   /* A vtable.  This has one subtree, the type for which this is a
211      vtable.  */
212   DEMANGLE_COMPONENT_VTABLE,
213   /* A VTT structure.  This has one subtree, the type for which this
214      is a VTT.  */
215   DEMANGLE_COMPONENT_VTT,
216   /* A construction vtable.  The left subtree is the type for which
217      this is a vtable, and the right subtree is the derived type for
218      which this vtable is built.  */
219   DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
220   /* A typeinfo structure.  This has one subtree, the type for which
221      this is the tpeinfo structure.  */
222   DEMANGLE_COMPONENT_TYPEINFO,
223   /* A typeinfo name.  This has one subtree, the type for which this
224      is the typeinfo name.  */
225   DEMANGLE_COMPONENT_TYPEINFO_NAME,
226   /* A typeinfo function.  This has one subtree, the type for which
227      this is the tpyeinfo function.  */
228   DEMANGLE_COMPONENT_TYPEINFO_FN,
229   /* A thunk.  This has one subtree, the name for which this is a
230      thunk.  */
231   DEMANGLE_COMPONENT_THUNK,
232   /* A virtual thunk.  This has one subtree, the name for which this
233      is a virtual thunk.  */
234   DEMANGLE_COMPONENT_VIRTUAL_THUNK,
235   /* A covariant thunk.  This has one subtree, the name for which this
236      is a covariant thunk.  */
237   DEMANGLE_COMPONENT_COVARIANT_THUNK,
238   /* A Java class.  This has one subtree, the type.  */
239   DEMANGLE_COMPONENT_JAVA_CLASS,
240   /* A guard variable.  This has one subtree, the name for which this
241      is a guard variable.  */
242   DEMANGLE_COMPONENT_GUARD,
243   /* A reference temporary.  This has one subtree, the name for which
244      this is a temporary.  */
245   DEMANGLE_COMPONENT_REFTEMP,
246   /* A hidden alias.  This has one subtree, the encoding for which it
247      is providing alternative linkage.  */
248   DEMANGLE_COMPONENT_HIDDEN_ALIAS,
249   /* A standard substitution.  This holds the name of the
250      substitution.  */
251   DEMANGLE_COMPONENT_SUB_STD,
252   /* The restrict qualifier.  The one subtree is the type which is
253      being qualified.  */
254   DEMANGLE_COMPONENT_RESTRICT,
255   /* The volatile qualifier.  The one subtree is the type which is
256      being qualified.  */
257   DEMANGLE_COMPONENT_VOLATILE,
258   /* The const qualifier.  The one subtree is the type which is being
259      qualified.  */
260   DEMANGLE_COMPONENT_CONST,
261   /* The restrict qualifier modifying a member function.  The one
262      subtree is the type which is being qualified.  */
263   DEMANGLE_COMPONENT_RESTRICT_THIS,
264   /* The volatile qualifier modifying a member function.  The one
265      subtree is the type which is being qualified.  */
266   DEMANGLE_COMPONENT_VOLATILE_THIS,
267   /* The const qualifier modifying a member function.  The one subtree
268      is the type which is being qualified.  */
269   DEMANGLE_COMPONENT_CONST_THIS,
270   /* A vendor qualifier.  The left subtree is the type which is being
271      qualified, and the right subtree is the name of the
272      qualifier.  */
273   DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
274   /* A pointer.  The one subtree is the type which is being pointed
275      to.  */
276   DEMANGLE_COMPONENT_POINTER,
277   /* A reference.  The one subtree is the type which is being
278      referenced.  */
279   DEMANGLE_COMPONENT_REFERENCE,
280   /* A complex type.  The one subtree is the base type.  */
281   DEMANGLE_COMPONENT_COMPLEX,
282   /* An imaginary type.  The one subtree is the base type.  */
283   DEMANGLE_COMPONENT_IMAGINARY,
284   /* A builtin type.  This holds the builtin type information.  */
285   DEMANGLE_COMPONENT_BUILTIN_TYPE,
286   /* A vendor's builtin type.  This holds the name of the type.  */
287   DEMANGLE_COMPONENT_VENDOR_TYPE,
288   /* A function type.  The left subtree is the return type.  The right
289      subtree is a list of ARGLIST nodes.  Either or both may be
290      NULL.  */
291   DEMANGLE_COMPONENT_FUNCTION_TYPE,
292   /* An array type.  The left subtree is the dimension, which may be
293      NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
294      expression.  The right subtree is the element type.  */
295   DEMANGLE_COMPONENT_ARRAY_TYPE,
296   /* A pointer to member type.  The left subtree is the class type,
297      and the right subtree is the member type.  CV-qualifiers appear
298      on the latter.  */
299   DEMANGLE_COMPONENT_PTRMEM_TYPE,
300   /* An argument list.  The left subtree is the current argument, and
301      the right subtree is either NULL or another ARGLIST node.  */
302   DEMANGLE_COMPONENT_ARGLIST,
303   /* A template argument list.  The left subtree is the current
304      template argument, and the right subtree is either NULL or
305      another TEMPLATE_ARGLIST node.  */
306   DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
307   /* An operator.  This holds information about a standard
308      operator.  */
309   DEMANGLE_COMPONENT_OPERATOR,
310   /* An extended operator.  This holds the number of arguments, and
311      the name of the extended operator.  */
312   DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
313   /* A typecast, represented as a unary operator.  The one subtree is
314      the type to which the argument should be cast.  */
315   DEMANGLE_COMPONENT_CAST,
316   /* A unary expression.  The left subtree is the operator, and the
317      right subtree is the single argument.  */
318   DEMANGLE_COMPONENT_UNARY,
319   /* A binary expression.  The left subtree is the operator, and the
320      right subtree is a BINARY_ARGS.  */
321   DEMANGLE_COMPONENT_BINARY,
322   /* Arguments to a binary expression.  The left subtree is the first
323      argument, and the right subtree is the second argument.  */
324   DEMANGLE_COMPONENT_BINARY_ARGS,
325   /* A trinary expression.  The left subtree is the operator, and the
326      right subtree is a TRINARY_ARG1.  */
327   DEMANGLE_COMPONENT_TRINARY,
328   /* Arguments to a trinary expression.  The left subtree is the first
329      argument, and the right subtree is a TRINARY_ARG2.  */
330   DEMANGLE_COMPONENT_TRINARY_ARG1,
331   /* More arguments to a trinary expression.  The left subtree is the
332      second argument, and the right subtree is the third argument.  */
333   DEMANGLE_COMPONENT_TRINARY_ARG2,
334   /* A literal.  The left subtree is the type, and the right subtree
335      is the value, represented as a DEMANGLE_COMPONENT_NAME.  */
336   DEMANGLE_COMPONENT_LITERAL,
337   /* A negative literal.  Like LITERAL, but the value is negated.
338      This is a minor hack: the NAME used for LITERAL points directly
339      to the mangled string, but since negative numbers are mangled
340      using 'n' instead of '-', we want a way to indicate a negative
341      number which involves neither modifying the mangled string nor
342      allocating a new copy of the literal in memory.  */
343   DEMANGLE_COMPONENT_LITERAL_NEG
344 };
345 
346 /* Types which are only used internally.  */
347 
348 struct demangle_operator_info;
349 struct demangle_builtin_type_info;
350 
351 /* A node in the tree representation is an instance of a struct
352    demangle_component.  Note that the field names of the struct are
353    not well protected against macros defined by the file including
354    this one.  We can fix this if it ever becomes a problem.  */
355 
356 struct demangle_component
357 {
358   /* The type of this component.  */
359   enum demangle_component_type type;
360 
361   union
362   {
363     /* For DEMANGLE_COMPONENT_NAME.  */
364     struct
365     {
366       /* A pointer to the name (which need not NULL terminated) and
367 	 its length.  */
368       const char *s;
369       int len;
370     } s_name;
371 
372     /* For DEMANGLE_COMPONENT_OPERATOR.  */
373     struct
374     {
375       /* Operator.  */
376       const struct demangle_operator_info *op;
377     } s_operator;
378 
379     /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
380     struct
381     {
382       /* Number of arguments.  */
383       int args;
384       /* Name.  */
385       struct demangle_component *name;
386     } s_extended_operator;
387 
388     /* For DEMANGLE_COMPONENT_CTOR.  */
389     struct
390     {
391       /* Kind of constructor.  */
392       enum gnu_v3_ctor_kinds kind;
393       /* Name.  */
394       struct demangle_component *name;
395     } s_ctor;
396 
397     /* For DEMANGLE_COMPONENT_DTOR.  */
398     struct
399     {
400       /* Kind of destructor.  */
401       enum gnu_v3_dtor_kinds kind;
402       /* Name.  */
403       struct demangle_component *name;
404     } s_dtor;
405 
406     /* For DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
407     struct
408     {
409       /* Builtin type.  */
410       const struct demangle_builtin_type_info *type;
411     } s_builtin;
412 
413     /* For DEMANGLE_COMPONENT_SUB_STD.  */
414     struct
415     {
416       /* Standard substitution string.  */
417       const char* string;
418       /* Length of string.  */
419       int len;
420     } s_string;
421 
422     /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM.  */
423     struct
424     {
425       /* Template parameter index.  */
426       long number;
427     } s_number;
428 
429     /* For other types.  */
430     struct
431     {
432       /* Left (or only) subtree.  */
433       struct demangle_component *left;
434       /* Right subtree.  */
435       struct demangle_component *right;
436     } s_binary;
437 
438   } u;
439 };
440 
441 /* People building mangled trees are expected to allocate instances of
442    struct demangle_component themselves.  They can then call one of
443    the following functions to fill them in.  */
444 
445 /* Fill in most component types with a left subtree and a right
446    subtree.  Returns non-zero on success, zero on failure, such as an
447    unrecognized or inappropriate component type.  */
448 
449 extern int
450 cplus_demangle_fill_component (struct demangle_component *fill,
451                                enum demangle_component_type,
452                                struct demangle_component *left,
453                                struct demangle_component *right);
454 
455 /* Fill in a DEMANGLE_COMPONENT_NAME.  Returns non-zero on success,
456    zero for bad arguments.  */
457 
458 extern int
459 cplus_demangle_fill_name (struct demangle_component *fill,
460                           const char *, int);
461 
462 /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
463    builtin type (e.g., "int", etc.).  Returns non-zero on success,
464    zero if the type is not recognized.  */
465 
466 extern int
467 cplus_demangle_fill_builtin_type (struct demangle_component *fill,
468                                   const char *type_name);
469 
470 /* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
471    operator and the number of arguments which it takes (the latter is
472    used to disambiguate operators which can be both binary and unary,
473    such as '-').  Returns non-zero on success, zero if the operator is
474    not recognized.  */
475 
476 extern int
477 cplus_demangle_fill_operator (struct demangle_component *fill,
478                               const char *opname, int args);
479 
480 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
481    number of arguments and the name.  Returns non-zero on success,
482    zero for bad arguments.  */
483 
484 extern int
485 cplus_demangle_fill_extended_operator (struct demangle_component *fill,
486                                        int numargs,
487                                        struct demangle_component *nm);
488 
489 /* Fill in a DEMANGLE_COMPONENT_CTOR.  Returns non-zero on success,
490    zero for bad arguments.  */
491 
492 extern int
493 cplus_demangle_fill_ctor (struct demangle_component *fill,
494                           enum gnu_v3_ctor_kinds kind,
495                           struct demangle_component *name);
496 
497 /* Fill in a DEMANGLE_COMPONENT_DTOR.  Returns non-zero on success,
498    zero for bad arguments.  */
499 
500 extern int
501 cplus_demangle_fill_dtor (struct demangle_component *fill,
502                           enum gnu_v3_dtor_kinds kind,
503                           struct demangle_component *name);
504 
505 /* This function translates a mangled name into a struct
506    demangle_component tree.  The first argument is the mangled name.
507    The second argument is DMGL_* options.  This returns a pointer to a
508    tree on success, or NULL on failure.  On success, the third
509    argument is set to a block of memory allocated by malloc.  This
510    block should be passed to free when the tree is no longer
511    needed.  */
512 
513 extern struct demangle_component *
514 cplus_demangle_v3_components (const char *mangled, int options, void **mem);
515 
516 /* This function takes a struct demangle_component tree and returns
517    the corresponding demangled string.  The first argument is DMGL_*
518    options.  The second is the tree to demangle.  The third is a guess
519    at the length of the demangled string, used to initially allocate
520    the return buffer.  The fourth is a pointer to a size_t.  On
521    success, this function returns a buffer allocated by malloc(), and
522    sets the size_t pointed to by the fourth argument to the size of
523    the allocated buffer (not the length of the returned string).  On
524    failure, this function returns NULL, and sets the size_t pointed to
525    by the fourth argument to 0 for an invalid tree, or to 1 for a
526    memory allocation error.  */
527 
528 extern char *
529 cplus_demangle_print (int options,
530                       const struct demangle_component *tree,
531                       int estimated_length,
532                       size_t *p_allocated_size);
533 
534 #ifdef __cplusplus
535 }
536 #endif /* __cplusplus */
537 
538 #endif	/* DEMANGLE_H */
539