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);
531 if (strcmp (string, ret) == 0)
540 /* Convert a mangled name to a demangle_component tree. *MEMORY is
541 set to the block of used memory that should be freed when finished
542 with the tree. DEMANGLED_P is set to the char * that should be
543 freed when finished with the tree, or NULL if none was needed.
544 OPTIONS will be passed to the demangler. */
546 static struct demangle_parse_info *
547 mangled_name_to_comp (const char *mangled_name, int options,
548 void **memory, char **demangled_p)
550 char *demangled_name;
551 struct demangle_parse_info *info;
553 /* If it looks like a v3 mangled name, then try to go directly
555 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
557 struct demangle_component *ret;
559 ret = cplus_demangle_v3_components (mangled_name,
563 info = cp_new_demangle_parse_info ();
570 /* If it doesn't, or if that failed, then try to demangle the
572 demangled_name = cplus_demangle (mangled_name, options);
573 if (demangled_name == NULL)
576 /* If we could demangle the name, parse it to build the component
578 info = cp_demangled_name_to_comp (demangled_name, NULL);
582 xfree (demangled_name);
586 *demangled_p = demangled_name;
590 /* Return the name of the class containing method PHYSNAME. */
593 cp_class_name_from_physname (const char *physname)
595 void *storage = NULL;
596 char *demangled_name = NULL, *ret;
597 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
598 struct demangle_parse_info *info;
601 info = mangled_name_to_comp (physname, DMGL_ANSI,
602 &storage, &demangled_name);
607 ret_comp = info->tree;
609 /* First strip off any qualifiers, if we have a function or
612 switch (ret_comp->type)
614 case DEMANGLE_COMPONENT_CONST:
615 case DEMANGLE_COMPONENT_RESTRICT:
616 case DEMANGLE_COMPONENT_VOLATILE:
617 case DEMANGLE_COMPONENT_CONST_THIS:
618 case DEMANGLE_COMPONENT_RESTRICT_THIS:
619 case DEMANGLE_COMPONENT_VOLATILE_THIS:
620 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
621 ret_comp = d_left (ret_comp);
628 /* If what we have now is a function, discard the argument list. */
629 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
630 ret_comp = d_left (ret_comp);
632 /* If what we have now is a template, strip off the template
633 arguments. The left subtree may be a qualified name. */
634 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
635 ret_comp = d_left (ret_comp);
637 /* What we have now should be a name, possibly qualified.
638 Additional qualifiers could live in the left subtree or the right
639 subtree. Find the last piece. */
644 switch (cur_comp->type)
646 case DEMANGLE_COMPONENT_QUAL_NAME:
647 case DEMANGLE_COMPONENT_LOCAL_NAME:
648 prev_comp = cur_comp;
649 cur_comp = d_right (cur_comp);
651 case DEMANGLE_COMPONENT_TEMPLATE:
652 case DEMANGLE_COMPONENT_NAME:
653 case DEMANGLE_COMPONENT_CTOR:
654 case DEMANGLE_COMPONENT_DTOR:
655 case DEMANGLE_COMPONENT_OPERATOR:
656 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
666 if (cur_comp != NULL && prev_comp != NULL)
668 /* We want to discard the rightmost child of PREV_COMP. */
669 *prev_comp = *d_left (prev_comp);
670 /* The ten is completely arbitrary; we don't have a good
672 ret = cp_comp_to_string (ret_comp, 10);
676 xfree (demangled_name);
677 cp_demangled_name_parse_free (info);
681 /* Return the child of COMP which is the basename of a method,
682 variable, et cetera. All scope qualifiers are discarded, but
683 template arguments will be included. The component tree may be
686 static struct demangle_component *
687 unqualified_name_from_comp (struct demangle_component *comp)
689 struct demangle_component *ret_comp = comp, *last_template;
693 last_template = NULL;
695 switch (ret_comp->type)
697 case DEMANGLE_COMPONENT_QUAL_NAME:
698 case DEMANGLE_COMPONENT_LOCAL_NAME:
699 ret_comp = d_right (ret_comp);
701 case DEMANGLE_COMPONENT_TYPED_NAME:
702 ret_comp = d_left (ret_comp);
704 case DEMANGLE_COMPONENT_TEMPLATE:
705 gdb_assert (last_template == NULL);
706 last_template = ret_comp;
707 ret_comp = d_left (ret_comp);
709 case DEMANGLE_COMPONENT_CONST:
710 case DEMANGLE_COMPONENT_RESTRICT:
711 case DEMANGLE_COMPONENT_VOLATILE:
712 case DEMANGLE_COMPONENT_CONST_THIS:
713 case DEMANGLE_COMPONENT_RESTRICT_THIS:
714 case DEMANGLE_COMPONENT_VOLATILE_THIS:
715 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
716 ret_comp = d_left (ret_comp);
718 case DEMANGLE_COMPONENT_NAME:
719 case DEMANGLE_COMPONENT_CTOR:
720 case DEMANGLE_COMPONENT_DTOR:
721 case DEMANGLE_COMPONENT_OPERATOR:
722 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
732 d_left (last_template) = ret_comp;
733 return last_template;
739 /* Return the name of the method whose linkage name is PHYSNAME. */
742 method_name_from_physname (const char *physname)
744 void *storage = NULL;
745 char *demangled_name = NULL, *ret;
746 struct demangle_component *ret_comp;
747 struct demangle_parse_info *info;
749 info = mangled_name_to_comp (physname, DMGL_ANSI,
750 &storage, &demangled_name);
754 ret_comp = unqualified_name_from_comp (info->tree);
757 if (ret_comp != NULL)
758 /* The ten is completely arbitrary; we don't have a good
760 ret = cp_comp_to_string (ret_comp, 10);
763 xfree (demangled_name);
764 cp_demangled_name_parse_free (info);
768 /* If FULL_NAME is the demangled name of a C++ function (including an
769 arg list, possibly including namespace/class qualifications),
770 return a new string containing only the function name (without the
771 arg list/class qualifications). Otherwise, return NULL. The
772 caller is responsible for freeing the memory in question. */
775 cp_func_name (const char *full_name)
778 struct demangle_component *ret_comp;
779 struct demangle_parse_info *info;
781 info = cp_demangled_name_to_comp (full_name, NULL);
785 ret_comp = unqualified_name_from_comp (info->tree);
788 if (ret_comp != NULL)
789 ret = cp_comp_to_string (ret_comp, 10);
791 cp_demangled_name_parse_free (info);
795 /* DEMANGLED_NAME is the name of a function, including parameters and
796 (optionally) a return type. Return the name of the function without
797 parameters or return type, or NULL if we can not parse the name. */
800 cp_remove_params (const char *demangled_name)
803 struct demangle_component *ret_comp;
804 struct demangle_parse_info *info;
807 if (demangled_name == NULL)
810 info = cp_demangled_name_to_comp (demangled_name, NULL);
814 /* First strip off any qualifiers, if we have a function or method. */
815 ret_comp = info->tree;
817 switch (ret_comp->type)
819 case DEMANGLE_COMPONENT_CONST:
820 case DEMANGLE_COMPONENT_RESTRICT:
821 case DEMANGLE_COMPONENT_VOLATILE:
822 case DEMANGLE_COMPONENT_CONST_THIS:
823 case DEMANGLE_COMPONENT_RESTRICT_THIS:
824 case DEMANGLE_COMPONENT_VOLATILE_THIS:
825 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
826 ret_comp = d_left (ret_comp);
833 /* What we have now should be a function. Return its name. */
834 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
835 ret = cp_comp_to_string (d_left (ret_comp), 10);
837 cp_demangled_name_parse_free (info);
841 /* Here are some random pieces of trivia to keep in mind while trying
842 to take apart demangled names:
844 - Names can contain function arguments or templates, so the process
845 has to be, to some extent recursive: maybe keep track of your
846 depth based on encountering <> and ().
848 - Parentheses don't just have to happen at the end of a name: they
849 can occur even if the name in question isn't a function, because
850 a template argument might be a type that's a function.
852 - Conversely, even if you're trying to deal with a function, its
853 demangled name might not end with ')': it could be a const or
854 volatile class method, in which case it ends with "const" or
857 - Parentheses are also used in anonymous namespaces: a variable
858 'foo' in an anonymous namespace gets demangled as "(anonymous
861 - And operator names can contain parentheses or angle brackets. */
863 /* FIXME: carlton/2003-03-13: We have several functions here with
864 overlapping functionality; can we combine them? Also, do they
865 handle all the above considerations correctly? */
868 /* This returns the length of first component of NAME, which should be
869 the demangled name of a C++ variable/function/method/etc.
870 Specifically, it returns the index of the first colon forming the
871 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
872 it returns the 1, and given 'foo', it returns 0. */
874 /* The character in NAME indexed by the return value is guaranteed to
875 always be either ':' or '\0'. */
877 /* NOTE: carlton/2003-03-13: This function is currently only intended
878 for internal use: it's probably not entirely safe when called on
879 user-generated input, because some of the 'index += 2' lines in
880 cp_find_first_component_aux might go past the end of malformed
884 cp_find_first_component (const char *name)
886 return cp_find_first_component_aux (name, 0);
889 /* Helper function for cp_find_first_component. Like that function,
890 it returns the length of the first component of NAME, but to make
891 the recursion easier, it also stops if it reaches an unexpected ')'
892 or '>' if the value of PERMISSIVE is nonzero. */
894 /* Let's optimize away calls to strlen("operator"). */
896 #define LENGTH_OF_OPERATOR 8
899 cp_find_first_component_aux (const char *name, int permissive)
901 unsigned int index = 0;
902 /* Operator names can show up in unexpected places. Since these can
903 contain parentheses or angle brackets, they can screw up the
904 recursion. But not every string 'operator' is part of an
905 operater name: e.g. you could have a variable 'cooperator'. So
906 this variable tells us whether or not we should treat the string
907 'operator' as starting an operator. */
908 int operator_possible = 1;
915 /* Template; eat it up. The calls to cp_first_component
916 should only return (I hope!) when they reach the '>'
917 terminating the component or a '::' between two
918 components. (Hence the '+ 2'.) */
920 for (index += cp_find_first_component_aux (name + index, 1);
922 index += cp_find_first_component_aux (name + index, 1))
924 if (name[index] != ':')
926 demangled_name_complaint (name);
927 return strlen (name);
931 operator_possible = 1;
934 /* Similar comment as to '<'. */
936 for (index += cp_find_first_component_aux (name + index, 1);
938 index += cp_find_first_component_aux (name + index, 1))
940 if (name[index] != ':')
942 demangled_name_complaint (name);
943 return strlen (name);
947 operator_possible = 1;
955 demangled_name_complaint (name);
956 return strlen (name);
962 /* Operator names can screw up the recursion. */
963 if (operator_possible
964 && strncmp (name + index, "operator",
965 LENGTH_OF_OPERATOR) == 0)
967 index += LENGTH_OF_OPERATOR;
968 while (ISSPACE(name[index]))
972 /* Skip over one less than the appropriate number of
973 characters: the for loop will skip over the last
976 if (name[index + 1] == '<')
983 if (name[index + 1] == '>')
996 operator_possible = 0;
1003 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1004 set of relevant characters are here: it's necessary to
1005 include any character that can show up before 'operator'
1006 in a demangled name, and it's safe to include any
1007 character that can't be part of an identifier's name. */
1008 operator_possible = 1;
1011 operator_possible = 0;
1017 /* Complain about a demangled name that we don't know how to parse.
1018 NAME is the demangled name in question. */
1021 demangled_name_complaint (const char *name)
1023 complaint (&symfile_complaints,
1024 "unexpected demangled name '%s'", name);
1027 /* If NAME is the fully-qualified name of a C++
1028 function/variable/method/etc., this returns the length of its
1029 entire prefix: all of the namespaces and classes that make up its
1030 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1031 4, given 'foo', it returns 0. */
1034 cp_entire_prefix_len (const char *name)
1036 unsigned int current_len = cp_find_first_component (name);
1037 unsigned int previous_len = 0;
1039 while (name[current_len] != '\0')
1041 gdb_assert (name[current_len] == ':');
1042 previous_len = current_len;
1043 /* Skip the '::'. */
1045 current_len += cp_find_first_component (name + current_len);
1048 return previous_len;
1051 /* Overload resolution functions. */
1053 /* Test to see if SYM is a symbol that we haven't seen corresponding
1054 to a function named OLOAD_NAME. If so, add it to the current
1058 overload_list_add_symbol (struct symbol *sym,
1059 const char *oload_name)
1065 /* If there is no type information, we can't do anything, so
1067 if (SYMBOL_TYPE (sym) == NULL)
1070 /* skip any symbols that we've already considered. */
1071 for (i = 0; i < sym_return_val_index; ++i)
1072 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1073 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1076 /* Get the demangled name without parameters */
1077 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1081 /* skip symbols that cannot match */
1082 if (strcmp (sym_name, oload_name) != 0)
1090 /* We have a match for an overload instance, so add SYM to the
1091 current list of overload instances */
1092 if (sym_return_val_index + 3 > sym_return_val_size)
1094 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1095 sym_return_val = (struct symbol **)
1096 xrealloc ((char *) sym_return_val, newsize);
1098 sym_return_val[sym_return_val_index++] = sym;
1099 sym_return_val[sym_return_val_index] = NULL;
1102 /* Return a null-terminated list of pointers to function symbols that
1103 are named FUNC_NAME and are visible within NAMESPACE. */
1106 make_symbol_overload_list (const char *func_name,
1107 const char *namespace)
1109 struct cleanup *old_cleanups;
1112 sym_return_val_size = 100;
1113 sym_return_val_index = 0;
1114 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1115 sizeof (struct symbol *));
1116 sym_return_val[0] = NULL;
1118 old_cleanups = make_cleanup (xfree, sym_return_val);
1120 make_symbol_overload_list_using (func_name, namespace);
1122 if (namespace[0] == '\0')
1126 char *concatenated_name
1127 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1128 strcpy (concatenated_name, namespace);
1129 strcat (concatenated_name, "::");
1130 strcat (concatenated_name, func_name);
1131 name = concatenated_name;
1134 make_symbol_overload_list_qualified (name);
1136 discard_cleanups (old_cleanups);
1138 return sym_return_val;
1141 /* Add all symbols with a name matching NAME in BLOCK to the overload
1145 make_symbol_overload_list_block (const char *name,
1146 const struct block *block)
1148 struct dict_iterator iter;
1151 const struct dictionary *dict = BLOCK_DICT (block);
1153 for (sym = dict_iter_name_first (dict, name, &iter);
1155 sym = dict_iter_name_next (name, &iter))
1156 overload_list_add_symbol (sym, name);
1159 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1162 make_symbol_overload_list_namespace (const char *func_name,
1163 const char *namespace)
1166 const struct block *block = NULL;
1168 if (namespace[0] == '\0')
1172 char *concatenated_name
1173 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1175 strcpy (concatenated_name, namespace);
1176 strcat (concatenated_name, "::");
1177 strcat (concatenated_name, func_name);
1178 name = concatenated_name;
1181 /* Look in the static block. */
1182 block = block_static_block (get_selected_block (0));
1184 make_symbol_overload_list_block (name, block);
1186 /* Look in the global block. */
1187 block = block_global_block (block);
1189 make_symbol_overload_list_block (name, block);
1193 /* Search the namespace of the given type and namespace of and public
1197 make_symbol_overload_list_adl_namespace (struct type *type,
1198 const char *func_name)
1204 while (TYPE_CODE (type) == TYPE_CODE_PTR
1205 || TYPE_CODE (type) == TYPE_CODE_REF
1206 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1207 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1209 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1210 type = check_typedef(type);
1212 type = TYPE_TARGET_TYPE (type);
1215 type_name = TYPE_NAME (type);
1217 if (type_name == NULL)
1220 prefix_len = cp_entire_prefix_len (type_name);
1222 if (prefix_len != 0)
1224 namespace = alloca (prefix_len + 1);
1225 strncpy (namespace, type_name, prefix_len);
1226 namespace[prefix_len] = '\0';
1228 make_symbol_overload_list_namespace (func_name, namespace);
1231 /* Check public base type */
1232 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1233 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1235 if (BASETYPE_VIA_PUBLIC (type, i))
1236 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1242 /* Adds the overload list overload candidates for FUNC_NAME found
1243 through argument dependent lookup. */
1246 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1247 const char *func_name)
1251 gdb_assert (sym_return_val_size != -1);
1253 for (i = 1; i <= nargs; i++)
1254 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1257 return sym_return_val;
1260 /* Used for cleanups to reset the "searched" flag in case of an
1264 reset_directive_searched (void *data)
1266 struct using_direct *direct = data;
1267 direct->searched = 0;
1270 /* This applies the using directives to add namespaces to search in,
1271 and then searches for overloads in all of those namespaces. It
1272 adds the symbols found to sym_return_val. Arguments are as in
1273 make_symbol_overload_list. */
1276 make_symbol_overload_list_using (const char *func_name,
1277 const char *namespace)
1279 struct using_direct *current;
1280 const struct block *block;
1282 /* First, go through the using directives. If any of them apply,
1283 look in the appropriate namespaces for new functions to match
1286 for (block = get_selected_block (0);
1288 block = BLOCK_SUPERBLOCK (block))
1289 for (current = block_using (block);
1291 current = current->next)
1293 /* Prevent recursive calls. */
1294 if (current->searched)
1297 /* If this is a namespace alias or imported declaration ignore
1299 if (current->alias != NULL || current->declaration != NULL)
1302 if (strcmp (namespace, current->import_dest) == 0)
1304 /* Mark this import as searched so that the recursive call
1305 does not search it again. */
1306 struct cleanup *old_chain;
1307 current->searched = 1;
1308 old_chain = make_cleanup (reset_directive_searched,
1311 make_symbol_overload_list_using (func_name,
1312 current->import_src);
1314 current->searched = 0;
1315 discard_cleanups (old_chain);
1319 /* Now, add names for this namespace. */
1320 make_symbol_overload_list_namespace (func_name, namespace);
1323 /* This does the bulk of the work of finding overloaded symbols.
1324 FUNC_NAME is the name of the overloaded function we're looking for
1325 (possibly including namespace info). */
1328 make_symbol_overload_list_qualified (const char *func_name)
1332 struct objfile *objfile;
1333 const struct block *b, *surrounding_static_block = 0;
1334 struct dict_iterator iter;
1335 const struct dictionary *dict;
1337 /* Look through the partial symtabs for all symbols which begin by
1338 matching FUNC_NAME. Make sure we read that symbol table in. */
1340 ALL_OBJFILES (objfile)
1343 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1346 /* Search upwards from currently selected frame (so that we can
1347 complete on local vars. */
1349 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1350 make_symbol_overload_list_block (func_name, b);
1352 surrounding_static_block = block_static_block (get_selected_block (0));
1354 /* Go through the symtabs and check the externs and statics for
1355 symbols which match. */
1357 ALL_PRIMARY_SYMTABS (objfile, s)
1360 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1361 make_symbol_overload_list_block (func_name, b);
1364 ALL_PRIMARY_SYMTABS (objfile, s)
1367 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1368 /* Don't do this block twice. */
1369 if (b == surrounding_static_block)
1371 make_symbol_overload_list_block (func_name, b);
1375 /* Lookup the rtti type for a class name. */
1378 cp_lookup_rtti_type (const char *name, struct block *block)
1380 struct symbol * rtti_sym;
1381 struct type * rtti_type;
1383 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1385 if (rtti_sym == NULL)
1387 warning (_("RTTI symbol not found for class '%s'"), name);
1391 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1393 warning (_("RTTI symbol for class '%s' is not a type"), name);
1397 rtti_type = SYMBOL_TYPE (rtti_sym);
1399 switch (TYPE_CODE (rtti_type))
1401 case TYPE_CODE_CLASS:
1403 case TYPE_CODE_NAMESPACE:
1404 /* chastain/2003-11-26: the symbol tables often contain fake
1405 symbols for namespaces with the same name as the struct.
1406 This warning is an indication of a bug in the lookup order
1407 or a bug in the way that the symbol tables are populated. */
1408 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1411 warning (_("RTTI symbol for class '%s' has bad type"), name);
1418 /* Don't allow just "maintenance cplus". */
1421 maint_cplus_command (char *arg, int from_tty)
1423 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1424 "by the name of a command.\n"));
1425 help_list (maint_cplus_cmd_list,
1426 "maintenance cplus ",
1430 /* This is a front end for cp_find_first_component, for unit testing.
1431 Be careful when using it: see the NOTE above
1432 cp_find_first_component. */
1435 first_component_command (char *arg, int from_tty)
1443 len = cp_find_first_component (arg);
1444 prefix = alloca (len + 1);
1446 memcpy (prefix, arg, len);
1449 printf_unfiltered ("%s\n", prefix);
1452 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1454 #define SKIP_SPACE(P) \
1457 while (*(P) == ' ' || *(P) == '\t') \
1462 /* Returns the length of the operator name or 0 if INPUT does not
1463 point to a valid C++ operator. INPUT should start with
1466 cp_validate_operator (const char *input)
1471 struct expression *expr;
1473 volatile struct gdb_exception except;
1477 if (strncmp (p, "operator", 8) == 0)
1484 i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
1487 int length = strlen (operator_tokens[i]);
1489 /* By using strncmp here, we MUST have operator_tokens
1490 ordered! See additional notes where operator_tokens is
1492 if (strncmp (p, operator_tokens[i], length) == 0)
1499 if (strncmp (op, "new", 3) == 0
1500 || strncmp (op, "delete", 6) == 0)
1503 /* Special case: new[] and delete[]. We must be
1504 careful to swallow whitespace before/in "[]". */
1523 /* Check input for a conversion operator. */
1525 /* Skip past base typename. */
1526 while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1530 /* Add modifiers '*' / '&'. */
1531 while (*p == '*' || *p == '&')
1537 /* Check for valid type. [Remember: input starts with
1539 copy = savestring (input + 8, p - input - 8);
1542 TRY_CATCH (except, RETURN_MASK_ALL)
1544 expr = parse_expression (copy);
1545 val = evaluate_type (expr);
1552 if (val != NULL && value_type (val) != NULL)
1560 _initialize_cp_support (void)
1562 add_prefix_cmd ("cplus", class_maintenance,
1563 maint_cplus_command,
1564 _("C++ maintenance commands."),
1565 &maint_cplus_cmd_list,
1566 "maintenance cplus ",
1567 0, &maintenancelist);
1568 add_alias_cmd ("cp", "cplus",
1569 class_maintenance, 1,
1572 add_cmd ("first_component",
1574 first_component_command,
1575 _("Print the first class/namespace component of NAME."),
1576 &maint_cplus_cmd_list);