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"
37 /* Functions related to demangled name parsing. */
39 static const char *find_last_component (const char *name);
41 static unsigned int cp_find_first_component_aux (const char *name,
44 static void demangled_name_complaint (const char *name);
46 /* Functions/variables related to overload resolution. */
48 static int sym_return_val_size;
49 static int sym_return_val_index;
50 static struct symbol **sym_return_val;
52 static char *remove_params (const char *demangled_name);
54 static void overload_list_add_symbol (struct symbol *sym, char *oload_name);
56 /* The list of "maint cplus" commands. */
58 static struct cmd_list_element *maint_cplus_cmd_list = NULL;
60 /* The actual commands. */
62 static void maint_cplus_command (char *arg, int from_tty);
63 static void first_component_command (char *arg, int from_tty);
65 /* Here are some random pieces of trivia to keep in mind while trying
66 to take apart demangled names:
68 - Names can contain function arguments or templates, so the process
69 has to be, to some extent recursive: maybe keep track of your
70 depth based on encountering <> and ().
72 - Parentheses don't just have to happen at the end of a name: they
73 can occur even if the name in question isn't a function, because
74 a template argument might be a type that's a function.
76 - Conversely, even if you're trying to deal with a function, its
77 demangled name might not end with ')': it could be a const or
78 volatile class method, in which case it ends with "const" or
81 - Parentheses are also used in anonymous namespaces: a variable
82 'foo' in an anonymous namespace gets demangled as "(anonymous
85 - And operator names can contain parentheses or angle brackets. */
87 /* FIXME: carlton/2003-03-13: We have several functions here with
88 overlapping functionality; can we combine them? Also, do they
89 handle all the above considerations correctly? */
91 /* Find the last component of the demangled C++ name NAME. NAME
92 must be a method name including arguments, in order to correctly
93 locate the last component.
95 This function return a pointer to the first colon before the
96 last component, or NULL if the name had only one component. */
99 find_last_component (const char *name)
104 /* Functions can have local classes, so we need to find the
105 beginning of the last argument list, not the end of the first
107 p = name + strlen (name) - 1;
108 while (p > name && *p != ')')
114 /* P now points at the `)' at the end of the argument list. Walk
115 back to the beginning. */
118 while (p > name && depth > 0)
120 if (*p == '<' || *p == '(')
122 else if (*p == '>' || *p == ')')
130 while (p > name && *p != ':')
133 if (p == name || p == name + 1 || p[-1] != ':')
139 /* Return the name of the class containing method PHYSNAME. */
142 class_name_from_physname (const char *physname)
147 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
149 if (demangled_name == NULL)
152 end = find_last_component (demangled_name);
155 ret = xmalloc (end - demangled_name + 1);
156 memcpy (ret, demangled_name, end - demangled_name);
157 ret[end - demangled_name] = '\0';
160 xfree (demangled_name);
164 /* Return the name of the method whose linkage name is PHYSNAME. */
167 method_name_from_physname (const char *physname)
172 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
174 if (demangled_name == NULL)
177 end = find_last_component (demangled_name);
186 /* Find the argument list, if any. */
187 args = strchr (end, '(');
189 len = strlen (end + 2);
195 len = args - end + 1;
197 ret = xmalloc (len + 1);
198 memcpy (ret, end, len);
202 xfree (demangled_name);
206 /* This returns the length of first component of NAME, which should be
207 the demangled name of a C++ variable/function/method/etc.
208 Specifically, it returns the index of the first colon forming the
209 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
210 it returns the 1, and given 'foo', it returns 0. */
212 /* The character in NAME indexed by the return value is guaranteed to
213 always be either ':' or '\0'. */
215 /* NOTE: carlton/2003-03-13: This function is currently only intended
216 for internal use: it's probably not entirely safe when called on
217 user-generated input, because some of the 'index += 2' lines in
218 cp_find_first_component_aux might go past the end of malformed
222 cp_find_first_component (const char *name)
224 return cp_find_first_component_aux (name, 0);
227 /* Helper function for cp_find_first_component. Like that function,
228 it returns the length of the first component of NAME, but to make
229 the recursion easier, it also stops if it reaches an unexpected ')'
230 or '>' if the value of PERMISSIVE is nonzero. */
232 /* Let's optimize away calls to strlen("operator"). */
234 #define LENGTH_OF_OPERATOR 8
237 cp_find_first_component_aux (const char *name, int permissive)
239 unsigned int index = 0;
240 /* Operator names can show up in unexpected places. Since these can
241 contain parentheses or angle brackets, they can screw up the
242 recursion. But not every string 'operator' is part of an
243 operater name: e.g. you could have a variable 'cooperator'. So
244 this variable tells us whether or not we should treat the string
245 'operator' as starting an operator. */
246 int operator_possible = 1;
253 /* Template; eat it up. The calls to cp_first_component
254 should only return (I hope!) when they reach the '>'
255 terminating the component or a '::' between two
256 components. (Hence the '+ 2'.) */
258 for (index += cp_find_first_component_aux (name + index, 1);
260 index += cp_find_first_component_aux (name + index, 1))
262 if (name[index] != ':')
264 demangled_name_complaint (name);
265 return strlen (name);
269 operator_possible = 1;
272 /* Similar comment as to '<'. */
274 for (index += cp_find_first_component_aux (name + index, 1);
276 index += cp_find_first_component_aux (name + index, 1))
278 if (name[index] != ':')
280 demangled_name_complaint (name);
281 return strlen (name);
285 operator_possible = 1;
293 demangled_name_complaint (name);
294 return strlen (name);
300 /* Operator names can screw up the recursion. */
301 if (operator_possible
302 && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
304 index += LENGTH_OF_OPERATOR;
305 while (isspace(name[index]))
309 /* Skip over one less than the appropriate number of
310 characters: the for loop will skip over the last
313 if (name[index + 1] == '<')
320 if (name[index + 1] == '>')
333 operator_possible = 0;
340 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
341 set of relevant characters are here: it's necessary to
342 include any character that can show up before 'operator'
343 in a demangled name, and it's safe to include any
344 character that can't be part of an identifier's name. */
345 operator_possible = 1;
348 operator_possible = 0;
354 /* Complain about a demangled name that we don't know how to parse.
355 NAME is the demangled name in question. */
358 demangled_name_complaint (const char *name)
360 complaint (&symfile_complaints,
361 "unexpected demangled name '%s'", name);
364 /* If NAME is the fully-qualified name of a C++
365 function/variable/method/etc., this returns the length of its
366 entire prefix: all of the namespaces and classes that make up its
367 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
368 4, given 'foo', it returns 0. */
371 cp_entire_prefix_len (const char *name)
373 unsigned int current_len = cp_find_first_component (name);
374 unsigned int previous_len = 0;
376 while (name[current_len] != '\0')
378 gdb_assert (name[current_len] == ':');
379 previous_len = current_len;
382 current_len += cp_find_first_component (name + current_len);
388 /* Overload resolution functions. */
391 remove_params (const char *demangled_name)
397 if (demangled_name == NULL)
400 /* First find the end of the arg list. */
401 argp = strrchr (demangled_name, ')');
405 /* Back up to the beginning. */
408 while (argp-- > demangled_name)
412 else if (*argp == '(')
421 internal_error (__FILE__, __LINE__,
422 "bad demangled name %s\n", demangled_name);
423 while (argp[-1] == ' ' && argp > demangled_name)
426 new_name = xmalloc (argp - demangled_name + 1);
427 memcpy (new_name, demangled_name, argp - demangled_name);
428 new_name[argp - demangled_name] = '\0';
432 /* Test to see if the symbol specified by SYMNAME (which is already
433 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
434 characters. If so, add it to the current completion list. */
437 overload_list_add_symbol (struct symbol *sym, char *oload_name)
443 /* If there is no type information, we can't do anything, so skip */
444 if (SYMBOL_TYPE (sym) == NULL)
447 /* skip any symbols that we've already considered. */
448 for (i = 0; i < sym_return_val_index; ++i)
449 if (!strcmp (DEPRECATED_SYMBOL_NAME (sym), DEPRECATED_SYMBOL_NAME (sym_return_val[i])))
452 /* Get the demangled name without parameters */
453 sym_name = remove_params (SYMBOL_DEMANGLED_NAME (sym));
457 /* skip symbols that cannot match */
458 if (strcmp (sym_name, oload_name) != 0)
466 /* We have a match for an overload instance, so add SYM to the current list
467 * of overload instances */
468 if (sym_return_val_index + 3 > sym_return_val_size)
470 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
471 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
473 sym_return_val[sym_return_val_index++] = sym;
474 sym_return_val[sym_return_val_index] = NULL;
477 /* Return a null-terminated list of pointers to function symbols that
478 * match name of the supplied symbol FSYM.
479 * This is used in finding all overloaded instances of a function name.
480 * This has been modified from make_symbol_completion_list. */
484 make_symbol_overload_list (struct symbol *fsym)
486 register struct symbol *sym;
487 register struct symtab *s;
488 register struct partial_symtab *ps;
489 register struct objfile *objfile;
490 register struct block *b, *surrounding_static_block = 0;
491 struct dict_iterator iter;
492 /* The name we are completing on. */
493 char *oload_name = NULL;
494 /* Length of name. */
495 int oload_name_len = 0;
497 /* Look for the symbol we are supposed to complete on. */
499 oload_name = remove_params (SYMBOL_DEMANGLED_NAME (fsym));
502 sym_return_val_size = 1;
503 sym_return_val = (struct symbol **) xmalloc (2 * sizeof (struct symbol *));
504 sym_return_val[0] = fsym;
505 sym_return_val[1] = NULL;
507 return sym_return_val;
509 oload_name_len = strlen (oload_name);
511 sym_return_val_size = 100;
512 sym_return_val_index = 0;
513 sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *));
514 sym_return_val[0] = NULL;
516 /* Read in all partial symtabs containing a partial symbol named
519 ALL_PSYMTABS (objfile, ps)
521 struct partial_symbol **psym;
523 /* If the psymtab's been read in we'll get it when we search
524 through the blockvector. */
528 if ((lookup_partial_symbol (ps, oload_name, NULL, 1, VAR_DOMAIN)
530 || (lookup_partial_symbol (ps, oload_name, NULL, 0, VAR_DOMAIN)
532 PSYMTAB_TO_SYMTAB (ps);
535 /* Search upwards from currently selected frame (so that we can
536 complete on local vars. */
538 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
540 if (!BLOCK_SUPERBLOCK (b))
542 surrounding_static_block = b; /* For elimination of dups */
545 /* Also catch fields of types defined in this places which match our
546 text string. Only complete on types visible from current context. */
548 ALL_BLOCK_SYMBOLS (b, iter, sym)
550 overload_list_add_symbol (sym, oload_name);
554 /* Go through the symtabs and check the externs and statics for
555 symbols which match. */
557 ALL_SYMTABS (objfile, s)
560 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
561 ALL_BLOCK_SYMBOLS (b, iter, sym)
563 overload_list_add_symbol (sym, oload_name);
567 ALL_SYMTABS (objfile, s)
570 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
571 /* Don't do this block twice. */
572 if (b == surrounding_static_block)
574 ALL_BLOCK_SYMBOLS (b, iter, sym)
576 overload_list_add_symbol (sym, oload_name);
582 return (sym_return_val);
586 /* Don't allow just "maintenance cplus". */
589 maint_cplus_command (char *arg, int from_tty)
591 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
592 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
595 /* This is a front end for cp_find_first_component, for unit testing.
596 Be careful when using it: see the NOTE above
597 cp_find_first_component. */
600 first_component_command (char *arg, int from_tty)
602 int len = cp_find_first_component (arg);
603 char *prefix = alloca (len + 1);
605 memcpy (prefix, arg, len);
608 printf_unfiltered ("%s\n", prefix);
611 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
614 _initialize_cp_support (void)
616 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
617 "C++ maintenance commands.", &maint_cplus_cmd_list,
618 "maintenance cplus ", 0, &maintenancelist);
619 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
621 add_cmd ("first_component", class_maintenance, first_component_command,
622 "Print the first class/namespace component of NAME.",
623 &maint_cplus_cmd_list);