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"
36 /* Functions/variables related to overload resolution. */
38 static int sym_return_val_size
;
39 static int sym_return_val_index
;
40 static struct symbol
**sym_return_val
;
42 static char *remove_params (const char *demangled_name
);
44 static void overload_list_add_symbol (struct symbol
*sym
, char *oload_name
);
46 /* The list of "maint cplus" commands. */
48 static struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
50 /* The actual commands. */
52 static void maint_cplus_command (char *arg
, int from_tty
);
53 static void first_component_command (char *arg
, int from_tty
);
55 /* Here are some random pieces of trivia to keep in mind while trying
56 to take apart demangled names:
58 - Names can contain function arguments or templates, so the process
59 has to be, to some extent recursive: maybe keep track of your
60 depth based on encountering <> and ().
62 - Parentheses don't just have to happen at the end of a name: they
63 can occur even if the name in question isn't a function, because
64 a template argument might be a type that's a function.
66 - Conversely, even if you're trying to deal with a function, its
67 demangled name might not end with ')': it could be a const or
68 volatile class method, in which case it ends with "const" or
71 - Parentheses are also used in anonymous namespaces: a variable
72 'foo' in an anonymous namespace gets demangled as "(anonymous
75 - And operator names can contain parentheses or angle brackets. */
77 /* FIXME: carlton/2003-03-13: We have several functions here with
78 overlapping functionality; can we combine them? Also, do they
79 handle all the above considerations correctly? */
81 /* Find the last component of the demangled C++ name NAME. NAME
82 must be a method name including arguments, in order to correctly
83 locate the last component.
85 This function return a pointer to the first colon before the
86 last component, or NULL if the name had only one component. */
89 find_last_component (const char *name
)
94 /* Functions can have local classes, so we need to find the
95 beginning of the last argument list, not the end of the first
97 p
= name
+ strlen (name
) - 1;
98 while (p
> name
&& *p
!= ')')
104 /* P now points at the `)' at the end of the argument list. Walk
105 back to the beginning. */
108 while (p
> name
&& depth
> 0)
110 if (*p
== '<' || *p
== '(')
112 else if (*p
== '>' || *p
== ')')
120 while (p
> name
&& *p
!= ':')
123 if (p
== name
|| p
== name
+ 1 || p
[-1] != ':')
129 /* Return the name of the class containing method PHYSNAME. */
132 class_name_from_physname (const char *physname
)
137 char *demangled_name
= cplus_demangle (physname
, DMGL_ANSI
);
139 if (demangled_name
== NULL
)
142 end
= find_last_component (demangled_name
);
145 ret
= xmalloc (end
- demangled_name
+ 1);
146 memcpy (ret
, demangled_name
, end
- demangled_name
);
147 ret
[end
- demangled_name
] = '\0';
150 xfree (demangled_name
);
154 /* Return the name of the method whose linkage name is PHYSNAME. */
157 method_name_from_physname (const char *physname
)
162 char *demangled_name
= cplus_demangle (physname
, DMGL_ANSI
);
164 if (demangled_name
== NULL
)
167 end
= find_last_component (demangled_name
);
176 /* Find the argument list, if any. */
177 args
= strchr (end
, '(');
179 len
= strlen (end
+ 2);
185 len
= args
- end
+ 1;
187 ret
= xmalloc (len
+ 1);
188 memcpy (ret
, end
, len
);
192 xfree (demangled_name
);
196 /* This returns the length of first component of NAME, which should be
197 the demangled name of a C++ variable/function/method/etc.
198 Specifically, it returns the index of the first colon forming the
199 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
200 it returns the 1, and given 'foo', it returns 0. */
202 /* Well, that's what it should do when called externally, but to make
203 the recursion easier, it also stops if it reaches an unexpected ')'
206 /* NOTE: carlton/2003-03-13: This function is currently only intended
207 for internal use: it's probably not entirely safe when called on
208 user-generated input, because some of the 'index += 2' lines might
209 go past the end of malformed input. */
211 /* Let's optimize away calls to strlen("operator"). */
213 #define LENGTH_OF_OPERATOR 8
216 cp_find_first_component (const char *name
)
218 unsigned int index
= 0;
219 /* Operator names can show up in unexpected places. Since these can
220 contain parentheses or angle brackets, they can screw up the
221 recursion. But not every string 'operator' is part of an
222 operater name: e.g. you could have a variable 'cooperator'. So
223 this variable tells us whether or not we should treat the string
224 'operator' as starting an operator. */
225 int operator_possible
= 1;
232 /* Template; eat it up. The calls to cp_first_component
233 should only return (I hope!) when they reach the '>'
234 terminating the component or a '::' between two
235 components. (Hence the '+ 2'.) */
237 for (index
+= cp_find_first_component (name
+ index
);
239 index
+= cp_find_first_component (name
+ index
))
241 gdb_assert (name
[index
] == ':');
244 operator_possible
= 1;
247 /* Similar comment as to '<'. */
249 for (index
+= cp_find_first_component (name
+ index
);
251 index
+= cp_find_first_component (name
+ index
))
253 gdb_assert (name
[index
] == ':');
256 operator_possible
= 1;
264 /* Operator names can screw up the recursion. */
265 if (operator_possible
266 && strncmp (name
+ index
, "operator", LENGTH_OF_OPERATOR
) == 0)
268 index
+= LENGTH_OF_OPERATOR
;
269 while (isspace(name
[index
]))
273 /* Skip over one less than the appropriate number of
274 characters: the for loop will skip over the last
277 if (name
[index
+ 1] == '<')
284 if (name
[index
+ 1] == '>')
297 operator_possible
= 0;
304 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
305 set of relevant characters are here: it's necessary to
306 include any character that can show up before 'operator'
307 in a demangled name, and it's safe to include any
308 character that can't be part of an identifier's name. */
309 operator_possible
= 1;
312 operator_possible
= 0;
318 /* If NAME is the fully-qualified name of a C++
319 function/variable/method/etc., this returns the length of its
320 entire prefix: all of the namespaces and classes that make up its
321 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
322 4, given 'foo', it returns 0. */
325 cp_entire_prefix_len (const char *name
)
327 unsigned int current_len
= cp_find_first_component (name
);
328 unsigned int previous_len
= 0;
330 while (name
[current_len
] != '\0')
332 gdb_assert (name
[current_len
] == ':');
333 previous_len
= current_len
;
336 current_len
+= cp_find_first_component (name
+ current_len
);
342 /* Overload resolution functions. */
345 remove_params (const char *demangled_name
)
351 if (demangled_name
== NULL
)
354 /* First find the end of the arg list. */
355 argp
= strrchr (demangled_name
, ')');
359 /* Back up to the beginning. */
362 while (argp
-- > demangled_name
)
366 else if (*argp
== '(')
375 internal_error (__FILE__
, __LINE__
,
376 "bad demangled name %s\n", demangled_name
);
377 while (argp
[-1] == ' ' && argp
> demangled_name
)
380 new_name
= xmalloc (argp
- demangled_name
+ 1);
381 memcpy (new_name
, demangled_name
, argp
- demangled_name
);
382 new_name
[argp
- demangled_name
] = '\0';
386 /* Test to see if the symbol specified by SYMNAME (which is already
387 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
388 characters. If so, add it to the current completion list. */
391 overload_list_add_symbol (struct symbol
*sym
, char *oload_name
)
397 /* If there is no type information, we can't do anything, so skip */
398 if (SYMBOL_TYPE (sym
) == NULL
)
401 /* skip any symbols that we've already considered. */
402 for (i
= 0; i
< sym_return_val_index
; ++i
)
403 if (!strcmp (DEPRECATED_SYMBOL_NAME (sym
), DEPRECATED_SYMBOL_NAME (sym_return_val
[i
])))
406 /* Get the demangled name without parameters */
407 sym_name
= remove_params (SYMBOL_DEMANGLED_NAME (sym
));
411 /* skip symbols that cannot match */
412 if (strcmp (sym_name
, oload_name
) != 0)
420 /* We have a match for an overload instance, so add SYM to the current list
421 * of overload instances */
422 if (sym_return_val_index
+ 3 > sym_return_val_size
)
424 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
425 sym_return_val
= (struct symbol
**) xrealloc ((char *) sym_return_val
, newsize
);
427 sym_return_val
[sym_return_val_index
++] = sym
;
428 sym_return_val
[sym_return_val_index
] = NULL
;
431 /* Return a null-terminated list of pointers to function symbols that
432 * match name of the supplied symbol FSYM.
433 * This is used in finding all overloaded instances of a function name.
434 * This has been modified from make_symbol_completion_list. */
438 make_symbol_overload_list (struct symbol
*fsym
)
440 register struct symbol
*sym
;
441 register struct symtab
*s
;
442 register struct partial_symtab
*ps
;
443 register struct objfile
*objfile
;
444 register struct block
*b
, *surrounding_static_block
= 0;
445 struct dict_iterator iter
;
446 /* The name we are completing on. */
447 char *oload_name
= NULL
;
448 /* Length of name. */
449 int oload_name_len
= 0;
451 /* Look for the symbol we are supposed to complete on. */
453 oload_name
= remove_params (SYMBOL_DEMANGLED_NAME (fsym
));
456 sym_return_val_size
= 1;
457 sym_return_val
= (struct symbol
**) xmalloc (2 * sizeof (struct symbol
*));
458 sym_return_val
[0] = fsym
;
459 sym_return_val
[1] = NULL
;
461 return sym_return_val
;
463 oload_name_len
= strlen (oload_name
);
465 sym_return_val_size
= 100;
466 sym_return_val_index
= 0;
467 sym_return_val
= (struct symbol
**) xmalloc ((sym_return_val_size
+ 1) * sizeof (struct symbol
*));
468 sym_return_val
[0] = NULL
;
470 /* Read in all partial symtabs containing a partial symbol named
473 ALL_PSYMTABS (objfile
, ps
)
475 struct partial_symbol
**psym
;
477 /* If the psymtab's been read in we'll get it when we search
478 through the blockvector. */
482 if ((lookup_partial_symbol (ps
, oload_name
, NULL
, 1, VAR_DOMAIN
)
484 || (lookup_partial_symbol (ps
, oload_name
, NULL
, 0, VAR_DOMAIN
)
486 PSYMTAB_TO_SYMTAB (ps
);
489 /* Search upwards from currently selected frame (so that we can
490 complete on local vars. */
492 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
494 if (!BLOCK_SUPERBLOCK (b
))
496 surrounding_static_block
= b
; /* For elimination of dups */
499 /* Also catch fields of types defined in this places which match our
500 text string. Only complete on types visible from current context. */
502 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
504 overload_list_add_symbol (sym
, oload_name
);
508 /* Go through the symtabs and check the externs and statics for
509 symbols which match. */
511 ALL_SYMTABS (objfile
, s
)
514 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
515 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
517 overload_list_add_symbol (sym
, oload_name
);
521 ALL_SYMTABS (objfile
, s
)
524 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
525 /* Don't do this block twice. */
526 if (b
== surrounding_static_block
)
528 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
530 overload_list_add_symbol (sym
, oload_name
);
536 return (sym_return_val
);
540 /* Don't allow just "maintenance cplus". */
543 maint_cplus_command (char *arg
, int from_tty
)
545 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
546 help_list (maint_cplus_cmd_list
, "maintenance cplus ", -1, gdb_stdout
);
549 /* This is a front end for cp_find_first_component, for unit testing.
550 Be careful when using it: see the NOTE above
551 cp_find_first_component. */
554 first_component_command (char *arg
, int from_tty
)
556 int len
= cp_find_first_component (arg
);
557 char *prefix
= alloca (len
+ 1);
559 memcpy (prefix
, arg
, len
);
562 printf_unfiltered ("%s\n", prefix
);
565 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
568 _initialize_cp_support (void)
570 add_prefix_cmd ("cplus", class_maintenance
, maint_cplus_command
,
571 "C++ maintenance commands.", &maint_cplus_cmd_list
,
572 "maintenance cplus ", 0, &maintenancelist
);
573 add_alias_cmd ("cp", "cplus", class_maintenance
, 1, &maintenancelist
);
575 add_cmd ("first_component", class_maintenance
, first_component_command
,
576 "Print the first class/namespace component of NAME.",
577 &maint_cplus_cmd_list
);