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