]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cp-support.c
[gdb/testsuite] Fix gdb.arch/amd64-disp-step-avx.exp on x86_64-freebsd
[thirdparty/binutils-gdb.git] / gdb / cp-support.c
CommitLineData
de17c821 1/* Helper routines for C++ support in GDB.
d01e8234 2 Copyright (C) 2002-2025 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 20
de17c821 21#include "cp-support.h"
83b6e1f1 22#include "language.h"
de17c821 23#include "demangle.h"
5b9707eb 24#include "cli/cli-cmds.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"
e0f4b3ec 38#include "gdbsupport/gdb-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"
c7d029ea 44#include "typeprint.h"
99d9c3b9 45#include "inferior.h"
f88e9fd3 46
fb4c6eba
DJ
47#define d_left(dc) (dc)->u.s_binary.left
48#define d_right(dc) (dc)->u.s_binary.right
b2a7f303 49
fb4c6eba 50/* Functions related to demangled name parsing. */
b2a7f303
DC
51
52static unsigned int cp_find_first_component_aux (const char *name,
53 int permissive);
54
55static void demangled_name_complaint (const char *name);
b6429628 56
0891c3cc 57/* Functions related to overload resolution. */
b6429628 58
8d577d32 59static void overload_list_add_symbol (struct symbol *sym,
0891c3cc
PA
60 const char *oload_name,
61 std::vector<symbol *> *overload_list);
8d577d32 62
0891c3cc
PA
63static void add_symbol_overload_list_using
64 (const char *func_name, const char *the_namespace,
65 std::vector<symbol *> *overload_list);
8d577d32 66
0891c3cc
PA
67static void add_symbol_overload_list_qualified
68 (const char *func_name,
69 std::vector<symbol *> *overload_list);
8d577d32 70
9219021c
DC
71/* The list of "maint cplus" commands. */
72
5c4e30ca 73struct cmd_list_element *maint_cplus_cmd_list = NULL;
9219021c 74
3a93a0c2
KS
75static void
76 replace_typedefs (struct demangle_parse_info *info,
2621e0fd
TT
77 struct demangle_component *ret_comp,
78 canonicalization_ftype *finder,
79 void *data);
3a93a0c2 80
cb2cd8cb
PA
81static struct demangle_component *
82 gdb_cplus_demangle_v3_components (const char *mangled,
83 int options, void **mem);
84
3a93a0c2
KS
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
3a93a0c2 149 sym = NULL;
3a93a0c2 150
a70b8144 151 try
492d29ea 152 {
ccf41c24 153 sym = lookup_symbol (name, 0, SEARCH_VFT, 0).symbol;
492d29ea 154 }
230d2906 155 catch (const gdb_exception &except)
492d29ea
PA
156 {
157 return 0;
158 }
492d29ea
PA
159
160 if (sym != NULL)
3a93a0c2 161 {
5f9c5a63 162 struct type *otype = sym->type ();
3a93a0c2 163
2621e0fd
TT
164 if (finder != NULL)
165 {
166 const char *new_name = (*finder) (otype, data);
167
168 if (new_name != NULL)
169 {
170 ret_comp->u.s_name.s = new_name;
171 ret_comp->u.s_name.len = strlen (new_name);
172 return 1;
173 }
174
175 return 0;
176 }
177
74921315 178 /* If the type is a typedef or namespace alias, replace it. */
78134374
SM
179 if (otype->code () == TYPE_CODE_TYPEDEF
180 || otype->code () == TYPE_CODE_NAMESPACE)
3a93a0c2
KS
181 {
182 long len;
183 int is_anon;
184 struct type *type;
c8b23b3f 185 std::unique_ptr<demangle_parse_info> i;
3a93a0c2
KS
186
187 /* Get the real type of the typedef. */
188 type = check_typedef (otype);
189
725cbb63
KS
190 /* If the symbol name is the same as the original type name,
191 don't substitute. That would cause infinite recursion in
192 symbol lookups, as the typedef symbol is often the first
193 found symbol in the symbol table.
194
195 However, this can happen in a number of situations, such as:
196
197 If the symbol is a namespace and its type name is no different
74921315 198 than the name we looked up, this symbol is not a namespace
725cbb63
KS
199 alias and does not need to be substituted.
200
201 If the symbol is typedef and its type name is the same
202 as the symbol's name, e.g., "typedef struct foo foo;". */
7d93a1e0
SM
203 if (type->name () != nullptr
204 && strcmp (type->name (), name) == 0)
74921315
KS
205 return 0;
206
7d93a1e0 207 is_anon = (type->name () == NULL
78134374
SM
208 && (type->code () == TYPE_CODE_ENUM
209 || type->code () == TYPE_CODE_STRUCT
210 || type->code () == TYPE_CODE_UNION));
3a93a0c2
KS
211 if (is_anon)
212 {
213 struct type *last = otype;
214
215 /* Find the last typedef for the type. */
27710edb
SM
216 while (last->target_type () != NULL
217 && (last->target_type ()->code ()
3a93a0c2 218 == TYPE_CODE_TYPEDEF))
27710edb 219 last = last->target_type ();
3a93a0c2
KS
220
221 /* If there is only one typedef for this anonymous type,
222 do not substitute it. */
223 if (type == otype)
224 return 0;
225 else
226 /* Use the last typedef seen as the type for this
227 anonymous type. */
228 type = last;
229 }
230
d7e74731 231 string_file buf;
a70b8144 232 try
d7e74731 233 {
c7d029ea
PA
234 /* Avoid using the current language. If the language is
235 C, and TYPE is a struct/class, the printed type is
236 prefixed with "struct " or "class ", which we don't
237 want when we're expanding a C++ typedef. Print using
238 the type symbol's language to expand a C++ typedef
239 the C++ way even if the current language is C. */
240 const language_defn *lang = language_def (sym->language ());
241 lang->print_type (type, "", &buf, -1, 0, &type_print_raw_options);
d7e74731 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. */
6921816e 261 cp_merge_demangle_parse_infos (info, ret_comp, std::move (i));
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 506 sym = lookup_symbol (local_name.get (), 0,
ccf41c24 507 SEARCH_VFT, 0).symbol;
2621e0fd 508 }
230d2906 509 catch (const gdb_exception &except)
492d29ea
PA
510 {
511 }
492d29ea 512
492d29ea 513 if (sym != NULL)
2621e0fd 514 {
5f9c5a63 515 struct type *otype = sym->type ();
2621e0fd
TT
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);
d182e398 533 [[fallthrough]];
3a93a0c2
KS
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
8d13d83a
TT
576/* A helper to strip a trailing "()" from PTR. The string is modified
577 in place. */
578
579static void
580maybe_strip_parens (char *ptr)
581{
582 size_t len = strlen (ptr);
583 if (len > 2 && ptr[len - 2] == '(' && ptr[len - 1] == ')')
584 ptr[len - 2] = '\0';
585}
586
2f408ecb
PA
587/* Parse STRING and convert it to canonical form, resolving any
588 typedefs. If parsing fails, or if STRING is already canonical,
596dc4ad 589 return nullptr. Otherwise return the canonical form. If
2f408ecb
PA
590 FINDER is not NULL, then type components are passed to FINDER to be
591 looked up. DATA is passed verbatim to FINDER. */
3a93a0c2 592
596dc4ad 593gdb::unique_xmalloc_ptr<char>
2621e0fd
TT
594cp_canonicalize_string_full (const char *string,
595 canonicalization_ftype *finder,
596 void *data)
3a93a0c2 597{
3a93a0c2 598 unsigned int estimated_len;
c8b23b3f 599 std::unique_ptr<demangle_parse_info> info;
3a93a0c2 600
3a93a0c2
KS
601 estimated_len = strlen (string) * 2;
602 info = cp_demangled_name_to_comp (string, NULL);
603 if (info != NULL)
604 {
605 /* Replace all the typedefs in the tree. */
c8b23b3f 606 replace_typedefs (info.get (), info->tree, finder, data);
3a93a0c2
KS
607
608 /* Convert the tree back into a string. */
29592bde
PA
609 gdb::unique_xmalloc_ptr<char> us = cp_comp_to_string (info->tree,
610 estimated_len);
e88e8651 611 gdb_assert (us);
3a93a0c2 612
8d13d83a
TT
613 if (info->added_parens)
614 maybe_strip_parens (us.get ());
615
3a93a0c2
KS
616 /* Finally, compare the original string with the computed
617 name, returning NULL if they are the same. */
596dc4ad
TT
618 if (strcmp (us.get (), string) == 0)
619 return nullptr;
620
621 return us;
3a93a0c2
KS
622 }
623
596dc4ad 624 return nullptr;
3a93a0c2
KS
625}
626
2621e0fd
TT
627/* Like cp_canonicalize_string_full, but always passes NULL for
628 FINDER. */
629
596dc4ad 630gdb::unique_xmalloc_ptr<char>
2621e0fd
TT
631cp_canonicalize_string_no_typedefs (const char *string)
632{
633 return cp_canonicalize_string_full (string, NULL, NULL);
634}
635
f88e9fd3 636/* Parse STRING and convert it to canonical form. If parsing fails,
596dc4ad 637 or if STRING is already canonical, return nullptr.
2f408ecb 638 Otherwise return the canonical form. */
9219021c 639
596dc4ad 640gdb::unique_xmalloc_ptr<char>
fb4c6eba
DJ
641cp_canonicalize_string (const char *string)
642{
c8b23b3f 643 std::unique_ptr<demangle_parse_info> info;
f88e9fd3 644 unsigned int estimated_len;
9219021c 645
f88e9fd3 646 if (cp_already_canonical (string))
596dc4ad 647 return nullptr;
9219021c 648
3a93a0c2
KS
649 info = cp_demangled_name_to_comp (string, NULL);
650 if (info == NULL)
596dc4ad 651 return nullptr;
9219021c 652
f88e9fd3 653 estimated_len = strlen (string) * 2;
e88e8651
YQ
654 gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree,
655 estimated_len));
9219021c 656
e88e8651 657 if (!us)
9934703b
JK
658 {
659 warning (_("internal error: string \"%s\" failed to be canonicalized"),
660 string);
596dc4ad 661 return nullptr;
9934703b
JK
662 }
663
8d13d83a
TT
664 if (info->added_parens)
665 maybe_strip_parens (us.get ());
666
596dc4ad
TT
667 if (strcmp (us.get (), string) == 0)
668 return nullptr;
de17c821 669
596dc4ad 670 return us;
fb4c6eba 671}
de17c821 672
aff410f1
MS
673/* Convert a mangled name to a demangle_component tree. *MEMORY is
674 set to the block of used memory that should be freed when finished
675 with the tree. DEMANGLED_P is set to the char * that should be
676 freed when finished with the tree, or NULL if none was needed.
677 OPTIONS will be passed to the demangler. */
de17c821 678
c8b23b3f 679static std::unique_ptr<demangle_parse_info>
fb4c6eba 680mangled_name_to_comp (const char *mangled_name, int options,
3456e70c
TT
681 void **memory,
682 gdb::unique_xmalloc_ptr<char> *demangled_p)
de17c821 683{
fb4c6eba
DJ
684 /* If it looks like a v3 mangled name, then try to go directly
685 to trees. */
686 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
de17c821 687 {
3a93a0c2
KS
688 struct demangle_component *ret;
689
cb2cd8cb
PA
690 ret = gdb_cplus_demangle_v3_components (mangled_name,
691 options, memory);
fb4c6eba
DJ
692 if (ret)
693 {
6b62451a 694 auto info = std::make_unique<demangle_parse_info> ();
3a93a0c2 695 info->tree = ret;
fb4c6eba 696 *demangled_p = NULL;
3a93a0c2 697 return info;
fb4c6eba 698 }
de17c821
DJ
699 }
700
aff410f1
MS
701 /* If it doesn't, or if that failed, then try to demangle the
702 name. */
3456e70c
TT
703 gdb::unique_xmalloc_ptr<char> demangled_name = gdb_demangle (mangled_name,
704 options);
fb4c6eba
DJ
705 if (demangled_name == NULL)
706 return NULL;
707
aff410f1
MS
708 /* If we could demangle the name, parse it to build the component
709 tree. */
c8b23b3f 710 std::unique_ptr<demangle_parse_info> info
3456e70c 711 = cp_demangled_name_to_comp (demangled_name.get (), NULL);
de17c821 712
3a93a0c2 713 if (info == NULL)
3456e70c 714 return NULL;
de17c821 715
3456e70c 716 *demangled_p = std::move (demangled_name);
3a93a0c2 717 return info;
de17c821
DJ
718}
719
720/* Return the name of the class containing method PHYSNAME. */
721
722char *
31c27f77 723cp_class_name_from_physname (const char *physname)
de17c821 724{
de237128 725 void *storage = NULL;
3456e70c 726 gdb::unique_xmalloc_ptr<char> demangled_name;
29592bde 727 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb 728 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
c8b23b3f 729 std::unique_ptr<demangle_parse_info> info;
fb4c6eba
DJ
730 int done;
731
3a93a0c2
KS
732 info = mangled_name_to_comp (physname, DMGL_ANSI,
733 &storage, &demangled_name);
734 if (info == NULL)
de17c821
DJ
735 return NULL;
736
fb4c6eba 737 done = 0;
3a93a0c2 738 ret_comp = info->tree;
5e5100cb 739
aff410f1
MS
740 /* First strip off any qualifiers, if we have a function or
741 method. */
fb4c6eba
DJ
742 while (!done)
743 switch (ret_comp->type)
744 {
fb4c6eba
DJ
745 case DEMANGLE_COMPONENT_CONST:
746 case DEMANGLE_COMPONENT_RESTRICT:
747 case DEMANGLE_COMPONENT_VOLATILE:
748 case DEMANGLE_COMPONENT_CONST_THIS:
749 case DEMANGLE_COMPONENT_RESTRICT_THIS:
750 case DEMANGLE_COMPONENT_VOLATILE_THIS:
751 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
dda83cd7
SM
752 ret_comp = d_left (ret_comp);
753 break;
5e5100cb
DJ
754 default:
755 done = 1;
756 break;
757 }
758
759 /* If what we have now is a function, discard the argument list. */
760 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
761 ret_comp = d_left (ret_comp);
762
763 /* If what we have now is a template, strip off the template
764 arguments. The left subtree may be a qualified name. */
765 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
766 ret_comp = d_left (ret_comp);
767
aff410f1
MS
768 /* What we have now should be a name, possibly qualified.
769 Additional qualifiers could live in the left subtree or the right
770 subtree. Find the last piece. */
5e5100cb
DJ
771 done = 0;
772 prev_comp = NULL;
773 cur_comp = ret_comp;
774 while (!done)
775 switch (cur_comp->type)
776 {
777 case DEMANGLE_COMPONENT_QUAL_NAME:
778 case DEMANGLE_COMPONENT_LOCAL_NAME:
779 prev_comp = cur_comp;
dda83cd7
SM
780 cur_comp = d_right (cur_comp);
781 break;
fb4c6eba 782 case DEMANGLE_COMPONENT_TEMPLATE:
5e5100cb 783 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
784 case DEMANGLE_COMPONENT_CTOR:
785 case DEMANGLE_COMPONENT_DTOR:
786 case DEMANGLE_COMPONENT_OPERATOR:
787 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
788 done = 1;
789 break;
790 default:
791 done = 1;
5e5100cb 792 cur_comp = NULL;
fb4c6eba
DJ
793 break;
794 }
795
5e5100cb 796 if (cur_comp != NULL && prev_comp != NULL)
de17c821 797 {
5e5100cb 798 /* We want to discard the rightmost child of PREV_COMP. */
fb4c6eba 799 *prev_comp = *d_left (prev_comp);
aff410f1
MS
800 /* The ten is completely arbitrary; we don't have a good
801 estimate. */
5e5100cb 802 ret = cp_comp_to_string (ret_comp, 10);
de17c821
DJ
803 }
804
fb4c6eba 805 xfree (storage);
29592bde 806 return ret.release ();
de17c821
DJ
807}
808
aff410f1
MS
809/* Return the child of COMP which is the basename of a method,
810 variable, et cetera. All scope qualifiers are discarded, but
811 template arguments will be included. The component tree may be
812 modified. */
de17c821 813
5e5100cb
DJ
814static struct demangle_component *
815unqualified_name_from_comp (struct demangle_component *comp)
de17c821 816{
5e5100cb 817 struct demangle_component *ret_comp = comp, *last_template;
fb4c6eba
DJ
818 int done;
819
fb4c6eba 820 done = 0;
5e5100cb 821 last_template = NULL;
fb4c6eba
DJ
822 while (!done)
823 switch (ret_comp->type)
824 {
825 case DEMANGLE_COMPONENT_QUAL_NAME:
826 case DEMANGLE_COMPONENT_LOCAL_NAME:
dda83cd7
SM
827 ret_comp = d_right (ret_comp);
828 break;
5e5100cb 829 case DEMANGLE_COMPONENT_TYPED_NAME:
dda83cd7
SM
830 ret_comp = d_left (ret_comp);
831 break;
5e5100cb
DJ
832 case DEMANGLE_COMPONENT_TEMPLATE:
833 gdb_assert (last_template == NULL);
834 last_template = ret_comp;
835 ret_comp = d_left (ret_comp);
836 break;
fb4c6eba
DJ
837 case DEMANGLE_COMPONENT_CONST:
838 case DEMANGLE_COMPONENT_RESTRICT:
839 case DEMANGLE_COMPONENT_VOLATILE:
840 case DEMANGLE_COMPONENT_CONST_THIS:
841 case DEMANGLE_COMPONENT_RESTRICT_THIS:
842 case DEMANGLE_COMPONENT_VOLATILE_THIS:
843 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
dda83cd7
SM
844 ret_comp = d_left (ret_comp);
845 break;
fb4c6eba 846 case DEMANGLE_COMPONENT_NAME:
fb4c6eba
DJ
847 case DEMANGLE_COMPONENT_CTOR:
848 case DEMANGLE_COMPONENT_DTOR:
849 case DEMANGLE_COMPONENT_OPERATOR:
850 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
851 done = 1;
852 break;
853 default:
5e5100cb 854 return NULL;
fb4c6eba
DJ
855 break;
856 }
857
5e5100cb
DJ
858 if (last_template)
859 {
860 d_left (last_template) = ret_comp;
861 return last_template;
862 }
863
864 return ret_comp;
865}
866
867/* Return the name of the method whose linkage name is PHYSNAME. */
868
869char *
870method_name_from_physname (const char *physname)
871{
de237128 872 void *storage = NULL;
3456e70c 873 gdb::unique_xmalloc_ptr<char> demangled_name;
29592bde 874 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb 875 struct demangle_component *ret_comp;
c8b23b3f 876 std::unique_ptr<demangle_parse_info> info;
5e5100cb 877
3a93a0c2
KS
878 info = mangled_name_to_comp (physname, DMGL_ANSI,
879 &storage, &demangled_name);
880 if (info == NULL)
5e5100cb
DJ
881 return NULL;
882
3a93a0c2 883 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb 884
fb4c6eba 885 if (ret_comp != NULL)
aff410f1
MS
886 /* The ten is completely arbitrary; we don't have a good
887 estimate. */
fb4c6eba
DJ
888 ret = cp_comp_to_string (ret_comp, 10);
889
890 xfree (storage);
29592bde 891 return ret.release ();
fb4c6eba 892}
de17c821 893
5e5100cb
DJ
894/* If FULL_NAME is the demangled name of a C++ function (including an
895 arg list, possibly including namespace/class qualifications),
896 return a new string containing only the function name (without the
06d3e5b0 897 arg list/class qualifications). Otherwise, return NULL. */
5e5100cb 898
06d3e5b0 899gdb::unique_xmalloc_ptr<char>
5e5100cb
DJ
900cp_func_name (const char *full_name)
901{
29592bde 902 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb 903 struct demangle_component *ret_comp;
c8b23b3f 904 std::unique_ptr<demangle_parse_info> info;
5e5100cb 905
3a93a0c2
KS
906 info = cp_demangled_name_to_comp (full_name, NULL);
907 if (!info)
06d3e5b0 908 return nullptr;
5e5100cb 909
3a93a0c2 910 ret_comp = unqualified_name_from_comp (info->tree);
5e5100cb 911
5e5100cb
DJ
912 if (ret_comp != NULL)
913 ret = cp_comp_to_string (ret_comp, 10);
914
06d3e5b0 915 return ret;
5e5100cb
DJ
916}
917
c62446b1
PA
918/* Helper for cp_remove_params. DEMANGLED_NAME is the name of a
919 function, including parameters and (optionally) a return type.
920 Return the name of the function without parameters or return type,
921 or NULL if we can not parse the name. If REQUIRE_PARAMS is false,
922 then tolerate a non-existing or unbalanced parameter list. */
5e5100cb 923
c62446b1
PA
924static gdb::unique_xmalloc_ptr<char>
925cp_remove_params_1 (const char *demangled_name, bool require_params)
5e5100cb 926{
109483d9 927 bool done = false;
5e5100cb 928 struct demangle_component *ret_comp;
c8b23b3f 929 std::unique_ptr<demangle_parse_info> info;
29592bde 930 gdb::unique_xmalloc_ptr<char> ret;
5e5100cb
DJ
931
932 if (demangled_name == NULL)
933 return NULL;
934
3a93a0c2
KS
935 info = cp_demangled_name_to_comp (demangled_name, NULL);
936 if (info == NULL)
5e5100cb
DJ
937 return NULL;
938
939 /* First strip off any qualifiers, if we have a function or method. */
3a93a0c2 940 ret_comp = info->tree;
5e5100cb
DJ
941 while (!done)
942 switch (ret_comp->type)
943 {
944 case DEMANGLE_COMPONENT_CONST:
945 case DEMANGLE_COMPONENT_RESTRICT:
946 case DEMANGLE_COMPONENT_VOLATILE:
947 case DEMANGLE_COMPONENT_CONST_THIS:
948 case DEMANGLE_COMPONENT_RESTRICT_THIS:
949 case DEMANGLE_COMPONENT_VOLATILE_THIS:
950 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
dda83cd7
SM
951 ret_comp = d_left (ret_comp);
952 break;
5e5100cb 953 default:
109483d9 954 done = true;
5e5100cb
DJ
955 break;
956 }
957
958 /* What we have now should be a function. Return its name. */
959 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
960 ret = cp_comp_to_string (d_left (ret_comp), 10);
c62446b1
PA
961 else if (!require_params
962 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
963 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
964 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE))
965 ret = cp_comp_to_string (ret_comp, 10);
5e5100cb 966
109483d9 967 return ret;
5e5100cb
DJ
968}
969
c62446b1
PA
970/* DEMANGLED_NAME is the name of a function, including parameters and
971 (optionally) a return type. Return the name of the function
972 without parameters or return type, or NULL if we can not parse the
973 name. */
974
975gdb::unique_xmalloc_ptr<char>
976cp_remove_params (const char *demangled_name)
977{
978 return cp_remove_params_1 (demangled_name, true);
979}
980
981/* See cp-support.h. */
982
983gdb::unique_xmalloc_ptr<char>
984cp_remove_params_if_any (const char *demangled_name, bool completion_mode)
985{
986 /* Trying to remove parameters from the empty string fails. If
987 we're completing / matching everything, avoid returning NULL
988 which would make callers interpret the result as an error. */
989 if (demangled_name[0] == '\0' && completion_mode)
b02f78f9 990 return make_unique_xstrdup ("");
c62446b1
PA
991
992 gdb::unique_xmalloc_ptr<char> without_params
993 = cp_remove_params_1 (demangled_name, false);
994
995 if (without_params == NULL && completion_mode)
996 {
997 std::string copy = demangled_name;
998
999 while (!copy.empty ())
1000 {
1001 copy.pop_back ();
1002 without_params = cp_remove_params_1 (copy.c_str (), false);
1003 if (without_params != NULL)
1004 break;
1005 }
1006 }
1007
1008 return without_params;
1009}
1010
fb4c6eba
DJ
1011/* Here are some random pieces of trivia to keep in mind while trying
1012 to take apart demangled names:
de17c821 1013
fb4c6eba
DJ
1014 - Names can contain function arguments or templates, so the process
1015 has to be, to some extent recursive: maybe keep track of your
1016 depth based on encountering <> and ().
1017
1018 - Parentheses don't just have to happen at the end of a name: they
1019 can occur even if the name in question isn't a function, because
1020 a template argument might be a type that's a function.
1021
1022 - Conversely, even if you're trying to deal with a function, its
1023 demangled name might not end with ')': it could be a const or
1024 volatile class method, in which case it ends with "const" or
1025 "volatile".
1026
1027 - Parentheses are also used in anonymous namespaces: a variable
1028 'foo' in an anonymous namespace gets demangled as "(anonymous
1029 namespace)::foo".
1030
1031 - And operator names can contain parentheses or angle brackets. */
1032
1033/* FIXME: carlton/2003-03-13: We have several functions here with
1034 overlapping functionality; can we combine them? Also, do they
1035 handle all the above considerations correctly? */
de17c821 1036
9219021c
DC
1037
1038/* This returns the length of first component of NAME, which should be
1039 the demangled name of a C++ variable/function/method/etc.
1040 Specifically, it returns the index of the first colon forming the
1041 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
1042 it returns the 1, and given 'foo', it returns 0. */
1043
b2a7f303
DC
1044/* The character in NAME indexed by the return value is guaranteed to
1045 always be either ':' or '\0'. */
9219021c
DC
1046
1047/* NOTE: carlton/2003-03-13: This function is currently only intended
1048 for internal use: it's probably not entirely safe when called on
b2a7f303
DC
1049 user-generated input, because some of the 'index += 2' lines in
1050 cp_find_first_component_aux might go past the end of malformed
1051 input. */
1052
1053unsigned int
1054cp_find_first_component (const char *name)
1055{
1056 return cp_find_first_component_aux (name, 0);
1057}
1058
1059/* Helper function for cp_find_first_component. Like that function,
1060 it returns the length of the first component of NAME, but to make
1061 the recursion easier, it also stops if it reaches an unexpected ')'
1062 or '>' if the value of PERMISSIVE is nonzero. */
9219021c 1063
b2a7f303
DC
1064static unsigned int
1065cp_find_first_component_aux (const char *name, int permissive)
9219021c 1066{
9219021c 1067 unsigned int index = 0;
0f20eeea
DC
1068 /* Operator names can show up in unexpected places. Since these can
1069 contain parentheses or angle brackets, they can screw up the
1070 recursion. But not every string 'operator' is part of an
30baf67b 1071 operator name: e.g. you could have a variable 'cooperator'. So
0f20eeea
DC
1072 this variable tells us whether or not we should treat the string
1073 'operator' as starting an operator. */
1074 int operator_possible = 1;
9219021c
DC
1075
1076 for (;; ++index)
1077 {
1078 switch (name[index])
1079 {
1080 case '<':
1081 /* Template; eat it up. The calls to cp_first_component
1082 should only return (I hope!) when they reach the '>'
1083 terminating the component or a '::' between two
1084 components. (Hence the '+ 2'.) */
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 /* Similar comment as to '<'. */
1101 index += 1;
b2a7f303 1102 for (index += cp_find_first_component_aux (name + index, 1);
9219021c 1103 name[index] != ')';
b2a7f303 1104 index += cp_find_first_component_aux (name + index, 1))
9219021c 1105 {
b2a7f303
DC
1106 if (name[index] != ':')
1107 {
1108 demangled_name_complaint (name);
1109 return strlen (name);
1110 }
9219021c
DC
1111 index += 2;
1112 }
0f20eeea 1113 operator_possible = 1;
9219021c
DC
1114 break;
1115 case '>':
1116 case ')':
b2a7f303 1117 if (permissive)
7a20f2c2 1118 return index;
b2a7f303
DC
1119 else
1120 {
1121 demangled_name_complaint (name);
1122 return strlen (name);
1123 }
9219021c 1124 case '\0':
9219021c 1125 return index;
1cafadb4
DB
1126 case ':':
1127 /* ':' marks a component iff the next character is also a ':'.
1128 Otherwise it is probably malformed input. */
1129 if (name[index + 1] == ':')
1130 return index;
1131 break;
0f20eeea
DC
1132 case 'o':
1133 /* Operator names can screw up the recursion. */
1134 if (operator_possible
8090b426 1135 && startswith (name + index, CP_OPERATOR_STR))
0f20eeea 1136 {
8090b426 1137 index += CP_OPERATOR_LEN;
f88e9fd3 1138 while (ISSPACE(name[index]))
0f20eeea
DC
1139 ++index;
1140 switch (name[index])
1141 {
cf325299
PA
1142 case '\0':
1143 return index;
0f20eeea
DC
1144 /* Skip over one less than the appropriate number of
1145 characters: the for loop will skip over the last
1146 one. */
1147 case '<':
1148 if (name[index + 1] == '<')
1149 index += 1;
1150 else
1151 index += 0;
1152 break;
1153 case '>':
1154 case '-':
1155 if (name[index + 1] == '>')
1156 index += 1;
1157 else
1158 index += 0;
1159 break;
1160 case '(':
1161 index += 1;
1162 break;
1163 default:
1164 index += 0;
1165 break;
1166 }
1167 }
1168 operator_possible = 0;
1169 break;
1170 case ' ':
1171 case ',':
1172 case '.':
1173 case '&':
1174 case '*':
1175 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1176 set of relevant characters are here: it's necessary to
1177 include any character that can show up before 'operator'
1178 in a demangled name, and it's safe to include any
1179 character that can't be part of an identifier's name. */
1180 operator_possible = 1;
1181 break;
9219021c 1182 default:
0f20eeea 1183 operator_possible = 0;
9219021c
DC
1184 break;
1185 }
1186 }
1187}
1188
b2a7f303
DC
1189/* Complain about a demangled name that we don't know how to parse.
1190 NAME is the demangled name in question. */
1191
1192static void
1193demangled_name_complaint (const char *name)
1194{
b98664d3 1195 complaint ("unexpected demangled name '%s'", name);
b2a7f303
DC
1196}
1197
9219021c
DC
1198/* If NAME is the fully-qualified name of a C++
1199 function/variable/method/etc., this returns the length of its
1200 entire prefix: all of the namespaces and classes that make up its
1201 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1202 4, given 'foo', it returns 0. */
1203
1204unsigned int
1205cp_entire_prefix_len (const char *name)
1206{
1207 unsigned int current_len = cp_find_first_component (name);
1208 unsigned int previous_len = 0;
1209
1210 while (name[current_len] != '\0')
1211 {
1212 gdb_assert (name[current_len] == ':');
1213 previous_len = current_len;
1214 /* Skip the '::'. */
1215 current_len += 2;
1216 current_len += cp_find_first_component (name + current_len);
1217 }
1218
1219 return previous_len;
1220}
1221
b6429628
DC
1222/* Overload resolution functions. */
1223
8d577d32 1224/* Test to see if SYM is a symbol that we haven't seen corresponding
0891c3cc
PA
1225 to a function named OLOAD_NAME. If so, add it to
1226 OVERLOAD_LIST. */
b6429628
DC
1227
1228static void
aff410f1 1229overload_list_add_symbol (struct symbol *sym,
0891c3cc
PA
1230 const char *oload_name,
1231 std::vector<symbol *> *overload_list)
b6429628 1232{
aff410f1
MS
1233 /* If there is no type information, we can't do anything, so
1234 skip. */
5f9c5a63 1235 if (sym->type () == NULL)
b6429628
DC
1236 return;
1237
aff410f1 1238 /* skip any symbols that we've already considered. */
0891c3cc 1239 for (symbol *listed_sym : *overload_list)
987012b8 1240 if (strcmp (sym->linkage_name (), listed_sym->linkage_name ()) == 0)
b6429628
DC
1241 return;
1242
1243 /* Get the demangled name without parameters */
0891c3cc 1244 gdb::unique_xmalloc_ptr<char> sym_name
987012b8 1245 = cp_remove_params (sym->natural_name ());
b6429628
DC
1246 if (!sym_name)
1247 return;
1248
1249 /* skip symbols that cannot match */
109483d9
PA
1250 if (strcmp (sym_name.get (), oload_name) != 0)
1251 return;
b6429628 1252
0891c3cc 1253 overload_list->push_back (sym);
b6429628
DC
1254}
1255
1256/* Return a null-terminated list of pointers to function symbols that
8d577d32 1257 are named FUNC_NAME and are visible within NAMESPACE. */
b6429628 1258
0891c3cc 1259struct std::vector<symbol *>
8d577d32 1260make_symbol_overload_list (const char *func_name,
fe978cb0 1261 const char *the_namespace)
b6429628 1262{
245040d7 1263 const char *name;
0891c3cc 1264 std::vector<symbol *> overload_list;
b6429628 1265
0891c3cc 1266 overload_list.reserve (100);
8d577d32 1267
0891c3cc 1268 add_symbol_overload_list_using (func_name, the_namespace, &overload_list);
8d577d32 1269
fe978cb0 1270 if (the_namespace[0] == '\0')
245040d7
SW
1271 name = func_name;
1272 else
1273 {
1274 char *concatenated_name
224c3ddb 1275 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
fe978cb0 1276 strcpy (concatenated_name, the_namespace);
245040d7
SW
1277 strcat (concatenated_name, "::");
1278 strcat (concatenated_name, func_name);
1279 name = concatenated_name;
1280 }
1281
0891c3cc
PA
1282 add_symbol_overload_list_qualified (name, &overload_list);
1283 return overload_list;
8d577d32
DC
1284}
1285
245040d7
SW
1286/* Add all symbols with a name matching NAME in BLOCK to the overload
1287 list. */
1288
1289static void
0891c3cc
PA
1290add_symbol_overload_list_block (const char *name,
1291 const struct block *block,
1292 std::vector<symbol *> *overload_list)
245040d7 1293{
b5ec771e
PA
1294 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1295
a1b29426 1296 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
0891c3cc 1297 overload_list_add_symbol (sym, name, overload_list);
245040d7
SW
1298}
1299
7322dca9
SW
1300/* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1301
1302static void
0891c3cc
PA
1303add_symbol_overload_list_namespace (const char *func_name,
1304 const char *the_namespace,
1305 std::vector<symbol *> *overload_list)
7322dca9 1306{
245040d7
SW
1307 const char *name;
1308 const struct block *block = NULL;
1309
fe978cb0 1310 if (the_namespace[0] == '\0')
245040d7 1311 name = func_name;
7322dca9
SW
1312 else
1313 {
1314 char *concatenated_name
224c3ddb 1315 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
c5504eaf 1316
fe978cb0 1317 strcpy (concatenated_name, the_namespace);
7322dca9
SW
1318 strcat (concatenated_name, "::");
1319 strcat (concatenated_name, func_name);
245040d7 1320 name = concatenated_name;
7322dca9 1321 }
245040d7
SW
1322
1323 /* Look in the static block. */
78004096 1324 block = get_selected_block (0);
d24e14a0 1325 block = block == nullptr ? nullptr : block->static_block ();
8f14fd11
TT
1326 if (block != nullptr)
1327 {
1328 add_symbol_overload_list_block (name, block, overload_list);
245040d7 1329
8f14fd11 1330 /* Look in the global block. */
d24e14a0 1331 block = block->global_block ();
8f14fd11
TT
1332 if (block)
1333 add_symbol_overload_list_block (name, block, overload_list);
1334 }
7322dca9
SW
1335}
1336
aff410f1
MS
1337/* Search the namespace of the given type and namespace of and public
1338 base types. */
7322dca9
SW
1339
1340static void
0891c3cc
PA
1341add_symbol_overload_list_adl_namespace (struct type *type,
1342 const char *func_name,
1343 std::vector<symbol *> *overload_list)
7322dca9 1344{
fe978cb0 1345 char *the_namespace;
0d5cff50 1346 const char *type_name;
7322dca9
SW
1347 int i, prefix_len;
1348
809f3be1 1349 while (type->is_pointer_or_reference ()
dda83cd7
SM
1350 || type->code () == TYPE_CODE_ARRAY
1351 || type->code () == TYPE_CODE_TYPEDEF)
7322dca9 1352 {
78134374
SM
1353 if (type->code () == TYPE_CODE_TYPEDEF)
1354 type = check_typedef (type);
7322dca9 1355 else
27710edb 1356 type = type->target_type ();
7322dca9
SW
1357 }
1358
7d93a1e0 1359 type_name = type->name ();
7322dca9 1360
7d3fe98e
SW
1361 if (type_name == NULL)
1362 return;
1363
7322dca9
SW
1364 prefix_len = cp_entire_prefix_len (type_name);
1365
1366 if (prefix_len != 0)
1367 {
224c3ddb 1368 the_namespace = (char *) alloca (prefix_len + 1);
fe978cb0
PA
1369 strncpy (the_namespace, type_name, prefix_len);
1370 the_namespace[prefix_len] = '\0';
7322dca9 1371
0891c3cc
PA
1372 add_symbol_overload_list_namespace (func_name, the_namespace,
1373 overload_list);
7322dca9
SW
1374 }
1375
1376 /* Check public base type */
78134374 1377 if (type->code () == TYPE_CODE_STRUCT)
7322dca9
SW
1378 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1379 {
1380 if (BASETYPE_VIA_PUBLIC (type, i))
0891c3cc
PA
1381 add_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, i),
1382 func_name,
1383 overload_list);
7322dca9
SW
1384 }
1385}
1386
0891c3cc
PA
1387/* Adds to OVERLOAD_LIST the overload list overload candidates for
1388 FUNC_NAME found through argument dependent lookup. */
7322dca9 1389
0891c3cc
PA
1390void
1391add_symbol_overload_list_adl (gdb::array_view<type *> arg_types,
1392 const char *func_name,
1393 std::vector<symbol *> *overload_list)
7322dca9 1394{
0891c3cc
PA
1395 for (type *arg_type : arg_types)
1396 add_symbol_overload_list_adl_namespace (arg_type, func_name,
1397 overload_list);
7322dca9
SW
1398}
1399
8d577d32
DC
1400/* This applies the using directives to add namespaces to search in,
1401 and then searches for overloads in all of those namespaces. It
1402 adds the symbols found to sym_return_val. Arguments are as in
1403 make_symbol_overload_list. */
1404
1405static void
0891c3cc
PA
1406add_symbol_overload_list_using (const char *func_name,
1407 const char *the_namespace,
1408 std::vector<symbol *> *overload_list)
8d577d32 1409{
4c3376c8 1410 const struct block *block;
8d577d32
DC
1411
1412 /* First, go through the using directives. If any of them apply,
1413 look in the appropriate namespaces for new functions to match
1414 on. */
b6429628 1415
4c3376c8
SW
1416 for (block = get_selected_block (0);
1417 block != NULL;
f135fe72 1418 block = block->superblock ())
b6829c3c 1419 for (using_direct *current : block->get_using ())
4c3376c8 1420 {
19c0c0f8
UW
1421 /* Prevent recursive calls. */
1422 if (current->searched)
1423 continue;
1424
dda83cd7 1425 /* If this is a namespace alias or imported declaration ignore
aff410f1 1426 it. */
dda83cd7
SM
1427 if (current->alias != NULL || current->declaration != NULL)
1428 continue;
4c3376c8 1429
dda83cd7 1430 if (strcmp (the_namespace, current->import_dest) == 0)
19c0c0f8 1431 {
aff410f1
MS
1432 /* Mark this import as searched so that the recursive call
1433 does not search it again. */
167b0be1
TT
1434 scoped_restore reset_directive_searched
1435 = make_scoped_restore (&current->searched, 1);
19c0c0f8 1436
0891c3cc
PA
1437 add_symbol_overload_list_using (func_name,
1438 current->import_src,
1439 overload_list);
19c0c0f8 1440 }
4c3376c8 1441 }
b6429628 1442
8d577d32 1443 /* Now, add names for this namespace. */
0891c3cc
PA
1444 add_symbol_overload_list_namespace (func_name, the_namespace,
1445 overload_list);
8d577d32 1446}
b6429628 1447
8d577d32
DC
1448/* This does the bulk of the work of finding overloaded symbols.
1449 FUNC_NAME is the name of the overloaded function we're looking for
1450 (possibly including namespace info). */
b6429628 1451
8d577d32 1452static void
0891c3cc
PA
1453add_symbol_overload_list_qualified (const char *func_name,
1454 std::vector<symbol *> *overload_list)
8d577d32 1455{
2cf6f573 1456 const block *selected_block = get_selected_block (0);
b6429628
DC
1457
1458 /* Search upwards from currently selected frame (so that we can
1459 complete on local vars. */
1460
2cf6f573 1461 for (const block *b = selected_block; b != nullptr; b = b->superblock ())
0891c3cc 1462 add_symbol_overload_list_block (func_name, b, overload_list);
b6429628 1463
2cf6f573
TT
1464 const block *surrounding_static_block = (selected_block == nullptr
1465 ? nullptr
1466 : selected_block->static_block ());
8d577d32 1467
b6429628
DC
1468 /* Go through the symtabs and check the externs and statics for
1469 symbols which match. */
1470
2cf6f573
TT
1471 struct objfile *current_objfile = (selected_block
1472 ? selected_block->objfile ()
1473 : nullptr);
2aab2438
MM
1474
1475 gdbarch_iterate_over_objfiles_in_search_order
99d9c3b9 1476 (current_objfile ? current_objfile->arch () : current_inferior ()->arch (),
2aab2438
MM
1477 [func_name, surrounding_static_block, &overload_list]
1478 (struct objfile *obj)
1479 {
2cf6f573
TT
1480 /* Look through the partial symtabs for all symbols which
1481 begin by matching FUNC_NAME. Make sure we read that
1482 symbol table in. */
1483 obj->expand_symtabs_for_function (func_name);
1484
2aab2438
MM
1485 for (compunit_symtab *cust : obj->compunits ())
1486 {
1487 QUIT;
1488 const struct block *b = cust->blockvector ()->global_block ();
1489 add_symbol_overload_list_block (func_name, b, overload_list);
1490
1491 b = cust->blockvector ()->static_block ();
1492 /* Don't do this block twice. */
1493 if (b == surrounding_static_block)
1494 continue;
1495
1496 add_symbol_overload_list_block (func_name, b, overload_list);
1497 }
1498
1499 return 0;
1500 }, current_objfile);
8d577d32
DC
1501}
1502
aff410f1 1503/* Lookup the rtti type for a class name. */
362ff856
MC
1504
1505struct type *
582942f4 1506cp_lookup_rtti_type (const char *name, const struct block *block)
362ff856
MC
1507{
1508 struct symbol * rtti_sym;
1509 struct type * rtti_type;
1510
033bc67b
TT
1511 rtti_sym = lookup_symbol (name, block,
1512 SEARCH_TYPE_DOMAIN | SEARCH_STRUCT_DOMAIN,
1513 nullptr).symbol;
362ff856
MC
1514
1515 if (rtti_sym == NULL)
1516 {
8a3fe4f8 1517 warning (_("RTTI symbol not found for class '%s'"), name);
362ff856
MC
1518 return NULL;
1519 }
1520
66d7f48f 1521 if (rtti_sym->aclass () != LOC_TYPEDEF)
362ff856 1522 {
8a3fe4f8 1523 warning (_("RTTI symbol for class '%s' is not a type"), name);
362ff856
MC
1524 return NULL;
1525 }
1526
5f9c5a63 1527 rtti_type = check_typedef (rtti_sym->type ());
362ff856 1528
78134374 1529 switch (rtti_type->code ())
362ff856 1530 {
4753d33b 1531 case TYPE_CODE_STRUCT:
362ff856
MC
1532 break;
1533 case TYPE_CODE_NAMESPACE:
1534 /* chastain/2003-11-26: the symbol tables often contain fake
1535 symbols for namespaces with the same name as the struct.
1536 This warning is an indication of a bug in the lookup order
1537 or a bug in the way that the symbol tables are populated. */
8a3fe4f8 1538 warning (_("RTTI symbol for class '%s' is a namespace"), name);
362ff856
MC
1539 return NULL;
1540 default:
8a3fe4f8 1541 warning (_("RTTI symbol for class '%s' has bad type"), name);
362ff856
MC
1542 return NULL;
1543 }
1544
1545 return rtti_type;
1546}
b6429628 1547
992c7d70
GB
1548#ifdef HAVE_WORKING_FORK
1549
491144b5 1550/* If true, attempt to catch crashes in the demangler and print
992c7d70
GB
1551 useful debugging information. */
1552
491144b5 1553static bool catch_demangler_crashes = true;
992c7d70 1554
992c7d70
GB
1555/* Stack context and environment for demangler crash recovery. */
1556
3b3978bc 1557static thread_local SIGJMP_BUF *gdb_demangle_jmp_buf;
992c7d70 1558
3b3978bc 1559/* If true, attempt to dump core from the signal handler. */
992c7d70 1560
3b3978bc 1561static std::atomic<bool> gdb_demangle_attempt_core_dump;
992c7d70
GB
1562
1563/* Signal handler for gdb_demangle. */
1564
1565static void
1566gdb_demangle_signal_handler (int signo)
1567{
1568 if (gdb_demangle_attempt_core_dump)
1569 {
1570 if (fork () == 0)
1571 dump_core ();
1572
3b3978bc 1573 gdb_demangle_attempt_core_dump = false;
992c7d70
GB
1574 }
1575
3b3978bc
TT
1576 SIGLONGJMP (*gdb_demangle_jmp_buf, signo);
1577}
1578
1579/* A helper for gdb_demangle that reports a demangling failure. */
1580
1581static void
1582report_failed_demangle (const char *name, bool core_dump_allowed,
1583 int crash_signal)
1584{
1585 static bool error_reported = false;
1586
1587 if (!error_reported)
1588 {
1589 std::string short_msg
1590 = string_printf (_("unable to demangle '%s' "
1591 "(demangler failed with signal %d)"),
1592 name, crash_signal);
1593
1594 std::string long_msg
1595 = string_printf ("%s:%d: %s: %s", __FILE__, __LINE__,
1596 "demangler-warning", short_msg.c_str ());
1597
1598 target_terminal::scoped_restore_terminal_state term_state;
1599 target_terminal::ours_for_output ();
1600
1601 begin_line ();
1602 if (core_dump_allowed)
6cb06a8c
TT
1603 gdb_printf (gdb_stderr,
1604 _("%s\nAttempting to dump core.\n"),
1605 long_msg.c_str ());
3b3978bc
TT
1606 else
1607 warn_cant_dump_core (long_msg.c_str ());
1608
1609 demangler_warning (__FILE__, __LINE__, "%s", short_msg.c_str ());
1610
1611 error_reported = true;
1612 }
992c7d70
GB
1613}
1614
1615#endif
1616
8de20a37
TT
1617/* A wrapper for bfd_demangle. */
1618
3456e70c 1619gdb::unique_xmalloc_ptr<char>
8de20a37
TT
1620gdb_demangle (const char *name, int options)
1621{
3456e70c 1622 gdb::unique_xmalloc_ptr<char> result;
992c7d70
GB
1623 int crash_signal = 0;
1624
1625#ifdef HAVE_WORKING_FORK
fece451c
CB
1626 scoped_segv_handler_restore restore_segv
1627 (catch_demangler_crashes
1628 ? gdb_demangle_signal_handler
1629 : nullptr);
3b3978bc
TT
1630
1631 bool core_dump_allowed = gdb_demangle_attempt_core_dump;
1632 SIGJMP_BUF jmp_buf;
1633 scoped_restore restore_jmp_buf
1634 = make_scoped_restore (&gdb_demangle_jmp_buf, &jmp_buf);
992c7d70
GB
1635 if (catch_demangler_crashes)
1636 {
17bfe554 1637 /* The signal handler may keep the signal blocked when we longjmp out
dda83cd7 1638 of it. If we have sigprocmask, we can use it to unblock the signal
17bfe554
CB
1639 afterwards and we can avoid the performance overhead of saving the
1640 signal mask just in case the signal gets triggered. Otherwise, just
1641 tell sigsetjmp to save the mask. */
1642#ifdef HAVE_SIGPROCMASK
3b3978bc 1643 crash_signal = SIGSETJMP (*gdb_demangle_jmp_buf, 0);
17bfe554 1644#else
3b3978bc 1645 crash_signal = SIGSETJMP (*gdb_demangle_jmp_buf, 1);
17bfe554 1646#endif
992c7d70
GB
1647 }
1648#endif
1649
1650 if (crash_signal == 0)
cb2cd8cb 1651 result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE));
992c7d70
GB
1652
1653#ifdef HAVE_WORKING_FORK
1654 if (catch_demangler_crashes)
1655 {
992c7d70 1656 if (crash_signal != 0)
dda83cd7 1657 {
17bfe554
CB
1658#ifdef HAVE_SIGPROCMASK
1659 /* If we got the signal, SIGSEGV may still be blocked; restore it. */
1660 sigset_t segv_sig_set;
1661 sigemptyset (&segv_sig_set);
1662 sigaddset (&segv_sig_set, SIGSEGV);
21987b9c 1663 gdb_sigmask (SIG_UNBLOCK, &segv_sig_set, NULL);
17bfe554
CB
1664#endif
1665
3b3978bc
TT
1666 /* If there was a failure, we can't report it here, because
1667 we might be in a background thread. Instead, arrange for
1668 the reporting to happen on the main thread. */
dda83cd7 1669 std::string copy = name;
cc1cc406 1670 run_on_main_thread ([=, copy = std::move (copy)] ()
dda83cd7
SM
1671 {
1672 report_failed_demangle (copy.c_str (), core_dump_allowed,
1673 crash_signal);
1674 });
1675
1676 result = NULL;
1677 }
992c7d70
GB
1678 }
1679#endif
1680
1681 return result;
8de20a37
TT
1682}
1683
8b302db8
TT
1684/* See cp-support.h. */
1685
cb2cd8cb
PA
1686char *
1687gdb_cplus_demangle_print (int options,
1688 struct demangle_component *tree,
1689 int estimated_length,
1690 size_t *p_allocated_size)
1691{
1692 return cplus_demangle_print (options | DMGL_VERBOSE, tree,
1693 estimated_length, p_allocated_size);
1694}
1695
1696/* A wrapper for cplus_demangle_v3_components that forces
1697 DMGL_VERBOSE. */
1698
1699static struct demangle_component *
1700gdb_cplus_demangle_v3_components (const char *mangled,
1701 int options, void **mem)
1702{
1703 return cplus_demangle_v3_components (mangled, options | DMGL_VERBOSE, mem);
1704}
1705
1706/* See cp-support.h. */
1707
a20714ff
PA
1708unsigned int
1709cp_search_name_hash (const char *search_name)
1710{
1711 /* cp_entire_prefix_len assumes a fully-qualified name with no
1712 leading "::". */
1713 if (startswith (search_name, "::"))
1714 search_name += 2;
1715
1716 unsigned int prefix_len = cp_entire_prefix_len (search_name);
1717 if (prefix_len != 0)
1718 search_name += prefix_len + 2;
1719
bd69330d
PA
1720 unsigned int hash = 0;
1721 for (const char *string = search_name; *string != '\0'; ++string)
1722 {
af3f129d 1723 const char *before_skip = string;
bd69330d
PA
1724 string = skip_spaces (string);
1725
1726 if (*string == '(')
1727 break;
1728
af3f129d
SV
1729 /* Could it be the beginning of a function name?
1730 If yes, does it begin with the keyword "operator"? */
1731 if ((string != before_skip || string == search_name)
1732 && (string[0] == 'o' && startswith (string, CP_OPERATOR_STR)))
1733 {
1734 /* Hash the "operator" part. */
1735 for (size_t i = 0; i < CP_OPERATOR_LEN; ++i)
1736 hash = SYMBOL_HASH_NEXT (hash, *string++);
1737
1738 string = skip_spaces (string);
1739
1740 /* If no more data to process, stop right now. This is specially
1741 intended for SEARCH_NAMEs that end with "operator". In such
1742 cases, the whole string is processed and STRING is pointing to a
1743 null-byte. Letting the loop body resume naturally would lead to
1744 a "++string" that causes STRING to point past the null-byte. */
1745 if (string[0] == '\0')
1746 break;
1747
1748 /* "<" and "<<" are sequences of interest here. This covers
1749 "operator{<,<<,<=,<=>}". In the last 2 cases, the "=" and "=>"
1750 parts are handled by the next iterations of the loop like other
1751 input chars. The goal is to process all the operator-related '<'
1752 chars, so that later if a '<' is visited it can be inferred for
1753 sure that it is the beginning of a template parameter list.
1754
1755 STRING is a null-byte terminated string. If string[0] is not
1756 a null-byte, according to the previous check, string[1] is not
1757 past the end of the allocation and can be referenced safely. */
1758 if (string[0] == '<')
1759 {
1760 hash = SYMBOL_HASH_NEXT (hash, *string);
1761 if (string[1] == '<')
1762 hash = SYMBOL_HASH_NEXT (hash, *++string);
1763 continue;
1764 }
1765 }
1766
bd69330d
PA
1767 /* Ignore ABI tags such as "[abi:cxx11]. */
1768 if (*string == '['
1769 && startswith (string + 1, "abi:")
1770 && string[5] != ':')
1771 break;
1772
af3f129d
SV
1773 /* Ignore template parameter lists. The likely "operator{<,<<,<=,<=>}"
1774 are already taken care of. Therefore, any encounter of '<' character
1775 at this point is related to template lists. */
1776 if (*string == '<')
64a97606
KS
1777 break;
1778
bd69330d
PA
1779 hash = SYMBOL_HASH_NEXT (hash, *string);
1780 }
1781 return hash;
a20714ff
PA
1782}
1783
af3f129d
SV
1784#if GDB_SELF_TEST
1785
1786namespace selftests {
1787
1788static void
1789test_cp_search_name_hash ()
1790{
1791 SELF_CHECK (cp_search_name_hash ("void func<(enum_test)0>(int*, int)")
1792 == cp_search_name_hash ("void func"));
1793 SELF_CHECK (cp_search_name_hash ("operator")
1794 != cp_search_name_hash ("operator<"));
1795 SELF_CHECK (cp_search_name_hash ("operator")
1796 != cp_search_name_hash ("operator<<"));
1797 SELF_CHECK (cp_search_name_hash ("operator<")
1798 != cp_search_name_hash ("operator<<"));
1799 SELF_CHECK (cp_search_name_hash ("operator<")
1800 == cp_search_name_hash ("operator <"));
1801 SELF_CHECK (cp_search_name_hash ("operator")
1802 != cp_search_name_hash ("foo_operator"));
1803 SELF_CHECK (cp_search_name_hash ("operator")
1804 != cp_search_name_hash ("operator_foo"));
1805 SELF_CHECK (cp_search_name_hash ("operator<")
1806 != cp_search_name_hash ("foo_operator"));
1807 SELF_CHECK (cp_search_name_hash ("operator<")
1808 != cp_search_name_hash ("operator_foo"));
1809 SELF_CHECK (cp_search_name_hash ("operator<<")
1810 != cp_search_name_hash ("foo_operator"));
1811 SELF_CHECK (cp_search_name_hash ("operator<<")
1812 != cp_search_name_hash ("operator_foo"));
1813
1814 SELF_CHECK (cp_search_name_hash ("func")
1815 == cp_search_name_hash ("func[abi:cxx11]"));
1816}
1817
1818} /* namespace selftests */
1819
1820#endif /* GDB_SELF_TEST */
1821
a20714ff
PA
1822/* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype
1823 implementation for symbol_name_match_type::WILD matching). Split
1824 to a separate function for unit-testing convenience.
1825
1826 If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to
1827 match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME.
1828 This allows conveniently setting breakpoints on functions/methods
1829 inside any namespace/class without specifying the fully-qualified
1830 name.
1831
1832 E.g., these match:
b5ec771e 1833
a20714ff
PA
1834 [symbol search name] [lookup name]
1835 foo::bar::func foo::bar::func
1836 foo::bar::func bar::func
1837 foo::bar::func func
1838
1839 While these don't:
1840
1841 [symbol search name] [lookup name]
1842 foo::zbar::func bar::func
1843 foo::bar::func foo::func
1844
1845 See more examples in the test_cp_symbol_name_matches selftest
1846 function below.
0662b6a7
PA
1847
1848 See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME
1849 and COMP_MATCH_RES.
1850
1851 LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up.
1852
1853 See strncmp_iw_with_mode for description of MODE.
1854*/
1855
1856static bool
1857cp_symbol_name_matches_1 (const char *symbol_search_name,
1858 const char *lookup_name,
1859 size_t lookup_name_len,
1860 strncmp_iw_mode mode,
a207cff2 1861 completion_match_result *comp_match_res)
0662b6a7 1862{
a20714ff 1863 const char *sname = symbol_search_name;
bd69330d
PA
1864 completion_match_for_lcd *match_for_lcd
1865 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
a20714ff 1866
454f8b67
AB
1867 gdb_assert (match_for_lcd == nullptr || match_for_lcd->empty ());
1868
a20714ff
PA
1869 while (true)
1870 {
1871 if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len,
64a97606 1872 mode, language_cplus, match_for_lcd, true) == 0)
a20714ff
PA
1873 {
1874 if (comp_match_res != NULL)
1875 {
1876 /* Note here we set different MATCH and MATCH_FOR_LCD
1877 strings. This is because with
1878
1879 (gdb) b push_bac[TAB]
1880
1881 we want the completion matches to list
1882
1883 std::vector<int>::push_back(...)
1884 std::vector<char>::push_back(...)
1885
1886 etc., which are SYMBOL_SEARCH_NAMEs, while we want
1887 the input line to auto-complete to
1888
1889 (gdb) push_back(...)
1890
1891 which is SNAME, not to
1892
1893 (gdb) std::vector<
1894
1895 which would be the regular common prefix between all
1896 the matches otherwise. */
1897 comp_match_res->set_match (symbol_search_name, sname);
1898 }
1899 return true;
1900 }
1901
454f8b67
AB
1902 /* Clear match_for_lcd so the next strncmp_iw_with_mode call starts
1903 from scratch. */
1904 if (match_for_lcd != nullptr)
1905 match_for_lcd->clear ();
1906
a20714ff
PA
1907 unsigned int len = cp_find_first_component (sname);
1908
1909 if (sname[len] == '\0')
1910 return false;
1911
1912 gdb_assert (sname[len] == ':');
1913 /* Skip the '::'. */
1914 sname += len + 2;
1915 }
1916}
1917
1918/* C++ symbol_name_matcher_ftype implementation. */
1919
1920static bool
1921cp_fq_symbol_name_matches (const char *symbol_search_name,
1922 const lookup_name_info &lookup_name,
1923 completion_match_result *comp_match_res)
1924{
1925 /* Get the demangled name. */
1926 const std::string &name = lookup_name.cplus ().lookup_name ();
bd69330d
PA
1927 completion_match_for_lcd *match_for_lcd
1928 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
a20714ff
PA
1929 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1930 ? strncmp_iw_mode::NORMAL
1931 : strncmp_iw_mode::MATCH_PARAMS);
1932
0662b6a7 1933 if (strncmp_iw_with_mode (symbol_search_name,
a20714ff 1934 name.c_str (), name.size (),
bd69330d 1935 mode, language_cplus, match_for_lcd) == 0)
0662b6a7 1936 {
a207cff2
PA
1937 if (comp_match_res != NULL)
1938 comp_match_res->set_match (symbol_search_name);
0662b6a7
PA
1939 return true;
1940 }
1941
1942 return false;
1943}
1944
a20714ff
PA
1945/* C++ symbol_name_matcher_ftype implementation for wild matches.
1946 Defers work to cp_symbol_name_matches_1. */
0662b6a7 1947
b5ec771e 1948static bool
a20714ff
PA
1949cp_symbol_name_matches (const char *symbol_search_name,
1950 const lookup_name_info &lookup_name,
1951 completion_match_result *comp_match_res)
b5ec771e
PA
1952{
1953 /* Get the demangled name. */
1954 const std::string &name = lookup_name.cplus ().lookup_name ();
1955
1956 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1957 ? strncmp_iw_mode::NORMAL
1958 : strncmp_iw_mode::MATCH_PARAMS);
1959
0662b6a7
PA
1960 return cp_symbol_name_matches_1 (symbol_search_name,
1961 name.c_str (), name.size (),
a207cff2 1962 mode, comp_match_res);
b5ec771e
PA
1963}
1964
1965/* See cp-support.h. */
1966
1967symbol_name_matcher_ftype *
1968cp_get_symbol_name_matcher (const lookup_name_info &lookup_name)
1969{
a20714ff
PA
1970 switch (lookup_name.match_type ())
1971 {
1972 case symbol_name_match_type::FULL:
1973 case symbol_name_match_type::EXPRESSION:
de63c46b 1974 case symbol_name_match_type::SEARCH_NAME:
a20714ff
PA
1975 return cp_fq_symbol_name_matches;
1976 case symbol_name_match_type::WILD:
1977 return cp_symbol_name_matches;
1978 }
1979
1980 gdb_assert_not_reached ("");
b5ec771e
PA
1981}
1982
c62446b1
PA
1983#if GDB_SELF_TEST
1984
1985namespace selftests {
1986
cb8c24b6 1987static void
0662b6a7
PA
1988test_cp_symbol_name_matches ()
1989{
1990#define CHECK_MATCH(SYMBOL, INPUT) \
1991 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \
1992 INPUT, sizeof (INPUT) - 1, \
1993 strncmp_iw_mode::MATCH_PARAMS, \
1994 NULL))
1995
1996#define CHECK_NOT_MATCH(SYMBOL, INPUT) \
1997 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \
1998 INPUT, sizeof (INPUT) - 1, \
1999 strncmp_iw_mode::MATCH_PARAMS, \
2000 NULL))
2001
2002 /* Like CHECK_MATCH, and also check that INPUT (and all substrings
2003 that start at index 0) completes to SYMBOL. */
2004#define CHECK_MATCH_C(SYMBOL, INPUT) \
2005 do \
2006 { \
2007 CHECK_MATCH (SYMBOL, INPUT); \
2008 for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \
2009 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \
2010 strncmp_iw_mode::NORMAL, \
2011 NULL)); \
2012 } while (0)
2013
2014 /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete
2015 to SYMBOL. */
2016#define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \
2017 do \
2018 { \
2019 CHECK_NOT_MATCH (SYMBOL, INPUT); \
2020 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \
2021 sizeof (INPUT) - 1, \
2022 strncmp_iw_mode::NORMAL, \
2023 NULL)); \
2024 } while (0)
2025
2026 /* Lookup name without parens matches all overloads. */
2027 CHECK_MATCH_C ("function()", "function");
2028 CHECK_MATCH_C ("function(int)", "function");
2029
2030 /* Check whitespace around parameters is ignored. */
2031 CHECK_MATCH_C ("function()", "function ()");
2032 CHECK_MATCH_C ("function ( )", "function()");
2033 CHECK_MATCH_C ("function ()", "function( )");
2034 CHECK_MATCH_C ("func(int)", "func( int )");
2035 CHECK_MATCH_C ("func(int)", "func ( int ) ");
2036 CHECK_MATCH_C ("func ( int )", "func( int )");
2037 CHECK_MATCH_C ("func ( int )", "func ( int ) ");
2038
2039 /* Check symbol name prefixes aren't incorrectly matched. */
2040 CHECK_NOT_MATCH ("func", "function");
2041 CHECK_NOT_MATCH ("function", "func");
2042 CHECK_NOT_MATCH ("function()", "func");
2043
2044 /* Check that if the lookup name includes parameters, only the right
2045 overload matches. */
2046 CHECK_MATCH_C ("function(int)", "function(int)");
2047 CHECK_NOT_MATCH_C ("function(int)", "function()");
2048
2049 /* Check that whitespace within symbol names is not ignored. */
2050 CHECK_NOT_MATCH_C ("function", "func tion");
2051 CHECK_NOT_MATCH_C ("func__tion", "func_ _tion");
2052 CHECK_NOT_MATCH_C ("func11tion", "func1 1tion");
2053
2054 /* Check the converse, which can happen with template function,
2055 where the return type is part of the demangled name. */
2056 CHECK_NOT_MATCH_C ("func tion", "function");
2057 CHECK_NOT_MATCH_C ("func1 1tion", "func11tion");
2058 CHECK_NOT_MATCH_C ("func_ _tion", "func__tion");
2059
2060 /* Within parameters too. */
2061 CHECK_NOT_MATCH_C ("func(param)", "func(par am)");
2062
2063 /* Check handling of whitespace around C++ operators. */
2064 CHECK_NOT_MATCH_C ("operator<<", "opera tor<<");
2065 CHECK_NOT_MATCH_C ("operator<<", "operator< <");
2066 CHECK_NOT_MATCH_C ("operator<<", "operator < <");
2067 CHECK_NOT_MATCH_C ("operator==", "operator= =");
2068 CHECK_NOT_MATCH_C ("operator==", "operator = =");
2069 CHECK_MATCH_C ("operator<<", "operator <<");
2070 CHECK_MATCH_C ("operator<<()", "operator <<");
2071 CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)");
2072 CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()");
2073 CHECK_MATCH_C ("operator==", "operator ==");
2074 CHECK_MATCH_C ("operator==()", "operator ==");
2075 CHECK_MATCH_C ("operator <<", "operator<<");
2076 CHECK_MATCH_C ("operator ==", "operator==");
2077 CHECK_MATCH_C ("operator bool", "operator bool");
2078 CHECK_MATCH_C ("operator bool ()", "operator bool");
2079 CHECK_MATCH_C ("operatorX<<", "operatorX < <");
2080 CHECK_MATCH_C ("Xoperator<<", "Xoperator < <");
2081
2082 CHECK_MATCH_C ("operator()(int)", "operator()(int)");
2083 CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )");
2084 CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )");
2085 /* The first "()" is not the parameter list. */
2086 CHECK_NOT_MATCH ("operator()(int)", "operator");
2087
2088 /* Misc user-defined operator tests. */
2089
2090 CHECK_NOT_MATCH_C ("operator/=()", "operator ^=");
2091 /* Same length at end of input. */
2092 CHECK_NOT_MATCH_C ("operator>>", "operator[]");
2093 /* Same length but not at end of input. */
2094 CHECK_NOT_MATCH_C ("operator>>()", "operator[]()");
2095
2096 CHECK_MATCH_C ("base::operator char*()", "base::operator char*()");
2097 CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()");
2098 CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()");
2099 CHECK_MATCH ("base::operator char**()", "base::operator char * *");
2100 CHECK_MATCH_C ("base::operator*()", "base::operator*()");
2101 CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc");
2102 CHECK_NOT_MATCH ("base::operator char*()", "base::operator char");
2103 CHECK_NOT_MATCH ("base::operator char*()", "base::operat");
2104
2105 /* Check handling of whitespace around C++ scope operators. */
2106 CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar");
2107 CHECK_MATCH_C ("foo::bar", "foo :: bar");
2108 CHECK_MATCH_C ("foo :: bar", "foo::bar");
2109
2110 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()");
2111 CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()");
2112 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )");
2113 CHECK_MATCH_C ("function()", "function()");
2114 CHECK_MATCH_C ("bar::function()", "bar::function()");
a20714ff
PA
2115
2116 /* Wild matching tests follow. */
2117
2118 /* Tests matching symbols in some scope. */
2119 CHECK_MATCH_C ("foo::function()", "function");
2120 CHECK_MATCH_C ("foo::function(int)", "function");
2121 CHECK_MATCH_C ("foo::bar::function()", "function");
2122 CHECK_MATCH_C ("bar::function()", "bar::function");
2123 CHECK_MATCH_C ("foo::bar::function()", "bar::function");
2124 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function");
2125
2126 /* Same, with parameters in the lookup name. */
2127 CHECK_MATCH_C ("foo::function()", "function()");
2128 CHECK_MATCH_C ("foo::bar::function()", "function()");
2129 CHECK_MATCH_C ("foo::function(int)", "function(int)");
2130 CHECK_MATCH_C ("foo::function()", "foo::function()");
2131 CHECK_MATCH_C ("foo::bar::function()", "bar::function()");
2132 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)");
2133 CHECK_MATCH_C ("bar::function()", "bar::function()");
2134
2135 CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()");
2136
2137 CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)",
2138 "bar::function(int)");
2139 CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)",
2140 "function(int)");
2141
2142 /* Lookup scope wider than symbol scope, should not match. */
2143 CHECK_NOT_MATCH_C ("function()", "bar::function");
2144 CHECK_NOT_MATCH_C ("function()", "bar::function()");
2145
2146 /* Explicit global scope doesn't match. */
2147 CHECK_NOT_MATCH_C ("foo::function()", "::function");
2148 CHECK_NOT_MATCH_C ("foo::function()", "::function()");
2149 CHECK_NOT_MATCH_C ("foo::function(int)", "::function()");
2150 CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)");
bd69330d
PA
2151
2152 /* Test ABI tag matching/ignoring. */
2153
2154 /* If the symbol name has an ABI tag, but the lookup name doesn't,
2155 then the ABI tag in the symbol name is ignored. */
2156 CHECK_MATCH_C ("function[abi:foo]()", "function");
2157 CHECK_MATCH_C ("function[abi:foo](int)", "function");
2158 CHECK_MATCH_C ("function[abi:foo]()", "function ()");
2159 CHECK_NOT_MATCH_C ("function[abi:foo]()", "function (int)");
2160
2161 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo]");
2162 CHECK_MATCH_C ("function[abi:foo](int)", "function[abi:foo]");
2163 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo] ()");
2164 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function");
2165 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function");
2166 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo]");
2167 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function[abi:foo]");
2168 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] ()");
2169 CHECK_NOT_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] (int)");
2170
2171 CHECK_MATCH_C ("function [abi:foo][abi:bar] ( )", "function [abi:foo]");
2172
2173 /* If the symbol name does not have an ABI tag, while the lookup
2174 name has one, then there's no match. */
2175 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]()");
2176 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]");
0662b6a7
PA
2177}
2178
c62446b1
PA
2179/* If non-NULL, return STR wrapped in quotes. Otherwise, return a
2180 "<null>" string (with no quotes). */
2181
2182static std::string
2183quote (const char *str)
2184{
2185 if (str != NULL)
fad03f6e 2186 return std::string (1, '"') + str + '"';
c62446b1
PA
2187 else
2188 return "<null>";
2189}
2190
2191/* Check that removing parameter info out of NAME produces EXPECTED.
2192 COMPLETION_MODE indicates whether we're testing normal and
2193 completion mode. FILE and LINE are used to provide better test
3bfdcabb 2194 location information in case the check fails. */
c62446b1
PA
2195
2196static void
2197check_remove_params (const char *file, int line,
2198 const char *name, const char *expected,
2199 bool completion_mode)
2200{
2201 gdb::unique_xmalloc_ptr<char> result
2202 = cp_remove_params_if_any (name, completion_mode);
2203
2204 if ((expected == NULL) != (result == NULL)
2205 || (expected != NULL
2206 && strcmp (result.get (), expected) != 0))
2207 {
2208 error (_("%s:%d: make-paramless self-test failed: (completion=%d) "
2209 "\"%s\" -> %s, expected %s"),
2210 file, line, completion_mode, name,
2211 quote (result.get ()).c_str (), quote (expected).c_str ());
2212 }
2213}
2214
2215/* Entry point for cp_remove_params unit tests. */
2216
2217static void
2218test_cp_remove_params ()
2219{
2220 /* Check that removing parameter info out of NAME produces EXPECTED.
2221 Checks both normal and completion modes. */
2222#define CHECK(NAME, EXPECTED) \
2223 do \
2224 { \
2225 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \
2226 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2227 } \
2228 while (0)
2229
2230 /* Similar, but used when NAME is incomplete -- i.e., is has
2231 unbalanced parentheses. In this case, looking for the exact name
2232 should fail / return empty. */
2233#define CHECK_INCOMPL(NAME, EXPECTED) \
2234 do \
2235 { \
2236 check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \
2237 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2238 } \
2239 while (0)
2240
2241 CHECK ("function()", "function");
2242 CHECK_INCOMPL ("function(", "function");
2243 CHECK ("function() const", "function");
2244
2245 CHECK ("(anonymous namespace)::A::B::C",
2246 "(anonymous namespace)::A::B::C");
2247
2248 CHECK ("A::(anonymous namespace)",
2249 "A::(anonymous namespace)");
2250
2251 CHECK_INCOMPL ("A::(anonymou", "A");
2252
2253 CHECK ("A::foo<int>()",
2254 "A::foo<int>");
2255
2256 CHECK_INCOMPL ("A::foo<int>(",
2257 "A::foo<int>");
2258
2259 CHECK ("A::foo<(anonymous namespace)::B>::func(int)",
2260 "A::foo<(anonymous namespace)::B>::func");
2261
2262 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in",
2263 "A::foo<(anonymous namespace)::B>::func");
2264
2265 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::",
2266 "A::foo<(anonymous namespace)::B>");
2267
2268 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:",
2269 "A::foo<(anonymous namespace)::B>");
2270
2271 CHECK ("A::foo<(anonymous namespace)::B>",
2272 "A::foo<(anonymous namespace)::B>");
2273
2274 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B",
2275 "A::foo");
2276
c62446b1
PA
2277 CHECK ("A::foo<void(int)>::func(int)",
2278 "A::foo<void(int)>::func");
c62446b1
PA
2279
2280 CHECK_INCOMPL ("A::foo<void(int",
2281 "A::foo");
2282
2283#undef CHECK
2284#undef CHECK_INCOMPL
2285}
2286
b6fb76ec 2287} /* namespace selftests */
c62446b1
PA
2288
2289#endif /* GDB_SELF_CHECK */
2290
9219021c
DC
2291/* This is a front end for cp_find_first_component, for unit testing.
2292 Be careful when using it: see the NOTE above
2293 cp_find_first_component. */
2294
2295static void
4a475551 2296first_component_command (const char *arg, int from_tty)
9219021c 2297{
c836824f
AR
2298 if (!arg)
2299 return;
2300
e6375bc8
TT
2301 int len = cp_find_first_component (arg);
2302 gdb_printf ("%.*s\n", len, arg);
9219021c
DC
2303}
2304
57651221 2305/* Implement "info vtbl". */
c4aeac85
TT
2306
2307static void
1d12d88f 2308info_vtbl_command (const char *arg, int from_tty)
c4aeac85
TT
2309{
2310 struct value *value;
2311
2312 value = parse_and_eval (arg);
2313 cplus_print_vtable (value);
2314}
2315
2f2c677e
KS
2316/* See description in cp-support.h. */
2317
2318const char *
2319find_toplevel_char (const char *s, char c)
2320{
2321 int quoted = 0; /* zero if we're not in quotes;
2322 '"' if we're in a double-quoted string;
2323 '\'' if we're in a single-quoted string. */
2324 int depth = 0; /* Number of unclosed parens we've seen. */
2325 const char *scan;
2326
2327 for (scan = s; *scan; scan++)
2328 {
2329 if (quoted)
2330 {
2331 if (*scan == quoted)
2332 quoted = 0;
2333 else if (*scan == '\\' && *(scan + 1))
2334 scan++;
2335 }
2336 else if (*scan == c && ! quoted && depth == 0)
2337 return scan;
2338 else if (*scan == '"' || *scan == '\'')
2339 quoted = *scan;
2340 else if (*scan == '(' || *scan == '<')
2341 depth++;
2342 else if ((*scan == ')' || *scan == '>') && depth > 0)
2343 depth--;
2344 else if (*scan == 'o' && !quoted && depth == 0)
2345 {
2346 /* Handle C++ operator names. */
2347 if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
2348 {
2349 scan += CP_OPERATOR_LEN;
2350 if (*scan == c)
2351 return scan;
2352 while (ISSPACE (*scan))
2353 {
2354 ++scan;
2355 if (*scan == c)
2356 return scan;
2357 }
2358 if (*scan == '\0')
2359 break;
2360
2361 switch (*scan)
2362 {
2363 /* Skip over one less than the appropriate number of
2364 characters: the for loop will skip over the last
2365 one. */
2366 case '<':
2367 if (scan[1] == '<')
2368 {
2369 scan++;
2370 if (*scan == c)
2371 return scan;
2372 }
2373 break;
2374 case '>':
2375 if (scan[1] == '>')
2376 {
2377 scan++;
2378 if (*scan == c)
2379 return scan;
2380 }
2381 break;
2382 }
2383 }
2384 }
2385 }
2386
2387 return 0;
2388}
2389
5fe70629 2390INIT_GDB_FILE (cp_support)
9219021c 2391{
5e84b7ee
SM
2392 cmd_list_element *maintenance_cplus
2393 = add_basic_prefix_cmd ("cplus", class_maintenance,
2394 _("C++ maintenance commands."),
2395 &maint_cplus_cmd_list,
2396 0, &maintenancelist);
2397 add_alias_cmd ("cp", maintenance_cplus, class_maintenance, 1,
aff410f1
MS
2398 &maintenancelist);
2399
2400 add_cmd ("first_component",
2401 class_maintenance,
2402 first_component_command,
1a966eab 2403 _("Print the first class/namespace component of NAME."),
9219021c 2404 &maint_cplus_cmd_list);
c4aeac85
TT
2405
2406 add_info ("vtbl", info_vtbl_command,
57651221 2407 _("Show the virtual function table for a C++ object.\n\
c4aeac85
TT
2408Usage: info vtbl EXPRESSION\n\
2409Evaluate EXPRESSION and display the virtual function table for the\n\
2410resulting object."));
992c7d70
GB
2411
2412#ifdef HAVE_WORKING_FORK
2413 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
2414 &catch_demangler_crashes, _("\
2415Set whether to attempt to catch demangler crashes."), _("\
2416Show whether to attempt to catch demangler crashes."), _("\
2417If enabled GDB will attempt to catch demangler crashes and\n\
2418display the offending symbol."),
2419 NULL,
2420 NULL,
2421 &maintenance_set_cmdlist,
2422 &maintenance_show_cmdlist);
57357d9d
TT
2423
2424 gdb_demangle_attempt_core_dump = can_dump_core (LIMIT_CUR);
992c7d70 2425#endif
c62446b1
PA
2426
2427#if GDB_SELF_TEST
0662b6a7
PA
2428 selftests::register_test ("cp_symbol_name_matches",
2429 selftests::test_cp_symbol_name_matches);
c62446b1
PA
2430 selftests::register_test ("cp_remove_params",
2431 selftests::test_cp_remove_params);
af3f129d
SV
2432 selftests::register_test ("cp_search_name_hash",
2433 selftests::test_cp_search_name_hash);
c62446b1 2434#endif
9219021c 2435}