1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "cp-support.h"
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
28 #include "dictionary.h"
33 #include "complaints.h"
36 #include "safe-ctype.h"
38 #define d_left(dc) (dc)->u.s_binary.left
39 #define d_right(dc) (dc)->u.s_binary.right
41 /* Functions related to demangled name parsing. */
43 static unsigned int cp_find_first_component_aux (const char *name,
46 static void demangled_name_complaint (const char *name);
48 /* Functions/variables related to overload resolution. */
50 static int sym_return_val_size;
51 static int sym_return_val_index;
52 static struct symbol **sym_return_val;
54 static void overload_list_add_symbol (struct symbol *sym,
55 const char *oload_name);
57 static void make_symbol_overload_list_using (const char *func_name,
58 const char *namespace);
60 static void make_symbol_overload_list_qualified (const char *func_name);
62 static void read_in_psymtabs (const char *oload_name);
64 /* The list of "maint cplus" commands. */
66 struct cmd_list_element *maint_cplus_cmd_list = NULL;
68 /* The actual commands. */
70 static void maint_cplus_command (char *arg, int from_tty);
71 static void first_component_command (char *arg, int from_tty);
73 /* Return 1 if STRING is clearly already in canonical form. This
74 function is conservative; things which it does not recognize are
75 assumed to be non-canonical, and the parser will sort them out
76 afterwards. This speeds up the critical path for alphanumeric
80 cp_already_canonical (const char *string)
82 /* Identifier start character [a-zA-Z_]. */
83 if (!ISIDST (string[0]))
86 /* These are the only two identifiers which canonicalize to other
87 than themselves or an error: unsigned -> unsigned int and
89 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
91 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
94 /* Identifier character [a-zA-Z0-9_]. */
95 while (ISIDNUM (string[1]))
98 if (string[1] == '\0')
104 /* Parse STRING and convert it to canonical form. If parsing fails,
105 or if STRING is already canonical, return NULL. Otherwise return
106 the canonical form. The return value is allocated via xmalloc. */
109 cp_canonicalize_string (const char *string)
111 struct demangle_component *ret_comp;
112 unsigned int estimated_len;
115 if (cp_already_canonical (string))
118 ret_comp = cp_demangled_name_to_comp (string, NULL);
119 if (ret_comp == NULL)
122 estimated_len = strlen (string) * 2;
123 ret = cp_comp_to_string (ret_comp, estimated_len);
125 if (strcmp (string, ret) == 0)
134 /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the
135 block of used memory that should be freed when finished with the tree.
136 DEMANGLED_P is set to the char * that should be freed when finished with
137 the tree, or NULL if none was needed. OPTIONS will be passed to the
140 static struct demangle_component *
141 mangled_name_to_comp (const char *mangled_name, int options,
142 void **memory, char **demangled_p)
144 struct demangle_component *ret;
145 char *demangled_name;
148 /* If it looks like a v3 mangled name, then try to go directly
150 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
152 ret = cplus_demangle_v3_components (mangled_name, options, memory);
160 /* If it doesn't, or if that failed, then try to demangle the name. */
161 demangled_name = cplus_demangle (mangled_name, options);
162 if (demangled_name == NULL)
165 /* If we could demangle the name, parse it to build the component tree. */
166 ret = cp_demangled_name_to_comp (demangled_name, NULL);
170 xfree (demangled_name);
174 *demangled_p = demangled_name;
178 /* Return the name of the class containing method PHYSNAME. */
181 cp_class_name_from_physname (const char *physname)
183 void *storage = NULL;
184 char *demangled_name = NULL, *ret;
185 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
188 ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage,
190 if (ret_comp == NULL)
195 /* First strip off any qualifiers, if we have a function or method. */
197 switch (ret_comp->type)
199 case DEMANGLE_COMPONENT_CONST:
200 case DEMANGLE_COMPONENT_RESTRICT:
201 case DEMANGLE_COMPONENT_VOLATILE:
202 case DEMANGLE_COMPONENT_CONST_THIS:
203 case DEMANGLE_COMPONENT_RESTRICT_THIS:
204 case DEMANGLE_COMPONENT_VOLATILE_THIS:
205 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
206 ret_comp = d_left (ret_comp);
213 /* If what we have now is a function, discard the argument list. */
214 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
215 ret_comp = d_left (ret_comp);
217 /* If what we have now is a template, strip off the template
218 arguments. The left subtree may be a qualified name. */
219 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
220 ret_comp = d_left (ret_comp);
222 /* What we have now should be a name, possibly qualified. Additional
223 qualifiers could live in the left subtree or the right subtree. Find
229 switch (cur_comp->type)
231 case DEMANGLE_COMPONENT_QUAL_NAME:
232 case DEMANGLE_COMPONENT_LOCAL_NAME:
233 prev_comp = cur_comp;
234 cur_comp = d_right (cur_comp);
236 case DEMANGLE_COMPONENT_TEMPLATE:
237 case DEMANGLE_COMPONENT_NAME:
238 case DEMANGLE_COMPONENT_CTOR:
239 case DEMANGLE_COMPONENT_DTOR:
240 case DEMANGLE_COMPONENT_OPERATOR:
241 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
251 if (cur_comp != NULL && prev_comp != NULL)
253 /* We want to discard the rightmost child of PREV_COMP. */
254 *prev_comp = *d_left (prev_comp);
255 /* The ten is completely arbitrary; we don't have a good estimate. */
256 ret = cp_comp_to_string (ret_comp, 10);
261 xfree (demangled_name);
265 /* Return the child of COMP which is the basename of a method, variable,
266 et cetera. All scope qualifiers are discarded, but template arguments
267 will be included. The component tree may be modified. */
269 static struct demangle_component *
270 unqualified_name_from_comp (struct demangle_component *comp)
272 struct demangle_component *ret_comp = comp, *last_template;
276 last_template = NULL;
278 switch (ret_comp->type)
280 case DEMANGLE_COMPONENT_QUAL_NAME:
281 case DEMANGLE_COMPONENT_LOCAL_NAME:
282 ret_comp = d_right (ret_comp);
284 case DEMANGLE_COMPONENT_TYPED_NAME:
285 ret_comp = d_left (ret_comp);
287 case DEMANGLE_COMPONENT_TEMPLATE:
288 gdb_assert (last_template == NULL);
289 last_template = ret_comp;
290 ret_comp = d_left (ret_comp);
292 case DEMANGLE_COMPONENT_CONST:
293 case DEMANGLE_COMPONENT_RESTRICT:
294 case DEMANGLE_COMPONENT_VOLATILE:
295 case DEMANGLE_COMPONENT_CONST_THIS:
296 case DEMANGLE_COMPONENT_RESTRICT_THIS:
297 case DEMANGLE_COMPONENT_VOLATILE_THIS:
298 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
299 ret_comp = d_left (ret_comp);
301 case DEMANGLE_COMPONENT_NAME:
302 case DEMANGLE_COMPONENT_CTOR:
303 case DEMANGLE_COMPONENT_DTOR:
304 case DEMANGLE_COMPONENT_OPERATOR:
305 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
315 d_left (last_template) = ret_comp;
316 return last_template;
322 /* Return the name of the method whose linkage name is PHYSNAME. */
325 method_name_from_physname (const char *physname)
327 void *storage = NULL;
328 char *demangled_name = NULL, *ret;
329 struct demangle_component *ret_comp;
332 ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage,
334 if (ret_comp == NULL)
337 ret_comp = unqualified_name_from_comp (ret_comp);
340 if (ret_comp != NULL)
341 /* The ten is completely arbitrary; we don't have a good estimate. */
342 ret = cp_comp_to_string (ret_comp, 10);
346 xfree (demangled_name);
350 /* If FULL_NAME is the demangled name of a C++ function (including an
351 arg list, possibly including namespace/class qualifications),
352 return a new string containing only the function name (without the
353 arg list/class qualifications). Otherwise, return NULL. The
354 caller is responsible for freeing the memory in question. */
357 cp_func_name (const char *full_name)
360 struct demangle_component *ret_comp;
363 ret_comp = cp_demangled_name_to_comp (full_name, NULL);
367 ret_comp = unqualified_name_from_comp (ret_comp);
370 if (ret_comp != NULL)
371 ret = cp_comp_to_string (ret_comp, 10);
376 /* DEMANGLED_NAME is the name of a function, including parameters and
377 (optionally) a return type. Return the name of the function without
378 parameters or return type, or NULL if we can not parse the name. */
381 cp_remove_params (const char *demangled_name)
384 struct demangle_component *ret_comp;
387 if (demangled_name == NULL)
390 ret_comp = cp_demangled_name_to_comp (demangled_name, NULL);
391 if (ret_comp == NULL)
394 /* First strip off any qualifiers, if we have a function or method. */
396 switch (ret_comp->type)
398 case DEMANGLE_COMPONENT_CONST:
399 case DEMANGLE_COMPONENT_RESTRICT:
400 case DEMANGLE_COMPONENT_VOLATILE:
401 case DEMANGLE_COMPONENT_CONST_THIS:
402 case DEMANGLE_COMPONENT_RESTRICT_THIS:
403 case DEMANGLE_COMPONENT_VOLATILE_THIS:
404 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
405 ret_comp = d_left (ret_comp);
412 /* What we have now should be a function. Return its name. */
413 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
414 ret = cp_comp_to_string (d_left (ret_comp), 10);
419 /* Here are some random pieces of trivia to keep in mind while trying
420 to take apart demangled names:
422 - Names can contain function arguments or templates, so the process
423 has to be, to some extent recursive: maybe keep track of your
424 depth based on encountering <> and ().
426 - Parentheses don't just have to happen at the end of a name: they
427 can occur even if the name in question isn't a function, because
428 a template argument might be a type that's a function.
430 - Conversely, even if you're trying to deal with a function, its
431 demangled name might not end with ')': it could be a const or
432 volatile class method, in which case it ends with "const" or
435 - Parentheses are also used in anonymous namespaces: a variable
436 'foo' in an anonymous namespace gets demangled as "(anonymous
439 - And operator names can contain parentheses or angle brackets. */
441 /* FIXME: carlton/2003-03-13: We have several functions here with
442 overlapping functionality; can we combine them? Also, do they
443 handle all the above considerations correctly? */
446 /* This returns the length of first component of NAME, which should be
447 the demangled name of a C++ variable/function/method/etc.
448 Specifically, it returns the index of the first colon forming the
449 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
450 it returns the 1, and given 'foo', it returns 0. */
452 /* The character in NAME indexed by the return value is guaranteed to
453 always be either ':' or '\0'. */
455 /* NOTE: carlton/2003-03-13: This function is currently only intended
456 for internal use: it's probably not entirely safe when called on
457 user-generated input, because some of the 'index += 2' lines in
458 cp_find_first_component_aux might go past the end of malformed
462 cp_find_first_component (const char *name)
464 return cp_find_first_component_aux (name, 0);
467 /* Helper function for cp_find_first_component. Like that function,
468 it returns the length of the first component of NAME, but to make
469 the recursion easier, it also stops if it reaches an unexpected ')'
470 or '>' if the value of PERMISSIVE is nonzero. */
472 /* Let's optimize away calls to strlen("operator"). */
474 #define LENGTH_OF_OPERATOR 8
477 cp_find_first_component_aux (const char *name, int permissive)
479 unsigned int index = 0;
480 /* Operator names can show up in unexpected places. Since these can
481 contain parentheses or angle brackets, they can screw up the
482 recursion. But not every string 'operator' is part of an
483 operater name: e.g. you could have a variable 'cooperator'. So
484 this variable tells us whether or not we should treat the string
485 'operator' as starting an operator. */
486 int operator_possible = 1;
493 /* Template; eat it up. The calls to cp_first_component
494 should only return (I hope!) when they reach the '>'
495 terminating the component or a '::' between two
496 components. (Hence the '+ 2'.) */
498 for (index += cp_find_first_component_aux (name + index, 1);
500 index += cp_find_first_component_aux (name + index, 1))
502 if (name[index] != ':')
504 demangled_name_complaint (name);
505 return strlen (name);
509 operator_possible = 1;
512 /* Similar comment as to '<'. */
514 for (index += cp_find_first_component_aux (name + index, 1);
516 index += cp_find_first_component_aux (name + index, 1))
518 if (name[index] != ':')
520 demangled_name_complaint (name);
521 return strlen (name);
525 operator_possible = 1;
533 demangled_name_complaint (name);
534 return strlen (name);
540 /* Operator names can screw up the recursion. */
541 if (operator_possible
542 && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
544 index += LENGTH_OF_OPERATOR;
545 while (ISSPACE(name[index]))
549 /* Skip over one less than the appropriate number of
550 characters: the for loop will skip over the last
553 if (name[index + 1] == '<')
560 if (name[index + 1] == '>')
573 operator_possible = 0;
580 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
581 set of relevant characters are here: it's necessary to
582 include any character that can show up before 'operator'
583 in a demangled name, and it's safe to include any
584 character that can't be part of an identifier's name. */
585 operator_possible = 1;
588 operator_possible = 0;
594 /* Complain about a demangled name that we don't know how to parse.
595 NAME is the demangled name in question. */
598 demangled_name_complaint (const char *name)
600 complaint (&symfile_complaints,
601 "unexpected demangled name '%s'", name);
604 /* If NAME is the fully-qualified name of a C++
605 function/variable/method/etc., this returns the length of its
606 entire prefix: all of the namespaces and classes that make up its
607 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
608 4, given 'foo', it returns 0. */
611 cp_entire_prefix_len (const char *name)
613 unsigned int current_len = cp_find_first_component (name);
614 unsigned int previous_len = 0;
616 while (name[current_len] != '\0')
618 gdb_assert (name[current_len] == ':');
619 previous_len = current_len;
622 current_len += cp_find_first_component (name + current_len);
628 /* Overload resolution functions. */
630 /* Test to see if SYM is a symbol that we haven't seen corresponding
631 to a function named OLOAD_NAME. If so, add it to the current
635 overload_list_add_symbol (struct symbol *sym, const char *oload_name)
641 /* If there is no type information, we can't do anything, so skip */
642 if (SYMBOL_TYPE (sym) == NULL)
645 /* skip any symbols that we've already considered. */
646 for (i = 0; i < sym_return_val_index; ++i)
647 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
648 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
651 /* Get the demangled name without parameters */
652 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
656 /* skip symbols that cannot match */
657 if (strcmp (sym_name, oload_name) != 0)
665 /* We have a match for an overload instance, so add SYM to the current list
666 * of overload instances */
667 if (sym_return_val_index + 3 > sym_return_val_size)
669 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
670 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
672 sym_return_val[sym_return_val_index++] = sym;
673 sym_return_val[sym_return_val_index] = NULL;
676 /* Return a null-terminated list of pointers to function symbols that
677 are named FUNC_NAME and are visible within NAMESPACE. */
680 make_symbol_overload_list (const char *func_name,
681 const char *namespace)
683 struct cleanup *old_cleanups;
685 sym_return_val_size = 100;
686 sym_return_val_index = 0;
687 sym_return_val = xmalloc ((sym_return_val_size + 1) *
688 sizeof (struct symbol *));
689 sym_return_val[0] = NULL;
691 old_cleanups = make_cleanup (xfree, sym_return_val);
693 make_symbol_overload_list_using (func_name, namespace);
695 discard_cleanups (old_cleanups);
697 return sym_return_val;
700 /* This applies the using directives to add namespaces to search in,
701 and then searches for overloads in all of those namespaces. It
702 adds the symbols found to sym_return_val. Arguments are as in
703 make_symbol_overload_list. */
706 make_symbol_overload_list_using (const char *func_name,
707 const char *namespace)
709 const struct using_direct *current;
711 /* First, go through the using directives. If any of them apply,
712 look in the appropriate namespaces for new functions to match
715 for (current = block_using (get_selected_block (0));
717 current = current->next)
719 if (strcmp (namespace, current->import_dest) == 0)
721 make_symbol_overload_list_using (func_name,
722 current->import_src);
726 /* Now, add names for this namespace. */
728 if (namespace[0] == '\0')
730 make_symbol_overload_list_qualified (func_name);
734 char *concatenated_name
735 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
736 strcpy (concatenated_name, namespace);
737 strcat (concatenated_name, "::");
738 strcat (concatenated_name, func_name);
739 make_symbol_overload_list_qualified (concatenated_name);
743 /* This does the bulk of the work of finding overloaded symbols.
744 FUNC_NAME is the name of the overloaded function we're looking for
745 (possibly including namespace info). */
748 make_symbol_overload_list_qualified (const char *func_name)
752 struct objfile *objfile;
753 const struct block *b, *surrounding_static_block = 0;
754 struct dict_iterator iter;
755 const struct dictionary *dict;
757 /* Look through the partial symtabs for all symbols which begin
758 by matching FUNC_NAME. Make sure we read that symbol table in. */
760 read_in_psymtabs (func_name);
762 /* Search upwards from currently selected frame (so that we can
763 complete on local vars. */
765 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
767 dict = BLOCK_DICT (b);
769 for (sym = dict_iter_name_first (dict, func_name, &iter);
771 sym = dict_iter_name_next (func_name, &iter))
773 overload_list_add_symbol (sym, func_name);
777 surrounding_static_block = block_static_block (get_selected_block (0));
779 /* Go through the symtabs and check the externs and statics for
780 symbols which match. */
782 ALL_PRIMARY_SYMTABS (objfile, s)
785 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
786 dict = BLOCK_DICT (b);
788 for (sym = dict_iter_name_first (dict, func_name, &iter);
790 sym = dict_iter_name_next (func_name, &iter))
792 overload_list_add_symbol (sym, func_name);
796 ALL_PRIMARY_SYMTABS (objfile, s)
799 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
800 /* Don't do this block twice. */
801 if (b == surrounding_static_block)
803 dict = BLOCK_DICT (b);
805 for (sym = dict_iter_name_first (dict, func_name, &iter);
807 sym = dict_iter_name_next (func_name, &iter))
809 overload_list_add_symbol (sym, func_name);
814 /* Look through the partial symtabs for all symbols which begin
815 by matching FUNC_NAME. Make sure we read that symbol table in. */
818 read_in_psymtabs (const char *func_name)
820 struct partial_symtab *ps;
821 struct objfile *objfile;
823 ALL_PSYMTABS (objfile, ps)
828 if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN)
830 || (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN)
832 psymtab_to_symtab (ps);
836 /* Lookup the rtti type for a class name. */
839 cp_lookup_rtti_type (const char *name, struct block *block)
841 struct symbol * rtti_sym;
842 struct type * rtti_type;
844 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
846 if (rtti_sym == NULL)
848 warning (_("RTTI symbol not found for class '%s'"), name);
852 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
854 warning (_("RTTI symbol for class '%s' is not a type"), name);
858 rtti_type = SYMBOL_TYPE (rtti_sym);
860 switch (TYPE_CODE (rtti_type))
862 case TYPE_CODE_CLASS:
864 case TYPE_CODE_NAMESPACE:
865 /* chastain/2003-11-26: the symbol tables often contain fake
866 symbols for namespaces with the same name as the struct.
867 This warning is an indication of a bug in the lookup order
868 or a bug in the way that the symbol tables are populated. */
869 warning (_("RTTI symbol for class '%s' is a namespace"), name);
872 warning (_("RTTI symbol for class '%s' has bad type"), name);
879 /* Don't allow just "maintenance cplus". */
882 maint_cplus_command (char *arg, int from_tty)
884 printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n"));
885 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
888 /* This is a front end for cp_find_first_component, for unit testing.
889 Be careful when using it: see the NOTE above
890 cp_find_first_component. */
893 first_component_command (char *arg, int from_tty)
901 len = cp_find_first_component (arg);
902 prefix = alloca (len + 1);
904 memcpy (prefix, arg, len);
907 printf_unfiltered ("%s\n", prefix);
910 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
913 _initialize_cp_support (void)
915 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
916 _("C++ maintenance commands."), &maint_cplus_cmd_list,
917 "maintenance cplus ", 0, &maintenancelist);
918 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
920 add_cmd ("first_component", class_maintenance, first_component_command,
921 _("Print the first class/namespace component of NAME."),
922 &maint_cplus_cmd_list);