1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2015 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"
25 #include "dictionary.h"
30 #include "complaints.h"
32 #include "expression.h"
35 #include "namespace.h"
38 #include "safe-ctype.h"
40 #define d_left(dc) (dc)->u.s_binary.left
41 #define d_right(dc) (dc)->u.s_binary.right
43 /* Functions related to demangled name parsing. */
45 static unsigned int cp_find_first_component_aux (const char *name,
48 static void demangled_name_complaint (const char *name);
50 /* Functions/variables related to overload resolution. */
52 static int sym_return_val_size = -1;
53 static int sym_return_val_index;
54 static struct symbol **sym_return_val;
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 *the_namespace);
62 static void make_symbol_overload_list_qualified (const char *func_name);
64 /* The list of "maint cplus" commands. */
66 struct cmd_list_element *maint_cplus_cmd_list = NULL;
68 /* The actual commands. */
70 static void maint_cplus_command (char *arg, int from_tty);
71 static void first_component_command (char *arg, int from_tty);
73 /* A list of typedefs which should not be substituted by replace_typedefs. */
74 static const char * const ignore_typedefs[] =
76 "std::istream", "std::iostream", "std::ostream", "std::string"
80 replace_typedefs (struct demangle_parse_info *info,
81 struct demangle_component *ret_comp,
82 canonicalization_ftype *finder,
85 /* A convenience function to copy STRING into OBSTACK, returning a pointer
86 to the newly allocated string and saving the number of bytes saved in LEN.
88 It does not copy the terminating '\0' byte! */
91 copy_string_to_obstack (struct obstack *obstack, const char *string,
94 *len = strlen (string);
95 return obstack_copy (obstack, string, *len);
98 /* A cleanup wrapper for cp_demangled_name_parse_free. */
101 do_demangled_name_parse_free_cleanup (void *data)
103 struct demangle_parse_info *info = (struct demangle_parse_info *) data;
105 cp_demangled_name_parse_free (info);
108 /* Create a cleanup for C++ name parsing. */
111 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
113 return make_cleanup (do_demangled_name_parse_free_cleanup, info);
116 /* Return 1 if STRING is clearly already in canonical form. This
117 function is conservative; things which it does not recognize are
118 assumed to be non-canonical, and the parser will sort them out
119 afterwards. This speeds up the critical path for alphanumeric
123 cp_already_canonical (const char *string)
125 /* Identifier start character [a-zA-Z_]. */
126 if (!ISIDST (string[0]))
129 /* These are the only two identifiers which canonicalize to other
130 than themselves or an error: unsigned -> unsigned int and
132 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
134 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
137 /* Identifier character [a-zA-Z0-9_]. */
138 while (ISIDNUM (string[1]))
141 if (string[1] == '\0')
147 /* Inspect the given RET_COMP for its type. If it is a typedef,
148 replace the node with the typedef's tree.
150 Returns 1 if any typedef substitutions were made, 0 otherwise. */
153 inspect_type (struct demangle_parse_info *info,
154 struct demangle_component *ret_comp,
155 canonicalization_ftype *finder,
162 /* Copy the symbol's name from RET_COMP and look it up
163 in the symbol table. */
164 name = (char *) alloca (ret_comp->u.s_name.len + 1);
165 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
166 name[ret_comp->u.s_name.len] = '\0';
168 /* Ignore any typedefs that should not be substituted. */
169 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
171 if (strcmp (name, ignore_typedefs[i]) == 0)
179 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol;
181 CATCH (except, RETURN_MASK_ALL)
189 struct type *otype = SYMBOL_TYPE (sym);
193 const char *new_name = (*finder) (otype, data);
195 if (new_name != NULL)
197 ret_comp->u.s_name.s = new_name;
198 ret_comp->u.s_name.len = strlen (new_name);
205 /* If the type is a typedef or namespace alias, replace it. */
206 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
207 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
212 struct demangle_parse_info *i;
215 /* Get the real type of the typedef. */
216 type = check_typedef (otype);
218 /* If the symbol is a namespace and its type name is no different
219 than the name we looked up, this symbol is not a namespace
220 alias and does not need to be substituted. */
221 if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
222 && strcmp (TYPE_NAME (type), name) == 0)
225 is_anon = (TYPE_TAG_NAME (type) == NULL
226 && (TYPE_CODE (type) == TYPE_CODE_ENUM
227 || TYPE_CODE (type) == TYPE_CODE_STRUCT
228 || TYPE_CODE (type) == TYPE_CODE_UNION));
231 struct type *last = otype;
233 /* Find the last typedef for the type. */
234 while (TYPE_TARGET_TYPE (last) != NULL
235 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
236 == TYPE_CODE_TYPEDEF))
237 last = TYPE_TARGET_TYPE (last);
239 /* If there is only one typedef for this anonymous type,
240 do not substitute it. */
244 /* Use the last typedef seen as the type for this
249 buf = mem_fileopen ();
252 type_print (type, "", buf, -1);
255 /* If type_print threw an exception, there is little point
256 in continuing, so just bow out gracefully. */
257 CATCH (except, RETURN_MASK_ERROR)
259 ui_file_delete (buf);
264 name = ui_file_obsavestring (buf, &info->obstack, &len);
265 ui_file_delete (buf);
267 /* Turn the result into a new tree. Note that this
268 tree will contain pointers into NAME, so NAME cannot
269 be free'd until all typedef conversion is done and
270 the final result is converted into a string. */
271 i = cp_demangled_name_to_comp (name, NULL);
274 /* Merge the two trees. */
275 cp_merge_demangle_parse_infos (info, ret_comp, i);
277 /* Replace any newly introduced typedefs -- but not
278 if the type is anonymous (that would lead to infinite
281 replace_typedefs (info, ret_comp, finder, data);
285 /* This shouldn't happen unless the type printer has
286 output something that the name parser cannot grok.
287 Nonetheless, an ounce of prevention...
289 Canonicalize the name again, and store it in the
290 current node (RET_COMP). */
291 char *canon = cp_canonicalize_string_no_typedefs (name);
295 /* Copy the canonicalization into the obstack and
297 name = copy_string_to_obstack (&info->obstack, canon, &len);
301 ret_comp->u.s_name.s = name;
302 ret_comp->u.s_name.len = len;
312 /* Replace any typedefs appearing in the qualified name
313 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
317 replace_typedefs_qualified_name (struct demangle_parse_info *info,
318 struct demangle_component *ret_comp,
319 canonicalization_ftype *finder,
324 struct ui_file *buf = mem_fileopen ();
325 struct demangle_component *comp = ret_comp;
327 /* Walk each node of the qualified name, reconstructing the name of
328 this element. With every node, check for any typedef substitutions.
329 If a substitution has occurred, replace the qualified name node
330 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
332 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
334 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
336 struct demangle_component newobj;
338 ui_file_write (buf, d_left (comp)->u.s_name.s,
339 d_left (comp)->u.s_name.len);
340 name = ui_file_obsavestring (buf, &info->obstack, &len);
341 newobj.type = DEMANGLE_COMPONENT_NAME;
342 newobj.u.s_name.s = name;
343 newobj.u.s_name.len = len;
344 if (inspect_type (info, &newobj, finder, data))
349 /* A typedef was substituted in NEW. Convert it to a
350 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
353 ui_file_rewind (buf);
354 n = cp_comp_to_string (&newobj, 100);
357 /* If something went astray, abort typedef substitutions. */
358 ui_file_delete (buf);
362 s = copy_string_to_obstack (&info->obstack, n, &slen);
365 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
366 d_left (ret_comp)->u.s_name.s = s;
367 d_left (ret_comp)->u.s_name.len = slen;
368 d_right (ret_comp) = d_right (comp);
375 /* The current node is not a name, so simply replace any
376 typedefs in it. Then print it to the stream to continue
377 checking for more typedefs in the tree. */
378 replace_typedefs (info, d_left (comp), finder, data);
379 name = cp_comp_to_string (d_left (comp), 100);
382 /* If something went astray, abort typedef substitutions. */
383 ui_file_delete (buf);
386 fputs_unfiltered (name, buf);
390 ui_file_write (buf, "::", 2);
391 comp = d_right (comp);
394 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
395 name assembled above and append the name given by COMP. Then use this
396 reassembled name to check for a typedef. */
398 if (comp->type == DEMANGLE_COMPONENT_NAME)
400 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
401 name = ui_file_obsavestring (buf, &info->obstack, &len);
403 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
404 with a DEMANGLE_COMPONENT_NAME node containing the whole
406 ret_comp->type = DEMANGLE_COMPONENT_NAME;
407 ret_comp->u.s_name.s = name;
408 ret_comp->u.s_name.len = len;
409 inspect_type (info, ret_comp, finder, data);
412 replace_typedefs (info, comp, finder, data);
414 ui_file_delete (buf);
418 /* A function to check const and volatile qualifiers for argument types.
420 "Parameter declarations that differ only in the presence
421 or absence of `const' and/or `volatile' are equivalent."
422 C++ Standard N3290, clause 13.1.3 #4. */
425 check_cv_qualifiers (struct demangle_component *ret_comp)
427 while (d_left (ret_comp) != NULL
428 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
429 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
431 d_left (ret_comp) = d_left (d_left (ret_comp));
435 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
436 their basic types. */
439 replace_typedefs (struct demangle_parse_info *info,
440 struct demangle_component *ret_comp,
441 canonicalization_ftype *finder,
447 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
448 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
449 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
450 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
452 char *local_name = cp_comp_to_string (ret_comp, 10);
454 if (local_name != NULL)
456 struct symbol *sym = NULL;
461 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0).symbol;
463 CATCH (except, RETURN_MASK_ALL)
472 struct type *otype = SYMBOL_TYPE (sym);
473 const char *new_name = (*finder) (otype, data);
475 if (new_name != NULL)
477 ret_comp->type = DEMANGLE_COMPONENT_NAME;
478 ret_comp->u.s_name.s = new_name;
479 ret_comp->u.s_name.len = strlen (new_name);
486 switch (ret_comp->type)
488 case DEMANGLE_COMPONENT_ARGLIST:
489 check_cv_qualifiers (ret_comp);
492 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
493 case DEMANGLE_COMPONENT_TEMPLATE:
494 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
495 case DEMANGLE_COMPONENT_TYPED_NAME:
496 replace_typedefs (info, d_left (ret_comp), finder, data);
497 replace_typedefs (info, d_right (ret_comp), finder, data);
500 case DEMANGLE_COMPONENT_NAME:
501 inspect_type (info, ret_comp, finder, data);
504 case DEMANGLE_COMPONENT_QUAL_NAME:
505 replace_typedefs_qualified_name (info, ret_comp, finder, data);
508 case DEMANGLE_COMPONENT_LOCAL_NAME:
509 case DEMANGLE_COMPONENT_CTOR:
510 case DEMANGLE_COMPONENT_ARRAY_TYPE:
511 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
512 replace_typedefs (info, d_right (ret_comp), finder, data);
515 case DEMANGLE_COMPONENT_CONST:
516 case DEMANGLE_COMPONENT_RESTRICT:
517 case DEMANGLE_COMPONENT_VOLATILE:
518 case DEMANGLE_COMPONENT_VOLATILE_THIS:
519 case DEMANGLE_COMPONENT_CONST_THIS:
520 case DEMANGLE_COMPONENT_RESTRICT_THIS:
521 case DEMANGLE_COMPONENT_POINTER:
522 case DEMANGLE_COMPONENT_REFERENCE:
523 replace_typedefs (info, d_left (ret_comp), finder, data);
532 /* Parse STRING and convert it to canonical form, resolving any typedefs.
533 If parsing fails, or if STRING is already canonical, return NULL.
534 Otherwise return the canonical form. The return value is allocated via
535 xmalloc. If FINDER is not NULL, then type components are passed to
536 FINDER to be looked up. DATA is passed verbatim to FINDER. */
539 cp_canonicalize_string_full (const char *string,
540 canonicalization_ftype *finder,
544 unsigned int estimated_len;
545 struct demangle_parse_info *info;
548 estimated_len = strlen (string) * 2;
549 info = cp_demangled_name_to_comp (string, NULL);
552 /* Replace all the typedefs in the tree. */
553 replace_typedefs (info, info->tree, finder, data);
555 /* Convert the tree back into a string. */
556 ret = cp_comp_to_string (info->tree, estimated_len);
557 gdb_assert (ret != NULL);
559 /* Free the parse information. */
560 cp_demangled_name_parse_free (info);
562 /* Finally, compare the original string with the computed
563 name, returning NULL if they are the same. */
564 if (strcmp (string, ret) == 0)
574 /* Like cp_canonicalize_string_full, but always passes NULL for
578 cp_canonicalize_string_no_typedefs (const char *string)
580 return cp_canonicalize_string_full (string, NULL, NULL);
583 /* Parse STRING and convert it to canonical form. If parsing fails,
584 or if STRING is already canonical, return NULL. Otherwise return
585 the canonical form. The return value is allocated via xmalloc. */
588 cp_canonicalize_string (const char *string)
590 struct demangle_parse_info *info;
591 unsigned int estimated_len;
594 if (cp_already_canonical (string))
597 info = cp_demangled_name_to_comp (string, NULL);
601 estimated_len = strlen (string) * 2;
602 ret = cp_comp_to_string (info->tree, estimated_len);
603 cp_demangled_name_parse_free (info);
607 warning (_("internal error: string \"%s\" failed to be canonicalized"),
612 if (strcmp (string, ret) == 0)
621 /* Convert a mangled name to a demangle_component tree. *MEMORY is
622 set to the block of used memory that should be freed when finished
623 with the tree. DEMANGLED_P is set to the char * that should be
624 freed when finished with the tree, or NULL if none was needed.
625 OPTIONS will be passed to the demangler. */
627 static struct demangle_parse_info *
628 mangled_name_to_comp (const char *mangled_name, int options,
629 void **memory, char **demangled_p)
631 char *demangled_name;
632 struct demangle_parse_info *info;
634 /* If it looks like a v3 mangled name, then try to go directly
636 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
638 struct demangle_component *ret;
640 ret = cplus_demangle_v3_components (mangled_name,
644 info = cp_new_demangle_parse_info ();
651 /* If it doesn't, or if that failed, then try to demangle the
653 demangled_name = gdb_demangle (mangled_name, options);
654 if (demangled_name == NULL)
657 /* If we could demangle the name, parse it to build the component
659 info = cp_demangled_name_to_comp (demangled_name, NULL);
663 xfree (demangled_name);
667 *demangled_p = demangled_name;
671 /* Return the name of the class containing method PHYSNAME. */
674 cp_class_name_from_physname (const char *physname)
676 void *storage = NULL;
677 char *demangled_name = NULL, *ret;
678 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
679 struct demangle_parse_info *info;
682 info = mangled_name_to_comp (physname, DMGL_ANSI,
683 &storage, &demangled_name);
688 ret_comp = info->tree;
690 /* First strip off any qualifiers, if we have a function or
693 switch (ret_comp->type)
695 case DEMANGLE_COMPONENT_CONST:
696 case DEMANGLE_COMPONENT_RESTRICT:
697 case DEMANGLE_COMPONENT_VOLATILE:
698 case DEMANGLE_COMPONENT_CONST_THIS:
699 case DEMANGLE_COMPONENT_RESTRICT_THIS:
700 case DEMANGLE_COMPONENT_VOLATILE_THIS:
701 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
702 ret_comp = d_left (ret_comp);
709 /* If what we have now is a function, discard the argument list. */
710 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
711 ret_comp = d_left (ret_comp);
713 /* If what we have now is a template, strip off the template
714 arguments. The left subtree may be a qualified name. */
715 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
716 ret_comp = d_left (ret_comp);
718 /* What we have now should be a name, possibly qualified.
719 Additional qualifiers could live in the left subtree or the right
720 subtree. Find the last piece. */
725 switch (cur_comp->type)
727 case DEMANGLE_COMPONENT_QUAL_NAME:
728 case DEMANGLE_COMPONENT_LOCAL_NAME:
729 prev_comp = cur_comp;
730 cur_comp = d_right (cur_comp);
732 case DEMANGLE_COMPONENT_TEMPLATE:
733 case DEMANGLE_COMPONENT_NAME:
734 case DEMANGLE_COMPONENT_CTOR:
735 case DEMANGLE_COMPONENT_DTOR:
736 case DEMANGLE_COMPONENT_OPERATOR:
737 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
747 if (cur_comp != NULL && prev_comp != NULL)
749 /* We want to discard the rightmost child of PREV_COMP. */
750 *prev_comp = *d_left (prev_comp);
751 /* The ten is completely arbitrary; we don't have a good
753 ret = cp_comp_to_string (ret_comp, 10);
757 xfree (demangled_name);
758 cp_demangled_name_parse_free (info);
762 /* Return the child of COMP which is the basename of a method,
763 variable, et cetera. All scope qualifiers are discarded, but
764 template arguments will be included. The component tree may be
767 static struct demangle_component *
768 unqualified_name_from_comp (struct demangle_component *comp)
770 struct demangle_component *ret_comp = comp, *last_template;
774 last_template = NULL;
776 switch (ret_comp->type)
778 case DEMANGLE_COMPONENT_QUAL_NAME:
779 case DEMANGLE_COMPONENT_LOCAL_NAME:
780 ret_comp = d_right (ret_comp);
782 case DEMANGLE_COMPONENT_TYPED_NAME:
783 ret_comp = d_left (ret_comp);
785 case DEMANGLE_COMPONENT_TEMPLATE:
786 gdb_assert (last_template == NULL);
787 last_template = ret_comp;
788 ret_comp = d_left (ret_comp);
790 case DEMANGLE_COMPONENT_CONST:
791 case DEMANGLE_COMPONENT_RESTRICT:
792 case DEMANGLE_COMPONENT_VOLATILE:
793 case DEMANGLE_COMPONENT_CONST_THIS:
794 case DEMANGLE_COMPONENT_RESTRICT_THIS:
795 case DEMANGLE_COMPONENT_VOLATILE_THIS:
796 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
797 ret_comp = d_left (ret_comp);
799 case DEMANGLE_COMPONENT_NAME:
800 case DEMANGLE_COMPONENT_CTOR:
801 case DEMANGLE_COMPONENT_DTOR:
802 case DEMANGLE_COMPONENT_OPERATOR:
803 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
813 d_left (last_template) = ret_comp;
814 return last_template;
820 /* Return the name of the method whose linkage name is PHYSNAME. */
823 method_name_from_physname (const char *physname)
825 void *storage = NULL;
826 char *demangled_name = NULL, *ret;
827 struct demangle_component *ret_comp;
828 struct demangle_parse_info *info;
830 info = mangled_name_to_comp (physname, DMGL_ANSI,
831 &storage, &demangled_name);
835 ret_comp = unqualified_name_from_comp (info->tree);
838 if (ret_comp != NULL)
839 /* The ten is completely arbitrary; we don't have a good
841 ret = cp_comp_to_string (ret_comp, 10);
844 xfree (demangled_name);
845 cp_demangled_name_parse_free (info);
849 /* If FULL_NAME is the demangled name of a C++ function (including an
850 arg list, possibly including namespace/class qualifications),
851 return a new string containing only the function name (without the
852 arg list/class qualifications). Otherwise, return NULL. The
853 caller is responsible for freeing the memory in question. */
856 cp_func_name (const char *full_name)
859 struct demangle_component *ret_comp;
860 struct demangle_parse_info *info;
862 info = cp_demangled_name_to_comp (full_name, NULL);
866 ret_comp = unqualified_name_from_comp (info->tree);
869 if (ret_comp != NULL)
870 ret = cp_comp_to_string (ret_comp, 10);
872 cp_demangled_name_parse_free (info);
876 /* DEMANGLED_NAME is the name of a function, including parameters and
877 (optionally) a return type. Return the name of the function without
878 parameters or return type, or NULL if we can not parse the name. */
881 cp_remove_params (const char *demangled_name)
884 struct demangle_component *ret_comp;
885 struct demangle_parse_info *info;
888 if (demangled_name == NULL)
891 info = cp_demangled_name_to_comp (demangled_name, NULL);
895 /* First strip off any qualifiers, if we have a function or method. */
896 ret_comp = info->tree;
898 switch (ret_comp->type)
900 case DEMANGLE_COMPONENT_CONST:
901 case DEMANGLE_COMPONENT_RESTRICT:
902 case DEMANGLE_COMPONENT_VOLATILE:
903 case DEMANGLE_COMPONENT_CONST_THIS:
904 case DEMANGLE_COMPONENT_RESTRICT_THIS:
905 case DEMANGLE_COMPONENT_VOLATILE_THIS:
906 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
907 ret_comp = d_left (ret_comp);
914 /* What we have now should be a function. Return its name. */
915 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
916 ret = cp_comp_to_string (d_left (ret_comp), 10);
918 cp_demangled_name_parse_free (info);
922 /* Here are some random pieces of trivia to keep in mind while trying
923 to take apart demangled names:
925 - Names can contain function arguments or templates, so the process
926 has to be, to some extent recursive: maybe keep track of your
927 depth based on encountering <> and ().
929 - Parentheses don't just have to happen at the end of a name: they
930 can occur even if the name in question isn't a function, because
931 a template argument might be a type that's a function.
933 - Conversely, even if you're trying to deal with a function, its
934 demangled name might not end with ')': it could be a const or
935 volatile class method, in which case it ends with "const" or
938 - Parentheses are also used in anonymous namespaces: a variable
939 'foo' in an anonymous namespace gets demangled as "(anonymous
942 - And operator names can contain parentheses or angle brackets. */
944 /* FIXME: carlton/2003-03-13: We have several functions here with
945 overlapping functionality; can we combine them? Also, do they
946 handle all the above considerations correctly? */
949 /* This returns the length of first component of NAME, which should be
950 the demangled name of a C++ variable/function/method/etc.
951 Specifically, it returns the index of the first colon forming the
952 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
953 it returns the 1, and given 'foo', it returns 0. */
955 /* The character in NAME indexed by the return value is guaranteed to
956 always be either ':' or '\0'. */
958 /* NOTE: carlton/2003-03-13: This function is currently only intended
959 for internal use: it's probably not entirely safe when called on
960 user-generated input, because some of the 'index += 2' lines in
961 cp_find_first_component_aux might go past the end of malformed
965 cp_find_first_component (const char *name)
967 return cp_find_first_component_aux (name, 0);
970 /* Helper function for cp_find_first_component. Like that function,
971 it returns the length of the first component of NAME, but to make
972 the recursion easier, it also stops if it reaches an unexpected ')'
973 or '>' if the value of PERMISSIVE is nonzero. */
975 /* Let's optimize away calls to strlen("operator"). */
977 #define LENGTH_OF_OPERATOR 8
980 cp_find_first_component_aux (const char *name, int permissive)
982 unsigned int index = 0;
983 /* Operator names can show up in unexpected places. Since these can
984 contain parentheses or angle brackets, they can screw up the
985 recursion. But not every string 'operator' is part of an
986 operater name: e.g. you could have a variable 'cooperator'. So
987 this variable tells us whether or not we should treat the string
988 'operator' as starting an operator. */
989 int operator_possible = 1;
996 /* Template; eat it up. The calls to cp_first_component
997 should only return (I hope!) when they reach the '>'
998 terminating the component or a '::' between two
999 components. (Hence the '+ 2'.) */
1001 for (index += cp_find_first_component_aux (name + index, 1);
1003 index += cp_find_first_component_aux (name + index, 1))
1005 if (name[index] != ':')
1007 demangled_name_complaint (name);
1008 return strlen (name);
1012 operator_possible = 1;
1015 /* Similar comment as to '<'. */
1017 for (index += cp_find_first_component_aux (name + index, 1);
1019 index += cp_find_first_component_aux (name + index, 1))
1021 if (name[index] != ':')
1023 demangled_name_complaint (name);
1024 return strlen (name);
1028 operator_possible = 1;
1036 demangled_name_complaint (name);
1037 return strlen (name);
1043 /* Operator names can screw up the recursion. */
1044 if (operator_possible
1045 && strncmp (name + index, "operator",
1046 LENGTH_OF_OPERATOR) == 0)
1048 index += LENGTH_OF_OPERATOR;
1049 while (ISSPACE(name[index]))
1051 switch (name[index])
1053 /* Skip over one less than the appropriate number of
1054 characters: the for loop will skip over the last
1057 if (name[index + 1] == '<')
1064 if (name[index + 1] == '>')
1077 operator_possible = 0;
1084 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1085 set of relevant characters are here: it's necessary to
1086 include any character that can show up before 'operator'
1087 in a demangled name, and it's safe to include any
1088 character that can't be part of an identifier's name. */
1089 operator_possible = 1;
1092 operator_possible = 0;
1098 /* Complain about a demangled name that we don't know how to parse.
1099 NAME is the demangled name in question. */
1102 demangled_name_complaint (const char *name)
1104 complaint (&symfile_complaints,
1105 "unexpected demangled name '%s'", name);
1108 /* If NAME is the fully-qualified name of a C++
1109 function/variable/method/etc., this returns the length of its
1110 entire prefix: all of the namespaces and classes that make up its
1111 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1112 4, given 'foo', it returns 0. */
1115 cp_entire_prefix_len (const char *name)
1117 unsigned int current_len = cp_find_first_component (name);
1118 unsigned int previous_len = 0;
1120 while (name[current_len] != '\0')
1122 gdb_assert (name[current_len] == ':');
1123 previous_len = current_len;
1124 /* Skip the '::'. */
1126 current_len += cp_find_first_component (name + current_len);
1129 return previous_len;
1132 /* Overload resolution functions. */
1134 /* Test to see if SYM is a symbol that we haven't seen corresponding
1135 to a function named OLOAD_NAME. If so, add it to the current
1139 overload_list_add_symbol (struct symbol *sym,
1140 const char *oload_name)
1146 /* If there is no type information, we can't do anything, so
1148 if (SYMBOL_TYPE (sym) == NULL)
1151 /* skip any symbols that we've already considered. */
1152 for (i = 0; i < sym_return_val_index; ++i)
1153 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1154 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1157 /* Get the demangled name without parameters */
1158 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1162 /* skip symbols that cannot match */
1163 if (strcmp (sym_name, oload_name) != 0)
1171 /* We have a match for an overload instance, so add SYM to the
1172 current list of overload instances */
1173 if (sym_return_val_index + 3 > sym_return_val_size)
1175 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1176 sym_return_val = (struct symbol **)
1177 xrealloc ((char *) sym_return_val, newsize);
1179 sym_return_val[sym_return_val_index++] = sym;
1180 sym_return_val[sym_return_val_index] = NULL;
1183 /* Return a null-terminated list of pointers to function symbols that
1184 are named FUNC_NAME and are visible within NAMESPACE. */
1187 make_symbol_overload_list (const char *func_name,
1188 const char *the_namespace)
1190 struct cleanup *old_cleanups;
1193 sym_return_val_size = 100;
1194 sym_return_val_index = 0;
1195 sym_return_val = XNEWVEC (struct symbol *, sym_return_val_size + 1);
1196 sym_return_val[0] = NULL;
1198 old_cleanups = make_cleanup (xfree, sym_return_val);
1200 make_symbol_overload_list_using (func_name, the_namespace);
1202 if (the_namespace[0] == '\0')
1206 char *concatenated_name
1207 = alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1208 strcpy (concatenated_name, the_namespace);
1209 strcat (concatenated_name, "::");
1210 strcat (concatenated_name, func_name);
1211 name = concatenated_name;
1214 make_symbol_overload_list_qualified (name);
1216 discard_cleanups (old_cleanups);
1218 return sym_return_val;
1221 /* Add all symbols with a name matching NAME in BLOCK to the overload
1225 make_symbol_overload_list_block (const char *name,
1226 const struct block *block)
1228 struct block_iterator iter;
1231 ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
1232 overload_list_add_symbol (sym, name);
1235 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1238 make_symbol_overload_list_namespace (const char *func_name,
1239 const char *the_namespace)
1242 const struct block *block = NULL;
1244 if (the_namespace[0] == '\0')
1248 char *concatenated_name
1249 = alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1251 strcpy (concatenated_name, the_namespace);
1252 strcat (concatenated_name, "::");
1253 strcat (concatenated_name, func_name);
1254 name = concatenated_name;
1257 /* Look in the static block. */
1258 block = block_static_block (get_selected_block (0));
1260 make_symbol_overload_list_block (name, block);
1262 /* Look in the global block. */
1263 block = block_global_block (block);
1265 make_symbol_overload_list_block (name, block);
1269 /* Search the namespace of the given type and namespace of and public
1273 make_symbol_overload_list_adl_namespace (struct type *type,
1274 const char *func_name)
1276 char *the_namespace;
1277 const char *type_name;
1280 while (TYPE_CODE (type) == TYPE_CODE_PTR
1281 || TYPE_CODE (type) == TYPE_CODE_REF
1282 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1283 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1285 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1286 type = check_typedef(type);
1288 type = TYPE_TARGET_TYPE (type);
1291 type_name = TYPE_NAME (type);
1293 if (type_name == NULL)
1296 prefix_len = cp_entire_prefix_len (type_name);
1298 if (prefix_len != 0)
1300 the_namespace = alloca (prefix_len + 1);
1301 strncpy (the_namespace, type_name, prefix_len);
1302 the_namespace[prefix_len] = '\0';
1304 make_symbol_overload_list_namespace (func_name, the_namespace);
1307 /* Check public base type */
1308 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1309 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1311 if (BASETYPE_VIA_PUBLIC (type, i))
1312 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1318 /* Adds the overload list overload candidates for FUNC_NAME found
1319 through argument dependent lookup. */
1322 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1323 const char *func_name)
1327 gdb_assert (sym_return_val_size != -1);
1329 for (i = 1; i <= nargs; i++)
1330 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1333 return sym_return_val;
1336 /* Used for cleanups to reset the "searched" flag in case of an
1340 reset_directive_searched (void *data)
1342 struct using_direct *direct = data;
1343 direct->searched = 0;
1346 /* This applies the using directives to add namespaces to search in,
1347 and then searches for overloads in all of those namespaces. It
1348 adds the symbols found to sym_return_val. Arguments are as in
1349 make_symbol_overload_list. */
1352 make_symbol_overload_list_using (const char *func_name,
1353 const char *the_namespace)
1355 struct using_direct *current;
1356 const struct block *block;
1358 /* First, go through the using directives. If any of them apply,
1359 look in the appropriate namespaces for new functions to match
1362 for (block = get_selected_block (0);
1364 block = BLOCK_SUPERBLOCK (block))
1365 for (current = block_using (block);
1367 current = current->next)
1369 /* Prevent recursive calls. */
1370 if (current->searched)
1373 /* If this is a namespace alias or imported declaration ignore
1375 if (current->alias != NULL || current->declaration != NULL)
1378 if (strcmp (the_namespace, current->import_dest) == 0)
1380 /* Mark this import as searched so that the recursive call
1381 does not search it again. */
1382 struct cleanup *old_chain;
1383 current->searched = 1;
1384 old_chain = make_cleanup (reset_directive_searched,
1387 make_symbol_overload_list_using (func_name,
1388 current->import_src);
1390 current->searched = 0;
1391 discard_cleanups (old_chain);
1395 /* Now, add names for this namespace. */
1396 make_symbol_overload_list_namespace (func_name, the_namespace);
1399 /* This does the bulk of the work of finding overloaded symbols.
1400 FUNC_NAME is the name of the overloaded function we're looking for
1401 (possibly including namespace info). */
1404 make_symbol_overload_list_qualified (const char *func_name)
1406 struct compunit_symtab *cust;
1407 struct objfile *objfile;
1408 const struct block *b, *surrounding_static_block = 0;
1410 /* Look through the partial symtabs for all symbols which begin by
1411 matching FUNC_NAME. Make sure we read that symbol table in. */
1413 ALL_OBJFILES (objfile)
1416 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1419 /* Search upwards from currently selected frame (so that we can
1420 complete on local vars. */
1422 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1423 make_symbol_overload_list_block (func_name, b);
1425 surrounding_static_block = block_static_block (get_selected_block (0));
1427 /* Go through the symtabs and check the externs and statics for
1428 symbols which match. */
1430 ALL_COMPUNITS (objfile, cust)
1433 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
1434 make_symbol_overload_list_block (func_name, b);
1437 ALL_COMPUNITS (objfile, cust)
1440 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
1441 /* Don't do this block twice. */
1442 if (b == surrounding_static_block)
1444 make_symbol_overload_list_block (func_name, b);
1448 /* Lookup the rtti type for a class name. */
1451 cp_lookup_rtti_type (const char *name, struct block *block)
1453 struct symbol * rtti_sym;
1454 struct type * rtti_type;
1456 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1457 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
1458 rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol;
1460 if (rtti_sym == NULL)
1462 warning (_("RTTI symbol not found for class '%s'"), name);
1466 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1468 warning (_("RTTI symbol for class '%s' is not a type"), name);
1472 rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym));
1474 switch (TYPE_CODE (rtti_type))
1476 case TYPE_CODE_STRUCT:
1478 case TYPE_CODE_NAMESPACE:
1479 /* chastain/2003-11-26: the symbol tables often contain fake
1480 symbols for namespaces with the same name as the struct.
1481 This warning is an indication of a bug in the lookup order
1482 or a bug in the way that the symbol tables are populated. */
1483 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1486 warning (_("RTTI symbol for class '%s' has bad type"), name);
1493 #ifdef HAVE_WORKING_FORK
1495 /* If nonzero, attempt to catch crashes in the demangler and print
1496 useful debugging information. */
1498 static int catch_demangler_crashes = 1;
1500 /* Stack context and environment for demangler crash recovery. */
1502 static SIGJMP_BUF gdb_demangle_jmp_buf;
1504 /* If nonzero, attempt to dump core from the signal handler. */
1506 static int gdb_demangle_attempt_core_dump = 1;
1508 /* Signal handler for gdb_demangle. */
1511 gdb_demangle_signal_handler (int signo)
1513 if (gdb_demangle_attempt_core_dump)
1518 gdb_demangle_attempt_core_dump = 0;
1521 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1526 /* A wrapper for bfd_demangle. */
1529 gdb_demangle (const char *name, int options)
1531 char *result = NULL;
1532 int crash_signal = 0;
1534 #ifdef HAVE_WORKING_FORK
1535 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1536 struct sigaction sa, old_sa;
1540 static int core_dump_allowed = -1;
1542 if (core_dump_allowed == -1)
1544 core_dump_allowed = can_dump_core (LIMIT_CUR);
1546 if (!core_dump_allowed)
1547 gdb_demangle_attempt_core_dump = 0;
1550 if (catch_demangler_crashes)
1552 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1553 sa.sa_handler = gdb_demangle_signal_handler;
1554 sigemptyset (&sa.sa_mask);
1555 #ifdef HAVE_SIGALTSTACK
1556 sa.sa_flags = SA_ONSTACK;
1560 sigaction (SIGSEGV, &sa, &old_sa);
1562 ofunc = signal (SIGSEGV, gdb_demangle_signal_handler);
1565 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1569 if (crash_signal == 0)
1570 result = bfd_demangle (NULL, name, options);
1572 #ifdef HAVE_WORKING_FORK
1573 if (catch_demangler_crashes)
1575 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1576 sigaction (SIGSEGV, &old_sa, NULL);
1578 signal (SIGSEGV, ofunc);
1581 if (crash_signal != 0)
1583 static int error_reported = 0;
1585 if (!error_reported)
1587 char *short_msg, *long_msg;
1588 struct cleanup *back_to;
1590 short_msg = xstrprintf (_("unable to demangle '%s' "
1591 "(demangler failed with signal %d)"),
1592 name, crash_signal);
1593 back_to = make_cleanup (xfree, short_msg);
1595 long_msg = xstrprintf ("%s:%d: %s: %s", __FILE__, __LINE__,
1596 "demangler-warning", short_msg);
1597 make_cleanup (xfree, long_msg);
1599 target_terminal_ours ();
1601 if (core_dump_allowed)
1602 fprintf_unfiltered (gdb_stderr,
1603 _("%s\nAttempting to dump core.\n"),
1606 warn_cant_dump_core (long_msg);
1608 demangler_warning (__FILE__, __LINE__, "%s", short_msg);
1610 do_cleanups (back_to);
1623 /* Don't allow just "maintenance cplus". */
1626 maint_cplus_command (char *arg, int from_tty)
1628 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1629 "by the name of a command.\n"));
1630 help_list (maint_cplus_cmd_list,
1631 "maintenance cplus ",
1632 all_commands, gdb_stdout);
1635 /* This is a front end for cp_find_first_component, for unit testing.
1636 Be careful when using it: see the NOTE above
1637 cp_find_first_component. */
1640 first_component_command (char *arg, int from_tty)
1648 len = cp_find_first_component (arg);
1649 prefix = alloca (len + 1);
1651 memcpy (prefix, arg, len);
1654 printf_unfiltered ("%s\n", prefix);
1657 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1660 /* Implement "info vtbl". */
1663 info_vtbl_command (char *arg, int from_tty)
1665 struct value *value;
1667 value = parse_and_eval (arg);
1668 cplus_print_vtable (value);
1672 _initialize_cp_support (void)
1674 add_prefix_cmd ("cplus", class_maintenance,
1675 maint_cplus_command,
1676 _("C++ maintenance commands."),
1677 &maint_cplus_cmd_list,
1678 "maintenance cplus ",
1679 0, &maintenancelist);
1680 add_alias_cmd ("cp", "cplus",
1681 class_maintenance, 1,
1684 add_cmd ("first_component",
1686 first_component_command,
1687 _("Print the first class/namespace component of NAME."),
1688 &maint_cplus_cmd_list);
1690 add_info ("vtbl", info_vtbl_command,
1691 _("Show the virtual function table for a C++ object.\n\
1692 Usage: info vtbl EXPRESSION\n\
1693 Evaluate EXPRESSION and display the virtual function table for the\n\
1694 resulting object."));
1696 #ifdef HAVE_WORKING_FORK
1697 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
1698 &catch_demangler_crashes, _("\
1699 Set whether to attempt to catch demangler crashes."), _("\
1700 Show whether to attempt to catch demangler crashes."), _("\
1701 If enabled GDB will attempt to catch demangler crashes and\n\
1702 display the offending symbol."),
1705 &maintenance_set_cmdlist,
1706 &maintenance_show_cmdlist);