1 /* Helper routines for C++ support in GDB.
2 Copyright 2002, 2003 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "cp-support.h"
26 #include "gdb_string.h"
28 #include "gdb_assert.h"
30 #include "dictionary.h"
35 #include "complaints.h"
38 /* Functions related to demangled name parsing. */
40 static const char *find_last_component (const char *name);
42 static unsigned int cp_find_first_component_aux (const char *name,
45 static void demangled_name_complaint (const char *name);
47 /* Functions/variables related to overload resolution. */
49 static int sym_return_val_size;
50 static int sym_return_val_index;
51 static struct symbol **sym_return_val;
53 static char *remove_params (const char *demangled_name);
55 static void overload_list_add_symbol (struct symbol *sym,
56 const char *oload_name);
58 static void make_symbol_overload_list_using (const char *func_name,
59 const char *namespace);
61 static void make_symbol_overload_list_qualified (const char *func_name);
63 static void read_in_psymtabs (const char *oload_name);
65 /* The list of "maint cplus" commands. */
67 struct cmd_list_element *maint_cplus_cmd_list = NULL;
69 /* The actual commands. */
71 static void maint_cplus_command (char *arg, int from_tty);
72 static void first_component_command (char *arg, int from_tty);
74 /* Here are some random pieces of trivia to keep in mind while trying
75 to take apart demangled names:
77 - Names can contain function arguments or templates, so the process
78 has to be, to some extent recursive: maybe keep track of your
79 depth based on encountering <> and ().
81 - Parentheses don't just have to happen at the end of a name: they
82 can occur even if the name in question isn't a function, because
83 a template argument might be a type that's a function.
85 - Conversely, even if you're trying to deal with a function, its
86 demangled name might not end with ')': it could be a const or
87 volatile class method, in which case it ends with "const" or
90 - Parentheses are also used in anonymous namespaces: a variable
91 'foo' in an anonymous namespace gets demangled as "(anonymous
94 - And operator names can contain parentheses or angle brackets. */
96 /* FIXME: carlton/2003-03-13: We have several functions here with
97 overlapping functionality; can we combine them? Also, do they
98 handle all the above considerations correctly? */
100 /* Find the last component of the demangled C++ name NAME. NAME
101 must be a method name including arguments, in order to correctly
102 locate the last component.
104 This function return a pointer to the first colon before the
105 last component, or NULL if the name had only one component. */
108 find_last_component (const char *name)
113 /* Functions can have local classes, so we need to find the
114 beginning of the last argument list, not the end of the first
116 p = name + strlen (name) - 1;
117 while (p > name && *p != ')')
123 /* P now points at the `)' at the end of the argument list. Walk
124 back to the beginning. */
127 while (p > name && depth > 0)
129 if (*p == '<' || *p == '(')
131 else if (*p == '>' || *p == ')')
139 while (p > name && *p != ':')
142 if (p == name || p == name + 1 || p[-1] != ':')
148 /* Return the name of the class containing method PHYSNAME. */
151 cp_class_name_from_physname (const char *physname)
156 char *demangled_name = cplus_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
158 if (demangled_name == NULL)
161 end = find_last_component (demangled_name);
164 ret = xmalloc (end - demangled_name + 1);
165 memcpy (ret, demangled_name, end - demangled_name);
166 ret[end - demangled_name] = '\0';
169 xfree (demangled_name);
173 /* Return the name of the method whose linkage name is PHYSNAME. */
176 method_name_from_physname (const char *physname)
181 char *demangled_name = cplus_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
183 if (demangled_name == NULL)
186 end = find_last_component (demangled_name);
195 /* Find the argument list, if any. */
196 args = strchr (end, '(');
198 len = strlen (end + 2);
204 len = args - end + 1;
206 ret = xmalloc (len + 1);
207 memcpy (ret, end, len);
211 xfree (demangled_name);
215 /* This returns the length of first component of NAME, which should be
216 the demangled name of a C++ variable/function/method/etc.
217 Specifically, it returns the index of the first colon forming the
218 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
219 it returns the 1, and given 'foo', it returns 0. */
221 /* The character in NAME indexed by the return value is guaranteed to
222 always be either ':' or '\0'. */
224 /* NOTE: carlton/2003-03-13: This function is currently only intended
225 for internal use: it's probably not entirely safe when called on
226 user-generated input, because some of the 'index += 2' lines in
227 cp_find_first_component_aux might go past the end of malformed
231 cp_find_first_component (const char *name)
233 return cp_find_first_component_aux (name, 0);
236 /* Helper function for cp_find_first_component. Like that function,
237 it returns the length of the first component of NAME, but to make
238 the recursion easier, it also stops if it reaches an unexpected ')'
239 or '>' if the value of PERMISSIVE is nonzero. */
241 /* Let's optimize away calls to strlen("operator"). */
243 #define LENGTH_OF_OPERATOR 8
246 cp_find_first_component_aux (const char *name, int permissive)
248 unsigned int index = 0;
249 /* Operator names can show up in unexpected places. Since these can
250 contain parentheses or angle brackets, they can screw up the
251 recursion. But not every string 'operator' is part of an
252 operater name: e.g. you could have a variable 'cooperator'. So
253 this variable tells us whether or not we should treat the string
254 'operator' as starting an operator. */
255 int operator_possible = 1;
262 /* Template; eat it up. The calls to cp_first_component
263 should only return (I hope!) when they reach the '>'
264 terminating the component or a '::' between two
265 components. (Hence the '+ 2'.) */
267 for (index += cp_find_first_component_aux (name + index, 1);
269 index += cp_find_first_component_aux (name + index, 1))
271 if (name[index] != ':')
273 demangled_name_complaint (name);
274 return strlen (name);
278 operator_possible = 1;
281 /* Similar comment as to '<'. */
283 for (index += cp_find_first_component_aux (name + index, 1);
285 index += cp_find_first_component_aux (name + index, 1))
287 if (name[index] != ':')
289 demangled_name_complaint (name);
290 return strlen (name);
294 operator_possible = 1;
302 demangled_name_complaint (name);
303 return strlen (name);
309 /* Operator names can screw up the recursion. */
310 if (operator_possible
311 && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
313 index += LENGTH_OF_OPERATOR;
314 while (isspace(name[index]))
318 /* Skip over one less than the appropriate number of
319 characters: the for loop will skip over the last
322 if (name[index + 1] == '<')
329 if (name[index + 1] == '>')
342 operator_possible = 0;
349 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
350 set of relevant characters are here: it's necessary to
351 include any character that can show up before 'operator'
352 in a demangled name, and it's safe to include any
353 character that can't be part of an identifier's name. */
354 operator_possible = 1;
357 operator_possible = 0;
363 /* Complain about a demangled name that we don't know how to parse.
364 NAME is the demangled name in question. */
367 demangled_name_complaint (const char *name)
369 complaint (&symfile_complaints,
370 "unexpected demangled name '%s'", name);
373 /* If NAME is the fully-qualified name of a C++
374 function/variable/method/etc., this returns the length of its
375 entire prefix: all of the namespaces and classes that make up its
376 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
377 4, given 'foo', it returns 0. */
380 cp_entire_prefix_len (const char *name)
382 unsigned int current_len = cp_find_first_component (name);
383 unsigned int previous_len = 0;
385 while (name[current_len] != '\0')
387 gdb_assert (name[current_len] == ':');
388 previous_len = current_len;
391 current_len += cp_find_first_component (name + current_len);
397 /* If FULL_NAME is the demangled name of a C++ function (including an
398 arg list, possibly including namespace/class qualifications),
399 return a new string containing only the function name (without the
400 arg list/class qualifications). Otherwise, return NULL. The
401 caller is responsible for freeing the memory in question. */
404 cp_func_name (const char *full_name)
406 const char *previous_component = full_name;
407 const char *next_component;
412 for (next_component = (previous_component
413 + cp_find_first_component (previous_component));
414 *next_component == ':';
415 next_component = (previous_component
416 + cp_find_first_component (previous_component)))
419 previous_component = next_component + 2;
422 return remove_params (previous_component);
425 /* Overload resolution functions. */
428 remove_params (const char *demangled_name)
434 if (demangled_name == NULL)
437 /* First find the end of the arg list. */
438 argp = strrchr (demangled_name, ')');
442 /* Back up to the beginning. */
445 while (argp-- > demangled_name)
449 else if (*argp == '(')
458 internal_error (__FILE__, __LINE__,
459 "bad demangled name %s\n", demangled_name);
460 while (argp[-1] == ' ' && argp > demangled_name)
463 new_name = xmalloc (argp - demangled_name + 1);
464 memcpy (new_name, demangled_name, argp - demangled_name);
465 new_name[argp - demangled_name] = '\0';
469 /* Test to see if SYM is a symbol that we haven't seen corresponding
470 to a function named OLOAD_NAME. If so, add it to the current
474 overload_list_add_symbol (struct symbol *sym, const char *oload_name)
480 /* If there is no type information, we can't do anything, so skip */
481 if (SYMBOL_TYPE (sym) == NULL)
484 /* skip any symbols that we've already considered. */
485 for (i = 0; i < sym_return_val_index; ++i)
486 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
487 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
490 /* Get the demangled name without parameters */
491 sym_name = remove_params (SYMBOL_NATURAL_NAME (sym));
495 /* skip symbols that cannot match */
496 if (strcmp (sym_name, oload_name) != 0)
504 /* We have a match for an overload instance, so add SYM to the current list
505 * of overload instances */
506 if (sym_return_val_index + 3 > sym_return_val_size)
508 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
509 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
511 sym_return_val[sym_return_val_index++] = sym;
512 sym_return_val[sym_return_val_index] = NULL;
515 /* Return a null-terminated list of pointers to function symbols that
516 are named FUNC_NAME and are visible within NAMESPACE. */
519 make_symbol_overload_list (const char *func_name,
520 const char *namespace)
522 struct cleanup *old_cleanups;
524 sym_return_val_size = 100;
525 sym_return_val_index = 0;
526 sym_return_val = xmalloc ((sym_return_val_size + 1) *
527 sizeof (struct symbol *));
528 sym_return_val[0] = NULL;
530 old_cleanups = make_cleanup (xfree, sym_return_val);
532 make_symbol_overload_list_using (func_name, namespace);
534 discard_cleanups (old_cleanups);
536 return sym_return_val;
539 /* This applies the using directives to add namespaces to search in,
540 and then searches for overloads in all of those namespaces. It
541 adds the symbols found to sym_return_val. Arguments are as in
542 make_symbol_overload_list. */
545 make_symbol_overload_list_using (const char *func_name,
546 const char *namespace)
548 const struct using_direct *current;
550 /* First, go through the using directives. If any of them apply,
551 look in the appropriate namespaces for new functions to match
554 for (current = block_using (get_selected_block (0));
556 current = current->next)
558 if (strcmp (namespace, current->outer) == 0)
560 make_symbol_overload_list_using (func_name,
565 /* Now, add names for this namespace. */
567 if (namespace[0] == '\0')
569 make_symbol_overload_list_qualified (func_name);
573 char *concatenated_name
574 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
575 strcpy (concatenated_name, namespace);
576 strcat (concatenated_name, "::");
577 strcat (concatenated_name, func_name);
578 make_symbol_overload_list_qualified (concatenated_name);
582 /* This does the bulk of the work of finding overloaded symbols.
583 FUNC_NAME is the name of the overloaded function we're looking for
584 (possibly including namespace info). */
587 make_symbol_overload_list_qualified (const char *func_name)
591 struct objfile *objfile;
592 const struct block *b, *surrounding_static_block = 0;
593 struct dict_iterator iter;
594 const struct dictionary *dict;
596 /* Look through the partial symtabs for all symbols which begin
597 by matching FUNC_NAME. Make sure we read that symbol table in. */
599 read_in_psymtabs (func_name);
601 /* Search upwards from currently selected frame (so that we can
602 complete on local vars. */
604 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
606 dict = BLOCK_DICT (b);
608 for (sym = dict_iter_name_first (dict, func_name, &iter);
610 sym = dict_iter_name_next (func_name, &iter))
612 overload_list_add_symbol (sym, func_name);
616 surrounding_static_block = block_static_block (get_selected_block (0));
618 /* Go through the symtabs and check the externs and statics for
619 symbols which match. */
621 ALL_SYMTABS (objfile, s)
624 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
625 dict = BLOCK_DICT (b);
627 for (sym = dict_iter_name_first (dict, func_name, &iter);
629 sym = dict_iter_name_next (func_name, &iter))
631 overload_list_add_symbol (sym, func_name);
635 ALL_SYMTABS (objfile, s)
638 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
639 /* Don't do this block twice. */
640 if (b == surrounding_static_block)
642 dict = BLOCK_DICT (b);
644 for (sym = dict_iter_name_first (dict, func_name, &iter);
646 sym = dict_iter_name_next (func_name, &iter))
648 overload_list_add_symbol (sym, func_name);
653 /* Look through the partial symtabs for all symbols which begin
654 by matching FUNC_NAME. Make sure we read that symbol table in. */
657 read_in_psymtabs (const char *func_name)
659 struct partial_symtab *ps;
660 struct objfile *objfile;
662 ALL_PSYMTABS (objfile, ps)
667 if ((lookup_partial_symbol (ps, func_name, NULL, 1, VAR_DOMAIN)
669 || (lookup_partial_symbol (ps, func_name, NULL, 0, VAR_DOMAIN)
671 psymtab_to_symtab (ps);
675 /* Lookup the rtti type for a class name. */
678 cp_lookup_rtti_type (const char *name, struct block *block)
680 struct symbol * rtti_sym;
681 struct type * rtti_type;
683 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL, NULL);
685 if (rtti_sym == NULL)
687 warning ("RTTI symbol not found for class '%s'", name);
691 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
693 warning ("RTTI symbol for class '%s' is not a type", name);
697 rtti_type = SYMBOL_TYPE (rtti_sym);
699 switch (TYPE_CODE (rtti_type))
701 case TYPE_CODE_CLASS:
703 case TYPE_CODE_NAMESPACE:
704 /* chastain/2003-11-26: the symbol tables often contain fake
705 symbols for namespaces with the same name as the struct.
706 This warning is an indication of a bug in the lookup order
707 or a bug in the way that the symbol tables are populated. */
708 warning ("RTTI symbol for class '%s' is a namespace", name);
711 warning ("RTTI symbol for class '%s' has bad type", name);
718 /* Don't allow just "maintenance cplus". */
721 maint_cplus_command (char *arg, int from_tty)
723 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
724 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
727 /* This is a front end for cp_find_first_component, for unit testing.
728 Be careful when using it: see the NOTE above
729 cp_find_first_component. */
732 first_component_command (char *arg, int from_tty)
734 int len = cp_find_first_component (arg);
735 char *prefix = alloca (len + 1);
737 memcpy (prefix, arg, len);
740 printf_unfiltered ("%s\n", prefix);
743 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
746 _initialize_cp_support (void)
748 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
749 "C++ maintenance commands.", &maint_cplus_cmd_list,
750 "maintenance cplus ", 0, &maintenancelist);
751 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
753 add_cmd ("first_component", class_maintenance, first_component_command,
754 "Print the first class/namespace component of NAME.",
755 &maint_cplus_cmd_list);