1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC 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 2, or (at your option)
14 GCC 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 GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
28 #include "coretypes.h"
39 static int is_subobject_of_p (tree, tree);
40 static tree dfs_dcast_hint_pre (tree, void *);
41 static tree dfs_dcast_hint_post (tree, void *);
42 static base_kind lookup_base_r (tree, tree, base_access, bool, tree *);
43 static tree dfs_debug_mark (tree, void *);
44 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
45 tree (*post_fn) (tree, void *), void *data);
46 static void dfs_unmark_r (tree);
47 static int check_hidden_convs (tree, int, int, tree, tree, tree);
48 static tree split_conversions (tree, tree, tree, tree);
49 static int lookup_conversions_r (tree, int, int,
50 tree, tree, tree, tree, tree *, tree *);
51 static int look_for_overrides_r (tree, tree);
52 static tree lookup_field_r (tree, void *);
53 static tree dfs_accessible_post (tree, void *);
54 static tree dfs_walk_once_accessible_r (tree, bool, bool,
55 tree (*pre_fn) (tree, void *),
56 tree (*post_fn) (tree, void *),
58 static tree dfs_walk_once_accessible (tree, bool,
59 tree (*pre_fn) (tree, void *),
60 tree (*post_fn) (tree, void *),
62 static tree dfs_access_in_type (tree, void *);
63 static access_kind access_in_type (tree, tree);
64 static int protected_accessible_p (tree, tree, tree);
65 static int friend_accessible_p (tree, tree, tree);
66 static int template_self_reference_p (tree, tree);
67 static tree dfs_get_pure_virtuals (tree, void *);
70 /* Variables for gathering statistics. */
71 #ifdef GATHER_STATISTICS
72 static int n_fields_searched;
73 static int n_calls_lookup_field, n_calls_lookup_field_1;
74 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
75 static int n_calls_get_base_type;
76 static int n_outer_fields_searched;
77 static int n_contexts_saved;
78 #endif /* GATHER_STATISTICS */
81 /* Worker for lookup_base. BINFO is the binfo we are searching at,
82 BASE is the RECORD_TYPE we are searching for. ACCESS is the
83 required access checks. IS_VIRTUAL indicates if BINFO is morally
86 If BINFO is of the required type, then *BINFO_PTR is examined to
87 compare with any other instance of BASE we might have already
88 discovered. *BINFO_PTR is initialized and a base_kind return value
89 indicates what kind of base was located.
91 Otherwise BINFO's bases are searched. */
94 lookup_base_r (tree binfo, tree base, base_access access,
95 bool is_virtual, /* inside a virtual part */
100 base_kind found = bk_not_base;
102 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
104 /* We have found a base. Check against what we have found
106 found = bk_same_type;
108 found = bk_via_virtual;
112 else if (binfo != *binfo_ptr)
114 if (access != ba_any)
116 else if (!is_virtual)
117 /* Prefer a non-virtual base. */
125 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
129 bk = lookup_base_r (base_binfo, base,
131 is_virtual || BINFO_VIRTUAL_P (base_binfo),
137 if (access != ba_any)
146 gcc_assert (found == bk_not_base);
151 if (found != bk_ambig)
165 /* Returns true if type BASE is accessible in T. (BASE is known to be
166 a (possibly non-proper) base class of T.) */
169 accessible_base_p (tree t, tree base)
173 /* [class.access.base]
175 A base class is said to be accessible if an invented public
176 member of the base class is accessible.
178 If BASE is a non-proper base, this condition is trivially
180 if (same_type_p (t, base))
182 /* Rather than inventing a public member, we use the implicit
183 public typedef created in the scope of every class. */
184 decl = TYPE_FIELDS (base);
185 while (!DECL_SELF_REFERENCE_P (decl))
186 decl = TREE_CHAIN (decl);
187 while (ANON_AGGR_TYPE_P (t))
188 t = TYPE_CONTEXT (t);
189 return accessible_p (t, decl);
192 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
193 ACCESS specifies. Return the binfo we discover. If KIND_PTR is
194 non-NULL, fill with information about what kind of base we
197 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
198 not set in ACCESS, then an error is issued and error_mark_node is
199 returned. If the ba_quiet bit is set, then no error is issued and
200 NULL_TREE is returned. */
203 lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
205 tree binfo = NULL_TREE; /* The binfo we've found so far. */
206 tree t_binfo = NULL_TREE;
209 if (t == error_mark_node || base == error_mark_node)
212 *kind_ptr = bk_not_base;
213 return error_mark_node;
215 gcc_assert (TYPE_P (base));
224 t = complete_type (TYPE_MAIN_VARIANT (t));
225 t_binfo = TYPE_BINFO (t);
228 base = complete_type (TYPE_MAIN_VARIANT (base));
231 bk = lookup_base_r (t_binfo, base, access, 0, &binfo);
235 /* Check that the base is unambiguous and accessible. */
236 if (access != ba_any)
244 if (!(access & ba_quiet))
246 error ("%qT is an ambiguous base of %qT", base, t);
247 binfo = error_mark_node;
252 if ((access & ~ba_quiet) != ba_ignore
253 /* If BASE is incomplete, then BASE and TYPE are probably
254 the same, in which case BASE is accessible. If they
255 are not the same, then TYPE is invalid. In that case,
256 there's no need to issue another error here, and
257 there's no implicit typedef to use in the code that
258 follows, so we skip the check. */
259 && COMPLETE_TYPE_P (base)
260 && !accessible_base_p (t, base))
262 if (!(access & ba_quiet))
264 error ("%qT is an inaccessible base of %qT", base, t);
265 binfo = error_mark_node;
269 bk = bk_inaccessible;
280 /* Data for dcast_base_hint walker. */
284 tree subtype; /* The base type we're looking for. */
285 int virt_depth; /* Number of virtual bases encountered from most
287 tree offset; /* Best hint offset discovered so far. */
288 bool repeated_base; /* Whether there are repeated bases in the
292 /* Worker for dcast_base_hint. Search for the base type being cast
296 dfs_dcast_hint_pre (tree binfo, void *data_)
298 struct dcast_data_s *data = data_;
300 if (BINFO_VIRTUAL_P (binfo))
303 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
305 if (data->virt_depth)
307 data->offset = ssize_int (-1);
311 data->offset = ssize_int (-3);
313 data->offset = BINFO_OFFSET (binfo);
315 return data->repeated_base ? dfs_skip_bases : data->offset;
321 /* Worker for dcast_base_hint. Track the virtual depth. */
324 dfs_dcast_hint_post (tree binfo, void *data_)
326 struct dcast_data_s *data = data_;
328 if (BINFO_VIRTUAL_P (binfo))
334 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
335 started from is related to the required TARGET type, in order to optimize
336 the inheritance graph search. This information is independent of the
337 current context, and ignores private paths, hence get_base_distance is
338 inappropriate. Return a TREE specifying the base offset, BOFF.
339 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
340 and there are no public virtual SUBTYPE bases.
341 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
342 BOFF == -2, SUBTYPE is not a public base.
343 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
346 dcast_base_hint (tree subtype, tree target)
348 struct dcast_data_s data;
350 data.subtype = subtype;
352 data.offset = NULL_TREE;
353 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
355 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
356 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
357 return data.offset ? data.offset : ssize_int (-2);
360 /* Search for a member with name NAME in a multiple inheritance
361 lattice specified by TYPE. If it does not exist, return NULL_TREE.
362 If the member is ambiguously referenced, return `error_mark_node'.
363 Otherwise, return a DECL with the indicated name. If WANT_TYPE is
364 true, type declarations are preferred. */
366 /* Do a 1-level search for NAME as a member of TYPE. The caller must
367 figure out whether it can access this field. (Since it is only one
368 level, this is reasonable.) */
371 lookup_field_1 (tree type, tree name, bool want_type)
375 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
376 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
377 || TREE_CODE (type) == TYPENAME_TYPE)
378 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
379 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
380 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
381 the code often worked even when we treated the index as a list
383 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
387 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
388 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
390 tree *fields = &DECL_SORTED_FIELDS (TYPE_NAME (type))->elts[0];
391 int lo = 0, hi = DECL_SORTED_FIELDS (TYPE_NAME (type))->len;
398 #ifdef GATHER_STATISTICS
400 #endif /* GATHER_STATISTICS */
402 if (DECL_NAME (fields[i]) > name)
404 else if (DECL_NAME (fields[i]) < name)
410 /* We might have a nested class and a field with the
411 same name; we sorted them appropriately via
412 field_decl_cmp, so just look for the first or last
413 field with this name. */
418 while (i >= lo && DECL_NAME (fields[i]) == name);
419 if (TREE_CODE (field) != TYPE_DECL
420 && !DECL_CLASS_TEMPLATE_P (field))
427 while (i < hi && DECL_NAME (fields[i]) == name);
435 field = TYPE_FIELDS (type);
437 #ifdef GATHER_STATISTICS
438 n_calls_lookup_field_1++;
439 #endif /* GATHER_STATISTICS */
440 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
442 #ifdef GATHER_STATISTICS
444 #endif /* GATHER_STATISTICS */
445 gcc_assert (DECL_P (field));
446 if (DECL_NAME (field) == NULL_TREE
447 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
449 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
453 if (TREE_CODE (field) == USING_DECL)
455 /* We generally treat class-scope using-declarations as
456 ARM-style access specifications, because support for the
457 ISO semantics has not been implemented. So, in general,
458 there's no reason to return a USING_DECL, and the rest of
459 the compiler cannot handle that. Once the class is
460 defined, USING_DECLs are purged from TYPE_FIELDS; see
461 handle_using_decl. However, we make special efforts to
462 make using-declarations in template classes work
464 if (CLASSTYPE_TEMPLATE_INFO (type)
465 && !CLASSTYPE_USE_TEMPLATE (type)
466 && !TREE_TYPE (field))
472 if (DECL_NAME (field) == name
474 || TREE_CODE (field) == TYPE_DECL
475 || DECL_CLASS_TEMPLATE_P (field)))
479 if (name == vptr_identifier)
481 /* Give the user what s/he thinks s/he wants. */
482 if (TYPE_POLYMORPHIC_P (type))
483 return TYPE_VFIELD (type);
488 /* There are a number of cases we need to be aware of here:
489 current_class_type current_function_decl
496 Those last two make life interesting. If we're in a function which is
497 itself inside a class, we need decls to go into the fn's decls (our
498 second case below). But if we're in a class and the class itself is
499 inside a function, we need decls to go into the decls for the class. To
500 achieve this last goal, we must see if, when both current_class_ptr and
501 current_function_decl are set, the class was declared inside that
502 function. If so, we know to put the decls into the class's scope. */
507 if (current_function_decl == NULL_TREE)
508 return current_class_type;
509 if (current_class_type == NULL_TREE)
510 return current_function_decl;
511 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
512 && same_type_p (DECL_CONTEXT (current_function_decl),
514 || (DECL_FRIEND_CONTEXT (current_function_decl)
515 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
516 current_class_type)))
517 return current_function_decl;
519 return current_class_type;
522 /* Returns nonzero if we are currently in a function scope. Note
523 that this function returns zero if we are within a local class, but
524 not within a member function body of the local class. */
527 at_function_scope_p (void)
529 tree cs = current_scope ();
530 return cs && TREE_CODE (cs) == FUNCTION_DECL;
533 /* Returns true if the innermost active scope is a class scope. */
536 at_class_scope_p (void)
538 tree cs = current_scope ();
539 return cs && TYPE_P (cs);
542 /* Returns true if the innermost active scope is a namespace scope. */
545 at_namespace_scope_p (void)
547 /* We are in a namespace scope if we are not it a class scope or a
549 return !current_scope();
552 /* Return the scope of DECL, as appropriate when doing name-lookup. */
555 context_for_name_lookup (tree decl)
559 For the purposes of name lookup, after the anonymous union
560 definition, the members of the anonymous union are considered to
561 have been defined in the scope in which the anonymous union is
563 tree context = DECL_CONTEXT (decl);
565 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
566 context = TYPE_CONTEXT (context);
568 context = global_namespace;
573 /* The accessibility routines use BINFO_ACCESS for scratch space
574 during the computation of the accessibility of some declaration. */
576 #define BINFO_ACCESS(NODE) \
577 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
579 /* Set the access associated with NODE to ACCESS. */
581 #define SET_BINFO_ACCESS(NODE, ACCESS) \
582 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
583 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
585 /* Called from access_in_type via dfs_walk. Calculate the access to
586 DATA (which is really a DECL) in BINFO. */
589 dfs_access_in_type (tree binfo, void *data)
591 tree decl = (tree) data;
592 tree type = BINFO_TYPE (binfo);
593 access_kind access = ak_none;
595 if (context_for_name_lookup (decl) == type)
597 /* If we have descended to the scope of DECL, just note the
598 appropriate access. */
599 if (TREE_PRIVATE (decl))
601 else if (TREE_PROTECTED (decl))
602 access = ak_protected;
608 /* First, check for an access-declaration that gives us more
609 access to the DECL. The CONST_DECL for an enumeration
610 constant will not have DECL_LANG_SPECIFIC, and thus no
612 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
614 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
618 decl_access = TREE_VALUE (decl_access);
620 if (decl_access == access_public_node)
622 else if (decl_access == access_protected_node)
623 access = ak_protected;
624 else if (decl_access == access_private_node)
635 VEC (tree) *accesses;
637 /* Otherwise, scan our baseclasses, and pick the most favorable
639 accesses = BINFO_BASE_ACCESSES (binfo);
640 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
642 tree base_access = VEC_index (tree, accesses, i);
643 access_kind base_access_now = BINFO_ACCESS (base_binfo);
645 if (base_access_now == ak_none || base_access_now == ak_private)
646 /* If it was not accessible in the base, or only
647 accessible as a private member, we can't access it
649 base_access_now = ak_none;
650 else if (base_access == access_protected_node)
651 /* Public and protected members in the base become
653 base_access_now = ak_protected;
654 else if (base_access == access_private_node)
655 /* Public and protected members in the base become
657 base_access_now = ak_private;
659 /* See if the new access, via this base, gives more
660 access than our previous best access. */
661 if (base_access_now != ak_none
662 && (access == ak_none || base_access_now < access))
664 access = base_access_now;
666 /* If the new access is public, we can't do better. */
667 if (access == ak_public)
674 /* Note the access to DECL in TYPE. */
675 SET_BINFO_ACCESS (binfo, access);
680 /* Return the access to DECL in TYPE. */
683 access_in_type (tree type, tree decl)
685 tree binfo = TYPE_BINFO (type);
687 /* We must take into account
691 If a name can be reached by several paths through a multiple
692 inheritance graph, the access is that of the path that gives
695 The algorithm we use is to make a post-order depth-first traversal
696 of the base-class hierarchy. As we come up the tree, we annotate
697 each node with the most lenient access. */
698 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
700 return BINFO_ACCESS (binfo);
703 /* Returns nonzero if it is OK to access DECL through an object
704 indicated by BINFO in the context of DERIVED. */
707 protected_accessible_p (tree decl, tree derived, tree binfo)
711 /* We're checking this clause from [class.access.base]
713 m as a member of N is protected, and the reference occurs in a
714 member or friend of class N, or in a member or friend of a
715 class P derived from N, where m as a member of P is private or
718 Here DERIVED is a possible P and DECL is m. accessible_p will
719 iterate over various values of N, but the access to m in DERIVED
722 Note that I believe that the passage above is wrong, and should read
723 "...is private or protected or public"; otherwise you get bizarre results
724 whereby a public using-decl can prevent you from accessing a protected
725 member of a base. (jason 2000/02/28) */
727 /* If DERIVED isn't derived from m's class, then it can't be a P. */
728 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
731 access = access_in_type (derived, decl);
733 /* If m is inaccessible in DERIVED, then it's not a P. */
734 if (access == ak_none)
739 When a friend or a member function of a derived class references
740 a protected nonstatic member of a base class, an access check
741 applies in addition to those described earlier in clause
742 _class.access_) Except when forming a pointer to member
743 (_expr.unary.op_), the access must be through a pointer to,
744 reference to, or object of the derived class itself (or any class
745 derived from that class) (_expr.ref_). If the access is to form
746 a pointer to member, the nested-name-specifier shall name the
747 derived class (or any class derived from that class). */
748 if (DECL_NONSTATIC_MEMBER_P (decl))
750 /* We can tell through what the reference is occurring by
751 chasing BINFO up to the root. */
753 while (BINFO_INHERITANCE_CHAIN (t))
754 t = BINFO_INHERITANCE_CHAIN (t);
756 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
763 /* Returns nonzero if SCOPE is a friend of a type which would be able
764 to access DECL through the object indicated by BINFO. */
767 friend_accessible_p (tree scope, tree decl, tree binfo)
769 tree befriending_classes;
775 if (TREE_CODE (scope) == FUNCTION_DECL
776 || DECL_FUNCTION_TEMPLATE_P (scope))
777 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
778 else if (TYPE_P (scope))
779 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
783 for (t = befriending_classes; t; t = TREE_CHAIN (t))
784 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
787 /* Nested classes are implicitly friends of their enclosing types, as
788 per core issue 45 (this is a change from the standard). */
790 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
791 if (protected_accessible_p (decl, t, binfo))
794 if (TREE_CODE (scope) == FUNCTION_DECL
795 || DECL_FUNCTION_TEMPLATE_P (scope))
797 /* Perhaps this SCOPE is a member of a class which is a
799 if (DECL_CLASS_SCOPE_P (decl)
800 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
803 /* Or an instantiation of something which is a friend. */
804 if (DECL_TEMPLATE_INFO (scope))
807 /* Increment processing_template_decl to make sure that
808 dependent_type_p works correctly. */
809 ++processing_template_decl;
810 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
811 --processing_template_decl;
815 else if (CLASSTYPE_TEMPLATE_INFO (scope))
818 /* Increment processing_template_decl to make sure that
819 dependent_type_p works correctly. */
820 ++processing_template_decl;
821 ret = friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
822 --processing_template_decl;
829 /* Called via dfs_walk_once_accessible from accessible_p */
832 dfs_accessible_post (tree binfo, void *data ATTRIBUTE_UNUSED)
834 if (BINFO_ACCESS (binfo) != ak_none
835 && is_friend (BINFO_TYPE (binfo), current_scope ()))
841 /* DECL is a declaration from a base class of TYPE, which was the
842 class used to name DECL. Return nonzero if, in the current
843 context, DECL is accessible. If TYPE is actually a BINFO node,
844 then we can tell in what context the access is occurring by looking
845 at the most derived class along the path indicated by BINFO. */
848 accessible_p (tree type, tree decl)
855 /* Nonzero if it's OK to access DECL if it has protected
856 accessibility in TYPE. */
857 int protected_ok = 0;
859 /* If this declaration is in a block or namespace scope, there's no
861 if (!TYPE_P (context_for_name_lookup (decl)))
864 /* There is no need to perform access checks inside a thunk. */
865 scope = current_scope ();
866 if (scope && DECL_THUNK_P (scope))
869 /* In a template declaration, we cannot be sure whether the
870 particular specialization that is instantiated will be a friend
871 or not. Therefore, all access checks are deferred until
873 if (processing_template_decl)
879 type = BINFO_TYPE (type);
882 binfo = TYPE_BINFO (type);
884 /* [class.access.base]
886 A member m is accessible when named in class N if
888 --m as a member of N is public, or
890 --m as a member of N is private, and the reference occurs in a
891 member or friend of class N, or
893 --m as a member of N is protected, and the reference occurs in a
894 member or friend of class N, or in a member or friend of a
895 class P derived from N, where m as a member of P is private or
898 --there exists a base class B of N that is accessible at the point
899 of reference, and m is accessible when named in class B.
901 We walk the base class hierarchy, checking these conditions. */
903 /* Figure out where the reference is occurring. Check to see if
904 DECL is private or protected in this scope, since that will
905 determine whether protected access is allowed. */
906 if (current_class_type)
907 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
909 /* Now, loop through the classes of which we are a friend. */
911 protected_ok = friend_accessible_p (scope, decl, binfo);
913 /* Standardize the binfo that access_in_type will use. We don't
914 need to know what path was chosen from this point onwards. */
915 binfo = TYPE_BINFO (type);
917 /* Compute the accessibility of DECL in the class hierarchy
918 dominated by type. */
919 access = access_in_type (type, decl);
920 if (access == ak_public
921 || (access == ak_protected && protected_ok))
925 /* Walk the hierarchy again, looking for a base class that allows
927 t = dfs_walk_once_accessible (binfo, /*friends=*/true,
928 NULL, dfs_accessible_post, NULL);
930 return t != NULL_TREE;
934 struct lookup_field_info {
935 /* The type in which we're looking. */
937 /* The name of the field for which we're looking. */
939 /* If non-NULL, the current result of the lookup. */
941 /* The path to RVAL. */
943 /* If non-NULL, the lookup was ambiguous, and this is a list of the
946 /* If nonzero, we are looking for types, not data members. */
948 /* If something went wrong, a message indicating what. */
952 /* Within the scope of a template class, you can refer to the to the
953 current specialization with the name of the template itself. For
956 template <typename T> struct S { S* sp; }
958 Returns nonzero if DECL is such a declaration in a class TYPE. */
961 template_self_reference_p (tree type, tree decl)
963 return (CLASSTYPE_USE_TEMPLATE (type)
964 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
965 && TREE_CODE (decl) == TYPE_DECL
966 && DECL_ARTIFICIAL (decl)
967 && DECL_NAME (decl) == constructor_name (type));
970 /* Nonzero for a class member means that it is shared between all objects
973 [class.member.lookup]:If the resulting set of declarations are not all
974 from sub-objects of the same type, or the set has a nonstatic member
975 and includes members from distinct sub-objects, there is an ambiguity
976 and the program is ill-formed.
978 This function checks that T contains no nonstatic members. */
981 shared_member_p (tree t)
983 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
984 || TREE_CODE (t) == CONST_DECL)
986 if (is_overloaded_fn (t))
988 for (; t; t = OVL_NEXT (t))
990 tree fn = OVL_CURRENT (t);
991 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
999 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1000 found as a base class and sub-object of the object denoted by
1004 is_subobject_of_p (tree parent, tree binfo)
1008 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1012 if (BINFO_VIRTUAL_P (probe))
1013 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
1019 /* DATA is really a struct lookup_field_info. Look for a field with
1020 the name indicated there in BINFO. If this function returns a
1021 non-NULL value it is the result of the lookup. Called from
1022 lookup_field via breadth_first_search. */
1025 lookup_field_r (tree binfo, void *data)
1027 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1028 tree type = BINFO_TYPE (binfo);
1029 tree nval = NULL_TREE;
1031 /* If this is a dependent base, don't look in it. */
1032 if (BINFO_DEPENDENT_BASE_P (binfo))
1035 /* If this base class is hidden by the best-known value so far, we
1036 don't need to look. */
1037 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
1038 && !BINFO_VIRTUAL_P (binfo))
1039 return dfs_skip_bases;
1041 /* First, look for a function. There can't be a function and a data
1042 member with the same name, and if there's a function and a type
1043 with the same name, the type is hidden by the function. */
1044 if (!lfi->want_type)
1046 int idx = lookup_fnfields_1 (type, lfi->name);
1048 nval = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
1052 /* Look for a data member or type. */
1053 nval = lookup_field_1 (type, lfi->name, lfi->want_type);
1055 /* If there is no declaration with the indicated name in this type,
1056 then there's nothing to do. */
1060 /* If we're looking up a type (as with an elaborated type specifier)
1061 we ignore all non-types we find. */
1062 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1063 && !DECL_CLASS_TEMPLATE_P (nval))
1065 if (lfi->name == TYPE_IDENTIFIER (type))
1067 /* If the aggregate has no user defined constructors, we allow
1068 it to have fields with the same name as the enclosing type.
1069 If we are looking for that name, find the corresponding
1071 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1072 if (DECL_NAME (nval) == lfi->name
1073 && TREE_CODE (nval) == TYPE_DECL)
1078 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
1080 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
1083 nval = TYPE_MAIN_DECL (e->type);
1089 /* You must name a template base class with a template-id. */
1090 if (!same_type_p (type, lfi->type)
1091 && template_self_reference_p (type, nval))
1094 /* If the lookup already found a match, and the new value doesn't
1095 hide the old one, we might have an ambiguity. */
1097 && !is_subobject_of_p (lfi->rval_binfo, binfo))
1100 if (nval == lfi->rval && shared_member_p (nval))
1101 /* The two things are really the same. */
1103 else if (is_subobject_of_p (binfo, lfi->rval_binfo))
1104 /* The previous value hides the new one. */
1108 /* We have a real ambiguity. We keep a chain of all the
1110 if (!lfi->ambiguous && lfi->rval)
1112 /* This is the first time we noticed an ambiguity. Add
1113 what we previously thought was a reasonable candidate
1115 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1116 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1119 /* Add the new value. */
1120 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1121 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1122 lfi->errstr = "request for member `%D' is ambiguous";
1128 lfi->rval_binfo = binfo;
1132 /* Don't look for constructors or destructors in base classes. */
1133 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1134 return dfs_skip_bases;
1138 /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1139 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1140 FUNCTIONS, and OPTYPE respectively. */
1143 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1147 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
1148 || TREE_CODE (functions) == TEMPLATE_DECL
1149 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1150 || TREE_CODE (functions) == OVERLOAD);
1151 gcc_assert (!optype || TYPE_P (optype));
1152 gcc_assert (TREE_TYPE (functions));
1154 baselink = make_node (BASELINK);
1155 TREE_TYPE (baselink) = TREE_TYPE (functions);
1156 BASELINK_BINFO (baselink) = binfo;
1157 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1158 BASELINK_FUNCTIONS (baselink) = functions;
1159 BASELINK_OPTYPE (baselink) = optype;
1164 /* Look for a member named NAME in an inheritance lattice dominated by
1165 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1166 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1167 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1168 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1169 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1170 TREE_VALUEs are the list of ambiguous candidates.
1172 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1174 If nothing can be found return NULL_TREE and do not issue an error. */
1177 lookup_member (tree xbasetype, tree name, int protect, bool want_type)
1179 tree rval, rval_binfo = NULL_TREE;
1180 tree type = NULL_TREE, basetype_path = NULL_TREE;
1181 struct lookup_field_info lfi;
1183 /* rval_binfo is the binfo associated with the found member, note,
1184 this can be set with useful information, even when rval is not
1185 set, because it must deal with ALL members, not just non-function
1186 members. It is used for ambiguity checking and the hidden
1187 checks. Whereas rval is only set if a proper (not hidden)
1188 non-function member is found. */
1190 const char *errstr = 0;
1192 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1194 if (TREE_CODE (xbasetype) == TREE_BINFO)
1196 type = BINFO_TYPE (xbasetype);
1197 basetype_path = xbasetype;
1201 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)));
1203 xbasetype = NULL_TREE;
1206 type = complete_type (type);
1208 basetype_path = TYPE_BINFO (type);
1213 #ifdef GATHER_STATISTICS
1214 n_calls_lookup_field++;
1215 #endif /* GATHER_STATISTICS */
1217 memset (&lfi, 0, sizeof (lfi));
1220 lfi.want_type = want_type;
1221 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
1223 rval_binfo = lfi.rval_binfo;
1225 type = BINFO_TYPE (rval_binfo);
1226 errstr = lfi.errstr;
1228 /* If we are not interested in ambiguities, don't report them;
1229 just return NULL_TREE. */
1230 if (!protect && lfi.ambiguous)
1236 return lfi.ambiguous;
1243 In the case of overloaded function names, access control is
1244 applied to the function selected by overloaded resolution. */
1245 if (rval && protect && !is_overloaded_fn (rval))
1246 perform_or_defer_access_check (basetype_path, rval);
1248 if (errstr && protect)
1250 error (errstr, name, type);
1252 print_candidates (lfi.ambiguous);
1253 rval = error_mark_node;
1256 if (rval && is_overloaded_fn (rval))
1257 rval = build_baselink (rval_binfo, basetype_path, rval,
1258 (IDENTIFIER_TYPENAME_P (name)
1259 ? TREE_TYPE (name): NULL_TREE));
1263 /* Like lookup_member, except that if we find a function member we
1264 return NULL_TREE. */
1267 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1269 tree rval = lookup_member (xbasetype, name, protect, want_type);
1271 /* Ignore functions, but propagate the ambiguity list. */
1272 if (!error_operand_p (rval)
1273 && (rval && BASELINK_P (rval)))
1279 /* Like lookup_member, except that if we find a non-function member we
1280 return NULL_TREE. */
1283 lookup_fnfields (tree xbasetype, tree name, int protect)
1285 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false);
1287 /* Ignore non-functions, but propagate the ambiguity list. */
1288 if (!error_operand_p (rval)
1289 && (rval && !BASELINK_P (rval)))
1295 /* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
1296 corresponding to "operator TYPE ()", or -1 if there is no such
1297 operator. Only CLASS_TYPE itself is searched; this routine does
1298 not scan the base classes of CLASS_TYPE. */
1301 lookup_conversion_operator (tree class_type, tree type)
1305 if (TYPE_HAS_CONVERSION (class_type))
1309 VEC(tree) *methods = CLASSTYPE_METHOD_VEC (class_type);
1311 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1312 VEC_iterate (tree, methods, i, fn); ++i)
1314 /* All the conversion operators come near the beginning of
1315 the class. Therefore, if FN is not a conversion
1316 operator, there is no matching conversion operator in
1318 fn = OVL_CURRENT (fn);
1319 if (!DECL_CONV_FN_P (fn))
1322 if (TREE_CODE (fn) == TEMPLATE_DECL)
1323 /* All the templated conversion functions are on the same
1324 slot, so remember it. */
1326 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
1334 /* TYPE is a class type. Return the index of the fields within
1335 the method vector with name NAME, or -1 is no such field exists. */
1338 lookup_fnfields_1 (tree type, tree name)
1340 VEC(tree) *method_vec;
1345 if (!CLASS_TYPE_P (type))
1348 if (COMPLETE_TYPE_P (type))
1350 if ((name == ctor_identifier
1351 || name == base_ctor_identifier
1352 || name == complete_ctor_identifier))
1354 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
1355 lazily_declare_fn (sfk_constructor, type);
1356 if (CLASSTYPE_LAZY_COPY_CTOR (type))
1357 lazily_declare_fn (sfk_copy_constructor, type);
1359 else if (name == ansi_assopname(NOP_EXPR)
1360 && CLASSTYPE_LAZY_ASSIGNMENT_OP (type))
1361 lazily_declare_fn (sfk_assignment_operator, type);
1364 method_vec = CLASSTYPE_METHOD_VEC (type);
1368 #ifdef GATHER_STATISTICS
1369 n_calls_lookup_fnfields_1++;
1370 #endif /* GATHER_STATISTICS */
1372 /* Constructors are first... */
1373 if (name == ctor_identifier)
1375 fn = CLASSTYPE_CONSTRUCTORS (type);
1376 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
1378 /* and destructors are second. */
1379 if (name == dtor_identifier)
1381 fn = CLASSTYPE_DESTRUCTORS (type);
1382 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1;
1384 if (IDENTIFIER_TYPENAME_P (name))
1385 return lookup_conversion_operator (type, TREE_TYPE (name));
1387 /* Skip the conversion operators. */
1388 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1389 VEC_iterate (tree, method_vec, i, fn);
1391 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1394 /* If the type is complete, use binary search. */
1395 if (COMPLETE_TYPE_P (type))
1401 hi = VEC_length (tree, method_vec);
1406 #ifdef GATHER_STATISTICS
1407 n_outer_fields_searched++;
1408 #endif /* GATHER_STATISTICS */
1410 tmp = VEC_index (tree, method_vec, i);
1411 tmp = DECL_NAME (OVL_CURRENT (tmp));
1414 else if (tmp < name)
1421 for (; VEC_iterate (tree, method_vec, i, fn); ++i)
1423 #ifdef GATHER_STATISTICS
1424 n_outer_fields_searched++;
1425 #endif /* GATHER_STATISTICS */
1426 if (DECL_NAME (OVL_CURRENT (fn)) == name)
1433 /* Like lookup_fnfields_1, except that the name is extracted from
1434 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */
1437 class_method_index_for_fn (tree class_type, tree function)
1439 gcc_assert (TREE_CODE (function) == FUNCTION_DECL
1440 || DECL_FUNCTION_TEMPLATE_P (function));
1442 return lookup_fnfields_1 (class_type,
1443 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
1444 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
1445 DECL_NAME (function));
1449 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1450 the class or namespace used to qualify the name. CONTEXT_CLASS is
1451 the class corresponding to the object in which DECL will be used.
1452 Return a possibly modified version of DECL that takes into account
1455 In particular, consider an expression like `B::m' in the context of
1456 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1457 then the most derived class indicated by the BASELINK_BINFO will be
1458 `B', not `D'. This function makes that adjustment. */
1461 adjust_result_of_qualified_name_lookup (tree decl,
1462 tree qualifying_scope,
1465 if (context_class && CLASS_TYPE_P (qualifying_scope)
1466 && DERIVED_FROM_P (qualifying_scope, context_class)
1467 && BASELINK_P (decl))
1471 gcc_assert (CLASS_TYPE_P (context_class));
1473 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
1474 Because we do not yet know which function will be chosen by
1475 overload resolution, we cannot yet check either accessibility
1476 or ambiguity -- in either case, the choice of a static member
1477 function might make the usage valid. */
1478 base = lookup_base (context_class, qualifying_scope,
1479 ba_ignore | ba_quiet, NULL);
1482 BASELINK_ACCESS_BINFO (decl) = base;
1483 BASELINK_BINFO (decl)
1484 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1485 ba_ignore | ba_quiet,
1494 /* Walk the class hierarchy within BINFO, in a depth-first traversal.
1495 PRE_FN is called in preorder, while POST_FN is called in postorder.
1496 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
1497 walked. If PRE_FN or POST_FN returns a different non-NULL value,
1498 that value is immediately returned and the walk is terminated. One
1499 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and
1500 POST_FN are passed the binfo to examine and the caller's DATA
1501 value. All paths are walked, thus virtual and morally virtual
1502 binfos can be multiply walked. */
1505 dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
1506 tree (*post_fn) (tree, void *), void *data)
1512 /* Call the pre-order walking function. */
1515 rval = pre_fn (binfo, data);
1518 if (rval == dfs_skip_bases)
1524 /* Find the next child binfo to walk. */
1525 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1527 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
1533 /* Call the post-order walking function. */
1535 return post_fn (binfo, data);
1539 /* Worker for dfs_walk_once. This behaves as dfs_walk_all, except
1540 that binfos are walked at most once. */
1543 dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
1544 tree (*post_fn) (tree, void *), void *data)
1550 /* Call the pre-order walking function. */
1553 rval = pre_fn (binfo, data);
1556 if (rval == dfs_skip_bases)
1563 /* Find the next child binfo to walk. */
1564 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1566 if (BINFO_VIRTUAL_P (base_binfo))
1568 if (BINFO_MARKED (base_binfo))
1570 BINFO_MARKED (base_binfo) = 1;
1573 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
1579 /* Call the post-order walking function. */
1581 return post_fn (binfo, data);
1586 /* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
1590 dfs_unmark_r (tree binfo)
1595 /* Process the basetypes. */
1596 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1598 if (BINFO_VIRTUAL_P (base_binfo))
1600 if (!BINFO_MARKED (base_binfo))
1602 BINFO_MARKED (base_binfo) = 0;
1604 /* Only walk, if it can contain more virtual bases. */
1605 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
1606 dfs_unmark_r (base_binfo);
1610 /* Like dfs_walk_all, except that binfos are not multiply walked. For
1611 non-diamond shaped hierarchies this is the same as dfs_walk_all.
1612 For diamond shaped hierarchies we must mark the virtual bases, to
1613 avoid multiple walks. */
1616 dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
1617 tree (*post_fn) (tree, void *), void *data)
1621 gcc_assert (pre_fn || post_fn);
1623 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
1624 /* We are not diamond shaped, and therefore cannot encounter the
1625 same binfo twice. */
1626 rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
1629 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
1630 if (!BINFO_INHERITANCE_CHAIN (binfo))
1632 /* We are at the top of the hierarchy, and can use the
1633 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1639 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1640 VEC_iterate (tree, vbases, ix, base_binfo); ix++)
1641 BINFO_MARKED (base_binfo) = 0;
1644 dfs_unmark_r (binfo);
1649 /* Worker function for dfs_walk_once_accessible. Behaves like
1650 dfs_walk_once_r, except (a) FRIENDS_P is true if special
1651 access given by the current context should be considered, (b) ONCE
1652 indicates whether bases should be marked during traversal. */
1655 dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
1656 tree (*pre_fn) (tree, void *),
1657 tree (*post_fn) (tree, void *), void *data)
1659 tree rval = NULL_TREE;
1663 /* Call the pre-order walking function. */
1666 rval = pre_fn (binfo, data);
1669 if (rval == dfs_skip_bases)
1676 /* Find the next child binfo to walk. */
1677 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1679 bool mark = once && BINFO_VIRTUAL_P (base_binfo);
1681 if (mark && BINFO_MARKED (base_binfo))
1684 /* If the base is inherited via private or protected
1685 inheritance, then we can't see it, unless we are a friend of
1686 the current binfo. */
1687 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node
1688 && !(friends_p && is_friend (BINFO_TYPE (binfo), current_scope ())))
1692 BINFO_MARKED (base_binfo) = 1;
1694 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
1695 pre_fn, post_fn, data);
1701 /* Call the post-order walking function. */
1703 return post_fn (binfo, data);
1708 /* Like dfs_walk_once except that only accessible bases are walked.
1709 FRIENDS_P indicates whether friendship of the local context
1710 should be considered when determining accessibility. */
1713 dfs_walk_once_accessible (tree binfo, bool friends_p,
1714 tree (*pre_fn) (tree, void *),
1715 tree (*post_fn) (tree, void *), void *data)
1717 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
1718 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
1719 pre_fn, post_fn, data);
1723 if (!BINFO_INHERITANCE_CHAIN (binfo))
1725 /* We are at the top of the hierarchy, and can use the
1726 CLASSTYPE_VBASECLASSES list for unmarking the virtual
1732 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
1733 VEC_iterate (tree, vbases, ix, base_binfo); ix++)
1734 BINFO_MARKED (base_binfo) = 0;
1737 dfs_unmark_r (binfo);
1742 /* Check that virtual overrider OVERRIDER is acceptable for base function
1743 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1746 check_final_overrider (tree overrider, tree basefn)
1748 tree over_type = TREE_TYPE (overrider);
1749 tree base_type = TREE_TYPE (basefn);
1750 tree over_return = TREE_TYPE (over_type);
1751 tree base_return = TREE_TYPE (base_type);
1752 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1753 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1756 if (DECL_INVALID_OVERRIDER_P (overrider))
1759 if (same_type_p (base_return, over_return))
1761 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1762 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1763 && POINTER_TYPE_P (base_return)))
1765 /* Potentially covariant. */
1766 unsigned base_quals, over_quals;
1768 fail = !POINTER_TYPE_P (base_return);
1771 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1773 base_return = TREE_TYPE (base_return);
1774 over_return = TREE_TYPE (over_return);
1776 base_quals = cp_type_quals (base_return);
1777 over_quals = cp_type_quals (over_return);
1779 if ((base_quals & over_quals) != over_quals)
1782 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1784 tree binfo = lookup_base (over_return, base_return,
1785 ba_check | ba_quiet, NULL);
1791 && can_convert (TREE_TYPE (base_type), TREE_TYPE (over_type)))
1792 /* GNU extension, allow trivial pointer conversions such as
1793 converting to void *, or qualification conversion. */
1795 /* can_convert will permit user defined conversion from a
1796 (reference to) class type. We must reject them. */
1797 over_return = non_reference (TREE_TYPE (over_type));
1798 if (CLASS_TYPE_P (over_return))
1812 cp_error_at ("invalid covariant return type for %q#D", overrider);
1813 cp_error_at (" overriding %q#D", basefn);
1817 cp_error_at ("conflicting return type specified for %q#D",
1819 cp_error_at (" overriding %q#D", basefn);
1821 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1825 /* Check throw specifier is at least as strict. */
1826 if (!comp_except_specs (base_throw, over_throw, 0))
1828 cp_error_at ("looser throw specifier for %q#F", overrider);
1829 cp_error_at (" overriding %q#F", basefn);
1830 DECL_INVALID_OVERRIDER_P (overrider) = 1;
1837 /* Given a class TYPE, and a function decl FNDECL, look for
1838 virtual functions in TYPE's hierarchy which FNDECL overrides.
1839 We do not look in TYPE itself, only its bases.
1841 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1842 find that it overrides anything.
1844 We check that every function which is overridden, is correctly
1848 look_for_overrides (tree type, tree fndecl)
1850 tree binfo = TYPE_BINFO (type);
1855 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1857 tree basetype = BINFO_TYPE (base_binfo);
1859 if (TYPE_POLYMORPHIC_P (basetype))
1860 found += look_for_overrides_r (basetype, fndecl);
1865 /* Look in TYPE for virtual functions with the same signature as
1869 look_for_overrides_here (tree type, tree fndecl)
1873 /* If there are no methods in TYPE (meaning that only implicitly
1874 declared methods will ever be provided for TYPE), then there are
1875 no virtual functions. */
1876 if (!CLASSTYPE_METHOD_VEC (type))
1879 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
1880 ix = CLASSTYPE_DESTRUCTOR_SLOT;
1882 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
1885 tree fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
1887 for (; fns; fns = OVL_NEXT (fns))
1889 tree fn = OVL_CURRENT (fns);
1891 if (!DECL_VIRTUAL_P (fn))
1892 /* Not a virtual. */;
1893 else if (DECL_CONTEXT (fn) != type)
1894 /* Introduced with a using declaration. */;
1895 else if (DECL_STATIC_FUNCTION_P (fndecl))
1897 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1898 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1899 if (compparms (TREE_CHAIN (btypes), dtypes))
1902 else if (same_signature_p (fndecl, fn))
1909 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1910 TYPE itself and its bases. */
1913 look_for_overrides_r (tree type, tree fndecl)
1915 tree fn = look_for_overrides_here (type, fndecl);
1918 if (DECL_STATIC_FUNCTION_P (fndecl))
1920 /* A static member function cannot match an inherited
1921 virtual member function. */
1922 cp_error_at ("%q#D cannot be declared", fndecl);
1923 cp_error_at (" since %q#D declared in base class", fn);
1927 /* It's definitely virtual, even if not explicitly set. */
1928 DECL_VIRTUAL_P (fndecl) = 1;
1929 check_final_overrider (fndecl, fn);
1934 /* We failed to find one declared in this class. Look in its bases. */
1935 return look_for_overrides (type, fndecl);
1938 /* Called via dfs_walk from dfs_get_pure_virtuals. */
1941 dfs_get_pure_virtuals (tree binfo, void *data)
1943 tree type = (tree) data;
1945 /* We're not interested in primary base classes; the derived class
1946 of which they are a primary base will contain the information we
1948 if (!BINFO_PRIMARY_P (binfo))
1952 for (virtuals = BINFO_VIRTUALS (binfo);
1954 virtuals = TREE_CHAIN (virtuals))
1955 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
1956 VEC_safe_push (tree, CLASSTYPE_PURE_VIRTUALS (type),
1963 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
1966 get_pure_virtuals (tree type)
1968 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
1969 is going to be overridden. */
1970 CLASSTYPE_PURE_VIRTUALS (type) = NULL;
1971 /* Now, run through all the bases which are not primary bases, and
1972 collect the pure virtual functions. We look at the vtable in
1973 each class to determine what pure virtual functions are present.
1974 (A primary base is not interesting because the derived class of
1975 which it is a primary base will contain vtable entries for the
1976 pure virtuals in the base class. */
1977 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
1980 /* Debug info for C++ classes can get very large; try to avoid
1981 emitting it everywhere.
1983 Note that this optimization wins even when the target supports
1984 BINCL (if only slightly), and reduces the amount of work for the
1988 maybe_suppress_debug_info (tree t)
1990 if (write_symbols == NO_DEBUG)
1993 /* We might have set this earlier in cp_finish_decl. */
1994 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
1996 /* If we already know how we're handling this class, handle debug info
1998 if (CLASSTYPE_INTERFACE_KNOWN (t))
2000 if (CLASSTYPE_INTERFACE_ONLY (t))
2001 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2002 /* else don't set it. */
2004 /* If the class has a vtable, write out the debug info along with
2006 else if (TYPE_CONTAINS_VPTR_P (t))
2007 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2009 /* Otherwise, just emit the debug info normally. */
2012 /* Note that we want debugging information for a base class of a class
2013 whose vtable is being emitted. Normally, this would happen because
2014 calling the constructor for a derived class implies calling the
2015 constructors for all bases, which involve initializing the
2016 appropriate vptr with the vtable for the base class; but in the
2017 presence of optimization, this initialization may be optimized
2018 away, so we tell finish_vtable_vardecl that we want the debugging
2019 information anyway. */
2022 dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
2024 tree t = BINFO_TYPE (binfo);
2026 if (CLASSTYPE_DEBUG_REQUESTED (t))
2027 return dfs_skip_bases;
2029 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2034 /* Write out the debugging information for TYPE, whose vtable is being
2035 emitted. Also walk through our bases and note that we want to
2036 write out information for them. This avoids the problem of not
2037 writing any debug info for intermediate basetypes whose
2038 constructors, and thus the references to their vtables, and thus
2039 the vtables themselves, were optimized away. */
2042 note_debug_info_needed (tree type)
2044 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2046 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2047 rest_of_type_compilation (type, toplevel_bindings_p ());
2050 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
2054 print_search_statistics (void)
2056 #ifdef GATHER_STATISTICS
2057 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2058 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2059 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2060 n_outer_fields_searched, n_calls_lookup_fnfields);
2061 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2062 #else /* GATHER_STATISTICS */
2063 fprintf (stderr, "no search statistics\n");
2064 #endif /* GATHER_STATISTICS */
2068 reinit_search_statistics (void)
2070 #ifdef GATHER_STATISTICS
2071 n_fields_searched = 0;
2072 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2073 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2074 n_calls_get_base_type = 0;
2075 n_outer_fields_searched = 0;
2076 n_contexts_saved = 0;
2077 #endif /* GATHER_STATISTICS */
2080 /* Helper for lookup_conversions_r. TO_TYPE is the type converted to
2081 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if
2082 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
2083 bases have been encountered already in the tree walk. PARENT_CONVS
2084 is the list of lists of conversion functions that could hide CONV
2085 and OTHER_CONVS is the list of lists of conversion functions that
2086 could hide or be hidden by CONV, should virtualness be involved in
2087 the hierarchy. Merely checking the conversion op's name is not
2088 enough because two conversion operators to the same type can have
2089 different names. Return nonzero if we are visible. */
2092 check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
2093 tree to_type, tree parent_convs, tree other_convs)
2097 /* See if we are hidden by a parent conversion. */
2098 for (level = parent_convs; level; level = TREE_CHAIN (level))
2099 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
2100 if (same_type_p (to_type, TREE_TYPE (probe)))
2103 if (virtual_depth || virtualness)
2105 /* In a virtual hierarchy, we could be hidden, or could hide a
2106 conversion function on the other_convs list. */
2107 for (level = other_convs; level; level = TREE_CHAIN (level))
2113 if (!(virtual_depth || TREE_STATIC (level)))
2114 /* Neither is morally virtual, so cannot hide each other. */
2117 if (!TREE_VALUE (level))
2118 /* They evaporated away already. */
2121 they_hide_us = (virtual_depth
2122 && original_binfo (binfo, TREE_PURPOSE (level)));
2123 we_hide_them = (!they_hide_us && TREE_STATIC (level)
2124 && original_binfo (TREE_PURPOSE (level), binfo));
2126 if (!(we_hide_them || they_hide_us))
2127 /* Neither is within the other, so no hiding can occur. */
2130 for (prev = &TREE_VALUE (level), other = *prev; other;)
2132 if (same_type_p (to_type, TREE_TYPE (other)))
2135 /* We are hidden. */
2140 /* We hide the other one. */
2141 other = TREE_CHAIN (other);
2146 prev = &TREE_CHAIN (other);
2154 /* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists
2155 of conversion functions, the first slot will be for the current
2156 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists
2157 of conversion functions from children of the current binfo,
2158 concatenated with conversions from elsewhere in the hierarchy --
2159 that list begins with OTHER_CONVS. Return a single list of lists
2160 containing only conversions from the current binfo and its
2164 split_conversions (tree my_convs, tree parent_convs,
2165 tree child_convs, tree other_convs)
2170 /* Remove the original other_convs portion from child_convs. */
2171 for (prev = NULL, t = child_convs;
2172 t != other_convs; prev = t, t = TREE_CHAIN (t))
2176 TREE_CHAIN (prev) = NULL_TREE;
2178 child_convs = NULL_TREE;
2180 /* Attach the child convs to any we had at this level. */
2183 my_convs = parent_convs;
2184 TREE_CHAIN (my_convs) = child_convs;
2187 my_convs = child_convs;
2192 /* Worker for lookup_conversions. Lookup conversion functions in
2193 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in
2194 a morally virtual base, and VIRTUALNESS is nonzero, if we've
2195 encountered virtual bases already in the tree walk. PARENT_CONVS &
2196 PARENT_TPL_CONVS are lists of list of conversions within parent
2197 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found
2198 elsewhere in the tree. Return the conversions found within this
2199 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we
2200 encountered virtualness. We keep template and non-template
2201 conversions separate, to avoid unnecessary type comparisons.
2203 The located conversion functions are held in lists of lists. The
2204 TREE_VALUE of the outer list is the list of conversion functions
2205 found in a particular binfo. The TREE_PURPOSE of both the outer
2206 and inner lists is the binfo at which those conversions were
2207 found. TREE_STATIC is set for those lists within of morally
2208 virtual binfos. The TREE_VALUE of the inner list is the conversion
2209 function or overload itself. The TREE_TYPE of each inner list node
2210 is the converted-to type. */
2213 lookup_conversions_r (tree binfo,
2214 int virtual_depth, int virtualness,
2215 tree parent_convs, tree parent_tpl_convs,
2216 tree other_convs, tree other_tpl_convs,
2217 tree *convs, tree *tpl_convs)
2219 int my_virtualness = 0;
2220 tree my_convs = NULL_TREE;
2221 tree my_tpl_convs = NULL_TREE;
2222 tree child_convs = NULL_TREE;
2223 tree child_tpl_convs = NULL_TREE;
2226 VEC(tree) *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2229 /* If we have no conversion operators, then don't look. */
2230 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
2232 *convs = *tpl_convs = NULL_TREE;
2237 if (BINFO_VIRTUAL_P (binfo))
2240 /* First, locate the unhidden ones at this level. */
2241 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2242 VEC_iterate (tree, method_vec, i, conv);
2245 tree cur = OVL_CURRENT (conv);
2247 if (!DECL_CONV_FN_P (cur))
2250 if (TREE_CODE (cur) == TEMPLATE_DECL)
2252 /* Only template conversions can be overloaded, and we must
2253 flatten them out and check each one individually. */
2256 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
2258 tree tpl = OVL_CURRENT (tpls);
2259 tree type = DECL_CONV_FN_TYPE (tpl);
2261 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2262 type, parent_tpl_convs, other_tpl_convs))
2264 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
2265 TREE_TYPE (my_tpl_convs) = type;
2268 TREE_STATIC (my_tpl_convs) = 1;
2276 tree name = DECL_NAME (cur);
2278 if (!IDENTIFIER_MARKED (name))
2280 tree type = DECL_CONV_FN_TYPE (cur);
2282 if (check_hidden_convs (binfo, virtual_depth, virtualness,
2283 type, parent_convs, other_convs))
2285 my_convs = tree_cons (binfo, conv, my_convs);
2286 TREE_TYPE (my_convs) = type;
2289 TREE_STATIC (my_convs) = 1;
2292 IDENTIFIER_MARKED (name) = 1;
2300 parent_convs = tree_cons (binfo, my_convs, parent_convs);
2302 TREE_STATIC (parent_convs) = 1;
2307 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
2309 TREE_STATIC (parent_convs) = 1;
2312 child_convs = other_convs;
2313 child_tpl_convs = other_tpl_convs;
2315 /* Now iterate over each base, looking for more conversions. */
2316 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2318 tree base_convs, base_tpl_convs;
2319 unsigned base_virtualness;
2321 base_virtualness = lookup_conversions_r (base_binfo,
2322 virtual_depth, virtualness,
2323 parent_convs, parent_tpl_convs,
2324 child_convs, child_tpl_convs,
2325 &base_convs, &base_tpl_convs);
2326 if (base_virtualness)
2327 my_virtualness = virtualness = 1;
2328 child_convs = chainon (base_convs, child_convs);
2329 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
2332 /* Unmark the conversions found at this level */
2333 for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
2334 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
2336 *convs = split_conversions (my_convs, parent_convs,
2337 child_convs, other_convs);
2338 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
2339 child_tpl_convs, other_tpl_convs);
2341 return my_virtualness;
2344 /* Return a TREE_LIST containing all the non-hidden user-defined
2345 conversion functions for TYPE (and its base-classes). The
2346 TREE_VALUE of each node is the FUNCTION_DECL of the conversion
2347 function. The TREE_PURPOSE is the BINFO from which the conversion
2348 functions in this node were selected. This function is effectively
2349 performing a set of member lookups as lookup_fnfield does, but
2350 using the type being converted to as the unique key, rather than the
2354 lookup_conversions (tree type)
2356 tree convs, tpl_convs;
2357 tree list = NULL_TREE;
2359 complete_type (type);
2360 if (!TYPE_BINFO (type))
2363 lookup_conversions_r (TYPE_BINFO (type), 0, 0,
2364 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
2365 &convs, &tpl_convs);
2367 /* Flatten the list-of-lists */
2368 for (; convs; convs = TREE_CHAIN (convs))
2372 for (probe = TREE_VALUE (convs); probe; probe = next)
2374 next = TREE_CHAIN (probe);
2376 TREE_CHAIN (probe) = list;
2381 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
2385 for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
2387 next = TREE_CHAIN (probe);
2389 TREE_CHAIN (probe) = list;
2397 /* Returns the binfo of the first direct or indirect virtual base derived
2398 from BINFO, or NULL if binfo is not via virtual. */
2401 binfo_from_vbase (tree binfo)
2403 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2405 if (BINFO_VIRTUAL_P (binfo))
2411 /* Returns the binfo of the first direct or indirect virtual base derived
2412 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2416 binfo_via_virtual (tree binfo, tree limit)
2418 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
2419 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2421 if (BINFO_VIRTUAL_P (binfo))
2427 /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
2428 Find the equivalent binfo within whatever graph HERE is located.
2429 This is the inverse of original_binfo. */
2432 copied_binfo (tree binfo, tree here)
2434 tree result = NULL_TREE;
2436 if (BINFO_VIRTUAL_P (binfo))
2440 for (t = here; BINFO_INHERITANCE_CHAIN (t);
2441 t = BINFO_INHERITANCE_CHAIN (t))
2444 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
2446 else if (BINFO_INHERITANCE_CHAIN (binfo))
2452 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2453 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
2454 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
2456 result = base_binfo;
2462 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
2466 gcc_assert (result);
2471 binfo_for_vbase (tree base, tree t)
2477 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
2478 VEC_iterate (tree, vbases, ix, binfo); ix++)
2479 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
2484 /* BINFO is some base binfo of HERE, within some other
2485 hierarchy. Return the equivalent binfo, but in the hierarchy
2486 dominated by HERE. This is the inverse of copied_binfo. If BINFO
2487 is not a base binfo of HERE, returns NULL_TREE. */
2490 original_binfo (tree binfo, tree here)
2494 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
2496 else if (BINFO_VIRTUAL_P (binfo))
2497 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
2498 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
2500 else if (BINFO_INHERITANCE_CHAIN (binfo))
2504 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
2510 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
2511 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2512 BINFO_TYPE (binfo)))
2514 result = base_binfo;