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 /* A list of typedefs which should not be substituted by replace_typedefs. */
77 static const char * const ignore_typedefs
[] =
79 "std::istream", "std::iostream", "std::ostream", "std::string"
83 replace_typedefs (struct demangle_parse_info
*info
,
84 struct demangle_component
*ret_comp
);
86 /* A convenience function to copy STRING into OBSTACK, returning a pointer
87 to the newly allocated string and saving the number of bytes saved in LEN.
89 It does not copy the terminating '\0' byte! */
92 copy_string_to_obstack (struct obstack
*obstack
, const char *string
,
95 *len
= strlen (string
);
96 return obstack_copy (obstack
, string
, *len
);
99 /* A cleanup wrapper for cp_demangled_name_parse_free. */
102 do_demangled_name_parse_free_cleanup (void *data
)
104 struct demangle_parse_info
*info
= (struct demangle_parse_info
*) data
;
106 cp_demangled_name_parse_free (info
);
109 /* Create a cleanup for C++ name parsing. */
112 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info
*info
)
114 return make_cleanup (do_demangled_name_parse_free_cleanup
, info
);
117 /* Return 1 if STRING is clearly already in canonical form. This
118 function is conservative; things which it does not recognize are
119 assumed to be non-canonical, and the parser will sort them out
120 afterwards. This speeds up the critical path for alphanumeric
124 cp_already_canonical (const char *string
)
126 /* Identifier start character [a-zA-Z_]. */
127 if (!ISIDST (string
[0]))
130 /* These are the only two identifiers which canonicalize to other
131 than themselves or an error: unsigned -> unsigned int and
133 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
135 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
138 /* Identifier character [a-zA-Z0-9_]. */
139 while (ISIDNUM (string
[1]))
142 if (string
[1] == '\0')
148 /* Inspect the given RET_COMP for its type. If it is a typedef,
149 replace the node with the typedef's tree.
151 Returns 1 if any typedef substitutions were made, 0 otherwise. */
154 inspect_type (struct demangle_parse_info
*info
,
155 struct demangle_component
*ret_comp
)
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
);
185 /* If the type is a typedef, replace it. */
186 if (TYPE_CODE (otype
) == TYPE_CODE_TYPEDEF
)
191 struct demangle_parse_info
*i
;
194 /* Get the real type of the typedef. */
195 type
= check_typedef (otype
);
197 is_anon
= (TYPE_TAG_NAME (type
) == NULL
198 && (TYPE_CODE (type
) == TYPE_CODE_ENUM
199 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
200 || TYPE_CODE (type
) == TYPE_CODE_UNION
));
203 struct type
*last
= otype
;
205 /* Find the last typedef for the type. */
206 while (TYPE_TARGET_TYPE (last
) != NULL
207 && (TYPE_CODE (TYPE_TARGET_TYPE (last
))
208 == TYPE_CODE_TYPEDEF
))
209 last
= TYPE_TARGET_TYPE (last
);
211 /* If there is only one typedef for this anonymous type,
212 do not substitute it. */
216 /* Use the last typedef seen as the type for this
221 buf
= mem_fileopen ();
222 TRY_CATCH (except
, RETURN_MASK_ERROR
)
224 type_print (type
, "", buf
, -1);
227 /* If type_print threw an exception, there is little point
228 in continuing, so just bow out gracefully. */
229 if (except
.reason
< 0)
231 ui_file_delete (buf
);
235 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
236 ui_file_delete (buf
);
238 /* Turn the result into a new tree. Note that this
239 tree will contain pointers into NAME, so NAME cannot
240 be free'd until all typedef conversion is done and
241 the final result is converted into a string. */
242 i
= cp_demangled_name_to_comp (name
, NULL
);
245 /* Merge the two trees. */
246 cp_merge_demangle_parse_infos (info
, ret_comp
, i
);
248 /* Replace any newly introduced typedefs -- but not
249 if the type is anonymous (that would lead to infinite
252 replace_typedefs (info
, ret_comp
);
256 /* This shouldn't happen unless the type printer has
257 output something that the name parser cannot grok.
258 Nonetheless, an ounce of prevention...
260 Canonicalize the name again, and store it in the
261 current node (RET_COMP). */
262 char *canon
= cp_canonicalize_string_no_typedefs (name
);
266 /* Copy the canonicalization into the obstack and
268 name
= copy_string_to_obstack (&info
->obstack
, canon
, &len
);
272 ret_comp
->u
.s_name
.s
= name
;
273 ret_comp
->u
.s_name
.len
= len
;
283 /* Replace any typedefs appearing in the qualified name
284 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
288 replace_typedefs_qualified_name (struct demangle_parse_info
*info
,
289 struct demangle_component
*ret_comp
)
293 struct ui_file
*buf
= mem_fileopen ();
294 struct demangle_component
*comp
= ret_comp
;
296 /* Walk each node of the qualified name, reconstructing the name of
297 this element. With every node, check for any typedef substitutions.
298 If a substitution has occurred, replace the qualified name node
299 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
301 while (comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
303 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_NAME
)
305 struct demangle_component
new;
307 ui_file_write (buf
, d_left (comp
)->u
.s_name
.s
,
308 d_left (comp
)->u
.s_name
.len
);
309 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
310 new.type
= DEMANGLE_COMPONENT_NAME
;
311 new.u
.s_name
.s
= name
;
312 new.u
.s_name
.len
= len
;
313 if (inspect_type (info
, &new))
318 /* A typedef was substituted in NEW. Convert it to a
319 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
322 ui_file_rewind (buf
);
323 n
= cp_comp_to_string (&new, 100);
326 /* If something went astray, abort typedef substitutions. */
327 ui_file_delete (buf
);
331 s
= copy_string_to_obstack (&info
->obstack
, n
, &slen
);
334 d_left (ret_comp
)->type
= DEMANGLE_COMPONENT_NAME
;
335 d_left (ret_comp
)->u
.s_name
.s
= s
;
336 d_left (ret_comp
)->u
.s_name
.len
= slen
;
337 d_right (ret_comp
) = d_right (comp
);
344 /* The current node is not a name, so simply replace any
345 typedefs in it. Then print it to the stream to continue
346 checking for more typedefs in the tree. */
347 replace_typedefs (info
, d_left (comp
));
348 name
= cp_comp_to_string (d_left (comp
), 100);
351 /* If something went astray, abort typedef substitutions. */
352 ui_file_delete (buf
);
355 fputs_unfiltered (name
, buf
);
358 ui_file_write (buf
, "::", 2);
359 comp
= d_right (comp
);
362 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
363 name assembled above and append the name given by COMP. Then use this
364 reassembled name to check for a typedef. */
366 if (comp
->type
== DEMANGLE_COMPONENT_NAME
)
368 ui_file_write (buf
, comp
->u
.s_name
.s
, comp
->u
.s_name
.len
);
369 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
371 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
372 with a DEMANGLE_COMPONENT_NAME node containing the whole
374 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
375 ret_comp
->u
.s_name
.s
= name
;
376 ret_comp
->u
.s_name
.len
= len
;
377 inspect_type (info
, ret_comp
);
380 replace_typedefs (info
, comp
);
382 ui_file_delete (buf
);
386 /* A function to check const and volatile qualifiers for argument types.
388 "Parameter declarations that differ only in the presence
389 or absence of `const' and/or `volatile' are equivalent."
390 C++ Standard N3290, clause 13.1.3 #4. */
393 check_cv_qualifiers (struct demangle_component
*ret_comp
)
395 while (d_left (ret_comp
) != NULL
396 && (d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_CONST
397 || d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_VOLATILE
))
399 d_left (ret_comp
) = d_left (d_left (ret_comp
));
403 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
404 their basic types. */
407 replace_typedefs (struct demangle_parse_info
*info
,
408 struct demangle_component
*ret_comp
)
412 switch (ret_comp
->type
)
414 case DEMANGLE_COMPONENT_ARGLIST
:
415 check_cv_qualifiers (ret_comp
);
418 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
419 case DEMANGLE_COMPONENT_TEMPLATE
:
420 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
421 case DEMANGLE_COMPONENT_TYPED_NAME
:
422 replace_typedefs (info
, d_left (ret_comp
));
423 replace_typedefs (info
, d_right (ret_comp
));
426 case DEMANGLE_COMPONENT_NAME
:
427 inspect_type (info
, ret_comp
);
430 case DEMANGLE_COMPONENT_QUAL_NAME
:
431 replace_typedefs_qualified_name (info
, ret_comp
);
434 case DEMANGLE_COMPONENT_LOCAL_NAME
:
435 case DEMANGLE_COMPONENT_CTOR
:
436 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
437 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
438 replace_typedefs (info
, d_right (ret_comp
));
441 case DEMANGLE_COMPONENT_CONST
:
442 case DEMANGLE_COMPONENT_RESTRICT
:
443 case DEMANGLE_COMPONENT_VOLATILE
:
444 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
445 case DEMANGLE_COMPONENT_CONST_THIS
:
446 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
447 case DEMANGLE_COMPONENT_POINTER
:
448 case DEMANGLE_COMPONENT_REFERENCE
:
449 replace_typedefs (info
, d_left (ret_comp
));
458 /* Parse STRING and convert it to canonical form, resolving any typedefs.
459 If parsing fails, or if STRING is already canonical, return NULL.
460 Otherwise return the canonical form. The return value is allocated via
464 cp_canonicalize_string_no_typedefs (const char *string
)
467 unsigned int estimated_len
;
468 struct demangle_parse_info
*info
;
471 estimated_len
= strlen (string
) * 2;
472 info
= cp_demangled_name_to_comp (string
, NULL
);
475 /* Replace all the typedefs in the tree. */
476 replace_typedefs (info
, info
->tree
);
478 /* Convert the tree back into a string. */
479 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
480 gdb_assert (ret
!= NULL
);
482 /* Free the parse information. */
483 cp_demangled_name_parse_free (info
);
485 /* Finally, compare the original string with the computed
486 name, returning NULL if they are the same. */
487 if (strcmp (string
, ret
) == 0)
497 /* Parse STRING and convert it to canonical form. If parsing fails,
498 or if STRING is already canonical, return NULL. Otherwise return
499 the canonical form. The return value is allocated via xmalloc. */
502 cp_canonicalize_string (const char *string
)
504 struct demangle_parse_info
*info
;
505 unsigned int estimated_len
;
508 if (cp_already_canonical (string
))
511 info
= cp_demangled_name_to_comp (string
, NULL
);
515 estimated_len
= strlen (string
) * 2;
516 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
517 cp_demangled_name_parse_free (info
);
521 warning (_("internal error: string \"%s\" failed to be canonicalized"),
526 if (strcmp (string
, ret
) == 0)
535 /* Convert a mangled name to a demangle_component tree. *MEMORY is
536 set to the block of used memory that should be freed when finished
537 with the tree. DEMANGLED_P is set to the char * that should be
538 freed when finished with the tree, or NULL if none was needed.
539 OPTIONS will be passed to the demangler. */
541 static struct demangle_parse_info
*
542 mangled_name_to_comp (const char *mangled_name
, int options
,
543 void **memory
, char **demangled_p
)
545 char *demangled_name
;
546 struct demangle_parse_info
*info
;
548 /* If it looks like a v3 mangled name, then try to go directly
550 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
552 struct demangle_component
*ret
;
554 ret
= cplus_demangle_v3_components (mangled_name
,
558 info
= cp_new_demangle_parse_info ();
565 /* If it doesn't, or if that failed, then try to demangle the
567 demangled_name
= cplus_demangle (mangled_name
, options
);
568 if (demangled_name
== NULL
)
571 /* If we could demangle the name, parse it to build the component
573 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
577 xfree (demangled_name
);
581 *demangled_p
= demangled_name
;
585 /* Return the name of the class containing method PHYSNAME. */
588 cp_class_name_from_physname (const char *physname
)
590 void *storage
= NULL
;
591 char *demangled_name
= NULL
, *ret
;
592 struct demangle_component
*ret_comp
, *prev_comp
, *cur_comp
;
593 struct demangle_parse_info
*info
;
596 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
597 &storage
, &demangled_name
);
602 ret_comp
= info
->tree
;
604 /* First strip off any qualifiers, if we have a function or
607 switch (ret_comp
->type
)
609 case DEMANGLE_COMPONENT_CONST
:
610 case DEMANGLE_COMPONENT_RESTRICT
:
611 case DEMANGLE_COMPONENT_VOLATILE
:
612 case DEMANGLE_COMPONENT_CONST_THIS
:
613 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
614 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
615 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
616 ret_comp
= d_left (ret_comp
);
623 /* If what we have now is a function, discard the argument list. */
624 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
625 ret_comp
= d_left (ret_comp
);
627 /* If what we have now is a template, strip off the template
628 arguments. The left subtree may be a qualified name. */
629 if (ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
630 ret_comp
= d_left (ret_comp
);
632 /* What we have now should be a name, possibly qualified.
633 Additional qualifiers could live in the left subtree or the right
634 subtree. Find the last piece. */
639 switch (cur_comp
->type
)
641 case DEMANGLE_COMPONENT_QUAL_NAME
:
642 case DEMANGLE_COMPONENT_LOCAL_NAME
:
643 prev_comp
= cur_comp
;
644 cur_comp
= d_right (cur_comp
);
646 case DEMANGLE_COMPONENT_TEMPLATE
:
647 case DEMANGLE_COMPONENT_NAME
:
648 case DEMANGLE_COMPONENT_CTOR
:
649 case DEMANGLE_COMPONENT_DTOR
:
650 case DEMANGLE_COMPONENT_OPERATOR
:
651 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
661 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
663 /* We want to discard the rightmost child of PREV_COMP. */
664 *prev_comp
= *d_left (prev_comp
);
665 /* The ten is completely arbitrary; we don't have a good
667 ret
= cp_comp_to_string (ret_comp
, 10);
671 xfree (demangled_name
);
672 cp_demangled_name_parse_free (info
);
676 /* Return the child of COMP which is the basename of a method,
677 variable, et cetera. All scope qualifiers are discarded, but
678 template arguments will be included. The component tree may be
681 static struct demangle_component
*
682 unqualified_name_from_comp (struct demangle_component
*comp
)
684 struct demangle_component
*ret_comp
= comp
, *last_template
;
688 last_template
= NULL
;
690 switch (ret_comp
->type
)
692 case DEMANGLE_COMPONENT_QUAL_NAME
:
693 case DEMANGLE_COMPONENT_LOCAL_NAME
:
694 ret_comp
= d_right (ret_comp
);
696 case DEMANGLE_COMPONENT_TYPED_NAME
:
697 ret_comp
= d_left (ret_comp
);
699 case DEMANGLE_COMPONENT_TEMPLATE
:
700 gdb_assert (last_template
== NULL
);
701 last_template
= ret_comp
;
702 ret_comp
= d_left (ret_comp
);
704 case DEMANGLE_COMPONENT_CONST
:
705 case DEMANGLE_COMPONENT_RESTRICT
:
706 case DEMANGLE_COMPONENT_VOLATILE
:
707 case DEMANGLE_COMPONENT_CONST_THIS
:
708 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
709 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
710 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
711 ret_comp
= d_left (ret_comp
);
713 case DEMANGLE_COMPONENT_NAME
:
714 case DEMANGLE_COMPONENT_CTOR
:
715 case DEMANGLE_COMPONENT_DTOR
:
716 case DEMANGLE_COMPONENT_OPERATOR
:
717 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
727 d_left (last_template
) = ret_comp
;
728 return last_template
;
734 /* Return the name of the method whose linkage name is PHYSNAME. */
737 method_name_from_physname (const char *physname
)
739 void *storage
= NULL
;
740 char *demangled_name
= NULL
, *ret
;
741 struct demangle_component
*ret_comp
;
742 struct demangle_parse_info
*info
;
744 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
745 &storage
, &demangled_name
);
749 ret_comp
= unqualified_name_from_comp (info
->tree
);
752 if (ret_comp
!= NULL
)
753 /* The ten is completely arbitrary; we don't have a good
755 ret
= cp_comp_to_string (ret_comp
, 10);
758 xfree (demangled_name
);
759 cp_demangled_name_parse_free (info
);
763 /* If FULL_NAME is the demangled name of a C++ function (including an
764 arg list, possibly including namespace/class qualifications),
765 return a new string containing only the function name (without the
766 arg list/class qualifications). Otherwise, return NULL. The
767 caller is responsible for freeing the memory in question. */
770 cp_func_name (const char *full_name
)
773 struct demangle_component
*ret_comp
;
774 struct demangle_parse_info
*info
;
776 info
= cp_demangled_name_to_comp (full_name
, NULL
);
780 ret_comp
= unqualified_name_from_comp (info
->tree
);
783 if (ret_comp
!= NULL
)
784 ret
= cp_comp_to_string (ret_comp
, 10);
786 cp_demangled_name_parse_free (info
);
790 /* DEMANGLED_NAME is the name of a function, including parameters and
791 (optionally) a return type. Return the name of the function without
792 parameters or return type, or NULL if we can not parse the name. */
795 cp_remove_params (const char *demangled_name
)
798 struct demangle_component
*ret_comp
;
799 struct demangle_parse_info
*info
;
802 if (demangled_name
== NULL
)
805 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
809 /* First strip off any qualifiers, if we have a function or method. */
810 ret_comp
= info
->tree
;
812 switch (ret_comp
->type
)
814 case DEMANGLE_COMPONENT_CONST
:
815 case DEMANGLE_COMPONENT_RESTRICT
:
816 case DEMANGLE_COMPONENT_VOLATILE
:
817 case DEMANGLE_COMPONENT_CONST_THIS
:
818 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
819 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
820 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
821 ret_comp
= d_left (ret_comp
);
828 /* What we have now should be a function. Return its name. */
829 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
830 ret
= cp_comp_to_string (d_left (ret_comp
), 10);
832 cp_demangled_name_parse_free (info
);
836 /* Here are some random pieces of trivia to keep in mind while trying
837 to take apart demangled names:
839 - Names can contain function arguments or templates, so the process
840 has to be, to some extent recursive: maybe keep track of your
841 depth based on encountering <> and ().
843 - Parentheses don't just have to happen at the end of a name: they
844 can occur even if the name in question isn't a function, because
845 a template argument might be a type that's a function.
847 - Conversely, even if you're trying to deal with a function, its
848 demangled name might not end with ')': it could be a const or
849 volatile class method, in which case it ends with "const" or
852 - Parentheses are also used in anonymous namespaces: a variable
853 'foo' in an anonymous namespace gets demangled as "(anonymous
856 - And operator names can contain parentheses or angle brackets. */
858 /* FIXME: carlton/2003-03-13: We have several functions here with
859 overlapping functionality; can we combine them? Also, do they
860 handle all the above considerations correctly? */
863 /* This returns the length of first component of NAME, which should be
864 the demangled name of a C++ variable/function/method/etc.
865 Specifically, it returns the index of the first colon forming the
866 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
867 it returns the 1, and given 'foo', it returns 0. */
869 /* The character in NAME indexed by the return value is guaranteed to
870 always be either ':' or '\0'. */
872 /* NOTE: carlton/2003-03-13: This function is currently only intended
873 for internal use: it's probably not entirely safe when called on
874 user-generated input, because some of the 'index += 2' lines in
875 cp_find_first_component_aux might go past the end of malformed
879 cp_find_first_component (const char *name
)
881 return cp_find_first_component_aux (name
, 0);
884 /* Helper function for cp_find_first_component. Like that function,
885 it returns the length of the first component of NAME, but to make
886 the recursion easier, it also stops if it reaches an unexpected ')'
887 or '>' if the value of PERMISSIVE is nonzero. */
889 /* Let's optimize away calls to strlen("operator"). */
891 #define LENGTH_OF_OPERATOR 8
894 cp_find_first_component_aux (const char *name
, int permissive
)
896 unsigned int index
= 0;
897 /* Operator names can show up in unexpected places. Since these can
898 contain parentheses or angle brackets, they can screw up the
899 recursion. But not every string 'operator' is part of an
900 operater name: e.g. you could have a variable 'cooperator'. So
901 this variable tells us whether or not we should treat the string
902 'operator' as starting an operator. */
903 int operator_possible
= 1;
910 /* Template; eat it up. The calls to cp_first_component
911 should only return (I hope!) when they reach the '>'
912 terminating the component or a '::' between two
913 components. (Hence the '+ 2'.) */
915 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
917 index
+= cp_find_first_component_aux (name
+ index
, 1))
919 if (name
[index
] != ':')
921 demangled_name_complaint (name
);
922 return strlen (name
);
926 operator_possible
= 1;
929 /* Similar comment as to '<'. */
931 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
933 index
+= cp_find_first_component_aux (name
+ index
, 1))
935 if (name
[index
] != ':')
937 demangled_name_complaint (name
);
938 return strlen (name
);
942 operator_possible
= 1;
950 demangled_name_complaint (name
);
951 return strlen (name
);
957 /* Operator names can screw up the recursion. */
958 if (operator_possible
959 && strncmp (name
+ index
, "operator",
960 LENGTH_OF_OPERATOR
) == 0)
962 index
+= LENGTH_OF_OPERATOR
;
963 while (ISSPACE(name
[index
]))
967 /* Skip over one less than the appropriate number of
968 characters: the for loop will skip over the last
971 if (name
[index
+ 1] == '<')
978 if (name
[index
+ 1] == '>')
991 operator_possible
= 0;
998 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
999 set of relevant characters are here: it's necessary to
1000 include any character that can show up before 'operator'
1001 in a demangled name, and it's safe to include any
1002 character that can't be part of an identifier's name. */
1003 operator_possible
= 1;
1006 operator_possible
= 0;
1012 /* Complain about a demangled name that we don't know how to parse.
1013 NAME is the demangled name in question. */
1016 demangled_name_complaint (const char *name
)
1018 complaint (&symfile_complaints
,
1019 "unexpected demangled name '%s'", name
);
1022 /* If NAME is the fully-qualified name of a C++
1023 function/variable/method/etc., this returns the length of its
1024 entire prefix: all of the namespaces and classes that make up its
1025 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1026 4, given 'foo', it returns 0. */
1029 cp_entire_prefix_len (const char *name
)
1031 unsigned int current_len
= cp_find_first_component (name
);
1032 unsigned int previous_len
= 0;
1034 while (name
[current_len
] != '\0')
1036 gdb_assert (name
[current_len
] == ':');
1037 previous_len
= current_len
;
1038 /* Skip the '::'. */
1040 current_len
+= cp_find_first_component (name
+ current_len
);
1043 return previous_len
;
1046 /* Overload resolution functions. */
1048 /* Test to see if SYM is a symbol that we haven't seen corresponding
1049 to a function named OLOAD_NAME. If so, add it to the current
1053 overload_list_add_symbol (struct symbol
*sym
,
1054 const char *oload_name
)
1060 /* If there is no type information, we can't do anything, so
1062 if (SYMBOL_TYPE (sym
) == NULL
)
1065 /* skip any symbols that we've already considered. */
1066 for (i
= 0; i
< sym_return_val_index
; ++i
)
1067 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
1068 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
1071 /* Get the demangled name without parameters */
1072 sym_name
= cp_remove_params (SYMBOL_NATURAL_NAME (sym
));
1076 /* skip symbols that cannot match */
1077 if (strcmp (sym_name
, oload_name
) != 0)
1085 /* We have a match for an overload instance, so add SYM to the
1086 current list of overload instances */
1087 if (sym_return_val_index
+ 3 > sym_return_val_size
)
1089 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
1090 sym_return_val
= (struct symbol
**)
1091 xrealloc ((char *) sym_return_val
, newsize
);
1093 sym_return_val
[sym_return_val_index
++] = sym
;
1094 sym_return_val
[sym_return_val_index
] = NULL
;
1097 /* Return a null-terminated list of pointers to function symbols that
1098 are named FUNC_NAME and are visible within NAMESPACE. */
1101 make_symbol_overload_list (const char *func_name
,
1102 const char *namespace)
1104 struct cleanup
*old_cleanups
;
1107 sym_return_val_size
= 100;
1108 sym_return_val_index
= 0;
1109 sym_return_val
= xmalloc ((sym_return_val_size
+ 1) *
1110 sizeof (struct symbol
*));
1111 sym_return_val
[0] = NULL
;
1113 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
1115 make_symbol_overload_list_using (func_name
, namespace);
1117 if (namespace[0] == '\0')
1121 char *concatenated_name
1122 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1123 strcpy (concatenated_name
, namespace);
1124 strcat (concatenated_name
, "::");
1125 strcat (concatenated_name
, func_name
);
1126 name
= concatenated_name
;
1129 make_symbol_overload_list_qualified (name
);
1131 discard_cleanups (old_cleanups
);
1133 return sym_return_val
;
1136 /* Add all symbols with a name matching NAME in BLOCK to the overload
1140 make_symbol_overload_list_block (const char *name
,
1141 const struct block
*block
)
1143 struct block_iterator iter
;
1146 for (sym
= block_iter_name_first (block
, name
, &iter
);
1148 sym
= block_iter_name_next (name
, &iter
))
1149 overload_list_add_symbol (sym
, name
);
1152 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1155 make_symbol_overload_list_namespace (const char *func_name
,
1156 const char *namespace)
1159 const struct block
*block
= NULL
;
1161 if (namespace[0] == '\0')
1165 char *concatenated_name
1166 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1168 strcpy (concatenated_name
, namespace);
1169 strcat (concatenated_name
, "::");
1170 strcat (concatenated_name
, func_name
);
1171 name
= concatenated_name
;
1174 /* Look in the static block. */
1175 block
= block_static_block (get_selected_block (0));
1177 make_symbol_overload_list_block (name
, block
);
1179 /* Look in the global block. */
1180 block
= block_global_block (block
);
1182 make_symbol_overload_list_block (name
, block
);
1186 /* Search the namespace of the given type and namespace of and public
1190 make_symbol_overload_list_adl_namespace (struct type
*type
,
1191 const char *func_name
)
1194 const char *type_name
;
1197 while (TYPE_CODE (type
) == TYPE_CODE_PTR
1198 || TYPE_CODE (type
) == TYPE_CODE_REF
1199 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1200 || TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1202 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1203 type
= check_typedef(type
);
1205 type
= TYPE_TARGET_TYPE (type
);
1208 type_name
= TYPE_NAME (type
);
1210 if (type_name
== NULL
)
1213 prefix_len
= cp_entire_prefix_len (type_name
);
1215 if (prefix_len
!= 0)
1217 namespace = alloca (prefix_len
+ 1);
1218 strncpy (namespace, type_name
, prefix_len
);
1219 namespace[prefix_len
] = '\0';
1221 make_symbol_overload_list_namespace (func_name
, namespace);
1224 /* Check public base type */
1225 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
1226 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
1228 if (BASETYPE_VIA_PUBLIC (type
, i
))
1229 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type
,
1235 /* Adds the overload list overload candidates for FUNC_NAME found
1236 through argument dependent lookup. */
1239 make_symbol_overload_list_adl (struct type
**arg_types
, int nargs
,
1240 const char *func_name
)
1244 gdb_assert (sym_return_val_size
!= -1);
1246 for (i
= 1; i
<= nargs
; i
++)
1247 make_symbol_overload_list_adl_namespace (arg_types
[i
- 1],
1250 return sym_return_val
;
1253 /* Used for cleanups to reset the "searched" flag in case of an
1257 reset_directive_searched (void *data
)
1259 struct using_direct
*direct
= data
;
1260 direct
->searched
= 0;
1263 /* This applies the using directives to add namespaces to search in,
1264 and then searches for overloads in all of those namespaces. It
1265 adds the symbols found to sym_return_val. Arguments are as in
1266 make_symbol_overload_list. */
1269 make_symbol_overload_list_using (const char *func_name
,
1270 const char *namespace)
1272 struct using_direct
*current
;
1273 const struct block
*block
;
1275 /* First, go through the using directives. If any of them apply,
1276 look in the appropriate namespaces for new functions to match
1279 for (block
= get_selected_block (0);
1281 block
= BLOCK_SUPERBLOCK (block
))
1282 for (current
= block_using (block
);
1284 current
= current
->next
)
1286 /* Prevent recursive calls. */
1287 if (current
->searched
)
1290 /* If this is a namespace alias or imported declaration ignore
1292 if (current
->alias
!= NULL
|| current
->declaration
!= NULL
)
1295 if (strcmp (namespace, current
->import_dest
) == 0)
1297 /* Mark this import as searched so that the recursive call
1298 does not search it again. */
1299 struct cleanup
*old_chain
;
1300 current
->searched
= 1;
1301 old_chain
= make_cleanup (reset_directive_searched
,
1304 make_symbol_overload_list_using (func_name
,
1305 current
->import_src
);
1307 current
->searched
= 0;
1308 discard_cleanups (old_chain
);
1312 /* Now, add names for this namespace. */
1313 make_symbol_overload_list_namespace (func_name
, namespace);
1316 /* This does the bulk of the work of finding overloaded symbols.
1317 FUNC_NAME is the name of the overloaded function we're looking for
1318 (possibly including namespace info). */
1321 make_symbol_overload_list_qualified (const char *func_name
)
1324 struct objfile
*objfile
;
1325 const struct block
*b
, *surrounding_static_block
= 0;
1327 /* Look through the partial symtabs for all symbols which begin by
1328 matching FUNC_NAME. Make sure we read that symbol table in. */
1330 ALL_OBJFILES (objfile
)
1333 objfile
->sf
->qf
->expand_symtabs_for_function (objfile
, func_name
);
1336 /* Search upwards from currently selected frame (so that we can
1337 complete on local vars. */
1339 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
1340 make_symbol_overload_list_block (func_name
, b
);
1342 surrounding_static_block
= block_static_block (get_selected_block (0));
1344 /* Go through the symtabs and check the externs and statics for
1345 symbols which match. */
1347 ALL_PRIMARY_SYMTABS (objfile
, s
)
1350 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
1351 make_symbol_overload_list_block (func_name
, b
);
1354 ALL_PRIMARY_SYMTABS (objfile
, s
)
1357 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1358 /* Don't do this block twice. */
1359 if (b
== surrounding_static_block
)
1361 make_symbol_overload_list_block (func_name
, b
);
1365 /* Lookup the rtti type for a class name. */
1368 cp_lookup_rtti_type (const char *name
, struct block
*block
)
1370 struct symbol
* rtti_sym
;
1371 struct type
* rtti_type
;
1373 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
);
1375 if (rtti_sym
== NULL
)
1377 warning (_("RTTI symbol not found for class '%s'"), name
);
1381 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
1383 warning (_("RTTI symbol for class '%s' is not a type"), name
);
1387 rtti_type
= SYMBOL_TYPE (rtti_sym
);
1389 switch (TYPE_CODE (rtti_type
))
1391 case TYPE_CODE_CLASS
:
1393 case TYPE_CODE_NAMESPACE
:
1394 /* chastain/2003-11-26: the symbol tables often contain fake
1395 symbols for namespaces with the same name as the struct.
1396 This warning is an indication of a bug in the lookup order
1397 or a bug in the way that the symbol tables are populated. */
1398 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
1401 warning (_("RTTI symbol for class '%s' has bad type"), name
);
1408 /* Don't allow just "maintenance cplus". */
1411 maint_cplus_command (char *arg
, int from_tty
)
1413 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1414 "by the name of a command.\n"));
1415 help_list (maint_cplus_cmd_list
,
1416 "maintenance cplus ",
1420 /* This is a front end for cp_find_first_component, for unit testing.
1421 Be careful when using it: see the NOTE above
1422 cp_find_first_component. */
1425 first_component_command (char *arg
, int from_tty
)
1433 len
= cp_find_first_component (arg
);
1434 prefix
= alloca (len
+ 1);
1436 memcpy (prefix
, arg
, len
);
1439 printf_unfiltered ("%s\n", prefix
);
1442 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
1445 /* Implement "info vtbl". */
1448 info_vtbl_command (char *arg
, int from_tty
)
1450 struct value
*value
;
1452 value
= parse_and_eval (arg
);
1453 cplus_print_vtable (value
);
1457 _initialize_cp_support (void)
1459 add_prefix_cmd ("cplus", class_maintenance
,
1460 maint_cplus_command
,
1461 _("C++ maintenance commands."),
1462 &maint_cplus_cmd_list
,
1463 "maintenance cplus ",
1464 0, &maintenancelist
);
1465 add_alias_cmd ("cp", "cplus",
1466 class_maintenance
, 1,
1469 add_cmd ("first_component",
1471 first_component_command
,
1472 _("Print the first class/namespace component of NAME."),
1473 &maint_cplus_cmd_list
);
1475 add_info ("vtbl", info_vtbl_command
,
1476 _("Show the virtual function table for a C++ object.\n\
1477 Usage: info vtbl EXPRESSION\n\
1478 Evaluate EXPRESSION and display the virtual function table for the\n\
1479 resulting object."));