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