1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2017 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"
37 #include "gdb_setjmp.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 (char *) obstack_copy (obstack
, string
, *len
);
98 /* Return 1 if STRING is clearly already in canonical form. This
99 function is conservative; things which it does not recognize are
100 assumed to be non-canonical, and the parser will sort them out
101 afterwards. This speeds up the critical path for alphanumeric
105 cp_already_canonical (const char *string
)
107 /* Identifier start character [a-zA-Z_]. */
108 if (!ISIDST (string
[0]))
111 /* These are the only two identifiers which canonicalize to other
112 than themselves or an error: unsigned -> unsigned int and
114 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
116 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
119 /* Identifier character [a-zA-Z0-9_]. */
120 while (ISIDNUM (string
[1]))
123 if (string
[1] == '\0')
129 /* Inspect the given RET_COMP for its type. If it is a typedef,
130 replace the node with the typedef's tree.
132 Returns 1 if any typedef substitutions were made, 0 otherwise. */
135 inspect_type (struct demangle_parse_info
*info
,
136 struct demangle_component
*ret_comp
,
137 canonicalization_ftype
*finder
,
144 /* Copy the symbol's name from RET_COMP and look it up
145 in the symbol table. */
146 name
= (char *) alloca (ret_comp
->u
.s_name
.len
+ 1);
147 memcpy (name
, ret_comp
->u
.s_name
.s
, ret_comp
->u
.s_name
.len
);
148 name
[ret_comp
->u
.s_name
.len
] = '\0';
150 /* Ignore any typedefs that should not be substituted. */
151 for (i
= 0; i
< ARRAY_SIZE (ignore_typedefs
); ++i
)
153 if (strcmp (name
, ignore_typedefs
[i
]) == 0)
161 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0).symbol
;
163 CATCH (except
, RETURN_MASK_ALL
)
171 struct type
*otype
= SYMBOL_TYPE (sym
);
175 const char *new_name
= (*finder
) (otype
, data
);
177 if (new_name
!= NULL
)
179 ret_comp
->u
.s_name
.s
= new_name
;
180 ret_comp
->u
.s_name
.len
= strlen (new_name
);
187 /* If the type is a typedef or namespace alias, replace it. */
188 if (TYPE_CODE (otype
) == TYPE_CODE_TYPEDEF
189 || TYPE_CODE (otype
) == TYPE_CODE_NAMESPACE
)
194 std::unique_ptr
<demangle_parse_info
> i
;
196 /* Get the real type of the typedef. */
197 type
= check_typedef (otype
);
199 /* If the symbol is a namespace and its type name is no different
200 than the name we looked up, this symbol is not a namespace
201 alias and does not need to be substituted. */
202 if (TYPE_CODE (otype
) == TYPE_CODE_NAMESPACE
203 && strcmp (TYPE_NAME (type
), name
) == 0)
206 is_anon
= (TYPE_TAG_NAME (type
) == NULL
207 && (TYPE_CODE (type
) == TYPE_CODE_ENUM
208 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
209 || TYPE_CODE (type
) == TYPE_CODE_UNION
));
212 struct type
*last
= otype
;
214 /* Find the last typedef for the type. */
215 while (TYPE_TARGET_TYPE (last
) != NULL
216 && (TYPE_CODE (TYPE_TARGET_TYPE (last
))
217 == TYPE_CODE_TYPEDEF
))
218 last
= TYPE_TARGET_TYPE (last
);
220 /* If there is only one typedef for this anonymous type,
221 do not substitute it. */
225 /* Use the last typedef seen as the type for this
233 type_print (type
, "", &buf
, -1);
235 /* If type_print threw an exception, there is little point
236 in continuing, so just bow out gracefully. */
237 CATCH (except
, RETURN_MASK_ERROR
)
244 name
= (char *) obstack_copy0 (&info
->obstack
, buf
.c_str (), len
);
246 /* Turn the result into a new tree. Note that this
247 tree will contain pointers into NAME, so NAME cannot
248 be free'd until all typedef conversion is done and
249 the final result is converted into a string. */
250 i
= cp_demangled_name_to_comp (name
, NULL
);
253 /* Merge the two trees. */
254 cp_merge_demangle_parse_infos (info
, ret_comp
, i
.get ());
256 /* Replace any newly introduced typedefs -- but not
257 if the type is anonymous (that would lead to infinite
260 replace_typedefs (info
, ret_comp
, finder
, data
);
264 /* This shouldn't happen unless the type printer has
265 output something that the name parser cannot grok.
266 Nonetheless, an ounce of prevention...
268 Canonicalize the name again, and store it in the
269 current node (RET_COMP). */
270 std::string canon
= cp_canonicalize_string_no_typedefs (name
);
274 /* Copy the canonicalization into the obstack. */
275 name
= copy_string_to_obstack (&info
->obstack
, canon
.c_str (), &len
);
278 ret_comp
->u
.s_name
.s
= name
;
279 ret_comp
->u
.s_name
.len
= len
;
289 /* Replace any typedefs appearing in the qualified name
290 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
294 replace_typedefs_qualified_name (struct demangle_parse_info
*info
,
295 struct demangle_component
*ret_comp
,
296 canonicalization_ftype
*finder
,
302 struct demangle_component
*comp
= ret_comp
;
304 /* Walk each node of the qualified name, reconstructing the name of
305 this element. With every node, check for any typedef substitutions.
306 If a substitution has occurred, replace the qualified name node
307 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
309 while (comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
311 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_NAME
)
313 struct demangle_component newobj
;
315 buf
.write (d_left (comp
)->u
.s_name
.s
, d_left (comp
)->u
.s_name
.len
);
317 name
= (char *) obstack_copy0 (&info
->obstack
, buf
.c_str (), len
);
318 newobj
.type
= DEMANGLE_COMPONENT_NAME
;
319 newobj
.u
.s_name
.s
= name
;
320 newobj
.u
.s_name
.len
= len
;
321 if (inspect_type (info
, &newobj
, finder
, data
))
326 /* A typedef was substituted in NEW. Convert it to a
327 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
331 n
= cp_comp_to_string (&newobj
, 100);
334 /* If something went astray, abort typedef substitutions. */
338 s
= copy_string_to_obstack (&info
->obstack
, n
, &slen
);
341 d_left (ret_comp
)->type
= DEMANGLE_COMPONENT_NAME
;
342 d_left (ret_comp
)->u
.s_name
.s
= s
;
343 d_left (ret_comp
)->u
.s_name
.len
= slen
;
344 d_right (ret_comp
) = d_right (comp
);
351 /* The current node is not a name, so simply replace any
352 typedefs in it. Then print it to the stream to continue
353 checking for more typedefs in the tree. */
354 replace_typedefs (info
, d_left (comp
), finder
, data
);
355 name
= cp_comp_to_string (d_left (comp
), 100);
358 /* If something went astray, abort typedef substitutions. */
366 comp
= d_right (comp
);
369 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
370 name assembled above and append the name given by COMP. Then use this
371 reassembled name to check for a typedef. */
373 if (comp
->type
== DEMANGLE_COMPONENT_NAME
)
375 buf
.write (comp
->u
.s_name
.s
, comp
->u
.s_name
.len
);
377 name
= (char *) obstack_copy0 (&info
->obstack
, buf
.c_str (), len
);
379 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
380 with a DEMANGLE_COMPONENT_NAME node containing the whole
382 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
383 ret_comp
->u
.s_name
.s
= name
;
384 ret_comp
->u
.s_name
.len
= len
;
385 inspect_type (info
, ret_comp
, finder
, data
);
388 replace_typedefs (info
, comp
, finder
, data
);
392 /* A function to check const and volatile qualifiers for argument types.
394 "Parameter declarations that differ only in the presence
395 or absence of `const' and/or `volatile' are equivalent."
396 C++ Standard N3290, clause 13.1.3 #4. */
399 check_cv_qualifiers (struct demangle_component
*ret_comp
)
401 while (d_left (ret_comp
) != NULL
402 && (d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_CONST
403 || d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_VOLATILE
))
405 d_left (ret_comp
) = d_left (d_left (ret_comp
));
409 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
410 their basic types. */
413 replace_typedefs (struct demangle_parse_info
*info
,
414 struct demangle_component
*ret_comp
,
415 canonicalization_ftype
*finder
,
421 && (ret_comp
->type
== DEMANGLE_COMPONENT_NAME
422 || ret_comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
423 || ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
424 || ret_comp
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
))
426 char *local_name
= cp_comp_to_string (ret_comp
, 10);
428 if (local_name
!= NULL
)
430 struct symbol
*sym
= NULL
;
435 sym
= lookup_symbol (local_name
, 0, VAR_DOMAIN
, 0).symbol
;
437 CATCH (except
, RETURN_MASK_ALL
)
446 struct type
*otype
= SYMBOL_TYPE (sym
);
447 const char *new_name
= (*finder
) (otype
, data
);
449 if (new_name
!= NULL
)
451 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
452 ret_comp
->u
.s_name
.s
= new_name
;
453 ret_comp
->u
.s_name
.len
= strlen (new_name
);
460 switch (ret_comp
->type
)
462 case DEMANGLE_COMPONENT_ARGLIST
:
463 check_cv_qualifiers (ret_comp
);
466 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
467 case DEMANGLE_COMPONENT_TEMPLATE
:
468 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
469 case DEMANGLE_COMPONENT_TYPED_NAME
:
470 replace_typedefs (info
, d_left (ret_comp
), finder
, data
);
471 replace_typedefs (info
, d_right (ret_comp
), finder
, data
);
474 case DEMANGLE_COMPONENT_NAME
:
475 inspect_type (info
, ret_comp
, finder
, data
);
478 case DEMANGLE_COMPONENT_QUAL_NAME
:
479 replace_typedefs_qualified_name (info
, ret_comp
, finder
, data
);
482 case DEMANGLE_COMPONENT_LOCAL_NAME
:
483 case DEMANGLE_COMPONENT_CTOR
:
484 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
485 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
486 replace_typedefs (info
, d_right (ret_comp
), finder
, data
);
489 case DEMANGLE_COMPONENT_CONST
:
490 case DEMANGLE_COMPONENT_RESTRICT
:
491 case DEMANGLE_COMPONENT_VOLATILE
:
492 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
493 case DEMANGLE_COMPONENT_CONST_THIS
:
494 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
495 case DEMANGLE_COMPONENT_POINTER
:
496 case DEMANGLE_COMPONENT_REFERENCE
:
497 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
498 replace_typedefs (info
, d_left (ret_comp
), finder
, data
);
507 /* Parse STRING and convert it to canonical form, resolving any
508 typedefs. If parsing fails, or if STRING is already canonical,
509 return the empty string. Otherwise return the canonical form. If
510 FINDER is not NULL, then type components are passed to FINDER to be
511 looked up. DATA is passed verbatim to FINDER. */
514 cp_canonicalize_string_full (const char *string
,
515 canonicalization_ftype
*finder
,
519 unsigned int estimated_len
;
520 std::unique_ptr
<demangle_parse_info
> info
;
522 estimated_len
= strlen (string
) * 2;
523 info
= cp_demangled_name_to_comp (string
, NULL
);
526 /* Replace all the typedefs in the tree. */
527 replace_typedefs (info
.get (), info
->tree
, finder
, data
);
529 /* Convert the tree back into a string. */
530 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
531 gdb_assert (!ret
.empty ());
533 /* Finally, compare the original string with the computed
534 name, returning NULL if they are the same. */
536 return std::string ();
542 /* Like cp_canonicalize_string_full, but always passes NULL for
546 cp_canonicalize_string_no_typedefs (const char *string
)
548 return cp_canonicalize_string_full (string
, NULL
, NULL
);
551 /* Parse STRING and convert it to canonical form. If parsing fails,
552 or if STRING is already canonical, return the empty string.
553 Otherwise return the canonical form. */
556 cp_canonicalize_string (const char *string
)
558 std::unique_ptr
<demangle_parse_info
> info
;
559 unsigned int estimated_len
;
561 if (cp_already_canonical (string
))
562 return std::string ();
564 info
= cp_demangled_name_to_comp (string
, NULL
);
566 return std::string ();
568 estimated_len
= strlen (string
) * 2;
569 std::string ret
= cp_comp_to_string (info
->tree
, estimated_len
);
573 warning (_("internal error: string \"%s\" failed to be canonicalized"),
575 return std::string ();
579 return std::string ();
584 /* Convert a mangled name to a demangle_component tree. *MEMORY is
585 set to the block of used memory that should be freed when finished
586 with the tree. DEMANGLED_P is set to the char * that should be
587 freed when finished with the tree, or NULL if none was needed.
588 OPTIONS will be passed to the demangler. */
590 static std::unique_ptr
<demangle_parse_info
>
591 mangled_name_to_comp (const char *mangled_name
, int options
,
592 void **memory
, char **demangled_p
)
594 char *demangled_name
;
596 /* If it looks like a v3 mangled name, then try to go directly
598 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
600 struct demangle_component
*ret
;
602 ret
= cplus_demangle_v3_components (mangled_name
,
606 std::unique_ptr
<demangle_parse_info
> info (new demangle_parse_info
);
613 /* If it doesn't, or if that failed, then try to demangle the
615 demangled_name
= gdb_demangle (mangled_name
, options
);
616 if (demangled_name
== NULL
)
619 /* If we could demangle the name, parse it to build the component
621 std::unique_ptr
<demangle_parse_info
> info
622 = cp_demangled_name_to_comp (demangled_name
, NULL
);
626 xfree (demangled_name
);
630 *demangled_p
= demangled_name
;
634 /* Return the name of the class containing method PHYSNAME. */
637 cp_class_name_from_physname (const char *physname
)
639 void *storage
= NULL
;
640 char *demangled_name
= NULL
, *ret
;
641 struct demangle_component
*ret_comp
, *prev_comp
, *cur_comp
;
642 std::unique_ptr
<demangle_parse_info
> info
;
645 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
646 &storage
, &demangled_name
);
651 ret_comp
= info
->tree
;
653 /* First strip off any qualifiers, if we have a function or
656 switch (ret_comp
->type
)
658 case DEMANGLE_COMPONENT_CONST
:
659 case DEMANGLE_COMPONENT_RESTRICT
:
660 case DEMANGLE_COMPONENT_VOLATILE
:
661 case DEMANGLE_COMPONENT_CONST_THIS
:
662 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
663 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
664 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
665 ret_comp
= d_left (ret_comp
);
672 /* If what we have now is a function, discard the argument list. */
673 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
674 ret_comp
= d_left (ret_comp
);
676 /* If what we have now is a template, strip off the template
677 arguments. The left subtree may be a qualified name. */
678 if (ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
679 ret_comp
= d_left (ret_comp
);
681 /* What we have now should be a name, possibly qualified.
682 Additional qualifiers could live in the left subtree or the right
683 subtree. Find the last piece. */
688 switch (cur_comp
->type
)
690 case DEMANGLE_COMPONENT_QUAL_NAME
:
691 case DEMANGLE_COMPONENT_LOCAL_NAME
:
692 prev_comp
= cur_comp
;
693 cur_comp
= d_right (cur_comp
);
695 case DEMANGLE_COMPONENT_TEMPLATE
:
696 case DEMANGLE_COMPONENT_NAME
:
697 case DEMANGLE_COMPONENT_CTOR
:
698 case DEMANGLE_COMPONENT_DTOR
:
699 case DEMANGLE_COMPONENT_OPERATOR
:
700 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
710 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
712 /* We want to discard the rightmost child of PREV_COMP. */
713 *prev_comp
= *d_left (prev_comp
);
714 /* The ten is completely arbitrary; we don't have a good
716 ret
= cp_comp_to_string (ret_comp
, 10);
720 xfree (demangled_name
);
724 /* Return the child of COMP which is the basename of a method,
725 variable, et cetera. All scope qualifiers are discarded, but
726 template arguments will be included. The component tree may be
729 static struct demangle_component
*
730 unqualified_name_from_comp (struct demangle_component
*comp
)
732 struct demangle_component
*ret_comp
= comp
, *last_template
;
736 last_template
= NULL
;
738 switch (ret_comp
->type
)
740 case DEMANGLE_COMPONENT_QUAL_NAME
:
741 case DEMANGLE_COMPONENT_LOCAL_NAME
:
742 ret_comp
= d_right (ret_comp
);
744 case DEMANGLE_COMPONENT_TYPED_NAME
:
745 ret_comp
= d_left (ret_comp
);
747 case DEMANGLE_COMPONENT_TEMPLATE
:
748 gdb_assert (last_template
== NULL
);
749 last_template
= ret_comp
;
750 ret_comp
= d_left (ret_comp
);
752 case DEMANGLE_COMPONENT_CONST
:
753 case DEMANGLE_COMPONENT_RESTRICT
:
754 case DEMANGLE_COMPONENT_VOLATILE
:
755 case DEMANGLE_COMPONENT_CONST_THIS
:
756 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
757 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
758 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
759 ret_comp
= d_left (ret_comp
);
761 case DEMANGLE_COMPONENT_NAME
:
762 case DEMANGLE_COMPONENT_CTOR
:
763 case DEMANGLE_COMPONENT_DTOR
:
764 case DEMANGLE_COMPONENT_OPERATOR
:
765 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
775 d_left (last_template
) = ret_comp
;
776 return last_template
;
782 /* Return the name of the method whose linkage name is PHYSNAME. */
785 method_name_from_physname (const char *physname
)
787 void *storage
= NULL
;
788 char *demangled_name
= NULL
, *ret
;
789 struct demangle_component
*ret_comp
;
790 std::unique_ptr
<demangle_parse_info
> info
;
792 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
793 &storage
, &demangled_name
);
797 ret_comp
= unqualified_name_from_comp (info
->tree
);
800 if (ret_comp
!= NULL
)
801 /* The ten is completely arbitrary; we don't have a good
803 ret
= cp_comp_to_string (ret_comp
, 10);
806 xfree (demangled_name
);
810 /* If FULL_NAME is the demangled name of a C++ function (including an
811 arg list, possibly including namespace/class qualifications),
812 return a new string containing only the function name (without the
813 arg list/class qualifications). Otherwise, return NULL. The
814 caller is responsible for freeing the memory in question. */
817 cp_func_name (const char *full_name
)
820 struct demangle_component
*ret_comp
;
821 std::unique_ptr
<demangle_parse_info
> info
;
823 info
= cp_demangled_name_to_comp (full_name
, NULL
);
827 ret_comp
= unqualified_name_from_comp (info
->tree
);
830 if (ret_comp
!= NULL
)
831 ret
= cp_comp_to_string (ret_comp
, 10);
836 /* DEMANGLED_NAME is the name of a function, including parameters and
837 (optionally) a return type. Return the name of the function without
838 parameters or return type, or NULL if we can not parse the name. */
841 cp_remove_params (const char *demangled_name
)
844 struct demangle_component
*ret_comp
;
845 std::unique_ptr
<demangle_parse_info
> info
;
848 if (demangled_name
== NULL
)
851 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
855 /* First strip off any qualifiers, if we have a function or method. */
856 ret_comp
= info
->tree
;
858 switch (ret_comp
->type
)
860 case DEMANGLE_COMPONENT_CONST
:
861 case DEMANGLE_COMPONENT_RESTRICT
:
862 case DEMANGLE_COMPONENT_VOLATILE
:
863 case DEMANGLE_COMPONENT_CONST_THIS
:
864 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
865 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
866 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
867 ret_comp
= d_left (ret_comp
);
874 /* What we have now should be a function. Return its name. */
875 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
876 ret
= cp_comp_to_string (d_left (ret_comp
), 10);
881 /* Here are some random pieces of trivia to keep in mind while trying
882 to take apart demangled names:
884 - Names can contain function arguments or templates, so the process
885 has to be, to some extent recursive: maybe keep track of your
886 depth based on encountering <> and ().
888 - Parentheses don't just have to happen at the end of a name: they
889 can occur even if the name in question isn't a function, because
890 a template argument might be a type that's a function.
892 - Conversely, even if you're trying to deal with a function, its
893 demangled name might not end with ')': it could be a const or
894 volatile class method, in which case it ends with "const" or
897 - Parentheses are also used in anonymous namespaces: a variable
898 'foo' in an anonymous namespace gets demangled as "(anonymous
901 - And operator names can contain parentheses or angle brackets. */
903 /* FIXME: carlton/2003-03-13: We have several functions here with
904 overlapping functionality; can we combine them? Also, do they
905 handle all the above considerations correctly? */
908 /* This returns the length of first component of NAME, which should be
909 the demangled name of a C++ variable/function/method/etc.
910 Specifically, it returns the index of the first colon forming the
911 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
912 it returns the 1, and given 'foo', it returns 0. */
914 /* The character in NAME indexed by the return value is guaranteed to
915 always be either ':' or '\0'. */
917 /* NOTE: carlton/2003-03-13: This function is currently only intended
918 for internal use: it's probably not entirely safe when called on
919 user-generated input, because some of the 'index += 2' lines in
920 cp_find_first_component_aux might go past the end of malformed
924 cp_find_first_component (const char *name
)
926 return cp_find_first_component_aux (name
, 0);
929 /* Helper function for cp_find_first_component. Like that function,
930 it returns the length of the first component of NAME, but to make
931 the recursion easier, it also stops if it reaches an unexpected ')'
932 or '>' if the value of PERMISSIVE is nonzero. */
934 /* Let's optimize away calls to strlen("operator"). */
936 #define LENGTH_OF_OPERATOR 8
939 cp_find_first_component_aux (const char *name
, int permissive
)
941 unsigned int index
= 0;
942 /* Operator names can show up in unexpected places. Since these can
943 contain parentheses or angle brackets, they can screw up the
944 recursion. But not every string 'operator' is part of an
945 operater name: e.g. you could have a variable 'cooperator'. So
946 this variable tells us whether or not we should treat the string
947 'operator' as starting an operator. */
948 int operator_possible
= 1;
955 /* Template; eat it up. The calls to cp_first_component
956 should only return (I hope!) when they reach the '>'
957 terminating the component or a '::' between two
958 components. (Hence the '+ 2'.) */
960 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
962 index
+= cp_find_first_component_aux (name
+ index
, 1))
964 if (name
[index
] != ':')
966 demangled_name_complaint (name
);
967 return strlen (name
);
971 operator_possible
= 1;
974 /* Similar comment as to '<'. */
976 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
978 index
+= cp_find_first_component_aux (name
+ index
, 1))
980 if (name
[index
] != ':')
982 demangled_name_complaint (name
);
983 return strlen (name
);
987 operator_possible
= 1;
995 demangled_name_complaint (name
);
996 return strlen (name
);
1001 /* ':' marks a component iff the next character is also a ':'.
1002 Otherwise it is probably malformed input. */
1003 if (name
[index
+ 1] == ':')
1007 /* Operator names can screw up the recursion. */
1008 if (operator_possible
1009 && strncmp (name
+ index
, "operator",
1010 LENGTH_OF_OPERATOR
) == 0)
1012 index
+= LENGTH_OF_OPERATOR
;
1013 while (ISSPACE(name
[index
]))
1015 switch (name
[index
])
1017 /* Skip over one less than the appropriate number of
1018 characters: the for loop will skip over the last
1021 if (name
[index
+ 1] == '<')
1028 if (name
[index
+ 1] == '>')
1041 operator_possible
= 0;
1048 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1049 set of relevant characters are here: it's necessary to
1050 include any character that can show up before 'operator'
1051 in a demangled name, and it's safe to include any
1052 character that can't be part of an identifier's name. */
1053 operator_possible
= 1;
1056 operator_possible
= 0;
1062 /* Complain about a demangled name that we don't know how to parse.
1063 NAME is the demangled name in question. */
1066 demangled_name_complaint (const char *name
)
1068 complaint (&symfile_complaints
,
1069 "unexpected demangled name '%s'", name
);
1072 /* If NAME is the fully-qualified name of a C++
1073 function/variable/method/etc., this returns the length of its
1074 entire prefix: all of the namespaces and classes that make up its
1075 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1076 4, given 'foo', it returns 0. */
1079 cp_entire_prefix_len (const char *name
)
1081 unsigned int current_len
= cp_find_first_component (name
);
1082 unsigned int previous_len
= 0;
1084 while (name
[current_len
] != '\0')
1086 gdb_assert (name
[current_len
] == ':');
1087 previous_len
= current_len
;
1088 /* Skip the '::'. */
1090 current_len
+= cp_find_first_component (name
+ current_len
);
1093 return previous_len
;
1096 /* Overload resolution functions. */
1098 /* Test to see if SYM is a symbol that we haven't seen corresponding
1099 to a function named OLOAD_NAME. If so, add it to the current
1103 overload_list_add_symbol (struct symbol
*sym
,
1104 const char *oload_name
)
1110 /* If there is no type information, we can't do anything, so
1112 if (SYMBOL_TYPE (sym
) == NULL
)
1115 /* skip any symbols that we've already considered. */
1116 for (i
= 0; i
< sym_return_val_index
; ++i
)
1117 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
1118 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
1121 /* Get the demangled name without parameters */
1122 sym_name
= cp_remove_params (SYMBOL_NATURAL_NAME (sym
));
1126 /* skip symbols that cannot match */
1127 if (strcmp (sym_name
, oload_name
) != 0)
1135 /* We have a match for an overload instance, so add SYM to the
1136 current list of overload instances */
1137 if (sym_return_val_index
+ 3 > sym_return_val_size
)
1139 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
1140 sym_return_val
= (struct symbol
**)
1141 xrealloc ((char *) sym_return_val
, newsize
);
1143 sym_return_val
[sym_return_val_index
++] = sym
;
1144 sym_return_val
[sym_return_val_index
] = NULL
;
1147 /* Return a null-terminated list of pointers to function symbols that
1148 are named FUNC_NAME and are visible within NAMESPACE. */
1151 make_symbol_overload_list (const char *func_name
,
1152 const char *the_namespace
)
1154 struct cleanup
*old_cleanups
;
1157 sym_return_val_size
= 100;
1158 sym_return_val_index
= 0;
1159 sym_return_val
= XNEWVEC (struct symbol
*, sym_return_val_size
+ 1);
1160 sym_return_val
[0] = NULL
;
1162 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
1164 make_symbol_overload_list_using (func_name
, the_namespace
);
1166 if (the_namespace
[0] == '\0')
1170 char *concatenated_name
1171 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (func_name
) + 1);
1172 strcpy (concatenated_name
, the_namespace
);
1173 strcat (concatenated_name
, "::");
1174 strcat (concatenated_name
, func_name
);
1175 name
= concatenated_name
;
1178 make_symbol_overload_list_qualified (name
);
1180 discard_cleanups (old_cleanups
);
1182 return sym_return_val
;
1185 /* Add all symbols with a name matching NAME in BLOCK to the overload
1189 make_symbol_overload_list_block (const char *name
,
1190 const struct block
*block
)
1192 struct block_iterator iter
;
1195 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, name
, iter
, sym
)
1196 overload_list_add_symbol (sym
, name
);
1199 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1202 make_symbol_overload_list_namespace (const char *func_name
,
1203 const char *the_namespace
)
1206 const struct block
*block
= NULL
;
1208 if (the_namespace
[0] == '\0')
1212 char *concatenated_name
1213 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (func_name
) + 1);
1215 strcpy (concatenated_name
, the_namespace
);
1216 strcat (concatenated_name
, "::");
1217 strcat (concatenated_name
, func_name
);
1218 name
= concatenated_name
;
1221 /* Look in the static block. */
1222 block
= block_static_block (get_selected_block (0));
1224 make_symbol_overload_list_block (name
, block
);
1226 /* Look in the global block. */
1227 block
= block_global_block (block
);
1229 make_symbol_overload_list_block (name
, block
);
1233 /* Search the namespace of the given type and namespace of and public
1237 make_symbol_overload_list_adl_namespace (struct type
*type
,
1238 const char *func_name
)
1240 char *the_namespace
;
1241 const char *type_name
;
1244 while (TYPE_CODE (type
) == TYPE_CODE_PTR
1245 || TYPE_IS_REFERENCE (type
)
1246 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1247 || TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1249 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1250 type
= check_typedef(type
);
1252 type
= TYPE_TARGET_TYPE (type
);
1255 type_name
= TYPE_NAME (type
);
1257 if (type_name
== NULL
)
1260 prefix_len
= cp_entire_prefix_len (type_name
);
1262 if (prefix_len
!= 0)
1264 the_namespace
= (char *) alloca (prefix_len
+ 1);
1265 strncpy (the_namespace
, type_name
, prefix_len
);
1266 the_namespace
[prefix_len
] = '\0';
1268 make_symbol_overload_list_namespace (func_name
, the_namespace
);
1271 /* Check public base type */
1272 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1273 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
1275 if (BASETYPE_VIA_PUBLIC (type
, i
))
1276 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type
,
1282 /* Adds the overload list overload candidates for FUNC_NAME found
1283 through argument dependent lookup. */
1286 make_symbol_overload_list_adl (struct type
**arg_types
, int nargs
,
1287 const char *func_name
)
1291 gdb_assert (sym_return_val_size
!= -1);
1293 for (i
= 1; i
<= nargs
; i
++)
1294 make_symbol_overload_list_adl_namespace (arg_types
[i
- 1],
1297 return sym_return_val
;
1300 /* Used for cleanups to reset the "searched" flag in case of an
1304 reset_directive_searched (void *data
)
1306 struct using_direct
*direct
= (struct using_direct
*) data
;
1307 direct
->searched
= 0;
1310 /* This applies the using directives to add namespaces to search in,
1311 and then searches for overloads in all of those namespaces. It
1312 adds the symbols found to sym_return_val. Arguments are as in
1313 make_symbol_overload_list. */
1316 make_symbol_overload_list_using (const char *func_name
,
1317 const char *the_namespace
)
1319 struct using_direct
*current
;
1320 const struct block
*block
;
1322 /* First, go through the using directives. If any of them apply,
1323 look in the appropriate namespaces for new functions to match
1326 for (block
= get_selected_block (0);
1328 block
= BLOCK_SUPERBLOCK (block
))
1329 for (current
= block_using (block
);
1331 current
= current
->next
)
1333 /* Prevent recursive calls. */
1334 if (current
->searched
)
1337 /* If this is a namespace alias or imported declaration ignore
1339 if (current
->alias
!= NULL
|| current
->declaration
!= NULL
)
1342 if (strcmp (the_namespace
, current
->import_dest
) == 0)
1344 /* Mark this import as searched so that the recursive call
1345 does not search it again. */
1346 struct cleanup
*old_chain
;
1347 current
->searched
= 1;
1348 old_chain
= make_cleanup (reset_directive_searched
,
1351 make_symbol_overload_list_using (func_name
,
1352 current
->import_src
);
1354 current
->searched
= 0;
1355 discard_cleanups (old_chain
);
1359 /* Now, add names for this namespace. */
1360 make_symbol_overload_list_namespace (func_name
, the_namespace
);
1363 /* This does the bulk of the work of finding overloaded symbols.
1364 FUNC_NAME is the name of the overloaded function we're looking for
1365 (possibly including namespace info). */
1368 make_symbol_overload_list_qualified (const char *func_name
)
1370 struct compunit_symtab
*cust
;
1371 struct objfile
*objfile
;
1372 const struct block
*b
, *surrounding_static_block
= 0;
1374 /* Look through the partial symtabs for all symbols which begin by
1375 matching FUNC_NAME. Make sure we read that symbol table in. */
1377 ALL_OBJFILES (objfile
)
1380 objfile
->sf
->qf
->expand_symtabs_for_function (objfile
, func_name
);
1383 /* Search upwards from currently selected frame (so that we can
1384 complete on local vars. */
1386 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
1387 make_symbol_overload_list_block (func_name
, b
);
1389 surrounding_static_block
= block_static_block (get_selected_block (0));
1391 /* Go through the symtabs and check the externs and statics for
1392 symbols which match. */
1394 ALL_COMPUNITS (objfile
, cust
)
1397 b
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), GLOBAL_BLOCK
);
1398 make_symbol_overload_list_block (func_name
, b
);
1401 ALL_COMPUNITS (objfile
, cust
)
1404 b
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), STATIC_BLOCK
);
1405 /* Don't do this block twice. */
1406 if (b
== surrounding_static_block
)
1408 make_symbol_overload_list_block (func_name
, b
);
1412 /* Lookup the rtti type for a class name. */
1415 cp_lookup_rtti_type (const char *name
, struct block
*block
)
1417 struct symbol
* rtti_sym
;
1418 struct type
* rtti_type
;
1420 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1421 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
1422 rtti_sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
).symbol
;
1424 if (rtti_sym
== NULL
)
1426 warning (_("RTTI symbol not found for class '%s'"), name
);
1430 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
1432 warning (_("RTTI symbol for class '%s' is not a type"), name
);
1436 rtti_type
= check_typedef (SYMBOL_TYPE (rtti_sym
));
1438 switch (TYPE_CODE (rtti_type
))
1440 case TYPE_CODE_STRUCT
:
1442 case TYPE_CODE_NAMESPACE
:
1443 /* chastain/2003-11-26: the symbol tables often contain fake
1444 symbols for namespaces with the same name as the struct.
1445 This warning is an indication of a bug in the lookup order
1446 or a bug in the way that the symbol tables are populated. */
1447 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
1450 warning (_("RTTI symbol for class '%s' has bad type"), name
);
1457 #ifdef HAVE_WORKING_FORK
1459 /* If nonzero, attempt to catch crashes in the demangler and print
1460 useful debugging information. */
1462 static int catch_demangler_crashes
= 1;
1464 /* Stack context and environment for demangler crash recovery. */
1466 static SIGJMP_BUF gdb_demangle_jmp_buf
;
1468 /* If nonzero, attempt to dump core from the signal handler. */
1470 static int gdb_demangle_attempt_core_dump
= 1;
1472 /* Signal handler for gdb_demangle. */
1475 gdb_demangle_signal_handler (int signo
)
1477 if (gdb_demangle_attempt_core_dump
)
1482 gdb_demangle_attempt_core_dump
= 0;
1485 SIGLONGJMP (gdb_demangle_jmp_buf
, signo
);
1490 /* A wrapper for bfd_demangle. */
1493 gdb_demangle (const char *name
, int options
)
1495 char *result
= NULL
;
1496 int crash_signal
= 0;
1498 #ifdef HAVE_WORKING_FORK
1499 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1500 struct sigaction sa
, old_sa
;
1504 static int core_dump_allowed
= -1;
1506 if (core_dump_allowed
== -1)
1508 core_dump_allowed
= can_dump_core (LIMIT_CUR
);
1510 if (!core_dump_allowed
)
1511 gdb_demangle_attempt_core_dump
= 0;
1514 if (catch_demangler_crashes
)
1516 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1517 sa
.sa_handler
= gdb_demangle_signal_handler
;
1518 sigemptyset (&sa
.sa_mask
);
1519 #ifdef HAVE_SIGALTSTACK
1520 sa
.sa_flags
= SA_ONSTACK
;
1524 sigaction (SIGSEGV
, &sa
, &old_sa
);
1526 ofunc
= signal (SIGSEGV
, gdb_demangle_signal_handler
);
1529 crash_signal
= SIGSETJMP (gdb_demangle_jmp_buf
);
1533 if (crash_signal
== 0)
1534 result
= bfd_demangle (NULL
, name
, options
);
1536 #ifdef HAVE_WORKING_FORK
1537 if (catch_demangler_crashes
)
1539 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1540 sigaction (SIGSEGV
, &old_sa
, NULL
);
1542 signal (SIGSEGV
, ofunc
);
1545 if (crash_signal
!= 0)
1547 static int error_reported
= 0;
1549 if (!error_reported
)
1551 char *short_msg
, *long_msg
;
1552 struct cleanup
*back_to
;
1554 short_msg
= xstrprintf (_("unable to demangle '%s' "
1555 "(demangler failed with signal %d)"),
1556 name
, crash_signal
);
1557 back_to
= make_cleanup (xfree
, short_msg
);
1559 long_msg
= xstrprintf ("%s:%d: %s: %s", __FILE__
, __LINE__
,
1560 "demangler-warning", short_msg
);
1561 make_cleanup (xfree
, long_msg
);
1563 make_cleanup_restore_target_terminal ();
1564 target_terminal_ours_for_output ();
1567 if (core_dump_allowed
)
1568 fprintf_unfiltered (gdb_stderr
,
1569 _("%s\nAttempting to dump core.\n"),
1572 warn_cant_dump_core (long_msg
);
1574 demangler_warning (__FILE__
, __LINE__
, "%s", short_msg
);
1576 do_cleanups (back_to
);
1589 /* See cp-support.h. */
1592 gdb_sniff_from_mangled_name (const char *mangled
, char **demangled
)
1594 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
1595 return *demangled
!= NULL
;
1598 /* Don't allow just "maintenance cplus". */
1601 maint_cplus_command (char *arg
, int from_tty
)
1603 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1604 "by the name of a command.\n"));
1605 help_list (maint_cplus_cmd_list
,
1606 "maintenance cplus ",
1607 all_commands
, gdb_stdout
);
1610 /* This is a front end for cp_find_first_component, for unit testing.
1611 Be careful when using it: see the NOTE above
1612 cp_find_first_component. */
1615 first_component_command (char *arg
, int from_tty
)
1623 len
= cp_find_first_component (arg
);
1624 prefix
= (char *) alloca (len
+ 1);
1626 memcpy (prefix
, arg
, len
);
1629 printf_unfiltered ("%s\n", prefix
);
1632 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
1635 /* Implement "info vtbl". */
1638 info_vtbl_command (char *arg
, int from_tty
)
1640 struct value
*value
;
1642 value
= parse_and_eval (arg
);
1643 cplus_print_vtable (value
);
1647 _initialize_cp_support (void)
1649 add_prefix_cmd ("cplus", class_maintenance
,
1650 maint_cplus_command
,
1651 _("C++ maintenance commands."),
1652 &maint_cplus_cmd_list
,
1653 "maintenance cplus ",
1654 0, &maintenancelist
);
1655 add_alias_cmd ("cp", "cplus",
1656 class_maintenance
, 1,
1659 add_cmd ("first_component",
1661 first_component_command
,
1662 _("Print the first class/namespace component of NAME."),
1663 &maint_cplus_cmd_list
);
1665 add_info ("vtbl", info_vtbl_command
,
1666 _("Show the virtual function table for a C++ object.\n\
1667 Usage: info vtbl EXPRESSION\n\
1668 Evaluate EXPRESSION and display the virtual function table for the\n\
1669 resulting object."));
1671 #ifdef HAVE_WORKING_FORK
1672 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance
,
1673 &catch_demangler_crashes
, _("\
1674 Set whether to attempt to catch demangler crashes."), _("\
1675 Show whether to attempt to catch demangler crashes."), _("\
1676 If enabled GDB will attempt to catch demangler crashes and\n\
1677 display the offending symbol."),
1680 &maintenance_set_cmdlist
,
1681 &maintenance_show_cmdlist
);