1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2014 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 "gdb_assert.h"
27 #include "dictionary.h"
32 #include "complaints.h"
34 #include "exceptions.h"
35 #include "expression.h"
40 #include "safe-ctype.h"
42 #define d_left(dc) (dc)->u.s_binary.left
43 #define d_right(dc) (dc)->u.s_binary.right
45 /* Functions related to demangled name parsing. */
47 static unsigned int cp_find_first_component_aux (const char *name,
50 static void demangled_name_complaint (const char *name);
52 /* Functions/variables related to overload resolution. */
54 static int sym_return_val_size = -1;
55 static int sym_return_val_index;
56 static struct symbol **sym_return_val;
58 static void overload_list_add_symbol (struct symbol *sym,
59 const char *oload_name);
61 static void make_symbol_overload_list_using (const char *func_name,
62 const char *namespace);
64 static void make_symbol_overload_list_qualified (const char *func_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 /* A list of typedefs which should not be substituted by replace_typedefs. */
76 static const char * const ignore_typedefs[] =
78 "std::istream", "std::iostream", "std::ostream", "std::string"
82 replace_typedefs (struct demangle_parse_info *info,
83 struct demangle_component *ret_comp,
84 canonicalization_ftype *finder,
87 /* A convenience function to copy STRING into OBSTACK, returning a pointer
88 to the newly allocated string and saving the number of bytes saved in LEN.
90 It does not copy the terminating '\0' byte! */
93 copy_string_to_obstack (struct obstack *obstack, const char *string,
96 *len = strlen (string);
97 return obstack_copy (obstack, string, *len);
100 /* A cleanup wrapper for cp_demangled_name_parse_free. */
103 do_demangled_name_parse_free_cleanup (void *data)
105 struct demangle_parse_info *info = (struct demangle_parse_info *) data;
107 cp_demangled_name_parse_free (info);
110 /* Create a cleanup for C++ name parsing. */
113 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
115 return make_cleanup (do_demangled_name_parse_free_cleanup, info);
118 /* Return 1 if STRING is clearly already in canonical form. This
119 function is conservative; things which it does not recognize are
120 assumed to be non-canonical, and the parser will sort them out
121 afterwards. This speeds up the critical path for alphanumeric
125 cp_already_canonical (const char *string)
127 /* Identifier start character [a-zA-Z_]. */
128 if (!ISIDST (string[0]))
131 /* These are the only two identifiers which canonicalize to other
132 than themselves or an error: unsigned -> unsigned int and
134 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
136 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
139 /* Identifier character [a-zA-Z0-9_]. */
140 while (ISIDNUM (string[1]))
143 if (string[1] == '\0')
149 /* Inspect the given RET_COMP for its type. If it is a typedef,
150 replace the node with the typedef's tree.
152 Returns 1 if any typedef substitutions were made, 0 otherwise. */
155 inspect_type (struct demangle_parse_info *info,
156 struct demangle_component *ret_comp,
157 canonicalization_ftype *finder,
163 volatile struct gdb_exception except;
165 /* Copy the symbol's name from RET_COMP and look it up
166 in the symbol table. */
167 name = (char *) alloca (ret_comp->u.s_name.len + 1);
168 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
169 name[ret_comp->u.s_name.len] = '\0';
171 /* Ignore any typedefs that should not be substituted. */
172 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
174 if (strcmp (name, ignore_typedefs[i]) == 0)
179 TRY_CATCH (except, RETURN_MASK_ALL)
181 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
184 if (except.reason >= 0 && sym != NULL)
186 struct type *otype = SYMBOL_TYPE (sym);
190 const char *new_name = (*finder) (otype, data);
192 if (new_name != NULL)
194 ret_comp->u.s_name.s = new_name;
195 ret_comp->u.s_name.len = strlen (new_name);
202 /* If the type is a typedef or namespace alias, replace it. */
203 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
204 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
209 struct demangle_parse_info *i;
212 /* Get the real type of the typedef. */
213 type = check_typedef (otype);
215 /* If the symbol is a namespace and its type name is no different
216 than the name we looked up, this symbol is not a namespace
217 alias and does not need to be substituted. */
218 if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
219 && strcmp (TYPE_NAME (type), name) == 0)
222 is_anon = (TYPE_TAG_NAME (type) == NULL
223 && (TYPE_CODE (type) == TYPE_CODE_ENUM
224 || TYPE_CODE (type) == TYPE_CODE_STRUCT
225 || TYPE_CODE (type) == TYPE_CODE_UNION));
228 struct type *last = otype;
230 /* Find the last typedef for the type. */
231 while (TYPE_TARGET_TYPE (last) != NULL
232 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
233 == TYPE_CODE_TYPEDEF))
234 last = TYPE_TARGET_TYPE (last);
236 /* If there is only one typedef for this anonymous type,
237 do not substitute it. */
241 /* Use the last typedef seen as the type for this
246 buf = mem_fileopen ();
247 TRY_CATCH (except, RETURN_MASK_ERROR)
249 type_print (type, "", buf, -1);
252 /* If type_print threw an exception, there is little point
253 in continuing, so just bow out gracefully. */
254 if (except.reason < 0)
256 ui_file_delete (buf);
260 name = ui_file_obsavestring (buf, &info->obstack, &len);
261 ui_file_delete (buf);
263 /* Turn the result into a new tree. Note that this
264 tree will contain pointers into NAME, so NAME cannot
265 be free'd until all typedef conversion is done and
266 the final result is converted into a string. */
267 i = cp_demangled_name_to_comp (name, NULL);
270 /* Merge the two trees. */
271 cp_merge_demangle_parse_infos (info, ret_comp, i);
273 /* Replace any newly introduced typedefs -- but not
274 if the type is anonymous (that would lead to infinite
277 replace_typedefs (info, ret_comp, finder, data);
281 /* This shouldn't happen unless the type printer has
282 output something that the name parser cannot grok.
283 Nonetheless, an ounce of prevention...
285 Canonicalize the name again, and store it in the
286 current node (RET_COMP). */
287 char *canon = cp_canonicalize_string_no_typedefs (name);
291 /* Copy the canonicalization into the obstack and
293 name = copy_string_to_obstack (&info->obstack, canon, &len);
297 ret_comp->u.s_name.s = name;
298 ret_comp->u.s_name.len = len;
308 /* Replace any typedefs appearing in the qualified name
309 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
313 replace_typedefs_qualified_name (struct demangle_parse_info *info,
314 struct demangle_component *ret_comp,
315 canonicalization_ftype *finder,
320 struct ui_file *buf = mem_fileopen ();
321 struct demangle_component *comp = ret_comp;
323 /* Walk each node of the qualified name, reconstructing the name of
324 this element. With every node, check for any typedef substitutions.
325 If a substitution has occurred, replace the qualified name node
326 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
328 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
330 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
332 struct demangle_component new;
334 ui_file_write (buf, d_left (comp)->u.s_name.s,
335 d_left (comp)->u.s_name.len);
336 name = ui_file_obsavestring (buf, &info->obstack, &len);
337 new.type = DEMANGLE_COMPONENT_NAME;
338 new.u.s_name.s = name;
339 new.u.s_name.len = len;
340 if (inspect_type (info, &new, finder, data))
345 /* A typedef was substituted in NEW. Convert it to a
346 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
349 ui_file_rewind (buf);
350 n = cp_comp_to_string (&new, 100);
353 /* If something went astray, abort typedef substitutions. */
354 ui_file_delete (buf);
358 s = copy_string_to_obstack (&info->obstack, n, &slen);
361 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
362 d_left (ret_comp)->u.s_name.s = s;
363 d_left (ret_comp)->u.s_name.len = slen;
364 d_right (ret_comp) = d_right (comp);
371 /* The current node is not a name, so simply replace any
372 typedefs in it. Then print it to the stream to continue
373 checking for more typedefs in the tree. */
374 replace_typedefs (info, d_left (comp), finder, data);
375 name = cp_comp_to_string (d_left (comp), 100);
378 /* If something went astray, abort typedef substitutions. */
379 ui_file_delete (buf);
382 fputs_unfiltered (name, buf);
386 ui_file_write (buf, "::", 2);
387 comp = d_right (comp);
390 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
391 name assembled above and append the name given by COMP. Then use this
392 reassembled name to check for a typedef. */
394 if (comp->type == DEMANGLE_COMPONENT_NAME)
396 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
397 name = ui_file_obsavestring (buf, &info->obstack, &len);
399 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
400 with a DEMANGLE_COMPONENT_NAME node containing the whole
402 ret_comp->type = DEMANGLE_COMPONENT_NAME;
403 ret_comp->u.s_name.s = name;
404 ret_comp->u.s_name.len = len;
405 inspect_type (info, ret_comp, finder, data);
408 replace_typedefs (info, comp, finder, data);
410 ui_file_delete (buf);
414 /* A function to check const and volatile qualifiers for argument types.
416 "Parameter declarations that differ only in the presence
417 or absence of `const' and/or `volatile' are equivalent."
418 C++ Standard N3290, clause 13.1.3 #4. */
421 check_cv_qualifiers (struct demangle_component *ret_comp)
423 while (d_left (ret_comp) != NULL
424 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
425 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
427 d_left (ret_comp) = d_left (d_left (ret_comp));
431 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
432 their basic types. */
435 replace_typedefs (struct demangle_parse_info *info,
436 struct demangle_component *ret_comp,
437 canonicalization_ftype *finder,
443 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
444 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
445 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
446 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
448 char *local_name = cp_comp_to_string (ret_comp, 10);
450 if (local_name != NULL)
453 volatile struct gdb_exception except;
456 TRY_CATCH (except, RETURN_MASK_ALL)
458 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0);
462 if (except.reason >= 0 && sym != NULL)
464 struct type *otype = SYMBOL_TYPE (sym);
465 const char *new_name = (*finder) (otype, data);
467 if (new_name != NULL)
469 ret_comp->type = DEMANGLE_COMPONENT_NAME;
470 ret_comp->u.s_name.s = new_name;
471 ret_comp->u.s_name.len = strlen (new_name);
478 switch (ret_comp->type)
480 case DEMANGLE_COMPONENT_ARGLIST:
481 check_cv_qualifiers (ret_comp);
484 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
485 case DEMANGLE_COMPONENT_TEMPLATE:
486 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
487 case DEMANGLE_COMPONENT_TYPED_NAME:
488 replace_typedefs (info, d_left (ret_comp), finder, data);
489 replace_typedefs (info, d_right (ret_comp), finder, data);
492 case DEMANGLE_COMPONENT_NAME:
493 inspect_type (info, ret_comp, finder, data);
496 case DEMANGLE_COMPONENT_QUAL_NAME:
497 replace_typedefs_qualified_name (info, ret_comp, finder, data);
500 case DEMANGLE_COMPONENT_LOCAL_NAME:
501 case DEMANGLE_COMPONENT_CTOR:
502 case DEMANGLE_COMPONENT_ARRAY_TYPE:
503 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
504 replace_typedefs (info, d_right (ret_comp), finder, data);
507 case DEMANGLE_COMPONENT_CONST:
508 case DEMANGLE_COMPONENT_RESTRICT:
509 case DEMANGLE_COMPONENT_VOLATILE:
510 case DEMANGLE_COMPONENT_VOLATILE_THIS:
511 case DEMANGLE_COMPONENT_CONST_THIS:
512 case DEMANGLE_COMPONENT_RESTRICT_THIS:
513 case DEMANGLE_COMPONENT_POINTER:
514 case DEMANGLE_COMPONENT_REFERENCE:
515 replace_typedefs (info, d_left (ret_comp), finder, data);
524 /* Parse STRING and convert it to canonical form, resolving any typedefs.
525 If parsing fails, or if STRING is already canonical, return NULL.
526 Otherwise return the canonical form. The return value is allocated via
527 xmalloc. If FINDER is not NULL, then type components are passed to
528 FINDER to be looked up. DATA is passed verbatim to FINDER. */
531 cp_canonicalize_string_full (const char *string,
532 canonicalization_ftype *finder,
536 unsigned int estimated_len;
537 struct demangle_parse_info *info;
540 estimated_len = strlen (string) * 2;
541 info = cp_demangled_name_to_comp (string, NULL);
544 /* Replace all the typedefs in the tree. */
545 replace_typedefs (info, info->tree, finder, data);
547 /* Convert the tree back into a string. */
548 ret = cp_comp_to_string (info->tree, estimated_len);
549 gdb_assert (ret != NULL);
551 /* Free the parse information. */
552 cp_demangled_name_parse_free (info);
554 /* Finally, compare the original string with the computed
555 name, returning NULL if they are the same. */
556 if (strcmp (string, ret) == 0)
566 /* Like cp_canonicalize_string_full, but always passes NULL for
570 cp_canonicalize_string_no_typedefs (const char *string)
572 return cp_canonicalize_string_full (string, NULL, NULL);
575 /* Parse STRING and convert it to canonical form. If parsing fails,
576 or if STRING is already canonical, return NULL. Otherwise return
577 the canonical form. The return value is allocated via xmalloc. */
580 cp_canonicalize_string (const char *string)
582 struct demangle_parse_info *info;
583 unsigned int estimated_len;
586 if (cp_already_canonical (string))
589 info = cp_demangled_name_to_comp (string, NULL);
593 estimated_len = strlen (string) * 2;
594 ret = cp_comp_to_string (info->tree, estimated_len);
595 cp_demangled_name_parse_free (info);
599 warning (_("internal error: string \"%s\" failed to be canonicalized"),
604 if (strcmp (string, ret) == 0)
613 /* Convert a mangled name to a demangle_component tree. *MEMORY is
614 set to the block of used memory that should be freed when finished
615 with the tree. DEMANGLED_P is set to the char * that should be
616 freed when finished with the tree, or NULL if none was needed.
617 OPTIONS will be passed to the demangler. */
619 static struct demangle_parse_info *
620 mangled_name_to_comp (const char *mangled_name, int options,
621 void **memory, char **demangled_p)
623 char *demangled_name;
624 struct demangle_parse_info *info;
626 /* If it looks like a v3 mangled name, then try to go directly
628 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
630 struct demangle_component *ret;
632 ret = cplus_demangle_v3_components (mangled_name,
636 info = cp_new_demangle_parse_info ();
643 /* If it doesn't, or if that failed, then try to demangle the
645 demangled_name = gdb_demangle (mangled_name, options);
646 if (demangled_name == NULL)
649 /* If we could demangle the name, parse it to build the component
651 info = cp_demangled_name_to_comp (demangled_name, NULL);
655 xfree (demangled_name);
659 *demangled_p = demangled_name;
663 /* Return the name of the class containing method PHYSNAME. */
666 cp_class_name_from_physname (const char *physname)
668 void *storage = NULL;
669 char *demangled_name = NULL, *ret;
670 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
671 struct demangle_parse_info *info;
674 info = mangled_name_to_comp (physname, DMGL_ANSI,
675 &storage, &demangled_name);
680 ret_comp = info->tree;
682 /* First strip off any qualifiers, if we have a function or
685 switch (ret_comp->type)
687 case DEMANGLE_COMPONENT_CONST:
688 case DEMANGLE_COMPONENT_RESTRICT:
689 case DEMANGLE_COMPONENT_VOLATILE:
690 case DEMANGLE_COMPONENT_CONST_THIS:
691 case DEMANGLE_COMPONENT_RESTRICT_THIS:
692 case DEMANGLE_COMPONENT_VOLATILE_THIS:
693 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
694 ret_comp = d_left (ret_comp);
701 /* If what we have now is a function, discard the argument list. */
702 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
703 ret_comp = d_left (ret_comp);
705 /* If what we have now is a template, strip off the template
706 arguments. The left subtree may be a qualified name. */
707 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
708 ret_comp = d_left (ret_comp);
710 /* What we have now should be a name, possibly qualified.
711 Additional qualifiers could live in the left subtree or the right
712 subtree. Find the last piece. */
717 switch (cur_comp->type)
719 case DEMANGLE_COMPONENT_QUAL_NAME:
720 case DEMANGLE_COMPONENT_LOCAL_NAME:
721 prev_comp = cur_comp;
722 cur_comp = d_right (cur_comp);
724 case DEMANGLE_COMPONENT_TEMPLATE:
725 case DEMANGLE_COMPONENT_NAME:
726 case DEMANGLE_COMPONENT_CTOR:
727 case DEMANGLE_COMPONENT_DTOR:
728 case DEMANGLE_COMPONENT_OPERATOR:
729 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
739 if (cur_comp != NULL && prev_comp != NULL)
741 /* We want to discard the rightmost child of PREV_COMP. */
742 *prev_comp = *d_left (prev_comp);
743 /* The ten is completely arbitrary; we don't have a good
745 ret = cp_comp_to_string (ret_comp, 10);
749 xfree (demangled_name);
750 cp_demangled_name_parse_free (info);
754 /* Return the child of COMP which is the basename of a method,
755 variable, et cetera. All scope qualifiers are discarded, but
756 template arguments will be included. The component tree may be
759 static struct demangle_component *
760 unqualified_name_from_comp (struct demangle_component *comp)
762 struct demangle_component *ret_comp = comp, *last_template;
766 last_template = NULL;
768 switch (ret_comp->type)
770 case DEMANGLE_COMPONENT_QUAL_NAME:
771 case DEMANGLE_COMPONENT_LOCAL_NAME:
772 ret_comp = d_right (ret_comp);
774 case DEMANGLE_COMPONENT_TYPED_NAME:
775 ret_comp = d_left (ret_comp);
777 case DEMANGLE_COMPONENT_TEMPLATE:
778 gdb_assert (last_template == NULL);
779 last_template = ret_comp;
780 ret_comp = d_left (ret_comp);
782 case DEMANGLE_COMPONENT_CONST:
783 case DEMANGLE_COMPONENT_RESTRICT:
784 case DEMANGLE_COMPONENT_VOLATILE:
785 case DEMANGLE_COMPONENT_CONST_THIS:
786 case DEMANGLE_COMPONENT_RESTRICT_THIS:
787 case DEMANGLE_COMPONENT_VOLATILE_THIS:
788 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
789 ret_comp = d_left (ret_comp);
791 case DEMANGLE_COMPONENT_NAME:
792 case DEMANGLE_COMPONENT_CTOR:
793 case DEMANGLE_COMPONENT_DTOR:
794 case DEMANGLE_COMPONENT_OPERATOR:
795 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
805 d_left (last_template) = ret_comp;
806 return last_template;
812 /* Return the name of the method whose linkage name is PHYSNAME. */
815 method_name_from_physname (const char *physname)
817 void *storage = NULL;
818 char *demangled_name = NULL, *ret;
819 struct demangle_component *ret_comp;
820 struct demangle_parse_info *info;
822 info = mangled_name_to_comp (physname, DMGL_ANSI,
823 &storage, &demangled_name);
827 ret_comp = unqualified_name_from_comp (info->tree);
830 if (ret_comp != NULL)
831 /* The ten is completely arbitrary; we don't have a good
833 ret = cp_comp_to_string (ret_comp, 10);
836 xfree (demangled_name);
837 cp_demangled_name_parse_free (info);
841 /* If FULL_NAME is the demangled name of a C++ function (including an
842 arg list, possibly including namespace/class qualifications),
843 return a new string containing only the function name (without the
844 arg list/class qualifications). Otherwise, return NULL. The
845 caller is responsible for freeing the memory in question. */
848 cp_func_name (const char *full_name)
851 struct demangle_component *ret_comp;
852 struct demangle_parse_info *info;
854 info = cp_demangled_name_to_comp (full_name, NULL);
858 ret_comp = unqualified_name_from_comp (info->tree);
861 if (ret_comp != NULL)
862 ret = cp_comp_to_string (ret_comp, 10);
864 cp_demangled_name_parse_free (info);
868 /* DEMANGLED_NAME is the name of a function, including parameters and
869 (optionally) a return type. Return the name of the function without
870 parameters or return type, or NULL if we can not parse the name. */
873 cp_remove_params (const char *demangled_name)
876 struct demangle_component *ret_comp;
877 struct demangle_parse_info *info;
880 if (demangled_name == NULL)
883 info = cp_demangled_name_to_comp (demangled_name, NULL);
887 /* First strip off any qualifiers, if we have a function or method. */
888 ret_comp = info->tree;
890 switch (ret_comp->type)
892 case DEMANGLE_COMPONENT_CONST:
893 case DEMANGLE_COMPONENT_RESTRICT:
894 case DEMANGLE_COMPONENT_VOLATILE:
895 case DEMANGLE_COMPONENT_CONST_THIS:
896 case DEMANGLE_COMPONENT_RESTRICT_THIS:
897 case DEMANGLE_COMPONENT_VOLATILE_THIS:
898 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
899 ret_comp = d_left (ret_comp);
906 /* What we have now should be a function. Return its name. */
907 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
908 ret = cp_comp_to_string (d_left (ret_comp), 10);
910 cp_demangled_name_parse_free (info);
914 /* Here are some random pieces of trivia to keep in mind while trying
915 to take apart demangled names:
917 - Names can contain function arguments or templates, so the process
918 has to be, to some extent recursive: maybe keep track of your
919 depth based on encountering <> and ().
921 - Parentheses don't just have to happen at the end of a name: they
922 can occur even if the name in question isn't a function, because
923 a template argument might be a type that's a function.
925 - Conversely, even if you're trying to deal with a function, its
926 demangled name might not end with ')': it could be a const or
927 volatile class method, in which case it ends with "const" or
930 - Parentheses are also used in anonymous namespaces: a variable
931 'foo' in an anonymous namespace gets demangled as "(anonymous
934 - And operator names can contain parentheses or angle brackets. */
936 /* FIXME: carlton/2003-03-13: We have several functions here with
937 overlapping functionality; can we combine them? Also, do they
938 handle all the above considerations correctly? */
941 /* This returns the length of first component of NAME, which should be
942 the demangled name of a C++ variable/function/method/etc.
943 Specifically, it returns the index of the first colon forming the
944 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
945 it returns the 1, and given 'foo', it returns 0. */
947 /* The character in NAME indexed by the return value is guaranteed to
948 always be either ':' or '\0'. */
950 /* NOTE: carlton/2003-03-13: This function is currently only intended
951 for internal use: it's probably not entirely safe when called on
952 user-generated input, because some of the 'index += 2' lines in
953 cp_find_first_component_aux might go past the end of malformed
957 cp_find_first_component (const char *name)
959 return cp_find_first_component_aux (name, 0);
962 /* Helper function for cp_find_first_component. Like that function,
963 it returns the length of the first component of NAME, but to make
964 the recursion easier, it also stops if it reaches an unexpected ')'
965 or '>' if the value of PERMISSIVE is nonzero. */
967 /* Let's optimize away calls to strlen("operator"). */
969 #define LENGTH_OF_OPERATOR 8
972 cp_find_first_component_aux (const char *name, int permissive)
974 unsigned int index = 0;
975 /* Operator names can show up in unexpected places. Since these can
976 contain parentheses or angle brackets, they can screw up the
977 recursion. But not every string 'operator' is part of an
978 operater name: e.g. you could have a variable 'cooperator'. So
979 this variable tells us whether or not we should treat the string
980 'operator' as starting an operator. */
981 int operator_possible = 1;
988 /* Template; eat it up. The calls to cp_first_component
989 should only return (I hope!) when they reach the '>'
990 terminating the component or a '::' between two
991 components. (Hence the '+ 2'.) */
993 for (index += cp_find_first_component_aux (name + index, 1);
995 index += cp_find_first_component_aux (name + index, 1))
997 if (name[index] != ':')
999 demangled_name_complaint (name);
1000 return strlen (name);
1004 operator_possible = 1;
1007 /* Similar comment as to '<'. */
1009 for (index += cp_find_first_component_aux (name + index, 1);
1011 index += cp_find_first_component_aux (name + index, 1))
1013 if (name[index] != ':')
1015 demangled_name_complaint (name);
1016 return strlen (name);
1020 operator_possible = 1;
1028 demangled_name_complaint (name);
1029 return strlen (name);
1035 /* Operator names can screw up the recursion. */
1036 if (operator_possible
1037 && strncmp (name + index, "operator",
1038 LENGTH_OF_OPERATOR) == 0)
1040 index += LENGTH_OF_OPERATOR;
1041 while (ISSPACE(name[index]))
1043 switch (name[index])
1045 /* Skip over one less than the appropriate number of
1046 characters: the for loop will skip over the last
1049 if (name[index + 1] == '<')
1056 if (name[index + 1] == '>')
1069 operator_possible = 0;
1076 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1077 set of relevant characters are here: it's necessary to
1078 include any character that can show up before 'operator'
1079 in a demangled name, and it's safe to include any
1080 character that can't be part of an identifier's name. */
1081 operator_possible = 1;
1084 operator_possible = 0;
1090 /* Complain about a demangled name that we don't know how to parse.
1091 NAME is the demangled name in question. */
1094 demangled_name_complaint (const char *name)
1096 complaint (&symfile_complaints,
1097 "unexpected demangled name '%s'", name);
1100 /* If NAME is the fully-qualified name of a C++
1101 function/variable/method/etc., this returns the length of its
1102 entire prefix: all of the namespaces and classes that make up its
1103 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1104 4, given 'foo', it returns 0. */
1107 cp_entire_prefix_len (const char *name)
1109 unsigned int current_len = cp_find_first_component (name);
1110 unsigned int previous_len = 0;
1112 while (name[current_len] != '\0')
1114 gdb_assert (name[current_len] == ':');
1115 previous_len = current_len;
1116 /* Skip the '::'. */
1118 current_len += cp_find_first_component (name + current_len);
1121 return previous_len;
1124 /* Overload resolution functions. */
1126 /* Test to see if SYM is a symbol that we haven't seen corresponding
1127 to a function named OLOAD_NAME. If so, add it to the current
1131 overload_list_add_symbol (struct symbol *sym,
1132 const char *oload_name)
1138 /* If there is no type information, we can't do anything, so
1140 if (SYMBOL_TYPE (sym) == NULL)
1143 /* skip any symbols that we've already considered. */
1144 for (i = 0; i < sym_return_val_index; ++i)
1145 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1146 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1149 /* Get the demangled name without parameters */
1150 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1154 /* skip symbols that cannot match */
1155 if (strcmp (sym_name, oload_name) != 0)
1163 /* We have a match for an overload instance, so add SYM to the
1164 current list of overload instances */
1165 if (sym_return_val_index + 3 > sym_return_val_size)
1167 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1168 sym_return_val = (struct symbol **)
1169 xrealloc ((char *) sym_return_val, newsize);
1171 sym_return_val[sym_return_val_index++] = sym;
1172 sym_return_val[sym_return_val_index] = NULL;
1175 /* Return a null-terminated list of pointers to function symbols that
1176 are named FUNC_NAME and are visible within NAMESPACE. */
1179 make_symbol_overload_list (const char *func_name,
1180 const char *namespace)
1182 struct cleanup *old_cleanups;
1185 sym_return_val_size = 100;
1186 sym_return_val_index = 0;
1187 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1188 sizeof (struct symbol *));
1189 sym_return_val[0] = NULL;
1191 old_cleanups = make_cleanup (xfree, sym_return_val);
1193 make_symbol_overload_list_using (func_name, namespace);
1195 if (namespace[0] == '\0')
1199 char *concatenated_name
1200 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1201 strcpy (concatenated_name, namespace);
1202 strcat (concatenated_name, "::");
1203 strcat (concatenated_name, func_name);
1204 name = concatenated_name;
1207 make_symbol_overload_list_qualified (name);
1209 discard_cleanups (old_cleanups);
1211 return sym_return_val;
1214 /* Add all symbols with a name matching NAME in BLOCK to the overload
1218 make_symbol_overload_list_block (const char *name,
1219 const struct block *block)
1221 struct block_iterator iter;
1224 for (sym = block_iter_name_first (block, name, &iter);
1226 sym = block_iter_name_next (name, &iter))
1227 overload_list_add_symbol (sym, name);
1230 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1233 make_symbol_overload_list_namespace (const char *func_name,
1234 const char *namespace)
1237 const struct block *block = NULL;
1239 if (namespace[0] == '\0')
1243 char *concatenated_name
1244 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1246 strcpy (concatenated_name, namespace);
1247 strcat (concatenated_name, "::");
1248 strcat (concatenated_name, func_name);
1249 name = concatenated_name;
1252 /* Look in the static block. */
1253 block = block_static_block (get_selected_block (0));
1255 make_symbol_overload_list_block (name, block);
1257 /* Look in the global block. */
1258 block = block_global_block (block);
1260 make_symbol_overload_list_block (name, block);
1264 /* Search the namespace of the given type and namespace of and public
1268 make_symbol_overload_list_adl_namespace (struct type *type,
1269 const char *func_name)
1272 const char *type_name;
1275 while (TYPE_CODE (type) == TYPE_CODE_PTR
1276 || TYPE_CODE (type) == TYPE_CODE_REF
1277 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1278 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1280 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1281 type = check_typedef(type);
1283 type = TYPE_TARGET_TYPE (type);
1286 type_name = TYPE_NAME (type);
1288 if (type_name == NULL)
1291 prefix_len = cp_entire_prefix_len (type_name);
1293 if (prefix_len != 0)
1295 namespace = alloca (prefix_len + 1);
1296 strncpy (namespace, type_name, prefix_len);
1297 namespace[prefix_len] = '\0';
1299 make_symbol_overload_list_namespace (func_name, namespace);
1302 /* Check public base type */
1303 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1304 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1306 if (BASETYPE_VIA_PUBLIC (type, i))
1307 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1313 /* Adds the overload list overload candidates for FUNC_NAME found
1314 through argument dependent lookup. */
1317 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1318 const char *func_name)
1322 gdb_assert (sym_return_val_size != -1);
1324 for (i = 1; i <= nargs; i++)
1325 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1328 return sym_return_val;
1331 /* Used for cleanups to reset the "searched" flag in case of an
1335 reset_directive_searched (void *data)
1337 struct using_direct *direct = data;
1338 direct->searched = 0;
1341 /* This applies the using directives to add namespaces to search in,
1342 and then searches for overloads in all of those namespaces. It
1343 adds the symbols found to sym_return_val. Arguments are as in
1344 make_symbol_overload_list. */
1347 make_symbol_overload_list_using (const char *func_name,
1348 const char *namespace)
1350 struct using_direct *current;
1351 const struct block *block;
1353 /* First, go through the using directives. If any of them apply,
1354 look in the appropriate namespaces for new functions to match
1357 for (block = get_selected_block (0);
1359 block = BLOCK_SUPERBLOCK (block))
1360 for (current = block_using (block);
1362 current = current->next)
1364 /* Prevent recursive calls. */
1365 if (current->searched)
1368 /* If this is a namespace alias or imported declaration ignore
1370 if (current->alias != NULL || current->declaration != NULL)
1373 if (strcmp (namespace, current->import_dest) == 0)
1375 /* Mark this import as searched so that the recursive call
1376 does not search it again. */
1377 struct cleanup *old_chain;
1378 current->searched = 1;
1379 old_chain = make_cleanup (reset_directive_searched,
1382 make_symbol_overload_list_using (func_name,
1383 current->import_src);
1385 current->searched = 0;
1386 discard_cleanups (old_chain);
1390 /* Now, add names for this namespace. */
1391 make_symbol_overload_list_namespace (func_name, namespace);
1394 /* This does the bulk of the work of finding overloaded symbols.
1395 FUNC_NAME is the name of the overloaded function we're looking for
1396 (possibly including namespace info). */
1399 make_symbol_overload_list_qualified (const char *func_name)
1402 struct objfile *objfile;
1403 const struct block *b, *surrounding_static_block = 0;
1405 /* Look through the partial symtabs for all symbols which begin by
1406 matching FUNC_NAME. Make sure we read that symbol table in. */
1408 ALL_OBJFILES (objfile)
1411 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1414 /* Search upwards from currently selected frame (so that we can
1415 complete on local vars. */
1417 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1418 make_symbol_overload_list_block (func_name, b);
1420 surrounding_static_block = block_static_block (get_selected_block (0));
1422 /* Go through the symtabs and check the externs and statics for
1423 symbols which match. */
1425 ALL_PRIMARY_SYMTABS (objfile, s)
1428 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1429 make_symbol_overload_list_block (func_name, b);
1432 ALL_PRIMARY_SYMTABS (objfile, s)
1435 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1436 /* Don't do this block twice. */
1437 if (b == surrounding_static_block)
1439 make_symbol_overload_list_block (func_name, b);
1443 /* Lookup the rtti type for a class name. */
1446 cp_lookup_rtti_type (const char *name, struct block *block)
1448 struct symbol * rtti_sym;
1449 struct type * rtti_type;
1451 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1453 if (rtti_sym == NULL)
1455 warning (_("RTTI symbol not found for class '%s'"), name);
1459 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1461 warning (_("RTTI symbol for class '%s' is not a type"), name);
1465 rtti_type = SYMBOL_TYPE (rtti_sym);
1467 switch (TYPE_CODE (rtti_type))
1469 case TYPE_CODE_CLASS:
1471 case TYPE_CODE_NAMESPACE:
1472 /* chastain/2003-11-26: the symbol tables often contain fake
1473 symbols for namespaces with the same name as the struct.
1474 This warning is an indication of a bug in the lookup order
1475 or a bug in the way that the symbol tables are populated. */
1476 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1479 warning (_("RTTI symbol for class '%s' has bad type"), name);
1486 #ifdef HAVE_WORKING_FORK
1488 /* If nonzero, attempt to catch crashes in the demangler and print
1489 useful debugging information. */
1491 static int catch_demangler_crashes = 1;
1493 /* Wrap set/long jmp so that it's more portable. */
1495 #if defined(HAVE_SIGSETJMP)
1496 #define SIGJMP_BUF sigjmp_buf
1497 #define SIGSETJMP(buf) sigsetjmp((buf), 1)
1498 #define SIGLONGJMP(buf,val) siglongjmp((buf), (val))
1500 #define SIGJMP_BUF jmp_buf
1501 #define SIGSETJMP(buf) setjmp(buf)
1502 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
1505 /* Stack context and environment for demangler crash recovery. */
1507 static SIGJMP_BUF gdb_demangle_jmp_buf;
1509 /* If nonzero, attempt to dump core from the signal handler. */
1511 static int gdb_demangle_attempt_core_dump = 1;
1513 /* Signal handler for gdb_demangle. */
1516 gdb_demangle_signal_handler (int signo)
1518 if (gdb_demangle_attempt_core_dump)
1523 gdb_demangle_attempt_core_dump = 0;
1526 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1531 /* A wrapper for bfd_demangle. */
1534 gdb_demangle (const char *name, int options)
1536 char *result = NULL;
1537 int crash_signal = 0;
1539 #ifdef HAVE_WORKING_FORK
1540 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1541 struct sigaction sa, old_sa;
1545 static int core_dump_allowed = -1;
1547 if (core_dump_allowed == -1)
1549 core_dump_allowed = can_dump_core (LIMIT_CUR);
1551 if (!core_dump_allowed)
1552 gdb_demangle_attempt_core_dump = 0;
1555 if (catch_demangler_crashes)
1557 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1558 sa.sa_handler = gdb_demangle_signal_handler;
1559 sigemptyset (&sa.sa_mask);
1560 sa.sa_flags = SA_ONSTACK;
1561 sigaction (SIGSEGV, &sa, &old_sa);
1563 ofunc = (void (*)()) signal (SIGSEGV, gdb_demangle_signal_handler);
1566 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1570 if (crash_signal == 0)
1571 result = bfd_demangle (NULL, name, options);
1573 #ifdef HAVE_WORKING_FORK
1574 if (catch_demangler_crashes)
1576 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1577 sigaction (SIGSEGV, &old_sa, NULL);
1579 signal (SIGSEGV, ofunc);
1582 if (crash_signal != 0)
1584 static int error_reported = 0;
1586 if (!error_reported)
1588 char *short_msg, *long_msg;
1589 struct cleanup *back_to;
1591 short_msg = xstrprintf (_("unable to demangle '%s' "
1592 "(demangler failed with signal %d)"),
1593 name, crash_signal);
1594 back_to = make_cleanup (xfree, short_msg);
1596 long_msg = xstrprintf ("%s:%d: %s: %s", __FILE__, __LINE__,
1597 "demangler-warning", short_msg);
1598 make_cleanup (xfree, long_msg);
1600 target_terminal_ours ();
1602 if (core_dump_allowed)
1603 fprintf_unfiltered (gdb_stderr,
1604 _("%s\nAttempting to dump core.\n"),
1607 warn_cant_dump_core (long_msg);
1609 demangler_warning (__FILE__, __LINE__, "%s", short_msg);
1611 do_cleanups (back_to);
1624 /* Don't allow just "maintenance cplus". */
1627 maint_cplus_command (char *arg, int from_tty)
1629 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1630 "by the name of a command.\n"));
1631 help_list (maint_cplus_cmd_list,
1632 "maintenance cplus ",
1633 all_commands, gdb_stdout);
1636 /* This is a front end for cp_find_first_component, for unit testing.
1637 Be careful when using it: see the NOTE above
1638 cp_find_first_component. */
1641 first_component_command (char *arg, int from_tty)
1649 len = cp_find_first_component (arg);
1650 prefix = alloca (len + 1);
1652 memcpy (prefix, arg, len);
1655 printf_unfiltered ("%s\n", prefix);
1658 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1661 /* Implement "info vtbl". */
1664 info_vtbl_command (char *arg, int from_tty)
1666 struct value *value;
1668 value = parse_and_eval (arg);
1669 cplus_print_vtable (value);
1673 _initialize_cp_support (void)
1675 add_prefix_cmd ("cplus", class_maintenance,
1676 maint_cplus_command,
1677 _("C++ maintenance commands."),
1678 &maint_cplus_cmd_list,
1679 "maintenance cplus ",
1680 0, &maintenancelist);
1681 add_alias_cmd ("cp", "cplus",
1682 class_maintenance, 1,
1685 add_cmd ("first_component",
1687 first_component_command,
1688 _("Print the first class/namespace component of NAME."),
1689 &maint_cplus_cmd_list);
1691 add_info ("vtbl", info_vtbl_command,
1692 _("Show the virtual function table for a C++ object.\n\
1693 Usage: info vtbl EXPRESSION\n\
1694 Evaluate EXPRESSION and display the virtual function table for the\n\
1695 resulting object."));
1697 #ifdef HAVE_WORKING_FORK
1698 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
1699 &catch_demangler_crashes, _("\
1700 Set whether to attempt to catch demangler crashes."), _("\
1701 Show whether to attempt to catch demangler crashes."), _("\
1702 If enabled GDB will attempt to catch demangler crashes and\n\
1703 display the offending symbol."),
1706 &maintenance_set_cmdlist,
1707 &maintenance_show_cmdlist);