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