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