virtio: Fix and activate PCI MSI-X support.
[dragonfly.git] / contrib / gdb-7 / gdb / cp-support.c
blob22e8fc4c134845b121be4bc08f05b50e487dd96e
1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
21 #include "defs.h"
22 #include "cp-support.h"
23 #include "gdb_string.h"
24 #include "demangle.h"
25 #include "gdb_assert.h"
26 #include "gdbcmd.h"
27 #include "dictionary.h"
28 #include "objfiles.h"
29 #include "frame.h"
30 #include "symtab.h"
31 #include "block.h"
32 #include "complaints.h"
33 #include "gdbtypes.h"
34 #include "exceptions.h"
35 #include "expression.h"
36 #include "value.h"
37 #include "cp-abi.h"
39 #include "safe-ctype.h"
41 #include "psymtab.h"
43 #define d_left(dc) (dc)->u.s_binary.left
44 #define d_right(dc) (dc)->u.s_binary.right
46 /* Functions related to demangled name parsing. */
48 static unsigned int cp_find_first_component_aux (const char *name,
49 int permissive);
51 static void demangled_name_complaint (const char *name);
53 /* Functions/variables related to overload resolution. */
55 static int sym_return_val_size = -1;
56 static int sym_return_val_index;
57 static struct symbol **sym_return_val;
59 static void overload_list_add_symbol (struct symbol *sym,
60 const char *oload_name);
62 static void make_symbol_overload_list_using (const char *func_name,
63 const char *namespace);
65 static void make_symbol_overload_list_qualified (const char *func_name);
67 /* The list of "maint cplus" commands. */
69 struct cmd_list_element *maint_cplus_cmd_list = NULL;
71 /* The actual commands. */
73 static void maint_cplus_command (char *arg, int from_tty);
74 static void first_component_command (char *arg, int from_tty);
76 /* A list of typedefs which should not be substituted by replace_typedefs. */
77 static const char * const ignore_typedefs[] =
79 "std::istream", "std::iostream", "std::ostream", "std::string"
82 static void
83 replace_typedefs (struct demangle_parse_info *info,
84 struct demangle_component *ret_comp,
85 canonicalization_ftype *finder,
86 void *data);
88 /* A convenience function to copy STRING into OBSTACK, returning a pointer
89 to the newly allocated string and saving the number of bytes saved in LEN.
91 It does not copy the terminating '\0' byte! */
93 static char *
94 copy_string_to_obstack (struct obstack *obstack, const char *string,
95 long *len)
97 *len = strlen (string);
98 return obstack_copy (obstack, string, *len);
101 /* A cleanup wrapper for cp_demangled_name_parse_free. */
103 static void
104 do_demangled_name_parse_free_cleanup (void *data)
106 struct demangle_parse_info *info = (struct demangle_parse_info *) data;
108 cp_demangled_name_parse_free (info);
111 /* Create a cleanup for C++ name parsing. */
113 struct cleanup *
114 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
116 return make_cleanup (do_demangled_name_parse_free_cleanup, info);
119 /* Return 1 if STRING is clearly already in canonical form. This
120 function is conservative; things which it does not recognize are
121 assumed to be non-canonical, and the parser will sort them out
122 afterwards. This speeds up the critical path for alphanumeric
123 identifiers. */
125 static int
126 cp_already_canonical (const char *string)
128 /* Identifier start character [a-zA-Z_]. */
129 if (!ISIDST (string[0]))
130 return 0;
132 /* These are the only two identifiers which canonicalize to other
133 than themselves or an error: unsigned -> unsigned int and
134 signed -> int. */
135 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
136 return 0;
137 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
138 return 0;
140 /* Identifier character [a-zA-Z0-9_]. */
141 while (ISIDNUM (string[1]))
142 string++;
144 if (string[1] == '\0')
145 return 1;
146 else
147 return 0;
150 /* Inspect the given RET_COMP for its type. If it is a typedef,
151 replace the node with the typedef's tree.
153 Returns 1 if any typedef substitutions were made, 0 otherwise. */
155 static int
156 inspect_type (struct demangle_parse_info *info,
157 struct demangle_component *ret_comp,
158 canonicalization_ftype *finder,
159 void *data)
161 int i;
162 char *name;
163 struct symbol *sym;
164 volatile struct gdb_exception except;
166 /* Copy the symbol's name from RET_COMP and look it up
167 in the symbol table. */
168 name = (char *) alloca (ret_comp->u.s_name.len + 1);
169 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
170 name[ret_comp->u.s_name.len] = '\0';
172 /* Ignore any typedefs that should not be substituted. */
173 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
175 if (strcmp (name, ignore_typedefs[i]) == 0)
176 return 0;
179 sym = NULL;
180 TRY_CATCH (except, RETURN_MASK_ALL)
182 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
185 if (except.reason >= 0 && sym != NULL)
187 struct type *otype = SYMBOL_TYPE (sym);
189 if (finder != NULL)
191 const char *new_name = (*finder) (otype, data);
193 if (new_name != NULL)
195 ret_comp->u.s_name.s = new_name;
196 ret_comp->u.s_name.len = strlen (new_name);
197 return 1;
200 return 0;
203 /* If the type is a typedef, replace it. */
204 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF)
206 long len;
207 int is_anon;
208 struct type *type;
209 struct demangle_parse_info *i;
210 struct ui_file *buf;
212 /* Get the real type of the typedef. */
213 type = check_typedef (otype);
215 is_anon = (TYPE_TAG_NAME (type) == NULL
216 && (TYPE_CODE (type) == TYPE_CODE_ENUM
217 || TYPE_CODE (type) == TYPE_CODE_STRUCT
218 || TYPE_CODE (type) == TYPE_CODE_UNION));
219 if (is_anon)
221 struct type *last = otype;
223 /* Find the last typedef for the type. */
224 while (TYPE_TARGET_TYPE (last) != NULL
225 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
226 == TYPE_CODE_TYPEDEF))
227 last = TYPE_TARGET_TYPE (last);
229 /* If there is only one typedef for this anonymous type,
230 do not substitute it. */
231 if (type == otype)
232 return 0;
233 else
234 /* Use the last typedef seen as the type for this
235 anonymous type. */
236 type = last;
239 buf = mem_fileopen ();
240 TRY_CATCH (except, RETURN_MASK_ERROR)
242 type_print (type, "", buf, -1);
245 /* If type_print threw an exception, there is little point
246 in continuing, so just bow out gracefully. */
247 if (except.reason < 0)
249 ui_file_delete (buf);
250 return 0;
253 name = ui_file_obsavestring (buf, &info->obstack, &len);
254 ui_file_delete (buf);
256 /* Turn the result into a new tree. Note that this
257 tree will contain pointers into NAME, so NAME cannot
258 be free'd until all typedef conversion is done and
259 the final result is converted into a string. */
260 i = cp_demangled_name_to_comp (name, NULL);
261 if (i != NULL)
263 /* Merge the two trees. */
264 cp_merge_demangle_parse_infos (info, ret_comp, i);
266 /* Replace any newly introduced typedefs -- but not
267 if the type is anonymous (that would lead to infinite
268 looping). */
269 if (!is_anon)
270 replace_typedefs (info, ret_comp, finder, data);
272 else
274 /* This shouldn't happen unless the type printer has
275 output something that the name parser cannot grok.
276 Nonetheless, an ounce of prevention...
278 Canonicalize the name again, and store it in the
279 current node (RET_COMP). */
280 char *canon = cp_canonicalize_string_no_typedefs (name);
282 if (canon != NULL)
284 /* Copy the canonicalization into the obstack and
285 free CANON. */
286 name = copy_string_to_obstack (&info->obstack, canon, &len);
287 xfree (canon);
290 ret_comp->u.s_name.s = name;
291 ret_comp->u.s_name.len = len;
294 return 1;
298 return 0;
301 /* Replace any typedefs appearing in the qualified name
302 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
303 given in INFO. */
305 static void
306 replace_typedefs_qualified_name (struct demangle_parse_info *info,
307 struct demangle_component *ret_comp,
308 canonicalization_ftype *finder,
309 void *data)
311 long len;
312 char *name;
313 struct ui_file *buf = mem_fileopen ();
314 struct demangle_component *comp = ret_comp;
316 /* Walk each node of the qualified name, reconstructing the name of
317 this element. With every node, check for any typedef substitutions.
318 If a substitution has occurred, replace the qualified name node
319 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
320 substituted name. */
321 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
323 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
325 struct demangle_component new;
327 ui_file_write (buf, d_left (comp)->u.s_name.s,
328 d_left (comp)->u.s_name.len);
329 name = ui_file_obsavestring (buf, &info->obstack, &len);
330 new.type = DEMANGLE_COMPONENT_NAME;
331 new.u.s_name.s = name;
332 new.u.s_name.len = len;
333 if (inspect_type (info, &new, finder, data))
335 char *n, *s;
336 long slen;
338 /* A typedef was substituted in NEW. Convert it to a
339 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
340 node. */
342 ui_file_rewind (buf);
343 n = cp_comp_to_string (&new, 100);
344 if (n == NULL)
346 /* If something went astray, abort typedef substitutions. */
347 ui_file_delete (buf);
348 return;
351 s = copy_string_to_obstack (&info->obstack, n, &slen);
352 xfree (n);
354 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
355 d_left (ret_comp)->u.s_name.s = s;
356 d_left (ret_comp)->u.s_name.len = slen;
357 d_right (ret_comp) = d_right (comp);
358 comp = ret_comp;
359 continue;
362 else
364 /* The current node is not a name, so simply replace any
365 typedefs in it. Then print it to the stream to continue
366 checking for more typedefs in the tree. */
367 replace_typedefs (info, d_left (comp), finder, data);
368 name = cp_comp_to_string (d_left (comp), 100);
369 if (name == NULL)
371 /* If something went astray, abort typedef substitutions. */
372 ui_file_delete (buf);
373 return;
375 fputs_unfiltered (name, buf);
376 xfree (name);
379 ui_file_write (buf, "::", 2);
380 comp = d_right (comp);
383 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
384 name assembled above and append the name given by COMP. Then use this
385 reassembled name to check for a typedef. */
387 if (comp->type == DEMANGLE_COMPONENT_NAME)
389 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
390 name = ui_file_obsavestring (buf, &info->obstack, &len);
392 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
393 with a DEMANGLE_COMPONENT_NAME node containing the whole
394 name. */
395 ret_comp->type = DEMANGLE_COMPONENT_NAME;
396 ret_comp->u.s_name.s = name;
397 ret_comp->u.s_name.len = len;
398 inspect_type (info, ret_comp, finder, data);
400 else
401 replace_typedefs (info, comp, finder, data);
403 ui_file_delete (buf);
407 /* A function to check const and volatile qualifiers for argument types.
409 "Parameter declarations that differ only in the presence
410 or absence of `const' and/or `volatile' are equivalent."
411 C++ Standard N3290, clause 13.1.3 #4. */
413 static void
414 check_cv_qualifiers (struct demangle_component *ret_comp)
416 while (d_left (ret_comp) != NULL
417 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
418 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
420 d_left (ret_comp) = d_left (d_left (ret_comp));
424 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
425 their basic types. */
427 static void
428 replace_typedefs (struct demangle_parse_info *info,
429 struct demangle_component *ret_comp,
430 canonicalization_ftype *finder,
431 void *data)
433 if (ret_comp)
435 if (finder != NULL
436 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
437 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
438 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
439 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
441 char *local_name = cp_comp_to_string (ret_comp, 10);
443 if (local_name != NULL)
445 struct symbol *sym;
446 volatile struct gdb_exception except;
448 sym = NULL;
449 TRY_CATCH (except, RETURN_MASK_ALL)
451 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0);
453 xfree (local_name);
455 if (except.reason >= 0 && sym != NULL)
457 struct type *otype = SYMBOL_TYPE (sym);
458 const char *new_name = (*finder) (otype, data);
460 if (new_name != NULL)
462 ret_comp->type = DEMANGLE_COMPONENT_NAME;
463 ret_comp->u.s_name.s = new_name;
464 ret_comp->u.s_name.len = strlen (new_name);
465 return;
471 switch (ret_comp->type)
473 case DEMANGLE_COMPONENT_ARGLIST:
474 check_cv_qualifiers (ret_comp);
475 /* Fall through */
477 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
478 case DEMANGLE_COMPONENT_TEMPLATE:
479 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
480 case DEMANGLE_COMPONENT_TYPED_NAME:
481 replace_typedefs (info, d_left (ret_comp), finder, data);
482 replace_typedefs (info, d_right (ret_comp), finder, data);
483 break;
485 case DEMANGLE_COMPONENT_NAME:
486 inspect_type (info, ret_comp, finder, data);
487 break;
489 case DEMANGLE_COMPONENT_QUAL_NAME:
490 replace_typedefs_qualified_name (info, ret_comp, finder, data);
491 break;
493 case DEMANGLE_COMPONENT_LOCAL_NAME:
494 case DEMANGLE_COMPONENT_CTOR:
495 case DEMANGLE_COMPONENT_ARRAY_TYPE:
496 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
497 replace_typedefs (info, d_right (ret_comp), finder, data);
498 break;
500 case DEMANGLE_COMPONENT_CONST:
501 case DEMANGLE_COMPONENT_RESTRICT:
502 case DEMANGLE_COMPONENT_VOLATILE:
503 case DEMANGLE_COMPONENT_VOLATILE_THIS:
504 case DEMANGLE_COMPONENT_CONST_THIS:
505 case DEMANGLE_COMPONENT_RESTRICT_THIS:
506 case DEMANGLE_COMPONENT_POINTER:
507 case DEMANGLE_COMPONENT_REFERENCE:
508 replace_typedefs (info, d_left (ret_comp), finder, data);
509 break;
511 default:
512 break;
517 /* Parse STRING and convert it to canonical form, resolving any typedefs.
518 If parsing fails, or if STRING is already canonical, return NULL.
519 Otherwise return the canonical form. The return value is allocated via
520 xmalloc. If FINDER is not NULL, then type components are passed to
521 FINDER to be looked up. DATA is passed verbatim to FINDER. */
523 char *
524 cp_canonicalize_string_full (const char *string,
525 canonicalization_ftype *finder,
526 void *data)
528 char *ret;
529 unsigned int estimated_len;
530 struct demangle_parse_info *info;
532 ret = NULL;
533 estimated_len = strlen (string) * 2;
534 info = cp_demangled_name_to_comp (string, NULL);
535 if (info != NULL)
537 /* Replace all the typedefs in the tree. */
538 replace_typedefs (info, info->tree, finder, data);
540 /* Convert the tree back into a string. */
541 ret = cp_comp_to_string (info->tree, estimated_len);
542 gdb_assert (ret != NULL);
544 /* Free the parse information. */
545 cp_demangled_name_parse_free (info);
547 /* Finally, compare the original string with the computed
548 name, returning NULL if they are the same. */
549 if (strcmp (string, ret) == 0)
551 xfree (ret);
552 return NULL;
556 return ret;
559 /* Like cp_canonicalize_string_full, but always passes NULL for
560 FINDER. */
562 char *
563 cp_canonicalize_string_no_typedefs (const char *string)
565 return cp_canonicalize_string_full (string, NULL, NULL);
568 /* Parse STRING and convert it to canonical form. If parsing fails,
569 or if STRING is already canonical, return NULL. Otherwise return
570 the canonical form. The return value is allocated via xmalloc. */
572 char *
573 cp_canonicalize_string (const char *string)
575 struct demangle_parse_info *info;
576 unsigned int estimated_len;
577 char *ret;
579 if (cp_already_canonical (string))
580 return NULL;
582 info = cp_demangled_name_to_comp (string, NULL);
583 if (info == NULL)
584 return NULL;
586 estimated_len = strlen (string) * 2;
587 ret = cp_comp_to_string (info->tree, estimated_len);
588 cp_demangled_name_parse_free (info);
590 if (ret == NULL)
592 warning (_("internal error: string \"%s\" failed to be canonicalized"),
593 string);
594 return NULL;
597 if (strcmp (string, ret) == 0)
599 xfree (ret);
600 return NULL;
603 return ret;
606 /* Convert a mangled name to a demangle_component tree. *MEMORY is
607 set to the block of used memory that should be freed when finished
608 with the tree. DEMANGLED_P is set to the char * that should be
609 freed when finished with the tree, or NULL if none was needed.
610 OPTIONS will be passed to the demangler. */
612 static struct demangle_parse_info *
613 mangled_name_to_comp (const char *mangled_name, int options,
614 void **memory, char **demangled_p)
616 char *demangled_name;
617 struct demangle_parse_info *info;
619 /* If it looks like a v3 mangled name, then try to go directly
620 to trees. */
621 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
623 struct demangle_component *ret;
625 ret = cplus_demangle_v3_components (mangled_name,
626 options, memory);
627 if (ret)
629 info = cp_new_demangle_parse_info ();
630 info->tree = ret;
631 *demangled_p = NULL;
632 return info;
636 /* If it doesn't, or if that failed, then try to demangle the
637 name. */
638 demangled_name = cplus_demangle (mangled_name, options);
639 if (demangled_name == NULL)
640 return NULL;
642 /* If we could demangle the name, parse it to build the component
643 tree. */
644 info = cp_demangled_name_to_comp (demangled_name, NULL);
646 if (info == NULL)
648 xfree (demangled_name);
649 return NULL;
652 *demangled_p = demangled_name;
653 return info;
656 /* Return the name of the class containing method PHYSNAME. */
658 char *
659 cp_class_name_from_physname (const char *physname)
661 void *storage = NULL;
662 char *demangled_name = NULL, *ret;
663 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
664 struct demangle_parse_info *info;
665 int done;
667 info = mangled_name_to_comp (physname, DMGL_ANSI,
668 &storage, &demangled_name);
669 if (info == NULL)
670 return NULL;
672 done = 0;
673 ret_comp = info->tree;
675 /* First strip off any qualifiers, if we have a function or
676 method. */
677 while (!done)
678 switch (ret_comp->type)
680 case DEMANGLE_COMPONENT_CONST:
681 case DEMANGLE_COMPONENT_RESTRICT:
682 case DEMANGLE_COMPONENT_VOLATILE:
683 case DEMANGLE_COMPONENT_CONST_THIS:
684 case DEMANGLE_COMPONENT_RESTRICT_THIS:
685 case DEMANGLE_COMPONENT_VOLATILE_THIS:
686 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
687 ret_comp = d_left (ret_comp);
688 break;
689 default:
690 done = 1;
691 break;
694 /* If what we have now is a function, discard the argument list. */
695 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
696 ret_comp = d_left (ret_comp);
698 /* If what we have now is a template, strip off the template
699 arguments. The left subtree may be a qualified name. */
700 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
701 ret_comp = d_left (ret_comp);
703 /* What we have now should be a name, possibly qualified.
704 Additional qualifiers could live in the left subtree or the right
705 subtree. Find the last piece. */
706 done = 0;
707 prev_comp = NULL;
708 cur_comp = ret_comp;
709 while (!done)
710 switch (cur_comp->type)
712 case DEMANGLE_COMPONENT_QUAL_NAME:
713 case DEMANGLE_COMPONENT_LOCAL_NAME:
714 prev_comp = cur_comp;
715 cur_comp = d_right (cur_comp);
716 break;
717 case DEMANGLE_COMPONENT_TEMPLATE:
718 case DEMANGLE_COMPONENT_NAME:
719 case DEMANGLE_COMPONENT_CTOR:
720 case DEMANGLE_COMPONENT_DTOR:
721 case DEMANGLE_COMPONENT_OPERATOR:
722 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
723 done = 1;
724 break;
725 default:
726 done = 1;
727 cur_comp = NULL;
728 break;
731 ret = NULL;
732 if (cur_comp != NULL && prev_comp != NULL)
734 /* We want to discard the rightmost child of PREV_COMP. */
735 *prev_comp = *d_left (prev_comp);
736 /* The ten is completely arbitrary; we don't have a good
737 estimate. */
738 ret = cp_comp_to_string (ret_comp, 10);
741 xfree (storage);
742 xfree (demangled_name);
743 cp_demangled_name_parse_free (info);
744 return ret;
747 /* Return the child of COMP which is the basename of a method,
748 variable, et cetera. All scope qualifiers are discarded, but
749 template arguments will be included. The component tree may be
750 modified. */
752 static struct demangle_component *
753 unqualified_name_from_comp (struct demangle_component *comp)
755 struct demangle_component *ret_comp = comp, *last_template;
756 int done;
758 done = 0;
759 last_template = NULL;
760 while (!done)
761 switch (ret_comp->type)
763 case DEMANGLE_COMPONENT_QUAL_NAME:
764 case DEMANGLE_COMPONENT_LOCAL_NAME:
765 ret_comp = d_right (ret_comp);
766 break;
767 case DEMANGLE_COMPONENT_TYPED_NAME:
768 ret_comp = d_left (ret_comp);
769 break;
770 case DEMANGLE_COMPONENT_TEMPLATE:
771 gdb_assert (last_template == NULL);
772 last_template = ret_comp;
773 ret_comp = d_left (ret_comp);
774 break;
775 case DEMANGLE_COMPONENT_CONST:
776 case DEMANGLE_COMPONENT_RESTRICT:
777 case DEMANGLE_COMPONENT_VOLATILE:
778 case DEMANGLE_COMPONENT_CONST_THIS:
779 case DEMANGLE_COMPONENT_RESTRICT_THIS:
780 case DEMANGLE_COMPONENT_VOLATILE_THIS:
781 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
782 ret_comp = d_left (ret_comp);
783 break;
784 case DEMANGLE_COMPONENT_NAME:
785 case DEMANGLE_COMPONENT_CTOR:
786 case DEMANGLE_COMPONENT_DTOR:
787 case DEMANGLE_COMPONENT_OPERATOR:
788 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
789 done = 1;
790 break;
791 default:
792 return NULL;
793 break;
796 if (last_template)
798 d_left (last_template) = ret_comp;
799 return last_template;
802 return ret_comp;
805 /* Return the name of the method whose linkage name is PHYSNAME. */
807 char *
808 method_name_from_physname (const char *physname)
810 void *storage = NULL;
811 char *demangled_name = NULL, *ret;
812 struct demangle_component *ret_comp;
813 struct demangle_parse_info *info;
815 info = mangled_name_to_comp (physname, DMGL_ANSI,
816 &storage, &demangled_name);
817 if (info == NULL)
818 return NULL;
820 ret_comp = unqualified_name_from_comp (info->tree);
822 ret = NULL;
823 if (ret_comp != NULL)
824 /* The ten is completely arbitrary; we don't have a good
825 estimate. */
826 ret = cp_comp_to_string (ret_comp, 10);
828 xfree (storage);
829 xfree (demangled_name);
830 cp_demangled_name_parse_free (info);
831 return ret;
834 /* If FULL_NAME is the demangled name of a C++ function (including an
835 arg list, possibly including namespace/class qualifications),
836 return a new string containing only the function name (without the
837 arg list/class qualifications). Otherwise, return NULL. The
838 caller is responsible for freeing the memory in question. */
840 char *
841 cp_func_name (const char *full_name)
843 char *ret;
844 struct demangle_component *ret_comp;
845 struct demangle_parse_info *info;
847 info = cp_demangled_name_to_comp (full_name, NULL);
848 if (!info)
849 return NULL;
851 ret_comp = unqualified_name_from_comp (info->tree);
853 ret = NULL;
854 if (ret_comp != NULL)
855 ret = cp_comp_to_string (ret_comp, 10);
857 cp_demangled_name_parse_free (info);
858 return ret;
861 /* DEMANGLED_NAME is the name of a function, including parameters and
862 (optionally) a return type. Return the name of the function without
863 parameters or return type, or NULL if we can not parse the name. */
865 char *
866 cp_remove_params (const char *demangled_name)
868 int done = 0;
869 struct demangle_component *ret_comp;
870 struct demangle_parse_info *info;
871 char *ret = NULL;
873 if (demangled_name == NULL)
874 return NULL;
876 info = cp_demangled_name_to_comp (demangled_name, NULL);
877 if (info == NULL)
878 return NULL;
880 /* First strip off any qualifiers, if we have a function or method. */
881 ret_comp = info->tree;
882 while (!done)
883 switch (ret_comp->type)
885 case DEMANGLE_COMPONENT_CONST:
886 case DEMANGLE_COMPONENT_RESTRICT:
887 case DEMANGLE_COMPONENT_VOLATILE:
888 case DEMANGLE_COMPONENT_CONST_THIS:
889 case DEMANGLE_COMPONENT_RESTRICT_THIS:
890 case DEMANGLE_COMPONENT_VOLATILE_THIS:
891 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
892 ret_comp = d_left (ret_comp);
893 break;
894 default:
895 done = 1;
896 break;
899 /* What we have now should be a function. Return its name. */
900 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
901 ret = cp_comp_to_string (d_left (ret_comp), 10);
903 cp_demangled_name_parse_free (info);
904 return ret;
907 /* Here are some random pieces of trivia to keep in mind while trying
908 to take apart demangled names:
910 - Names can contain function arguments or templates, so the process
911 has to be, to some extent recursive: maybe keep track of your
912 depth based on encountering <> and ().
914 - Parentheses don't just have to happen at the end of a name: they
915 can occur even if the name in question isn't a function, because
916 a template argument might be a type that's a function.
918 - Conversely, even if you're trying to deal with a function, its
919 demangled name might not end with ')': it could be a const or
920 volatile class method, in which case it ends with "const" or
921 "volatile".
923 - Parentheses are also used in anonymous namespaces: a variable
924 'foo' in an anonymous namespace gets demangled as "(anonymous
925 namespace)::foo".
927 - And operator names can contain parentheses or angle brackets. */
929 /* FIXME: carlton/2003-03-13: We have several functions here with
930 overlapping functionality; can we combine them? Also, do they
931 handle all the above considerations correctly? */
934 /* This returns the length of first component of NAME, which should be
935 the demangled name of a C++ variable/function/method/etc.
936 Specifically, it returns the index of the first colon forming the
937 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
938 it returns the 1, and given 'foo', it returns 0. */
940 /* The character in NAME indexed by the return value is guaranteed to
941 always be either ':' or '\0'. */
943 /* NOTE: carlton/2003-03-13: This function is currently only intended
944 for internal use: it's probably not entirely safe when called on
945 user-generated input, because some of the 'index += 2' lines in
946 cp_find_first_component_aux might go past the end of malformed
947 input. */
949 unsigned int
950 cp_find_first_component (const char *name)
952 return cp_find_first_component_aux (name, 0);
955 /* Helper function for cp_find_first_component. Like that function,
956 it returns the length of the first component of NAME, but to make
957 the recursion easier, it also stops if it reaches an unexpected ')'
958 or '>' if the value of PERMISSIVE is nonzero. */
960 /* Let's optimize away calls to strlen("operator"). */
962 #define LENGTH_OF_OPERATOR 8
964 static unsigned int
965 cp_find_first_component_aux (const char *name, int permissive)
967 unsigned int index = 0;
968 /* Operator names can show up in unexpected places. Since these can
969 contain parentheses or angle brackets, they can screw up the
970 recursion. But not every string 'operator' is part of an
971 operater name: e.g. you could have a variable 'cooperator'. So
972 this variable tells us whether or not we should treat the string
973 'operator' as starting an operator. */
974 int operator_possible = 1;
976 for (;; ++index)
978 switch (name[index])
980 case '<':
981 /* Template; eat it up. The calls to cp_first_component
982 should only return (I hope!) when they reach the '>'
983 terminating the component or a '::' between two
984 components. (Hence the '+ 2'.) */
985 index += 1;
986 for (index += cp_find_first_component_aux (name + index, 1);
987 name[index] != '>';
988 index += cp_find_first_component_aux (name + index, 1))
990 if (name[index] != ':')
992 demangled_name_complaint (name);
993 return strlen (name);
995 index += 2;
997 operator_possible = 1;
998 break;
999 case '(':
1000 /* Similar comment as to '<'. */
1001 index += 1;
1002 for (index += cp_find_first_component_aux (name + index, 1);
1003 name[index] != ')';
1004 index += cp_find_first_component_aux (name + index, 1))
1006 if (name[index] != ':')
1008 demangled_name_complaint (name);
1009 return strlen (name);
1011 index += 2;
1013 operator_possible = 1;
1014 break;
1015 case '>':
1016 case ')':
1017 if (permissive)
1018 return index;
1019 else
1021 demangled_name_complaint (name);
1022 return strlen (name);
1024 case '\0':
1025 case ':':
1026 return index;
1027 case 'o':
1028 /* Operator names can screw up the recursion. */
1029 if (operator_possible
1030 && strncmp (name + index, "operator",
1031 LENGTH_OF_OPERATOR) == 0)
1033 index += LENGTH_OF_OPERATOR;
1034 while (ISSPACE(name[index]))
1035 ++index;
1036 switch (name[index])
1038 /* Skip over one less than the appropriate number of
1039 characters: the for loop will skip over the last
1040 one. */
1041 case '<':
1042 if (name[index + 1] == '<')
1043 index += 1;
1044 else
1045 index += 0;
1046 break;
1047 case '>':
1048 case '-':
1049 if (name[index + 1] == '>')
1050 index += 1;
1051 else
1052 index += 0;
1053 break;
1054 case '(':
1055 index += 1;
1056 break;
1057 default:
1058 index += 0;
1059 break;
1062 operator_possible = 0;
1063 break;
1064 case ' ':
1065 case ',':
1066 case '.':
1067 case '&':
1068 case '*':
1069 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1070 set of relevant characters are here: it's necessary to
1071 include any character that can show up before 'operator'
1072 in a demangled name, and it's safe to include any
1073 character that can't be part of an identifier's name. */
1074 operator_possible = 1;
1075 break;
1076 default:
1077 operator_possible = 0;
1078 break;
1083 /* Complain about a demangled name that we don't know how to parse.
1084 NAME is the demangled name in question. */
1086 static void
1087 demangled_name_complaint (const char *name)
1089 complaint (&symfile_complaints,
1090 "unexpected demangled name '%s'", name);
1093 /* If NAME is the fully-qualified name of a C++
1094 function/variable/method/etc., this returns the length of its
1095 entire prefix: all of the namespaces and classes that make up its
1096 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1097 4, given 'foo', it returns 0. */
1099 unsigned int
1100 cp_entire_prefix_len (const char *name)
1102 unsigned int current_len = cp_find_first_component (name);
1103 unsigned int previous_len = 0;
1105 while (name[current_len] != '\0')
1107 gdb_assert (name[current_len] == ':');
1108 previous_len = current_len;
1109 /* Skip the '::'. */
1110 current_len += 2;
1111 current_len += cp_find_first_component (name + current_len);
1114 return previous_len;
1117 /* Overload resolution functions. */
1119 /* Test to see if SYM is a symbol that we haven't seen corresponding
1120 to a function named OLOAD_NAME. If so, add it to the current
1121 completion list. */
1123 static void
1124 overload_list_add_symbol (struct symbol *sym,
1125 const char *oload_name)
1127 int newsize;
1128 int i;
1129 char *sym_name;
1131 /* If there is no type information, we can't do anything, so
1132 skip. */
1133 if (SYMBOL_TYPE (sym) == NULL)
1134 return;
1136 /* skip any symbols that we've already considered. */
1137 for (i = 0; i < sym_return_val_index; ++i)
1138 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1139 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1140 return;
1142 /* Get the demangled name without parameters */
1143 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1144 if (!sym_name)
1145 return;
1147 /* skip symbols that cannot match */
1148 if (strcmp (sym_name, oload_name) != 0)
1150 xfree (sym_name);
1151 return;
1154 xfree (sym_name);
1156 /* We have a match for an overload instance, so add SYM to the
1157 current list of overload instances */
1158 if (sym_return_val_index + 3 > sym_return_val_size)
1160 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1161 sym_return_val = (struct symbol **)
1162 xrealloc ((char *) sym_return_val, newsize);
1164 sym_return_val[sym_return_val_index++] = sym;
1165 sym_return_val[sym_return_val_index] = NULL;
1168 /* Return a null-terminated list of pointers to function symbols that
1169 are named FUNC_NAME and are visible within NAMESPACE. */
1171 struct symbol **
1172 make_symbol_overload_list (const char *func_name,
1173 const char *namespace)
1175 struct cleanup *old_cleanups;
1176 const char *name;
1178 sym_return_val_size = 100;
1179 sym_return_val_index = 0;
1180 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1181 sizeof (struct symbol *));
1182 sym_return_val[0] = NULL;
1184 old_cleanups = make_cleanup (xfree, sym_return_val);
1186 make_symbol_overload_list_using (func_name, namespace);
1188 if (namespace[0] == '\0')
1189 name = func_name;
1190 else
1192 char *concatenated_name
1193 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1194 strcpy (concatenated_name, namespace);
1195 strcat (concatenated_name, "::");
1196 strcat (concatenated_name, func_name);
1197 name = concatenated_name;
1200 make_symbol_overload_list_qualified (name);
1202 discard_cleanups (old_cleanups);
1204 return sym_return_val;
1207 /* Add all symbols with a name matching NAME in BLOCK to the overload
1208 list. */
1210 static void
1211 make_symbol_overload_list_block (const char *name,
1212 const struct block *block)
1214 struct block_iterator iter;
1215 struct symbol *sym;
1217 for (sym = block_iter_name_first (block, name, &iter);
1218 sym != NULL;
1219 sym = block_iter_name_next (name, &iter))
1220 overload_list_add_symbol (sym, name);
1223 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1225 static void
1226 make_symbol_overload_list_namespace (const char *func_name,
1227 const char *namespace)
1229 const char *name;
1230 const struct block *block = NULL;
1232 if (namespace[0] == '\0')
1233 name = func_name;
1234 else
1236 char *concatenated_name
1237 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1239 strcpy (concatenated_name, namespace);
1240 strcat (concatenated_name, "::");
1241 strcat (concatenated_name, func_name);
1242 name = concatenated_name;
1245 /* Look in the static block. */
1246 block = block_static_block (get_selected_block (0));
1247 if (block)
1248 make_symbol_overload_list_block (name, block);
1250 /* Look in the global block. */
1251 block = block_global_block (block);
1252 if (block)
1253 make_symbol_overload_list_block (name, block);
1257 /* Search the namespace of the given type and namespace of and public
1258 base types. */
1260 static void
1261 make_symbol_overload_list_adl_namespace (struct type *type,
1262 const char *func_name)
1264 char *namespace;
1265 const char *type_name;
1266 int i, prefix_len;
1268 while (TYPE_CODE (type) == TYPE_CODE_PTR
1269 || TYPE_CODE (type) == TYPE_CODE_REF
1270 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1271 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1273 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1274 type = check_typedef(type);
1275 else
1276 type = TYPE_TARGET_TYPE (type);
1279 type_name = TYPE_NAME (type);
1281 if (type_name == NULL)
1282 return;
1284 prefix_len = cp_entire_prefix_len (type_name);
1286 if (prefix_len != 0)
1288 namespace = alloca (prefix_len + 1);
1289 strncpy (namespace, type_name, prefix_len);
1290 namespace[prefix_len] = '\0';
1292 make_symbol_overload_list_namespace (func_name, namespace);
1295 /* Check public base type */
1296 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1297 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1299 if (BASETYPE_VIA_PUBLIC (type, i))
1300 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1302 func_name);
1306 /* Adds the overload list overload candidates for FUNC_NAME found
1307 through argument dependent lookup. */
1309 struct symbol **
1310 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1311 const char *func_name)
1313 int i;
1315 gdb_assert (sym_return_val_size != -1);
1317 for (i = 1; i <= nargs; i++)
1318 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1319 func_name);
1321 return sym_return_val;
1324 /* Used for cleanups to reset the "searched" flag in case of an
1325 error. */
1327 static void
1328 reset_directive_searched (void *data)
1330 struct using_direct *direct = data;
1331 direct->searched = 0;
1334 /* This applies the using directives to add namespaces to search in,
1335 and then searches for overloads in all of those namespaces. It
1336 adds the symbols found to sym_return_val. Arguments are as in
1337 make_symbol_overload_list. */
1339 static void
1340 make_symbol_overload_list_using (const char *func_name,
1341 const char *namespace)
1343 struct using_direct *current;
1344 const struct block *block;
1346 /* First, go through the using directives. If any of them apply,
1347 look in the appropriate namespaces for new functions to match
1348 on. */
1350 for (block = get_selected_block (0);
1351 block != NULL;
1352 block = BLOCK_SUPERBLOCK (block))
1353 for (current = block_using (block);
1354 current != NULL;
1355 current = current->next)
1357 /* Prevent recursive calls. */
1358 if (current->searched)
1359 continue;
1361 /* If this is a namespace alias or imported declaration ignore
1362 it. */
1363 if (current->alias != NULL || current->declaration != NULL)
1364 continue;
1366 if (strcmp (namespace, current->import_dest) == 0)
1368 /* Mark this import as searched so that the recursive call
1369 does not search it again. */
1370 struct cleanup *old_chain;
1371 current->searched = 1;
1372 old_chain = make_cleanup (reset_directive_searched,
1373 current);
1375 make_symbol_overload_list_using (func_name,
1376 current->import_src);
1378 current->searched = 0;
1379 discard_cleanups (old_chain);
1383 /* Now, add names for this namespace. */
1384 make_symbol_overload_list_namespace (func_name, namespace);
1387 /* This does the bulk of the work of finding overloaded symbols.
1388 FUNC_NAME is the name of the overloaded function we're looking for
1389 (possibly including namespace info). */
1391 static void
1392 make_symbol_overload_list_qualified (const char *func_name)
1394 struct symtab *s;
1395 struct objfile *objfile;
1396 const struct block *b, *surrounding_static_block = 0;
1398 /* Look through the partial symtabs for all symbols which begin by
1399 matching FUNC_NAME. Make sure we read that symbol table in. */
1401 ALL_OBJFILES (objfile)
1403 if (objfile->sf)
1404 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1407 /* Search upwards from currently selected frame (so that we can
1408 complete on local vars. */
1410 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1411 make_symbol_overload_list_block (func_name, b);
1413 surrounding_static_block = block_static_block (get_selected_block (0));
1415 /* Go through the symtabs and check the externs and statics for
1416 symbols which match. */
1418 ALL_PRIMARY_SYMTABS (objfile, s)
1420 QUIT;
1421 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1422 make_symbol_overload_list_block (func_name, b);
1425 ALL_PRIMARY_SYMTABS (objfile, s)
1427 QUIT;
1428 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1429 /* Don't do this block twice. */
1430 if (b == surrounding_static_block)
1431 continue;
1432 make_symbol_overload_list_block (func_name, b);
1436 /* Lookup the rtti type for a class name. */
1438 struct type *
1439 cp_lookup_rtti_type (const char *name, struct block *block)
1441 struct symbol * rtti_sym;
1442 struct type * rtti_type;
1444 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1446 if (rtti_sym == NULL)
1448 warning (_("RTTI symbol not found for class '%s'"), name);
1449 return NULL;
1452 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1454 warning (_("RTTI symbol for class '%s' is not a type"), name);
1455 return NULL;
1458 rtti_type = SYMBOL_TYPE (rtti_sym);
1460 switch (TYPE_CODE (rtti_type))
1462 case TYPE_CODE_CLASS:
1463 break;
1464 case TYPE_CODE_NAMESPACE:
1465 /* chastain/2003-11-26: the symbol tables often contain fake
1466 symbols for namespaces with the same name as the struct.
1467 This warning is an indication of a bug in the lookup order
1468 or a bug in the way that the symbol tables are populated. */
1469 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1470 return NULL;
1471 default:
1472 warning (_("RTTI symbol for class '%s' has bad type"), name);
1473 return NULL;
1476 return rtti_type;
1479 /* Don't allow just "maintenance cplus". */
1481 static void
1482 maint_cplus_command (char *arg, int from_tty)
1484 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1485 "by the name of a command.\n"));
1486 help_list (maint_cplus_cmd_list,
1487 "maintenance cplus ",
1488 -1, gdb_stdout);
1491 /* This is a front end for cp_find_first_component, for unit testing.
1492 Be careful when using it: see the NOTE above
1493 cp_find_first_component. */
1495 static void
1496 first_component_command (char *arg, int from_tty)
1498 int len;
1499 char *prefix;
1501 if (!arg)
1502 return;
1504 len = cp_find_first_component (arg);
1505 prefix = alloca (len + 1);
1507 memcpy (prefix, arg, len);
1508 prefix[len] = '\0';
1510 printf_unfiltered ("%s\n", prefix);
1513 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1516 /* Implement "info vtbl". */
1518 static void
1519 info_vtbl_command (char *arg, int from_tty)
1521 struct value *value;
1523 value = parse_and_eval (arg);
1524 cplus_print_vtable (value);
1527 void
1528 _initialize_cp_support (void)
1530 add_prefix_cmd ("cplus", class_maintenance,
1531 maint_cplus_command,
1532 _("C++ maintenance commands."),
1533 &maint_cplus_cmd_list,
1534 "maintenance cplus ",
1535 0, &maintenancelist);
1536 add_alias_cmd ("cp", "cplus",
1537 class_maintenance, 1,
1538 &maintenancelist);
1540 add_cmd ("first_component",
1541 class_maintenance,
1542 first_component_command,
1543 _("Print the first class/namespace component of NAME."),
1544 &maint_cplus_cmd_list);
1546 add_info ("vtbl", info_vtbl_command,
1547 _("Show the virtual function table for a C++ object.\n\
1548 Usage: info vtbl EXPRESSION\n\
1549 Evaluate EXPRESSION and display the virtual function table for the\n\
1550 resulting object."));