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
, char *oload_name
);
57 /* The list of "maint cplus" commands. */
59 struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
61 /* The actual commands. */
63 static void maint_cplus_command (char *arg
, int from_tty
);
64 static void first_component_command (char *arg
, int from_tty
);
66 /* Here are some random pieces of trivia to keep in mind while trying
67 to take apart demangled names:
69 - Names can contain function arguments or templates, so the process
70 has to be, to some extent recursive: maybe keep track of your
71 depth based on encountering <> and ().
73 - Parentheses don't just have to happen at the end of a name: they
74 can occur even if the name in question isn't a function, because
75 a template argument might be a type that's a function.
77 - Conversely, even if you're trying to deal with a function, its
78 demangled name might not end with ')': it could be a const or
79 volatile class method, in which case it ends with "const" or
82 - Parentheses are also used in anonymous namespaces: a variable
83 'foo' in an anonymous namespace gets demangled as "(anonymous
86 - And operator names can contain parentheses or angle brackets. */
88 /* FIXME: carlton/2003-03-13: We have several functions here with
89 overlapping functionality; can we combine them? Also, do they
90 handle all the above considerations correctly? */
92 /* Find the last component of the demangled C++ name NAME. NAME
93 must be a method name including arguments, in order to correctly
94 locate the last component.
96 This function return a pointer to the first colon before the
97 last component, or NULL if the name had only one component. */
100 find_last_component (const char *name
)
105 /* Functions can have local classes, so we need to find the
106 beginning of the last argument list, not the end of the first
108 p
= name
+ strlen (name
) - 1;
109 while (p
> name
&& *p
!= ')')
115 /* P now points at the `)' at the end of the argument list. Walk
116 back to the beginning. */
119 while (p
> name
&& depth
> 0)
121 if (*p
== '<' || *p
== '(')
123 else if (*p
== '>' || *p
== ')')
131 while (p
> name
&& *p
!= ':')
134 if (p
== name
|| p
== name
+ 1 || p
[-1] != ':')
140 /* Return the name of the class containing method PHYSNAME. */
143 class_name_from_physname (const char *physname
)
148 char *demangled_name
= cplus_demangle (physname
, DMGL_ANSI
| DMGL_PARAMS
);
150 if (demangled_name
== NULL
)
153 end
= find_last_component (demangled_name
);
156 ret
= xmalloc (end
- demangled_name
+ 1);
157 memcpy (ret
, demangled_name
, end
- demangled_name
);
158 ret
[end
- demangled_name
] = '\0';
161 xfree (demangled_name
);
165 /* Return the name of the method whose linkage name is PHYSNAME. */
168 method_name_from_physname (const char *physname
)
173 char *demangled_name
= cplus_demangle (physname
, DMGL_ANSI
| DMGL_PARAMS
);
175 if (demangled_name
== NULL
)
178 end
= find_last_component (demangled_name
);
187 /* Find the argument list, if any. */
188 args
= strchr (end
, '(');
190 len
= strlen (end
+ 2);
196 len
= args
- end
+ 1;
198 ret
= xmalloc (len
+ 1);
199 memcpy (ret
, end
, len
);
203 xfree (demangled_name
);
207 /* This returns the length of first component of NAME, which should be
208 the demangled name of a C++ variable/function/method/etc.
209 Specifically, it returns the index of the first colon forming the
210 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
211 it returns the 1, and given 'foo', it returns 0. */
213 /* The character in NAME indexed by the return value is guaranteed to
214 always be either ':' or '\0'. */
216 /* NOTE: carlton/2003-03-13: This function is currently only intended
217 for internal use: it's probably not entirely safe when called on
218 user-generated input, because some of the 'index += 2' lines in
219 cp_find_first_component_aux might go past the end of malformed
223 cp_find_first_component (const char *name
)
225 return cp_find_first_component_aux (name
, 0);
228 /* Helper function for cp_find_first_component. Like that function,
229 it returns the length of the first component of NAME, but to make
230 the recursion easier, it also stops if it reaches an unexpected ')'
231 or '>' if the value of PERMISSIVE is nonzero. */
233 /* Let's optimize away calls to strlen("operator"). */
235 #define LENGTH_OF_OPERATOR 8
238 cp_find_first_component_aux (const char *name
, int permissive
)
240 unsigned int index
= 0;
241 /* Operator names can show up in unexpected places. Since these can
242 contain parentheses or angle brackets, they can screw up the
243 recursion. But not every string 'operator' is part of an
244 operater name: e.g. you could have a variable 'cooperator'. So
245 this variable tells us whether or not we should treat the string
246 'operator' as starting an operator. */
247 int operator_possible
= 1;
254 /* Template; eat it up. The calls to cp_first_component
255 should only return (I hope!) when they reach the '>'
256 terminating the component or a '::' between two
257 components. (Hence the '+ 2'.) */
259 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
261 index
+= cp_find_first_component_aux (name
+ index
, 1))
263 if (name
[index
] != ':')
265 demangled_name_complaint (name
);
266 return strlen (name
);
270 operator_possible
= 1;
273 /* Similar comment as to '<'. */
275 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
277 index
+= cp_find_first_component_aux (name
+ index
, 1))
279 if (name
[index
] != ':')
281 demangled_name_complaint (name
);
282 return strlen (name
);
286 operator_possible
= 1;
294 demangled_name_complaint (name
);
295 return strlen (name
);
301 /* Operator names can screw up the recursion. */
302 if (operator_possible
303 && strncmp (name
+ index
, "operator", LENGTH_OF_OPERATOR
) == 0)
305 index
+= LENGTH_OF_OPERATOR
;
306 while (isspace(name
[index
]))
310 /* Skip over one less than the appropriate number of
311 characters: the for loop will skip over the last
314 if (name
[index
+ 1] == '<')
321 if (name
[index
+ 1] == '>')
334 operator_possible
= 0;
341 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
342 set of relevant characters are here: it's necessary to
343 include any character that can show up before 'operator'
344 in a demangled name, and it's safe to include any
345 character that can't be part of an identifier's name. */
346 operator_possible
= 1;
349 operator_possible
= 0;
355 /* Complain about a demangled name that we don't know how to parse.
356 NAME is the demangled name in question. */
359 demangled_name_complaint (const char *name
)
361 complaint (&symfile_complaints
,
362 "unexpected demangled name '%s'", name
);
365 /* If NAME is the fully-qualified name of a C++
366 function/variable/method/etc., this returns the length of its
367 entire prefix: all of the namespaces and classes that make up its
368 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
369 4, given 'foo', it returns 0. */
372 cp_entire_prefix_len (const char *name
)
374 unsigned int current_len
= cp_find_first_component (name
);
375 unsigned int previous_len
= 0;
377 while (name
[current_len
] != '\0')
379 gdb_assert (name
[current_len
] == ':');
380 previous_len
= current_len
;
383 current_len
+= cp_find_first_component (name
+ current_len
);
389 /* Overload resolution functions. */
392 remove_params (const char *demangled_name
)
398 if (demangled_name
== NULL
)
401 /* First find the end of the arg list. */
402 argp
= strrchr (demangled_name
, ')');
406 /* Back up to the beginning. */
409 while (argp
-- > demangled_name
)
413 else if (*argp
== '(')
422 internal_error (__FILE__
, __LINE__
,
423 "bad demangled name %s\n", demangled_name
);
424 while (argp
[-1] == ' ' && argp
> demangled_name
)
427 new_name
= xmalloc (argp
- demangled_name
+ 1);
428 memcpy (new_name
, demangled_name
, argp
- demangled_name
);
429 new_name
[argp
- demangled_name
] = '\0';
433 /* Test to see if the symbol specified by SYMNAME (which is already
434 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
435 characters. If so, add it to the current completion list. */
438 overload_list_add_symbol (struct symbol
*sym
, char *oload_name
)
444 /* If there is no type information, we can't do anything, so skip */
445 if (SYMBOL_TYPE (sym
) == NULL
)
448 /* skip any symbols that we've already considered. */
449 for (i
= 0; i
< sym_return_val_index
; ++i
)
450 if (!strcmp (DEPRECATED_SYMBOL_NAME (sym
), DEPRECATED_SYMBOL_NAME (sym_return_val
[i
])))
453 /* Get the demangled name without parameters */
454 sym_name
= remove_params (SYMBOL_DEMANGLED_NAME (sym
));
458 /* skip symbols that cannot match */
459 if (strcmp (sym_name
, oload_name
) != 0)
467 /* We have a match for an overload instance, so add SYM to the current list
468 * of overload instances */
469 if (sym_return_val_index
+ 3 > sym_return_val_size
)
471 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
472 sym_return_val
= (struct symbol
**) xrealloc ((char *) sym_return_val
, newsize
);
474 sym_return_val
[sym_return_val_index
++] = sym
;
475 sym_return_val
[sym_return_val_index
] = NULL
;
478 /* Return a null-terminated list of pointers to function symbols that
479 * match name of the supplied symbol FSYM.
480 * This is used in finding all overloaded instances of a function name.
481 * This has been modified from make_symbol_completion_list. */
485 make_symbol_overload_list (struct symbol
*fsym
)
489 struct partial_symtab
*ps
;
490 struct objfile
*objfile
;
491 struct block
*b
, *surrounding_static_block
= 0;
492 struct dict_iterator iter
;
493 /* The name we are completing on. */
494 char *oload_name
= NULL
;
495 /* Length of name. */
496 int oload_name_len
= 0;
498 /* Look for the symbol we are supposed to complete on. */
500 oload_name
= remove_params (SYMBOL_DEMANGLED_NAME (fsym
));
503 sym_return_val_size
= 1;
504 sym_return_val
= (struct symbol
**) xmalloc (2 * sizeof (struct symbol
*));
505 sym_return_val
[0] = fsym
;
506 sym_return_val
[1] = NULL
;
508 return sym_return_val
;
510 oload_name_len
= strlen (oload_name
);
512 sym_return_val_size
= 100;
513 sym_return_val_index
= 0;
514 sym_return_val
= (struct symbol
**) xmalloc ((sym_return_val_size
+ 1) * sizeof (struct symbol
*));
515 sym_return_val
[0] = NULL
;
517 /* Read in all partial symtabs containing a partial symbol named
520 ALL_PSYMTABS (objfile
, ps
)
522 struct partial_symbol
**psym
;
524 /* If the psymtab's been read in we'll get it when we search
525 through the blockvector. */
529 if ((lookup_partial_symbol (ps
, oload_name
, NULL
, 1, VAR_DOMAIN
)
531 || (lookup_partial_symbol (ps
, oload_name
, NULL
, 0, VAR_DOMAIN
)
533 PSYMTAB_TO_SYMTAB (ps
);
536 /* Search upwards from currently selected frame (so that we can
537 complete on local vars. */
539 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
541 if (!BLOCK_SUPERBLOCK (b
))
543 surrounding_static_block
= b
; /* For elimination of dups */
546 /* Also catch fields of types defined in this places which match our
547 text string. Only complete on types visible from current context. */
549 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
551 overload_list_add_symbol (sym
, oload_name
);
555 /* Go through the symtabs and check the externs and statics for
556 symbols which match. */
558 ALL_SYMTABS (objfile
, s
)
561 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
562 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
564 overload_list_add_symbol (sym
, oload_name
);
568 ALL_SYMTABS (objfile
, s
)
571 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
572 /* Don't do this block twice. */
573 if (b
== surrounding_static_block
)
575 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
577 overload_list_add_symbol (sym
, oload_name
);
583 return (sym_return_val
);
586 /* Lookup the rtti type for a class name. */
589 cp_lookup_rtti_type (const char *name
, struct block
*block
)
591 struct symbol
* rtti_sym
;
592 struct type
* rtti_type
;
594 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
, NULL
);
596 if (rtti_sym
== NULL
)
598 warning ("RTTI symbol not found for class '%s'", name
);
602 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
604 warning ("RTTI symbol for class '%s' is not a type", name
);
608 rtti_type
= SYMBOL_TYPE (rtti_sym
);
610 switch (TYPE_CODE (rtti_type
))
612 case TYPE_CODE_CLASS
:
614 case TYPE_CODE_NAMESPACE
:
615 /* chastain/2003-11-26: the symbol tables often contain fake
616 symbols for namespaces with the same name as the struct.
617 This warning is an indication of a bug in the lookup order
618 or a bug in the way that the symbol tables are populated. */
619 warning ("RTTI symbol for class '%s' is a namespace", name
);
622 warning ("RTTI symbol for class '%s' has bad type", name
);
629 /* Don't allow just "maintenance cplus". */
632 maint_cplus_command (char *arg
, int from_tty
)
634 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
635 help_list (maint_cplus_cmd_list
, "maintenance cplus ", -1, gdb_stdout
);
638 /* This is a front end for cp_find_first_component, for unit testing.
639 Be careful when using it: see the NOTE above
640 cp_find_first_component. */
643 first_component_command (char *arg
, int from_tty
)
645 int len
= cp_find_first_component (arg
);
646 char *prefix
= alloca (len
+ 1);
648 memcpy (prefix
, arg
, len
);
651 printf_unfiltered ("%s\n", prefix
);
654 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
657 _initialize_cp_support (void)
659 add_prefix_cmd ("cplus", class_maintenance
, maint_cplus_command
,
660 "C++ maintenance commands.", &maint_cplus_cmd_list
,
661 "maintenance cplus ", 0, &maintenancelist
);
662 add_alias_cmd ("cp", "cplus", class_maintenance
, 1, &maintenancelist
);
664 add_cmd ("first_component", class_maintenance
, first_component_command
,
665 "Print the first class/namespace component of NAME.",
666 &maint_cplus_cmd_list
);