Merge from vendor branch GDB:
[dragonfly.git] / contrib / binutils-2.15 / libiberty / cp-demint.c
1 /* Demangler component interface functions.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@wasabisystems.com>.
4
5    This file is part of the libiberty library, which is part of GCC.
6
7    This file is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    In addition to the permissions in the GNU General Public License, the
13    Free Software Foundation gives you unlimited permission to link the
14    compiled version of this file into combinations with other programs,
15    and to distribute those combinations without any restriction coming
16    from the use of this file.  (The General Public License restrictions
17    do apply in other respects; for example, they cover modification of
18    the file, and distribution when not linked into a combined
19    executable.)
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
29 */
30
31 /* This file implements a few interface functions which are provided
32    for use with struct demangle_component trees.  These functions are
33    declared in demangle.h.  These functions are closely tied to the
34    demangler code in cp-demangle.c, and other interface functions can
35    be found in that file.  We put these functions in a separate file
36    because they are not needed by the demangler, and so we avoid
37    having them pulled in by programs which only need the
38    demangler.  */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #ifdef HAVE_STDLIB_H
45 #include <stdlib.h>
46 #endif
47 #ifdef HAVE_STRING_H
48 #include <string.h>
49 #endif
50
51 #include "ansidecl.h"
52 #include "libiberty.h"
53 #include "demangle.h"
54 #include "cp-demangle.h"
55
56 /* Fill in most component types.  */
57
58 int
59 cplus_demangle_fill_component (p, type, left, right)
60      struct demangle_component *p;
61      enum demangle_component_type type;
62      struct demangle_component *left;
63      struct demangle_component *right;
64 {
65   if (p == NULL)
66     return 0;
67   switch (type)
68     {
69     case DEMANGLE_COMPONENT_QUAL_NAME:
70     case DEMANGLE_COMPONENT_LOCAL_NAME:
71     case DEMANGLE_COMPONENT_TYPED_NAME:
72     case DEMANGLE_COMPONENT_TEMPLATE:
73     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
74     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
75     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
76     case DEMANGLE_COMPONENT_ARRAY_TYPE:
77     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
78     case DEMANGLE_COMPONENT_ARGLIST:
79     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
80     case DEMANGLE_COMPONENT_UNARY:
81     case DEMANGLE_COMPONENT_BINARY:
82     case DEMANGLE_COMPONENT_BINARY_ARGS:
83     case DEMANGLE_COMPONENT_TRINARY:
84     case DEMANGLE_COMPONENT_TRINARY_ARG1:
85     case DEMANGLE_COMPONENT_TRINARY_ARG2:
86     case DEMANGLE_COMPONENT_LITERAL:
87     case DEMANGLE_COMPONENT_LITERAL_NEG:
88       break;
89
90       /* These component types only have one subtree.  */
91     case DEMANGLE_COMPONENT_VTABLE:
92     case DEMANGLE_COMPONENT_VTT:
93     case DEMANGLE_COMPONENT_TYPEINFO:
94     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
95     case DEMANGLE_COMPONENT_TYPEINFO_FN:
96     case DEMANGLE_COMPONENT_THUNK:
97     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
98     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
99     case DEMANGLE_COMPONENT_JAVA_CLASS:
100     case DEMANGLE_COMPONENT_GUARD:
101     case DEMANGLE_COMPONENT_REFTEMP:
102     case DEMANGLE_COMPONENT_RESTRICT:
103     case DEMANGLE_COMPONENT_VOLATILE:
104     case DEMANGLE_COMPONENT_CONST:
105     case DEMANGLE_COMPONENT_RESTRICT_THIS:
106     case DEMANGLE_COMPONENT_VOLATILE_THIS:
107     case DEMANGLE_COMPONENT_CONST_THIS:
108     case DEMANGLE_COMPONENT_POINTER:
109     case DEMANGLE_COMPONENT_REFERENCE:
110     case DEMANGLE_COMPONENT_COMPLEX:
111     case DEMANGLE_COMPONENT_IMAGINARY:
112     case DEMANGLE_COMPONENT_VENDOR_TYPE:
113     case DEMANGLE_COMPONENT_CAST:
114       if (right != NULL)
115         return 0;
116       break;
117
118     default:
119       /* Other types do not use subtrees.  */
120       return 0;
121     }
122
123   p->type = type;
124   p->u.s_binary.left = left;
125   p->u.s_binary.right = right;
126
127   return 1;
128 }
129
130 /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
131
132 int
133 cplus_demangle_fill_builtin_type (p, typename)
134      struct demangle_component *p;
135      const char *typename;
136 {
137   int len;
138   unsigned int i;
139
140   if (p == NULL || typename == NULL)
141     return 0;
142   len = strlen (typename);
143   for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
144     {
145       if (len == cplus_demangle_builtin_types[i].len
146           && strcmp (typename, cplus_demangle_builtin_types[i].name) == 0)
147         {
148           p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
149           p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
150           return 1;
151         }
152     }
153   return 0;
154 }
155
156 /* Fill in a DEMANGLE_COMPONENT_OPERATOR.  */
157
158 int
159 cplus_demangle_fill_operator (p, opname, args)
160      struct demangle_component *p;
161      const char *opname;
162      int args;
163 {
164   int len;
165   unsigned int i;
166
167   if (p == NULL || opname == NULL)
168     return 0;
169   len = strlen (opname);
170   for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
171     {
172       if (len == cplus_demangle_operators[i].len
173           && args == cplus_demangle_operators[i].args
174           && strcmp (opname, cplus_demangle_operators[i].name) == 0)
175         {
176           p->type = DEMANGLE_COMPONENT_OPERATOR;
177           p->u.s_operator.op = &cplus_demangle_operators[i];
178           return 1;
179         }
180     }
181   return 0;
182 }
183
184 /* Translate a mangled name into components.  */
185
186 struct demangle_component *
187 cplus_demangle_v3_components (mangled, options, mem)
188      const char *mangled;
189      int options;
190      void **mem;
191 {
192   size_t len;
193   int type;
194   struct d_info di;
195   struct demangle_component *dc;
196
197   len = strlen (mangled);
198
199   if (mangled[0] == '_' && mangled[1] == 'Z')
200     type = 0;
201   else
202     {
203       if ((options & DMGL_TYPES) == 0)
204         return NULL;
205       type = 1;
206     }
207
208   cplus_demangle_init_info (mangled, options, len, &di);
209
210   di.comps = ((struct demangle_component *)
211               malloc (di.num_comps * sizeof (struct demangle_component)));
212   di.subs = ((struct demangle_component **)
213              malloc (di.num_subs * sizeof (struct demangle_component *)));
214   if (di.comps == NULL || di.subs == NULL)
215     {
216       if (di.comps != NULL)
217         free (di.comps);
218       if (di.subs != NULL)
219         free (di.subs);
220       return NULL;
221     }
222
223   if (! type)
224     dc = cplus_demangle_mangled_name (&di, 1);
225   else
226     dc = cplus_demangle_type (&di);
227
228   /* If DMGL_PARAMS is set, then if we didn't consume the entire
229      mangled string, then we didn't successfully demangle it.  */
230   if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
231     dc = NULL;
232
233   free (di.subs);
234
235   if (dc != NULL)
236     *mem = di.comps;
237   else
238     free (di.comps);
239
240   return dc;
241 }