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