]>
Commit | Line | Data |
---|---|---|
de17c821 | 1 | /* Helper routines for C++ support in GDB. |
213516ef | 2 | Copyright (C) 2002-2023 Free Software Foundation, Inc. |
de17c821 DJ |
3 | |
4 | Contributed by MontaVista Software. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
de17c821 DJ |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
de17c821 DJ |
20 | |
21 | #include "defs.h" | |
22 | #include "cp-support.h" | |
83b6e1f1 | 23 | #include "language.h" |
de17c821 | 24 | #include "demangle.h" |
4de283e4 | 25 | #include "gdbcmd.h" |
b6429628 | 26 | #include "dictionary.h" |
4de283e4 | 27 | #include "objfiles.h" |
b6429628 | 28 | #include "frame.h" |
4de283e4 TT |
29 | #include "symtab.h" |
30 | #include "block.h" | |
31 | #include "complaints.h" | |
362ff856 | 32 | #include "gdbtypes.h" |
4de283e4 TT |
33 | #include "expression.h" |
34 | #include "value.h" | |
35 | #include "cp-abi.h" | |
22cee43f | 36 | #include "namespace.h" |
4de283e4 | 37 | #include <signal.h> |
268a13a5 | 38 | #include "gdbsupport/gdb_setjmp.h" |
e0f4b3ec | 39 | #include "gdbsupport/gdb-safe-ctype.h" |
268a13a5 | 40 | #include "gdbsupport/selftest.h" |
21987b9c | 41 | #include "gdbsupport/gdb-sigmask.h" |
3b3978bc TT |
42 | #include <atomic> |
43 | #include "event-top.h" | |
44 | #include "run-on-main-thread.h" | |
c7d029ea | 45 | #include "typeprint.h" |
99d9c3b9 | 46 | #include "inferior.h" |
f88e9fd3 | 47 | |
fb4c6eba DJ |
48 | #define d_left(dc) (dc)->u.s_binary.left |
49 | #define d_right(dc) (dc)->u.s_binary.right | |
b2a7f303 | 50 | |
fb4c6eba | 51 | /* Functions related to demangled name parsing. */ |
b2a7f303 DC |
52 | |
53 | static unsigned int cp_find_first_component_aux (const char *name, | |
54 | int permissive); | |
55 | ||
56 | static void demangled_name_complaint (const char *name); | |
b6429628 | 57 | |
0891c3cc | 58 | /* Functions related to overload resolution. */ |
b6429628 | 59 | |
8d577d32 | 60 | static void overload_list_add_symbol (struct symbol *sym, |
0891c3cc PA |
61 | const char *oload_name, |
62 | std::vector<symbol *> *overload_list); | |
8d577d32 | 63 | |
0891c3cc PA |
64 | static void add_symbol_overload_list_using |
65 | (const char *func_name, const char *the_namespace, | |
66 | std::vector<symbol *> *overload_list); | |
8d577d32 | 67 | |
0891c3cc PA |
68 | static void add_symbol_overload_list_qualified |
69 | (const char *func_name, | |
70 | std::vector<symbol *> *overload_list); | |
8d577d32 | 71 | |
9219021c DC |
72 | /* The list of "maint cplus" commands. */ |
73 | ||
5c4e30ca | 74 | struct cmd_list_element *maint_cplus_cmd_list = NULL; |
9219021c | 75 | |
3a93a0c2 KS |
76 | static void |
77 | replace_typedefs (struct demangle_parse_info *info, | |
2621e0fd TT |
78 | struct demangle_component *ret_comp, |
79 | canonicalization_ftype *finder, | |
80 | void *data); | |
3a93a0c2 | 81 | |
cb2cd8cb PA |
82 | static struct demangle_component * |
83 | gdb_cplus_demangle_v3_components (const char *mangled, | |
84 | int options, void **mem); | |
85 | ||
3a93a0c2 KS |
86 | /* A convenience function to copy STRING into OBSTACK, returning a pointer |
87 | to the newly allocated string and saving the number of bytes saved in LEN. | |
88 | ||
89 | It does not copy the terminating '\0' byte! */ | |
90 | ||
91 | static char * | |
92 | copy_string_to_obstack (struct obstack *obstack, const char *string, | |
93 | long *len) | |
94 | { | |
95 | *len = strlen (string); | |
224c3ddb | 96 | return (char *) obstack_copy (obstack, string, *len); |
3a93a0c2 KS |
97 | } |
98 | ||
f88e9fd3 DJ |
99 | /* Return 1 if STRING is clearly already in canonical form. This |
100 | function is conservative; things which it does not recognize are | |
101 | assumed to be non-canonical, and the parser will sort them out | |
102 | afterwards. This speeds up the critical path for alphanumeric | |
103 | identifiers. */ | |
104 | ||
105 | static int | |
106 | cp_already_canonical (const char *string) | |
107 | { | |
108 | /* Identifier start character [a-zA-Z_]. */ | |
109 | if (!ISIDST (string[0])) | |
110 | return 0; | |
111 | ||
112 | /* These are the only two identifiers which canonicalize to other | |
113 | than themselves or an error: unsigned -> unsigned int and | |
114 | signed -> int. */ | |
115 | if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) | |
116 | return 0; | |
117 | else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) | |
118 | return 0; | |
119 | ||
120 | /* Identifier character [a-zA-Z0-9_]. */ | |
121 | while (ISIDNUM (string[1])) | |
122 | string++; | |
123 | ||
124 | if (string[1] == '\0') | |
125 | return 1; | |
126 | else | |
127 | return 0; | |
128 | } | |
9219021c | 129 | |
3a93a0c2 KS |
130 | /* Inspect the given RET_COMP for its type. If it is a typedef, |
131 | replace the node with the typedef's tree. | |
132 | ||
133 | Returns 1 if any typedef substitutions were made, 0 otherwise. */ | |
134 | ||
135 | static int | |
136 | inspect_type (struct demangle_parse_info *info, | |
2621e0fd TT |
137 | struct demangle_component *ret_comp, |
138 | canonicalization_ftype *finder, | |
139 | void *data) | |
3a93a0c2 | 140 | { |
3a93a0c2 KS |
141 | char *name; |
142 | struct symbol *sym; | |
3a93a0c2 KS |
143 | |
144 | /* Copy the symbol's name from RET_COMP and look it up | |
145 | in the symbol table. */ | |
146 | name = (char *) alloca (ret_comp->u.s_name.len + 1); | |
147 | memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len); | |
148 | name[ret_comp->u.s_name.len] = '\0'; | |
149 | ||
3a93a0c2 | 150 | sym = NULL; |
3a93a0c2 | 151 | |
a70b8144 | 152 | try |
492d29ea | 153 | { |
d12307c1 | 154 | sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol; |
492d29ea | 155 | } |
230d2906 | 156 | catch (const gdb_exception &except) |
492d29ea PA |
157 | { |
158 | return 0; | |
159 | } | |
492d29ea PA |
160 | |
161 | if (sym != NULL) | |
3a93a0c2 | 162 | { |
5f9c5a63 | 163 | struct type *otype = sym->type (); |
3a93a0c2 | 164 | |
2621e0fd TT |
165 | if (finder != NULL) |
166 | { | |
167 | const char *new_name = (*finder) (otype, data); | |
168 | ||
169 | if (new_name != NULL) | |
170 | { | |
171 | ret_comp->u.s_name.s = new_name; | |
172 | ret_comp->u.s_name.len = strlen (new_name); | |
173 | return 1; | |
174 | } | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
74921315 | 179 | /* If the type is a typedef or namespace alias, replace it. */ |
78134374 SM |
180 | if (otype->code () == TYPE_CODE_TYPEDEF |
181 | || otype->code () == TYPE_CODE_NAMESPACE) | |
3a93a0c2 KS |
182 | { |
183 | long len; | |
184 | int is_anon; | |
185 | struct type *type; | |
c8b23b3f | 186 | std::unique_ptr<demangle_parse_info> i; |
3a93a0c2 KS |
187 | |
188 | /* Get the real type of the typedef. */ | |
189 | type = check_typedef (otype); | |
190 | ||
725cbb63 KS |
191 | /* If the symbol name is the same as the original type name, |
192 | don't substitute. That would cause infinite recursion in | |
193 | symbol lookups, as the typedef symbol is often the first | |
194 | found symbol in the symbol table. | |
195 | ||
196 | However, this can happen in a number of situations, such as: | |
197 | ||
198 | If the symbol is a namespace and its type name is no different | |
74921315 | 199 | than the name we looked up, this symbol is not a namespace |
725cbb63 KS |
200 | alias and does not need to be substituted. |
201 | ||
202 | If the symbol is typedef and its type name is the same | |
203 | as the symbol's name, e.g., "typedef struct foo foo;". */ | |
7d93a1e0 SM |
204 | if (type->name () != nullptr |
205 | && strcmp (type->name (), name) == 0) | |
74921315 KS |
206 | return 0; |
207 | ||
7d93a1e0 | 208 | is_anon = (type->name () == NULL |
78134374 SM |
209 | && (type->code () == TYPE_CODE_ENUM |
210 | || type->code () == TYPE_CODE_STRUCT | |
211 | || type->code () == TYPE_CODE_UNION)); | |
3a93a0c2 KS |
212 | if (is_anon) |
213 | { | |
214 | struct type *last = otype; | |
215 | ||
216 | /* Find the last typedef for the type. */ | |
27710edb SM |
217 | while (last->target_type () != NULL |
218 | && (last->target_type ()->code () | |
3a93a0c2 | 219 | == TYPE_CODE_TYPEDEF)) |
27710edb | 220 | last = last->target_type (); |
3a93a0c2 KS |
221 | |
222 | /* If there is only one typedef for this anonymous type, | |
223 | do not substitute it. */ | |
224 | if (type == otype) | |
225 | return 0; | |
226 | else | |
227 | /* Use the last typedef seen as the type for this | |
228 | anonymous type. */ | |
229 | type = last; | |
230 | } | |
231 | ||
d7e74731 | 232 | string_file buf; |
a70b8144 | 233 | try |
d7e74731 | 234 | { |
c7d029ea PA |
235 | /* Avoid using the current language. If the language is |
236 | C, and TYPE is a struct/class, the printed type is | |
237 | prefixed with "struct " or "class ", which we don't | |
238 | want when we're expanding a C++ typedef. Print using | |
239 | the type symbol's language to expand a C++ typedef | |
240 | the C++ way even if the current language is C. */ | |
241 | const language_defn *lang = language_def (sym->language ()); | |
242 | lang->print_type (type, "", &buf, -1, 0, &type_print_raw_options); | |
d7e74731 | 243 | } |
3a93a0c2 KS |
244 | /* If type_print threw an exception, there is little point |
245 | in continuing, so just bow out gracefully. */ | |
230d2906 | 246 | catch (const gdb_exception_error &except) |
3a93a0c2 | 247 | { |
3a93a0c2 KS |
248 | return 0; |
249 | } | |
250 | ||
d7e74731 | 251 | len = buf.size (); |
efba19b0 | 252 | name = obstack_strdup (&info->obstack, buf.string ()); |
3a93a0c2 KS |
253 | |
254 | /* Turn the result into a new tree. Note that this | |
255 | tree will contain pointers into NAME, so NAME cannot | |
256 | be free'd until all typedef conversion is done and | |
257 | the final result is converted into a string. */ | |
258 | i = cp_demangled_name_to_comp (name, NULL); | |
259 | if (i != NULL) | |
260 | { | |
261 | /* Merge the two trees. */ | |
c8b23b3f | 262 | cp_merge_demangle_parse_infos (info, ret_comp, i.get ()); |
3a93a0c2 KS |
263 | |
264 | /* Replace any newly introduced typedefs -- but not | |
265 | if the type is anonymous (that would lead to infinite | |
266 | looping). */ | |
267 | if (!is_anon) | |
2621e0fd | 268 | replace_typedefs (info, ret_comp, finder, data); |
3a93a0c2 KS |
269 | } |
270 | else | |
271 | { | |
272 | /* This shouldn't happen unless the type printer has | |
273 | output something that the name parser cannot grok. | |
274 | Nonetheless, an ounce of prevention... | |
275 | ||
276 | Canonicalize the name again, and store it in the | |
277 | current node (RET_COMP). */ | |
596dc4ad TT |
278 | gdb::unique_xmalloc_ptr<char> canon |
279 | = cp_canonicalize_string_no_typedefs (name); | |
3a93a0c2 | 280 | |
596dc4ad | 281 | if (canon != nullptr) |
3a93a0c2 | 282 | { |
2f408ecb | 283 | /* Copy the canonicalization into the obstack. */ |
596dc4ad | 284 | name = copy_string_to_obstack (&info->obstack, canon.get (), &len); |
3a93a0c2 KS |
285 | } |
286 | ||
287 | ret_comp->u.s_name.s = name; | |
288 | ret_comp->u.s_name.len = len; | |
289 | } | |
290 | ||
291 | return 1; | |
292 | } | |
293 | } | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
f68f85b5 PA |
298 | /* Helper for replace_typedefs_qualified_name to handle |
299 | DEMANGLE_COMPONENT_TEMPLATE. TMPL is the template node. BUF is | |
300 | the buffer that holds the qualified name being built by | |
301 | replace_typedefs_qualified_name. REPL is the node that will be | |
302 | rewritten as a DEMANGLE_COMPONENT_NAME node holding the 'template | |
303 | plus template arguments' name with typedefs replaced. */ | |
304 | ||
305 | static bool | |
306 | replace_typedefs_template (struct demangle_parse_info *info, | |
307 | string_file &buf, | |
308 | struct demangle_component *tmpl, | |
309 | struct demangle_component *repl, | |
310 | canonicalization_ftype *finder, | |
311 | void *data) | |
312 | { | |
313 | demangle_component *tmpl_arglist = d_right (tmpl); | |
314 | ||
315 | /* Replace typedefs in the template argument list. */ | |
316 | replace_typedefs (info, tmpl_arglist, finder, data); | |
317 | ||
318 | /* Convert 'template + replaced template argument list' to a string | |
319 | and replace the REPL node. */ | |
320 | gdb::unique_xmalloc_ptr<char> tmpl_str = cp_comp_to_string (tmpl, 100); | |
321 | if (tmpl_str == nullptr) | |
322 | { | |
323 | /* If something went astray, abort typedef substitutions. */ | |
324 | return false; | |
325 | } | |
326 | buf.puts (tmpl_str.get ()); | |
327 | ||
328 | repl->type = DEMANGLE_COMPONENT_NAME; | |
329 | repl->u.s_name.s = obstack_strdup (&info->obstack, buf.string ()); | |
330 | repl->u.s_name.len = buf.size (); | |
331 | return true; | |
332 | } | |
333 | ||
3a93a0c2 KS |
334 | /* Replace any typedefs appearing in the qualified name |
335 | (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse | |
336 | given in INFO. */ | |
337 | ||
338 | static void | |
339 | replace_typedefs_qualified_name (struct demangle_parse_info *info, | |
2621e0fd TT |
340 | struct demangle_component *ret_comp, |
341 | canonicalization_ftype *finder, | |
342 | void *data) | |
3a93a0c2 | 343 | { |
d7e74731 | 344 | string_file buf; |
3a93a0c2 KS |
345 | struct demangle_component *comp = ret_comp; |
346 | ||
347 | /* Walk each node of the qualified name, reconstructing the name of | |
348 | this element. With every node, check for any typedef substitutions. | |
349 | If a substitution has occurred, replace the qualified name node | |
350 | with a DEMANGLE_COMPONENT_NAME node representing the new, typedef- | |
351 | substituted name. */ | |
352 | while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME) | |
353 | { | |
f68f85b5 PA |
354 | if (d_left (comp)->type == DEMANGLE_COMPONENT_TEMPLATE) |
355 | { | |
356 | /* Convert 'template + replaced template argument list' to a | |
357 | string and replace the top DEMANGLE_COMPONENT_QUAL_NAME | |
358 | node. */ | |
359 | if (!replace_typedefs_template (info, buf, | |
360 | d_left (comp), d_left (ret_comp), | |
361 | finder, data)) | |
362 | return; | |
363 | ||
364 | buf.clear (); | |
365 | d_right (ret_comp) = d_right (comp); | |
366 | comp = ret_comp; | |
367 | ||
368 | /* Fallback to DEMANGLE_COMPONENT_NAME processing. We want | |
369 | to call inspect_type for this template, in case we have a | |
370 | template alias, like: | |
371 | template<typename T> using alias = base<int, t>; | |
372 | in which case we want inspect_type to do a replacement like: | |
373 | alias<int> -> base<int, int> | |
374 | */ | |
375 | } | |
376 | ||
3a93a0c2 KS |
377 | if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME) |
378 | { | |
fe978cb0 | 379 | struct demangle_component newobj; |
3a93a0c2 | 380 | |
d7e74731 | 381 | buf.write (d_left (comp)->u.s_name.s, d_left (comp)->u.s_name.len); |
fe978cb0 | 382 | newobj.type = DEMANGLE_COMPONENT_NAME; |
efba19b0 | 383 | newobj.u.s_name.s = obstack_strdup (&info->obstack, buf.string ()); |
29592bde | 384 | newobj.u.s_name.len = buf.size (); |
fe978cb0 | 385 | if (inspect_type (info, &newobj, finder, data)) |
3a93a0c2 | 386 | { |
29592bde | 387 | char *s; |
3a93a0c2 KS |
388 | long slen; |
389 | ||
390 | /* A typedef was substituted in NEW. Convert it to a | |
391 | string and replace the top DEMANGLE_COMPONENT_QUAL_NAME | |
392 | node. */ | |
393 | ||
d7e74731 | 394 | buf.clear (); |
29592bde PA |
395 | gdb::unique_xmalloc_ptr<char> n |
396 | = cp_comp_to_string (&newobj, 100); | |
3a93a0c2 KS |
397 | if (n == NULL) |
398 | { | |
399 | /* If something went astray, abort typedef substitutions. */ | |
3a93a0c2 KS |
400 | return; |
401 | } | |
402 | ||
29592bde | 403 | s = copy_string_to_obstack (&info->obstack, n.get (), &slen); |
3a93a0c2 KS |
404 | |
405 | d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME; | |
406 | d_left (ret_comp)->u.s_name.s = s; | |
407 | d_left (ret_comp)->u.s_name.len = slen; | |
408 | d_right (ret_comp) = d_right (comp); | |
409 | comp = ret_comp; | |
410 | continue; | |
411 | } | |
412 | } | |
413 | else | |
414 | { | |
415 | /* The current node is not a name, so simply replace any | |
416 | typedefs in it. Then print it to the stream to continue | |
417 | checking for more typedefs in the tree. */ | |
2621e0fd | 418 | replace_typedefs (info, d_left (comp), finder, data); |
29592bde PA |
419 | gdb::unique_xmalloc_ptr<char> name |
420 | = cp_comp_to_string (d_left (comp), 100); | |
3a93a0c2 KS |
421 | if (name == NULL) |
422 | { | |
423 | /* If something went astray, abort typedef substitutions. */ | |
3a93a0c2 KS |
424 | return; |
425 | } | |
29592bde | 426 | buf.puts (name.get ()); |
3a93a0c2 | 427 | } |
2621e0fd | 428 | |
d7e74731 | 429 | buf.write ("::", 2); |
3a93a0c2 KS |
430 | comp = d_right (comp); |
431 | } | |
432 | ||
f68f85b5 PA |
433 | /* If the next component is DEMANGLE_COMPONENT_TEMPLATE or |
434 | DEMANGLE_COMPONENT_NAME, save the qualified name assembled above | |
435 | and append the name given by COMP. Then use this reassembled | |
436 | name to check for a typedef. */ | |
3a93a0c2 | 437 | |
f68f85b5 PA |
438 | if (comp->type == DEMANGLE_COMPONENT_TEMPLATE) |
439 | { | |
440 | /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node with a | |
441 | DEMANGLE_COMPONENT_NAME node containing the whole name. */ | |
442 | if (!replace_typedefs_template (info, buf, comp, ret_comp, finder, data)) | |
443 | return; | |
444 | inspect_type (info, ret_comp, finder, data); | |
445 | } | |
446 | else if (comp->type == DEMANGLE_COMPONENT_NAME) | |
3a93a0c2 | 447 | { |
d7e74731 | 448 | buf.write (comp->u.s_name.s, comp->u.s_name.len); |
3a93a0c2 KS |
449 | |
450 | /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node | |
451 | with a DEMANGLE_COMPONENT_NAME node containing the whole | |
452 | name. */ | |
453 | ret_comp->type = DEMANGLE_COMPONENT_NAME; | |
efba19b0 | 454 | ret_comp->u.s_name.s = obstack_strdup (&info->obstack, buf.string ()); |
29592bde | 455 | ret_comp->u.s_name.len = buf.size (); |
2621e0fd | 456 | inspect_type (info, ret_comp, finder, data); |
3a93a0c2 KS |
457 | } |
458 | else | |
2621e0fd | 459 | replace_typedefs (info, comp, finder, data); |
3a93a0c2 KS |
460 | } |
461 | ||
462 | ||
463 | /* A function to check const and volatile qualifiers for argument types. | |
464 | ||
465 | "Parameter declarations that differ only in the presence | |
466 | or absence of `const' and/or `volatile' are equivalent." | |
467 | C++ Standard N3290, clause 13.1.3 #4. */ | |
468 | ||
469 | static void | |
470 | check_cv_qualifiers (struct demangle_component *ret_comp) | |
471 | { | |
472 | while (d_left (ret_comp) != NULL | |
473 | && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST | |
474 | || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE)) | |
475 | { | |
476 | d_left (ret_comp) = d_left (d_left (ret_comp)); | |
477 | } | |
478 | } | |
479 | ||
480 | /* Walk the parse tree given by RET_COMP, replacing any typedefs with | |
481 | their basic types. */ | |
482 | ||
483 | static void | |
484 | replace_typedefs (struct demangle_parse_info *info, | |
2621e0fd TT |
485 | struct demangle_component *ret_comp, |
486 | canonicalization_ftype *finder, | |
487 | void *data) | |
3a93a0c2 KS |
488 | { |
489 | if (ret_comp) | |
490 | { | |
2621e0fd TT |
491 | if (finder != NULL |
492 | && (ret_comp->type == DEMANGLE_COMPONENT_NAME | |
493 | || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME | |
494 | || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE | |
495 | || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)) | |
496 | { | |
29592bde PA |
497 | gdb::unique_xmalloc_ptr<char> local_name |
498 | = cp_comp_to_string (ret_comp, 10); | |
2621e0fd TT |
499 | |
500 | if (local_name != NULL) | |
501 | { | |
492d29ea | 502 | struct symbol *sym = NULL; |
2621e0fd TT |
503 | |
504 | sym = NULL; | |
a70b8144 | 505 | try |
2621e0fd | 506 | { |
29592bde PA |
507 | sym = lookup_symbol (local_name.get (), 0, |
508 | VAR_DOMAIN, 0).symbol; | |
2621e0fd | 509 | } |
230d2906 | 510 | catch (const gdb_exception &except) |
492d29ea PA |
511 | { |
512 | } | |
492d29ea | 513 | |
492d29ea | 514 | if (sym != NULL) |
2621e0fd | 515 | { |
5f9c5a63 | 516 | struct type *otype = sym->type (); |
2621e0fd TT |
517 | const char *new_name = (*finder) (otype, data); |
518 | ||
519 | if (new_name != NULL) | |
520 | { | |
521 | ret_comp->type = DEMANGLE_COMPONENT_NAME; | |
522 | ret_comp->u.s_name.s = new_name; | |
523 | ret_comp->u.s_name.len = strlen (new_name); | |
524 | return; | |
525 | } | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
3a93a0c2 KS |
530 | switch (ret_comp->type) |
531 | { | |
532 | case DEMANGLE_COMPONENT_ARGLIST: | |
533 | check_cv_qualifiers (ret_comp); | |
534 | /* Fall through */ | |
535 | ||
536 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: | |
537 | case DEMANGLE_COMPONENT_TEMPLATE: | |
538 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: | |
539 | case DEMANGLE_COMPONENT_TYPED_NAME: | |
2621e0fd TT |
540 | replace_typedefs (info, d_left (ret_comp), finder, data); |
541 | replace_typedefs (info, d_right (ret_comp), finder, data); | |
3a93a0c2 KS |
542 | break; |
543 | ||
544 | case DEMANGLE_COMPONENT_NAME: | |
2621e0fd | 545 | inspect_type (info, ret_comp, finder, data); |
3a93a0c2 KS |
546 | break; |
547 | ||
548 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
2621e0fd | 549 | replace_typedefs_qualified_name (info, ret_comp, finder, data); |
3a93a0c2 KS |
550 | break; |
551 | ||
552 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
553 | case DEMANGLE_COMPONENT_CTOR: | |
554 | case DEMANGLE_COMPONENT_ARRAY_TYPE: | |
555 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: | |
2621e0fd | 556 | replace_typedefs (info, d_right (ret_comp), finder, data); |
3a93a0c2 KS |
557 | break; |
558 | ||
559 | case DEMANGLE_COMPONENT_CONST: | |
560 | case DEMANGLE_COMPONENT_RESTRICT: | |
561 | case DEMANGLE_COMPONENT_VOLATILE: | |
562 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
563 | case DEMANGLE_COMPONENT_CONST_THIS: | |
564 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
565 | case DEMANGLE_COMPONENT_POINTER: | |
566 | case DEMANGLE_COMPONENT_REFERENCE: | |
e4347c89 | 567 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
2621e0fd | 568 | replace_typedefs (info, d_left (ret_comp), finder, data); |
3a93a0c2 KS |
569 | break; |
570 | ||
571 | default: | |
572 | break; | |
573 | } | |
574 | } | |
575 | } | |
576 | ||
2f408ecb PA |
577 | /* Parse STRING and convert it to canonical form, resolving any |
578 | typedefs. If parsing fails, or if STRING is already canonical, | |
596dc4ad | 579 | return nullptr. Otherwise return the canonical form. If |
2f408ecb PA |
580 | FINDER is not NULL, then type components are passed to FINDER to be |
581 | looked up. DATA is passed verbatim to FINDER. */ | |
3a93a0c2 | 582 | |
596dc4ad | 583 | gdb::unique_xmalloc_ptr<char> |
2621e0fd TT |
584 | cp_canonicalize_string_full (const char *string, |
585 | canonicalization_ftype *finder, | |
586 | void *data) | |
3a93a0c2 | 587 | { |
3a93a0c2 | 588 | unsigned int estimated_len; |
c8b23b3f | 589 | std::unique_ptr<demangle_parse_info> info; |
3a93a0c2 | 590 | |
3a93a0c2 KS |
591 | estimated_len = strlen (string) * 2; |
592 | info = cp_demangled_name_to_comp (string, NULL); | |
593 | if (info != NULL) | |
594 | { | |
595 | /* Replace all the typedefs in the tree. */ | |
c8b23b3f | 596 | replace_typedefs (info.get (), info->tree, finder, data); |
3a93a0c2 KS |
597 | |
598 | /* Convert the tree back into a string. */ | |
29592bde PA |
599 | gdb::unique_xmalloc_ptr<char> us = cp_comp_to_string (info->tree, |
600 | estimated_len); | |
e88e8651 | 601 | gdb_assert (us); |
3a93a0c2 | 602 | |
3a93a0c2 KS |
603 | /* Finally, compare the original string with the computed |
604 | name, returning NULL if they are the same. */ | |
596dc4ad TT |
605 | if (strcmp (us.get (), string) == 0) |
606 | return nullptr; | |
607 | ||
608 | return us; | |
3a93a0c2 KS |
609 | } |
610 | ||
596dc4ad | 611 | return nullptr; |
3a93a0c2 KS |
612 | } |
613 | ||
2621e0fd TT |
614 | /* Like cp_canonicalize_string_full, but always passes NULL for |
615 | FINDER. */ | |
616 | ||
596dc4ad | 617 | gdb::unique_xmalloc_ptr<char> |
2621e0fd TT |
618 | cp_canonicalize_string_no_typedefs (const char *string) |
619 | { | |
620 | return cp_canonicalize_string_full (string, NULL, NULL); | |
621 | } | |
622 | ||
f88e9fd3 | 623 | /* Parse STRING and convert it to canonical form. If parsing fails, |
596dc4ad | 624 | or if STRING is already canonical, return nullptr. |
2f408ecb | 625 | Otherwise return the canonical form. */ |
9219021c | 626 | |
596dc4ad | 627 | gdb::unique_xmalloc_ptr<char> |
fb4c6eba DJ |
628 | cp_canonicalize_string (const char *string) |
629 | { | |
c8b23b3f | 630 | std::unique_ptr<demangle_parse_info> info; |
f88e9fd3 | 631 | unsigned int estimated_len; |
9219021c | 632 | |
f88e9fd3 | 633 | if (cp_already_canonical (string)) |
596dc4ad | 634 | return nullptr; |
9219021c | 635 | |
3a93a0c2 KS |
636 | info = cp_demangled_name_to_comp (string, NULL); |
637 | if (info == NULL) | |
596dc4ad | 638 | return nullptr; |
9219021c | 639 | |
f88e9fd3 | 640 | estimated_len = strlen (string) * 2; |
e88e8651 YQ |
641 | gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree, |
642 | estimated_len)); | |
9219021c | 643 | |
e88e8651 | 644 | if (!us) |
9934703b JK |
645 | { |
646 | warning (_("internal error: string \"%s\" failed to be canonicalized"), | |
647 | string); | |
596dc4ad | 648 | return nullptr; |
9934703b JK |
649 | } |
650 | ||
596dc4ad TT |
651 | if (strcmp (us.get (), string) == 0) |
652 | return nullptr; | |
de17c821 | 653 | |
596dc4ad | 654 | return us; |
fb4c6eba | 655 | } |
de17c821 | 656 | |
aff410f1 MS |
657 | /* Convert a mangled name to a demangle_component tree. *MEMORY is |
658 | set to the block of used memory that should be freed when finished | |
659 | with the tree. DEMANGLED_P is set to the char * that should be | |
660 | freed when finished with the tree, or NULL if none was needed. | |
661 | OPTIONS will be passed to the demangler. */ | |
de17c821 | 662 | |
c8b23b3f | 663 | static std::unique_ptr<demangle_parse_info> |
fb4c6eba | 664 | mangled_name_to_comp (const char *mangled_name, int options, |
3456e70c TT |
665 | void **memory, |
666 | gdb::unique_xmalloc_ptr<char> *demangled_p) | |
de17c821 | 667 | { |
fb4c6eba DJ |
668 | /* If it looks like a v3 mangled name, then try to go directly |
669 | to trees. */ | |
670 | if (mangled_name[0] == '_' && mangled_name[1] == 'Z') | |
de17c821 | 671 | { |
3a93a0c2 KS |
672 | struct demangle_component *ret; |
673 | ||
cb2cd8cb PA |
674 | ret = gdb_cplus_demangle_v3_components (mangled_name, |
675 | options, memory); | |
fb4c6eba DJ |
676 | if (ret) |
677 | { | |
0b72cde3 | 678 | auto info = gdb::make_unique<demangle_parse_info> (); |
3a93a0c2 | 679 | info->tree = ret; |
fb4c6eba | 680 | *demangled_p = NULL; |
3a93a0c2 | 681 | return info; |
fb4c6eba | 682 | } |
de17c821 DJ |
683 | } |
684 | ||
aff410f1 MS |
685 | /* If it doesn't, or if that failed, then try to demangle the |
686 | name. */ | |
3456e70c TT |
687 | gdb::unique_xmalloc_ptr<char> demangled_name = gdb_demangle (mangled_name, |
688 | options); | |
fb4c6eba DJ |
689 | if (demangled_name == NULL) |
690 | return NULL; | |
691 | ||
aff410f1 MS |
692 | /* If we could demangle the name, parse it to build the component |
693 | tree. */ | |
c8b23b3f | 694 | std::unique_ptr<demangle_parse_info> info |
3456e70c | 695 | = cp_demangled_name_to_comp (demangled_name.get (), NULL); |
de17c821 | 696 | |
3a93a0c2 | 697 | if (info == NULL) |
3456e70c | 698 | return NULL; |
de17c821 | 699 | |
3456e70c | 700 | *demangled_p = std::move (demangled_name); |
3a93a0c2 | 701 | return info; |
de17c821 DJ |
702 | } |
703 | ||
704 | /* Return the name of the class containing method PHYSNAME. */ | |
705 | ||
706 | char * | |
31c27f77 | 707 | cp_class_name_from_physname (const char *physname) |
de17c821 | 708 | { |
de237128 | 709 | void *storage = NULL; |
3456e70c | 710 | gdb::unique_xmalloc_ptr<char> demangled_name; |
29592bde | 711 | gdb::unique_xmalloc_ptr<char> ret; |
5e5100cb | 712 | struct demangle_component *ret_comp, *prev_comp, *cur_comp; |
c8b23b3f | 713 | std::unique_ptr<demangle_parse_info> info; |
fb4c6eba DJ |
714 | int done; |
715 | ||
3a93a0c2 KS |
716 | info = mangled_name_to_comp (physname, DMGL_ANSI, |
717 | &storage, &demangled_name); | |
718 | if (info == NULL) | |
de17c821 DJ |
719 | return NULL; |
720 | ||
fb4c6eba | 721 | done = 0; |
3a93a0c2 | 722 | ret_comp = info->tree; |
5e5100cb | 723 | |
aff410f1 MS |
724 | /* First strip off any qualifiers, if we have a function or |
725 | method. */ | |
fb4c6eba DJ |
726 | while (!done) |
727 | switch (ret_comp->type) | |
728 | { | |
fb4c6eba DJ |
729 | case DEMANGLE_COMPONENT_CONST: |
730 | case DEMANGLE_COMPONENT_RESTRICT: | |
731 | case DEMANGLE_COMPONENT_VOLATILE: | |
732 | case DEMANGLE_COMPONENT_CONST_THIS: | |
733 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
734 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
735 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
dda83cd7 SM |
736 | ret_comp = d_left (ret_comp); |
737 | break; | |
5e5100cb DJ |
738 | default: |
739 | done = 1; | |
740 | break; | |
741 | } | |
742 | ||
743 | /* If what we have now is a function, discard the argument list. */ | |
744 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
745 | ret_comp = d_left (ret_comp); | |
746 | ||
747 | /* If what we have now is a template, strip off the template | |
748 | arguments. The left subtree may be a qualified name. */ | |
749 | if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) | |
750 | ret_comp = d_left (ret_comp); | |
751 | ||
aff410f1 MS |
752 | /* What we have now should be a name, possibly qualified. |
753 | Additional qualifiers could live in the left subtree or the right | |
754 | subtree. Find the last piece. */ | |
5e5100cb DJ |
755 | done = 0; |
756 | prev_comp = NULL; | |
757 | cur_comp = ret_comp; | |
758 | while (!done) | |
759 | switch (cur_comp->type) | |
760 | { | |
761 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
762 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
763 | prev_comp = cur_comp; | |
dda83cd7 SM |
764 | cur_comp = d_right (cur_comp); |
765 | break; | |
fb4c6eba | 766 | case DEMANGLE_COMPONENT_TEMPLATE: |
5e5100cb | 767 | case DEMANGLE_COMPONENT_NAME: |
fb4c6eba DJ |
768 | case DEMANGLE_COMPONENT_CTOR: |
769 | case DEMANGLE_COMPONENT_DTOR: | |
770 | case DEMANGLE_COMPONENT_OPERATOR: | |
771 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
772 | done = 1; | |
773 | break; | |
774 | default: | |
775 | done = 1; | |
5e5100cb | 776 | cur_comp = NULL; |
fb4c6eba DJ |
777 | break; |
778 | } | |
779 | ||
5e5100cb | 780 | if (cur_comp != NULL && prev_comp != NULL) |
de17c821 | 781 | { |
5e5100cb | 782 | /* We want to discard the rightmost child of PREV_COMP. */ |
fb4c6eba | 783 | *prev_comp = *d_left (prev_comp); |
aff410f1 MS |
784 | /* The ten is completely arbitrary; we don't have a good |
785 | estimate. */ | |
5e5100cb | 786 | ret = cp_comp_to_string (ret_comp, 10); |
de17c821 DJ |
787 | } |
788 | ||
fb4c6eba | 789 | xfree (storage); |
29592bde | 790 | return ret.release (); |
de17c821 DJ |
791 | } |
792 | ||
aff410f1 MS |
793 | /* Return the child of COMP which is the basename of a method, |
794 | variable, et cetera. All scope qualifiers are discarded, but | |
795 | template arguments will be included. The component tree may be | |
796 | modified. */ | |
de17c821 | 797 | |
5e5100cb DJ |
798 | static struct demangle_component * |
799 | unqualified_name_from_comp (struct demangle_component *comp) | |
de17c821 | 800 | { |
5e5100cb | 801 | struct demangle_component *ret_comp = comp, *last_template; |
fb4c6eba DJ |
802 | int done; |
803 | ||
fb4c6eba | 804 | done = 0; |
5e5100cb | 805 | last_template = NULL; |
fb4c6eba DJ |
806 | while (!done) |
807 | switch (ret_comp->type) | |
808 | { | |
809 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
810 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
dda83cd7 SM |
811 | ret_comp = d_right (ret_comp); |
812 | break; | |
5e5100cb | 813 | case DEMANGLE_COMPONENT_TYPED_NAME: |
dda83cd7 SM |
814 | ret_comp = d_left (ret_comp); |
815 | break; | |
5e5100cb DJ |
816 | case DEMANGLE_COMPONENT_TEMPLATE: |
817 | gdb_assert (last_template == NULL); | |
818 | last_template = ret_comp; | |
819 | ret_comp = d_left (ret_comp); | |
820 | break; | |
fb4c6eba DJ |
821 | case DEMANGLE_COMPONENT_CONST: |
822 | case DEMANGLE_COMPONENT_RESTRICT: | |
823 | case DEMANGLE_COMPONENT_VOLATILE: | |
824 | case DEMANGLE_COMPONENT_CONST_THIS: | |
825 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
826 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
827 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
dda83cd7 SM |
828 | ret_comp = d_left (ret_comp); |
829 | break; | |
fb4c6eba | 830 | case DEMANGLE_COMPONENT_NAME: |
fb4c6eba DJ |
831 | case DEMANGLE_COMPONENT_CTOR: |
832 | case DEMANGLE_COMPONENT_DTOR: | |
833 | case DEMANGLE_COMPONENT_OPERATOR: | |
834 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
835 | done = 1; | |
836 | break; | |
837 | default: | |
5e5100cb | 838 | return NULL; |
fb4c6eba DJ |
839 | break; |
840 | } | |
841 | ||
5e5100cb DJ |
842 | if (last_template) |
843 | { | |
844 | d_left (last_template) = ret_comp; | |
845 | return last_template; | |
846 | } | |
847 | ||
848 | return ret_comp; | |
849 | } | |
850 | ||
851 | /* Return the name of the method whose linkage name is PHYSNAME. */ | |
852 | ||
853 | char * | |
854 | method_name_from_physname (const char *physname) | |
855 | { | |
de237128 | 856 | void *storage = NULL; |
3456e70c | 857 | gdb::unique_xmalloc_ptr<char> demangled_name; |
29592bde | 858 | gdb::unique_xmalloc_ptr<char> ret; |
5e5100cb | 859 | struct demangle_component *ret_comp; |
c8b23b3f | 860 | std::unique_ptr<demangle_parse_info> info; |
5e5100cb | 861 | |
3a93a0c2 KS |
862 | info = mangled_name_to_comp (physname, DMGL_ANSI, |
863 | &storage, &demangled_name); | |
864 | if (info == NULL) | |
5e5100cb DJ |
865 | return NULL; |
866 | ||
3a93a0c2 | 867 | ret_comp = unqualified_name_from_comp (info->tree); |
5e5100cb | 868 | |
fb4c6eba | 869 | if (ret_comp != NULL) |
aff410f1 MS |
870 | /* The ten is completely arbitrary; we don't have a good |
871 | estimate. */ | |
fb4c6eba DJ |
872 | ret = cp_comp_to_string (ret_comp, 10); |
873 | ||
874 | xfree (storage); | |
29592bde | 875 | return ret.release (); |
fb4c6eba | 876 | } |
de17c821 | 877 | |
5e5100cb DJ |
878 | /* If FULL_NAME is the demangled name of a C++ function (including an |
879 | arg list, possibly including namespace/class qualifications), | |
880 | return a new string containing only the function name (without the | |
06d3e5b0 | 881 | arg list/class qualifications). Otherwise, return NULL. */ |
5e5100cb | 882 | |
06d3e5b0 | 883 | gdb::unique_xmalloc_ptr<char> |
5e5100cb DJ |
884 | cp_func_name (const char *full_name) |
885 | { | |
29592bde | 886 | gdb::unique_xmalloc_ptr<char> ret; |
5e5100cb | 887 | struct demangle_component *ret_comp; |
c8b23b3f | 888 | std::unique_ptr<demangle_parse_info> info; |
5e5100cb | 889 | |
3a93a0c2 KS |
890 | info = cp_demangled_name_to_comp (full_name, NULL); |
891 | if (!info) | |
06d3e5b0 | 892 | return nullptr; |
5e5100cb | 893 | |
3a93a0c2 | 894 | ret_comp = unqualified_name_from_comp (info->tree); |
5e5100cb | 895 | |
5e5100cb DJ |
896 | if (ret_comp != NULL) |
897 | ret = cp_comp_to_string (ret_comp, 10); | |
898 | ||
06d3e5b0 | 899 | return ret; |
5e5100cb DJ |
900 | } |
901 | ||
c62446b1 PA |
902 | /* Helper for cp_remove_params. DEMANGLED_NAME is the name of a |
903 | function, including parameters and (optionally) a return type. | |
904 | Return the name of the function without parameters or return type, | |
905 | or NULL if we can not parse the name. If REQUIRE_PARAMS is false, | |
906 | then tolerate a non-existing or unbalanced parameter list. */ | |
5e5100cb | 907 | |
c62446b1 PA |
908 | static gdb::unique_xmalloc_ptr<char> |
909 | cp_remove_params_1 (const char *demangled_name, bool require_params) | |
5e5100cb | 910 | { |
109483d9 | 911 | bool done = false; |
5e5100cb | 912 | struct demangle_component *ret_comp; |
c8b23b3f | 913 | std::unique_ptr<demangle_parse_info> info; |
29592bde | 914 | gdb::unique_xmalloc_ptr<char> ret; |
5e5100cb DJ |
915 | |
916 | if (demangled_name == NULL) | |
917 | return NULL; | |
918 | ||
3a93a0c2 KS |
919 | info = cp_demangled_name_to_comp (demangled_name, NULL); |
920 | if (info == NULL) | |
5e5100cb DJ |
921 | return NULL; |
922 | ||
923 | /* First strip off any qualifiers, if we have a function or method. */ | |
3a93a0c2 | 924 | ret_comp = info->tree; |
5e5100cb DJ |
925 | while (!done) |
926 | switch (ret_comp->type) | |
927 | { | |
928 | case DEMANGLE_COMPONENT_CONST: | |
929 | case DEMANGLE_COMPONENT_RESTRICT: | |
930 | case DEMANGLE_COMPONENT_VOLATILE: | |
931 | case DEMANGLE_COMPONENT_CONST_THIS: | |
932 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
933 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
934 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
dda83cd7 SM |
935 | ret_comp = d_left (ret_comp); |
936 | break; | |
5e5100cb | 937 | default: |
109483d9 | 938 | done = true; |
5e5100cb DJ |
939 | break; |
940 | } | |
941 | ||
942 | /* What we have now should be a function. Return its name. */ | |
943 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
944 | ret = cp_comp_to_string (d_left (ret_comp), 10); | |
c62446b1 PA |
945 | else if (!require_params |
946 | && (ret_comp->type == DEMANGLE_COMPONENT_NAME | |
947 | || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME | |
948 | || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)) | |
949 | ret = cp_comp_to_string (ret_comp, 10); | |
5e5100cb | 950 | |
109483d9 | 951 | return ret; |
5e5100cb DJ |
952 | } |
953 | ||
c62446b1 PA |
954 | /* DEMANGLED_NAME is the name of a function, including parameters and |
955 | (optionally) a return type. Return the name of the function | |
956 | without parameters or return type, or NULL if we can not parse the | |
957 | name. */ | |
958 | ||
959 | gdb::unique_xmalloc_ptr<char> | |
960 | cp_remove_params (const char *demangled_name) | |
961 | { | |
962 | return cp_remove_params_1 (demangled_name, true); | |
963 | } | |
964 | ||
965 | /* See cp-support.h. */ | |
966 | ||
967 | gdb::unique_xmalloc_ptr<char> | |
968 | cp_remove_params_if_any (const char *demangled_name, bool completion_mode) | |
969 | { | |
970 | /* Trying to remove parameters from the empty string fails. If | |
971 | we're completing / matching everything, avoid returning NULL | |
972 | which would make callers interpret the result as an error. */ | |
973 | if (demangled_name[0] == '\0' && completion_mode) | |
b02f78f9 | 974 | return make_unique_xstrdup (""); |
c62446b1 PA |
975 | |
976 | gdb::unique_xmalloc_ptr<char> without_params | |
977 | = cp_remove_params_1 (demangled_name, false); | |
978 | ||
979 | if (without_params == NULL && completion_mode) | |
980 | { | |
981 | std::string copy = demangled_name; | |
982 | ||
983 | while (!copy.empty ()) | |
984 | { | |
985 | copy.pop_back (); | |
986 | without_params = cp_remove_params_1 (copy.c_str (), false); | |
987 | if (without_params != NULL) | |
988 | break; | |
989 | } | |
990 | } | |
991 | ||
992 | return without_params; | |
993 | } | |
994 | ||
fb4c6eba DJ |
995 | /* Here are some random pieces of trivia to keep in mind while trying |
996 | to take apart demangled names: | |
de17c821 | 997 | |
fb4c6eba DJ |
998 | - Names can contain function arguments or templates, so the process |
999 | has to be, to some extent recursive: maybe keep track of your | |
1000 | depth based on encountering <> and (). | |
1001 | ||
1002 | - Parentheses don't just have to happen at the end of a name: they | |
1003 | can occur even if the name in question isn't a function, because | |
1004 | a template argument might be a type that's a function. | |
1005 | ||
1006 | - Conversely, even if you're trying to deal with a function, its | |
1007 | demangled name might not end with ')': it could be a const or | |
1008 | volatile class method, in which case it ends with "const" or | |
1009 | "volatile". | |
1010 | ||
1011 | - Parentheses are also used in anonymous namespaces: a variable | |
1012 | 'foo' in an anonymous namespace gets demangled as "(anonymous | |
1013 | namespace)::foo". | |
1014 | ||
1015 | - And operator names can contain parentheses or angle brackets. */ | |
1016 | ||
1017 | /* FIXME: carlton/2003-03-13: We have several functions here with | |
1018 | overlapping functionality; can we combine them? Also, do they | |
1019 | handle all the above considerations correctly? */ | |
de17c821 | 1020 | |
9219021c DC |
1021 | |
1022 | /* This returns the length of first component of NAME, which should be | |
1023 | the demangled name of a C++ variable/function/method/etc. | |
1024 | Specifically, it returns the index of the first colon forming the | |
1025 | boundary of the first component: so, given 'A::foo' or 'A::B::foo' | |
1026 | it returns the 1, and given 'foo', it returns 0. */ | |
1027 | ||
b2a7f303 DC |
1028 | /* The character in NAME indexed by the return value is guaranteed to |
1029 | always be either ':' or '\0'. */ | |
9219021c DC |
1030 | |
1031 | /* NOTE: carlton/2003-03-13: This function is currently only intended | |
1032 | for internal use: it's probably not entirely safe when called on | |
b2a7f303 DC |
1033 | user-generated input, because some of the 'index += 2' lines in |
1034 | cp_find_first_component_aux might go past the end of malformed | |
1035 | input. */ | |
1036 | ||
1037 | unsigned int | |
1038 | cp_find_first_component (const char *name) | |
1039 | { | |
1040 | return cp_find_first_component_aux (name, 0); | |
1041 | } | |
1042 | ||
1043 | /* Helper function for cp_find_first_component. Like that function, | |
1044 | it returns the length of the first component of NAME, but to make | |
1045 | the recursion easier, it also stops if it reaches an unexpected ')' | |
1046 | or '>' if the value of PERMISSIVE is nonzero. */ | |
9219021c | 1047 | |
b2a7f303 DC |
1048 | static unsigned int |
1049 | cp_find_first_component_aux (const char *name, int permissive) | |
9219021c | 1050 | { |
9219021c | 1051 | unsigned int index = 0; |
0f20eeea DC |
1052 | /* Operator names can show up in unexpected places. Since these can |
1053 | contain parentheses or angle brackets, they can screw up the | |
1054 | recursion. But not every string 'operator' is part of an | |
30baf67b | 1055 | operator name: e.g. you could have a variable 'cooperator'. So |
0f20eeea DC |
1056 | this variable tells us whether or not we should treat the string |
1057 | 'operator' as starting an operator. */ | |
1058 | int operator_possible = 1; | |
9219021c DC |
1059 | |
1060 | for (;; ++index) | |
1061 | { | |
1062 | switch (name[index]) | |
1063 | { | |
1064 | case '<': | |
1065 | /* Template; eat it up. The calls to cp_first_component | |
1066 | should only return (I hope!) when they reach the '>' | |
1067 | terminating the component or a '::' between two | |
1068 | components. (Hence the '+ 2'.) */ | |
1069 | index += 1; | |
b2a7f303 | 1070 | for (index += cp_find_first_component_aux (name + index, 1); |
9219021c | 1071 | name[index] != '>'; |
b2a7f303 | 1072 | index += cp_find_first_component_aux (name + index, 1)) |
9219021c | 1073 | { |
b2a7f303 DC |
1074 | if (name[index] != ':') |
1075 | { | |
1076 | demangled_name_complaint (name); | |
1077 | return strlen (name); | |
1078 | } | |
9219021c DC |
1079 | index += 2; |
1080 | } | |
0f20eeea | 1081 | operator_possible = 1; |
9219021c DC |
1082 | break; |
1083 | case '(': | |
1084 | /* Similar comment as to '<'. */ | |
1085 | index += 1; | |
b2a7f303 | 1086 | for (index += cp_find_first_component_aux (name + index, 1); |
9219021c | 1087 | name[index] != ')'; |
b2a7f303 | 1088 | index += cp_find_first_component_aux (name + index, 1)) |
9219021c | 1089 | { |
b2a7f303 DC |
1090 | if (name[index] != ':') |
1091 | { | |
1092 | demangled_name_complaint (name); | |
1093 | return strlen (name); | |
1094 | } | |
9219021c DC |
1095 | index += 2; |
1096 | } | |
0f20eeea | 1097 | operator_possible = 1; |
9219021c DC |
1098 | break; |
1099 | case '>': | |
1100 | case ')': | |
b2a7f303 | 1101 | if (permissive) |
7a20f2c2 | 1102 | return index; |
b2a7f303 DC |
1103 | else |
1104 | { | |
1105 | demangled_name_complaint (name); | |
1106 | return strlen (name); | |
1107 | } | |
9219021c | 1108 | case '\0': |
9219021c | 1109 | return index; |
1cafadb4 DB |
1110 | case ':': |
1111 | /* ':' marks a component iff the next character is also a ':'. | |
1112 | Otherwise it is probably malformed input. */ | |
1113 | if (name[index + 1] == ':') | |
1114 | return index; | |
1115 | break; | |
0f20eeea DC |
1116 | case 'o': |
1117 | /* Operator names can screw up the recursion. */ | |
1118 | if (operator_possible | |
8090b426 | 1119 | && startswith (name + index, CP_OPERATOR_STR)) |
0f20eeea | 1120 | { |
8090b426 | 1121 | index += CP_OPERATOR_LEN; |
f88e9fd3 | 1122 | while (ISSPACE(name[index])) |
0f20eeea DC |
1123 | ++index; |
1124 | switch (name[index]) | |
1125 | { | |
cf325299 PA |
1126 | case '\0': |
1127 | return index; | |
0f20eeea DC |
1128 | /* Skip over one less than the appropriate number of |
1129 | characters: the for loop will skip over the last | |
1130 | one. */ | |
1131 | case '<': | |
1132 | if (name[index + 1] == '<') | |
1133 | index += 1; | |
1134 | else | |
1135 | index += 0; | |
1136 | break; | |
1137 | case '>': | |
1138 | case '-': | |
1139 | if (name[index + 1] == '>') | |
1140 | index += 1; | |
1141 | else | |
1142 | index += 0; | |
1143 | break; | |
1144 | case '(': | |
1145 | index += 1; | |
1146 | break; | |
1147 | default: | |
1148 | index += 0; | |
1149 | break; | |
1150 | } | |
1151 | } | |
1152 | operator_possible = 0; | |
1153 | break; | |
1154 | case ' ': | |
1155 | case ',': | |
1156 | case '.': | |
1157 | case '&': | |
1158 | case '*': | |
1159 | /* NOTE: carlton/2003-04-18: I'm not sure what the precise | |
1160 | set of relevant characters are here: it's necessary to | |
1161 | include any character that can show up before 'operator' | |
1162 | in a demangled name, and it's safe to include any | |
1163 | character that can't be part of an identifier's name. */ | |
1164 | operator_possible = 1; | |
1165 | break; | |
9219021c | 1166 | default: |
0f20eeea | 1167 | operator_possible = 0; |
9219021c DC |
1168 | break; |
1169 | } | |
1170 | } | |
1171 | } | |
1172 | ||
b2a7f303 DC |
1173 | /* Complain about a demangled name that we don't know how to parse. |
1174 | NAME is the demangled name in question. */ | |
1175 | ||
1176 | static void | |
1177 | demangled_name_complaint (const char *name) | |
1178 | { | |
b98664d3 | 1179 | complaint ("unexpected demangled name '%s'", name); |
b2a7f303 DC |
1180 | } |
1181 | ||
9219021c DC |
1182 | /* If NAME is the fully-qualified name of a C++ |
1183 | function/variable/method/etc., this returns the length of its | |
1184 | entire prefix: all of the namespaces and classes that make up its | |
1185 | name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns | |
1186 | 4, given 'foo', it returns 0. */ | |
1187 | ||
1188 | unsigned int | |
1189 | cp_entire_prefix_len (const char *name) | |
1190 | { | |
1191 | unsigned int current_len = cp_find_first_component (name); | |
1192 | unsigned int previous_len = 0; | |
1193 | ||
1194 | while (name[current_len] != '\0') | |
1195 | { | |
1196 | gdb_assert (name[current_len] == ':'); | |
1197 | previous_len = current_len; | |
1198 | /* Skip the '::'. */ | |
1199 | current_len += 2; | |
1200 | current_len += cp_find_first_component (name + current_len); | |
1201 | } | |
1202 | ||
1203 | return previous_len; | |
1204 | } | |
1205 | ||
b6429628 DC |
1206 | /* Overload resolution functions. */ |
1207 | ||
8d577d32 | 1208 | /* Test to see if SYM is a symbol that we haven't seen corresponding |
0891c3cc PA |
1209 | to a function named OLOAD_NAME. If so, add it to |
1210 | OVERLOAD_LIST. */ | |
b6429628 DC |
1211 | |
1212 | static void | |
aff410f1 | 1213 | overload_list_add_symbol (struct symbol *sym, |
0891c3cc PA |
1214 | const char *oload_name, |
1215 | std::vector<symbol *> *overload_list) | |
b6429628 | 1216 | { |
aff410f1 MS |
1217 | /* If there is no type information, we can't do anything, so |
1218 | skip. */ | |
5f9c5a63 | 1219 | if (sym->type () == NULL) |
b6429628 DC |
1220 | return; |
1221 | ||
aff410f1 | 1222 | /* skip any symbols that we've already considered. */ |
0891c3cc | 1223 | for (symbol *listed_sym : *overload_list) |
987012b8 | 1224 | if (strcmp (sym->linkage_name (), listed_sym->linkage_name ()) == 0) |
b6429628 DC |
1225 | return; |
1226 | ||
1227 | /* Get the demangled name without parameters */ | |
0891c3cc | 1228 | gdb::unique_xmalloc_ptr<char> sym_name |
987012b8 | 1229 | = cp_remove_params (sym->natural_name ()); |
b6429628 DC |
1230 | if (!sym_name) |
1231 | return; | |
1232 | ||
1233 | /* skip symbols that cannot match */ | |
109483d9 PA |
1234 | if (strcmp (sym_name.get (), oload_name) != 0) |
1235 | return; | |
b6429628 | 1236 | |
0891c3cc | 1237 | overload_list->push_back (sym); |
b6429628 DC |
1238 | } |
1239 | ||
1240 | /* Return a null-terminated list of pointers to function symbols that | |
8d577d32 | 1241 | are named FUNC_NAME and are visible within NAMESPACE. */ |
b6429628 | 1242 | |
0891c3cc | 1243 | struct std::vector<symbol *> |
8d577d32 | 1244 | make_symbol_overload_list (const char *func_name, |
fe978cb0 | 1245 | const char *the_namespace) |
b6429628 | 1246 | { |
245040d7 | 1247 | const char *name; |
0891c3cc | 1248 | std::vector<symbol *> overload_list; |
b6429628 | 1249 | |
0891c3cc | 1250 | overload_list.reserve (100); |
8d577d32 | 1251 | |
0891c3cc | 1252 | add_symbol_overload_list_using (func_name, the_namespace, &overload_list); |
8d577d32 | 1253 | |
fe978cb0 | 1254 | if (the_namespace[0] == '\0') |
245040d7 SW |
1255 | name = func_name; |
1256 | else | |
1257 | { | |
1258 | char *concatenated_name | |
224c3ddb | 1259 | = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1); |
fe978cb0 | 1260 | strcpy (concatenated_name, the_namespace); |
245040d7 SW |
1261 | strcat (concatenated_name, "::"); |
1262 | strcat (concatenated_name, func_name); | |
1263 | name = concatenated_name; | |
1264 | } | |
1265 | ||
0891c3cc PA |
1266 | add_symbol_overload_list_qualified (name, &overload_list); |
1267 | return overload_list; | |
8d577d32 DC |
1268 | } |
1269 | ||
245040d7 SW |
1270 | /* Add all symbols with a name matching NAME in BLOCK to the overload |
1271 | list. */ | |
1272 | ||
1273 | static void | |
0891c3cc PA |
1274 | add_symbol_overload_list_block (const char *name, |
1275 | const struct block *block, | |
1276 | std::vector<symbol *> *overload_list) | |
245040d7 | 1277 | { |
b5ec771e PA |
1278 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
1279 | ||
a1b29426 | 1280 | for (struct symbol *sym : block_iterator_range (block, &lookup_name)) |
0891c3cc | 1281 | overload_list_add_symbol (sym, name, overload_list); |
245040d7 SW |
1282 | } |
1283 | ||
7322dca9 SW |
1284 | /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ |
1285 | ||
1286 | static void | |
0891c3cc PA |
1287 | add_symbol_overload_list_namespace (const char *func_name, |
1288 | const char *the_namespace, | |
1289 | std::vector<symbol *> *overload_list) | |
7322dca9 | 1290 | { |
245040d7 SW |
1291 | const char *name; |
1292 | const struct block *block = NULL; | |
1293 | ||
fe978cb0 | 1294 | if (the_namespace[0] == '\0') |
245040d7 | 1295 | name = func_name; |
7322dca9 SW |
1296 | else |
1297 | { | |
1298 | char *concatenated_name | |
224c3ddb | 1299 | = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1); |
c5504eaf | 1300 | |
fe978cb0 | 1301 | strcpy (concatenated_name, the_namespace); |
7322dca9 SW |
1302 | strcat (concatenated_name, "::"); |
1303 | strcat (concatenated_name, func_name); | |
245040d7 | 1304 | name = concatenated_name; |
7322dca9 | 1305 | } |
245040d7 SW |
1306 | |
1307 | /* Look in the static block. */ | |
78004096 | 1308 | block = get_selected_block (0); |
d24e14a0 | 1309 | block = block == nullptr ? nullptr : block->static_block (); |
8f14fd11 TT |
1310 | if (block != nullptr) |
1311 | { | |
1312 | add_symbol_overload_list_block (name, block, overload_list); | |
245040d7 | 1313 | |
8f14fd11 | 1314 | /* Look in the global block. */ |
d24e14a0 | 1315 | block = block->global_block (); |
8f14fd11 TT |
1316 | if (block) |
1317 | add_symbol_overload_list_block (name, block, overload_list); | |
1318 | } | |
7322dca9 SW |
1319 | } |
1320 | ||
aff410f1 MS |
1321 | /* Search the namespace of the given type and namespace of and public |
1322 | base types. */ | |
7322dca9 SW |
1323 | |
1324 | static void | |
0891c3cc PA |
1325 | add_symbol_overload_list_adl_namespace (struct type *type, |
1326 | const char *func_name, | |
1327 | std::vector<symbol *> *overload_list) | |
7322dca9 | 1328 | { |
fe978cb0 | 1329 | char *the_namespace; |
0d5cff50 | 1330 | const char *type_name; |
7322dca9 SW |
1331 | int i, prefix_len; |
1332 | ||
809f3be1 | 1333 | while (type->is_pointer_or_reference () |
dda83cd7 SM |
1334 | || type->code () == TYPE_CODE_ARRAY |
1335 | || type->code () == TYPE_CODE_TYPEDEF) | |
7322dca9 | 1336 | { |
78134374 SM |
1337 | if (type->code () == TYPE_CODE_TYPEDEF) |
1338 | type = check_typedef (type); | |
7322dca9 | 1339 | else |
27710edb | 1340 | type = type->target_type (); |
7322dca9 SW |
1341 | } |
1342 | ||
7d93a1e0 | 1343 | type_name = type->name (); |
7322dca9 | 1344 | |
7d3fe98e SW |
1345 | if (type_name == NULL) |
1346 | return; | |
1347 | ||
7322dca9 SW |
1348 | prefix_len = cp_entire_prefix_len (type_name); |
1349 | ||
1350 | if (prefix_len != 0) | |
1351 | { | |
224c3ddb | 1352 | the_namespace = (char *) alloca (prefix_len + 1); |
fe978cb0 PA |
1353 | strncpy (the_namespace, type_name, prefix_len); |
1354 | the_namespace[prefix_len] = '\0'; | |
7322dca9 | 1355 | |
0891c3cc PA |
1356 | add_symbol_overload_list_namespace (func_name, the_namespace, |
1357 | overload_list); | |
7322dca9 SW |
1358 | } |
1359 | ||
1360 | /* Check public base type */ | |
78134374 | 1361 | if (type->code () == TYPE_CODE_STRUCT) |
7322dca9 SW |
1362 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) |
1363 | { | |
1364 | if (BASETYPE_VIA_PUBLIC (type, i)) | |
0891c3cc PA |
1365 | add_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, i), |
1366 | func_name, | |
1367 | overload_list); | |
7322dca9 SW |
1368 | } |
1369 | } | |
1370 | ||
0891c3cc PA |
1371 | /* Adds to OVERLOAD_LIST the overload list overload candidates for |
1372 | FUNC_NAME found through argument dependent lookup. */ | |
7322dca9 | 1373 | |
0891c3cc PA |
1374 | void |
1375 | add_symbol_overload_list_adl (gdb::array_view<type *> arg_types, | |
1376 | const char *func_name, | |
1377 | std::vector<symbol *> *overload_list) | |
7322dca9 | 1378 | { |
0891c3cc PA |
1379 | for (type *arg_type : arg_types) |
1380 | add_symbol_overload_list_adl_namespace (arg_type, func_name, | |
1381 | overload_list); | |
7322dca9 SW |
1382 | } |
1383 | ||
8d577d32 DC |
1384 | /* This applies the using directives to add namespaces to search in, |
1385 | and then searches for overloads in all of those namespaces. It | |
1386 | adds the symbols found to sym_return_val. Arguments are as in | |
1387 | make_symbol_overload_list. */ | |
1388 | ||
1389 | static void | |
0891c3cc PA |
1390 | add_symbol_overload_list_using (const char *func_name, |
1391 | const char *the_namespace, | |
1392 | std::vector<symbol *> *overload_list) | |
8d577d32 | 1393 | { |
19c0c0f8 | 1394 | struct using_direct *current; |
4c3376c8 | 1395 | const struct block *block; |
8d577d32 DC |
1396 | |
1397 | /* First, go through the using directives. If any of them apply, | |
1398 | look in the appropriate namespaces for new functions to match | |
1399 | on. */ | |
b6429628 | 1400 | |
4c3376c8 SW |
1401 | for (block = get_selected_block (0); |
1402 | block != NULL; | |
f135fe72 | 1403 | block = block->superblock ()) |
3c45e9f9 | 1404 | for (current = block->get_using (); |
4c3376c8 SW |
1405 | current != NULL; |
1406 | current = current->next) | |
1407 | { | |
19c0c0f8 UW |
1408 | /* Prevent recursive calls. */ |
1409 | if (current->searched) | |
1410 | continue; | |
1411 | ||
dda83cd7 | 1412 | /* If this is a namespace alias or imported declaration ignore |
aff410f1 | 1413 | it. */ |
dda83cd7 SM |
1414 | if (current->alias != NULL || current->declaration != NULL) |
1415 | continue; | |
4c3376c8 | 1416 | |
dda83cd7 | 1417 | if (strcmp (the_namespace, current->import_dest) == 0) |
19c0c0f8 | 1418 | { |
aff410f1 MS |
1419 | /* Mark this import as searched so that the recursive call |
1420 | does not search it again. */ | |
167b0be1 TT |
1421 | scoped_restore reset_directive_searched |
1422 | = make_scoped_restore (¤t->searched, 1); | |
19c0c0f8 | 1423 | |
0891c3cc PA |
1424 | add_symbol_overload_list_using (func_name, |
1425 | current->import_src, | |
1426 | overload_list); | |
19c0c0f8 | 1427 | } |
4c3376c8 | 1428 | } |
b6429628 | 1429 | |
8d577d32 | 1430 | /* Now, add names for this namespace. */ |
0891c3cc PA |
1431 | add_symbol_overload_list_namespace (func_name, the_namespace, |
1432 | overload_list); | |
8d577d32 | 1433 | } |
b6429628 | 1434 | |
8d577d32 DC |
1435 | /* This does the bulk of the work of finding overloaded symbols. |
1436 | FUNC_NAME is the name of the overloaded function we're looking for | |
1437 | (possibly including namespace info). */ | |
b6429628 | 1438 | |
8d577d32 | 1439 | static void |
0891c3cc PA |
1440 | add_symbol_overload_list_qualified (const char *func_name, |
1441 | std::vector<symbol *> *overload_list) | |
8d577d32 | 1442 | { |
63d609de | 1443 | const struct block *surrounding_static_block = 0; |
b6429628 | 1444 | |
aff410f1 MS |
1445 | /* Look through the partial symtabs for all symbols which begin by |
1446 | matching FUNC_NAME. Make sure we read that symbol table in. */ | |
b6429628 | 1447 | |
2030c079 | 1448 | for (objfile *objf : current_program_space->objfiles ()) |
4d080b46 | 1449 | objf->expand_symtabs_for_function (func_name); |
b6429628 DC |
1450 | |
1451 | /* Search upwards from currently selected frame (so that we can | |
1452 | complete on local vars. */ | |
1453 | ||
63d609de SM |
1454 | for (const block *b = get_selected_block (0); |
1455 | b != nullptr; | |
1456 | b = b->superblock ()) | |
0891c3cc | 1457 | add_symbol_overload_list_block (func_name, b, overload_list); |
b6429628 | 1458 | |
d24e14a0 | 1459 | surrounding_static_block = get_selected_block (0); |
78004096 TT |
1460 | surrounding_static_block = (surrounding_static_block == nullptr |
1461 | ? nullptr | |
d24e14a0 | 1462 | : surrounding_static_block->static_block ()); |
8d577d32 | 1463 | |
b6429628 DC |
1464 | /* Go through the symtabs and check the externs and statics for |
1465 | symbols which match. */ | |
1466 | ||
2aab2438 | 1467 | const block *block = get_selected_block (0); |
46baa3c6 | 1468 | struct objfile *current_objfile = block ? block->objfile () : nullptr; |
2aab2438 MM |
1469 | |
1470 | gdbarch_iterate_over_objfiles_in_search_order | |
99d9c3b9 | 1471 | (current_objfile ? current_objfile->arch () : current_inferior ()->arch (), |
2aab2438 MM |
1472 | [func_name, surrounding_static_block, &overload_list] |
1473 | (struct objfile *obj) | |
1474 | { | |
1475 | for (compunit_symtab *cust : obj->compunits ()) | |
1476 | { | |
1477 | QUIT; | |
1478 | const struct block *b = cust->blockvector ()->global_block (); | |
1479 | add_symbol_overload_list_block (func_name, b, overload_list); | |
1480 | ||
1481 | b = cust->blockvector ()->static_block (); | |
1482 | /* Don't do this block twice. */ | |
1483 | if (b == surrounding_static_block) | |
1484 | continue; | |
1485 | ||
1486 | add_symbol_overload_list_block (func_name, b, overload_list); | |
1487 | } | |
1488 | ||
1489 | return 0; | |
1490 | }, current_objfile); | |
8d577d32 DC |
1491 | } |
1492 | ||
aff410f1 | 1493 | /* Lookup the rtti type for a class name. */ |
362ff856 MC |
1494 | |
1495 | struct type * | |
582942f4 | 1496 | cp_lookup_rtti_type (const char *name, const struct block *block) |
362ff856 MC |
1497 | { |
1498 | struct symbol * rtti_sym; | |
1499 | struct type * rtti_type; | |
1500 | ||
82c7be31 DE |
1501 | /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417. |
1502 | Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */ | |
d12307c1 | 1503 | rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol; |
362ff856 MC |
1504 | |
1505 | if (rtti_sym == NULL) | |
1506 | { | |
8a3fe4f8 | 1507 | warning (_("RTTI symbol not found for class '%s'"), name); |
362ff856 MC |
1508 | return NULL; |
1509 | } | |
1510 | ||
66d7f48f | 1511 | if (rtti_sym->aclass () != LOC_TYPEDEF) |
362ff856 | 1512 | { |
8a3fe4f8 | 1513 | warning (_("RTTI symbol for class '%s' is not a type"), name); |
362ff856 MC |
1514 | return NULL; |
1515 | } | |
1516 | ||
5f9c5a63 | 1517 | rtti_type = check_typedef (rtti_sym->type ()); |
362ff856 | 1518 | |
78134374 | 1519 | switch (rtti_type->code ()) |
362ff856 | 1520 | { |
4753d33b | 1521 | case TYPE_CODE_STRUCT: |
362ff856 MC |
1522 | break; |
1523 | case TYPE_CODE_NAMESPACE: | |
1524 | /* chastain/2003-11-26: the symbol tables often contain fake | |
1525 | symbols for namespaces with the same name as the struct. | |
1526 | This warning is an indication of a bug in the lookup order | |
1527 | or a bug in the way that the symbol tables are populated. */ | |
8a3fe4f8 | 1528 | warning (_("RTTI symbol for class '%s' is a namespace"), name); |
362ff856 MC |
1529 | return NULL; |
1530 | default: | |
8a3fe4f8 | 1531 | warning (_("RTTI symbol for class '%s' has bad type"), name); |
362ff856 MC |
1532 | return NULL; |
1533 | } | |
1534 | ||
1535 | return rtti_type; | |
1536 | } | |
b6429628 | 1537 | |
992c7d70 GB |
1538 | #ifdef HAVE_WORKING_FORK |
1539 | ||
491144b5 | 1540 | /* If true, attempt to catch crashes in the demangler and print |
992c7d70 GB |
1541 | useful debugging information. */ |
1542 | ||
491144b5 | 1543 | static bool catch_demangler_crashes = true; |
992c7d70 | 1544 | |
992c7d70 GB |
1545 | /* Stack context and environment for demangler crash recovery. */ |
1546 | ||
3b3978bc | 1547 | static thread_local SIGJMP_BUF *gdb_demangle_jmp_buf; |
992c7d70 | 1548 | |
3b3978bc | 1549 | /* If true, attempt to dump core from the signal handler. */ |
992c7d70 | 1550 | |
3b3978bc | 1551 | static std::atomic<bool> gdb_demangle_attempt_core_dump; |
992c7d70 GB |
1552 | |
1553 | /* Signal handler for gdb_demangle. */ | |
1554 | ||
1555 | static void | |
1556 | gdb_demangle_signal_handler (int signo) | |
1557 | { | |
1558 | if (gdb_demangle_attempt_core_dump) | |
1559 | { | |
1560 | if (fork () == 0) | |
1561 | dump_core (); | |
1562 | ||
3b3978bc | 1563 | gdb_demangle_attempt_core_dump = false; |
992c7d70 GB |
1564 | } |
1565 | ||
3b3978bc TT |
1566 | SIGLONGJMP (*gdb_demangle_jmp_buf, signo); |
1567 | } | |
1568 | ||
1569 | /* A helper for gdb_demangle that reports a demangling failure. */ | |
1570 | ||
1571 | static void | |
1572 | report_failed_demangle (const char *name, bool core_dump_allowed, | |
1573 | int crash_signal) | |
1574 | { | |
1575 | static bool error_reported = false; | |
1576 | ||
1577 | if (!error_reported) | |
1578 | { | |
1579 | std::string short_msg | |
1580 | = string_printf (_("unable to demangle '%s' " | |
1581 | "(demangler failed with signal %d)"), | |
1582 | name, crash_signal); | |
1583 | ||
1584 | std::string long_msg | |
1585 | = string_printf ("%s:%d: %s: %s", __FILE__, __LINE__, | |
1586 | "demangler-warning", short_msg.c_str ()); | |
1587 | ||
1588 | target_terminal::scoped_restore_terminal_state term_state; | |
1589 | target_terminal::ours_for_output (); | |
1590 | ||
1591 | begin_line (); | |
1592 | if (core_dump_allowed) | |
6cb06a8c TT |
1593 | gdb_printf (gdb_stderr, |
1594 | _("%s\nAttempting to dump core.\n"), | |
1595 | long_msg.c_str ()); | |
3b3978bc TT |
1596 | else |
1597 | warn_cant_dump_core (long_msg.c_str ()); | |
1598 | ||
1599 | demangler_warning (__FILE__, __LINE__, "%s", short_msg.c_str ()); | |
1600 | ||
1601 | error_reported = true; | |
1602 | } | |
992c7d70 GB |
1603 | } |
1604 | ||
1605 | #endif | |
1606 | ||
8de20a37 TT |
1607 | /* A wrapper for bfd_demangle. */ |
1608 | ||
3456e70c | 1609 | gdb::unique_xmalloc_ptr<char> |
8de20a37 TT |
1610 | gdb_demangle (const char *name, int options) |
1611 | { | |
3456e70c | 1612 | gdb::unique_xmalloc_ptr<char> result; |
992c7d70 GB |
1613 | int crash_signal = 0; |
1614 | ||
1615 | #ifdef HAVE_WORKING_FORK | |
fece451c CB |
1616 | scoped_segv_handler_restore restore_segv |
1617 | (catch_demangler_crashes | |
1618 | ? gdb_demangle_signal_handler | |
1619 | : nullptr); | |
3b3978bc TT |
1620 | |
1621 | bool core_dump_allowed = gdb_demangle_attempt_core_dump; | |
1622 | SIGJMP_BUF jmp_buf; | |
1623 | scoped_restore restore_jmp_buf | |
1624 | = make_scoped_restore (&gdb_demangle_jmp_buf, &jmp_buf); | |
992c7d70 GB |
1625 | if (catch_demangler_crashes) |
1626 | { | |
17bfe554 | 1627 | /* The signal handler may keep the signal blocked when we longjmp out |
dda83cd7 | 1628 | of it. If we have sigprocmask, we can use it to unblock the signal |
17bfe554 CB |
1629 | afterwards and we can avoid the performance overhead of saving the |
1630 | signal mask just in case the signal gets triggered. Otherwise, just | |
1631 | tell sigsetjmp to save the mask. */ | |
1632 | #ifdef HAVE_SIGPROCMASK | |
3b3978bc | 1633 | crash_signal = SIGSETJMP (*gdb_demangle_jmp_buf, 0); |
17bfe554 | 1634 | #else |
3b3978bc | 1635 | crash_signal = SIGSETJMP (*gdb_demangle_jmp_buf, 1); |
17bfe554 | 1636 | #endif |
992c7d70 GB |
1637 | } |
1638 | #endif | |
1639 | ||
1640 | if (crash_signal == 0) | |
cb2cd8cb | 1641 | result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); |
992c7d70 GB |
1642 | |
1643 | #ifdef HAVE_WORKING_FORK | |
1644 | if (catch_demangler_crashes) | |
1645 | { | |
992c7d70 | 1646 | if (crash_signal != 0) |
dda83cd7 | 1647 | { |
17bfe554 CB |
1648 | #ifdef HAVE_SIGPROCMASK |
1649 | /* If we got the signal, SIGSEGV may still be blocked; restore it. */ | |
1650 | sigset_t segv_sig_set; | |
1651 | sigemptyset (&segv_sig_set); | |
1652 | sigaddset (&segv_sig_set, SIGSEGV); | |
21987b9c | 1653 | gdb_sigmask (SIG_UNBLOCK, &segv_sig_set, NULL); |
17bfe554 CB |
1654 | #endif |
1655 | ||
3b3978bc TT |
1656 | /* If there was a failure, we can't report it here, because |
1657 | we might be in a background thread. Instead, arrange for | |
1658 | the reporting to happen on the main thread. */ | |
dda83cd7 | 1659 | std::string copy = name; |
7570a17c TV |
1660 | run_on_main_thread ([ |
1661 | #if __cplusplus >= 201402L | |
1662 | =, copy = std::move (copy) | |
1663 | #else | |
1664 | = | |
1665 | #endif | |
1666 | ] () | |
dda83cd7 SM |
1667 | { |
1668 | report_failed_demangle (copy.c_str (), core_dump_allowed, | |
1669 | crash_signal); | |
1670 | }); | |
1671 | ||
1672 | result = NULL; | |
1673 | } | |
992c7d70 GB |
1674 | } |
1675 | #endif | |
1676 | ||
1677 | return result; | |
8de20a37 TT |
1678 | } |
1679 | ||
8b302db8 TT |
1680 | /* See cp-support.h. */ |
1681 | ||
cb2cd8cb PA |
1682 | char * |
1683 | gdb_cplus_demangle_print (int options, | |
1684 | struct demangle_component *tree, | |
1685 | int estimated_length, | |
1686 | size_t *p_allocated_size) | |
1687 | { | |
1688 | return cplus_demangle_print (options | DMGL_VERBOSE, tree, | |
1689 | estimated_length, p_allocated_size); | |
1690 | } | |
1691 | ||
1692 | /* A wrapper for cplus_demangle_v3_components that forces | |
1693 | DMGL_VERBOSE. */ | |
1694 | ||
1695 | static struct demangle_component * | |
1696 | gdb_cplus_demangle_v3_components (const char *mangled, | |
1697 | int options, void **mem) | |
1698 | { | |
1699 | return cplus_demangle_v3_components (mangled, options | DMGL_VERBOSE, mem); | |
1700 | } | |
1701 | ||
1702 | /* See cp-support.h. */ | |
1703 | ||
a20714ff PA |
1704 | unsigned int |
1705 | cp_search_name_hash (const char *search_name) | |
1706 | { | |
1707 | /* cp_entire_prefix_len assumes a fully-qualified name with no | |
1708 | leading "::". */ | |
1709 | if (startswith (search_name, "::")) | |
1710 | search_name += 2; | |
1711 | ||
1712 | unsigned int prefix_len = cp_entire_prefix_len (search_name); | |
1713 | if (prefix_len != 0) | |
1714 | search_name += prefix_len + 2; | |
1715 | ||
bd69330d PA |
1716 | unsigned int hash = 0; |
1717 | for (const char *string = search_name; *string != '\0'; ++string) | |
1718 | { | |
1719 | string = skip_spaces (string); | |
1720 | ||
1721 | if (*string == '(') | |
1722 | break; | |
1723 | ||
1724 | /* Ignore ABI tags such as "[abi:cxx11]. */ | |
1725 | if (*string == '[' | |
1726 | && startswith (string + 1, "abi:") | |
1727 | && string[5] != ':') | |
1728 | break; | |
1729 | ||
64a97606 KS |
1730 | /* Ignore template parameter lists. */ |
1731 | if (string[0] == '<' | |
1732 | && string[1] != '(' && string[1] != '<' && string[1] != '=' | |
1733 | && string[1] != ' ' && string[1] != '\0') | |
1734 | break; | |
1735 | ||
bd69330d PA |
1736 | hash = SYMBOL_HASH_NEXT (hash, *string); |
1737 | } | |
1738 | return hash; | |
a20714ff PA |
1739 | } |
1740 | ||
1741 | /* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype | |
1742 | implementation for symbol_name_match_type::WILD matching). Split | |
1743 | to a separate function for unit-testing convenience. | |
1744 | ||
1745 | If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to | |
1746 | match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME. | |
1747 | This allows conveniently setting breakpoints on functions/methods | |
1748 | inside any namespace/class without specifying the fully-qualified | |
1749 | name. | |
1750 | ||
1751 | E.g., these match: | |
b5ec771e | 1752 | |
a20714ff PA |
1753 | [symbol search name] [lookup name] |
1754 | foo::bar::func foo::bar::func | |
1755 | foo::bar::func bar::func | |
1756 | foo::bar::func func | |
1757 | ||
1758 | While these don't: | |
1759 | ||
1760 | [symbol search name] [lookup name] | |
1761 | foo::zbar::func bar::func | |
1762 | foo::bar::func foo::func | |
1763 | ||
1764 | See more examples in the test_cp_symbol_name_matches selftest | |
1765 | function below. | |
0662b6a7 PA |
1766 | |
1767 | See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME | |
1768 | and COMP_MATCH_RES. | |
1769 | ||
1770 | LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up. | |
1771 | ||
1772 | See strncmp_iw_with_mode for description of MODE. | |
1773 | */ | |
1774 | ||
1775 | static bool | |
1776 | cp_symbol_name_matches_1 (const char *symbol_search_name, | |
1777 | const char *lookup_name, | |
1778 | size_t lookup_name_len, | |
1779 | strncmp_iw_mode mode, | |
a207cff2 | 1780 | completion_match_result *comp_match_res) |
0662b6a7 | 1781 | { |
a20714ff | 1782 | const char *sname = symbol_search_name; |
bd69330d PA |
1783 | completion_match_for_lcd *match_for_lcd |
1784 | = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL); | |
a20714ff | 1785 | |
454f8b67 AB |
1786 | gdb_assert (match_for_lcd == nullptr || match_for_lcd->empty ()); |
1787 | ||
a20714ff PA |
1788 | while (true) |
1789 | { | |
1790 | if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len, | |
64a97606 | 1791 | mode, language_cplus, match_for_lcd, true) == 0) |
a20714ff PA |
1792 | { |
1793 | if (comp_match_res != NULL) | |
1794 | { | |
1795 | /* Note here we set different MATCH and MATCH_FOR_LCD | |
1796 | strings. This is because with | |
1797 | ||
1798 | (gdb) b push_bac[TAB] | |
1799 | ||
1800 | we want the completion matches to list | |
1801 | ||
1802 | std::vector<int>::push_back(...) | |
1803 | std::vector<char>::push_back(...) | |
1804 | ||
1805 | etc., which are SYMBOL_SEARCH_NAMEs, while we want | |
1806 | the input line to auto-complete to | |
1807 | ||
1808 | (gdb) push_back(...) | |
1809 | ||
1810 | which is SNAME, not to | |
1811 | ||
1812 | (gdb) std::vector< | |
1813 | ||
1814 | which would be the regular common prefix between all | |
1815 | the matches otherwise. */ | |
1816 | comp_match_res->set_match (symbol_search_name, sname); | |
1817 | } | |
1818 | return true; | |
1819 | } | |
1820 | ||
454f8b67 AB |
1821 | /* Clear match_for_lcd so the next strncmp_iw_with_mode call starts |
1822 | from scratch. */ | |
1823 | if (match_for_lcd != nullptr) | |
1824 | match_for_lcd->clear (); | |
1825 | ||
a20714ff PA |
1826 | unsigned int len = cp_find_first_component (sname); |
1827 | ||
1828 | if (sname[len] == '\0') | |
1829 | return false; | |
1830 | ||
1831 | gdb_assert (sname[len] == ':'); | |
1832 | /* Skip the '::'. */ | |
1833 | sname += len + 2; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | /* C++ symbol_name_matcher_ftype implementation. */ | |
1838 | ||
1839 | static bool | |
1840 | cp_fq_symbol_name_matches (const char *symbol_search_name, | |
1841 | const lookup_name_info &lookup_name, | |
1842 | completion_match_result *comp_match_res) | |
1843 | { | |
1844 | /* Get the demangled name. */ | |
1845 | const std::string &name = lookup_name.cplus ().lookup_name (); | |
bd69330d PA |
1846 | completion_match_for_lcd *match_for_lcd |
1847 | = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL); | |
a20714ff PA |
1848 | strncmp_iw_mode mode = (lookup_name.completion_mode () |
1849 | ? strncmp_iw_mode::NORMAL | |
1850 | : strncmp_iw_mode::MATCH_PARAMS); | |
1851 | ||
0662b6a7 | 1852 | if (strncmp_iw_with_mode (symbol_search_name, |
a20714ff | 1853 | name.c_str (), name.size (), |
bd69330d | 1854 | mode, language_cplus, match_for_lcd) == 0) |
0662b6a7 | 1855 | { |
a207cff2 PA |
1856 | if (comp_match_res != NULL) |
1857 | comp_match_res->set_match (symbol_search_name); | |
0662b6a7 PA |
1858 | return true; |
1859 | } | |
1860 | ||
1861 | return false; | |
1862 | } | |
1863 | ||
a20714ff PA |
1864 | /* C++ symbol_name_matcher_ftype implementation for wild matches. |
1865 | Defers work to cp_symbol_name_matches_1. */ | |
0662b6a7 | 1866 | |
b5ec771e | 1867 | static bool |
a20714ff PA |
1868 | cp_symbol_name_matches (const char *symbol_search_name, |
1869 | const lookup_name_info &lookup_name, | |
1870 | completion_match_result *comp_match_res) | |
b5ec771e PA |
1871 | { |
1872 | /* Get the demangled name. */ | |
1873 | const std::string &name = lookup_name.cplus ().lookup_name (); | |
1874 | ||
1875 | strncmp_iw_mode mode = (lookup_name.completion_mode () | |
1876 | ? strncmp_iw_mode::NORMAL | |
1877 | : strncmp_iw_mode::MATCH_PARAMS); | |
1878 | ||
0662b6a7 PA |
1879 | return cp_symbol_name_matches_1 (symbol_search_name, |
1880 | name.c_str (), name.size (), | |
a207cff2 | 1881 | mode, comp_match_res); |
b5ec771e PA |
1882 | } |
1883 | ||
1884 | /* See cp-support.h. */ | |
1885 | ||
1886 | symbol_name_matcher_ftype * | |
1887 | cp_get_symbol_name_matcher (const lookup_name_info &lookup_name) | |
1888 | { | |
a20714ff PA |
1889 | switch (lookup_name.match_type ()) |
1890 | { | |
1891 | case symbol_name_match_type::FULL: | |
1892 | case symbol_name_match_type::EXPRESSION: | |
de63c46b | 1893 | case symbol_name_match_type::SEARCH_NAME: |
a20714ff PA |
1894 | return cp_fq_symbol_name_matches; |
1895 | case symbol_name_match_type::WILD: | |
1896 | return cp_symbol_name_matches; | |
1897 | } | |
1898 | ||
1899 | gdb_assert_not_reached (""); | |
b5ec771e PA |
1900 | } |
1901 | ||
c62446b1 PA |
1902 | #if GDB_SELF_TEST |
1903 | ||
1904 | namespace selftests { | |
1905 | ||
cb8c24b6 | 1906 | static void |
0662b6a7 PA |
1907 | test_cp_symbol_name_matches () |
1908 | { | |
1909 | #define CHECK_MATCH(SYMBOL, INPUT) \ | |
1910 | SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \ | |
1911 | INPUT, sizeof (INPUT) - 1, \ | |
1912 | strncmp_iw_mode::MATCH_PARAMS, \ | |
1913 | NULL)) | |
1914 | ||
1915 | #define CHECK_NOT_MATCH(SYMBOL, INPUT) \ | |
1916 | SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \ | |
1917 | INPUT, sizeof (INPUT) - 1, \ | |
1918 | strncmp_iw_mode::MATCH_PARAMS, \ | |
1919 | NULL)) | |
1920 | ||
1921 | /* Like CHECK_MATCH, and also check that INPUT (and all substrings | |
1922 | that start at index 0) completes to SYMBOL. */ | |
1923 | #define CHECK_MATCH_C(SYMBOL, INPUT) \ | |
1924 | do \ | |
1925 | { \ | |
1926 | CHECK_MATCH (SYMBOL, INPUT); \ | |
1927 | for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \ | |
1928 | SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \ | |
1929 | strncmp_iw_mode::NORMAL, \ | |
1930 | NULL)); \ | |
1931 | } while (0) | |
1932 | ||
1933 | /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete | |
1934 | to SYMBOL. */ | |
1935 | #define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \ | |
1936 | do \ | |
1937 | { \ | |
1938 | CHECK_NOT_MATCH (SYMBOL, INPUT); \ | |
1939 | SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \ | |
1940 | sizeof (INPUT) - 1, \ | |
1941 | strncmp_iw_mode::NORMAL, \ | |
1942 | NULL)); \ | |
1943 | } while (0) | |
1944 | ||
1945 | /* Lookup name without parens matches all overloads. */ | |
1946 | CHECK_MATCH_C ("function()", "function"); | |
1947 | CHECK_MATCH_C ("function(int)", "function"); | |
1948 | ||
1949 | /* Check whitespace around parameters is ignored. */ | |
1950 | CHECK_MATCH_C ("function()", "function ()"); | |
1951 | CHECK_MATCH_C ("function ( )", "function()"); | |
1952 | CHECK_MATCH_C ("function ()", "function( )"); | |
1953 | CHECK_MATCH_C ("func(int)", "func( int )"); | |
1954 | CHECK_MATCH_C ("func(int)", "func ( int ) "); | |
1955 | CHECK_MATCH_C ("func ( int )", "func( int )"); | |
1956 | CHECK_MATCH_C ("func ( int )", "func ( int ) "); | |
1957 | ||
1958 | /* Check symbol name prefixes aren't incorrectly matched. */ | |
1959 | CHECK_NOT_MATCH ("func", "function"); | |
1960 | CHECK_NOT_MATCH ("function", "func"); | |
1961 | CHECK_NOT_MATCH ("function()", "func"); | |
1962 | ||
1963 | /* Check that if the lookup name includes parameters, only the right | |
1964 | overload matches. */ | |
1965 | CHECK_MATCH_C ("function(int)", "function(int)"); | |
1966 | CHECK_NOT_MATCH_C ("function(int)", "function()"); | |
1967 | ||
1968 | /* Check that whitespace within symbol names is not ignored. */ | |
1969 | CHECK_NOT_MATCH_C ("function", "func tion"); | |
1970 | CHECK_NOT_MATCH_C ("func__tion", "func_ _tion"); | |
1971 | CHECK_NOT_MATCH_C ("func11tion", "func1 1tion"); | |
1972 | ||
1973 | /* Check the converse, which can happen with template function, | |
1974 | where the return type is part of the demangled name. */ | |
1975 | CHECK_NOT_MATCH_C ("func tion", "function"); | |
1976 | CHECK_NOT_MATCH_C ("func1 1tion", "func11tion"); | |
1977 | CHECK_NOT_MATCH_C ("func_ _tion", "func__tion"); | |
1978 | ||
1979 | /* Within parameters too. */ | |
1980 | CHECK_NOT_MATCH_C ("func(param)", "func(par am)"); | |
1981 | ||
1982 | /* Check handling of whitespace around C++ operators. */ | |
1983 | CHECK_NOT_MATCH_C ("operator<<", "opera tor<<"); | |
1984 | CHECK_NOT_MATCH_C ("operator<<", "operator< <"); | |
1985 | CHECK_NOT_MATCH_C ("operator<<", "operator < <"); | |
1986 | CHECK_NOT_MATCH_C ("operator==", "operator= ="); | |
1987 | CHECK_NOT_MATCH_C ("operator==", "operator = ="); | |
1988 | CHECK_MATCH_C ("operator<<", "operator <<"); | |
1989 | CHECK_MATCH_C ("operator<<()", "operator <<"); | |
1990 | CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)"); | |
1991 | CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()"); | |
1992 | CHECK_MATCH_C ("operator==", "operator =="); | |
1993 | CHECK_MATCH_C ("operator==()", "operator =="); | |
1994 | CHECK_MATCH_C ("operator <<", "operator<<"); | |
1995 | CHECK_MATCH_C ("operator ==", "operator=="); | |
1996 | CHECK_MATCH_C ("operator bool", "operator bool"); | |
1997 | CHECK_MATCH_C ("operator bool ()", "operator bool"); | |
1998 | CHECK_MATCH_C ("operatorX<<", "operatorX < <"); | |
1999 | CHECK_MATCH_C ("Xoperator<<", "Xoperator < <"); | |
2000 | ||
2001 | CHECK_MATCH_C ("operator()(int)", "operator()(int)"); | |
2002 | CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )"); | |
2003 | CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )"); | |
2004 | /* The first "()" is not the parameter list. */ | |
2005 | CHECK_NOT_MATCH ("operator()(int)", "operator"); | |
2006 | ||
2007 | /* Misc user-defined operator tests. */ | |
2008 | ||
2009 | CHECK_NOT_MATCH_C ("operator/=()", "operator ^="); | |
2010 | /* Same length at end of input. */ | |
2011 | CHECK_NOT_MATCH_C ("operator>>", "operator[]"); | |
2012 | /* Same length but not at end of input. */ | |
2013 | CHECK_NOT_MATCH_C ("operator>>()", "operator[]()"); | |
2014 | ||
2015 | CHECK_MATCH_C ("base::operator char*()", "base::operator char*()"); | |
2016 | CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()"); | |
2017 | CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()"); | |
2018 | CHECK_MATCH ("base::operator char**()", "base::operator char * *"); | |
2019 | CHECK_MATCH_C ("base::operator*()", "base::operator*()"); | |
2020 | CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc"); | |
2021 | CHECK_NOT_MATCH ("base::operator char*()", "base::operator char"); | |
2022 | CHECK_NOT_MATCH ("base::operator char*()", "base::operat"); | |
2023 | ||
2024 | /* Check handling of whitespace around C++ scope operators. */ | |
2025 | CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar"); | |
2026 | CHECK_MATCH_C ("foo::bar", "foo :: bar"); | |
2027 | CHECK_MATCH_C ("foo :: bar", "foo::bar"); | |
2028 | ||
2029 | CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()"); | |
2030 | CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()"); | |
2031 | CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )"); | |
2032 | CHECK_MATCH_C ("function()", "function()"); | |
2033 | CHECK_MATCH_C ("bar::function()", "bar::function()"); | |
a20714ff PA |
2034 | |
2035 | /* Wild matching tests follow. */ | |
2036 | ||
2037 | /* Tests matching symbols in some scope. */ | |
2038 | CHECK_MATCH_C ("foo::function()", "function"); | |
2039 | CHECK_MATCH_C ("foo::function(int)", "function"); | |
2040 | CHECK_MATCH_C ("foo::bar::function()", "function"); | |
2041 | CHECK_MATCH_C ("bar::function()", "bar::function"); | |
2042 | CHECK_MATCH_C ("foo::bar::function()", "bar::function"); | |
2043 | CHECK_MATCH_C ("foo::bar::function(int)", "bar::function"); | |
2044 | ||
2045 | /* Same, with parameters in the lookup name. */ | |
2046 | CHECK_MATCH_C ("foo::function()", "function()"); | |
2047 | CHECK_MATCH_C ("foo::bar::function()", "function()"); | |
2048 | CHECK_MATCH_C ("foo::function(int)", "function(int)"); | |
2049 | CHECK_MATCH_C ("foo::function()", "foo::function()"); | |
2050 | CHECK_MATCH_C ("foo::bar::function()", "bar::function()"); | |
2051 | CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)"); | |
2052 | CHECK_MATCH_C ("bar::function()", "bar::function()"); | |
2053 | ||
2054 | CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()"); | |
2055 | ||
2056 | CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)", | |
2057 | "bar::function(int)"); | |
2058 | CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)", | |
2059 | "function(int)"); | |
2060 | ||
2061 | /* Lookup scope wider than symbol scope, should not match. */ | |
2062 | CHECK_NOT_MATCH_C ("function()", "bar::function"); | |
2063 | CHECK_NOT_MATCH_C ("function()", "bar::function()"); | |
2064 | ||
2065 | /* Explicit global scope doesn't match. */ | |
2066 | CHECK_NOT_MATCH_C ("foo::function()", "::function"); | |
2067 | CHECK_NOT_MATCH_C ("foo::function()", "::function()"); | |
2068 | CHECK_NOT_MATCH_C ("foo::function(int)", "::function()"); | |
2069 | CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)"); | |
bd69330d PA |
2070 | |
2071 | /* Test ABI tag matching/ignoring. */ | |
2072 | ||
2073 | /* If the symbol name has an ABI tag, but the lookup name doesn't, | |
2074 | then the ABI tag in the symbol name is ignored. */ | |
2075 | CHECK_MATCH_C ("function[abi:foo]()", "function"); | |
2076 | CHECK_MATCH_C ("function[abi:foo](int)", "function"); | |
2077 | CHECK_MATCH_C ("function[abi:foo]()", "function ()"); | |
2078 | CHECK_NOT_MATCH_C ("function[abi:foo]()", "function (int)"); | |
2079 | ||
2080 | CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo]"); | |
2081 | CHECK_MATCH_C ("function[abi:foo](int)", "function[abi:foo]"); | |
2082 | CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo] ()"); | |
2083 | CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function"); | |
2084 | CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function"); | |
2085 | CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo]"); | |
2086 | CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function[abi:foo]"); | |
2087 | CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] ()"); | |
2088 | CHECK_NOT_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] (int)"); | |
2089 | ||
2090 | CHECK_MATCH_C ("function [abi:foo][abi:bar] ( )", "function [abi:foo]"); | |
2091 | ||
2092 | /* If the symbol name does not have an ABI tag, while the lookup | |
2093 | name has one, then there's no match. */ | |
2094 | CHECK_NOT_MATCH_C ("function()", "function[abi:foo]()"); | |
2095 | CHECK_NOT_MATCH_C ("function()", "function[abi:foo]"); | |
0662b6a7 PA |
2096 | } |
2097 | ||
c62446b1 PA |
2098 | /* If non-NULL, return STR wrapped in quotes. Otherwise, return a |
2099 | "<null>" string (with no quotes). */ | |
2100 | ||
2101 | static std::string | |
2102 | quote (const char *str) | |
2103 | { | |
2104 | if (str != NULL) | |
fad03f6e | 2105 | return std::string (1, '"') + str + '"'; |
c62446b1 PA |
2106 | else |
2107 | return "<null>"; | |
2108 | } | |
2109 | ||
2110 | /* Check that removing parameter info out of NAME produces EXPECTED. | |
2111 | COMPLETION_MODE indicates whether we're testing normal and | |
2112 | completion mode. FILE and LINE are used to provide better test | |
3bfdcabb | 2113 | location information in case the check fails. */ |
c62446b1 PA |
2114 | |
2115 | static void | |
2116 | check_remove_params (const char *file, int line, | |
2117 | const char *name, const char *expected, | |
2118 | bool completion_mode) | |
2119 | { | |
2120 | gdb::unique_xmalloc_ptr<char> result | |
2121 | = cp_remove_params_if_any (name, completion_mode); | |
2122 | ||
2123 | if ((expected == NULL) != (result == NULL) | |
2124 | || (expected != NULL | |
2125 | && strcmp (result.get (), expected) != 0)) | |
2126 | { | |
2127 | error (_("%s:%d: make-paramless self-test failed: (completion=%d) " | |
2128 | "\"%s\" -> %s, expected %s"), | |
2129 | file, line, completion_mode, name, | |
2130 | quote (result.get ()).c_str (), quote (expected).c_str ()); | |
2131 | } | |
2132 | } | |
2133 | ||
2134 | /* Entry point for cp_remove_params unit tests. */ | |
2135 | ||
2136 | static void | |
2137 | test_cp_remove_params () | |
2138 | { | |
2139 | /* Check that removing parameter info out of NAME produces EXPECTED. | |
2140 | Checks both normal and completion modes. */ | |
2141 | #define CHECK(NAME, EXPECTED) \ | |
2142 | do \ | |
2143 | { \ | |
2144 | check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \ | |
2145 | check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \ | |
2146 | } \ | |
2147 | while (0) | |
2148 | ||
2149 | /* Similar, but used when NAME is incomplete -- i.e., is has | |
2150 | unbalanced parentheses. In this case, looking for the exact name | |
2151 | should fail / return empty. */ | |
2152 | #define CHECK_INCOMPL(NAME, EXPECTED) \ | |
2153 | do \ | |
2154 | { \ | |
2155 | check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \ | |
2156 | check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \ | |
2157 | } \ | |
2158 | while (0) | |
2159 | ||
2160 | CHECK ("function()", "function"); | |
2161 | CHECK_INCOMPL ("function(", "function"); | |
2162 | CHECK ("function() const", "function"); | |
2163 | ||
2164 | CHECK ("(anonymous namespace)::A::B::C", | |
2165 | "(anonymous namespace)::A::B::C"); | |
2166 | ||
2167 | CHECK ("A::(anonymous namespace)", | |
2168 | "A::(anonymous namespace)"); | |
2169 | ||
2170 | CHECK_INCOMPL ("A::(anonymou", "A"); | |
2171 | ||
2172 | CHECK ("A::foo<int>()", | |
2173 | "A::foo<int>"); | |
2174 | ||
2175 | CHECK_INCOMPL ("A::foo<int>(", | |
2176 | "A::foo<int>"); | |
2177 | ||
2178 | CHECK ("A::foo<(anonymous namespace)::B>::func(int)", | |
2179 | "A::foo<(anonymous namespace)::B>::func"); | |
2180 | ||
2181 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in", | |
2182 | "A::foo<(anonymous namespace)::B>::func"); | |
2183 | ||
2184 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::", | |
2185 | "A::foo<(anonymous namespace)::B>"); | |
2186 | ||
2187 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:", | |
2188 | "A::foo<(anonymous namespace)::B>"); | |
2189 | ||
2190 | CHECK ("A::foo<(anonymous namespace)::B>", | |
2191 | "A::foo<(anonymous namespace)::B>"); | |
2192 | ||
2193 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B", | |
2194 | "A::foo"); | |
2195 | ||
2196 | /* Shouldn't this parse? Looks like a bug in | |
2197 | cp_demangled_name_to_comp. See PR c++/22411. */ | |
2198 | #if 0 | |
2199 | CHECK ("A::foo<void(int)>::func(int)", | |
2200 | "A::foo<void(int)>::func"); | |
2201 | #else | |
2202 | CHECK_INCOMPL ("A::foo<void(int)>::func(int)", | |
2203 | "A::foo"); | |
2204 | #endif | |
2205 | ||
2206 | CHECK_INCOMPL ("A::foo<void(int", | |
2207 | "A::foo"); | |
2208 | ||
2209 | #undef CHECK | |
2210 | #undef CHECK_INCOMPL | |
2211 | } | |
2212 | ||
2213 | } // namespace selftests | |
2214 | ||
2215 | #endif /* GDB_SELF_CHECK */ | |
2216 | ||
9219021c DC |
2217 | /* This is a front end for cp_find_first_component, for unit testing. |
2218 | Be careful when using it: see the NOTE above | |
2219 | cp_find_first_component. */ | |
2220 | ||
2221 | static void | |
4a475551 | 2222 | first_component_command (const char *arg, int from_tty) |
9219021c | 2223 | { |
c836824f AR |
2224 | int len; |
2225 | char *prefix; | |
2226 | ||
2227 | if (!arg) | |
2228 | return; | |
2229 | ||
2230 | len = cp_find_first_component (arg); | |
224c3ddb | 2231 | prefix = (char *) alloca (len + 1); |
9219021c DC |
2232 | |
2233 | memcpy (prefix, arg, len); | |
2234 | prefix[len] = '\0'; | |
2235 | ||
6cb06a8c | 2236 | gdb_printf ("%s\n", prefix); |
9219021c DC |
2237 | } |
2238 | ||
57651221 | 2239 | /* Implement "info vtbl". */ |
c4aeac85 TT |
2240 | |
2241 | static void | |
1d12d88f | 2242 | info_vtbl_command (const char *arg, int from_tty) |
c4aeac85 TT |
2243 | { |
2244 | struct value *value; | |
2245 | ||
2246 | value = parse_and_eval (arg); | |
2247 | cplus_print_vtable (value); | |
2248 | } | |
2249 | ||
2f2c677e KS |
2250 | /* See description in cp-support.h. */ |
2251 | ||
2252 | const char * | |
2253 | find_toplevel_char (const char *s, char c) | |
2254 | { | |
2255 | int quoted = 0; /* zero if we're not in quotes; | |
2256 | '"' if we're in a double-quoted string; | |
2257 | '\'' if we're in a single-quoted string. */ | |
2258 | int depth = 0; /* Number of unclosed parens we've seen. */ | |
2259 | const char *scan; | |
2260 | ||
2261 | for (scan = s; *scan; scan++) | |
2262 | { | |
2263 | if (quoted) | |
2264 | { | |
2265 | if (*scan == quoted) | |
2266 | quoted = 0; | |
2267 | else if (*scan == '\\' && *(scan + 1)) | |
2268 | scan++; | |
2269 | } | |
2270 | else if (*scan == c && ! quoted && depth == 0) | |
2271 | return scan; | |
2272 | else if (*scan == '"' || *scan == '\'') | |
2273 | quoted = *scan; | |
2274 | else if (*scan == '(' || *scan == '<') | |
2275 | depth++; | |
2276 | else if ((*scan == ')' || *scan == '>') && depth > 0) | |
2277 | depth--; | |
2278 | else if (*scan == 'o' && !quoted && depth == 0) | |
2279 | { | |
2280 | /* Handle C++ operator names. */ | |
2281 | if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0) | |
2282 | { | |
2283 | scan += CP_OPERATOR_LEN; | |
2284 | if (*scan == c) | |
2285 | return scan; | |
2286 | while (ISSPACE (*scan)) | |
2287 | { | |
2288 | ++scan; | |
2289 | if (*scan == c) | |
2290 | return scan; | |
2291 | } | |
2292 | if (*scan == '\0') | |
2293 | break; | |
2294 | ||
2295 | switch (*scan) | |
2296 | { | |
2297 | /* Skip over one less than the appropriate number of | |
2298 | characters: the for loop will skip over the last | |
2299 | one. */ | |
2300 | case '<': | |
2301 | if (scan[1] == '<') | |
2302 | { | |
2303 | scan++; | |
2304 | if (*scan == c) | |
2305 | return scan; | |
2306 | } | |
2307 | break; | |
2308 | case '>': | |
2309 | if (scan[1] == '>') | |
2310 | { | |
2311 | scan++; | |
2312 | if (*scan == c) | |
2313 | return scan; | |
2314 | } | |
2315 | break; | |
2316 | } | |
2317 | } | |
2318 | } | |
2319 | } | |
2320 | ||
2321 | return 0; | |
2322 | } | |
2323 | ||
6c265988 | 2324 | void _initialize_cp_support (); |
9219021c | 2325 | void |
6c265988 | 2326 | _initialize_cp_support () |
9219021c | 2327 | { |
5e84b7ee SM |
2328 | cmd_list_element *maintenance_cplus |
2329 | = add_basic_prefix_cmd ("cplus", class_maintenance, | |
2330 | _("C++ maintenance commands."), | |
2331 | &maint_cplus_cmd_list, | |
2332 | 0, &maintenancelist); | |
2333 | add_alias_cmd ("cp", maintenance_cplus, class_maintenance, 1, | |
aff410f1 MS |
2334 | &maintenancelist); |
2335 | ||
2336 | add_cmd ("first_component", | |
2337 | class_maintenance, | |
2338 | first_component_command, | |
1a966eab | 2339 | _("Print the first class/namespace component of NAME."), |
9219021c | 2340 | &maint_cplus_cmd_list); |
c4aeac85 TT |
2341 | |
2342 | add_info ("vtbl", info_vtbl_command, | |
57651221 | 2343 | _("Show the virtual function table for a C++ object.\n\ |
c4aeac85 TT |
2344 | Usage: info vtbl EXPRESSION\n\ |
2345 | Evaluate EXPRESSION and display the virtual function table for the\n\ | |
2346 | resulting object.")); | |
992c7d70 GB |
2347 | |
2348 | #ifdef HAVE_WORKING_FORK | |
2349 | add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance, | |
2350 | &catch_demangler_crashes, _("\ | |
2351 | Set whether to attempt to catch demangler crashes."), _("\ | |
2352 | Show whether to attempt to catch demangler crashes."), _("\ | |
2353 | If enabled GDB will attempt to catch demangler crashes and\n\ | |
2354 | display the offending symbol."), | |
2355 | NULL, | |
2356 | NULL, | |
2357 | &maintenance_set_cmdlist, | |
2358 | &maintenance_show_cmdlist); | |
57357d9d TT |
2359 | |
2360 | gdb_demangle_attempt_core_dump = can_dump_core (LIMIT_CUR); | |
992c7d70 | 2361 | #endif |
c62446b1 PA |
2362 | |
2363 | #if GDB_SELF_TEST | |
0662b6a7 PA |
2364 | selftests::register_test ("cp_symbol_name_matches", |
2365 | selftests::test_cp_symbol_name_matches); | |
c62446b1 PA |
2366 | selftests::register_test ("cp_remove_params", |
2367 | selftests::test_cp_remove_params); | |
2368 | #endif | |
9219021c | 2369 | } |