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