1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "cp-support.h"
24 #include "gdb_string.h"
26 #include "gdb_assert.h"
28 #include "dictionary.h"
33 #include "complaints.h"
35 #include "exceptions.h"
36 #include "expression.h"
39 #include "safe-ctype.h"
43 #define d_left(dc) (dc)->u.s_binary.left
44 #define d_right(dc) (dc)->u.s_binary.right
46 /* Functions related to demangled name parsing. */
48 static unsigned int cp_find_first_component_aux (const char *name,
51 static void demangled_name_complaint (const char *name);
53 /* Functions/variables related to overload resolution. */
55 static int sym_return_val_size = -1;
56 static int sym_return_val_index;
57 static struct symbol **sym_return_val;
59 static void overload_list_add_symbol (struct symbol *sym,
60 const char *oload_name);
62 static void make_symbol_overload_list_using (const char *func_name,
63 const char *namespace);
65 static void make_symbol_overload_list_qualified (const char *func_name);
67 /* The list of "maint cplus" commands. */
69 struct cmd_list_element *maint_cplus_cmd_list = NULL;
71 /* The actual commands. */
73 static void maint_cplus_command (char *arg, int from_tty);
74 static void first_component_command (char *arg, int from_tty);
76 /* Operator validation.
77 NOTE: Multi-byte operators (usually the assignment variety
78 operator) must appear before the single byte version, i.e., "+="
80 static const char *operator_tokens[] =
82 "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*",
83 "/=", "/", "%=", "%", "!=", "==", "!", "&&", "<<=", "<<",
84 ">>=", ">>", "<=", "<", ">=", ">", "~", "&=", "&", "|=",
85 "||", "|", "^=", "^", "=", "()", "[]", ",", "new", "delete"
86 /* new[] and delete[] require special whitespace handling */
89 /* A list of typedefs which should not be substituted by replace_typedefs. */
90 static const char * const ignore_typedefs[] =
92 "std::istream", "std::iostream", "std::ostream", "std::string"
96 replace_typedefs (struct demangle_parse_info *info,
97 struct demangle_component *ret_comp);
99 /* A convenience function to copy STRING into OBSTACK, returning a pointer
100 to the newly allocated string and saving the number of bytes saved in LEN.
102 It does not copy the terminating '\0' byte! */
105 copy_string_to_obstack (struct obstack *obstack, const char *string,
108 *len = strlen (string);
109 return obstack_copy (obstack, string, *len);
112 /* A cleanup wrapper for cp_demangled_name_parse_free. */
115 do_demangled_name_parse_free_cleanup (void *data)
117 struct demangle_parse_info *info = (struct demangle_parse_info *) data;
119 cp_demangled_name_parse_free (info);
122 /* Create a cleanup for C++ name parsing. */
125 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info)
127 return make_cleanup (do_demangled_name_parse_free_cleanup, info);
130 /* Return 1 if STRING is clearly already in canonical form. This
131 function is conservative; things which it does not recognize are
132 assumed to be non-canonical, and the parser will sort them out
133 afterwards. This speeds up the critical path for alphanumeric
137 cp_already_canonical (const char *string)
139 /* Identifier start character [a-zA-Z_]. */
140 if (!ISIDST (string[0]))
143 /* These are the only two identifiers which canonicalize to other
144 than themselves or an error: unsigned -> unsigned int and
146 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
148 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
151 /* Identifier character [a-zA-Z0-9_]. */
152 while (ISIDNUM (string[1]))
155 if (string[1] == '\0')
161 /* Inspect the given RET_COMP for its type. If it is a typedef,
162 replace the node with the typedef's tree.
164 Returns 1 if any typedef substitutions were made, 0 otherwise. */
167 inspect_type (struct demangle_parse_info *info,
168 struct demangle_component *ret_comp)
173 volatile struct gdb_exception except;
175 /* Copy the symbol's name from RET_COMP and look it up
176 in the symbol table. */
177 name = (char *) alloca (ret_comp->u.s_name.len + 1);
178 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
179 name[ret_comp->u.s_name.len] = '\0';
181 /* Ignore any typedefs that should not be substituted. */
182 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
184 if (strcmp (name, ignore_typedefs[i]) == 0)
189 TRY_CATCH (except, RETURN_MASK_ALL)
191 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
194 if (except.reason >= 0 && sym != NULL)
196 struct type *otype = SYMBOL_TYPE (sym);
198 /* If the type is a typedef, replace it. */
199 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF)
204 struct demangle_parse_info *i;
207 /* Get the real type of the typedef. */
208 type = check_typedef (otype);
210 is_anon = (TYPE_TAG_NAME (type) == NULL
211 && (TYPE_CODE (type) == TYPE_CODE_ENUM
212 || TYPE_CODE (type) == TYPE_CODE_STRUCT
213 || TYPE_CODE (type) == TYPE_CODE_UNION));
216 struct type *last = otype;
218 /* Find the last typedef for the type. */
219 while (TYPE_TARGET_TYPE (last) != NULL
220 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
221 == TYPE_CODE_TYPEDEF))
222 last = TYPE_TARGET_TYPE (last);
224 /* If there is only one typedef for this anonymous type,
225 do not substitute it. */
229 /* Use the last typedef seen as the type for this
234 buf = mem_fileopen ();
235 TRY_CATCH (except, RETURN_MASK_ERROR)
237 type_print (type, "", buf, -1);
240 /* If type_print threw an exception, there is little point
241 in continuing, so just bow out gracefully. */
242 if (except.reason < 0)
244 ui_file_delete (buf);
248 name = ui_file_obsavestring (buf, &info->obstack, &len);
249 ui_file_delete (buf);
251 /* Turn the result into a new tree. Note that this
252 tree will contain pointers into NAME, so NAME cannot
253 be free'd until all typedef conversion is done and
254 the final result is converted into a string. */
255 i = cp_demangled_name_to_comp (name, NULL);
258 /* Merge the two trees. */
259 cp_merge_demangle_parse_infos (info, ret_comp, i);
261 /* Replace any newly introduced typedefs -- but not
262 if the type is anonymous (that would lead to infinite
265 replace_typedefs (info, ret_comp);
269 /* This shouldn't happen unless the type printer has
270 output something that the name parser cannot grok.
271 Nonetheless, an ounce of prevention...
273 Canonicalize the name again, and store it in the
274 current node (RET_COMP). */
275 char *canon = cp_canonicalize_string_no_typedefs (name);
279 /* Copy the canonicalization into the obstack and
281 name = copy_string_to_obstack (&info->obstack, canon, &len);
285 ret_comp->u.s_name.s = name;
286 ret_comp->u.s_name.len = len;
296 /* Replace any typedefs appearing in the qualified name
297 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
301 replace_typedefs_qualified_name (struct demangle_parse_info *info,
302 struct demangle_component *ret_comp)
306 struct ui_file *buf = mem_fileopen ();
307 struct demangle_component *comp = ret_comp;
309 /* Walk each node of the qualified name, reconstructing the name of
310 this element. With every node, check for any typedef substitutions.
311 If a substitution has occurred, replace the qualified name node
312 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
314 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
316 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
318 struct demangle_component new;
320 ui_file_write (buf, d_left (comp)->u.s_name.s,
321 d_left (comp)->u.s_name.len);
322 name = ui_file_obsavestring (buf, &info->obstack, &len);
323 new.type = DEMANGLE_COMPONENT_NAME;
324 new.u.s_name.s = name;
325 new.u.s_name.len = len;
326 if (inspect_type (info, &new))
331 /* A typedef was substituted in NEW. Convert it to a
332 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
335 ui_file_rewind (buf);
336 n = cp_comp_to_string (&new, 100);
339 /* If something went astray, abort typedef substitutions. */
340 ui_file_delete (buf);
344 s = copy_string_to_obstack (&info->obstack, n, &slen);
347 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
348 d_left (ret_comp)->u.s_name.s = s;
349 d_left (ret_comp)->u.s_name.len = slen;
350 d_right (ret_comp) = d_right (comp);
357 /* The current node is not a name, so simply replace any
358 typedefs in it. Then print it to the stream to continue
359 checking for more typedefs in the tree. */
360 replace_typedefs (info, d_left (comp));
361 name = cp_comp_to_string (d_left (comp), 100);
364 /* If something went astray, abort typedef substitutions. */
365 ui_file_delete (buf);
368 fputs_unfiltered (name, buf);
371 ui_file_write (buf, "::", 2);
372 comp = d_right (comp);
375 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
376 name assembled above and append the name given by COMP. Then use this
377 reassembled name to check for a typedef. */
379 if (comp->type == DEMANGLE_COMPONENT_NAME)
381 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len);
382 name = ui_file_obsavestring (buf, &info->obstack, &len);
384 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
385 with a DEMANGLE_COMPONENT_NAME node containing the whole
387 ret_comp->type = DEMANGLE_COMPONENT_NAME;
388 ret_comp->u.s_name.s = name;
389 ret_comp->u.s_name.len = len;
390 inspect_type (info, ret_comp);
393 replace_typedefs (info, comp);
395 ui_file_delete (buf);
399 /* A function to check const and volatile qualifiers for argument types.
401 "Parameter declarations that differ only in the presence
402 or absence of `const' and/or `volatile' are equivalent."
403 C++ Standard N3290, clause 13.1.3 #4. */
406 check_cv_qualifiers (struct demangle_component *ret_comp)
408 while (d_left (ret_comp) != NULL
409 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
410 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
412 d_left (ret_comp) = d_left (d_left (ret_comp));
416 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
417 their basic types. */
420 replace_typedefs (struct demangle_parse_info *info,
421 struct demangle_component *ret_comp)
425 switch (ret_comp->type)
427 case DEMANGLE_COMPONENT_ARGLIST:
428 check_cv_qualifiers (ret_comp);
431 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
432 case DEMANGLE_COMPONENT_TEMPLATE:
433 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
434 case DEMANGLE_COMPONENT_TYPED_NAME:
435 replace_typedefs (info, d_left (ret_comp));
436 replace_typedefs (info, d_right (ret_comp));
439 case DEMANGLE_COMPONENT_NAME:
440 inspect_type (info, ret_comp);
443 case DEMANGLE_COMPONENT_QUAL_NAME:
444 replace_typedefs_qualified_name (info, ret_comp);
447 case DEMANGLE_COMPONENT_LOCAL_NAME:
448 case DEMANGLE_COMPONENT_CTOR:
449 case DEMANGLE_COMPONENT_ARRAY_TYPE:
450 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
451 replace_typedefs (info, d_right (ret_comp));
454 case DEMANGLE_COMPONENT_CONST:
455 case DEMANGLE_COMPONENT_RESTRICT:
456 case DEMANGLE_COMPONENT_VOLATILE:
457 case DEMANGLE_COMPONENT_VOLATILE_THIS:
458 case DEMANGLE_COMPONENT_CONST_THIS:
459 case DEMANGLE_COMPONENT_RESTRICT_THIS:
460 case DEMANGLE_COMPONENT_POINTER:
461 case DEMANGLE_COMPONENT_REFERENCE:
462 replace_typedefs (info, d_left (ret_comp));
471 /* Parse STRING and convert it to canonical form, resolving any typedefs.
472 If parsing fails, or if STRING is already canonical, return NULL.
473 Otherwise return the canonical form. The return value is allocated via
477 cp_canonicalize_string_no_typedefs (const char *string)
480 unsigned int estimated_len;
481 struct demangle_parse_info *info;
484 estimated_len = strlen (string) * 2;
485 info = cp_demangled_name_to_comp (string, NULL);
488 /* Replace all the typedefs in the tree. */
489 replace_typedefs (info, info->tree);
491 /* Convert the tree back into a string. */
492 ret = cp_comp_to_string (info->tree, estimated_len);
493 gdb_assert (ret != NULL);
495 /* Free the parse information. */
496 cp_demangled_name_parse_free (info);
498 /* Finally, compare the original string with the computed
499 name, returning NULL if they are the same. */
500 if (strcmp (string, ret) == 0)
510 /* Parse STRING and convert it to canonical form. If parsing fails,
511 or if STRING is already canonical, return NULL. Otherwise return
512 the canonical form. The return value is allocated via xmalloc. */
515 cp_canonicalize_string (const char *string)
517 struct demangle_parse_info *info;
518 unsigned int estimated_len;
521 if (cp_already_canonical (string))
524 info = cp_demangled_name_to_comp (string, NULL);
528 estimated_len = strlen (string) * 2;
529 ret = cp_comp_to_string (info->tree, estimated_len);
530 cp_demangled_name_parse_free (info);
532 if (strcmp (string, ret) == 0)
541 /* Convert a mangled name to a demangle_component tree. *MEMORY is
542 set to the block of used memory that should be freed when finished
543 with the tree. DEMANGLED_P is set to the char * that should be
544 freed when finished with the tree, or NULL if none was needed.
545 OPTIONS will be passed to the demangler. */
547 static struct demangle_parse_info *
548 mangled_name_to_comp (const char *mangled_name, int options,
549 void **memory, char **demangled_p)
551 char *demangled_name;
552 struct demangle_parse_info *info;
554 /* If it looks like a v3 mangled name, then try to go directly
556 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
558 struct demangle_component *ret;
560 ret = cplus_demangle_v3_components (mangled_name,
564 info = cp_new_demangle_parse_info ();
571 /* If it doesn't, or if that failed, then try to demangle the
573 demangled_name = cplus_demangle (mangled_name, options);
574 if (demangled_name == NULL)
577 /* If we could demangle the name, parse it to build the component
579 info = cp_demangled_name_to_comp (demangled_name, NULL);
583 xfree (demangled_name);
587 *demangled_p = demangled_name;
591 /* Return the name of the class containing method PHYSNAME. */
594 cp_class_name_from_physname (const char *physname)
596 void *storage = NULL;
597 char *demangled_name = NULL, *ret;
598 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
599 struct demangle_parse_info *info;
602 info = mangled_name_to_comp (physname, DMGL_ANSI,
603 &storage, &demangled_name);
608 ret_comp = info->tree;
610 /* First strip off any qualifiers, if we have a function or
613 switch (ret_comp->type)
615 case DEMANGLE_COMPONENT_CONST:
616 case DEMANGLE_COMPONENT_RESTRICT:
617 case DEMANGLE_COMPONENT_VOLATILE:
618 case DEMANGLE_COMPONENT_CONST_THIS:
619 case DEMANGLE_COMPONENT_RESTRICT_THIS:
620 case DEMANGLE_COMPONENT_VOLATILE_THIS:
621 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
622 ret_comp = d_left (ret_comp);
629 /* If what we have now is a function, discard the argument list. */
630 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
631 ret_comp = d_left (ret_comp);
633 /* If what we have now is a template, strip off the template
634 arguments. The left subtree may be a qualified name. */
635 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
636 ret_comp = d_left (ret_comp);
638 /* What we have now should be a name, possibly qualified.
639 Additional qualifiers could live in the left subtree or the right
640 subtree. Find the last piece. */
645 switch (cur_comp->type)
647 case DEMANGLE_COMPONENT_QUAL_NAME:
648 case DEMANGLE_COMPONENT_LOCAL_NAME:
649 prev_comp = cur_comp;
650 cur_comp = d_right (cur_comp);
652 case DEMANGLE_COMPONENT_TEMPLATE:
653 case DEMANGLE_COMPONENT_NAME:
654 case DEMANGLE_COMPONENT_CTOR:
655 case DEMANGLE_COMPONENT_DTOR:
656 case DEMANGLE_COMPONENT_OPERATOR:
657 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
667 if (cur_comp != NULL && prev_comp != NULL)
669 /* We want to discard the rightmost child of PREV_COMP. */
670 *prev_comp = *d_left (prev_comp);
671 /* The ten is completely arbitrary; we don't have a good
673 ret = cp_comp_to_string (ret_comp, 10);
677 xfree (demangled_name);
678 cp_demangled_name_parse_free (info);
682 /* Return the child of COMP which is the basename of a method,
683 variable, et cetera. All scope qualifiers are discarded, but
684 template arguments will be included. The component tree may be
687 static struct demangle_component *
688 unqualified_name_from_comp (struct demangle_component *comp)
690 struct demangle_component *ret_comp = comp, *last_template;
694 last_template = NULL;
696 switch (ret_comp->type)
698 case DEMANGLE_COMPONENT_QUAL_NAME:
699 case DEMANGLE_COMPONENT_LOCAL_NAME:
700 ret_comp = d_right (ret_comp);
702 case DEMANGLE_COMPONENT_TYPED_NAME:
703 ret_comp = d_left (ret_comp);
705 case DEMANGLE_COMPONENT_TEMPLATE:
706 gdb_assert (last_template == NULL);
707 last_template = ret_comp;
708 ret_comp = d_left (ret_comp);
710 case DEMANGLE_COMPONENT_CONST:
711 case DEMANGLE_COMPONENT_RESTRICT:
712 case DEMANGLE_COMPONENT_VOLATILE:
713 case DEMANGLE_COMPONENT_CONST_THIS:
714 case DEMANGLE_COMPONENT_RESTRICT_THIS:
715 case DEMANGLE_COMPONENT_VOLATILE_THIS:
716 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
717 ret_comp = d_left (ret_comp);
719 case DEMANGLE_COMPONENT_NAME:
720 case DEMANGLE_COMPONENT_CTOR:
721 case DEMANGLE_COMPONENT_DTOR:
722 case DEMANGLE_COMPONENT_OPERATOR:
723 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
733 d_left (last_template) = ret_comp;
734 return last_template;
740 /* Return the name of the method whose linkage name is PHYSNAME. */
743 method_name_from_physname (const char *physname)
745 void *storage = NULL;
746 char *demangled_name = NULL, *ret;
747 struct demangle_component *ret_comp;
748 struct demangle_parse_info *info;
750 info = mangled_name_to_comp (physname, DMGL_ANSI,
751 &storage, &demangled_name);
755 ret_comp = unqualified_name_from_comp (info->tree);
758 if (ret_comp != NULL)
759 /* The ten is completely arbitrary; we don't have a good
761 ret = cp_comp_to_string (ret_comp, 10);
764 xfree (demangled_name);
765 cp_demangled_name_parse_free (info);
769 /* If FULL_NAME is the demangled name of a C++ function (including an
770 arg list, possibly including namespace/class qualifications),
771 return a new string containing only the function name (without the
772 arg list/class qualifications). Otherwise, return NULL. The
773 caller is responsible for freeing the memory in question. */
776 cp_func_name (const char *full_name)
779 struct demangle_component *ret_comp;
780 struct demangle_parse_info *info;
782 info = cp_demangled_name_to_comp (full_name, NULL);
786 ret_comp = unqualified_name_from_comp (info->tree);
789 if (ret_comp != NULL)
790 ret = cp_comp_to_string (ret_comp, 10);
792 cp_demangled_name_parse_free (info);
796 /* DEMANGLED_NAME is the name of a function, including parameters and
797 (optionally) a return type. Return the name of the function without
798 parameters or return type, or NULL if we can not parse the name. */
801 cp_remove_params (const char *demangled_name)
804 struct demangle_component *ret_comp;
805 struct demangle_parse_info *info;
808 if (demangled_name == NULL)
811 info = cp_demangled_name_to_comp (demangled_name, NULL);
815 /* First strip off any qualifiers, if we have a function or method. */
816 ret_comp = info->tree;
818 switch (ret_comp->type)
820 case DEMANGLE_COMPONENT_CONST:
821 case DEMANGLE_COMPONENT_RESTRICT:
822 case DEMANGLE_COMPONENT_VOLATILE:
823 case DEMANGLE_COMPONENT_CONST_THIS:
824 case DEMANGLE_COMPONENT_RESTRICT_THIS:
825 case DEMANGLE_COMPONENT_VOLATILE_THIS:
826 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
827 ret_comp = d_left (ret_comp);
834 /* What we have now should be a function. Return its name. */
835 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
836 ret = cp_comp_to_string (d_left (ret_comp), 10);
838 cp_demangled_name_parse_free (info);
842 /* Here are some random pieces of trivia to keep in mind while trying
843 to take apart demangled names:
845 - Names can contain function arguments or templates, so the process
846 has to be, to some extent recursive: maybe keep track of your
847 depth based on encountering <> and ().
849 - Parentheses don't just have to happen at the end of a name: they
850 can occur even if the name in question isn't a function, because
851 a template argument might be a type that's a function.
853 - Conversely, even if you're trying to deal with a function, its
854 demangled name might not end with ')': it could be a const or
855 volatile class method, in which case it ends with "const" or
858 - Parentheses are also used in anonymous namespaces: a variable
859 'foo' in an anonymous namespace gets demangled as "(anonymous
862 - And operator names can contain parentheses or angle brackets. */
864 /* FIXME: carlton/2003-03-13: We have several functions here with
865 overlapping functionality; can we combine them? Also, do they
866 handle all the above considerations correctly? */
869 /* This returns the length of first component of NAME, which should be
870 the demangled name of a C++ variable/function/method/etc.
871 Specifically, it returns the index of the first colon forming the
872 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
873 it returns the 1, and given 'foo', it returns 0. */
875 /* The character in NAME indexed by the return value is guaranteed to
876 always be either ':' or '\0'. */
878 /* NOTE: carlton/2003-03-13: This function is currently only intended
879 for internal use: it's probably not entirely safe when called on
880 user-generated input, because some of the 'index += 2' lines in
881 cp_find_first_component_aux might go past the end of malformed
885 cp_find_first_component (const char *name)
887 return cp_find_first_component_aux (name, 0);
890 /* Helper function for cp_find_first_component. Like that function,
891 it returns the length of the first component of NAME, but to make
892 the recursion easier, it also stops if it reaches an unexpected ')'
893 or '>' if the value of PERMISSIVE is nonzero. */
895 /* Let's optimize away calls to strlen("operator"). */
897 #define LENGTH_OF_OPERATOR 8
900 cp_find_first_component_aux (const char *name, int permissive)
902 unsigned int index = 0;
903 /* Operator names can show up in unexpected places. Since these can
904 contain parentheses or angle brackets, they can screw up the
905 recursion. But not every string 'operator' is part of an
906 operater name: e.g. you could have a variable 'cooperator'. So
907 this variable tells us whether or not we should treat the string
908 'operator' as starting an operator. */
909 int operator_possible = 1;
916 /* Template; eat it up. The calls to cp_first_component
917 should only return (I hope!) when they reach the '>'
918 terminating the component or a '::' between two
919 components. (Hence the '+ 2'.) */
921 for (index += cp_find_first_component_aux (name + index, 1);
923 index += cp_find_first_component_aux (name + index, 1))
925 if (name[index] != ':')
927 demangled_name_complaint (name);
928 return strlen (name);
932 operator_possible = 1;
935 /* Similar comment as to '<'. */
937 for (index += cp_find_first_component_aux (name + index, 1);
939 index += cp_find_first_component_aux (name + index, 1))
941 if (name[index] != ':')
943 demangled_name_complaint (name);
944 return strlen (name);
948 operator_possible = 1;
956 demangled_name_complaint (name);
957 return strlen (name);
963 /* Operator names can screw up the recursion. */
964 if (operator_possible
965 && strncmp (name + index, "operator",
966 LENGTH_OF_OPERATOR) == 0)
968 index += LENGTH_OF_OPERATOR;
969 while (ISSPACE(name[index]))
973 /* Skip over one less than the appropriate number of
974 characters: the for loop will skip over the last
977 if (name[index + 1] == '<')
984 if (name[index + 1] == '>')
997 operator_possible = 0;
1004 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1005 set of relevant characters are here: it's necessary to
1006 include any character that can show up before 'operator'
1007 in a demangled name, and it's safe to include any
1008 character that can't be part of an identifier's name. */
1009 operator_possible = 1;
1012 operator_possible = 0;
1018 /* Complain about a demangled name that we don't know how to parse.
1019 NAME is the demangled name in question. */
1022 demangled_name_complaint (const char *name)
1024 complaint (&symfile_complaints,
1025 "unexpected demangled name '%s'", name);
1028 /* If NAME is the fully-qualified name of a C++
1029 function/variable/method/etc., this returns the length of its
1030 entire prefix: all of the namespaces and classes that make up its
1031 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1032 4, given 'foo', it returns 0. */
1035 cp_entire_prefix_len (const char *name)
1037 unsigned int current_len = cp_find_first_component (name);
1038 unsigned int previous_len = 0;
1040 while (name[current_len] != '\0')
1042 gdb_assert (name[current_len] == ':');
1043 previous_len = current_len;
1044 /* Skip the '::'. */
1046 current_len += cp_find_first_component (name + current_len);
1049 return previous_len;
1052 /* Overload resolution functions. */
1054 /* Test to see if SYM is a symbol that we haven't seen corresponding
1055 to a function named OLOAD_NAME. If so, add it to the current
1059 overload_list_add_symbol (struct symbol *sym,
1060 const char *oload_name)
1066 /* If there is no type information, we can't do anything, so
1068 if (SYMBOL_TYPE (sym) == NULL)
1071 /* skip any symbols that we've already considered. */
1072 for (i = 0; i < sym_return_val_index; ++i)
1073 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1074 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1077 /* Get the demangled name without parameters */
1078 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1082 /* skip symbols that cannot match */
1083 if (strcmp (sym_name, oload_name) != 0)
1091 /* We have a match for an overload instance, so add SYM to the
1092 current list of overload instances */
1093 if (sym_return_val_index + 3 > sym_return_val_size)
1095 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1096 sym_return_val = (struct symbol **)
1097 xrealloc ((char *) sym_return_val, newsize);
1099 sym_return_val[sym_return_val_index++] = sym;
1100 sym_return_val[sym_return_val_index] = NULL;
1103 /* Return a null-terminated list of pointers to function symbols that
1104 are named FUNC_NAME and are visible within NAMESPACE. */
1107 make_symbol_overload_list (const char *func_name,
1108 const char *namespace)
1110 struct cleanup *old_cleanups;
1113 sym_return_val_size = 100;
1114 sym_return_val_index = 0;
1115 sym_return_val = xmalloc ((sym_return_val_size + 1) *
1116 sizeof (struct symbol *));
1117 sym_return_val[0] = NULL;
1119 old_cleanups = make_cleanup (xfree, sym_return_val);
1121 make_symbol_overload_list_using (func_name, namespace);
1123 if (namespace[0] == '\0')
1127 char *concatenated_name
1128 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1129 strcpy (concatenated_name, namespace);
1130 strcat (concatenated_name, "::");
1131 strcat (concatenated_name, func_name);
1132 name = concatenated_name;
1135 make_symbol_overload_list_qualified (name);
1137 discard_cleanups (old_cleanups);
1139 return sym_return_val;
1142 /* Add all symbols with a name matching NAME in BLOCK to the overload
1146 make_symbol_overload_list_block (const char *name,
1147 const struct block *block)
1149 struct dict_iterator iter;
1152 const struct dictionary *dict = BLOCK_DICT (block);
1154 for (sym = dict_iter_name_first (dict, name, &iter);
1156 sym = dict_iter_name_next (name, &iter))
1157 overload_list_add_symbol (sym, name);
1160 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1163 make_symbol_overload_list_namespace (const char *func_name,
1164 const char *namespace)
1167 const struct block *block = NULL;
1169 if (namespace[0] == '\0')
1173 char *concatenated_name
1174 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1);
1176 strcpy (concatenated_name, namespace);
1177 strcat (concatenated_name, "::");
1178 strcat (concatenated_name, func_name);
1179 name = concatenated_name;
1182 /* Look in the static block. */
1183 block = block_static_block (get_selected_block (0));
1185 make_symbol_overload_list_block (name, block);
1187 /* Look in the global block. */
1188 block = block_global_block (block);
1190 make_symbol_overload_list_block (name, block);
1194 /* Search the namespace of the given type and namespace of and public
1198 make_symbol_overload_list_adl_namespace (struct type *type,
1199 const char *func_name)
1205 while (TYPE_CODE (type) == TYPE_CODE_PTR
1206 || TYPE_CODE (type) == TYPE_CODE_REF
1207 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1208 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1210 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1211 type = check_typedef(type);
1213 type = TYPE_TARGET_TYPE (type);
1216 type_name = TYPE_NAME (type);
1218 if (type_name == NULL)
1221 prefix_len = cp_entire_prefix_len (type_name);
1223 if (prefix_len != 0)
1225 namespace = alloca (prefix_len + 1);
1226 strncpy (namespace, type_name, prefix_len);
1227 namespace[prefix_len] = '\0';
1229 make_symbol_overload_list_namespace (func_name, namespace);
1232 /* Check public base type */
1233 if (TYPE_CODE (type) == TYPE_CODE_CLASS)
1234 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1236 if (BASETYPE_VIA_PUBLIC (type, i))
1237 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1243 /* Adds the overload list overload candidates for FUNC_NAME found
1244 through argument dependent lookup. */
1247 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1248 const char *func_name)
1252 gdb_assert (sym_return_val_size != -1);
1254 for (i = 1; i <= nargs; i++)
1255 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1258 return sym_return_val;
1261 /* Used for cleanups to reset the "searched" flag in case of an
1265 reset_directive_searched (void *data)
1267 struct using_direct *direct = data;
1268 direct->searched = 0;
1271 /* This applies the using directives to add namespaces to search in,
1272 and then searches for overloads in all of those namespaces. It
1273 adds the symbols found to sym_return_val. Arguments are as in
1274 make_symbol_overload_list. */
1277 make_symbol_overload_list_using (const char *func_name,
1278 const char *namespace)
1280 struct using_direct *current;
1281 const struct block *block;
1283 /* First, go through the using directives. If any of them apply,
1284 look in the appropriate namespaces for new functions to match
1287 for (block = get_selected_block (0);
1289 block = BLOCK_SUPERBLOCK (block))
1290 for (current = block_using (block);
1292 current = current->next)
1294 /* Prevent recursive calls. */
1295 if (current->searched)
1298 /* If this is a namespace alias or imported declaration ignore
1300 if (current->alias != NULL || current->declaration != NULL)
1303 if (strcmp (namespace, current->import_dest) == 0)
1305 /* Mark this import as searched so that the recursive call
1306 does not search it again. */
1307 struct cleanup *old_chain;
1308 current->searched = 1;
1309 old_chain = make_cleanup (reset_directive_searched,
1312 make_symbol_overload_list_using (func_name,
1313 current->import_src);
1315 current->searched = 0;
1316 discard_cleanups (old_chain);
1320 /* Now, add names for this namespace. */
1321 make_symbol_overload_list_namespace (func_name, namespace);
1324 /* This does the bulk of the work of finding overloaded symbols.
1325 FUNC_NAME is the name of the overloaded function we're looking for
1326 (possibly including namespace info). */
1329 make_symbol_overload_list_qualified (const char *func_name)
1333 struct objfile *objfile;
1334 const struct block *b, *surrounding_static_block = 0;
1335 struct dict_iterator iter;
1336 const struct dictionary *dict;
1338 /* Look through the partial symtabs for all symbols which begin by
1339 matching FUNC_NAME. Make sure we read that symbol table in. */
1341 ALL_OBJFILES (objfile)
1344 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1347 /* Search upwards from currently selected frame (so that we can
1348 complete on local vars. */
1350 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1351 make_symbol_overload_list_block (func_name, b);
1353 surrounding_static_block = block_static_block (get_selected_block (0));
1355 /* Go through the symtabs and check the externs and statics for
1356 symbols which match. */
1358 ALL_PRIMARY_SYMTABS (objfile, s)
1361 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
1362 make_symbol_overload_list_block (func_name, b);
1365 ALL_PRIMARY_SYMTABS (objfile, s)
1368 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1369 /* Don't do this block twice. */
1370 if (b == surrounding_static_block)
1372 make_symbol_overload_list_block (func_name, b);
1376 /* Lookup the rtti type for a class name. */
1379 cp_lookup_rtti_type (const char *name, struct block *block)
1381 struct symbol * rtti_sym;
1382 struct type * rtti_type;
1384 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1386 if (rtti_sym == NULL)
1388 warning (_("RTTI symbol not found for class '%s'"), name);
1392 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1394 warning (_("RTTI symbol for class '%s' is not a type"), name);
1398 rtti_type = SYMBOL_TYPE (rtti_sym);
1400 switch (TYPE_CODE (rtti_type))
1402 case TYPE_CODE_CLASS:
1404 case TYPE_CODE_NAMESPACE:
1405 /* chastain/2003-11-26: the symbol tables often contain fake
1406 symbols for namespaces with the same name as the struct.
1407 This warning is an indication of a bug in the lookup order
1408 or a bug in the way that the symbol tables are populated. */
1409 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1412 warning (_("RTTI symbol for class '%s' has bad type"), name);
1419 /* Don't allow just "maintenance cplus". */
1422 maint_cplus_command (char *arg, int from_tty)
1424 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1425 "by the name of a command.\n"));
1426 help_list (maint_cplus_cmd_list,
1427 "maintenance cplus ",
1431 /* This is a front end for cp_find_first_component, for unit testing.
1432 Be careful when using it: see the NOTE above
1433 cp_find_first_component. */
1436 first_component_command (char *arg, int from_tty)
1444 len = cp_find_first_component (arg);
1445 prefix = alloca (len + 1);
1447 memcpy (prefix, arg, len);
1450 printf_unfiltered ("%s\n", prefix);
1453 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
1455 #define SKIP_SPACE(P) \
1458 while (*(P) == ' ' || *(P) == '\t') \
1463 /* Returns the length of the operator name or 0 if INPUT does not
1464 point to a valid C++ operator. INPUT should start with
1467 cp_validate_operator (const char *input)
1472 struct expression *expr;
1474 struct gdb_exception except;
1478 if (strncmp (p, "operator", 8) == 0)
1485 i < sizeof (operator_tokens) / sizeof (operator_tokens[0]);
1488 int length = strlen (operator_tokens[i]);
1490 /* By using strncmp here, we MUST have operator_tokens
1491 ordered! See additional notes where operator_tokens is
1493 if (strncmp (p, operator_tokens[i], length) == 0)
1500 if (strncmp (op, "new", 3) == 0
1501 || strncmp (op, "delete", 6) == 0)
1504 /* Special case: new[] and delete[]. We must be
1505 careful to swallow whitespace before/in "[]". */
1524 /* Check input for a conversion operator. */
1526 /* Skip past base typename. */
1527 while (*p != '*' && *p != '&' && *p != 0 && *p != ' ')
1531 /* Add modifiers '*' / '&'. */
1532 while (*p == '*' || *p == '&')
1538 /* Check for valid type. [Remember: input starts with
1540 copy = savestring (input + 8, p - input - 8);
1543 TRY_CATCH (except, RETURN_MASK_ALL)
1545 expr = parse_expression (copy);
1546 val = evaluate_type (expr);
1553 if (val != NULL && value_type (val) != NULL)
1561 _initialize_cp_support (void)
1563 add_prefix_cmd ("cplus", class_maintenance,
1564 maint_cplus_command,
1565 _("C++ maintenance commands."),
1566 &maint_cplus_cmd_list,
1567 "maintenance cplus ",
1568 0, &maintenancelist);
1569 add_alias_cmd ("cp", "cplus",
1570 class_maintenance, 1,
1573 add_cmd ("first_component",
1575 first_component_command,
1576 _("Print the first class/namespace component of NAME."),
1577 &maint_cplus_cmd_list);