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