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