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"
38 #include "safe-ctype.h"
42 #define d_left(dc) (dc)->u.s_binary.left
43 #define d_right(dc) (dc)->u.s_binary.right
45 /* Functions related to demangled name parsing. */
47 static unsigned int cp_find_first_component_aux (const char *name
,
50 static void demangled_name_complaint (const char *name
);
52 /* Functions/variables related to overload resolution. */
54 static int sym_return_val_size
= -1;
55 static int sym_return_val_index
;
56 static struct symbol
**sym_return_val
;
58 static void overload_list_add_symbol (struct symbol
*sym
,
59 const char *oload_name
);
61 static void make_symbol_overload_list_using (const char *func_name
,
62 const char *namespace);
64 static void make_symbol_overload_list_qualified (const char *func_name
);
66 /* The list of "maint cplus" commands. */
68 struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
70 /* The actual commands. */
72 static void maint_cplus_command (char *arg
, int from_tty
);
73 static void first_component_command (char *arg
, int from_tty
);
75 /* Operator validation.
76 NOTE: Multi-byte operators (usually the assignment variety
77 operator) must appear before the single byte version, i.e., "+="
79 static const char *operator_tokens
[] =
81 "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*",
82 "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<",
83 ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=",
84 "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete"
85 /* new[] and delete[] require special whitespace handling */
88 /* A list of typedefs which should not be substituted by replace_typedefs. */
89 static const char * const ignore_typedefs
[] =
91 "std::istream", "std::iostream", "std::ostream", "std::string"
95 replace_typedefs (struct demangle_parse_info
*info
,
96 struct demangle_component
*ret_comp
);
98 /* A convenience function to copy STRING into OBSTACK, returning a pointer
99 to the newly allocated string and saving the number of bytes saved in LEN.
101 It does not copy the terminating '\0' byte! */
104 copy_string_to_obstack (struct obstack
*obstack
, const char *string
,
107 *len
= strlen (string
);
108 return obstack_copy (obstack
, string
, *len
);
111 /* A cleanup wrapper for cp_demangled_name_parse_free. */
114 do_demangled_name_parse_free_cleanup (void *data
)
116 struct demangle_parse_info
*info
= (struct demangle_parse_info
*) data
;
118 cp_demangled_name_parse_free (info
);
121 /* Create a cleanup for C++ name parsing. */
124 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info
*info
)
126 return make_cleanup (do_demangled_name_parse_free_cleanup
, info
);
129 /* Return 1 if STRING is clearly already in canonical form. This
130 function is conservative; things which it does not recognize are
131 assumed to be non-canonical, and the parser will sort them out
132 afterwards. This speeds up the critical path for alphanumeric
136 cp_already_canonical (const char *string
)
138 /* Identifier start character [a-zA-Z_]. */
139 if (!ISIDST (string
[0]))
142 /* These are the only two identifiers which canonicalize to other
143 than themselves or an error: unsigned -> unsigned int and
145 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
147 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
150 /* Identifier character [a-zA-Z0-9_]. */
151 while (ISIDNUM (string
[1]))
154 if (string
[1] == '\0')
160 /* Inspect the given RET_COMP for its type. If it is a typedef,
161 replace the node with the typedef's tree.
163 Returns 1 if any typedef substitutions were made, 0 otherwise. */
166 inspect_type (struct demangle_parse_info
*info
,
167 struct demangle_component
*ret_comp
)
172 volatile struct gdb_exception except
;
174 /* Copy the symbol's name from RET_COMP and look it up
175 in the symbol table. */
176 name
= (char *) alloca (ret_comp
->u
.s_name
.len
+ 1);
177 memcpy (name
, ret_comp
->u
.s_name
.s
, ret_comp
->u
.s_name
.len
);
178 name
[ret_comp
->u
.s_name
.len
] = '\0';
180 /* Ignore any typedefs that should not be substituted. */
181 for (i
= 0; i
< ARRAY_SIZE (ignore_typedefs
); ++i
)
183 if (strcmp (name
, ignore_typedefs
[i
]) == 0)
188 TRY_CATCH (except
, RETURN_MASK_ALL
)
190 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0);
193 if (except
.reason
>= 0 && sym
!= NULL
)
195 struct type
*otype
= SYMBOL_TYPE (sym
);
197 /* If the type is a typedef, replace it. */
198 if (TYPE_CODE (otype
) == TYPE_CODE_TYPEDEF
)
203 struct demangle_parse_info
*i
;
206 /* Get the real type of the typedef. */
207 type
= check_typedef (otype
);
209 is_anon
= (TYPE_TAG_NAME (type
) == NULL
210 && (TYPE_CODE (type
) == TYPE_CODE_ENUM
211 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
212 || TYPE_CODE (type
) == TYPE_CODE_UNION
));
215 struct type
*last
= otype
;
217 /* Find the last typedef for the type. */
218 while (TYPE_TARGET_TYPE (last
) != NULL
219 && (TYPE_CODE (TYPE_TARGET_TYPE (last
))
220 == TYPE_CODE_TYPEDEF
))
221 last
= TYPE_TARGET_TYPE (last
);
223 /* If there is only one typedef for this anonymous type,
224 do not substitute it. */
228 /* Use the last typedef seen as the type for this
233 buf
= mem_fileopen ();
234 TRY_CATCH (except
, RETURN_MASK_ERROR
)
236 type_print (type
, "", buf
, -1);
239 /* If type_print threw an exception, there is little point
240 in continuing, so just bow out gracefully. */
241 if (except
.reason
< 0)
243 ui_file_delete (buf
);
247 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
248 ui_file_delete (buf
);
250 /* Turn the result into a new tree. Note that this
251 tree will contain pointers into NAME, so NAME cannot
252 be free'd until all typedef conversion is done and
253 the final result is converted into a string. */
254 i
= cp_demangled_name_to_comp (name
, NULL
);
257 /* Merge the two trees. */
258 cp_merge_demangle_parse_infos (info
, ret_comp
, i
);
260 /* Replace any newly introduced typedefs -- but not
261 if the type is anonymous (that would lead to infinite
264 replace_typedefs (info
, ret_comp
);
268 /* This shouldn't happen unless the type printer has
269 output something that the name parser cannot grok.
270 Nonetheless, an ounce of prevention...
272 Canonicalize the name again, and store it in the
273 current node (RET_COMP). */
274 char *canon
= cp_canonicalize_string_no_typedefs (name
);
278 /* Copy the canonicalization into the obstack and
280 name
= copy_string_to_obstack (&info
->obstack
, canon
, &len
);
284 ret_comp
->u
.s_name
.s
= name
;
285 ret_comp
->u
.s_name
.len
= len
;
295 /* Replace any typedefs appearing in the qualified name
296 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
300 replace_typedefs_qualified_name (struct demangle_parse_info
*info
,
301 struct demangle_component
*ret_comp
)
305 struct ui_file
*buf
= mem_fileopen ();
306 struct demangle_component
*comp
= ret_comp
;
308 /* Walk each node of the qualified name, reconstructing the name of
309 this element. With every node, check for any typedef substitutions.
310 If a substitution has occurred, replace the qualified name node
311 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
313 while (comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
315 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_NAME
)
317 struct demangle_component
new;
319 ui_file_write (buf
, d_left (comp
)->u
.s_name
.s
,
320 d_left (comp
)->u
.s_name
.len
);
321 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
322 new.type
= DEMANGLE_COMPONENT_NAME
;
323 new.u
.s_name
.s
= name
;
324 new.u
.s_name
.len
= len
;
325 if (inspect_type (info
, &new))
330 /* A typedef was substituted in NEW. Convert it to a
331 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
334 ui_file_rewind (buf
);
335 n
= cp_comp_to_string (&new, 100);
338 /* If something went astray, abort typedef substitutions. */
339 ui_file_delete (buf
);
343 s
= copy_string_to_obstack (&info
->obstack
, n
, &slen
);
346 d_left (ret_comp
)->type
= DEMANGLE_COMPONENT_NAME
;
347 d_left (ret_comp
)->u
.s_name
.s
= s
;
348 d_left (ret_comp
)->u
.s_name
.len
= slen
;
349 d_right (ret_comp
) = d_right (comp
);
356 /* The current node is not a name, so simply replace any
357 typedefs in it. Then print it to the stream to continue
358 checking for more typedefs in the tree. */
359 replace_typedefs (info
, d_left (comp
));
360 name
= cp_comp_to_string (d_left (comp
), 100);
363 /* If something went astray, abort typedef substitutions. */
364 ui_file_delete (buf
);
367 fputs_unfiltered (name
, buf
);
370 ui_file_write (buf
, "::", 2);
371 comp
= d_right (comp
);
374 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
375 name assembled above and append the name given by COMP. Then use this
376 reassembled name to check for a typedef. */
378 if (comp
->type
== DEMANGLE_COMPONENT_NAME
)
380 ui_file_write (buf
, comp
->u
.s_name
.s
, comp
->u
.s_name
.len
);
381 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
383 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
384 with a DEMANGLE_COMPONENT_NAME node containing the whole
386 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
387 ret_comp
->u
.s_name
.s
= name
;
388 ret_comp
->u
.s_name
.len
= len
;
389 inspect_type (info
, ret_comp
);
392 replace_typedefs (info
, comp
);
394 ui_file_delete (buf
);
398 /* A function to check const and volatile qualifiers for argument types.
400 "Parameter declarations that differ only in the presence
401 or absence of `const' and/or `volatile' are equivalent."
402 C++ Standard N3290, clause 13.1.3 #4. */
405 check_cv_qualifiers (struct demangle_component
*ret_comp
)
407 while (d_left (ret_comp
) != NULL
408 && (d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_CONST
409 || d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_VOLATILE
))
411 d_left (ret_comp
) = d_left (d_left (ret_comp
));
415 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
416 their basic types. */
419 replace_typedefs (struct demangle_parse_info
*info
,
420 struct demangle_component
*ret_comp
)
424 switch (ret_comp
->type
)
426 case DEMANGLE_COMPONENT_ARGLIST
:
427 check_cv_qualifiers (ret_comp
);
430 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
431 case DEMANGLE_COMPONENT_TEMPLATE
:
432 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
433 case DEMANGLE_COMPONENT_TYPED_NAME
:
434 replace_typedefs (info
, d_left (ret_comp
));
435 replace_typedefs (info
, d_right (ret_comp
));
438 case DEMANGLE_COMPONENT_NAME
:
439 inspect_type (info
, ret_comp
);
442 case DEMANGLE_COMPONENT_QUAL_NAME
:
443 replace_typedefs_qualified_name (info
, ret_comp
);
446 case DEMANGLE_COMPONENT_LOCAL_NAME
:
447 case DEMANGLE_COMPONENT_CTOR
:
448 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
449 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
450 replace_typedefs (info
, d_right (ret_comp
));
453 case DEMANGLE_COMPONENT_CONST
:
454 case DEMANGLE_COMPONENT_RESTRICT
:
455 case DEMANGLE_COMPONENT_VOLATILE
:
456 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
457 case DEMANGLE_COMPONENT_CONST_THIS
:
458 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
459 case DEMANGLE_COMPONENT_POINTER
:
460 case DEMANGLE_COMPONENT_REFERENCE
:
461 replace_typedefs (info
, d_left (ret_comp
));
470 /* Parse STRING and convert it to canonical form, resolving any typedefs.
471 If parsing fails, or if STRING is already canonical, return NULL.
472 Otherwise return the canonical form. The return value is allocated via
476 cp_canonicalize_string_no_typedefs (const char *string
)
479 unsigned int estimated_len
;
480 struct demangle_parse_info
*info
;
483 estimated_len
= strlen (string
) * 2;
484 info
= cp_demangled_name_to_comp (string
, NULL
);
487 /* Replace all the typedefs in the tree. */
488 replace_typedefs (info
, info
->tree
);
490 /* Convert the tree back into a string. */
491 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
492 gdb_assert (ret
!= NULL
);
494 /* Free the parse information. */
495 cp_demangled_name_parse_free (info
);
497 /* Finally, compare the original string with the computed
498 name, returning NULL if they are the same. */
499 if (strcmp (string
, ret
) == 0)
509 /* Parse STRING and convert it to canonical form. If parsing fails,
510 or if STRING is already canonical, return NULL. Otherwise return
511 the canonical form. The return value is allocated via xmalloc. */
514 cp_canonicalize_string (const char *string
)
516 struct demangle_parse_info
*info
;
517 unsigned int estimated_len
;
520 if (cp_already_canonical (string
))
523 info
= cp_demangled_name_to_comp (string
, NULL
);
527 estimated_len
= strlen (string
) * 2;
528 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
529 cp_demangled_name_parse_free (info
);
533 warning (_("internal error: string \"%s\" failed to be canonicalized"),
538 if (strcmp (string
, ret
) == 0)
547 /* Convert a mangled name to a demangle_component tree. *MEMORY is
548 set to the block of used memory that should be freed when finished
549 with the tree. DEMANGLED_P is set to the char * that should be
550 freed when finished with the tree, or NULL if none was needed.
551 OPTIONS will be passed to the demangler. */
553 static struct demangle_parse_info
*
554 mangled_name_to_comp (const char *mangled_name
, int options
,
555 void **memory
, char **demangled_p
)
557 char *demangled_name
;
558 struct demangle_parse_info
*info
;
560 /* If it looks like a v3 mangled name, then try to go directly
562 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
564 struct demangle_component
*ret
;
566 ret
= cplus_demangle_v3_components (mangled_name
,
570 info
= cp_new_demangle_parse_info ();
577 /* If it doesn't, or if that failed, then try to demangle the
579 demangled_name
= cplus_demangle (mangled_name
, options
);
580 if (demangled_name
== NULL
)
583 /* If we could demangle the name, parse it to build the component
585 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
589 xfree (demangled_name
);
593 *demangled_p
= demangled_name
;
597 /* Return the name of the class containing method PHYSNAME. */
600 cp_class_name_from_physname (const char *physname
)
602 void *storage
= NULL
;
603 char *demangled_name
= NULL
, *ret
;
604 struct demangle_component
*ret_comp
, *prev_comp
, *cur_comp
;
605 struct demangle_parse_info
*info
;
608 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
609 &storage
, &demangled_name
);
614 ret_comp
= info
->tree
;
616 /* First strip off any qualifiers, if we have a function or
619 switch (ret_comp
->type
)
621 case DEMANGLE_COMPONENT_CONST
:
622 case DEMANGLE_COMPONENT_RESTRICT
:
623 case DEMANGLE_COMPONENT_VOLATILE
:
624 case DEMANGLE_COMPONENT_CONST_THIS
:
625 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
626 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
627 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
628 ret_comp
= d_left (ret_comp
);
635 /* If what we have now is a function, discard the argument list. */
636 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
637 ret_comp
= d_left (ret_comp
);
639 /* If what we have now is a template, strip off the template
640 arguments. The left subtree may be a qualified name. */
641 if (ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
642 ret_comp
= d_left (ret_comp
);
644 /* What we have now should be a name, possibly qualified.
645 Additional qualifiers could live in the left subtree or the right
646 subtree. Find the last piece. */
651 switch (cur_comp
->type
)
653 case DEMANGLE_COMPONENT_QUAL_NAME
:
654 case DEMANGLE_COMPONENT_LOCAL_NAME
:
655 prev_comp
= cur_comp
;
656 cur_comp
= d_right (cur_comp
);
658 case DEMANGLE_COMPONENT_TEMPLATE
:
659 case DEMANGLE_COMPONENT_NAME
:
660 case DEMANGLE_COMPONENT_CTOR
:
661 case DEMANGLE_COMPONENT_DTOR
:
662 case DEMANGLE_COMPONENT_OPERATOR
:
663 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
673 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
675 /* We want to discard the rightmost child of PREV_COMP. */
676 *prev_comp
= *d_left (prev_comp
);
677 /* The ten is completely arbitrary; we don't have a good
679 ret
= cp_comp_to_string (ret_comp
, 10);
683 xfree (demangled_name
);
684 cp_demangled_name_parse_free (info
);
688 /* Return the child of COMP which is the basename of a method,
689 variable, et cetera. All scope qualifiers are discarded, but
690 template arguments will be included. The component tree may be
693 static struct demangle_component
*
694 unqualified_name_from_comp (struct demangle_component
*comp
)
696 struct demangle_component
*ret_comp
= comp
, *last_template
;
700 last_template
= NULL
;
702 switch (ret_comp
->type
)
704 case DEMANGLE_COMPONENT_QUAL_NAME
:
705 case DEMANGLE_COMPONENT_LOCAL_NAME
:
706 ret_comp
= d_right (ret_comp
);
708 case DEMANGLE_COMPONENT_TYPED_NAME
:
709 ret_comp
= d_left (ret_comp
);
711 case DEMANGLE_COMPONENT_TEMPLATE
:
712 gdb_assert (last_template
== NULL
);
713 last_template
= ret_comp
;
714 ret_comp
= d_left (ret_comp
);
716 case DEMANGLE_COMPONENT_CONST
:
717 case DEMANGLE_COMPONENT_RESTRICT
:
718 case DEMANGLE_COMPONENT_VOLATILE
:
719 case DEMANGLE_COMPONENT_CONST_THIS
:
720 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
721 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
722 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
723 ret_comp
= d_left (ret_comp
);
725 case DEMANGLE_COMPONENT_NAME
:
726 case DEMANGLE_COMPONENT_CTOR
:
727 case DEMANGLE_COMPONENT_DTOR
:
728 case DEMANGLE_COMPONENT_OPERATOR
:
729 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
739 d_left (last_template
) = ret_comp
;
740 return last_template
;
746 /* Return the name of the method whose linkage name is PHYSNAME. */
749 method_name_from_physname (const char *physname
)
751 void *storage
= NULL
;
752 char *demangled_name
= NULL
, *ret
;
753 struct demangle_component
*ret_comp
;
754 struct demangle_parse_info
*info
;
756 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
757 &storage
, &demangled_name
);
761 ret_comp
= unqualified_name_from_comp (info
->tree
);
764 if (ret_comp
!= NULL
)
765 /* The ten is completely arbitrary; we don't have a good
767 ret
= cp_comp_to_string (ret_comp
, 10);
770 xfree (demangled_name
);
771 cp_demangled_name_parse_free (info
);
775 /* If FULL_NAME is the demangled name of a C++ function (including an
776 arg list, possibly including namespace/class qualifications),
777 return a new string containing only the function name (without the
778 arg list/class qualifications). Otherwise, return NULL. The
779 caller is responsible for freeing the memory in question. */
782 cp_func_name (const char *full_name
)
785 struct demangle_component
*ret_comp
;
786 struct demangle_parse_info
*info
;
788 info
= cp_demangled_name_to_comp (full_name
, NULL
);
792 ret_comp
= unqualified_name_from_comp (info
->tree
);
795 if (ret_comp
!= NULL
)
796 ret
= cp_comp_to_string (ret_comp
, 10);
798 cp_demangled_name_parse_free (info
);
802 /* DEMANGLED_NAME is the name of a function, including parameters and
803 (optionally) a return type. Return the name of the function without
804 parameters or return type, or NULL if we can not parse the name. */
807 cp_remove_params (const char *demangled_name
)
810 struct demangle_component
*ret_comp
;
811 struct demangle_parse_info
*info
;
814 if (demangled_name
== NULL
)
817 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
821 /* First strip off any qualifiers, if we have a function or method. */
822 ret_comp
= info
->tree
;
824 switch (ret_comp
->type
)
826 case DEMANGLE_COMPONENT_CONST
:
827 case DEMANGLE_COMPONENT_RESTRICT
:
828 case DEMANGLE_COMPONENT_VOLATILE
:
829 case DEMANGLE_COMPONENT_CONST_THIS
:
830 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
831 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
832 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
833 ret_comp
= d_left (ret_comp
);
840 /* What we have now should be a function. Return its name. */
841 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
842 ret
= cp_comp_to_string (d_left (ret_comp
), 10);
844 cp_demangled_name_parse_free (info
);
848 /* Here are some random pieces of trivia to keep in mind while trying
849 to take apart demangled names:
851 - Names can contain function arguments or templates, so the process
852 has to be, to some extent recursive: maybe keep track of your
853 depth based on encountering <> and ().
855 - Parentheses don't just have to happen at the end of a name: they
856 can occur even if the name in question isn't a function, because
857 a template argument might be a type that's a function.
859 - Conversely, even if you're trying to deal with a function, its
860 demangled name might not end with ')': it could be a const or
861 volatile class method, in which case it ends with "const" or
864 - Parentheses are also used in anonymous namespaces: a variable
865 'foo' in an anonymous namespace gets demangled as "(anonymous
868 - And operator names can contain parentheses or angle brackets. */
870 /* FIXME: carlton/2003-03-13: We have several functions here with
871 overlapping functionality; can we combine them? Also, do they
872 handle all the above considerations correctly? */
875 /* This returns the length of first component of NAME, which should be
876 the demangled name of a C++ variable/function/method/etc.
877 Specifically, it returns the index of the first colon forming the
878 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
879 it returns the 1, and given 'foo', it returns 0. */
881 /* The character in NAME indexed by the return value is guaranteed to
882 always be either ':' or '\0'. */
884 /* NOTE: carlton/2003-03-13: This function is currently only intended
885 for internal use: it's probably not entirely safe when called on
886 user-generated input, because some of the 'index += 2' lines in
887 cp_find_first_component_aux might go past the end of malformed
891 cp_find_first_component (const char *name
)
893 return cp_find_first_component_aux (name
, 0);
896 /* Helper function for cp_find_first_component. Like that function,
897 it returns the length of the first component of NAME, but to make
898 the recursion easier, it also stops if it reaches an unexpected ')'
899 or '>' if the value of PERMISSIVE is nonzero. */
901 /* Let's optimize away calls to strlen("operator"). */
903 #define LENGTH_OF_OPERATOR 8
906 cp_find_first_component_aux (const char *name
, int permissive
)
908 unsigned int index
= 0;
909 /* Operator names can show up in unexpected places. Since these can
910 contain parentheses or angle brackets, they can screw up the
911 recursion. But not every string 'operator' is part of an
912 operater name: e.g. you could have a variable 'cooperator'. So
913 this variable tells us whether or not we should treat the string
914 'operator' as starting an operator. */
915 int operator_possible
= 1;
922 /* Template; eat it up. The calls to cp_first_component
923 should only return (I hope!) when they reach the '>'
924 terminating the component or a '::' between two
925 components. (Hence the '+ 2'.) */
927 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
929 index
+= cp_find_first_component_aux (name
+ index
, 1))
931 if (name
[index
] != ':')
933 demangled_name_complaint (name
);
934 return strlen (name
);
938 operator_possible
= 1;
941 /* Similar comment as to '<'. */
943 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
945 index
+= cp_find_first_component_aux (name
+ index
, 1))
947 if (name
[index
] != ':')
949 demangled_name_complaint (name
);
950 return strlen (name
);
954 operator_possible
= 1;
962 demangled_name_complaint (name
);
963 return strlen (name
);
969 /* Operator names can screw up the recursion. */
970 if (operator_possible
971 && strncmp (name
+ index
, "operator",
972 LENGTH_OF_OPERATOR
) == 0)
974 index
+= LENGTH_OF_OPERATOR
;
975 while (ISSPACE(name
[index
]))
979 /* Skip over one less than the appropriate number of
980 characters: the for loop will skip over the last
983 if (name
[index
+ 1] == '<')
990 if (name
[index
+ 1] == '>')
1003 operator_possible
= 0;
1010 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1011 set of relevant characters are here: it's necessary to
1012 include any character that can show up before 'operator'
1013 in a demangled name, and it's safe to include any
1014 character that can't be part of an identifier's name. */
1015 operator_possible
= 1;
1018 operator_possible
= 0;
1024 /* Complain about a demangled name that we don't know how to parse.
1025 NAME is the demangled name in question. */
1028 demangled_name_complaint (const char *name
)
1030 complaint (&symfile_complaints
,
1031 "unexpected demangled name '%s'", name
);
1034 /* If NAME is the fully-qualified name of a C++
1035 function/variable/method/etc., this returns the length of its
1036 entire prefix: all of the namespaces and classes that make up its
1037 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1038 4, given 'foo', it returns 0. */
1041 cp_entire_prefix_len (const char *name
)
1043 unsigned int current_len
= cp_find_first_component (name
);
1044 unsigned int previous_len
= 0;
1046 while (name
[current_len
] != '\0')
1048 gdb_assert (name
[current_len
] == ':');
1049 previous_len
= current_len
;
1050 /* Skip the '::'. */
1052 current_len
+= cp_find_first_component (name
+ current_len
);
1055 return previous_len
;
1058 /* Overload resolution functions. */
1060 /* Test to see if SYM is a symbol that we haven't seen corresponding
1061 to a function named OLOAD_NAME. If so, add it to the current
1065 overload_list_add_symbol (struct symbol
*sym
,
1066 const char *oload_name
)
1072 /* If there is no type information, we can't do anything, so
1074 if (SYMBOL_TYPE (sym
) == NULL
)
1077 /* skip any symbols that we've already considered. */
1078 for (i
= 0; i
< sym_return_val_index
; ++i
)
1079 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
1080 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
1083 /* Get the demangled name without parameters */
1084 sym_name
= cp_remove_params (SYMBOL_NATURAL_NAME (sym
));
1088 /* skip symbols that cannot match */
1089 if (strcmp (sym_name
, oload_name
) != 0)
1097 /* We have a match for an overload instance, so add SYM to the
1098 current list of overload instances */
1099 if (sym_return_val_index
+ 3 > sym_return_val_size
)
1101 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
1102 sym_return_val
= (struct symbol
**)
1103 xrealloc ((char *) sym_return_val
, newsize
);
1105 sym_return_val
[sym_return_val_index
++] = sym
;
1106 sym_return_val
[sym_return_val_index
] = NULL
;
1109 /* Return a null-terminated list of pointers to function symbols that
1110 are named FUNC_NAME and are visible within NAMESPACE. */
1113 make_symbol_overload_list (const char *func_name
,
1114 const char *namespace)
1116 struct cleanup
*old_cleanups
;
1119 sym_return_val_size
= 100;
1120 sym_return_val_index
= 0;
1121 sym_return_val
= xmalloc ((sym_return_val_size
+ 1) *
1122 sizeof (struct symbol
*));
1123 sym_return_val
[0] = NULL
;
1125 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
1127 make_symbol_overload_list_using (func_name
, namespace);
1129 if (namespace[0] == '\0')
1133 char *concatenated_name
1134 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1135 strcpy (concatenated_name
, namespace);
1136 strcat (concatenated_name
, "::");
1137 strcat (concatenated_name
, func_name
);
1138 name
= concatenated_name
;
1141 make_symbol_overload_list_qualified (name
);
1143 discard_cleanups (old_cleanups
);
1145 return sym_return_val
;
1148 /* Add all symbols with a name matching NAME in BLOCK to the overload
1152 make_symbol_overload_list_block (const char *name
,
1153 const struct block
*block
)
1155 struct dict_iterator iter
;
1158 const struct dictionary
*dict
= BLOCK_DICT (block
);
1160 for (sym
= dict_iter_name_first (dict
, name
, &iter
);
1162 sym
= dict_iter_name_next (name
, &iter
))
1163 overload_list_add_symbol (sym
, name
);
1166 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1169 make_symbol_overload_list_namespace (const char *func_name
,
1170 const char *namespace)
1173 const struct block
*block
= NULL
;
1175 if (namespace[0] == '\0')
1179 char *concatenated_name
1180 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1182 strcpy (concatenated_name
, namespace);
1183 strcat (concatenated_name
, "::");
1184 strcat (concatenated_name
, func_name
);
1185 name
= concatenated_name
;
1188 /* Look in the static block. */
1189 block
= block_static_block (get_selected_block (0));
1191 make_symbol_overload_list_block (name
, block
);
1193 /* Look in the global block. */
1194 block
= block_global_block (block
);
1196 make_symbol_overload_list_block (name
, block
);
1200 /* Search the namespace of the given type and namespace of and public
1204 make_symbol_overload_list_adl_namespace (struct type
*type
,
1205 const char *func_name
)
1208 const char *type_name
;
1211 while (TYPE_CODE (type
) == TYPE_CODE_PTR
1212 || TYPE_CODE (type
) == TYPE_CODE_REF
1213 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1214 || TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1216 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1217 type
= check_typedef(type
);
1219 type
= TYPE_TARGET_TYPE (type
);
1222 type_name
= TYPE_NAME (type
);
1224 if (type_name
== NULL
)
1227 prefix_len
= cp_entire_prefix_len (type_name
);
1229 if (prefix_len
!= 0)
1231 namespace = alloca (prefix_len
+ 1);
1232 strncpy (namespace, type_name
, prefix_len
);
1233 namespace[prefix_len
] = '\0';
1235 make_symbol_overload_list_namespace (func_name
, namespace);
1238 /* Check public base type */
1239 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
1240 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
1242 if (BASETYPE_VIA_PUBLIC (type
, i
))
1243 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type
,
1249 /* Adds the overload list overload candidates for FUNC_NAME found
1250 through argument dependent lookup. */
1253 make_symbol_overload_list_adl (struct type
**arg_types
, int nargs
,
1254 const char *func_name
)
1258 gdb_assert (sym_return_val_size
!= -1);
1260 for (i
= 1; i
<= nargs
; i
++)
1261 make_symbol_overload_list_adl_namespace (arg_types
[i
- 1],
1264 return sym_return_val
;
1267 /* Used for cleanups to reset the "searched" flag in case of an
1271 reset_directive_searched (void *data
)
1273 struct using_direct
*direct
= data
;
1274 direct
->searched
= 0;
1277 /* This applies the using directives to add namespaces to search in,
1278 and then searches for overloads in all of those namespaces. It
1279 adds the symbols found to sym_return_val. Arguments are as in
1280 make_symbol_overload_list. */
1283 make_symbol_overload_list_using (const char *func_name
,
1284 const char *namespace)
1286 struct using_direct
*current
;
1287 const struct block
*block
;
1289 /* First, go through the using directives. If any of them apply,
1290 look in the appropriate namespaces for new functions to match
1293 for (block
= get_selected_block (0);
1295 block
= BLOCK_SUPERBLOCK (block
))
1296 for (current
= block_using (block
);
1298 current
= current
->next
)
1300 /* Prevent recursive calls. */
1301 if (current
->searched
)
1304 /* If this is a namespace alias or imported declaration ignore
1306 if (current
->alias
!= NULL
|| current
->declaration
!= NULL
)
1309 if (strcmp (namespace, current
->import_dest
) == 0)
1311 /* Mark this import as searched so that the recursive call
1312 does not search it again. */
1313 struct cleanup
*old_chain
;
1314 current
->searched
= 1;
1315 old_chain
= make_cleanup (reset_directive_searched
,
1318 make_symbol_overload_list_using (func_name
,
1319 current
->import_src
);
1321 current
->searched
= 0;
1322 discard_cleanups (old_chain
);
1326 /* Now, add names for this namespace. */
1327 make_symbol_overload_list_namespace (func_name
, namespace);
1330 /* This does the bulk of the work of finding overloaded symbols.
1331 FUNC_NAME is the name of the overloaded function we're looking for
1332 (possibly including namespace info). */
1335 make_symbol_overload_list_qualified (const char *func_name
)
1339 struct objfile
*objfile
;
1340 const struct block
*b
, *surrounding_static_block
= 0;
1341 struct dict_iterator iter
;
1342 const struct dictionary
*dict
;
1344 /* Look through the partial symtabs for all symbols which begin by
1345 matching FUNC_NAME. Make sure we read that symbol table in. */
1347 ALL_OBJFILES (objfile
)
1350 objfile
->sf
->qf
->expand_symtabs_for_function (objfile
, func_name
);
1353 /* Search upwards from currently selected frame (so that we can
1354 complete on local vars. */
1356 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
1357 make_symbol_overload_list_block (func_name
, b
);
1359 surrounding_static_block
= block_static_block (get_selected_block (0));
1361 /* Go through the symtabs and check the externs and statics for
1362 symbols which match. */
1364 ALL_PRIMARY_SYMTABS (objfile
, s
)
1367 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
1368 make_symbol_overload_list_block (func_name
, b
);
1371 ALL_PRIMARY_SYMTABS (objfile
, s
)
1374 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1375 /* Don't do this block twice. */
1376 if (b
== surrounding_static_block
)
1378 make_symbol_overload_list_block (func_name
, b
);
1382 /* Lookup the rtti type for a class name. */
1385 cp_lookup_rtti_type (const char *name
, struct block
*block
)
1387 struct symbol
* rtti_sym
;
1388 struct type
* rtti_type
;
1390 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
);
1392 if (rtti_sym
== NULL
)
1394 warning (_("RTTI symbol not found for class '%s'"), name
);
1398 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
1400 warning (_("RTTI symbol for class '%s' is not a type"), name
);
1404 rtti_type
= SYMBOL_TYPE (rtti_sym
);
1406 switch (TYPE_CODE (rtti_type
))
1408 case TYPE_CODE_CLASS
:
1410 case TYPE_CODE_NAMESPACE
:
1411 /* chastain/2003-11-26: the symbol tables often contain fake
1412 symbols for namespaces with the same name as the struct.
1413 This warning is an indication of a bug in the lookup order
1414 or a bug in the way that the symbol tables are populated. */
1415 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
1418 warning (_("RTTI symbol for class '%s' has bad type"), name
);
1425 /* Don't allow just "maintenance cplus". */
1428 maint_cplus_command (char *arg
, int from_tty
)
1430 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1431 "by the name of a command.\n"));
1432 help_list (maint_cplus_cmd_list
,
1433 "maintenance cplus ",
1437 /* This is a front end for cp_find_first_component, for unit testing.
1438 Be careful when using it: see the NOTE above
1439 cp_find_first_component. */
1442 first_component_command (char *arg
, int from_tty
)
1450 len
= cp_find_first_component (arg
);
1451 prefix
= alloca (len
+ 1);
1453 memcpy (prefix
, arg
, len
);
1456 printf_unfiltered ("%s\n", prefix
);
1459 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
1461 #define SKIP_SPACE(P) \
1464 while (*(P) == ' ' || *(P) == '\t') \
1469 /* Returns the length of the operator name or 0 if INPUT does not
1470 point to a valid C++ operator. INPUT should start with
1473 cp_validate_operator (const char *input
)
1478 struct expression
*expr
;
1480 volatile struct gdb_exception except
;
1484 if (strncmp (p
, "operator", 8) == 0)
1491 i
< sizeof (operator_tokens
) / sizeof (operator_tokens
[0]);
1494 int length
= strlen (operator_tokens
[i
]);
1496 /* By using strncmp here, we MUST have operator_tokens
1497 ordered! See additional notes where operator_tokens is
1499 if (strncmp (p
, operator_tokens
[i
], length
) == 0)
1506 if (strncmp (op
, "new", 3) == 0
1507 || strncmp (op
, "delete", 6) == 0)
1510 /* Special case: new[] and delete[]. We must be
1511 careful to swallow whitespace before/in "[]". */
1530 /* Check input for a conversion operator. */
1532 /* Skip past base typename. */
1533 while (*p
!= '*' && *p
!= '&' && *p
!= 0 && *p
!= ' ')
1537 /* Add modifiers '*' / '&'. */
1538 while (*p
== '*' || *p
== '&')
1544 /* Check for valid type. [Remember: input starts with
1546 copy
= savestring (input
+ 8, p
- input
- 8);
1549 TRY_CATCH (except
, RETURN_MASK_ALL
)
1551 expr
= parse_expression (copy
);
1552 val
= evaluate_type (expr
);
1559 if (val
!= NULL
&& value_type (val
) != NULL
)
1567 _initialize_cp_support (void)
1569 add_prefix_cmd ("cplus", class_maintenance
,
1570 maint_cplus_command
,
1571 _("C++ maintenance commands."),
1572 &maint_cplus_cmd_list
,
1573 "maintenance cplus ",
1574 0, &maintenancelist
);
1575 add_alias_cmd ("cp", "cplus",
1576 class_maintenance
, 1,
1579 add_cmd ("first_component",
1581 first_component_command
,
1582 _("Print the first class/namespace component of NAME."),
1583 &maint_cplus_cmd_list
);