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"
37 #include "safe-ctype.h"
39 #define d_left(dc) (dc)->u.s_binary.left
40 #define d_right(dc) (dc)->u.s_binary.right
42 /* Functions related to demangled name parsing. */
44 static unsigned int cp_find_first_component_aux (const char *name,
47 static void demangled_name_complaint (const char *name);
49 /* Functions/variables related to overload resolution. */
51 static int sym_return_val_size = -1;
52 static int sym_return_val_index;
53 static struct symbol **sym_return_val;
55 static void overload_list_add_symbol (struct symbol *sym,
56 const char *oload_name);
58 static void make_symbol_overload_list_using (const char *func_name,
59 const char *the_namespace);
61 static void make_symbol_overload_list_qualified (const char *func_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 /* A list of typedefs which should not be substituted by replace_typedefs. */
73 static const char * const ignore_typedefs[] =
75 "std::istream", "std::iostream", "std::ostream", "std::string"
79 replace_typedefs (struct demangle_parse_info *info,
80 struct demangle_component *ret_comp,
81 canonicalization_ftype *finder,
84 /* A convenience function to copy STRING into OBSTACK, returning a pointer
85 to the newly allocated string and saving the number of bytes saved in LEN.
87 It does not copy the terminating '\0' byte! */
90 copy_string_to_obstack (struct obstack *obstack, const char *string,
93 *len = strlen (string);
94 return obstack_copy (obstack, string, *len);
97 /* A cleanup wrapper for cp_demangled_name_parse_free. */
100 do_demangled_name_parse_free_cleanup (void *data)
102 struct demangle_parse_info *info = (struct demangle_parse_info *) data;
104 cp_demangled_name_parse_free (info);
107 /* Create a cleanup for C++ name parsing. */
110 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
112 return make_cleanup (do_demangled_name_parse_free_cleanup, info);
115 /* Return 1 if STRING is clearly already in canonical form. This
116 function is conservative; things which it does not recognize are
117 assumed to be non-canonical, and the parser will sort them out
118 afterwards. This speeds up the critical path for alphanumeric
122 cp_already_canonical (const char *string)
124 /* Identifier start character [a-zA-Z_]. */
125 if (!ISIDST (string[0]))
128 /* These are the only two identifiers which canonicalize to other
129 than themselves or an error: unsigned -> unsigned int and
131 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
133 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
136 /* Identifier character [a-zA-Z0-9_]. */
137 while (ISIDNUM (string[1]))
140 if (string[1] == '\0')
146 /* Inspect the given RET_COMP for its type. If it is a typedef,
147 replace the node with the typedef's tree.
149 Returns 1 if any typedef substitutions were made, 0 otherwise. */
152 inspect_type (struct demangle_parse_info *info,
153 struct demangle_component *ret_comp,
154 canonicalization_ftype *finder,
160 volatile struct gdb_exception except;
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)
176 TRY_CATCH (except, RETURN_MASK_ALL)
178 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
181 if (except.reason >= 0 && sym != NULL)
183 struct type *otype = SYMBOL_TYPE (sym);
187 const char *new_name = (*finder) (otype, data);
189 if (new_name != NULL)
191 ret_comp->u.s_name.s = new_name;
192 ret_comp->u.s_name.len = strlen (new_name);
199 /* If the type is a typedef or namespace alias, replace it. */
200 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
201 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
206 struct demangle_parse_info *i;
209 /* Get the real type of the typedef. */
210 type = check_typedef (otype);
212 /* If the symbol is a namespace and its type name is no different
213 than the name we looked up, this symbol is not a namespace
214 alias and does not need to be substituted. */
215 if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
216 && strcmp (TYPE_NAME (type), name) == 0)
219 is_anon = (TYPE_TAG_NAME (type) == NULL
220 && (TYPE_CODE (type) == TYPE_CODE_ENUM
221 || TYPE_CODE (type) == TYPE_CODE_STRUCT
222 || TYPE_CODE (type) == TYPE_CODE_UNION));
225 struct type *last = otype;
227 /* Find the last typedef for the type. */
228 while (TYPE_TARGET_TYPE (last) != NULL
229 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
230 == TYPE_CODE_TYPEDEF))
231 last = TYPE_TARGET_TYPE (last);
233 /* If there is only one typedef for this anonymous type,
234 do not substitute it. */
238 /* Use the last typedef seen as the type for this
243 buf = mem_fileopen ();
244 TRY_CATCH (except, RETURN_MASK_ERROR)
246 type_print (type, "", buf, -1);
249 /* If type_print threw an exception, there is little point
250 in continuing, so just bow out gracefully. */
251 if (except.reason < 0)
253 ui_file_delete (buf);
257 name = ui_file_obsavestring (buf, &info->obstack, &len);
258 ui_file_delete (buf);
260 /* Turn the result into a new tree. Note that this
261 tree will contain pointers into NAME, so NAME cannot
262 be free'd until all typedef conversion is done and
263 the final result is converted into a string. */
264 i = cp_demangled_name_to_comp (name, NULL);
267 /* Merge the two trees. */
268 cp_merge_demangle_parse_infos (info, ret_comp, i);
270 /* Replace any newly introduced typedefs -- but not
271 if the type is anonymous (that would lead to infinite
274 replace_typedefs (info, ret_comp, finder, data);
278 /* This shouldn't happen unless the type printer has
279 output something that the name parser cannot grok.
280 Nonetheless, an ounce of prevention...
282 Canonicalize the name again, and store it in the
283 current node (RET_COMP). */
284 char *canon = cp_canonicalize_string_no_typedefs (name);
288 /* Copy the canonicalization into the obstack and
290 name = copy_string_to_obstack (&info->obstack, canon, &len);
294 ret_comp->u.s_name.s = name;
295 ret_comp->u.s_name.len = len;
305 /* Replace any typedefs appearing in the qualified name
306 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
310 replace_typedefs_qualified_name (struct demangle_parse_info *info,
311 struct demangle_component *ret_comp,
312 canonicalization_ftype *finder,
317 struct ui_file *buf = mem_fileopen ();
318 struct demangle_component *comp = ret_comp;
320 /* Walk each node of the qualified name, reconstructing the name of
321 this element. With every node, check for any typedef substitutions.
322 If a substitution has occurred, replace the qualified name node
323 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
325 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
327 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
329 struct demangle_component newobj;
331 ui_file_write (buf, d_left (comp)->u.s_name.s,
332 d_left (comp)->u.s_name.len);
333 name = ui_file_obsavestring (buf, &info->obstack, &len);
334 newobj.type = DEMANGLE_COMPONENT_NAME;
335 newobj.u.s_name.s = name;
336 newobj.u.s_name.len = len;
337 if (inspect_type (info, &newobj, finder, data))
342 /* A typedef was substituted in NEW. Convert it to a
343 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
346 ui_file_rewind (buf);
347 n = cp_comp_to_string (&newobj, 100);
350 /* If something went astray, abort typedef substitutions. */
351 ui_file_delete (buf);
355 s = copy_string_to_obstack (&info->obstack, n, &slen);
358 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
359 d_left (ret_comp)->u.s_name.s = s;
360 d_left (ret_comp)->u.s_name.len = slen;
361 d_right (ret_comp) = d_right (comp);
368 /* The current node is not a name, so simply replace any
369 typedefs in it. Then print it to the stream to continue
370 checking for more typedefs in the tree. */
371 replace_typedefs (info, d_left (comp), finder, data);
372 name = cp_comp_to_string (d_left (comp), 100);
375 /* If something went astray, abort typedef substitutions. */
376 ui_file_delete (buf);
379 fputs_unfiltered (name, buf);
383 ui_file_write (buf, "::", 2);
384 comp = d_right (comp);
387 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
388 name assembled above and append the name given by COMP. Then use this
389 reassembled name to check for a typedef. */
391 if (comp->type == DEMANGLE_COMPONENT_NAME)
393 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
394 name = ui_file_obsavestring (buf, &info->obstack, &len);
396 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
397 with a DEMANGLE_COMPONENT_NAME node containing the whole
399 ret_comp->type = DEMANGLE_COMPONENT_NAME;
400 ret_comp->u.s_name.s = name;
401 ret_comp->u.s_name.len = len;
402 inspect_type (info, ret_comp, finder, data);
405 replace_typedefs (info, comp, finder, data);
407 ui_file_delete (buf);
411 /* A function to check const and volatile qualifiers for argument types.
413 "Parameter declarations that differ only in the presence
414 or absence of `const' and/or `volatile' are equivalent."
415 C++ Standard N3290, clause 13.1.3 #4. */
418 check_cv_qualifiers (struct demangle_component *ret_comp)
420 while (d_left (ret_comp) != NULL
421 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
422 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
424 d_left (ret_comp) = d_left (d_left (ret_comp));
428 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
429 their basic types. */
432 replace_typedefs (struct demangle_parse_info *info,
433 struct demangle_component *ret_comp,
434 canonicalization_ftype *finder,
440 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
441 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
442 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
443 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
445 char *local_name = cp_comp_to_string (ret_comp, 10);
447 if (local_name != NULL)
450 volatile struct gdb_exception except;
453 TRY_CATCH (except, RETURN_MASK_ALL)
455 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0);
459 if (except.reason >= 0 && sym != NULL)
461 struct type *otype = SYMBOL_TYPE (sym);
462 const char *new_name = (*finder) (otype, data);
464 if (new_name != NULL)
466 ret_comp->type = DEMANGLE_COMPONENT_NAME;
467 ret_comp->u.s_name.s = new_name;
468 ret_comp->u.s_name.len = strlen (new_name);
475 switch (ret_comp->type)
477 case DEMANGLE_COMPONENT_ARGLIST:
478 check_cv_qualifiers (ret_comp);
481 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
482 case DEMANGLE_COMPONENT_TEMPLATE:
483 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
484 case DEMANGLE_COMPONENT_TYPED_NAME:
485 replace_typedefs (info, d_left (ret_comp), finder, data);
486 replace_typedefs (info, d_right (ret_comp), finder, data);
489 case DEMANGLE_COMPONENT_NAME:
490 inspect_type (info, ret_comp, finder, data);
493 case DEMANGLE_COMPONENT_QUAL_NAME:
494 replace_typedefs_qualified_name (info, ret_comp, finder, data);
497 case DEMANGLE_COMPONENT_LOCAL_NAME:
498 case DEMANGLE_COMPONENT_CTOR:
499 case DEMANGLE_COMPONENT_ARRAY_TYPE:
500 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
501 replace_typedefs (info, d_right (ret_comp), finder, data);
504 case DEMANGLE_COMPONENT_CONST:
505 case DEMANGLE_COMPONENT_RESTRICT:
506 case DEMANGLE_COMPONENT_VOLATILE:
507 case DEMANGLE_COMPONENT_VOLATILE_THIS:
508 case DEMANGLE_COMPONENT_CONST_THIS:
509 case DEMANGLE_COMPONENT_RESTRICT_THIS:
510 case DEMANGLE_COMPONENT_POINTER:
511 case DEMANGLE_COMPONENT_REFERENCE:
512 replace_typedefs (info, d_left (ret_comp), finder, data);
521 /* Parse STRING and convert it to canonical form, resolving any typedefs.
522 If parsing fails, or if STRING is already canonical, return NULL.
523 Otherwise return the canonical form. The return value is allocated via
524 xmalloc. If FINDER is not NULL, then type components are passed to
525 FINDER to be looked up. DATA is passed verbatim to FINDER. */
528 cp_canonicalize_string_full (const char *string,
529 canonicalization_ftype *finder,
533 unsigned int estimated_len;
534 struct demangle_parse_info *info;
537 estimated_len = strlen (string) * 2;
538 info = cp_demangled_name_to_comp (string, NULL);
541 /* Replace all the typedefs in the tree. */
542 replace_typedefs (info, info->tree, finder, data);
544 /* Convert the tree back into a string. */
545 ret = cp_comp_to_string (info->tree, estimated_len);
546 gdb_assert (ret != NULL);
548 /* Free the parse information. */
549 cp_demangled_name_parse_free (info);
551 /* Finally, compare the original string with the computed
552 name, returning NULL if they are the same. */
553 if (strcmp (string, ret) == 0)
563 /* Like cp_canonicalize_string_full, but always passes NULL for
567 cp_canonicalize_string_no_typedefs (const char *string)
569 return cp_canonicalize_string_full (string, NULL, NULL);
572 /* Parse STRING and convert it to canonical form. If parsing fails,
573 or if STRING is already canonical, return NULL. Otherwise return
574 the canonical form. The return value is allocated via xmalloc. */
577 cp_canonicalize_string (const char *string)
579 struct demangle_parse_info *info;
580 unsigned int estimated_len;
583 if (cp_already_canonical (string))
586 info = cp_demangled_name_to_comp (string, NULL);
590 estimated_len = strlen (string) * 2;
591 ret = cp_comp_to_string (info->tree, estimated_len);
592 cp_demangled_name_parse_free (info);
596 warning (_("internal error: string \"%s\" failed to be canonicalized"),
601 if (strcmp (string, ret) == 0)
610 /* Convert a mangled name to a demangle_component tree. *MEMORY is
611 set to the block of used memory that should be freed when finished
612 with the tree. DEMANGLED_P is set to the char * that should be
613 freed when finished with the tree, or NULL if none was needed.
614 OPTIONS will be passed to the demangler. */
616 static struct demangle_parse_info *
617 mangled_name_to_comp (const char *mangled_name, int options,
618 void **memory, char **demangled_p)
620 char *demangled_name;
621 struct demangle_parse_info *info;
623 /* If it looks like a v3 mangled name, then try to go directly
625 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
627 struct demangle_component *ret;
629 ret = cplus_demangle_v3_components (mangled_name,
633 info = cp_new_demangle_parse_info ();
640 /* If it doesn't, or if that failed, then try to demangle the
642 demangled_name = gdb_demangle (mangled_name, options);
643 if (demangled_name == NULL)
646 /* If we could demangle the name, parse it to build the component
648 info = cp_demangled_name_to_comp (demangled_name, NULL);
652 xfree (demangled_name);
656 *demangled_p = demangled_name;
660 /* Return the name of the class containing method PHYSNAME. */
663 cp_class_name_from_physname (const char *physname)
665 void *storage = NULL;
666 char *demangled_name = NULL, *ret;
667 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
668 struct demangle_parse_info *info;
671 info = mangled_name_to_comp (physname, DMGL_ANSI,
672 &storage, &demangled_name);
677 ret_comp = info->tree;
679 /* First strip off any qualifiers, if we have a function or
682 switch (ret_comp->type)
684 case DEMANGLE_COMPONENT_CONST:
685 case DEMANGLE_COMPONENT_RESTRICT:
686 case DEMANGLE_COMPONENT_VOLATILE:
687 case DEMANGLE_COMPONENT_CONST_THIS:
688 case DEMANGLE_COMPONENT_RESTRICT_THIS:
689 case DEMANGLE_COMPONENT_VOLATILE_THIS:
690 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
691 ret_comp = d_left (ret_comp);
698 /* If what we have now is a function, discard the argument list. */
699 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
700 ret_comp = d_left (ret_comp);
702 /* If what we have now is a template, strip off the template
703 arguments. The left subtree may be a qualified name. */
704 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
705 ret_comp = d_left (ret_comp);
707 /* What we have now should be a name, possibly qualified.
708 Additional qualifiers could live in the left subtree or the right
709 subtree. Find the last piece. */
714 switch (cur_comp->type)
716 case DEMANGLE_COMPONENT_QUAL_NAME:
717 case DEMANGLE_COMPONENT_LOCAL_NAME:
718 prev_comp = cur_comp;
719 cur_comp = d_right (cur_comp);
721 case DEMANGLE_COMPONENT_TEMPLATE:
722 case DEMANGLE_COMPONENT_NAME:
723 case DEMANGLE_COMPONENT_CTOR:
724 case DEMANGLE_COMPONENT_DTOR:
725 case DEMANGLE_COMPONENT_OPERATOR:
726 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
736 if (cur_comp != NULL && prev_comp != NULL)
738 /* We want to discard the rightmost child of PREV_COMP. */
739 *prev_comp = *d_left (prev_comp);
740 /* The ten is completely arbitrary; we don't have a good
742 ret = cp_comp_to_string (ret_comp, 10);
746 xfree (demangled_name);
747 cp_demangled_name_parse_free (info);
751 /* Return the child of COMP which is the basename of a method,
752 variable, et cetera. All scope qualifiers are discarded, but
753 template arguments will be included. The component tree may be
756 static struct demangle_component *
757 unqualified_name_from_comp (struct demangle_component *comp)
759 struct demangle_component *ret_comp = comp, *last_template;
763 last_template = NULL;
765 switch (ret_comp->type)
767 case DEMANGLE_COMPONENT_QUAL_NAME:
768 case DEMANGLE_COMPONENT_LOCAL_NAME:
769 ret_comp = d_right (ret_comp);
771 case DEMANGLE_COMPONENT_TYPED_NAME:
772 ret_comp = d_left (ret_comp);
774 case DEMANGLE_COMPONENT_TEMPLATE:
775 gdb_assert (last_template == NULL);
776 last_template = ret_comp;
777 ret_comp = d_left (ret_comp);
779 case DEMANGLE_COMPONENT_CONST:
780 case DEMANGLE_COMPONENT_RESTRICT:
781 case DEMANGLE_COMPONENT_VOLATILE:
782 case DEMANGLE_COMPONENT_CONST_THIS:
783 case DEMANGLE_COMPONENT_RESTRICT_THIS:
784 case DEMANGLE_COMPONENT_VOLATILE_THIS:
785 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
786 ret_comp = d_left (ret_comp);
788 case DEMANGLE_COMPONENT_NAME:
789 case DEMANGLE_COMPONENT_CTOR:
790 case DEMANGLE_COMPONENT_DTOR:
791 case DEMANGLE_COMPONENT_OPERATOR:
792 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
802 d_left (last_template) = ret_comp;
803 return last_template;
809 /* Return the name of the method whose linkage name is PHYSNAME. */
812 method_name_from_physname (const char *physname)
814 void *storage = NULL;
815 char *demangled_name = NULL, *ret;
816 struct demangle_component *ret_comp;
817 struct demangle_parse_info *info;
819 info = mangled_name_to_comp (physname, DMGL_ANSI,
820 &storage, &demangled_name);
824 ret_comp = unqualified_name_from_comp (info->tree);
827 if (ret_comp != NULL)
828 /* The ten is completely arbitrary; we don't have a good
830 ret = cp_comp_to_string (ret_comp, 10);
833 xfree (demangled_name);
834 cp_demangled_name_parse_free (info);
838 /* If FULL_NAME is the demangled name of a C++ function (including an
839 arg list, possibly including namespace/class qualifications),
840 return a new string containing only the function name (without the
841 arg list/class qualifications). Otherwise, return NULL. The
842 caller is responsible for freeing the memory in question. */
845 cp_func_name (const char *full_name)
848 struct demangle_component *ret_comp;
849 struct demangle_parse_info *info;
851 info = cp_demangled_name_to_comp (full_name, NULL);
855 ret_comp = unqualified_name_from_comp (info->tree);
858 if (ret_comp != NULL)
859 ret = cp_comp_to_string (ret_comp, 10);
861 cp_demangled_name_parse_free (info);
865 /* DEMANGLED_NAME is the name of a function, including parameters and
866 (optionally) a return type. Return the name of the function without
867 parameters or return type, or NULL if we can not parse the name. */
870 cp_remove_params (const char *demangled_name)
873 struct demangle_component *ret_comp;
874 struct demangle_parse_info *info;
877 if (demangled_name == NULL)
880 info = cp_demangled_name_to_comp (demangled_name, NULL);
884 /* First strip off any qualifiers, if we have a function or method. */
885 ret_comp = info->tree;
887 switch (ret_comp->type)
889 case DEMANGLE_COMPONENT_CONST:
890 case DEMANGLE_COMPONENT_RESTRICT:
891 case DEMANGLE_COMPONENT_VOLATILE:
892 case DEMANGLE_COMPONENT_CONST_THIS:
893 case DEMANGLE_COMPONENT_RESTRICT_THIS:
894 case DEMANGLE_COMPONENT_VOLATILE_THIS:
895 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
896 ret_comp = d_left (ret_comp);
903 /* What we have now should be a function. Return its name. */
904 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
905 ret = cp_comp_to_string (d_left (ret_comp), 10);
907 cp_demangled_name_parse_free (info);
911 /* Here are some random pieces of trivia to keep in mind while trying
912 to take apart demangled names:
914 - Names can contain function arguments or templates, so the process
915 has to be, to some extent recursive: maybe keep track of your
916 depth based on encountering <> and ().
918 - Parentheses don't just have to happen at the end of a name: they
919 can occur even if the name in question isn't a function, because
920 a template argument might be a type that's a function.
922 - Conversely, even if you're trying to deal with a function, its
923 demangled name might not end with ')': it could be a const or
924 volatile class method, in which case it ends with "const" or
927 - Parentheses are also used in anonymous namespaces: a variable
928 'foo' in an anonymous namespace gets demangled as "(anonymous
931 - And operator names can contain parentheses or angle brackets. */
933 /* FIXME: carlton/2003-03-13: We have several functions here with
934 overlapping functionality; can we combine them? Also, do they
935 handle all the above considerations correctly? */
938 /* This returns the length of first component of NAME, which should be
939 the demangled name of a C++ variable/function/method/etc.
940 Specifically, it returns the index of the first colon forming the
941 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
942 it returns the 1, and given 'foo', it returns 0. */
944 /* The character in NAME indexed by the return value is guaranteed to
945 always be either ':' or '\0'. */
947 /* NOTE: carlton/2003-03-13: This function is currently only intended
948 for internal use: it's probably not entirely safe when called on
949 user-generated input, because some of the 'index += 2' lines in
950 cp_find_first_component_aux might go past the end of malformed
954 cp_find_first_component (const char *name)
956 return cp_find_first_component_aux (name, 0);
959 /* Helper function for cp_find_first_component. Like that function,
960 it returns the length of the first component of NAME, but to make
961 the recursion easier, it also stops if it reaches an unexpected ')'
962 or '>' if the value of PERMISSIVE is nonzero. */
964 /* Let's optimize away calls to strlen("operator"). */
966 #define LENGTH_OF_OPERATOR 8
969 cp_find_first_component_aux (const char *name, int permissive)
971 unsigned int index = 0;
972 /* Operator names can show up in unexpected places. Since these can
973 contain parentheses or angle brackets, they can screw up the
974 recursion. But not every string 'operator' is part of an
975 operater name: e.g. you could have a variable 'cooperator'. So
976 this variable tells us whether or not we should treat the string
977 'operator' as starting an operator. */
978 int operator_possible = 1;
985 /* Template; eat it up. The calls to cp_first_component
986 should only return (I hope!) when they reach the '>'
987 terminating the component or a '::' between two
988 components. (Hence the '+ 2'.) */
990 for (index += cp_find_first_component_aux (name + index, 1);
992 index += cp_find_first_component_aux (name + index, 1))
994 if (name[index] != ':')
996 demangled_name_complaint (name);
997 return strlen (name);
1001 operator_possible = 1;
1004 /* Similar comment as to '<'. */
1006 for (index += cp_find_first_component_aux (name + index, 1);
1008 index += cp_find_first_component_aux (name + index, 1))
1010 if (name[index] != ':')
1012 demangled_name_complaint (name);
1013 return strlen (name);
1017 operator_possible = 1;
1025 demangled_name_complaint (name);
1026 return strlen (name);
1032 /* Operator names can screw up the recursion. */
1033 if (operator_possible
1034 && strncmp (name + index, "operator",
1035 LENGTH_OF_OPERATOR) == 0)
1037 index += LENGTH_OF_OPERATOR;
1038 while (ISSPACE(name[index]))
1040 switch (name[index])
1042 /* Skip over one less than the appropriate number of
1043 characters: the for loop will skip over the last
1046 if (name[index + 1] == '<')
1053 if (name[index + 1] == '>')
1066 operator_possible = 0;
1073 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1074 set of relevant characters are here: it's necessary to
1075 include any character that can show up before 'operator'
1076 in a demangled name, and it's safe to include any
1077 character that can't be part of an identifier's name. */
1078 operator_possible = 1;
1081 operator_possible = 0;
1087 /* Complain about a demangled name that we don't know how to parse.
1088 NAME is the demangled name in question. */
1091 demangled_name_complaint (const char *name)
1093 complaint (&symfile_complaints,
1094 "unexpected demangled name '%s'", name);
1097 /* If NAME is the fully-qualified name of a C++
1098 function/variable/method/etc., this returns the length of its
1099 entire prefix: all of the namespaces and classes that make up its
1100 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1101 4, given 'foo', it returns 0. */
1104 cp_entire_prefix_len (const char *name)
1106 unsigned int current_len = cp_find_first_component (name);
1107 unsigned int previous_len = 0;
1109 while (name[current_len] != '\0')
1111 gdb_assert (name[current_len] == ':');
1112 previous_len = current_len;
1113 /* Skip the '::'. */
1115 current_len += cp_find_first_component (name + current_len);
1118 return previous_len;
1121 /* Overload resolution functions. */
1123 /* Test to see if SYM is a symbol that we haven't seen corresponding
1124 to a function named OLOAD_NAME. If so, add it to the current
1128 overload_list_add_symbol (struct symbol *sym,
1129 const char *oload_name)
1135 /* If there is no type information, we can't do anything, so
1137 if (SYMBOL_TYPE (sym) == NULL)
1140 /* skip any symbols that we've already considered. */
1141 for (i = 0; i < sym_return_val_index; ++i)
1142 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1143 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1146 /* Get the demangled name without parameters */
1147 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1151 /* skip symbols that cannot match */
1152 if (strcmp (sym_name, oload_name) != 0)
1160 /* We have a match for an overload instance, so add SYM to the
1161 current list of overload instances */
1162 if (sym_return_val_index + 3 > sym_return_val_size)
1164 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1165 sym_return_val = (struct symbol **)
1166 xrealloc ((char *) sym_return_val, newsize);
1168 sym_return_val[sym_return_val_index++] = sym;
1169 sym_return_val[sym_return_val_index] = NULL;
1172 /* Return a null-terminated list of pointers to function symbols that
1173 are named FUNC_NAME and are visible within NAMESPACE. */
1176 make_symbol_overload_list (const char *func_name,
1177 const char *the_namespace)
1179 struct cleanup *old_cleanups;
1182 sym_return_val_size = 100;
1183 sym_return_val_index = 0;
1184 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1185 sizeof (struct symbol *));
1186 sym_return_val[0] = NULL;
1188 old_cleanups = make_cleanup (xfree, sym_return_val);
1190 make_symbol_overload_list_using (func_name, the_namespace);
1192 if (the_namespace[0] == '\0')
1196 char *concatenated_name
1197 = alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1198 strcpy (concatenated_name, the_namespace);
1199 strcat (concatenated_name, "::");
1200 strcat (concatenated_name, func_name);
1201 name = concatenated_name;
1204 make_symbol_overload_list_qualified (name);
1206 discard_cleanups (old_cleanups);
1208 return sym_return_val;
1211 /* Add all symbols with a name matching NAME in BLOCK to the overload
1215 make_symbol_overload_list_block (const char *name,
1216 const struct block *block)
1218 struct block_iterator iter;
1221 ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
1222 overload_list_add_symbol (sym, name);
1225 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1228 make_symbol_overload_list_namespace (const char *func_name,
1229 const char *the_namespace)
1232 const struct block *block = NULL;
1234 if (the_namespace[0] == '\0')
1238 char *concatenated_name
1239 = alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1241 strcpy (concatenated_name, the_namespace);
1242 strcat (concatenated_name, "::");
1243 strcat (concatenated_name, func_name);
1244 name = concatenated_name;
1247 /* Look in the static block. */
1248 block = block_static_block (get_selected_block (0));
1250 make_symbol_overload_list_block (name, block);
1252 /* Look in the global block. */
1253 block = block_global_block (block);
1255 make_symbol_overload_list_block (name, block);
1259 /* Search the namespace of the given type and namespace of and public
1263 make_symbol_overload_list_adl_namespace (struct type *type,
1264 const char *func_name)
1266 char *the_namespace;
1267 const char *type_name;
1270 while (TYPE_CODE (type) == TYPE_CODE_PTR
1271 || TYPE_CODE (type) == TYPE_CODE_REF
1272 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1273 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1275 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1276 type = check_typedef(type);
1278 type = TYPE_TARGET_TYPE (type);
1281 type_name = TYPE_NAME (type);
1283 if (type_name == NULL)
1286 prefix_len = cp_entire_prefix_len (type_name);
1288 if (prefix_len != 0)
1290 the_namespace = alloca (prefix_len + 1);
1291 strncpy (the_namespace, type_name, prefix_len);
1292 the_namespace[prefix_len] = '\0';
1294 make_symbol_overload_list_namespace (func_name, the_namespace);
1297 /* Check public base type */
1298 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1299 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1301 if (BASETYPE_VIA_PUBLIC (type, i))
1302 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1308 /* Adds the overload list overload candidates for FUNC_NAME found
1309 through argument dependent lookup. */
1312 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1313 const char *func_name)
1317 gdb_assert (sym_return_val_size != -1);
1319 for (i = 1; i <= nargs; i++)
1320 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1323 return sym_return_val;
1326 /* Used for cleanups to reset the "searched" flag in case of an
1330 reset_directive_searched (void *data)
1332 struct using_direct *direct = data;
1333 direct->searched = 0;
1336 /* This applies the using directives to add namespaces to search in,
1337 and then searches for overloads in all of those namespaces. It
1338 adds the symbols found to sym_return_val. Arguments are as in
1339 make_symbol_overload_list. */
1342 make_symbol_overload_list_using (const char *func_name,
1343 const char *the_namespace)
1345 struct using_direct *current;
1346 const struct block *block;
1348 /* First, go through the using directives. If any of them apply,
1349 look in the appropriate namespaces for new functions to match
1352 for (block = get_selected_block (0);
1354 block = BLOCK_SUPERBLOCK (block))
1355 for (current = block_using (block);
1357 current = current->next)
1359 /* Prevent recursive calls. */
1360 if (current->searched)
1363 /* If this is a namespace alias or imported declaration ignore
1365 if (current->alias != NULL || current->declaration != NULL)
1368 if (strcmp (the_namespace, current->import_dest) == 0)
1370 /* Mark this import as searched so that the recursive call
1371 does not search it again. */
1372 struct cleanup *old_chain;
1373 current->searched = 1;
1374 old_chain = make_cleanup (reset_directive_searched,
1377 make_symbol_overload_list_using (func_name,
1378 current->import_src);
1380 current->searched = 0;
1381 discard_cleanups (old_chain);
1385 /* Now, add names for this namespace. */
1386 make_symbol_overload_list_namespace (func_name, the_namespace);
1389 /* This does the bulk of the work of finding overloaded symbols.
1390 FUNC_NAME is the name of the overloaded function we're looking for
1391 (possibly including namespace info). */
1394 make_symbol_overload_list_qualified (const char *func_name)
1396 struct compunit_symtab *cust;
1397 struct objfile *objfile;
1398 const struct block *b, *surrounding_static_block = 0;
1400 /* Look through the partial symtabs for all symbols which begin by
1401 matching FUNC_NAME. Make sure we read that symbol table in. */
1403 ALL_OBJFILES (objfile)
1406 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1409 /* Search upwards from currently selected frame (so that we can
1410 complete on local vars. */
1412 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1413 make_symbol_overload_list_block (func_name, b);
1415 surrounding_static_block = block_static_block (get_selected_block (0));
1417 /* Go through the symtabs and check the externs and statics for
1418 symbols which match. */
1420 ALL_COMPUNITS (objfile, cust)
1423 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
1424 make_symbol_overload_list_block (func_name, b);
1427 ALL_COMPUNITS (objfile, cust)
1430 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
1431 /* Don't do this block twice. */
1432 if (b == surrounding_static_block)
1434 make_symbol_overload_list_block (func_name, b);
1438 /* Lookup the rtti type for a class name. */
1441 cp_lookup_rtti_type (const char *name, struct block *block)
1443 struct symbol * rtti_sym;
1444 struct type * rtti_type;
1446 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1448 if (rtti_sym == NULL)
1450 warning (_("RTTI symbol not found for class '%s'"), name);
1454 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1456 warning (_("RTTI symbol for class '%s' is not a type"), name);
1460 rtti_type = SYMBOL_TYPE (rtti_sym);
1462 switch (TYPE_CODE (rtti_type))
1464 case TYPE_CODE_STRUCT:
1466 case TYPE_CODE_NAMESPACE:
1467 /* chastain/2003-11-26: the symbol tables often contain fake
1468 symbols for namespaces with the same name as the struct.
1469 This warning is an indication of a bug in the lookup order
1470 or a bug in the way that the symbol tables are populated. */
1471 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1474 warning (_("RTTI symbol for class '%s' has bad type"), name);
1481 #ifdef HAVE_WORKING_FORK
1483 /* If nonzero, attempt to catch crashes in the demangler and print
1484 useful debugging information. */
1486 static int catch_demangler_crashes = 1;
1488 /* Stack context and environment for demangler crash recovery. */
1490 static SIGJMP_BUF gdb_demangle_jmp_buf;
1492 /* If nonzero, attempt to dump core from the signal handler. */
1494 static int gdb_demangle_attempt_core_dump = 1;
1496 /* Signal handler for gdb_demangle. */
1499 gdb_demangle_signal_handler (int signo)
1501 if (gdb_demangle_attempt_core_dump)
1506 gdb_demangle_attempt_core_dump = 0;
1509 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1514 /* A wrapper for bfd_demangle. */
1517 gdb_demangle (const char *name, int options)
1519 char *result = NULL;
1520 int crash_signal = 0;
1522 #ifdef HAVE_WORKING_FORK
1523 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1524 struct sigaction sa, old_sa;
1528 static int core_dump_allowed = -1;
1530 if (core_dump_allowed == -1)
1532 core_dump_allowed = can_dump_core (LIMIT_CUR);
1534 if (!core_dump_allowed)
1535 gdb_demangle_attempt_core_dump = 0;
1538 if (catch_demangler_crashes)
1540 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1541 sa.sa_handler = gdb_demangle_signal_handler;
1542 sigemptyset (&sa.sa_mask);
1543 #ifdef HAVE_SIGALTSTACK
1544 sa.sa_flags = SA_ONSTACK;
1548 sigaction (SIGSEGV, &sa, &old_sa);
1550 ofunc = (void (*)()) signal (SIGSEGV, gdb_demangle_signal_handler);
1553 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1557 if (crash_signal == 0)
1558 result = bfd_demangle (NULL, name, options);
1560 #ifdef HAVE_WORKING_FORK
1561 if (catch_demangler_crashes)
1563 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1564 sigaction (SIGSEGV, &old_sa, NULL);
1566 signal (SIGSEGV, ofunc);
1569 if (crash_signal != 0)
1571 static int error_reported = 0;
1573 if (!error_reported)
1575 char *short_msg, *long_msg;
1576 struct cleanup *back_to;
1578 short_msg = xstrprintf (_("unable to demangle '%s' "
1579 "(demangler failed with signal %d)"),
1580 name, crash_signal);
1581 back_to = make_cleanup (xfree, short_msg);
1583 long_msg = xstrprintf ("%s:%d: %s: %s", __FILE__, __LINE__,
1584 "demangler-warning", short_msg);
1585 make_cleanup (xfree, long_msg);
1587 target_terminal_ours ();
1589 if (core_dump_allowed)
1590 fprintf_unfiltered (gdb_stderr,
1591 _("%s\nAttempting to dump core.\n"),
1594 warn_cant_dump_core (long_msg);
1596 demangler_warning (__FILE__, __LINE__, "%s", short_msg);
1598 do_cleanups (back_to);
1611 /* Don't allow just "maintenance cplus". */
1614 maint_cplus_command (char *arg, int from_tty)
1616 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1617 "by the name of a command.\n"));
1618 help_list (maint_cplus_cmd_list,
1619 "maintenance cplus ",
1620 all_commands, gdb_stdout);
1623 /* This is a front end for cp_find_first_component, for unit testing.
1624 Be careful when using it: see the NOTE above
1625 cp_find_first_component. */
1628 first_component_command (char *arg, int from_tty)
1636 len = cp_find_first_component (arg);
1637 prefix = alloca (len + 1);
1639 memcpy (prefix, arg, len);
1642 printf_unfiltered ("%s\n", prefix);
1645 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1648 /* Implement "info vtbl". */
1651 info_vtbl_command (char *arg, int from_tty)
1653 struct value *value;
1655 value = parse_and_eval (arg);
1656 cplus_print_vtable (value);
1660 _initialize_cp_support (void)
1662 add_prefix_cmd ("cplus", class_maintenance,
1663 maint_cplus_command,
1664 _("C++ maintenance commands."),
1665 &maint_cplus_cmd_list,
1666 "maintenance cplus ",
1667 0, &maintenancelist);
1668 add_alias_cmd ("cp", "cplus",
1669 class_maintenance, 1,
1672 add_cmd ("first_component",
1674 first_component_command,
1675 _("Print the first class/namespace component of NAME."),
1676 &maint_cplus_cmd_list);
1678 add_info ("vtbl", info_vtbl_command,
1679 _("Show the virtual function table for a C++ object.\n\
1680 Usage: info vtbl EXPRESSION\n\
1681 Evaluate EXPRESSION and display the virtual function table for the\n\
1682 resulting object."));
1684 #ifdef HAVE_WORKING_FORK
1685 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
1686 &catch_demangler_crashes, _("\
1687 Set whether to attempt to catch demangler crashes."), _("\
1688 Show whether to attempt to catch demangler crashes."), _("\
1689 If enabled GDB will attempt to catch demangler crashes and\n\
1690 display the offending symbol."),
1693 &maintenance_set_cmdlist,
1694 &maintenance_show_cmdlist);