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