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