1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2005, 2007-2012 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "cp-support.h"
23 #include "gdb_string.h"
25 #include "gdb_assert.h"
27 #include "dictionary.h"
32 #include "complaints.h"
34 #include "exceptions.h"
35 #include "expression.h"
39 #include "safe-ctype.h"
43 #define d_left(dc) (dc)->u.s_binary.left
44 #define d_right(dc) (dc)->u.s_binary.right
46 /* Functions related to demangled name parsing. */
48 static unsigned int cp_find_first_component_aux (const char *name
,
51 static void demangled_name_complaint (const char *name
);
53 /* Functions/variables related to overload resolution. */
55 static int sym_return_val_size
= -1;
56 static int sym_return_val_index
;
57 static struct symbol
**sym_return_val
;
59 static void overload_list_add_symbol (struct symbol
*sym
,
60 const char *oload_name
);
62 static void make_symbol_overload_list_using (const char *func_name
,
63 const char *namespace);
65 static void make_symbol_overload_list_qualified (const char *func_name
);
67 /* The list of "maint cplus" commands. */
69 struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
71 /* The actual commands. */
73 static void maint_cplus_command (char *arg
, int from_tty
);
74 static void first_component_command (char *arg
, int from_tty
);
76 /* Operator validation.
77 NOTE: Multi-byte operators (usually the assignment variety
78 operator) must appear before the single byte version, i.e., "+="
80 static const char *operator_tokens
[] =
82 "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*",
83 "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<",
84 ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=",
85 "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete"
86 /* new[] and delete[] require special whitespace handling */
89 /* A list of typedefs which should not be substituted by replace_typedefs. */
90 static const char * const ignore_typedefs
[] =
92 "std::istream", "std::iostream", "std::ostream", "std::string"
96 replace_typedefs (struct demangle_parse_info
*info
,
97 struct demangle_component
*ret_comp
);
99 /* A convenience function to copy STRING into OBSTACK, returning a pointer
100 to the newly allocated string and saving the number of bytes saved in LEN.
102 It does not copy the terminating '\0' byte! */
105 copy_string_to_obstack (struct obstack
*obstack
, const char *string
,
108 *len
= strlen (string
);
109 return obstack_copy (obstack
, string
, *len
);
112 /* A cleanup wrapper for cp_demangled_name_parse_free. */
115 do_demangled_name_parse_free_cleanup (void *data
)
117 struct demangle_parse_info
*info
= (struct demangle_parse_info
*) data
;
119 cp_demangled_name_parse_free (info
);
122 /* Create a cleanup for C++ name parsing. */
125 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info
*info
)
127 return make_cleanup (do_demangled_name_parse_free_cleanup
, info
);
130 /* Return 1 if STRING is clearly already in canonical form. This
131 function is conservative; things which it does not recognize are
132 assumed to be non-canonical, and the parser will sort them out
133 afterwards. This speeds up the critical path for alphanumeric
137 cp_already_canonical (const char *string
)
139 /* Identifier start character [a-zA-Z_]. */
140 if (!ISIDST (string
[0]))
143 /* These are the only two identifiers which canonicalize to other
144 than themselves or an error: unsigned -> unsigned int and
146 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
148 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
151 /* Identifier character [a-zA-Z0-9_]. */
152 while (ISIDNUM (string
[1]))
155 if (string
[1] == '\0')
161 /* Inspect the given RET_COMP for its type. If it is a typedef,
162 replace the node with the typedef's tree.
164 Returns 1 if any typedef substitutions were made, 0 otherwise. */
167 inspect_type (struct demangle_parse_info
*info
,
168 struct demangle_component
*ret_comp
)
173 volatile struct gdb_exception except
;
175 /* Copy the symbol's name from RET_COMP and look it up
176 in the symbol table. */
177 name
= (char *) alloca (ret_comp
->u
.s_name
.len
+ 1);
178 memcpy (name
, ret_comp
->u
.s_name
.s
, ret_comp
->u
.s_name
.len
);
179 name
[ret_comp
->u
.s_name
.len
] = '\0';
181 /* Ignore any typedefs that should not be substituted. */
182 for (i
= 0; i
< ARRAY_SIZE (ignore_typedefs
); ++i
)
184 if (strcmp (name
, ignore_typedefs
[i
]) == 0)
189 TRY_CATCH (except
, RETURN_MASK_ALL
)
191 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0);
194 if (except
.reason
>= 0 && sym
!= NULL
)
196 struct type
*otype
= SYMBOL_TYPE (sym
);
198 /* If the type is a typedef, replace it. */
199 if (TYPE_CODE (otype
) == TYPE_CODE_TYPEDEF
)
204 struct demangle_parse_info
*i
;
207 /* Get the real type of the typedef. */
208 type
= check_typedef (otype
);
210 is_anon
= (TYPE_TAG_NAME (type
) == NULL
211 && (TYPE_CODE (type
) == TYPE_CODE_ENUM
212 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
213 || TYPE_CODE (type
) == TYPE_CODE_UNION
));
216 struct type
*last
= otype
;
218 /* Find the last typedef for the type. */
219 while (TYPE_TARGET_TYPE (last
) != NULL
220 && (TYPE_CODE (TYPE_TARGET_TYPE (last
))
221 == TYPE_CODE_TYPEDEF
))
222 last
= TYPE_TARGET_TYPE (last
);
224 /* If there is only one typedef for this anonymous type,
225 do not substitute it. */
229 /* Use the last typedef seen as the type for this
234 buf
= mem_fileopen ();
235 TRY_CATCH (except
, RETURN_MASK_ERROR
)
237 type_print (type
, "", buf
, -1);
240 /* If type_print threw an exception, there is little point
241 in continuing, so just bow out gracefully. */
242 if (except
.reason
< 0)
244 ui_file_delete (buf
);
248 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
249 ui_file_delete (buf
);
251 /* Turn the result into a new tree. Note that this
252 tree will contain pointers into NAME, so NAME cannot
253 be free'd until all typedef conversion is done and
254 the final result is converted into a string. */
255 i
= cp_demangled_name_to_comp (name
, NULL
);
258 /* Merge the two trees. */
259 cp_merge_demangle_parse_infos (info
, ret_comp
, i
);
261 /* Replace any newly introduced typedefs -- but not
262 if the type is anonymous (that would lead to infinite
265 replace_typedefs (info
, ret_comp
);
269 /* This shouldn't happen unless the type printer has
270 output something that the name parser cannot grok.
271 Nonetheless, an ounce of prevention...
273 Canonicalize the name again, and store it in the
274 current node (RET_COMP). */
275 char *canon
= cp_canonicalize_string_no_typedefs (name
);
279 /* Copy the canonicalization into the obstack and
281 name
= copy_string_to_obstack (&info
->obstack
, canon
, &len
);
285 ret_comp
->u
.s_name
.s
= name
;
286 ret_comp
->u
.s_name
.len
= len
;
296 /* Replace any typedefs appearing in the qualified name
297 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
301 replace_typedefs_qualified_name (struct demangle_parse_info
*info
,
302 struct demangle_component
*ret_comp
)
306 struct ui_file
*buf
= mem_fileopen ();
307 struct demangle_component
*comp
= ret_comp
;
309 /* Walk each node of the qualified name, reconstructing the name of
310 this element. With every node, check for any typedef substitutions.
311 If a substitution has occurred, replace the qualified name node
312 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
314 while (comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
316 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_NAME
)
318 struct demangle_component
new;
320 ui_file_write (buf
, d_left (comp
)->u
.s_name
.s
,
321 d_left (comp
)->u
.s_name
.len
);
322 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
323 new.type
= DEMANGLE_COMPONENT_NAME
;
324 new.u
.s_name
.s
= name
;
325 new.u
.s_name
.len
= len
;
326 if (inspect_type (info
, &new))
331 /* A typedef was substituted in NEW. Convert it to a
332 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
335 ui_file_rewind (buf
);
336 n
= cp_comp_to_string (&new, 100);
339 /* If something went astray, abort typedef substitutions. */
340 ui_file_delete (buf
);
344 s
= copy_string_to_obstack (&info
->obstack
, n
, &slen
);
347 d_left (ret_comp
)->type
= DEMANGLE_COMPONENT_NAME
;
348 d_left (ret_comp
)->u
.s_name
.s
= s
;
349 d_left (ret_comp
)->u
.s_name
.len
= slen
;
350 d_right (ret_comp
) = d_right (comp
);
357 /* The current node is not a name, so simply replace any
358 typedefs in it. Then print it to the stream to continue
359 checking for more typedefs in the tree. */
360 replace_typedefs (info
, d_left (comp
));
361 name
= cp_comp_to_string (d_left (comp
), 100);
364 /* If something went astray, abort typedef substitutions. */
365 ui_file_delete (buf
);
368 fputs_unfiltered (name
, buf
);
371 ui_file_write (buf
, "::", 2);
372 comp
= d_right (comp
);
375 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
376 name assembled above and append the name given by COMP. Then use this
377 reassembled name to check for a typedef. */
379 if (comp
->type
== DEMANGLE_COMPONENT_NAME
)
381 ui_file_write (buf
, comp
->u
.s_name
.s
, comp
->u
.s_name
.len
);
382 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
384 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
385 with a DEMANGLE_COMPONENT_NAME node containing the whole
387 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
388 ret_comp
->u
.s_name
.s
= name
;
389 ret_comp
->u
.s_name
.len
= len
;
390 inspect_type (info
, ret_comp
);
393 replace_typedefs (info
, comp
);
395 ui_file_delete (buf
);
399 /* A function to check const and volatile qualifiers for argument types.
401 "Parameter declarations that differ only in the presence
402 or absence of `const' and/or `volatile' are equivalent."
403 C++ Standard N3290, clause 13.1.3 #4. */
406 check_cv_qualifiers (struct demangle_component
*ret_comp
)
408 while (d_left (ret_comp
) != NULL
409 && (d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_CONST
410 || d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_VOLATILE
))
412 d_left (ret_comp
) = d_left (d_left (ret_comp
));
416 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
417 their basic types. */
420 replace_typedefs (struct demangle_parse_info
*info
,
421 struct demangle_component
*ret_comp
)
425 switch (ret_comp
->type
)
427 case DEMANGLE_COMPONENT_ARGLIST
:
428 check_cv_qualifiers (ret_comp
);
431 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
432 case DEMANGLE_COMPONENT_TEMPLATE
:
433 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
434 case DEMANGLE_COMPONENT_TYPED_NAME
:
435 replace_typedefs (info
, d_left (ret_comp
));
436 replace_typedefs (info
, d_right (ret_comp
));
439 case DEMANGLE_COMPONENT_NAME
:
440 inspect_type (info
, ret_comp
);
443 case DEMANGLE_COMPONENT_QUAL_NAME
:
444 replace_typedefs_qualified_name (info
, ret_comp
);
447 case DEMANGLE_COMPONENT_LOCAL_NAME
:
448 case DEMANGLE_COMPONENT_CTOR
:
449 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
450 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
451 replace_typedefs (info
, d_right (ret_comp
));
454 case DEMANGLE_COMPONENT_CONST
:
455 case DEMANGLE_COMPONENT_RESTRICT
:
456 case DEMANGLE_COMPONENT_VOLATILE
:
457 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
458 case DEMANGLE_COMPONENT_CONST_THIS
:
459 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
460 case DEMANGLE_COMPONENT_POINTER
:
461 case DEMANGLE_COMPONENT_REFERENCE
:
462 replace_typedefs (info
, d_left (ret_comp
));
471 /* Parse STRING and convert it to canonical form, resolving any typedefs.
472 If parsing fails, or if STRING is already canonical, return NULL.
473 Otherwise return the canonical form. The return value is allocated via
477 cp_canonicalize_string_no_typedefs (const char *string
)
480 unsigned int estimated_len
;
481 struct demangle_parse_info
*info
;
484 estimated_len
= strlen (string
) * 2;
485 info
= cp_demangled_name_to_comp (string
, NULL
);
488 /* Replace all the typedefs in the tree. */
489 replace_typedefs (info
, info
->tree
);
491 /* Convert the tree back into a string. */
492 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
493 gdb_assert (ret
!= NULL
);
495 /* Free the parse information. */
496 cp_demangled_name_parse_free (info
);
498 /* Finally, compare the original string with the computed
499 name, returning NULL if they are the same. */
500 if (strcmp (string
, ret
) == 0)
510 /* Parse STRING and convert it to canonical form. If parsing fails,
511 or if STRING is already canonical, return NULL. Otherwise return
512 the canonical form. The return value is allocated via xmalloc. */
515 cp_canonicalize_string (const char *string
)
517 struct demangle_parse_info
*info
;
518 unsigned int estimated_len
;
521 if (cp_already_canonical (string
))
524 info
= cp_demangled_name_to_comp (string
, NULL
);
528 estimated_len
= strlen (string
) * 2;
529 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
530 cp_demangled_name_parse_free (info
);
534 warning (_("internal error: string \"%s\" failed to be canonicalized"),
539 if (strcmp (string
, ret
) == 0)
548 /* Convert a mangled name to a demangle_component tree. *MEMORY is
549 set to the block of used memory that should be freed when finished
550 with the tree. DEMANGLED_P is set to the char * that should be
551 freed when finished with the tree, or NULL if none was needed.
552 OPTIONS will be passed to the demangler. */
554 static struct demangle_parse_info
*
555 mangled_name_to_comp (const char *mangled_name
, int options
,
556 void **memory
, char **demangled_p
)
558 char *demangled_name
;
559 struct demangle_parse_info
*info
;
561 /* If it looks like a v3 mangled name, then try to go directly
563 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
565 struct demangle_component
*ret
;
567 ret
= cplus_demangle_v3_components (mangled_name
,
571 info
= cp_new_demangle_parse_info ();
578 /* If it doesn't, or if that failed, then try to demangle the
580 demangled_name
= cplus_demangle (mangled_name
, options
);
581 if (demangled_name
== NULL
)
584 /* If we could demangle the name, parse it to build the component
586 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
590 xfree (demangled_name
);
594 *demangled_p
= demangled_name
;
598 /* Return the name of the class containing method PHYSNAME. */
601 cp_class_name_from_physname (const char *physname
)
603 void *storage
= NULL
;
604 char *demangled_name
= NULL
, *ret
;
605 struct demangle_component
*ret_comp
, *prev_comp
, *cur_comp
;
606 struct demangle_parse_info
*info
;
609 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
610 &storage
, &demangled_name
);
615 ret_comp
= info
->tree
;
617 /* First strip off any qualifiers, if we have a function or
620 switch (ret_comp
->type
)
622 case DEMANGLE_COMPONENT_CONST
:
623 case DEMANGLE_COMPONENT_RESTRICT
:
624 case DEMANGLE_COMPONENT_VOLATILE
:
625 case DEMANGLE_COMPONENT_CONST_THIS
:
626 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
627 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
628 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
629 ret_comp
= d_left (ret_comp
);
636 /* If what we have now is a function, discard the argument list. */
637 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
638 ret_comp
= d_left (ret_comp
);
640 /* If what we have now is a template, strip off the template
641 arguments. The left subtree may be a qualified name. */
642 if (ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
643 ret_comp
= d_left (ret_comp
);
645 /* What we have now should be a name, possibly qualified.
646 Additional qualifiers could live in the left subtree or the right
647 subtree. Find the last piece. */
652 switch (cur_comp
->type
)
654 case DEMANGLE_COMPONENT_QUAL_NAME
:
655 case DEMANGLE_COMPONENT_LOCAL_NAME
:
656 prev_comp
= cur_comp
;
657 cur_comp
= d_right (cur_comp
);
659 case DEMANGLE_COMPONENT_TEMPLATE
:
660 case DEMANGLE_COMPONENT_NAME
:
661 case DEMANGLE_COMPONENT_CTOR
:
662 case DEMANGLE_COMPONENT_DTOR
:
663 case DEMANGLE_COMPONENT_OPERATOR
:
664 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
674 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
676 /* We want to discard the rightmost child of PREV_COMP. */
677 *prev_comp
= *d_left (prev_comp
);
678 /* The ten is completely arbitrary; we don't have a good
680 ret
= cp_comp_to_string (ret_comp
, 10);
684 xfree (demangled_name
);
685 cp_demangled_name_parse_free (info
);
689 /* Return the child of COMP which is the basename of a method,
690 variable, et cetera. All scope qualifiers are discarded, but
691 template arguments will be included. The component tree may be
694 static struct demangle_component
*
695 unqualified_name_from_comp (struct demangle_component
*comp
)
697 struct demangle_component
*ret_comp
= comp
, *last_template
;
701 last_template
= NULL
;
703 switch (ret_comp
->type
)
705 case DEMANGLE_COMPONENT_QUAL_NAME
:
706 case DEMANGLE_COMPONENT_LOCAL_NAME
:
707 ret_comp
= d_right (ret_comp
);
709 case DEMANGLE_COMPONENT_TYPED_NAME
:
710 ret_comp
= d_left (ret_comp
);
712 case DEMANGLE_COMPONENT_TEMPLATE
:
713 gdb_assert (last_template
== NULL
);
714 last_template
= ret_comp
;
715 ret_comp
= d_left (ret_comp
);
717 case DEMANGLE_COMPONENT_CONST
:
718 case DEMANGLE_COMPONENT_RESTRICT
:
719 case DEMANGLE_COMPONENT_VOLATILE
:
720 case DEMANGLE_COMPONENT_CONST_THIS
:
721 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
722 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
723 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
724 ret_comp
= d_left (ret_comp
);
726 case DEMANGLE_COMPONENT_NAME
:
727 case DEMANGLE_COMPONENT_CTOR
:
728 case DEMANGLE_COMPONENT_DTOR
:
729 case DEMANGLE_COMPONENT_OPERATOR
:
730 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
740 d_left (last_template
) = ret_comp
;
741 return last_template
;
747 /* Return the name of the method whose linkage name is PHYSNAME. */
750 method_name_from_physname (const char *physname
)
752 void *storage
= NULL
;
753 char *demangled_name
= NULL
, *ret
;
754 struct demangle_component
*ret_comp
;
755 struct demangle_parse_info
*info
;
757 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
758 &storage
, &demangled_name
);
762 ret_comp
= unqualified_name_from_comp (info
->tree
);
765 if (ret_comp
!= NULL
)
766 /* The ten is completely arbitrary; we don't have a good
768 ret
= cp_comp_to_string (ret_comp
, 10);
771 xfree (demangled_name
);
772 cp_demangled_name_parse_free (info
);
776 /* If FULL_NAME is the demangled name of a C++ function (including an
777 arg list, possibly including namespace/class qualifications),
778 return a new string containing only the function name (without the
779 arg list/class qualifications). Otherwise, return NULL. The
780 caller is responsible for freeing the memory in question. */
783 cp_func_name (const char *full_name
)
786 struct demangle_component
*ret_comp
;
787 struct demangle_parse_info
*info
;
789 info
= cp_demangled_name_to_comp (full_name
, NULL
);
793 ret_comp
= unqualified_name_from_comp (info
->tree
);
796 if (ret_comp
!= NULL
)
797 ret
= cp_comp_to_string (ret_comp
, 10);
799 cp_demangled_name_parse_free (info
);
803 /* DEMANGLED_NAME is the name of a function, including parameters and
804 (optionally) a return type. Return the name of the function without
805 parameters or return type, or NULL if we can not parse the name. */
808 cp_remove_params (const char *demangled_name
)
811 struct demangle_component
*ret_comp
;
812 struct demangle_parse_info
*info
;
815 if (demangled_name
== NULL
)
818 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
822 /* First strip off any qualifiers, if we have a function or method. */
823 ret_comp
= info
->tree
;
825 switch (ret_comp
->type
)
827 case DEMANGLE_COMPONENT_CONST
:
828 case DEMANGLE_COMPONENT_RESTRICT
:
829 case DEMANGLE_COMPONENT_VOLATILE
:
830 case DEMANGLE_COMPONENT_CONST_THIS
:
831 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
832 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
833 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
834 ret_comp
= d_left (ret_comp
);
841 /* What we have now should be a function. Return its name. */
842 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
843 ret
= cp_comp_to_string (d_left (ret_comp
), 10);
845 cp_demangled_name_parse_free (info
);
849 /* Here are some random pieces of trivia to keep in mind while trying
850 to take apart demangled names:
852 - Names can contain function arguments or templates, so the process
853 has to be, to some extent recursive: maybe keep track of your
854 depth based on encountering <> and ().
856 - Parentheses don't just have to happen at the end of a name: they
857 can occur even if the name in question isn't a function, because
858 a template argument might be a type that's a function.
860 - Conversely, even if you're trying to deal with a function, its
861 demangled name might not end with ')': it could be a const or
862 volatile class method, in which case it ends with "const" or
865 - Parentheses are also used in anonymous namespaces: a variable
866 'foo' in an anonymous namespace gets demangled as "(anonymous
869 - And operator names can contain parentheses or angle brackets. */
871 /* FIXME: carlton/2003-03-13: We have several functions here with
872 overlapping functionality; can we combine them? Also, do they
873 handle all the above considerations correctly? */
876 /* This returns the length of first component of NAME, which should be
877 the demangled name of a C++ variable/function/method/etc.
878 Specifically, it returns the index of the first colon forming the
879 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
880 it returns the 1, and given 'foo', it returns 0. */
882 /* The character in NAME indexed by the return value is guaranteed to
883 always be either ':' or '\0'. */
885 /* NOTE: carlton/2003-03-13: This function is currently only intended
886 for internal use: it's probably not entirely safe when called on
887 user-generated input, because some of the 'index += 2' lines in
888 cp_find_first_component_aux might go past the end of malformed
892 cp_find_first_component (const char *name
)
894 return cp_find_first_component_aux (name
, 0);
897 /* Helper function for cp_find_first_component. Like that function,
898 it returns the length of the first component of NAME, but to make
899 the recursion easier, it also stops if it reaches an unexpected ')'
900 or '>' if the value of PERMISSIVE is nonzero. */
902 /* Let's optimize away calls to strlen("operator"). */
904 #define LENGTH_OF_OPERATOR 8
907 cp_find_first_component_aux (const char *name
, int permissive
)
909 unsigned int index
= 0;
910 /* Operator names can show up in unexpected places. Since these can
911 contain parentheses or angle brackets, they can screw up the
912 recursion. But not every string 'operator' is part of an
913 operater name: e.g. you could have a variable 'cooperator'. So
914 this variable tells us whether or not we should treat the string
915 'operator' as starting an operator. */
916 int operator_possible
= 1;
923 /* Template; eat it up. The calls to cp_first_component
924 should only return (I hope!) when they reach the '>'
925 terminating the component or a '::' between two
926 components. (Hence the '+ 2'.) */
928 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
930 index
+= cp_find_first_component_aux (name
+ index
, 1))
932 if (name
[index
] != ':')
934 demangled_name_complaint (name
);
935 return strlen (name
);
939 operator_possible
= 1;
942 /* Similar comment as to '<'. */
944 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
946 index
+= cp_find_first_component_aux (name
+ index
, 1))
948 if (name
[index
] != ':')
950 demangled_name_complaint (name
);
951 return strlen (name
);
955 operator_possible
= 1;
963 demangled_name_complaint (name
);
964 return strlen (name
);
970 /* Operator names can screw up the recursion. */
971 if (operator_possible
972 && strncmp (name
+ index
, "operator",
973 LENGTH_OF_OPERATOR
) == 0)
975 index
+= LENGTH_OF_OPERATOR
;
976 while (ISSPACE(name
[index
]))
980 /* Skip over one less than the appropriate number of
981 characters: the for loop will skip over the last
984 if (name
[index
+ 1] == '<')
991 if (name
[index
+ 1] == '>')
1004 operator_possible
= 0;
1011 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1012 set of relevant characters are here: it's necessary to
1013 include any character that can show up before 'operator'
1014 in a demangled name, and it's safe to include any
1015 character that can't be part of an identifier's name. */
1016 operator_possible
= 1;
1019 operator_possible
= 0;
1025 /* Complain about a demangled name that we don't know how to parse.
1026 NAME is the demangled name in question. */
1029 demangled_name_complaint (const char *name
)
1031 complaint (&symfile_complaints
,
1032 "unexpected demangled name '%s'", name
);
1035 /* If NAME is the fully-qualified name of a C++
1036 function/variable/method/etc., this returns the length of its
1037 entire prefix: all of the namespaces and classes that make up its
1038 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1039 4, given 'foo', it returns 0. */
1042 cp_entire_prefix_len (const char *name
)
1044 unsigned int current_len
= cp_find_first_component (name
);
1045 unsigned int previous_len
= 0;
1047 while (name
[current_len
] != '\0')
1049 gdb_assert (name
[current_len
] == ':');
1050 previous_len
= current_len
;
1051 /* Skip the '::'. */
1053 current_len
+= cp_find_first_component (name
+ current_len
);
1056 return previous_len
;
1059 /* Overload resolution functions. */
1061 /* Test to see if SYM is a symbol that we haven't seen corresponding
1062 to a function named OLOAD_NAME. If so, add it to the current
1066 overload_list_add_symbol (struct symbol
*sym
,
1067 const char *oload_name
)
1073 /* If there is no type information, we can't do anything, so
1075 if (SYMBOL_TYPE (sym
) == NULL
)
1078 /* skip any symbols that we've already considered. */
1079 for (i
= 0; i
< sym_return_val_index
; ++i
)
1080 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
1081 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
1084 /* Get the demangled name without parameters */
1085 sym_name
= cp_remove_params (SYMBOL_NATURAL_NAME (sym
));
1089 /* skip symbols that cannot match */
1090 if (strcmp (sym_name
, oload_name
) != 0)
1098 /* We have a match for an overload instance, so add SYM to the
1099 current list of overload instances */
1100 if (sym_return_val_index
+ 3 > sym_return_val_size
)
1102 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
1103 sym_return_val
= (struct symbol
**)
1104 xrealloc ((char *) sym_return_val
, newsize
);
1106 sym_return_val
[sym_return_val_index
++] = sym
;
1107 sym_return_val
[sym_return_val_index
] = NULL
;
1110 /* Return a null-terminated list of pointers to function symbols that
1111 are named FUNC_NAME and are visible within NAMESPACE. */
1114 make_symbol_overload_list (const char *func_name
,
1115 const char *namespace)
1117 struct cleanup
*old_cleanups
;
1120 sym_return_val_size
= 100;
1121 sym_return_val_index
= 0;
1122 sym_return_val
= xmalloc ((sym_return_val_size
+ 1) *
1123 sizeof (struct symbol
*));
1124 sym_return_val
[0] = NULL
;
1126 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
1128 make_symbol_overload_list_using (func_name
, namespace);
1130 if (namespace[0] == '\0')
1134 char *concatenated_name
1135 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1136 strcpy (concatenated_name
, namespace);
1137 strcat (concatenated_name
, "::");
1138 strcat (concatenated_name
, func_name
);
1139 name
= concatenated_name
;
1142 make_symbol_overload_list_qualified (name
);
1144 discard_cleanups (old_cleanups
);
1146 return sym_return_val
;
1149 /* Add all symbols with a name matching NAME in BLOCK to the overload
1153 make_symbol_overload_list_block (const char *name
,
1154 const struct block
*block
)
1156 struct dict_iterator iter
;
1159 const struct dictionary
*dict
= BLOCK_DICT (block
);
1161 for (sym
= dict_iter_name_first (dict
, name
, &iter
);
1163 sym
= dict_iter_name_next (name
, &iter
))
1164 overload_list_add_symbol (sym
, name
);
1167 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1170 make_symbol_overload_list_namespace (const char *func_name
,
1171 const char *namespace)
1174 const struct block
*block
= NULL
;
1176 if (namespace[0] == '\0')
1180 char *concatenated_name
1181 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1183 strcpy (concatenated_name
, namespace);
1184 strcat (concatenated_name
, "::");
1185 strcat (concatenated_name
, func_name
);
1186 name
= concatenated_name
;
1189 /* Look in the static block. */
1190 block
= block_static_block (get_selected_block (0));
1192 make_symbol_overload_list_block (name
, block
);
1194 /* Look in the global block. */
1195 block
= block_global_block (block
);
1197 make_symbol_overload_list_block (name
, block
);
1201 /* Search the namespace of the given type and namespace of and public
1205 make_symbol_overload_list_adl_namespace (struct type
*type
,
1206 const char *func_name
)
1209 const char *type_name
;
1212 while (TYPE_CODE (type
) == TYPE_CODE_PTR
1213 || TYPE_CODE (type
) == TYPE_CODE_REF
1214 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1215 || TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1217 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1218 type
= check_typedef(type
);
1220 type
= TYPE_TARGET_TYPE (type
);
1223 type_name
= TYPE_NAME (type
);
1225 if (type_name
== NULL
)
1228 prefix_len
= cp_entire_prefix_len (type_name
);
1230 if (prefix_len
!= 0)
1232 namespace = alloca (prefix_len
+ 1);
1233 strncpy (namespace, type_name
, prefix_len
);
1234 namespace[prefix_len
] = '\0';
1236 make_symbol_overload_list_namespace (func_name
, namespace);
1239 /* Check public base type */
1240 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
1241 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
1243 if (BASETYPE_VIA_PUBLIC (type
, i
))
1244 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type
,
1250 /* Adds the overload list overload candidates for FUNC_NAME found
1251 through argument dependent lookup. */
1254 make_symbol_overload_list_adl (struct type
**arg_types
, int nargs
,
1255 const char *func_name
)
1259 gdb_assert (sym_return_val_size
!= -1);
1261 for (i
= 1; i
<= nargs
; i
++)
1262 make_symbol_overload_list_adl_namespace (arg_types
[i
- 1],
1265 return sym_return_val
;
1268 /* Used for cleanups to reset the "searched" flag in case of an
1272 reset_directive_searched (void *data
)
1274 struct using_direct
*direct
= data
;
1275 direct
->searched
= 0;
1278 /* This applies the using directives to add namespaces to search in,
1279 and then searches for overloads in all of those namespaces. It
1280 adds the symbols found to sym_return_val. Arguments are as in
1281 make_symbol_overload_list. */
1284 make_symbol_overload_list_using (const char *func_name
,
1285 const char *namespace)
1287 struct using_direct
*current
;
1288 const struct block
*block
;
1290 /* First, go through the using directives. If any of them apply,
1291 look in the appropriate namespaces for new functions to match
1294 for (block
= get_selected_block (0);
1296 block
= BLOCK_SUPERBLOCK (block
))
1297 for (current
= block_using (block
);
1299 current
= current
->next
)
1301 /* Prevent recursive calls. */
1302 if (current
->searched
)
1305 /* If this is a namespace alias or imported declaration ignore
1307 if (current
->alias
!= NULL
|| current
->declaration
!= NULL
)
1310 if (strcmp (namespace, current
->import_dest
) == 0)
1312 /* Mark this import as searched so that the recursive call
1313 does not search it again. */
1314 struct cleanup
*old_chain
;
1315 current
->searched
= 1;
1316 old_chain
= make_cleanup (reset_directive_searched
,
1319 make_symbol_overload_list_using (func_name
,
1320 current
->import_src
);
1322 current
->searched
= 0;
1323 discard_cleanups (old_chain
);
1327 /* Now, add names for this namespace. */
1328 make_symbol_overload_list_namespace (func_name
, namespace);
1331 /* This does the bulk of the work of finding overloaded symbols.
1332 FUNC_NAME is the name of the overloaded function we're looking for
1333 (possibly including namespace info). */
1336 make_symbol_overload_list_qualified (const char *func_name
)
1340 struct objfile
*objfile
;
1341 const struct block
*b
, *surrounding_static_block
= 0;
1342 struct dict_iterator iter
;
1343 const struct dictionary
*dict
;
1345 /* Look through the partial symtabs for all symbols which begin by
1346 matching FUNC_NAME. Make sure we read that symbol table in. */
1348 ALL_OBJFILES (objfile
)
1351 objfile
->sf
->qf
->expand_symtabs_for_function (objfile
, func_name
);
1354 /* Search upwards from currently selected frame (so that we can
1355 complete on local vars. */
1357 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
1358 make_symbol_overload_list_block (func_name
, b
);
1360 surrounding_static_block
= block_static_block (get_selected_block (0));
1362 /* Go through the symtabs and check the externs and statics for
1363 symbols which match. */
1365 ALL_PRIMARY_SYMTABS (objfile
, s
)
1368 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
1369 make_symbol_overload_list_block (func_name
, b
);
1372 ALL_PRIMARY_SYMTABS (objfile
, s
)
1375 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1376 /* Don't do this block twice. */
1377 if (b
== surrounding_static_block
)
1379 make_symbol_overload_list_block (func_name
, b
);
1383 /* Lookup the rtti type for a class name. */
1386 cp_lookup_rtti_type (const char *name
, struct block
*block
)
1388 struct symbol
* rtti_sym
;
1389 struct type
* rtti_type
;
1391 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
);
1393 if (rtti_sym
== NULL
)
1395 warning (_("RTTI symbol not found for class '%s'"), name
);
1399 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
1401 warning (_("RTTI symbol for class '%s' is not a type"), name
);
1405 rtti_type
= SYMBOL_TYPE (rtti_sym
);
1407 switch (TYPE_CODE (rtti_type
))
1409 case TYPE_CODE_CLASS
:
1411 case TYPE_CODE_NAMESPACE
:
1412 /* chastain/2003-11-26: the symbol tables often contain fake
1413 symbols for namespaces with the same name as the struct.
1414 This warning is an indication of a bug in the lookup order
1415 or a bug in the way that the symbol tables are populated. */
1416 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
1419 warning (_("RTTI symbol for class '%s' has bad type"), name
);
1426 /* Don't allow just "maintenance cplus". */
1429 maint_cplus_command (char *arg
, int from_tty
)
1431 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1432 "by the name of a command.\n"));
1433 help_list (maint_cplus_cmd_list
,
1434 "maintenance cplus ",
1438 /* This is a front end for cp_find_first_component, for unit testing.
1439 Be careful when using it: see the NOTE above
1440 cp_find_first_component. */
1443 first_component_command (char *arg
, int from_tty
)
1451 len
= cp_find_first_component (arg
);
1452 prefix
= alloca (len
+ 1);
1454 memcpy (prefix
, arg
, len
);
1457 printf_unfiltered ("%s\n", prefix
);
1460 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
1462 #define SKIP_SPACE(P) \
1465 while (*(P) == ' ' || *(P) == '\t') \
1470 /* Returns the length of the operator name or 0 if INPUT does not
1471 point to a valid C++ operator. INPUT should start with
1474 cp_validate_operator (const char *input
)
1479 struct expression
*expr
;
1481 volatile struct gdb_exception except
;
1485 if (strncmp (p
, "operator", 8) == 0)
1492 i
< sizeof (operator_tokens
) / sizeof (operator_tokens
[0]);
1495 int length
= strlen (operator_tokens
[i
]);
1497 /* By using strncmp here, we MUST have operator_tokens
1498 ordered! See additional notes where operator_tokens is
1500 if (strncmp (p
, operator_tokens
[i
], length
) == 0)
1507 if (strncmp (op
, "new", 3) == 0
1508 || strncmp (op
, "delete", 6) == 0)
1511 /* Special case: new[] and delete[]. We must be
1512 careful to swallow whitespace before/in "[]". */
1531 /* Check input for a conversion operator. */
1533 /* Skip past base typename. */
1534 while (*p
!= '*' && *p
!= '&' && *p
!= 0 && *p
!= ' ')
1538 /* Add modifiers '*' / '&'. */
1539 while (*p
== '*' || *p
== '&')
1545 /* Check for valid type. [Remember: input starts with
1547 copy
= savestring (input
+ 8, p
- input
- 8);
1550 TRY_CATCH (except
, RETURN_MASK_ALL
)
1552 expr
= parse_expression (copy
);
1553 val
= evaluate_type (expr
);
1560 if (val
!= NULL
&& value_type (val
) != NULL
)
1567 /* Implement "info vtbl". */
1570 info_vtbl_command (char *arg
, int from_tty
)
1572 struct value
*value
;
1574 value
= parse_and_eval (arg
);
1575 cplus_print_vtable (value
);
1579 _initialize_cp_support (void)
1581 add_prefix_cmd ("cplus", class_maintenance
,
1582 maint_cplus_command
,
1583 _("C++ maintenance commands."),
1584 &maint_cplus_cmd_list
,
1585 "maintenance cplus ",
1586 0, &maintenancelist
);
1587 add_alias_cmd ("cp", "cplus",
1588 class_maintenance
, 1,
1591 add_cmd ("first_component",
1593 first_component_command
,
1594 _("Print the first class/namespace component of NAME."),
1595 &maint_cplus_cmd_list
);
1597 add_info ("vtbl", info_vtbl_command
,
1598 _("Show the virtual function table for a C++ object.\n\
1599 Usage: info vtbl EXPRESSION\n\
1600 Evaluate EXPRESSION and display the virtual function table for the\n\
1601 resulting object."));