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