1 /* Helper routines for C++ support in GDB.
2 Copyright 2002, 2003, 2004, 2005 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 #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 char *remove_params (const char *demangled_name
);
56 static void overload_list_add_symbol (struct symbol
*sym
,
57 const char *oload_name
);
59 static void make_symbol_overload_list_using (const char *func_name
,
60 const char *namespace);
62 static void make_symbol_overload_list_qualified (const char *func_name
);
64 static void read_in_psymtabs (const char *oload_name
);
66 /* The list of "maint cplus" commands. */
68 struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
70 /* The actual commands. */
72 static void maint_cplus_command (char *arg
, int from_tty
);
73 static void first_component_command (char *arg
, int from_tty
);
75 /* Return the canonicalized form of STRING, or NULL if STRING can not be
76 parsed. The return value is allocated via xmalloc.
78 drow/2005-03-07: Should we also return NULL for things that trivially do
79 not require any change? e.g. simple identifiers. This could be more
83 cp_canonicalize_string (const char *string
)
86 struct demangle_component
*ret_comp
;
88 int len
= strlen (string
);
92 ret_comp
= cp_demangled_name_to_comp (string
, &storage
, NULL
);
96 ret
= cp_comp_to_string (ret_comp
, len
);
103 /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the
104 block of used memory that should be freed when finished with the tree.
105 DEMANGLED_P is set to the char * that should be freed when finished with
106 the tree, or NULL if none was needed. OPTIONS will be passed to the
109 static struct demangle_component
*
110 mangled_name_to_comp (const char *mangled_name
, int options
,
111 void **memory
, char **demangled_p
)
113 struct demangle_component
*ret
;
114 char *demangled_name
;
117 /* If it looks like a v3 mangled name, then try to go directly
119 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
121 ret
= cplus_demangle_v3_components (mangled_name
, options
, memory
);
129 /* If it doesn't, or if that failed, then try to demangle the name. */
130 demangled_name
= cplus_demangle (mangled_name
, options
);
131 if (demangled_name
== NULL
)
134 /* If we could demangle the name, parse it to build the component tree. */
135 ret
= cp_demangled_name_to_comp (demangled_name
, memory
, NULL
);
139 free (demangled_name
);
143 *demangled_p
= demangled_name
;
147 /* Return the name of the class containing method PHYSNAME. */
150 cp_class_name_from_physname (const char *physname
)
153 char *demangled_name
= NULL
, *ret
;
154 struct demangle_component
*ret_comp
, *prev_comp
;
157 ret_comp
= mangled_name_to_comp (physname
, DMGL_ANSI
, &storage
,
159 if (ret_comp
== NULL
)
165 switch (ret_comp
->type
)
167 case DEMANGLE_COMPONENT_TYPED_NAME
:
169 ret_comp
= d_right (ret_comp
);
171 case DEMANGLE_COMPONENT_QUAL_NAME
:
172 case DEMANGLE_COMPONENT_LOCAL_NAME
:
173 prev_comp
= ret_comp
;
174 ret_comp
= d_right (ret_comp
);
176 case DEMANGLE_COMPONENT_CONST
:
177 case DEMANGLE_COMPONENT_RESTRICT
:
178 case DEMANGLE_COMPONENT_VOLATILE
:
179 case DEMANGLE_COMPONENT_CONST_THIS
:
180 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
181 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
182 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
184 ret_comp
= d_left (ret_comp
);
186 case DEMANGLE_COMPONENT_NAME
:
187 case DEMANGLE_COMPONENT_TEMPLATE
:
188 case DEMANGLE_COMPONENT_CTOR
:
189 case DEMANGLE_COMPONENT_DTOR
:
190 case DEMANGLE_COMPONENT_OPERATOR
:
191 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
202 if (prev_comp
!= NULL
)
204 *prev_comp
= *d_left (prev_comp
);
205 /* The ten is completely arbitrary; we don't have a good estimate. */
206 ret
= cp_comp_to_string (prev_comp
, 10);
211 xfree (demangled_name
);
215 /* Return the name of the method whose linkage name is PHYSNAME. */
218 method_name_from_physname (const char *physname
)
221 char *demangled_name
= NULL
, *ret
;
222 struct demangle_component
*ret_comp
;
225 ret_comp
= mangled_name_to_comp (physname
, DMGL_ANSI
, &storage
,
227 if (ret_comp
== NULL
)
232 switch (ret_comp
->type
)
234 case DEMANGLE_COMPONENT_QUAL_NAME
:
235 case DEMANGLE_COMPONENT_LOCAL_NAME
:
236 case DEMANGLE_COMPONENT_TYPED_NAME
:
237 ret_comp
= d_right (ret_comp
);
239 case DEMANGLE_COMPONENT_CONST
:
240 case DEMANGLE_COMPONENT_RESTRICT
:
241 case DEMANGLE_COMPONENT_VOLATILE
:
242 case DEMANGLE_COMPONENT_CONST_THIS
:
243 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
244 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
245 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
246 ret_comp
= d_left (ret_comp
);
248 case DEMANGLE_COMPONENT_NAME
:
249 case DEMANGLE_COMPONENT_TEMPLATE
:
250 case DEMANGLE_COMPONENT_CTOR
:
251 case DEMANGLE_COMPONENT_DTOR
:
252 case DEMANGLE_COMPONENT_OPERATOR
:
253 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
263 if (ret_comp
!= NULL
)
264 /* The ten is completely arbitrary; we don't have a good estimate. */
265 ret
= cp_comp_to_string (ret_comp
, 10);
269 xfree (demangled_name
);
273 /* Here are some random pieces of trivia to keep in mind while trying
274 to take apart demangled names:
276 - Names can contain function arguments or templates, so the process
277 has to be, to some extent recursive: maybe keep track of your
278 depth based on encountering <> and ().
280 - Parentheses don't just have to happen at the end of a name: they
281 can occur even if the name in question isn't a function, because
282 a template argument might be a type that's a function.
284 - Conversely, even if you're trying to deal with a function, its
285 demangled name might not end with ')': it could be a const or
286 volatile class method, in which case it ends with "const" or
289 - Parentheses are also used in anonymous namespaces: a variable
290 'foo' in an anonymous namespace gets demangled as "(anonymous
293 - And operator names can contain parentheses or angle brackets. */
295 /* FIXME: carlton/2003-03-13: We have several functions here with
296 overlapping functionality; can we combine them? Also, do they
297 handle all the above considerations correctly? */
300 /* This returns the length of first component of NAME, which should be
301 the demangled name of a C++ variable/function/method/etc.
302 Specifically, it returns the index of the first colon forming the
303 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
304 it returns the 1, and given 'foo', it returns 0. */
306 /* The character in NAME indexed by the return value is guaranteed to
307 always be either ':' or '\0'. */
309 /* NOTE: carlton/2003-03-13: This function is currently only intended
310 for internal use: it's probably not entirely safe when called on
311 user-generated input, because some of the 'index += 2' lines in
312 cp_find_first_component_aux might go past the end of malformed
316 cp_find_first_component (const char *name
)
318 return cp_find_first_component_aux (name
, 0);
321 /* Helper function for cp_find_first_component. Like that function,
322 it returns the length of the first component of NAME, but to make
323 the recursion easier, it also stops if it reaches an unexpected ')'
324 or '>' if the value of PERMISSIVE is nonzero. */
326 /* Let's optimize away calls to strlen("operator"). */
328 #define LENGTH_OF_OPERATOR 8
331 cp_find_first_component_aux (const char *name
, int permissive
)
333 unsigned int index
= 0;
334 /* Operator names can show up in unexpected places. Since these can
335 contain parentheses or angle brackets, they can screw up the
336 recursion. But not every string 'operator' is part of an
337 operater name: e.g. you could have a variable 'cooperator'. So
338 this variable tells us whether or not we should treat the string
339 'operator' as starting an operator. */
340 int operator_possible
= 1;
347 /* Template; eat it up. The calls to cp_first_component
348 should only return (I hope!) when they reach the '>'
349 terminating the component or a '::' between two
350 components. (Hence the '+ 2'.) */
352 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
354 index
+= cp_find_first_component_aux (name
+ index
, 1))
356 if (name
[index
] != ':')
358 demangled_name_complaint (name
);
359 return strlen (name
);
363 operator_possible
= 1;
366 /* Similar comment as to '<'. */
368 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
370 index
+= cp_find_first_component_aux (name
+ index
, 1))
372 if (name
[index
] != ':')
374 demangled_name_complaint (name
);
375 return strlen (name
);
379 operator_possible
= 1;
387 demangled_name_complaint (name
);
388 return strlen (name
);
394 /* Operator names can screw up the recursion. */
395 if (operator_possible
396 && strncmp (name
+ index
, "operator", LENGTH_OF_OPERATOR
) == 0)
398 index
+= LENGTH_OF_OPERATOR
;
399 while (isspace(name
[index
]))
403 /* Skip over one less than the appropriate number of
404 characters: the for loop will skip over the last
407 if (name
[index
+ 1] == '<')
414 if (name
[index
+ 1] == '>')
427 operator_possible
= 0;
434 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
435 set of relevant characters are here: it's necessary to
436 include any character that can show up before 'operator'
437 in a demangled name, and it's safe to include any
438 character that can't be part of an identifier's name. */
439 operator_possible
= 1;
442 operator_possible
= 0;
448 /* Complain about a demangled name that we don't know how to parse.
449 NAME is the demangled name in question. */
452 demangled_name_complaint (const char *name
)
454 complaint (&symfile_complaints
,
455 "unexpected demangled name '%s'", name
);
458 /* If NAME is the fully-qualified name of a C++
459 function/variable/method/etc., this returns the length of its
460 entire prefix: all of the namespaces and classes that make up its
461 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
462 4, given 'foo', it returns 0. */
465 cp_entire_prefix_len (const char *name
)
467 unsigned int current_len
= cp_find_first_component (name
);
468 unsigned int previous_len
= 0;
470 while (name
[current_len
] != '\0')
472 gdb_assert (name
[current_len
] == ':');
473 previous_len
= current_len
;
476 current_len
+= cp_find_first_component (name
+ current_len
);
482 /* If FULL_NAME is the demangled name of a C++ function (including an
483 arg list, possibly including namespace/class qualifications),
484 return a new string containing only the function name (without the
485 arg list/class qualifications). Otherwise, return NULL. The
486 caller is responsible for freeing the memory in question. */
489 cp_func_name (const char *full_name
)
491 const char *previous_component
= full_name
;
492 const char *next_component
;
497 for (next_component
= (previous_component
498 + cp_find_first_component (previous_component
));
499 *next_component
== ':';
500 next_component
= (previous_component
501 + cp_find_first_component (previous_component
)))
504 previous_component
= next_component
+ 2;
507 return remove_params (previous_component
);
510 /* Overload resolution functions. */
513 remove_params (const char *demangled_name
)
519 if (demangled_name
== NULL
)
522 /* First find the end of the arg list. */
523 argp
= strrchr (demangled_name
, ')');
527 /* Back up to the beginning. */
530 while (argp
-- > demangled_name
)
534 else if (*argp
== '(')
543 internal_error (__FILE__
, __LINE__
,
544 _("bad demangled name %s\n"), demangled_name
);
545 while (argp
[-1] == ' ' && argp
> demangled_name
)
548 new_name
= xmalloc (argp
- demangled_name
+ 1);
549 memcpy (new_name
, demangled_name
, argp
- demangled_name
);
550 new_name
[argp
- demangled_name
] = '\0';
554 /* Test to see if SYM is a symbol that we haven't seen corresponding
555 to a function named OLOAD_NAME. If so, add it to the current
559 overload_list_add_symbol (struct symbol
*sym
, const char *oload_name
)
565 /* If there is no type information, we can't do anything, so skip */
566 if (SYMBOL_TYPE (sym
) == NULL
)
569 /* skip any symbols that we've already considered. */
570 for (i
= 0; i
< sym_return_val_index
; ++i
)
571 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
572 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
575 /* Get the demangled name without parameters */
576 sym_name
= remove_params (SYMBOL_NATURAL_NAME (sym
));
580 /* skip symbols that cannot match */
581 if (strcmp (sym_name
, oload_name
) != 0)
589 /* We have a match for an overload instance, so add SYM to the current list
590 * of overload instances */
591 if (sym_return_val_index
+ 3 > sym_return_val_size
)
593 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
594 sym_return_val
= (struct symbol
**) xrealloc ((char *) sym_return_val
, newsize
);
596 sym_return_val
[sym_return_val_index
++] = sym
;
597 sym_return_val
[sym_return_val_index
] = NULL
;
600 /* Return a null-terminated list of pointers to function symbols that
601 are named FUNC_NAME and are visible within NAMESPACE. */
604 make_symbol_overload_list (const char *func_name
,
605 const char *namespace)
607 struct cleanup
*old_cleanups
;
609 sym_return_val_size
= 100;
610 sym_return_val_index
= 0;
611 sym_return_val
= xmalloc ((sym_return_val_size
+ 1) *
612 sizeof (struct symbol
*));
613 sym_return_val
[0] = NULL
;
615 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
617 make_symbol_overload_list_using (func_name
, namespace);
619 discard_cleanups (old_cleanups
);
621 return sym_return_val
;
624 /* This applies the using directives to add namespaces to search in,
625 and then searches for overloads in all of those namespaces. It
626 adds the symbols found to sym_return_val. Arguments are as in
627 make_symbol_overload_list. */
630 make_symbol_overload_list_using (const char *func_name
,
631 const char *namespace)
633 const struct using_direct
*current
;
635 /* First, go through the using directives. If any of them apply,
636 look in the appropriate namespaces for new functions to match
639 for (current
= block_using (get_selected_block (0));
641 current
= current
->next
)
643 if (strcmp (namespace, current
->outer
) == 0)
645 make_symbol_overload_list_using (func_name
,
650 /* Now, add names for this namespace. */
652 if (namespace[0] == '\0')
654 make_symbol_overload_list_qualified (func_name
);
658 char *concatenated_name
659 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
660 strcpy (concatenated_name
, namespace);
661 strcat (concatenated_name
, "::");
662 strcat (concatenated_name
, func_name
);
663 make_symbol_overload_list_qualified (concatenated_name
);
667 /* This does the bulk of the work of finding overloaded symbols.
668 FUNC_NAME is the name of the overloaded function we're looking for
669 (possibly including namespace info). */
672 make_symbol_overload_list_qualified (const char *func_name
)
676 struct objfile
*objfile
;
677 const struct block
*b
, *surrounding_static_block
= 0;
678 struct dict_iterator iter
;
679 const struct dictionary
*dict
;
681 /* Look through the partial symtabs for all symbols which begin
682 by matching FUNC_NAME. Make sure we read that symbol table in. */
684 read_in_psymtabs (func_name
);
686 /* Search upwards from currently selected frame (so that we can
687 complete on local vars. */
689 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
691 dict
= BLOCK_DICT (b
);
693 for (sym
= dict_iter_name_first (dict
, func_name
, &iter
);
695 sym
= dict_iter_name_next (func_name
, &iter
))
697 overload_list_add_symbol (sym
, func_name
);
701 surrounding_static_block
= block_static_block (get_selected_block (0));
703 /* Go through the symtabs and check the externs and statics for
704 symbols which match. */
706 ALL_SYMTABS (objfile
, s
)
709 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
710 dict
= BLOCK_DICT (b
);
712 for (sym
= dict_iter_name_first (dict
, func_name
, &iter
);
714 sym
= dict_iter_name_next (func_name
, &iter
))
716 overload_list_add_symbol (sym
, func_name
);
720 ALL_SYMTABS (objfile
, s
)
723 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
724 /* Don't do this block twice. */
725 if (b
== surrounding_static_block
)
727 dict
= BLOCK_DICT (b
);
729 for (sym
= dict_iter_name_first (dict
, func_name
, &iter
);
731 sym
= dict_iter_name_next (func_name
, &iter
))
733 overload_list_add_symbol (sym
, func_name
);
738 /* Look through the partial symtabs for all symbols which begin
739 by matching FUNC_NAME. Make sure we read that symbol table in. */
742 read_in_psymtabs (const char *func_name
)
744 struct partial_symtab
*ps
;
745 struct objfile
*objfile
;
747 ALL_PSYMTABS (objfile
, ps
)
752 if ((lookup_partial_symbol (ps
, func_name
, NULL
, 1, VAR_DOMAIN
)
754 || (lookup_partial_symbol (ps
, func_name
, NULL
, 0, VAR_DOMAIN
)
756 psymtab_to_symtab (ps
);
760 /* Lookup the rtti type for a class name. */
763 cp_lookup_rtti_type (const char *name
, struct block
*block
)
765 struct symbol
* rtti_sym
;
766 struct type
* rtti_type
;
768 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
, NULL
);
770 if (rtti_sym
== NULL
)
772 warning (_("RTTI symbol not found for class '%s'"), name
);
776 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
778 warning (_("RTTI symbol for class '%s' is not a type"), name
);
782 rtti_type
= SYMBOL_TYPE (rtti_sym
);
784 switch (TYPE_CODE (rtti_type
))
786 case TYPE_CODE_CLASS
:
788 case TYPE_CODE_NAMESPACE
:
789 /* chastain/2003-11-26: the symbol tables often contain fake
790 symbols for namespaces with the same name as the struct.
791 This warning is an indication of a bug in the lookup order
792 or a bug in the way that the symbol tables are populated. */
793 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
796 warning (_("RTTI symbol for class '%s' has bad type"), name
);
803 /* Don't allow just "maintenance cplus". */
806 maint_cplus_command (char *arg
, int from_tty
)
808 printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n"));
809 help_list (maint_cplus_cmd_list
, "maintenance cplus ", -1, gdb_stdout
);
812 /* This is a front end for cp_find_first_component, for unit testing.
813 Be careful when using it: see the NOTE above
814 cp_find_first_component. */
817 first_component_command (char *arg
, int from_tty
)
819 int len
= cp_find_first_component (arg
);
820 char *prefix
= alloca (len
+ 1);
822 memcpy (prefix
, arg
, len
);
825 printf_unfiltered ("%s\n", prefix
);
828 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
831 _initialize_cp_support (void)
833 add_prefix_cmd ("cplus", class_maintenance
, maint_cplus_command
,
834 _("C++ maintenance commands."), &maint_cplus_cmd_list
,
835 "maintenance cplus ", 0, &maintenancelist
);
836 add_alias_cmd ("cp", "cplus", class_maintenance
, 1, &maintenancelist
);
838 add_cmd ("first_component", class_maintenance
, first_component_command
,
839 _("Print the first class/namespace component of NAME."),
840 &maint_cplus_cmd_list
);