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