1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "cp-support.h"
23 #include "gdb_string.h"
25 #include "gdb_assert.h"
27 #include "dictionary.h"
32 #include "complaints.h"
35 #include "safe-ctype.h"
37 #define d_left(dc) (dc)->u.s_binary.left
38 #define d_right(dc) (dc)->u.s_binary.right
40 /* Functions related to demangled name parsing. */
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 void overload_list_add_symbol (struct symbol
*sym
,
54 const char *oload_name
);
56 static void make_symbol_overload_list_using (const char *func_name
,
57 const char *namespace);
59 static void make_symbol_overload_list_qualified (const char *func_name
);
61 static void read_in_psymtabs (const char *oload_name
);
63 /* The list of "maint cplus" commands. */
65 struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
67 /* The actual commands. */
69 static void maint_cplus_command (char *arg
, int from_tty
);
70 static void first_component_command (char *arg
, int from_tty
);
72 /* Return 1 if STRING is clearly already in canonical form. This
73 function is conservative; things which it does not recognize are
74 assumed to be non-canonical, and the parser will sort them out
75 afterwards. This speeds up the critical path for alphanumeric
79 cp_already_canonical (const char *string
)
81 /* Identifier start character [a-zA-Z_]. */
82 if (!ISIDST (string
[0]))
85 /* These are the only two identifiers which canonicalize to other
86 than themselves or an error: unsigned -> unsigned int and
88 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
90 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
93 /* Identifier character [a-zA-Z0-9_]. */
94 while (ISIDNUM (string
[1]))
97 if (string
[1] == '\0')
103 /* Parse STRING and convert it to canonical form. If parsing fails,
104 or if STRING is already canonical, return NULL. Otherwise return
105 the canonical form. The return value is allocated via xmalloc. */
108 cp_canonicalize_string (const char *string
)
110 struct demangle_component
*ret_comp
;
111 unsigned int estimated_len
;
114 if (cp_already_canonical (string
))
117 ret_comp
= cp_demangled_name_to_comp (string
, NULL
);
118 if (ret_comp
== NULL
)
121 estimated_len
= strlen (string
) * 2;
122 ret
= cp_comp_to_string (ret_comp
, estimated_len
);
124 if (strcmp (string
, ret
) == 0)
133 /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the
134 block of used memory that should be freed when finished with the tree.
135 DEMANGLED_P is set to the char * that should be freed when finished with
136 the tree, or NULL if none was needed. OPTIONS will be passed to the
139 static struct demangle_component
*
140 mangled_name_to_comp (const char *mangled_name
, int options
,
141 void **memory
, char **demangled_p
)
143 struct demangle_component
*ret
;
144 char *demangled_name
;
147 /* If it looks like a v3 mangled name, then try to go directly
149 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
151 ret
= cplus_demangle_v3_components (mangled_name
, options
, memory
);
159 /* If it doesn't, or if that failed, then try to demangle the name. */
160 demangled_name
= cplus_demangle (mangled_name
, options
);
161 if (demangled_name
== NULL
)
164 /* If we could demangle the name, parse it to build the component tree. */
165 ret
= cp_demangled_name_to_comp (demangled_name
, NULL
);
169 free (demangled_name
);
173 *demangled_p
= demangled_name
;
177 /* Return the name of the class containing method PHYSNAME. */
180 cp_class_name_from_physname (const char *physname
)
183 char *demangled_name
= NULL
, *ret
;
184 struct demangle_component
*ret_comp
, *prev_comp
, *cur_comp
;
187 ret_comp
= mangled_name_to_comp (physname
, DMGL_ANSI
, &storage
,
189 if (ret_comp
== NULL
)
194 /* First strip off any qualifiers, if we have a function or method. */
196 switch (ret_comp
->type
)
198 case DEMANGLE_COMPONENT_CONST
:
199 case DEMANGLE_COMPONENT_RESTRICT
:
200 case DEMANGLE_COMPONENT_VOLATILE
:
201 case DEMANGLE_COMPONENT_CONST_THIS
:
202 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
203 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
204 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
205 ret_comp
= d_left (ret_comp
);
212 /* If what we have now is a function, discard the argument list. */
213 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
214 ret_comp
= d_left (ret_comp
);
216 /* If what we have now is a template, strip off the template
217 arguments. The left subtree may be a qualified name. */
218 if (ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
219 ret_comp
= d_left (ret_comp
);
221 /* What we have now should be a name, possibly qualified. Additional
222 qualifiers could live in the left subtree or the right subtree. Find
228 switch (cur_comp
->type
)
230 case DEMANGLE_COMPONENT_QUAL_NAME
:
231 case DEMANGLE_COMPONENT_LOCAL_NAME
:
232 prev_comp
= cur_comp
;
233 cur_comp
= d_right (cur_comp
);
235 case DEMANGLE_COMPONENT_TEMPLATE
:
236 case DEMANGLE_COMPONENT_NAME
:
237 case DEMANGLE_COMPONENT_CTOR
:
238 case DEMANGLE_COMPONENT_DTOR
:
239 case DEMANGLE_COMPONENT_OPERATOR
:
240 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
250 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
252 /* We want to discard the rightmost child of PREV_COMP. */
253 *prev_comp
= *d_left (prev_comp
);
254 /* The ten is completely arbitrary; we don't have a good estimate. */
255 ret
= cp_comp_to_string (ret_comp
, 10);
260 xfree (demangled_name
);
264 /* Return the child of COMP which is the basename of a method, variable,
265 et cetera. All scope qualifiers are discarded, but template arguments
266 will be included. The component tree may be modified. */
268 static struct demangle_component
*
269 unqualified_name_from_comp (struct demangle_component
*comp
)
271 struct demangle_component
*ret_comp
= comp
, *last_template
;
275 last_template
= NULL
;
277 switch (ret_comp
->type
)
279 case DEMANGLE_COMPONENT_QUAL_NAME
:
280 case DEMANGLE_COMPONENT_LOCAL_NAME
:
281 ret_comp
= d_right (ret_comp
);
283 case DEMANGLE_COMPONENT_TYPED_NAME
:
284 ret_comp
= d_left (ret_comp
);
286 case DEMANGLE_COMPONENT_TEMPLATE
:
287 gdb_assert (last_template
== NULL
);
288 last_template
= ret_comp
;
289 ret_comp
= d_left (ret_comp
);
291 case DEMANGLE_COMPONENT_CONST
:
292 case DEMANGLE_COMPONENT_RESTRICT
:
293 case DEMANGLE_COMPONENT_VOLATILE
:
294 case DEMANGLE_COMPONENT_CONST_THIS
:
295 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
296 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
297 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
298 ret_comp
= d_left (ret_comp
);
300 case DEMANGLE_COMPONENT_NAME
:
301 case DEMANGLE_COMPONENT_CTOR
:
302 case DEMANGLE_COMPONENT_DTOR
:
303 case DEMANGLE_COMPONENT_OPERATOR
:
304 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
314 d_left (last_template
) = ret_comp
;
315 return last_template
;
321 /* Return the name of the method whose linkage name is PHYSNAME. */
324 method_name_from_physname (const char *physname
)
327 char *demangled_name
= NULL
, *ret
;
328 struct demangle_component
*ret_comp
;
331 ret_comp
= mangled_name_to_comp (physname
, DMGL_ANSI
, &storage
,
333 if (ret_comp
== NULL
)
336 ret_comp
= unqualified_name_from_comp (ret_comp
);
339 if (ret_comp
!= NULL
)
340 /* The ten is completely arbitrary; we don't have a good estimate. */
341 ret
= cp_comp_to_string (ret_comp
, 10);
345 xfree (demangled_name
);
349 /* If FULL_NAME is the demangled name of a C++ function (including an
350 arg list, possibly including namespace/class qualifications),
351 return a new string containing only the function name (without the
352 arg list/class qualifications). Otherwise, return NULL. The
353 caller is responsible for freeing the memory in question. */
356 cp_func_name (const char *full_name
)
359 struct demangle_component
*ret_comp
;
362 ret_comp
= cp_demangled_name_to_comp (full_name
, NULL
);
366 ret_comp
= unqualified_name_from_comp (ret_comp
);
369 if (ret_comp
!= NULL
)
370 ret
= cp_comp_to_string (ret_comp
, 10);
375 /* DEMANGLED_NAME is the name of a function, including parameters and
376 (optionally) a return type. Return the name of the function without
377 parameters or return type, or NULL if we can not parse the name. */
380 remove_params (const char *demangled_name
)
383 struct demangle_component
*ret_comp
;
386 if (demangled_name
== NULL
)
389 ret_comp
= cp_demangled_name_to_comp (demangled_name
, NULL
);
390 if (ret_comp
== NULL
)
393 /* First strip off any qualifiers, if we have a function or method. */
395 switch (ret_comp
->type
)
397 case DEMANGLE_COMPONENT_CONST
:
398 case DEMANGLE_COMPONENT_RESTRICT
:
399 case DEMANGLE_COMPONENT_VOLATILE
:
400 case DEMANGLE_COMPONENT_CONST_THIS
:
401 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
402 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
403 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
404 ret_comp
= d_left (ret_comp
);
411 /* What we have now should be a function. Return its name. */
412 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
413 ret
= cp_comp_to_string (d_left (ret_comp
), 10);
418 /* Here are some random pieces of trivia to keep in mind while trying
419 to take apart demangled names:
421 - Names can contain function arguments or templates, so the process
422 has to be, to some extent recursive: maybe keep track of your
423 depth based on encountering <> and ().
425 - Parentheses don't just have to happen at the end of a name: they
426 can occur even if the name in question isn't a function, because
427 a template argument might be a type that's a function.
429 - Conversely, even if you're trying to deal with a function, its
430 demangled name might not end with ')': it could be a const or
431 volatile class method, in which case it ends with "const" or
434 - Parentheses are also used in anonymous namespaces: a variable
435 'foo' in an anonymous namespace gets demangled as "(anonymous
438 - And operator names can contain parentheses or angle brackets. */
440 /* FIXME: carlton/2003-03-13: We have several functions here with
441 overlapping functionality; can we combine them? Also, do they
442 handle all the above considerations correctly? */
445 /* This returns the length of first component of NAME, which should be
446 the demangled name of a C++ variable/function/method/etc.
447 Specifically, it returns the index of the first colon forming the
448 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
449 it returns the 1, and given 'foo', it returns 0. */
451 /* The character in NAME indexed by the return value is guaranteed to
452 always be either ':' or '\0'. */
454 /* NOTE: carlton/2003-03-13: This function is currently only intended
455 for internal use: it's probably not entirely safe when called on
456 user-generated input, because some of the 'index += 2' lines in
457 cp_find_first_component_aux might go past the end of malformed
461 cp_find_first_component (const char *name
)
463 return cp_find_first_component_aux (name
, 0);
466 /* Helper function for cp_find_first_component. Like that function,
467 it returns the length of the first component of NAME, but to make
468 the recursion easier, it also stops if it reaches an unexpected ')'
469 or '>' if the value of PERMISSIVE is nonzero. */
471 /* Let's optimize away calls to strlen("operator"). */
473 #define LENGTH_OF_OPERATOR 8
476 cp_find_first_component_aux (const char *name
, int permissive
)
478 unsigned int index
= 0;
479 /* Operator names can show up in unexpected places. Since these can
480 contain parentheses or angle brackets, they can screw up the
481 recursion. But not every string 'operator' is part of an
482 operater name: e.g. you could have a variable 'cooperator'. So
483 this variable tells us whether or not we should treat the string
484 'operator' as starting an operator. */
485 int operator_possible
= 1;
492 /* Template; eat it up. The calls to cp_first_component
493 should only return (I hope!) when they reach the '>'
494 terminating the component or a '::' between two
495 components. (Hence the '+ 2'.) */
497 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
499 index
+= cp_find_first_component_aux (name
+ index
, 1))
501 if (name
[index
] != ':')
503 demangled_name_complaint (name
);
504 return strlen (name
);
508 operator_possible
= 1;
511 /* Similar comment as to '<'. */
513 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
515 index
+= cp_find_first_component_aux (name
+ index
, 1))
517 if (name
[index
] != ':')
519 demangled_name_complaint (name
);
520 return strlen (name
);
524 operator_possible
= 1;
532 demangled_name_complaint (name
);
533 return strlen (name
);
539 /* Operator names can screw up the recursion. */
540 if (operator_possible
541 && strncmp (name
+ index
, "operator", LENGTH_OF_OPERATOR
) == 0)
543 index
+= LENGTH_OF_OPERATOR
;
544 while (ISSPACE(name
[index
]))
548 /* Skip over one less than the appropriate number of
549 characters: the for loop will skip over the last
552 if (name
[index
+ 1] == '<')
559 if (name
[index
+ 1] == '>')
572 operator_possible
= 0;
579 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
580 set of relevant characters are here: it's necessary to
581 include any character that can show up before 'operator'
582 in a demangled name, and it's safe to include any
583 character that can't be part of an identifier's name. */
584 operator_possible
= 1;
587 operator_possible
= 0;
593 /* Complain about a demangled name that we don't know how to parse.
594 NAME is the demangled name in question. */
597 demangled_name_complaint (const char *name
)
599 complaint (&symfile_complaints
,
600 "unexpected demangled name '%s'", name
);
603 /* If NAME is the fully-qualified name of a C++
604 function/variable/method/etc., this returns the length of its
605 entire prefix: all of the namespaces and classes that make up its
606 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
607 4, given 'foo', it returns 0. */
610 cp_entire_prefix_len (const char *name
)
612 unsigned int current_len
= cp_find_first_component (name
);
613 unsigned int previous_len
= 0;
615 while (name
[current_len
] != '\0')
617 gdb_assert (name
[current_len
] == ':');
618 previous_len
= current_len
;
621 current_len
+= cp_find_first_component (name
+ current_len
);
627 /* Overload resolution functions. */
629 /* Test to see if SYM is a symbol that we haven't seen corresponding
630 to a function named OLOAD_NAME. If so, add it to the current
634 overload_list_add_symbol (struct symbol
*sym
, const char *oload_name
)
640 /* If there is no type information, we can't do anything, so skip */
641 if (SYMBOL_TYPE (sym
) == NULL
)
644 /* skip any symbols that we've already considered. */
645 for (i
= 0; i
< sym_return_val_index
; ++i
)
646 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
647 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
650 /* Get the demangled name without parameters */
651 sym_name
= remove_params (SYMBOL_NATURAL_NAME (sym
));
655 /* skip symbols that cannot match */
656 if (strcmp (sym_name
, oload_name
) != 0)
664 /* We have a match for an overload instance, so add SYM to the current list
665 * of overload instances */
666 if (sym_return_val_index
+ 3 > sym_return_val_size
)
668 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
669 sym_return_val
= (struct symbol
**) xrealloc ((char *) sym_return_val
, newsize
);
671 sym_return_val
[sym_return_val_index
++] = sym
;
672 sym_return_val
[sym_return_val_index
] = NULL
;
675 /* Return a null-terminated list of pointers to function symbols that
676 are named FUNC_NAME and are visible within NAMESPACE. */
679 make_symbol_overload_list (const char *func_name
,
680 const char *namespace)
682 struct cleanup
*old_cleanups
;
684 sym_return_val_size
= 100;
685 sym_return_val_index
= 0;
686 sym_return_val
= xmalloc ((sym_return_val_size
+ 1) *
687 sizeof (struct symbol
*));
688 sym_return_val
[0] = NULL
;
690 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
692 make_symbol_overload_list_using (func_name
, namespace);
694 discard_cleanups (old_cleanups
);
696 return sym_return_val
;
699 /* This applies the using directives to add namespaces to search in,
700 and then searches for overloads in all of those namespaces. It
701 adds the symbols found to sym_return_val. Arguments are as in
702 make_symbol_overload_list. */
705 make_symbol_overload_list_using (const char *func_name
,
706 const char *namespace)
708 const struct using_direct
*current
;
710 /* First, go through the using directives. If any of them apply,
711 look in the appropriate namespaces for new functions to match
714 for (current
= block_using (get_selected_block (0));
716 current
= current
->next
)
718 if (strcmp (namespace, current
->outer
) == 0)
720 make_symbol_overload_list_using (func_name
,
725 /* Now, add names for this namespace. */
727 if (namespace[0] == '\0')
729 make_symbol_overload_list_qualified (func_name
);
733 char *concatenated_name
734 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
735 strcpy (concatenated_name
, namespace);
736 strcat (concatenated_name
, "::");
737 strcat (concatenated_name
, func_name
);
738 make_symbol_overload_list_qualified (concatenated_name
);
742 /* This does the bulk of the work of finding overloaded symbols.
743 FUNC_NAME is the name of the overloaded function we're looking for
744 (possibly including namespace info). */
747 make_symbol_overload_list_qualified (const char *func_name
)
751 struct objfile
*objfile
;
752 const struct block
*b
, *surrounding_static_block
= 0;
753 struct dict_iterator iter
;
754 const struct dictionary
*dict
;
756 /* Look through the partial symtabs for all symbols which begin
757 by matching FUNC_NAME. Make sure we read that symbol table in. */
759 read_in_psymtabs (func_name
);
761 /* Search upwards from currently selected frame (so that we can
762 complete on local vars. */
764 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
766 dict
= BLOCK_DICT (b
);
768 for (sym
= dict_iter_name_first (dict
, func_name
, &iter
);
770 sym
= dict_iter_name_next (func_name
, &iter
))
772 overload_list_add_symbol (sym
, func_name
);
776 surrounding_static_block
= block_static_block (get_selected_block (0));
778 /* Go through the symtabs and check the externs and statics for
779 symbols which match. */
781 ALL_PRIMARY_SYMTABS (objfile
, s
)
784 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
785 dict
= BLOCK_DICT (b
);
787 for (sym
= dict_iter_name_first (dict
, func_name
, &iter
);
789 sym
= dict_iter_name_next (func_name
, &iter
))
791 overload_list_add_symbol (sym
, func_name
);
795 ALL_PRIMARY_SYMTABS (objfile
, s
)
798 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
799 /* Don't do this block twice. */
800 if (b
== surrounding_static_block
)
802 dict
= BLOCK_DICT (b
);
804 for (sym
= dict_iter_name_first (dict
, func_name
, &iter
);
806 sym
= dict_iter_name_next (func_name
, &iter
))
808 overload_list_add_symbol (sym
, func_name
);
813 /* Look through the partial symtabs for all symbols which begin
814 by matching FUNC_NAME. Make sure we read that symbol table in. */
817 read_in_psymtabs (const char *func_name
)
819 struct partial_symtab
*ps
;
820 struct objfile
*objfile
;
822 ALL_PSYMTABS (objfile
, ps
)
827 if ((lookup_partial_symbol (ps
, func_name
, NULL
, 1, VAR_DOMAIN
)
829 || (lookup_partial_symbol (ps
, func_name
, NULL
, 0, VAR_DOMAIN
)
831 psymtab_to_symtab (ps
);
835 /* Lookup the rtti type for a class name. */
838 cp_lookup_rtti_type (const char *name
, struct block
*block
)
840 struct symbol
* rtti_sym
;
841 struct type
* rtti_type
;
843 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
, NULL
);
845 if (rtti_sym
== NULL
)
847 warning (_("RTTI symbol not found for class '%s'"), name
);
851 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
853 warning (_("RTTI symbol for class '%s' is not a type"), name
);
857 rtti_type
= SYMBOL_TYPE (rtti_sym
);
859 switch (TYPE_CODE (rtti_type
))
861 case TYPE_CODE_CLASS
:
863 case TYPE_CODE_NAMESPACE
:
864 /* chastain/2003-11-26: the symbol tables often contain fake
865 symbols for namespaces with the same name as the struct.
866 This warning is an indication of a bug in the lookup order
867 or a bug in the way that the symbol tables are populated. */
868 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
871 warning (_("RTTI symbol for class '%s' has bad type"), name
);
878 /* Don't allow just "maintenance cplus". */
881 maint_cplus_command (char *arg
, int from_tty
)
883 printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n"));
884 help_list (maint_cplus_cmd_list
, "maintenance cplus ", -1, gdb_stdout
);
887 /* This is a front end for cp_find_first_component, for unit testing.
888 Be careful when using it: see the NOTE above
889 cp_find_first_component. */
892 first_component_command (char *arg
, int from_tty
)
894 int len
= cp_find_first_component (arg
);
895 char *prefix
= alloca (len
+ 1);
897 memcpy (prefix
, arg
, len
);
900 printf_unfiltered ("%s\n", prefix
);
903 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
906 _initialize_cp_support (void)
908 add_prefix_cmd ("cplus", class_maintenance
, maint_cplus_command
,
909 _("C++ maintenance commands."), &maint_cplus_cmd_list
,
910 "maintenance cplus ", 0, &maintenancelist
);
911 add_alias_cmd ("cp", "cplus", class_maintenance
, 1, &maintenancelist
);
913 add_cmd ("first_component", class_maintenance
, first_component_command
,
914 _("Print the first class/namespace component of NAME."),
915 &maint_cplus_cmd_list
);