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