1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2014 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"
25 #include "gdb_assert.h"
27 #include "dictionary.h"
32 #include "complaints.h"
34 #include "exceptions.h"
35 #include "expression.h"
40 #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 /* A list of typedefs which should not be substituted by replace_typedefs. */
76 static const char * const ignore_typedefs
[] =
78 "std::istream", "std::iostream", "std::ostream", "std::string"
82 replace_typedefs (struct demangle_parse_info
*info
,
83 struct demangle_component
*ret_comp
,
84 canonicalization_ftype
*finder
,
87 /* A convenience function to copy STRING into OBSTACK, returning a pointer
88 to the newly allocated string and saving the number of bytes saved in LEN.
90 It does not copy the terminating '\0' byte! */
93 copy_string_to_obstack (struct obstack
*obstack
, const char *string
,
96 *len
= strlen (string
);
97 return obstack_copy (obstack
, string
, *len
);
100 /* A cleanup wrapper for cp_demangled_name_parse_free. */
103 do_demangled_name_parse_free_cleanup (void *data
)
105 struct demangle_parse_info
*info
= (struct demangle_parse_info
*) data
;
107 cp_demangled_name_parse_free (info
);
110 /* Create a cleanup for C++ name parsing. */
113 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info
*info
)
115 return make_cleanup (do_demangled_name_parse_free_cleanup
, info
);
118 /* Return 1 if STRING is clearly already in canonical form. This
119 function is conservative; things which it does not recognize are
120 assumed to be non-canonical, and the parser will sort them out
121 afterwards. This speeds up the critical path for alphanumeric
125 cp_already_canonical (const char *string
)
127 /* Identifier start character [a-zA-Z_]. */
128 if (!ISIDST (string
[0]))
131 /* These are the only two identifiers which canonicalize to other
132 than themselves or an error: unsigned -> unsigned int and
134 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
136 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
139 /* Identifier character [a-zA-Z0-9_]. */
140 while (ISIDNUM (string
[1]))
143 if (string
[1] == '\0')
149 /* Inspect the given RET_COMP for its type. If it is a typedef,
150 replace the node with the typedef's tree.
152 Returns 1 if any typedef substitutions were made, 0 otherwise. */
155 inspect_type (struct demangle_parse_info
*info
,
156 struct demangle_component
*ret_comp
,
157 canonicalization_ftype
*finder
,
163 volatile struct gdb_exception except
;
165 /* Copy the symbol's name from RET_COMP and look it up
166 in the symbol table. */
167 name
= (char *) alloca (ret_comp
->u
.s_name
.len
+ 1);
168 memcpy (name
, ret_comp
->u
.s_name
.s
, ret_comp
->u
.s_name
.len
);
169 name
[ret_comp
->u
.s_name
.len
] = '\0';
171 /* Ignore any typedefs that should not be substituted. */
172 for (i
= 0; i
< ARRAY_SIZE (ignore_typedefs
); ++i
)
174 if (strcmp (name
, ignore_typedefs
[i
]) == 0)
179 TRY_CATCH (except
, RETURN_MASK_ALL
)
181 /* It is not legal to have a typedef and tag name of the same
182 name in C++. However, anonymous composite types that are defined
183 with a typedef ["typedef struct {...} anonymous_struct;"] WILL
184 have symbols for a TYPE_CODE_TYPEDEF (in VAR_DOMAIN) and a
185 TYPE_CODE_STRUCT (in STRUCT_DOMAIN).
187 If VAR_DOMAIN is searched first, it will return the TYPEDEF symbol,
188 and this function will never output the definition of the typedef,
189 since type_print is called below with SHOW = -1. [The typedef hash
190 is never initialized/used when SHOW <= 0 -- and the finder
191 (find_typedef_for_canonicalize) will always return NULL as a result.]
193 Consequently, type_print will eventually keep calling this function
194 to replace the typedef (via
195 print_name_maybe_canonical/cp_canonicalize_full). This leads to
198 This can all be safely avoid by explicitly searching STRUCT_DOMAIN
199 first to find the structure definition. */
200 if (current_language
->la_language
== language_cplus
)
201 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0);
203 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, NULL
);
206 if (except
.reason
>= 0 && sym
!= NULL
)
208 struct type
*otype
= SYMBOL_TYPE (sym
);
212 const char *new_name
= (*finder
) (otype
, data
);
214 if (new_name
!= NULL
)
216 ret_comp
->u
.s_name
.s
= new_name
;
217 ret_comp
->u
.s_name
.len
= strlen (new_name
);
224 /* If the type is a typedef or namespace alias, replace it. */
225 if (TYPE_CODE (otype
) == TYPE_CODE_TYPEDEF
226 || TYPE_CODE (otype
) == TYPE_CODE_NAMESPACE
)
231 struct demangle_parse_info
*i
;
234 /* Get the real type of the typedef. */
235 type
= check_typedef (otype
);
237 /* If the symbol is a namespace and its type name is no different
238 than the name we looked up, this symbol is not a namespace
239 alias and does not need to be substituted. */
240 if (TYPE_CODE (otype
) == TYPE_CODE_NAMESPACE
241 && strcmp (TYPE_NAME (type
), name
) == 0)
244 is_anon
= (TYPE_TAG_NAME (type
) == NULL
245 && (TYPE_CODE (type
) == TYPE_CODE_ENUM
246 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
247 || TYPE_CODE (type
) == TYPE_CODE_UNION
));
250 struct type
*last
= otype
;
252 /* Find the last typedef for the type. */
253 while (TYPE_TARGET_TYPE (last
) != NULL
254 && (TYPE_CODE (TYPE_TARGET_TYPE (last
))
255 == TYPE_CODE_TYPEDEF
))
256 last
= TYPE_TARGET_TYPE (last
);
258 /* If there is only one typedef for this anonymous type,
259 do not substitute it. */
263 /* Use the last typedef seen as the type for this
268 buf
= mem_fileopen ();
269 TRY_CATCH (except
, RETURN_MASK_ERROR
)
271 type_print (type
, "", buf
, -1);
274 /* If type_print threw an exception, there is little point
275 in continuing, so just bow out gracefully. */
276 if (except
.reason
< 0)
278 ui_file_delete (buf
);
282 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
283 ui_file_delete (buf
);
285 /* Turn the result into a new tree. Note that this
286 tree will contain pointers into NAME, so NAME cannot
287 be free'd until all typedef conversion is done and
288 the final result is converted into a string. */
289 i
= cp_demangled_name_to_comp (name
, NULL
);
292 /* Merge the two trees. */
293 cp_merge_demangle_parse_infos (info
, ret_comp
, i
);
295 /* Replace any newly introduced typedefs -- but not
296 if the type is anonymous (that would lead to infinite
299 replace_typedefs (info
, ret_comp
, finder
, data
);
303 /* This shouldn't happen unless the type printer has
304 output something that the name parser cannot grok.
305 Nonetheless, an ounce of prevention...
307 Canonicalize the name again, and store it in the
308 current node (RET_COMP). */
309 char *canon
= cp_canonicalize_string_no_typedefs (name
);
313 /* Copy the canonicalization into the obstack and
315 name
= copy_string_to_obstack (&info
->obstack
, canon
, &len
);
319 ret_comp
->u
.s_name
.s
= name
;
320 ret_comp
->u
.s_name
.len
= len
;
330 /* Replace any typedefs appearing in the qualified name
331 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
335 replace_typedefs_qualified_name (struct demangle_parse_info
*info
,
336 struct demangle_component
*ret_comp
,
337 canonicalization_ftype
*finder
,
342 struct ui_file
*buf
= mem_fileopen ();
343 struct demangle_component
*comp
= ret_comp
;
345 /* Walk each node of the qualified name, reconstructing the name of
346 this element. With every node, check for any typedef substitutions.
347 If a substitution has occurred, replace the qualified name node
348 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
350 while (comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
352 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_NAME
)
354 struct demangle_component
new;
356 ui_file_write (buf
, d_left (comp
)->u
.s_name
.s
,
357 d_left (comp
)->u
.s_name
.len
);
358 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
359 new.type
= DEMANGLE_COMPONENT_NAME
;
360 new.u
.s_name
.s
= name
;
361 new.u
.s_name
.len
= len
;
362 if (inspect_type (info
, &new, finder
, data
))
367 /* A typedef was substituted in NEW. Convert it to a
368 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
371 ui_file_rewind (buf
);
372 n
= cp_comp_to_string (&new, 100);
375 /* If something went astray, abort typedef substitutions. */
376 ui_file_delete (buf
);
380 s
= copy_string_to_obstack (&info
->obstack
, n
, &slen
);
383 d_left (ret_comp
)->type
= DEMANGLE_COMPONENT_NAME
;
384 d_left (ret_comp
)->u
.s_name
.s
= s
;
385 d_left (ret_comp
)->u
.s_name
.len
= slen
;
386 d_right (ret_comp
) = d_right (comp
);
393 /* The current node is not a name, so simply replace any
394 typedefs in it. Then print it to the stream to continue
395 checking for more typedefs in the tree. */
396 replace_typedefs (info
, d_left (comp
), finder
, data
);
397 name
= cp_comp_to_string (d_left (comp
), 100);
400 /* If something went astray, abort typedef substitutions. */
401 ui_file_delete (buf
);
404 fputs_unfiltered (name
, buf
);
408 ui_file_write (buf
, "::", 2);
409 comp
= d_right (comp
);
412 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
413 name assembled above and append the name given by COMP. Then use this
414 reassembled name to check for a typedef. */
416 if (comp
->type
== DEMANGLE_COMPONENT_NAME
)
418 ui_file_write (buf
, comp
->u
.s_name
.s
, comp
->u
.s_name
.len
);
419 name
= ui_file_obsavestring (buf
, &info
->obstack
, &len
);
421 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
422 with a DEMANGLE_COMPONENT_NAME node containing the whole
424 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
425 ret_comp
->u
.s_name
.s
= name
;
426 ret_comp
->u
.s_name
.len
= len
;
427 inspect_type (info
, ret_comp
, finder
, data
);
430 replace_typedefs (info
, comp
, finder
, data
);
432 ui_file_delete (buf
);
436 /* A function to check const and volatile qualifiers for argument types.
438 "Parameter declarations that differ only in the presence
439 or absence of `const' and/or `volatile' are equivalent."
440 C++ Standard N3290, clause 13.1.3 #4. */
443 check_cv_qualifiers (struct demangle_component
*ret_comp
)
445 while (d_left (ret_comp
) != NULL
446 && (d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_CONST
447 || d_left (ret_comp
)->type
== DEMANGLE_COMPONENT_VOLATILE
))
449 d_left (ret_comp
) = d_left (d_left (ret_comp
));
453 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
454 their basic types. */
457 replace_typedefs (struct demangle_parse_info
*info
,
458 struct demangle_component
*ret_comp
,
459 canonicalization_ftype
*finder
,
465 && (ret_comp
->type
== DEMANGLE_COMPONENT_NAME
466 || ret_comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
467 || ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
468 || ret_comp
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
))
470 char *local_name
= cp_comp_to_string (ret_comp
, 10);
472 if (local_name
!= NULL
)
475 volatile struct gdb_exception except
;
478 TRY_CATCH (except
, RETURN_MASK_ALL
)
480 sym
= lookup_symbol (local_name
, 0, VAR_DOMAIN
, 0);
484 if (except
.reason
>= 0 && sym
!= NULL
)
486 struct type
*otype
= SYMBOL_TYPE (sym
);
487 const char *new_name
= (*finder
) (otype
, data
);
489 if (new_name
!= NULL
)
491 ret_comp
->type
= DEMANGLE_COMPONENT_NAME
;
492 ret_comp
->u
.s_name
.s
= new_name
;
493 ret_comp
->u
.s_name
.len
= strlen (new_name
);
500 switch (ret_comp
->type
)
502 case DEMANGLE_COMPONENT_ARGLIST
:
503 check_cv_qualifiers (ret_comp
);
506 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
507 case DEMANGLE_COMPONENT_TEMPLATE
:
508 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
509 case DEMANGLE_COMPONENT_TYPED_NAME
:
510 replace_typedefs (info
, d_left (ret_comp
), finder
, data
);
511 replace_typedefs (info
, d_right (ret_comp
), finder
, data
);
514 case DEMANGLE_COMPONENT_NAME
:
515 inspect_type (info
, ret_comp
, finder
, data
);
518 case DEMANGLE_COMPONENT_QUAL_NAME
:
519 replace_typedefs_qualified_name (info
, ret_comp
, finder
, data
);
522 case DEMANGLE_COMPONENT_LOCAL_NAME
:
523 case DEMANGLE_COMPONENT_CTOR
:
524 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
525 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
526 replace_typedefs (info
, d_right (ret_comp
), finder
, data
);
529 case DEMANGLE_COMPONENT_CONST
:
530 case DEMANGLE_COMPONENT_RESTRICT
:
531 case DEMANGLE_COMPONENT_VOLATILE
:
532 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
533 case DEMANGLE_COMPONENT_CONST_THIS
:
534 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
535 case DEMANGLE_COMPONENT_POINTER
:
536 case DEMANGLE_COMPONENT_REFERENCE
:
537 replace_typedefs (info
, d_left (ret_comp
), finder
, data
);
546 /* Parse STRING and convert it to canonical form, resolving any typedefs.
547 If parsing fails, or if STRING is already canonical, return NULL.
548 Otherwise return the canonical form. The return value is allocated via
549 xmalloc. If FINDER is not NULL, then type components are passed to
550 FINDER to be looked up. DATA is passed verbatim to FINDER. */
553 cp_canonicalize_string_full (const char *string
,
554 canonicalization_ftype
*finder
,
558 unsigned int estimated_len
;
559 struct demangle_parse_info
*info
;
562 estimated_len
= strlen (string
) * 2;
563 info
= cp_demangled_name_to_comp (string
, NULL
);
566 /* Replace all the typedefs in the tree. */
567 replace_typedefs (info
, info
->tree
, finder
, data
);
569 /* Convert the tree back into a string. */
570 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
571 gdb_assert (ret
!= NULL
);
573 /* Free the parse information. */
574 cp_demangled_name_parse_free (info
);
576 /* Finally, compare the original string with the computed
577 name, returning NULL if they are the same. */
578 if (strcmp (string
, ret
) == 0)
588 /* Like cp_canonicalize_string_full, but always passes NULL for
592 cp_canonicalize_string_no_typedefs (const char *string
)
594 return cp_canonicalize_string_full (string
, NULL
, NULL
);
597 /* Parse STRING and convert it to canonical form. If parsing fails,
598 or if STRING is already canonical, return NULL. Otherwise return
599 the canonical form. The return value is allocated via xmalloc. */
602 cp_canonicalize_string (const char *string
)
604 struct demangle_parse_info
*info
;
605 unsigned int estimated_len
;
608 if (cp_already_canonical (string
))
611 info
= cp_demangled_name_to_comp (string
, NULL
);
615 estimated_len
= strlen (string
) * 2;
616 ret
= cp_comp_to_string (info
->tree
, estimated_len
);
617 cp_demangled_name_parse_free (info
);
621 warning (_("internal error: string \"%s\" failed to be canonicalized"),
626 if (strcmp (string
, ret
) == 0)
635 /* Convert a mangled name to a demangle_component tree. *MEMORY is
636 set to the block of used memory that should be freed when finished
637 with the tree. DEMANGLED_P is set to the char * that should be
638 freed when finished with the tree, or NULL if none was needed.
639 OPTIONS will be passed to the demangler. */
641 static struct demangle_parse_info
*
642 mangled_name_to_comp (const char *mangled_name
, int options
,
643 void **memory
, char **demangled_p
)
645 char *demangled_name
;
646 struct demangle_parse_info
*info
;
648 /* If it looks like a v3 mangled name, then try to go directly
650 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
652 struct demangle_component
*ret
;
654 ret
= cplus_demangle_v3_components (mangled_name
,
658 info
= cp_new_demangle_parse_info ();
665 /* If it doesn't, or if that failed, then try to demangle the
667 demangled_name
= gdb_demangle (mangled_name
, options
);
668 if (demangled_name
== NULL
)
671 /* If we could demangle the name, parse it to build the component
673 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
677 xfree (demangled_name
);
681 *demangled_p
= demangled_name
;
685 /* Return the name of the class containing method PHYSNAME. */
688 cp_class_name_from_physname (const char *physname
)
690 void *storage
= NULL
;
691 char *demangled_name
= NULL
, *ret
;
692 struct demangle_component
*ret_comp
, *prev_comp
, *cur_comp
;
693 struct demangle_parse_info
*info
;
696 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
697 &storage
, &demangled_name
);
702 ret_comp
= info
->tree
;
704 /* First strip off any qualifiers, if we have a function or
707 switch (ret_comp
->type
)
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
);
723 /* If what we have now is a function, discard the argument list. */
724 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
725 ret_comp
= d_left (ret_comp
);
727 /* If what we have now is a template, strip off the template
728 arguments. The left subtree may be a qualified name. */
729 if (ret_comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
730 ret_comp
= d_left (ret_comp
);
732 /* What we have now should be a name, possibly qualified.
733 Additional qualifiers could live in the left subtree or the right
734 subtree. Find the last piece. */
739 switch (cur_comp
->type
)
741 case DEMANGLE_COMPONENT_QUAL_NAME
:
742 case DEMANGLE_COMPONENT_LOCAL_NAME
:
743 prev_comp
= cur_comp
;
744 cur_comp
= d_right (cur_comp
);
746 case DEMANGLE_COMPONENT_TEMPLATE
:
747 case DEMANGLE_COMPONENT_NAME
:
748 case DEMANGLE_COMPONENT_CTOR
:
749 case DEMANGLE_COMPONENT_DTOR
:
750 case DEMANGLE_COMPONENT_OPERATOR
:
751 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
761 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
763 /* We want to discard the rightmost child of PREV_COMP. */
764 *prev_comp
= *d_left (prev_comp
);
765 /* The ten is completely arbitrary; we don't have a good
767 ret
= cp_comp_to_string (ret_comp
, 10);
771 xfree (demangled_name
);
772 cp_demangled_name_parse_free (info
);
776 /* Return the child of COMP which is the basename of a method,
777 variable, et cetera. All scope qualifiers are discarded, but
778 template arguments will be included. The component tree may be
781 static struct demangle_component
*
782 unqualified_name_from_comp (struct demangle_component
*comp
)
784 struct demangle_component
*ret_comp
= comp
, *last_template
;
788 last_template
= NULL
;
790 switch (ret_comp
->type
)
792 case DEMANGLE_COMPONENT_QUAL_NAME
:
793 case DEMANGLE_COMPONENT_LOCAL_NAME
:
794 ret_comp
= d_right (ret_comp
);
796 case DEMANGLE_COMPONENT_TYPED_NAME
:
797 ret_comp
= d_left (ret_comp
);
799 case DEMANGLE_COMPONENT_TEMPLATE
:
800 gdb_assert (last_template
== NULL
);
801 last_template
= ret_comp
;
802 ret_comp
= d_left (ret_comp
);
804 case DEMANGLE_COMPONENT_CONST
:
805 case DEMANGLE_COMPONENT_RESTRICT
:
806 case DEMANGLE_COMPONENT_VOLATILE
:
807 case DEMANGLE_COMPONENT_CONST_THIS
:
808 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
809 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
810 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
811 ret_comp
= d_left (ret_comp
);
813 case DEMANGLE_COMPONENT_NAME
:
814 case DEMANGLE_COMPONENT_CTOR
:
815 case DEMANGLE_COMPONENT_DTOR
:
816 case DEMANGLE_COMPONENT_OPERATOR
:
817 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
827 d_left (last_template
) = ret_comp
;
828 return last_template
;
834 /* Return the name of the method whose linkage name is PHYSNAME. */
837 method_name_from_physname (const char *physname
)
839 void *storage
= NULL
;
840 char *demangled_name
= NULL
, *ret
;
841 struct demangle_component
*ret_comp
;
842 struct demangle_parse_info
*info
;
844 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
845 &storage
, &demangled_name
);
849 ret_comp
= unqualified_name_from_comp (info
->tree
);
852 if (ret_comp
!= NULL
)
853 /* The ten is completely arbitrary; we don't have a good
855 ret
= cp_comp_to_string (ret_comp
, 10);
858 xfree (demangled_name
);
859 cp_demangled_name_parse_free (info
);
863 /* If FULL_NAME is the demangled name of a C++ function (including an
864 arg list, possibly including namespace/class qualifications),
865 return a new string containing only the function name (without the
866 arg list/class qualifications). Otherwise, return NULL. The
867 caller is responsible for freeing the memory in question. */
870 cp_func_name (const char *full_name
)
873 struct demangle_component
*ret_comp
;
874 struct demangle_parse_info
*info
;
876 info
= cp_demangled_name_to_comp (full_name
, NULL
);
880 ret_comp
= unqualified_name_from_comp (info
->tree
);
883 if (ret_comp
!= NULL
)
884 ret
= cp_comp_to_string (ret_comp
, 10);
886 cp_demangled_name_parse_free (info
);
890 /* DEMANGLED_NAME is the name of a function, including parameters and
891 (optionally) a return type. Return the name of the function without
892 parameters or return type, or NULL if we can not parse the name. */
895 cp_remove_params (const char *demangled_name
)
898 struct demangle_component
*ret_comp
;
899 struct demangle_parse_info
*info
;
902 if (demangled_name
== NULL
)
905 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
909 /* First strip off any qualifiers, if we have a function or method. */
910 ret_comp
= info
->tree
;
912 switch (ret_comp
->type
)
914 case DEMANGLE_COMPONENT_CONST
:
915 case DEMANGLE_COMPONENT_RESTRICT
:
916 case DEMANGLE_COMPONENT_VOLATILE
:
917 case DEMANGLE_COMPONENT_CONST_THIS
:
918 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
919 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
920 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
921 ret_comp
= d_left (ret_comp
);
928 /* What we have now should be a function. Return its name. */
929 if (ret_comp
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
930 ret
= cp_comp_to_string (d_left (ret_comp
), 10);
932 cp_demangled_name_parse_free (info
);
936 /* Here are some random pieces of trivia to keep in mind while trying
937 to take apart demangled names:
939 - Names can contain function arguments or templates, so the process
940 has to be, to some extent recursive: maybe keep track of your
941 depth based on encountering <> and ().
943 - Parentheses don't just have to happen at the end of a name: they
944 can occur even if the name in question isn't a function, because
945 a template argument might be a type that's a function.
947 - Conversely, even if you're trying to deal with a function, its
948 demangled name might not end with ')': it could be a const or
949 volatile class method, in which case it ends with "const" or
952 - Parentheses are also used in anonymous namespaces: a variable
953 'foo' in an anonymous namespace gets demangled as "(anonymous
956 - And operator names can contain parentheses or angle brackets. */
958 /* FIXME: carlton/2003-03-13: We have several functions here with
959 overlapping functionality; can we combine them? Also, do they
960 handle all the above considerations correctly? */
963 /* This returns the length of first component of NAME, which should be
964 the demangled name of a C++ variable/function/method/etc.
965 Specifically, it returns the index of the first colon forming the
966 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
967 it returns the 1, and given 'foo', it returns 0. */
969 /* The character in NAME indexed by the return value is guaranteed to
970 always be either ':' or '\0'. */
972 /* NOTE: carlton/2003-03-13: This function is currently only intended
973 for internal use: it's probably not entirely safe when called on
974 user-generated input, because some of the 'index += 2' lines in
975 cp_find_first_component_aux might go past the end of malformed
979 cp_find_first_component (const char *name
)
981 return cp_find_first_component_aux (name
, 0);
984 /* Helper function for cp_find_first_component. Like that function,
985 it returns the length of the first component of NAME, but to make
986 the recursion easier, it also stops if it reaches an unexpected ')'
987 or '>' if the value of PERMISSIVE is nonzero. */
989 /* Let's optimize away calls to strlen("operator"). */
991 #define LENGTH_OF_OPERATOR 8
994 cp_find_first_component_aux (const char *name
, int permissive
)
996 unsigned int index
= 0;
997 /* Operator names can show up in unexpected places. Since these can
998 contain parentheses or angle brackets, they can screw up the
999 recursion. But not every string 'operator' is part of an
1000 operater name: e.g. you could have a variable 'cooperator'. So
1001 this variable tells us whether or not we should treat the string
1002 'operator' as starting an operator. */
1003 int operator_possible
= 1;
1007 switch (name
[index
])
1010 /* Template; eat it up. The calls to cp_first_component
1011 should only return (I hope!) when they reach the '>'
1012 terminating the component or a '::' between two
1013 components. (Hence the '+ 2'.) */
1015 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
1017 index
+= cp_find_first_component_aux (name
+ index
, 1))
1019 if (name
[index
] != ':')
1021 demangled_name_complaint (name
);
1022 return strlen (name
);
1026 operator_possible
= 1;
1029 /* Similar comment as to '<'. */
1031 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
1033 index
+= cp_find_first_component_aux (name
+ index
, 1))
1035 if (name
[index
] != ':')
1037 demangled_name_complaint (name
);
1038 return strlen (name
);
1042 operator_possible
= 1;
1050 demangled_name_complaint (name
);
1051 return strlen (name
);
1057 /* Operator names can screw up the recursion. */
1058 if (operator_possible
1059 && strncmp (name
+ index
, "operator",
1060 LENGTH_OF_OPERATOR
) == 0)
1062 index
+= LENGTH_OF_OPERATOR
;
1063 while (ISSPACE(name
[index
]))
1065 switch (name
[index
])
1067 /* Skip over one less than the appropriate number of
1068 characters: the for loop will skip over the last
1071 if (name
[index
+ 1] == '<')
1078 if (name
[index
+ 1] == '>')
1091 operator_possible
= 0;
1098 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1099 set of relevant characters are here: it's necessary to
1100 include any character that can show up before 'operator'
1101 in a demangled name, and it's safe to include any
1102 character that can't be part of an identifier's name. */
1103 operator_possible
= 1;
1106 operator_possible
= 0;
1112 /* Complain about a demangled name that we don't know how to parse.
1113 NAME is the demangled name in question. */
1116 demangled_name_complaint (const char *name
)
1118 complaint (&symfile_complaints
,
1119 "unexpected demangled name '%s'", name
);
1122 /* If NAME is the fully-qualified name of a C++
1123 function/variable/method/etc., this returns the length of its
1124 entire prefix: all of the namespaces and classes that make up its
1125 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1126 4, given 'foo', it returns 0. */
1129 cp_entire_prefix_len (const char *name
)
1131 unsigned int current_len
= cp_find_first_component (name
);
1132 unsigned int previous_len
= 0;
1134 while (name
[current_len
] != '\0')
1136 gdb_assert (name
[current_len
] == ':');
1137 previous_len
= current_len
;
1138 /* Skip the '::'. */
1140 current_len
+= cp_find_first_component (name
+ current_len
);
1143 return previous_len
;
1146 /* Overload resolution functions. */
1148 /* Test to see if SYM is a symbol that we haven't seen corresponding
1149 to a function named OLOAD_NAME. If so, add it to the current
1153 overload_list_add_symbol (struct symbol
*sym
,
1154 const char *oload_name
)
1160 /* If there is no type information, we can't do anything, so
1162 if (SYMBOL_TYPE (sym
) == NULL
)
1165 /* skip any symbols that we've already considered. */
1166 for (i
= 0; i
< sym_return_val_index
; ++i
)
1167 if (strcmp (SYMBOL_LINKAGE_NAME (sym
),
1168 SYMBOL_LINKAGE_NAME (sym_return_val
[i
])) == 0)
1171 /* Get the demangled name without parameters */
1172 sym_name
= cp_remove_params (SYMBOL_NATURAL_NAME (sym
));
1176 /* skip symbols that cannot match */
1177 if (strcmp (sym_name
, oload_name
) != 0)
1185 /* We have a match for an overload instance, so add SYM to the
1186 current list of overload instances */
1187 if (sym_return_val_index
+ 3 > sym_return_val_size
)
1189 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
1190 sym_return_val
= (struct symbol
**)
1191 xrealloc ((char *) sym_return_val
, newsize
);
1193 sym_return_val
[sym_return_val_index
++] = sym
;
1194 sym_return_val
[sym_return_val_index
] = NULL
;
1197 /* Return a null-terminated list of pointers to function symbols that
1198 are named FUNC_NAME and are visible within NAMESPACE. */
1201 make_symbol_overload_list (const char *func_name
,
1202 const char *namespace)
1204 struct cleanup
*old_cleanups
;
1207 sym_return_val_size
= 100;
1208 sym_return_val_index
= 0;
1209 sym_return_val
= xmalloc ((sym_return_val_size
+ 1) *
1210 sizeof (struct symbol
*));
1211 sym_return_val
[0] = NULL
;
1213 old_cleanups
= make_cleanup (xfree
, sym_return_val
);
1215 make_symbol_overload_list_using (func_name
, namespace);
1217 if (namespace[0] == '\0')
1221 char *concatenated_name
1222 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1223 strcpy (concatenated_name
, namespace);
1224 strcat (concatenated_name
, "::");
1225 strcat (concatenated_name
, func_name
);
1226 name
= concatenated_name
;
1229 make_symbol_overload_list_qualified (name
);
1231 discard_cleanups (old_cleanups
);
1233 return sym_return_val
;
1236 /* Add all symbols with a name matching NAME in BLOCK to the overload
1240 make_symbol_overload_list_block (const char *name
,
1241 const struct block
*block
)
1243 struct block_iterator iter
;
1246 for (sym
= block_iter_name_first (block
, name
, &iter
);
1248 sym
= block_iter_name_next (name
, &iter
))
1249 overload_list_add_symbol (sym
, name
);
1252 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1255 make_symbol_overload_list_namespace (const char *func_name
,
1256 const char *namespace)
1259 const struct block
*block
= NULL
;
1261 if (namespace[0] == '\0')
1265 char *concatenated_name
1266 = alloca (strlen (namespace) + 2 + strlen (func_name
) + 1);
1268 strcpy (concatenated_name
, namespace);
1269 strcat (concatenated_name
, "::");
1270 strcat (concatenated_name
, func_name
);
1271 name
= concatenated_name
;
1274 /* Look in the static block. */
1275 block
= block_static_block (get_selected_block (0));
1277 make_symbol_overload_list_block (name
, block
);
1279 /* Look in the global block. */
1280 block
= block_global_block (block
);
1282 make_symbol_overload_list_block (name
, block
);
1286 /* Search the namespace of the given type and namespace of and public
1290 make_symbol_overload_list_adl_namespace (struct type
*type
,
1291 const char *func_name
)
1294 const char *type_name
;
1297 while (TYPE_CODE (type
) == TYPE_CODE_PTR
1298 || TYPE_CODE (type
) == TYPE_CODE_REF
1299 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1300 || TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1302 if (TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1303 type
= check_typedef(type
);
1305 type
= TYPE_TARGET_TYPE (type
);
1308 type_name
= TYPE_NAME (type
);
1310 if (type_name
== NULL
)
1313 prefix_len
= cp_entire_prefix_len (type_name
);
1315 if (prefix_len
!= 0)
1317 namespace = alloca (prefix_len
+ 1);
1318 strncpy (namespace, type_name
, prefix_len
);
1319 namespace[prefix_len
] = '\0';
1321 make_symbol_overload_list_namespace (func_name
, namespace);
1324 /* Check public base type */
1325 if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
1326 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
1328 if (BASETYPE_VIA_PUBLIC (type
, i
))
1329 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type
,
1335 /* Adds the overload list overload candidates for FUNC_NAME found
1336 through argument dependent lookup. */
1339 make_symbol_overload_list_adl (struct type
**arg_types
, int nargs
,
1340 const char *func_name
)
1344 gdb_assert (sym_return_val_size
!= -1);
1346 for (i
= 1; i
<= nargs
; i
++)
1347 make_symbol_overload_list_adl_namespace (arg_types
[i
- 1],
1350 return sym_return_val
;
1353 /* Used for cleanups to reset the "searched" flag in case of an
1357 reset_directive_searched (void *data
)
1359 struct using_direct
*direct
= data
;
1360 direct
->searched
= 0;
1363 /* This applies the using directives to add namespaces to search in,
1364 and then searches for overloads in all of those namespaces. It
1365 adds the symbols found to sym_return_val. Arguments are as in
1366 make_symbol_overload_list. */
1369 make_symbol_overload_list_using (const char *func_name
,
1370 const char *namespace)
1372 struct using_direct
*current
;
1373 const struct block
*block
;
1375 /* First, go through the using directives. If any of them apply,
1376 look in the appropriate namespaces for new functions to match
1379 for (block
= get_selected_block (0);
1381 block
= BLOCK_SUPERBLOCK (block
))
1382 for (current
= block_using (block
);
1384 current
= current
->next
)
1386 /* Prevent recursive calls. */
1387 if (current
->searched
)
1390 /* If this is a namespace alias or imported declaration ignore
1392 if (current
->alias
!= NULL
|| current
->declaration
!= NULL
)
1395 if (strcmp (namespace, current
->import_dest
) == 0)
1397 /* Mark this import as searched so that the recursive call
1398 does not search it again. */
1399 struct cleanup
*old_chain
;
1400 current
->searched
= 1;
1401 old_chain
= make_cleanup (reset_directive_searched
,
1404 make_symbol_overload_list_using (func_name
,
1405 current
->import_src
);
1407 current
->searched
= 0;
1408 discard_cleanups (old_chain
);
1412 /* Now, add names for this namespace. */
1413 make_symbol_overload_list_namespace (func_name
, namespace);
1416 /* This does the bulk of the work of finding overloaded symbols.
1417 FUNC_NAME is the name of the overloaded function we're looking for
1418 (possibly including namespace info). */
1421 make_symbol_overload_list_qualified (const char *func_name
)
1424 struct objfile
*objfile
;
1425 const struct block
*b
, *surrounding_static_block
= 0;
1427 /* Look through the partial symtabs for all symbols which begin by
1428 matching FUNC_NAME. Make sure we read that symbol table in. */
1430 ALL_OBJFILES (objfile
)
1433 objfile
->sf
->qf
->expand_symtabs_for_function (objfile
, func_name
);
1436 /* Search upwards from currently selected frame (so that we can
1437 complete on local vars. */
1439 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
1440 make_symbol_overload_list_block (func_name
, b
);
1442 surrounding_static_block
= block_static_block (get_selected_block (0));
1444 /* Go through the symtabs and check the externs and statics for
1445 symbols which match. */
1447 ALL_PRIMARY_SYMTABS (objfile
, s
)
1450 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
1451 make_symbol_overload_list_block (func_name
, b
);
1454 ALL_PRIMARY_SYMTABS (objfile
, s
)
1457 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1458 /* Don't do this block twice. */
1459 if (b
== surrounding_static_block
)
1461 make_symbol_overload_list_block (func_name
, b
);
1465 /* Lookup the rtti type for a class name. */
1468 cp_lookup_rtti_type (const char *name
, struct block
*block
)
1470 struct symbol
* rtti_sym
;
1471 struct type
* rtti_type
;
1473 rtti_sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
);
1475 if (rtti_sym
== NULL
)
1477 warning (_("RTTI symbol not found for class '%s'"), name
);
1481 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
1483 warning (_("RTTI symbol for class '%s' is not a type"), name
);
1487 rtti_type
= SYMBOL_TYPE (rtti_sym
);
1489 switch (TYPE_CODE (rtti_type
))
1491 case TYPE_CODE_CLASS
:
1493 case TYPE_CODE_NAMESPACE
:
1494 /* chastain/2003-11-26: the symbol tables often contain fake
1495 symbols for namespaces with the same name as the struct.
1496 This warning is an indication of a bug in the lookup order
1497 or a bug in the way that the symbol tables are populated. */
1498 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
1501 warning (_("RTTI symbol for class '%s' has bad type"), name
);
1508 /* A wrapper for bfd_demangle. */
1511 gdb_demangle (const char *name
, int options
)
1513 return bfd_demangle (NULL
, name
, options
);
1516 /* Don't allow just "maintenance cplus". */
1519 maint_cplus_command (char *arg
, int from_tty
)
1521 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1522 "by the name of a command.\n"));
1523 help_list (maint_cplus_cmd_list
,
1524 "maintenance cplus ",
1528 /* This is a front end for cp_find_first_component, for unit testing.
1529 Be careful when using it: see the NOTE above
1530 cp_find_first_component. */
1533 first_component_command (char *arg
, int from_tty
)
1541 len
= cp_find_first_component (arg
);
1542 prefix
= alloca (len
+ 1);
1544 memcpy (prefix
, arg
, len
);
1547 printf_unfiltered ("%s\n", prefix
);
1550 extern initialize_file_ftype _initialize_cp_support
; /* -Wmissing-prototypes */
1553 /* Implement "info vtbl". */
1556 info_vtbl_command (char *arg
, int from_tty
)
1558 struct value
*value
;
1560 value
= parse_and_eval (arg
);
1561 cplus_print_vtable (value
);
1565 _initialize_cp_support (void)
1567 add_prefix_cmd ("cplus", class_maintenance
,
1568 maint_cplus_command
,
1569 _("C++ maintenance commands."),
1570 &maint_cplus_cmd_list
,
1571 "maintenance cplus ",
1572 0, &maintenancelist
);
1573 add_alias_cmd ("cp", "cplus",
1574 class_maintenance
, 1,
1577 add_cmd ("first_component",
1579 first_component_command
,
1580 _("Print the first class/namespace component of NAME."),
1581 &maint_cplus_cmd_list
);
1583 add_info ("vtbl", info_vtbl_command
,
1584 _("Show the virtual function table for a C++ object.\n\
1585 Usage: info vtbl EXPRESSION\n\
1586 Evaluate EXPRESSION and display the virtual function table for the\n\
1587 resulting object."));