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 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GNU CC.
9 GNU CC 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 GNU CC 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 GNU CC; 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. */
38 /* Obstack used for remembering decision points of breadth-first. */
40 static struct obstack search_obstack;
42 /* Methods for pushing and popping objects to and from obstacks. */
45 push_stack_level (obstack, tp, size)
46 struct obstack *obstack;
47 char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
50 struct stack_level *stack;
51 obstack_grow (obstack, tp, size);
52 stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
53 obstack_finish (obstack);
54 stack->obstack = obstack;
55 stack->first = (tree *) obstack_base (obstack);
56 stack->limit = obstack_room (obstack) / sizeof (tree *);
61 pop_stack_level (stack)
62 struct stack_level *stack;
64 struct stack_level *tem = stack;
65 struct obstack *obstack = tem->obstack;
67 obstack_free (obstack, tem);
71 #define search_level stack_level
72 static struct search_level *search_stack;
76 /* The class dominating the hierarchy. */
78 /* A pointer to a complete object of the indicated TYPE. */
83 static tree lookup_field_1 PARAMS ((tree, tree));
84 static int is_subobject_of_p PARAMS ((tree, tree, tree));
85 static tree dfs_check_overlap PARAMS ((tree, void *));
86 static tree dfs_no_overlap_yet PARAMS ((tree, void *));
87 static base_kind lookup_base_r
88 PARAMS ((tree, tree, base_access, int, int, int, tree *));
89 static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
90 static tree marked_pushdecls_p PARAMS ((tree, void *));
91 static tree unmarked_pushdecls_p PARAMS ((tree, void *));
92 static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
93 static tree dfs_debug_mark PARAMS ((tree, void *));
94 static tree dfs_get_vbase_types PARAMS ((tree, void *));
95 static tree dfs_push_type_decls PARAMS ((tree, void *));
96 static tree dfs_push_decls PARAMS ((tree, void *));
97 static tree dfs_unuse_fields PARAMS ((tree, void *));
98 static tree add_conversions PARAMS ((tree, void *));
99 static int covariant_return_p PARAMS ((tree, tree));
100 static int look_for_overrides_r PARAMS ((tree, tree));
101 static struct search_level *push_search_level
102 PARAMS ((struct stack_level *, struct obstack *));
103 static struct search_level *pop_search_level
104 PARAMS ((struct stack_level *));
106 PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
108 static tree lookup_field_queue_p PARAMS ((tree, void *));
109 static int shared_member_p PARAMS ((tree));
110 static tree lookup_field_r PARAMS ((tree, void *));
111 static tree canonical_binfo PARAMS ((tree));
112 static tree shared_marked_p PARAMS ((tree, void *));
113 static tree shared_unmarked_p PARAMS ((tree, void *));
114 static int dependent_base_p PARAMS ((tree));
115 static tree dfs_accessible_queue_p PARAMS ((tree, void *));
116 static tree dfs_accessible_p PARAMS ((tree, void *));
117 static tree dfs_access_in_type PARAMS ((tree, void *));
118 static access_kind access_in_type PARAMS ((tree, tree));
119 static tree dfs_canonical_queue PARAMS ((tree, void *));
120 static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
121 static void assert_canonical_unmarked PARAMS ((tree));
122 static int protected_accessible_p PARAMS ((tree, tree, tree));
123 static int friend_accessible_p PARAMS ((tree, tree, tree));
124 static void setup_class_bindings PARAMS ((tree, int));
125 static int template_self_reference_p PARAMS ((tree, tree));
126 static tree dfs_find_vbase_instance PARAMS ((tree, void *));
127 static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
128 static tree dfs_build_inheritance_graph_order PARAMS ((tree, void *));
130 /* Allocate a level of searching. */
132 static struct search_level *
133 push_search_level (stack, obstack)
134 struct stack_level *stack;
135 struct obstack *obstack;
137 struct search_level tem;
140 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
143 /* Discard a level of search allocation. */
145 static struct search_level *
146 pop_search_level (obstack)
147 struct stack_level *obstack;
149 register struct search_level *stack = pop_stack_level (obstack);
154 /* Variables for gathering statistics. */
155 #ifdef GATHER_STATISTICS
156 static int n_fields_searched;
157 static int n_calls_lookup_field, n_calls_lookup_field_1;
158 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
159 static int n_calls_get_base_type;
160 static int n_outer_fields_searched;
161 static int n_contexts_saved;
162 #endif /* GATHER_STATISTICS */
165 /* Worker for lookup_base. BINFO is the binfo we are searching at,
166 BASE is the RECORD_TYPE we are searching for. ACCESS is the
167 required access checks. WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
168 IS_VIRTUAL indicate how BINFO was reached from the start of the
169 search. WITHIN_CURRENT_SCOPE is true if we met the current scope,
170 or friend thereof (this allows us to determine whether a protected
171 base is accessible or not). IS_NON_PUBLIC indicates whether BINFO
172 is accessible and IS_VIRTUAL indicates if it is morally virtual.
174 If BINFO is of the required type, then *BINFO_PTR is examined to
175 compare with any other instance of BASE we might have already
176 discovered. *BINFO_PTR is initialized and a base_kind return value
177 indicates what kind of base was located.
179 Otherwise BINFO's bases are searched. */
182 lookup_base_r (binfo, base, access, within_current_scope,
183 is_non_public, is_virtual, binfo_ptr)
186 int within_current_scope;
187 int is_non_public; /* inside a non-public part */
188 int is_virtual; /* inside a virtual part */
193 base_kind found = bk_not_base;
195 if (access == ba_check
196 && !within_current_scope
197 && is_friend (BINFO_TYPE (binfo), current_scope ()))
199 /* Do not clear is_non_public here. If A is a private base of B, A
200 is not allowed to convert a B* to an A*. */
201 within_current_scope = 1;
204 if (same_type_p (BINFO_TYPE (binfo), base))
206 /* We have found a base. Check against what we have found
208 found = bk_same_type;
210 found = bk_via_virtual;
212 found = bk_inaccessible;
216 else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
217 BINFO_OFFSET (*binfo_ptr)))
219 if (access != ba_any)
221 else if (!is_virtual)
222 /* Prefer a non-virtual base. */
230 bases = BINFO_BASETYPES (binfo);
234 for (i = TREE_VEC_LENGTH (bases); i--;)
236 tree base_binfo = TREE_VEC_ELT (bases, i);
237 int this_non_public = is_non_public;
238 int this_virtual = is_virtual;
241 if (access <= ba_ignore)
243 else if (TREE_VIA_PUBLIC (base_binfo))
245 else if (access == ba_not_special)
247 else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
249 else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
254 if (TREE_VIA_VIRTUAL (base_binfo))
257 bk = lookup_base_r (base_binfo, base,
258 access, within_current_scope,
259 this_non_public, this_virtual,
265 if (access != ba_any)
270 case bk_inaccessible:
271 if (found == bk_not_base)
273 my_friendly_assert (found == bk_via_virtual
274 || found == bk_inaccessible, 20010723);
282 my_friendly_assert (found == bk_not_base, 20010723);
287 if (found != bk_ambig)
298 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
299 ACCESS specifies. Return the binfo we discover (which might not be
300 canonical). If KIND_PTR is non-NULL, fill with information about
301 what kind of base we discovered.
303 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
304 not set in ACCESS, then an error is issued and error_mark_node is
305 returned. If the ba_quiet bit is set, then no error is issued and
306 NULL_TREE is returned. */
309 lookup_base (t, base, access, kind_ptr)
314 tree binfo = NULL; /* The binfo we've found so far. */
318 if (t == error_mark_node || base == error_mark_node)
321 *kind_ptr = bk_not_base;
322 return error_mark_node;
324 my_friendly_assert (TYPE_P (base), 20011127);
332 t_binfo = TYPE_BINFO (t);
334 /* Ensure that the types are instantiated. */
335 t = complete_type (TYPE_MAIN_VARIANT (t));
336 base = complete_type (TYPE_MAIN_VARIANT (base));
338 bk = lookup_base_r (t_binfo, base, access & ~ba_quiet,
343 case bk_inaccessible:
345 if (!(access & ba_quiet))
347 error ("`%T' is an inaccessible base of `%T'", base, t);
348 binfo = error_mark_node;
352 if (access != ba_any)
355 if (!(access & ba_quiet))
357 error ("`%T' is an ambiguous base of `%T'", base, t);
358 binfo = error_mark_node;
371 /* Worker function for get_dynamic_cast_base_type. */
374 dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
384 if (BINFO_TYPE (binfo) == subtype)
390 *offset_ptr = BINFO_OFFSET (binfo);
395 binfos = BINFO_BASETYPES (binfo);
396 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
397 for (i = 0; i < n_baselinks; i++)
399 tree base_binfo = TREE_VEC_ELT (binfos, i);
402 if (!TREE_VIA_PUBLIC (base_binfo))
404 rval = dynamic_cast_base_recurse
405 (subtype, base_binfo,
406 via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
410 worst = worst >= 0 ? -3 : worst;
413 else if (rval == -3 && worst != -1)
419 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
420 started from is related to the required TARGET type, in order to optimize
421 the inheritance graph search. This information is independent of the
422 current context, and ignores private paths, hence get_base_distance is
423 inappropriate. Return a TREE specifying the base offset, BOFF.
424 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
425 and there are no public virtual SUBTYPE bases.
426 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
427 BOFF == -2, SUBTYPE is not a public base.
428 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
431 get_dynamic_cast_base_type (subtype, target)
435 tree offset = NULL_TREE;
436 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
441 offset = build_int_2 (boff, -1);
442 TREE_TYPE (offset) = ssizetype;
446 /* Search for a member with name NAME in a multiple inheritance lattice
447 specified by TYPE. If it does not exist, return NULL_TREE.
448 If the member is ambiguously referenced, return `error_mark_node'.
449 Otherwise, return the FIELD_DECL. */
451 /* Do a 1-level search for NAME as a member of TYPE. The caller must
452 figure out whether it can access this field. (Since it is only one
453 level, this is reasonable.) */
456 lookup_field_1 (type, name)
461 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
462 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
463 || TREE_CODE (type) == TYPENAME_TYPE)
464 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
465 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
466 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
467 the code often worked even when we treated the index as a list
469 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
473 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
474 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
476 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
477 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
484 #ifdef GATHER_STATISTICS
486 #endif /* GATHER_STATISTICS */
488 if (DECL_NAME (fields[i]) > name)
490 else if (DECL_NAME (fields[i]) < name)
494 /* We might have a nested class and a field with the
495 same name; we sorted them appropriately via
496 field_decl_cmp, so just look for the last field with
499 && DECL_NAME (fields[i+1]) == name)
507 field = TYPE_FIELDS (type);
509 #ifdef GATHER_STATISTICS
510 n_calls_lookup_field_1++;
511 #endif /* GATHER_STATISTICS */
514 #ifdef GATHER_STATISTICS
516 #endif /* GATHER_STATISTICS */
517 my_friendly_assert (DECL_P (field), 0);
518 if (DECL_NAME (field) == NULL_TREE
519 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
521 tree temp = lookup_field_1 (TREE_TYPE (field), name);
525 if (TREE_CODE (field) == USING_DECL)
526 /* For now, we're just treating member using declarations as
527 old ARM-style access declarations. Thus, there's no reason
528 to return a USING_DECL, and the rest of the compiler can't
529 handle it. Once the class is defined, these are purged
530 from TYPE_FIELDS anyhow; see handle_using_decl. */
532 else if (DECL_NAME (field) == name)
534 field = TREE_CHAIN (field);
537 if (name == vptr_identifier)
539 /* Give the user what s/he thinks s/he wants. */
540 if (TYPE_POLYMORPHIC_P (type))
541 return TYPE_VFIELD (type);
546 /* There are a number of cases we need to be aware of here:
547 current_class_type current_function_decl
554 Those last two make life interesting. If we're in a function which is
555 itself inside a class, we need decls to go into the fn's decls (our
556 second case below). But if we're in a class and the class itself is
557 inside a function, we need decls to go into the decls for the class. To
558 achieve this last goal, we must see if, when both current_class_ptr and
559 current_function_decl are set, the class was declared inside that
560 function. If so, we know to put the decls into the class's scope. */
565 if (current_function_decl == NULL_TREE)
566 return current_class_type;
567 if (current_class_type == NULL_TREE)
568 return current_function_decl;
569 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
570 && same_type_p (DECL_CONTEXT (current_function_decl),
572 || (DECL_FRIEND_CONTEXT (current_function_decl)
573 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
574 current_class_type)))
575 return current_function_decl;
577 return current_class_type;
580 /* Returns nonzero if we are currently in a function scope. Note
581 that this function returns zero if we are within a local class, but
582 not within a member function body of the local class. */
585 at_function_scope_p ()
587 tree cs = current_scope ();
588 return cs && TREE_CODE (cs) == FUNCTION_DECL;
591 /* Returns true if the innermost active scope is a class scope. */
596 tree cs = current_scope ();
597 return cs && TYPE_P (cs);
600 /* Return the scope of DECL, as appropriate when doing name-lookup. */
603 context_for_name_lookup (decl)
608 For the purposes of name lookup, after the anonymous union
609 definition, the members of the anonymous union are considered to
610 have been defined in the scope in which the anonymous union is
612 tree context = DECL_CONTEXT (decl);
614 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
615 context = TYPE_CONTEXT (context);
617 context = global_namespace;
622 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
626 canonical_binfo (binfo)
629 return (TREE_VIA_VIRTUAL (binfo)
630 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
633 /* A queue function that simply ensures that we walk into the
634 canonical versions of virtual bases. */
637 dfs_canonical_queue (binfo, data)
639 void *data ATTRIBUTE_UNUSED;
641 return canonical_binfo (binfo);
644 /* Called via dfs_walk from assert_canonical_unmarked. */
647 dfs_assert_unmarked_p (binfo, data)
649 void *data ATTRIBUTE_UNUSED;
651 my_friendly_assert (!BINFO_MARKED (binfo), 0);
655 /* Asserts that all the nodes below BINFO (using the canonical
656 versions of virtual bases) are unmarked. */
659 assert_canonical_unmarked (binfo)
662 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
665 /* If BINFO is marked, return a canonical version of BINFO.
666 Otherwise, return NULL_TREE. */
669 shared_marked_p (binfo, data)
673 binfo = canonical_binfo (binfo);
674 return markedp (binfo, data);
677 /* If BINFO is not marked, return a canonical version of BINFO.
678 Otherwise, return NULL_TREE. */
681 shared_unmarked_p (binfo, data)
685 binfo = canonical_binfo (binfo);
686 return unmarkedp (binfo, data);
689 /* The accessibility routines use BINFO_ACCESS for scratch space
690 during the computation of the accssibility of some declaration. */
692 #define BINFO_ACCESS(NODE) \
693 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
695 /* Set the access associated with NODE to ACCESS. */
697 #define SET_BINFO_ACCESS(NODE, ACCESS) \
698 ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0), \
699 (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
701 /* Called from access_in_type via dfs_walk. Calculate the access to
702 DATA (which is really a DECL) in BINFO. */
705 dfs_access_in_type (binfo, data)
709 tree decl = (tree) data;
710 tree type = BINFO_TYPE (binfo);
711 access_kind access = ak_none;
713 if (context_for_name_lookup (decl) == type)
715 /* If we have desceneded to the scope of DECL, just note the
716 appropriate access. */
717 if (TREE_PRIVATE (decl))
719 else if (TREE_PROTECTED (decl))
720 access = ak_protected;
726 /* First, check for an access-declaration that gives us more
727 access to the DECL. The CONST_DECL for an enumeration
728 constant will not have DECL_LANG_SPECIFIC, and thus no
730 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
732 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
734 access = ((access_kind)
735 TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
744 /* Otherwise, scan our baseclasses, and pick the most favorable
746 binfos = BINFO_BASETYPES (binfo);
747 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
748 for (i = 0; i < n_baselinks; ++i)
750 tree base_binfo = TREE_VEC_ELT (binfos, i);
751 access_kind base_access
752 = BINFO_ACCESS (canonical_binfo (base_binfo));
754 if (base_access == ak_none || base_access == ak_private)
755 /* If it was not accessible in the base, or only
756 accessible as a private member, we can't access it
758 base_access = ak_none;
759 else if (TREE_VIA_PROTECTED (base_binfo))
760 /* Public and protected members in the base are
762 base_access = ak_protected;
763 else if (!TREE_VIA_PUBLIC (base_binfo))
764 /* Public and protected members in the base are
766 base_access = ak_private;
768 /* See if the new access, via this base, gives more
769 access than our previous best access. */
770 if (base_access != ak_none
771 && (base_access == ak_public
772 || (base_access == ak_protected
773 && access != ak_public)
774 || (base_access == ak_private
775 && access == ak_none)))
777 access = base_access;
779 /* If the new access is public, we can't do better. */
780 if (access == ak_public)
787 /* Note the access to DECL in TYPE. */
788 SET_BINFO_ACCESS (binfo, access);
790 /* Mark TYPE as visited so that if we reach it again we do not
791 duplicate our efforts here. */
792 SET_BINFO_MARKED (binfo);
797 /* Return the access to DECL in TYPE. */
800 access_in_type (type, decl)
804 tree binfo = TYPE_BINFO (type);
806 /* We must take into account
810 If a name can be reached by several paths through a multiple
811 inheritance graph, the access is that of the path that gives
814 The algorithm we use is to make a post-order depth-first traversal
815 of the base-class hierarchy. As we come up the tree, we annotate
816 each node with the most lenient access. */
817 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
818 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
819 assert_canonical_unmarked (binfo);
821 return BINFO_ACCESS (binfo);
824 /* Called from dfs_accessible_p via dfs_walk. */
827 dfs_accessible_queue_p (binfo, data)
829 void *data ATTRIBUTE_UNUSED;
831 if (BINFO_MARKED (binfo))
834 /* If this class is inherited via private or protected inheritance,
835 then we can't see it, unless we are a friend of the subclass. */
836 if (!TREE_VIA_PUBLIC (binfo)
837 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
841 return canonical_binfo (binfo);
844 /* Called from dfs_accessible_p via dfs_walk. */
847 dfs_accessible_p (binfo, data)
851 int protected_ok = data != 0;
854 SET_BINFO_MARKED (binfo);
855 access = BINFO_ACCESS (binfo);
856 if (access == ak_public || (access == ak_protected && protected_ok))
858 else if (access != ak_none
859 && is_friend (BINFO_TYPE (binfo), current_scope ()))
865 /* Returns nonzero if it is OK to access DECL through an object
866 indiated by BINFO in the context of DERIVED. */
869 protected_accessible_p (decl, derived, binfo)
876 /* We're checking this clause from [class.access.base]
878 m as a member of N is protected, and the reference occurs in a
879 member or friend of class N, or in a member or friend of a
880 class P derived from N, where m as a member of P is private or
883 Here DERIVED is a possible P and DECL is m. accessible_p will
884 iterate over various values of N, but the access to m in DERIVED
887 Note that I believe that the passage above is wrong, and should read
888 "...is private or protected or public"; otherwise you get bizarre results
889 whereby a public using-decl can prevent you from accessing a protected
890 member of a base. (jason 2000/02/28) */
892 /* If DERIVED isn't derived from m's class, then it can't be a P. */
893 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
896 access = access_in_type (derived, decl);
898 /* If m is inaccessible in DERIVED, then it's not a P. */
899 if (access == ak_none)
904 When a friend or a member function of a derived class references
905 a protected nonstatic member of a base class, an access check
906 applies in addition to those described earlier in clause
907 _class.access_) Except when forming a pointer to member
908 (_expr.unary.op_), the access must be through a pointer to,
909 reference to, or object of the derived class itself (or any class
910 derived from that class) (_expr.ref_). If the access is to form
911 a pointer to member, the nested-name-specifier shall name the
912 derived class (or any class derived from that class). */
913 if (DECL_NONSTATIC_MEMBER_P (decl))
915 /* We can tell through what the reference is occurring by
916 chasing BINFO up to the root. */
918 while (BINFO_INHERITANCE_CHAIN (t))
919 t = BINFO_INHERITANCE_CHAIN (t);
921 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
928 /* Returns nonzero if SCOPE is a friend of a type which would be able
929 to access DECL through the object indicated by BINFO. */
932 friend_accessible_p (scope, decl, binfo)
937 tree befriending_classes;
943 if (TREE_CODE (scope) == FUNCTION_DECL
944 || DECL_FUNCTION_TEMPLATE_P (scope))
945 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
946 else if (TYPE_P (scope))
947 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
951 for (t = befriending_classes; t; t = TREE_CHAIN (t))
952 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
955 /* Nested classes are implicitly friends of their enclosing types, as
956 per core issue 45 (this is a change from the standard). */
958 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
959 if (protected_accessible_p (decl, t, binfo))
962 if (TREE_CODE (scope) == FUNCTION_DECL
963 || DECL_FUNCTION_TEMPLATE_P (scope))
965 /* Perhaps this SCOPE is a member of a class which is a
967 if (DECL_CLASS_SCOPE_P (decl)
968 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
971 /* Or an instantiation of something which is a friend. */
972 if (DECL_TEMPLATE_INFO (scope))
973 return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
975 else if (CLASSTYPE_TEMPLATE_INFO (scope))
976 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
981 /* Perform access control on TYPE_DECL or TEMPLATE_DECL VAL, which was
982 looked up in TYPE. This is fairly complex, so here's the design:
984 The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
985 start to process a top-level declaration.
986 As we process the decl-specifier-seq for the declaration, any types we
987 see that might need access control are passed to type_access_control,
988 which defers checking by adding them to type_lookups.
989 When we are done with the decl-specifier-seq, we record the lookups we've
990 seen in the lookups field of the typed_declspecs nonterminal.
991 When we process the first declarator, either in parse_decl or
992 begin_function_definition, we call save_type_access_control,
993 which stores the lookups from the decl-specifier-seq in
994 current_type_lookups.
995 As we finish with each declarator, we process everything in type_lookups
996 via decl_type_access_control, which resets type_lookups to the value of
997 current_type_lookups for subsequent declarators.
998 When we enter a function, we set type_lookups to error_mark_node, so all
999 lookups are processed immediately. */
1002 type_access_control (type, val)
1005 if (val == NULL_TREE
1006 || (TREE_CODE (val) != TEMPLATE_DECL && TREE_CODE (val) != TYPE_DECL)
1007 || ! DECL_CLASS_SCOPE_P (val))
1010 if (type_lookups == error_mark_node)
1011 enforce_access (type, val);
1012 else if (! accessible_p (type, val))
1013 type_lookups = tree_cons (type, val, type_lookups);
1016 /* DECL is a declaration from a base class of TYPE, which was the
1017 class used to name DECL. Return nonzero if, in the current
1018 context, DECL is accessible. If TYPE is actually a BINFO node,
1019 then we can tell in what context the access is occurring by looking
1020 at the most derived class along the path indicated by BINFO. */
1023 accessible_p (type, decl)
1031 /* Nonzero if it's OK to access DECL if it has protected
1032 accessibility in TYPE. */
1033 int protected_ok = 0;
1035 /* If we're not checking access, everything is accessible. */
1036 if (!flag_access_control)
1039 /* If this declaration is in a block or namespace scope, there's no
1041 if (!TYPE_P (context_for_name_lookup (decl)))
1047 type = BINFO_TYPE (type);
1050 binfo = TYPE_BINFO (type);
1052 /* [class.access.base]
1054 A member m is accessible when named in class N if
1056 --m as a member of N is public, or
1058 --m as a member of N is private, and the reference occurs in a
1059 member or friend of class N, or
1061 --m as a member of N is protected, and the reference occurs in a
1062 member or friend of class N, or in a member or friend of a
1063 class P derived from N, where m as a member of P is private or
1066 --there exists a base class B of N that is accessible at the point
1067 of reference, and m is accessible when named in class B.
1069 We walk the base class hierarchy, checking these conditions. */
1071 /* Figure out where the reference is occurring. Check to see if
1072 DECL is private or protected in this scope, since that will
1073 determine whether protected access is allowed. */
1074 if (current_class_type)
1075 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
1077 /* Now, loop through the classes of which we are a friend. */
1079 protected_ok = friend_accessible_p (current_scope (), decl, binfo);
1081 /* Standardize the binfo that access_in_type will use. We don't
1082 need to know what path was chosen from this point onwards. */
1083 binfo = TYPE_BINFO (type);
1085 /* Compute the accessibility of DECL in the class hierarchy
1086 dominated by type. */
1087 access_in_type (type, decl);
1088 /* Walk the hierarchy again, looking for a base class that allows
1090 t = dfs_walk (binfo, dfs_accessible_p,
1091 dfs_accessible_queue_p,
1092 protected_ok ? &protected_ok : 0);
1093 /* Clear any mark bits. Note that we have to walk the whole tree
1094 here, since we have aborted the previous walk from some point
1095 deep in the tree. */
1096 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1097 assert_canonical_unmarked (binfo);
1099 return t != NULL_TREE;
1102 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1103 found as a base class and sub-object of the object denoted by
1104 BINFO. MOST_DERIVED is the most derived type of the hierarchy being
1108 is_subobject_of_p (parent, binfo, most_derived)
1109 tree parent, binfo, most_derived;
1114 if (parent == binfo)
1117 binfos = BINFO_BASETYPES (binfo);
1118 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1120 /* Iterate the base types. */
1121 for (i = 0; i < n_baselinks; i++)
1123 tree base_binfo = TREE_VEC_ELT (binfos, i);
1124 if (!CLASS_TYPE_P (TREE_TYPE (base_binfo)))
1125 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1126 class there's no way to descend into it. */
1129 if (is_subobject_of_p (parent,
1130 CANONICAL_BINFO (base_binfo, most_derived),
1137 struct lookup_field_info {
1138 /* The type in which we're looking. */
1140 /* The name of the field for which we're looking. */
1142 /* If non-NULL, the current result of the lookup. */
1144 /* The path to RVAL. */
1146 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1149 /* If nonzero, we are looking for types, not data members. */
1151 /* If nonzero, RVAL was found by looking through a dependent base. */
1152 int from_dep_base_p;
1153 /* If something went wrong, a message indicating what. */
1157 /* Returns nonzero if BINFO is not hidden by the value found by the
1158 lookup so far. If BINFO is hidden, then there's no need to look in
1159 it. DATA is really a struct lookup_field_info. Called from
1160 lookup_field via breadth_first_search. */
1163 lookup_field_queue_p (binfo, data)
1167 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1169 /* Don't look for constructors or destructors in base classes. */
1170 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1173 /* If this base class is hidden by the best-known value so far, we
1174 don't need to look. */
1175 if (!lfi->from_dep_base_p && lfi->rval_binfo
1176 && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1179 return CANONICAL_BINFO (binfo, lfi->type);
1182 /* Within the scope of a template class, you can refer to the to the
1183 current specialization with the name of the template itself. For
1186 template <typename T> struct S { S* sp; }
1188 Returns nonzero if DECL is such a declaration in a class TYPE. */
1191 template_self_reference_p (type, decl)
1195 return (CLASSTYPE_USE_TEMPLATE (type)
1196 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
1197 && TREE_CODE (decl) == TYPE_DECL
1198 && DECL_ARTIFICIAL (decl)
1199 && DECL_NAME (decl) == constructor_name (type));
1203 /* Nonzero for a class member means that it is shared between all objects
1206 [class.member.lookup]:If the resulting set of declarations are not all
1207 from sub-objects of the same type, or the set has a nonstatic member
1208 and includes members from distinct sub-objects, there is an ambiguity
1209 and the program is ill-formed.
1211 This function checks that T contains no nonstatic members. */
1217 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
1218 || TREE_CODE (t) == CONST_DECL)
1220 if (is_overloaded_fn (t))
1222 for (; t; t = OVL_NEXT (t))
1224 tree fn = OVL_CURRENT (t);
1225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1233 /* DATA is really a struct lookup_field_info. Look for a field with
1234 the name indicated there in BINFO. If this function returns a
1235 non-NULL value it is the result of the lookup. Called from
1236 lookup_field via breadth_first_search. */
1239 lookup_field_r (binfo, data)
1243 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1244 tree type = BINFO_TYPE (binfo);
1245 tree nval = NULL_TREE;
1246 int from_dep_base_p;
1248 /* First, look for a function. There can't be a function and a data
1249 member with the same name, and if there's a function and a type
1250 with the same name, the type is hidden by the function. */
1251 if (!lfi->want_type)
1253 int idx = lookup_fnfields_1 (type, lfi->name);
1255 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1259 /* Look for a data member or type. */
1260 nval = lookup_field_1 (type, lfi->name);
1262 /* If there is no declaration with the indicated name in this type,
1263 then there's nothing to do. */
1267 /* If we're looking up a type (as with an elaborated type specifier)
1268 we ignore all non-types we find. */
1269 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1270 && !DECL_CLASS_TEMPLATE_P (nval))
1272 if (lfi->name == TYPE_IDENTIFIER (type))
1274 /* If the aggregate has no user defined constructors, we allow
1275 it to have fields with the same name as the enclosing type.
1276 If we are looking for that name, find the corresponding
1278 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1279 if (DECL_NAME (nval) == lfi->name
1280 && TREE_CODE (nval) == TYPE_DECL)
1287 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1289 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1295 /* You must name a template base class with a template-id. */
1296 if (!same_type_p (type, lfi->type)
1297 && template_self_reference_p (type, nval))
1300 from_dep_base_p = dependent_base_p (binfo);
1301 if (lfi->from_dep_base_p && !from_dep_base_p)
1303 /* If the new declaration is not found via a dependent base, and
1304 the old one was, then we must prefer the new one. We weren't
1305 really supposed to be able to find the old one, so we don't
1306 want to be affected by a specialization. Consider:
1308 struct B { typedef int I; };
1309 template <typename T> struct D1 : virtual public B {};
1310 template <typename T> struct D :
1311 public D1, virtual pubic B { I i; };
1313 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1314 D1 is specialized. */
1315 lfi->from_dep_base_p = 0;
1316 lfi->rval = NULL_TREE;
1317 lfi->rval_binfo = NULL_TREE;
1318 lfi->ambiguous = NULL_TREE;
1321 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1322 /* Similarly, if the old declaration was not found via a dependent
1323 base, and the new one is, ignore the new one. */
1326 /* If the lookup already found a match, and the new value doesn't
1327 hide the old one, we might have an ambiguity. */
1328 if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
1330 if (nval == lfi->rval && shared_member_p (nval))
1331 /* The two things are really the same. */
1333 else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1334 /* The previous value hides the new one. */
1338 /* We have a real ambiguity. We keep a chain of all the
1340 if (!lfi->ambiguous && lfi->rval)
1342 /* This is the first time we noticed an ambiguity. Add
1343 what we previously thought was a reasonable candidate
1345 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1346 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1349 /* Add the new value. */
1350 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1351 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1352 lfi->errstr = "request for member `%D' is ambiguous";
1357 if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
1358 /* We need to return a member template class so we can
1359 define partial specializations. Is there a better
1361 && !DECL_CLASS_TEMPLATE_P (nval))
1362 /* The thing we're looking for isn't a type, so the implicit
1363 typename extension doesn't apply, so we just pretend we
1364 didn't find anything. */
1368 lfi->from_dep_base_p = from_dep_base_p;
1369 lfi->rval_binfo = binfo;
1375 /* Return a "baselink" which BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1376 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1377 FUNCTIONS, and OPTYPE respectively. */
1380 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1384 my_friendly_assert (TREE_CODE (functions) == FUNCTION_DECL
1385 || TREE_CODE (functions) == TEMPLATE_DECL
1386 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1387 || TREE_CODE (functions) == OVERLOAD,
1389 my_friendly_assert (!optype || TYPE_P (optype), 20020730);
1390 my_friendly_assert (TREE_TYPE (functions), 20020805);
1392 baselink = build (BASELINK, TREE_TYPE (functions), NULL_TREE,
1393 NULL_TREE, NULL_TREE);
1394 BASELINK_BINFO (baselink) = binfo;
1395 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1396 BASELINK_FUNCTIONS (baselink) = functions;
1397 BASELINK_OPTYPE (baselink) = optype;
1402 /* Look for a member named NAME in an inheritance lattice dominated by
1403 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1404 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1405 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1406 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1407 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1408 TREE_VALUEs are the list of ambiguous candidates.
1410 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1412 If nothing can be found return NULL_TREE and do not issue an error. */
1415 lookup_member (xbasetype, name, protect, want_type)
1416 register tree xbasetype, name;
1417 int protect, want_type;
1419 tree rval, rval_binfo = NULL_TREE;
1420 tree type = NULL_TREE, basetype_path = NULL_TREE;
1421 struct lookup_field_info lfi;
1423 /* rval_binfo is the binfo associated with the found member, note,
1424 this can be set with useful information, even when rval is not
1425 set, because it must deal with ALL members, not just non-function
1426 members. It is used for ambiguity checking and the hidden
1427 checks. Whereas rval is only set if a proper (not hidden)
1428 non-function member is found. */
1430 const char *errstr = 0;
1432 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1433 && IDENTIFIER_CLASS_VALUE (name))
1435 tree field = IDENTIFIER_CLASS_VALUE (name);
1436 if (TREE_CODE (field) != FUNCTION_DECL
1437 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1438 /* We're in the scope of this class, and the value has already
1439 been looked up. Just return the cached value. */
1443 if (TREE_CODE (xbasetype) == TREE_VEC)
1445 type = BINFO_TYPE (xbasetype);
1446 basetype_path = xbasetype;
1448 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1451 basetype_path = TYPE_BINFO (type);
1452 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1458 complete_type (type);
1460 #ifdef GATHER_STATISTICS
1461 n_calls_lookup_field++;
1462 #endif /* GATHER_STATISTICS */
1464 memset ((PTR) &lfi, 0, sizeof (lfi));
1467 lfi.want_type = want_type;
1468 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
1470 rval_binfo = lfi.rval_binfo;
1472 type = BINFO_TYPE (rval_binfo);
1473 errstr = lfi.errstr;
1475 /* If we are not interested in ambiguities, don't report them;
1476 just return NULL_TREE. */
1477 if (!protect && lfi.ambiguous)
1483 return lfi.ambiguous;
1490 In the case of overloaded function names, access control is
1491 applied to the function selected by overloaded resolution. */
1492 if (rval && protect && !is_overloaded_fn (rval)
1493 && !enforce_access (xbasetype, rval))
1494 return error_mark_node;
1496 if (errstr && protect)
1498 error (errstr, name, type);
1500 print_candidates (lfi.ambiguous);
1501 rval = error_mark_node;
1504 /* If the thing we found was found via the implicit typename
1505 extension, build the typename type. */
1506 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1507 rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
1511 if (rval && is_overloaded_fn (rval))
1512 rval = build_baselink (rval_binfo, basetype_path, rval,
1513 (IDENTIFIER_TYPENAME_P (name)
1514 ? TREE_TYPE (name): NULL_TREE));
1518 /* Like lookup_member, except that if we find a function member we
1519 return NULL_TREE. */
1522 lookup_field (xbasetype, name, protect, want_type)
1523 register tree xbasetype, name;
1524 int protect, want_type;
1526 tree rval = lookup_member (xbasetype, name, protect, want_type);
1528 /* Ignore functions. */
1529 if (rval && BASELINK_P (rval))
1535 /* Like lookup_member, except that if we find a non-function member we
1536 return NULL_TREE. */
1539 lookup_fnfields (xbasetype, name, protect)
1540 register tree xbasetype, name;
1543 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1545 /* Ignore non-functions. */
1546 if (rval && !BASELINK_P (rval))
1552 /* TYPE is a class type. Return the index of the fields within
1553 the method vector with name NAME, or -1 is no such field exists. */
1556 lookup_fnfields_1 (type, name)
1559 tree method_vec = (CLASS_TYPE_P (type)
1560 ? CLASSTYPE_METHOD_VEC (type)
1563 if (method_vec != 0)
1566 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1567 int len = TREE_VEC_LENGTH (method_vec);
1570 #ifdef GATHER_STATISTICS
1571 n_calls_lookup_fnfields_1++;
1572 #endif /* GATHER_STATISTICS */
1574 /* Constructors are first... */
1575 if (name == ctor_identifier)
1576 return (methods[CLASSTYPE_CONSTRUCTOR_SLOT]
1577 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
1578 /* and destructors are second. */
1579 if (name == dtor_identifier)
1580 return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1581 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
1583 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1584 i < len && methods[i];
1587 #ifdef GATHER_STATISTICS
1588 n_outer_fields_searched++;
1589 #endif /* GATHER_STATISTICS */
1591 tmp = OVL_CURRENT (methods[i]);
1592 if (DECL_NAME (tmp) == name)
1595 /* If the type is complete and we're past the conversion ops,
1596 switch to binary search. */
1597 if (! DECL_CONV_FN_P (tmp)
1598 && COMPLETE_TYPE_P (type))
1600 int lo = i + 1, hi = len;
1606 #ifdef GATHER_STATISTICS
1607 n_outer_fields_searched++;
1608 #endif /* GATHER_STATISTICS */
1610 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1614 else if (tmp < name)
1623 /* If we didn't find it, it might have been a template
1624 conversion operator to a templated type. If there are any,
1625 such template conversion operators will all be overloaded on
1626 the first conversion slot. (Note that we don't look for this
1627 case above so that we will always find specializations
1629 if (IDENTIFIER_TYPENAME_P (name))
1631 i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1632 if (i < len && methods[i])
1634 tmp = OVL_CURRENT (methods[i]);
1635 if (TREE_CODE (tmp) == TEMPLATE_DECL
1636 && DECL_TEMPLATE_CONV_FN_P (tmp))
1645 /* DECL is the result of a qualified name lookup. QUALIFYING_CLASS
1646 was the class used to qualify the name. CONTEXT_CLASS is the class
1647 corresponding to the object in which DECL will be used. Return a
1648 possibly modified version of DECL that takes into account the
1651 In particular, consider an expression like `B::m' in the context of
1652 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1653 then the most derived class indicated by the BASELINK_BINFO will be
1654 `B', not `D'. This function makes that adjustment. */
1657 adjust_result_of_qualified_name_lookup (tree decl,
1658 tree qualifying_class,
1661 my_friendly_assert (CLASS_TYPE_P (qualifying_class), 20020808);
1662 my_friendly_assert (CLASS_TYPE_P (context_class), 20020808);
1664 if (BASELINK_P (decl)
1665 && DERIVED_FROM_P (qualifying_class, context_class))
1669 /* Look for the QUALIFYING_CLASS as a base of the
1670 CONTEXT_CLASS. If QUALIFYING_CLASS is ambiguous, we cannot
1671 be sure yet than an error has occurred; perhaps the function
1672 chosen by overload resolution will be static. */
1673 base = lookup_base (context_class, qualifying_class,
1674 ba_ignore | ba_quiet, NULL);
1677 BASELINK_ACCESS_BINFO (decl) = base;
1678 BASELINK_BINFO (decl)
1679 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1680 ba_ignore | ba_quiet,
1689 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1690 type in the hierarchy, in a breadth-first preorder traversal.
1691 If it ever returns a non-NULL value, that value is immediately
1692 returned and the walk is terminated. At each node, FN is passed a
1693 BINFO indicating the path from the curently visited base-class to
1694 TYPE. Before each base-class is walked QFN is called. If the
1695 value returned is nonzero, the base-class is walked; otherwise it
1696 is not. If QFN is NULL, it is treated as a function which always
1697 returns 1. Both FN and QFN are passed the DATA whenever they are
1701 bfs_walk (binfo, fn, qfn, data)
1703 tree (*fn) PARAMS ((tree, void *));
1704 tree (*qfn) PARAMS ((tree, void *));
1709 tree rval = NULL_TREE;
1710 /* An array of the base classes of BINFO. These will be built up in
1711 breadth-first order, except where QFN prunes the search. */
1712 varray_type bfs_bases;
1714 /* Start with enough room for ten base classes. That will be enough
1715 for most hierarchies. */
1716 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
1718 /* Put the first type into the stack. */
1719 VARRAY_TREE (bfs_bases, 0) = binfo;
1722 for (head = 0; head < tail; ++head)
1728 /* Pull the next type out of the queue. */
1729 binfo = VARRAY_TREE (bfs_bases, head);
1731 /* If this is the one we're looking for, we're done. */
1732 rval = (*fn) (binfo, data);
1736 /* Queue up the base types. */
1737 binfos = BINFO_BASETYPES (binfo);
1738 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1739 for (i = 0; i < n_baselinks; i++)
1741 tree base_binfo = TREE_VEC_ELT (binfos, i);
1744 base_binfo = (*qfn) (base_binfo, data);
1748 if (tail == VARRAY_SIZE (bfs_bases))
1749 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1750 VARRAY_TREE (bfs_bases, tail) = base_binfo;
1759 /* Exactly like bfs_walk, except that a depth-first traversal is
1760 performed, and PREFN is called in preorder, while POSTFN is called
1764 dfs_walk_real (binfo, prefn, postfn, qfn, data)
1766 tree (*prefn) PARAMS ((tree, void *));
1767 tree (*postfn) PARAMS ((tree, void *));
1768 tree (*qfn) PARAMS ((tree, void *));
1774 tree rval = NULL_TREE;
1776 /* Call the pre-order walking function. */
1779 rval = (*prefn) (binfo, data);
1784 /* Process the basetypes. */
1785 binfos = BINFO_BASETYPES (binfo);
1786 n_baselinks = BINFO_N_BASETYPES (binfo);
1787 for (i = 0; i < n_baselinks; i++)
1789 tree base_binfo = TREE_VEC_ELT (binfos, i);
1792 base_binfo = (*qfn) (base_binfo, data);
1796 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1802 /* Call the post-order walking function. */
1804 rval = (*postfn) (binfo, data);
1809 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1813 dfs_walk (binfo, fn, qfn, data)
1815 tree (*fn) PARAMS ((tree, void *));
1816 tree (*qfn) PARAMS ((tree, void *));
1819 return dfs_walk_real (binfo, 0, fn, qfn, data);
1822 /* Returns > 0 if a function with type DRETTYPE overriding a function
1823 with type BRETTYPE is covariant, as defined in [class.virtual].
1825 Returns 1 if trivial covariance, 2 if non-trivial (requiring runtime
1826 adjustment), or -1 if pedantically invalid covariance. */
1829 covariant_return_p (brettype, drettype)
1830 tree brettype, drettype;
1835 if (TREE_CODE (brettype) == FUNCTION_DECL)
1837 brettype = TREE_TYPE (TREE_TYPE (brettype));
1838 drettype = TREE_TYPE (TREE_TYPE (drettype));
1840 else if (TREE_CODE (brettype) == METHOD_TYPE)
1842 brettype = TREE_TYPE (brettype);
1843 drettype = TREE_TYPE (drettype);
1846 if (same_type_p (brettype, drettype))
1849 if (! (TREE_CODE (brettype) == TREE_CODE (drettype)
1850 && (TREE_CODE (brettype) == POINTER_TYPE
1851 || TREE_CODE (brettype) == REFERENCE_TYPE)
1852 && TYPE_QUALS (brettype) == TYPE_QUALS (drettype)))
1855 if (! can_convert (brettype, drettype))
1858 brettype = TREE_TYPE (brettype);
1859 drettype = TREE_TYPE (drettype);
1861 /* If not pedantic, allow any standard pointer conversion. */
1862 if (! IS_AGGR_TYPE (drettype) || ! IS_AGGR_TYPE (brettype))
1865 binfo = lookup_base (drettype, brettype, ba_check | ba_quiet, &kind);
1869 if (BINFO_OFFSET_ZEROP (binfo) && kind != bk_via_virtual)
1874 /* Check that virtual overrider OVERRIDER is acceptable for base function
1875 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1878 check_final_overrider (overrider, basefn)
1879 tree overrider, basefn;
1881 tree over_type = TREE_TYPE (overrider);
1882 tree base_type = TREE_TYPE (basefn);
1883 tree over_return = TREE_TYPE (over_type);
1884 tree base_return = TREE_TYPE (base_type);
1885 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1886 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1889 if (same_type_p (base_return, over_return))
1891 else if ((i = covariant_return_p (base_return, over_return)))
1894 sorry ("adjusting pointers for covariant returns");
1896 if (pedantic && i == -1)
1898 cp_pedwarn_at ("invalid covariant return type for `%#D'", overrider);
1899 cp_pedwarn_at (" overriding `%#D' (must be pointer or reference to class)", basefn);
1902 else if (IS_AGGR_TYPE_2 (base_return, over_return)
1903 && same_or_base_type_p (base_return, over_return))
1905 cp_error_at ("invalid covariant return type for `%#D'", overrider);
1906 cp_error_at (" overriding `%#D' (must use pointer or reference)", basefn);
1909 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)) == NULL_TREE)
1911 cp_error_at ("conflicting return type specified for `%#D'", overrider);
1912 cp_error_at (" overriding `%#D'", basefn);
1913 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1914 DECL_CONTEXT (overrider));
1918 /* Check throw specifier is at least as strict. */
1919 if (!comp_except_specs (base_throw, over_throw, 0))
1921 cp_error_at ("looser throw specifier for `%#F'", overrider);
1922 cp_error_at (" overriding `%#F'", basefn);
1928 /* Given a class TYPE, and a function decl FNDECL, look for
1929 virtual functions in TYPE's hierarchy which FNDECL overrides.
1930 We do not look in TYPE itself, only its bases.
1932 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1933 find that it overrides anything.
1935 We check that every function which is overridden, is correctly
1939 look_for_overrides (type, fndecl)
1942 tree binfo = TYPE_BINFO (type);
1943 tree basebinfos = BINFO_BASETYPES (binfo);
1944 int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
1948 for (ix = 0; ix != nbasebinfos; ix++)
1950 tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
1952 if (TYPE_POLYMORPHIC_P (basetype))
1953 found += look_for_overrides_r (basetype, fndecl);
1958 /* Look in TYPE for virtual functions with the same signature as FNDECL.
1959 This differs from get_matching_virtual in that it will only return
1960 a function from TYPE. */
1963 look_for_overrides_here (type, fndecl)
1968 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
1969 ix = CLASSTYPE_DESTRUCTOR_SLOT;
1971 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
1974 tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
1976 for (; fns; fns = OVL_NEXT (fns))
1978 tree fn = OVL_CURRENT (fns);
1980 if (!DECL_VIRTUAL_P (fn))
1981 /* Not a virtual. */;
1982 else if (DECL_CONTEXT (fn) != type)
1983 /* Introduced with a using declaration. */;
1984 else if (DECL_STATIC_FUNCTION_P (fndecl))
1986 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1987 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1988 if (compparms (TREE_CHAIN (btypes), dtypes))
1991 else if (same_signature_p (fndecl, fn))
1998 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1999 TYPE itself and its bases. */
2002 look_for_overrides_r (type, fndecl)
2005 tree fn = look_for_overrides_here (type, fndecl);
2008 if (DECL_STATIC_FUNCTION_P (fndecl))
2010 /* A static member function cannot match an inherited
2011 virtual member function. */
2012 cp_error_at ("`%#D' cannot be declared", fndecl);
2013 cp_error_at (" since `%#D' declared in base class", fn);
2017 /* It's definitely virtual, even if not explicitly set. */
2018 DECL_VIRTUAL_P (fndecl) = 1;
2019 check_final_overrider (fndecl, fn);
2024 /* We failed to find one declared in this class. Look in its bases. */
2025 return look_for_overrides (type, fndecl);
2028 /* A queue function to use with dfs_walk that only walks into
2029 canonical bases. DATA should be the type of the complete object,
2030 or a TREE_LIST whose TREE_PURPOSE is the type of the complete
2031 object. By using this function as a queue function, you will walk
2032 over exactly those BINFOs that actually exist in the complete
2033 object, including those for virtual base classes. If you
2034 SET_BINFO_MARKED for each binfo you process, you are further
2035 guaranteed that you will walk into each virtual base class exactly
2039 dfs_unmarked_real_bases_queue_p (binfo, data)
2043 if (TREE_VIA_VIRTUAL (binfo))
2045 tree type = (tree) data;
2047 if (TREE_CODE (type) == TREE_LIST)
2048 type = TREE_PURPOSE (type);
2049 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2051 return unmarkedp (binfo, NULL);
2054 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2055 that are marked, rather than unmarked. */
2058 dfs_marked_real_bases_queue_p (binfo, data)
2062 if (TREE_VIA_VIRTUAL (binfo))
2064 tree type = (tree) data;
2066 if (TREE_CODE (type) == TREE_LIST)
2067 type = TREE_PURPOSE (type);
2068 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2070 return markedp (binfo, NULL);
2073 /* A queue function that skips all virtual bases (and their
2077 dfs_skip_vbases (binfo, data)
2079 void *data ATTRIBUTE_UNUSED;
2081 if (TREE_VIA_VIRTUAL (binfo))
2087 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2090 dfs_get_pure_virtuals (binfo, data)
2094 tree type = (tree) data;
2096 /* We're not interested in primary base classes; the derived class
2097 of which they are a primary base will contain the information we
2099 if (!BINFO_PRIMARY_P (binfo))
2103 for (virtuals = BINFO_VIRTUALS (binfo);
2105 virtuals = TREE_CHAIN (virtuals))
2106 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2107 CLASSTYPE_PURE_VIRTUALS (type)
2108 = tree_cons (NULL_TREE, BV_FN (virtuals),
2109 CLASSTYPE_PURE_VIRTUALS (type));
2112 SET_BINFO_MARKED (binfo);
2117 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2120 get_pure_virtuals (type)
2125 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2126 is going to be overridden. */
2127 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
2128 /* Now, run through all the bases which are not primary bases, and
2129 collect the pure virtual functions. We look at the vtable in
2130 each class to determine what pure virtual functions are present.
2131 (A primary base is not interesting because the derived class of
2132 which it is a primary base will contain vtable entries for the
2133 pure virtuals in the base class. */
2134 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2135 dfs_unmarked_real_bases_queue_p, type);
2136 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2137 dfs_marked_real_bases_queue_p, type);
2139 /* Put the pure virtuals in dfs order. */
2140 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2142 for (vbases = CLASSTYPE_VBASECLASSES (type);
2144 vbases = TREE_CHAIN (vbases))
2148 for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
2150 virtuals = TREE_CHAIN (virtuals))
2152 tree base_fndecl = BV_FN (virtuals);
2153 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2154 error ("`%#D' needs a final overrider", base_fndecl);
2159 /* DEPTH-FIRST SEARCH ROUTINES. */
2162 markedp (binfo, data)
2164 void *data ATTRIBUTE_UNUSED;
2166 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2170 unmarkedp (binfo, data)
2172 void *data ATTRIBUTE_UNUSED;
2174 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2178 marked_vtable_pathp (binfo, data)
2180 void *data ATTRIBUTE_UNUSED;
2182 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2186 unmarked_vtable_pathp (binfo, data)
2188 void *data ATTRIBUTE_UNUSED;
2190 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2194 marked_pushdecls_p (binfo, data)
2196 void *data ATTRIBUTE_UNUSED;
2198 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2199 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2203 unmarked_pushdecls_p (binfo, data)
2205 void *data ATTRIBUTE_UNUSED;
2207 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2208 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2211 /* The worker functions for `dfs_walk'. These do not need to
2212 test anything (vis a vis marking) if they are paired with
2213 a predicate function (above). */
2216 dfs_unmark (binfo, data)
2218 void *data ATTRIBUTE_UNUSED;
2220 CLEAR_BINFO_MARKED (binfo);
2224 /* get virtual base class types.
2225 This adds type to the vbase_types list in reverse dfs order.
2226 Ordering is very important, so don't change it. */
2229 dfs_get_vbase_types (binfo, data)
2233 tree type = (tree) data;
2235 if (TREE_VIA_VIRTUAL (binfo))
2236 CLASSTYPE_VBASECLASSES (type)
2237 = tree_cons (BINFO_TYPE (binfo),
2239 CLASSTYPE_VBASECLASSES (type));
2240 SET_BINFO_MARKED (binfo);
2244 /* Called via dfs_walk from mark_primary_bases. Builds the
2245 inheritance graph order list of BINFOs. */
2248 dfs_build_inheritance_graph_order (binfo, data)
2252 tree *last_binfo = (tree *) data;
2255 TREE_CHAIN (*last_binfo) = binfo;
2256 *last_binfo = binfo;
2257 SET_BINFO_MARKED (binfo);
2261 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
2264 get_vbase_types (type)
2269 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
2270 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
2271 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2272 reverse it so that we get normal dfs ordering. */
2273 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
2274 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2275 /* Thread the BINFOs in inheritance-graph order. */
2277 dfs_walk_real (TYPE_BINFO (type),
2278 dfs_build_inheritance_graph_order,
2282 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
2285 /* Called from find_vbase_instance via dfs_walk. */
2288 dfs_find_vbase_instance (binfo, data)
2292 tree base = TREE_VALUE ((tree) data);
2294 if (BINFO_PRIMARY_P (binfo)
2295 && same_type_p (BINFO_TYPE (binfo), base))
2301 /* Find the real occurrence of the virtual BASE (a class type) in the
2302 hierarchy dominated by TYPE. */
2305 find_vbase_instance (base, type)
2311 instance = binfo_for_vbase (base, type);
2312 if (!BINFO_PRIMARY_P (instance))
2315 return dfs_walk (TYPE_BINFO (type),
2316 dfs_find_vbase_instance,
2318 build_tree_list (type, base));
2322 /* Debug info for C++ classes can get very large; try to avoid
2323 emitting it everywhere.
2325 Note that this optimization wins even when the target supports
2326 BINCL (if only slightly), and reduces the amount of work for the
2330 maybe_suppress_debug_info (t)
2333 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2334 does not support name references between translation units. It supports
2335 symbolic references between translation units, but only within a single
2336 executable or shared library.
2338 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2339 that the type was never defined, so we only get the members we
2341 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
2344 /* We might have set this earlier in cp_finish_decl. */
2345 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2347 /* If we already know how we're handling this class, handle debug info
2349 if (CLASSTYPE_INTERFACE_KNOWN (t))
2351 if (CLASSTYPE_INTERFACE_ONLY (t))
2352 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2353 /* else don't set it. */
2355 /* If the class has a vtable, write out the debug info along with
2357 else if (TYPE_CONTAINS_VPTR_P (t))
2358 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2360 /* Otherwise, just emit the debug info normally. */
2363 /* Note that we want debugging information for a base class of a class
2364 whose vtable is being emitted. Normally, this would happen because
2365 calling the constructor for a derived class implies calling the
2366 constructors for all bases, which involve initializing the
2367 appropriate vptr with the vtable for the base class; but in the
2368 presence of optimization, this initialization may be optimized
2369 away, so we tell finish_vtable_vardecl that we want the debugging
2370 information anyway. */
2373 dfs_debug_mark (binfo, data)
2375 void *data ATTRIBUTE_UNUSED;
2377 tree t = BINFO_TYPE (binfo);
2379 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2384 /* Returns BINFO if we haven't already noted that we want debugging
2385 info for this base class. */
2388 dfs_debug_unmarkedp (binfo, data)
2390 void *data ATTRIBUTE_UNUSED;
2392 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2393 ? binfo : NULL_TREE);
2396 /* Write out the debugging information for TYPE, whose vtable is being
2397 emitted. Also walk through our bases and note that we want to
2398 write out information for them. This avoids the problem of not
2399 writing any debug info for intermediate basetypes whose
2400 constructors, and thus the references to their vtables, and thus
2401 the vtables themselves, were optimized away. */
2404 note_debug_info_needed (type)
2407 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2409 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2410 rest_of_type_compilation (type, toplevel_bindings_p ());
2413 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
2416 /* Subroutines of push_class_decls (). */
2418 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2419 because it (or one of the intermediate bases) depends on template parms. */
2422 dependent_base_p (binfo)
2425 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2427 if (currently_open_class (TREE_TYPE (binfo)))
2429 if (uses_template_parms (TREE_TYPE (binfo)))
2436 setup_class_bindings (name, type_binding_p)
2440 tree type_binding = NULL_TREE;
2443 /* If we've already done the lookup for this declaration, we're
2445 if (IDENTIFIER_CLASS_VALUE (name))
2448 /* First, deal with the type binding. */
2451 type_binding = lookup_member (current_class_type, name,
2454 if (TREE_CODE (type_binding) == TREE_LIST
2455 && TREE_TYPE (type_binding) == error_mark_node)
2456 /* NAME is ambiguous. */
2457 push_class_level_binding (name, type_binding);
2459 pushdecl_class_level (type_binding);
2462 /* Now, do the value binding. */
2463 value_binding = lookup_member (current_class_type, name,
2468 && (TREE_CODE (value_binding) == TYPE_DECL
2469 || DECL_CLASS_TEMPLATE_P (value_binding)
2470 || (TREE_CODE (value_binding) == TREE_LIST
2471 && TREE_TYPE (value_binding) == error_mark_node
2472 && (TREE_CODE (TREE_VALUE (value_binding))
2474 /* We found a type-binding, even when looking for a non-type
2475 binding. This means that we already processed this binding
2477 else if (value_binding)
2479 if (TREE_CODE (value_binding) == TREE_LIST
2480 && TREE_TYPE (value_binding) == error_mark_node)
2481 /* NAME is ambiguous. */
2482 push_class_level_binding (name, value_binding);
2485 if (BASELINK_P (value_binding))
2486 /* NAME is some overloaded functions. */
2487 value_binding = BASELINK_FUNCTIONS (value_binding);
2488 pushdecl_class_level (value_binding);
2493 /* Push class-level declarations for any names appearing in BINFO that
2497 dfs_push_type_decls (binfo, data)
2499 void *data ATTRIBUTE_UNUSED;
2504 type = BINFO_TYPE (binfo);
2505 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2506 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
2507 && !(!same_type_p (type, current_class_type)
2508 && template_self_reference_p (type, fields)))
2509 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
2511 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2512 DERIVED_FROM_P, which calls get_base_distance. */
2513 SET_BINFO_PUSHDECLS_MARKED (binfo);
2518 /* Push class-level declarations for any names appearing in BINFO that
2519 are not TYPE_DECLS. */
2522 dfs_push_decls (binfo, data)
2530 type = BINFO_TYPE (binfo);
2531 dep_base_p = (processing_template_decl && type != current_class_type
2532 && dependent_base_p (binfo));
2536 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2537 if (DECL_NAME (fields)
2538 && TREE_CODE (fields) != TYPE_DECL
2539 && TREE_CODE (fields) != USING_DECL
2540 && !DECL_ARTIFICIAL (fields))
2541 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
2542 else if (TREE_CODE (fields) == FIELD_DECL
2543 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2544 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
2546 method_vec = (CLASS_TYPE_P (type)
2547 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
2553 /* Farm out constructors and destructors. */
2554 end = TREE_VEC_END (method_vec);
2556 for (methods = &TREE_VEC_ELT (method_vec, 2);
2557 *methods && methods != end;
2559 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
2560 /*type_binding_p=*/0);
2564 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
2569 /* When entering the scope of a class, we cache all of the
2570 fields that that class provides within its inheritance
2571 lattice. Where ambiguities result, we mark them
2572 with `error_mark_node' so that if they are encountered
2573 without explicit qualification, we can emit an error
2577 push_class_decls (type)
2580 search_stack = push_search_level (search_stack, &search_obstack);
2582 /* Enter type declarations and mark. */
2583 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
2585 /* Enter non-type declarations and unmark. */
2586 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
2589 /* Here's a subroutine we need because C lacks lambdas. */
2592 dfs_unuse_fields (binfo, data)
2594 void *data ATTRIBUTE_UNUSED;
2596 tree type = TREE_TYPE (binfo);
2599 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2601 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
2604 TREE_USED (fields) = 0;
2605 if (DECL_NAME (fields) == NULL_TREE
2606 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2607 unuse_fields (TREE_TYPE (fields));
2617 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
2623 /* We haven't pushed a search level when dealing with cached classes,
2624 so we'd better not try to pop it. */
2626 search_stack = pop_search_level (search_stack);
2630 print_search_statistics ()
2632 #ifdef GATHER_STATISTICS
2633 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2634 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2635 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2636 n_outer_fields_searched, n_calls_lookup_fnfields);
2637 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2638 #else /* GATHER_STATISTICS */
2639 fprintf (stderr, "no search statistics\n");
2640 #endif /* GATHER_STATISTICS */
2644 init_search_processing ()
2646 gcc_obstack_init (&search_obstack);
2650 reinit_search_statistics ()
2652 #ifdef GATHER_STATISTICS
2653 n_fields_searched = 0;
2654 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2655 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2656 n_calls_get_base_type = 0;
2657 n_outer_fields_searched = 0;
2658 n_contexts_saved = 0;
2659 #endif /* GATHER_STATISTICS */
2663 add_conversions (binfo, data)
2668 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2669 tree *conversions = (tree *) data;
2671 /* Some builtin types have no method vector, not even an empty one. */
2675 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
2677 tree tmp = TREE_VEC_ELT (method_vec, i);
2680 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
2683 name = DECL_NAME (OVL_CURRENT (tmp));
2685 /* Make sure we don't already have this conversion. */
2686 if (! IDENTIFIER_MARKED (name))
2688 *conversions = tree_cons (binfo, tmp, *conversions);
2689 IDENTIFIER_MARKED (name) = 1;
2695 /* Return a TREE_LIST containing all the non-hidden user-defined
2696 conversion functions for TYPE (and its base-classes). The
2697 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2698 containing the conversion functions. The TREE_PURPOSE is the BINFO
2699 from which the conversion functions in this node were selected. */
2702 lookup_conversions (type)
2706 tree conversions = NULL_TREE;
2708 if (COMPLETE_TYPE_P (type))
2709 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
2711 for (t = conversions; t; t = TREE_CHAIN (t))
2712 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
2723 /* Check whether the empty class indicated by EMPTY_BINFO is also present
2724 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
2727 dfs_check_overlap (empty_binfo, data)
2731 struct overlap_info *oi = (struct overlap_info *) data;
2733 for (binfo = TYPE_BINFO (oi->compare_type);
2735 binfo = BINFO_BASETYPE (binfo, 0))
2737 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
2739 oi->found_overlap = 1;
2742 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
2749 /* Trivial function to stop base traversal when we find something. */
2752 dfs_no_overlap_yet (binfo, data)
2756 struct overlap_info *oi = (struct overlap_info *) data;
2757 return !oi->found_overlap ? binfo : NULL_TREE;
2760 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2761 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
2764 types_overlap_p (empty_type, next_type)
2765 tree empty_type, next_type;
2767 struct overlap_info oi;
2769 if (! IS_AGGR_TYPE (next_type))
2771 oi.compare_type = next_type;
2772 oi.found_overlap = 0;
2773 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
2774 dfs_no_overlap_yet, &oi);
2775 return oi.found_overlap;
2778 /* Given a vtable VAR, determine which of the inherited classes the vtable
2779 inherits (in a loose sense) functions from.
2781 FIXME: This does not work with the new ABI. */
2784 binfo_for_vtable (var)
2787 tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
2788 tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
2789 int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
2792 for (i = 0; i < n_baseclasses; i++)
2794 tree base_binfo = TREE_VEC_ELT (binfos, i);
2795 if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
2799 /* If no secondary base classes matched, return the primary base, if
2801 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
2802 return get_primary_binfo (main_binfo);
2807 /* Returns the binfo of the first direct or indirect virtual base derived
2808 from BINFO, or NULL if binfo is not via virtual. */
2811 binfo_from_vbase (binfo)
2814 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2816 if (TREE_VIA_VIRTUAL (binfo))
2822 /* Returns the binfo of the first direct or indirect virtual base derived
2823 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2827 binfo_via_virtual (binfo, limit)
2831 for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
2832 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2834 if (TREE_VIA_VIRTUAL (binfo))
2840 /* Returns the BINFO (if any) for the virtual baseclass T of the class
2841 C from the CLASSTYPE_VBASECLASSES list. */
2844 binfo_for_vbase (basetype, classtype)
2850 binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
2851 return binfo ? TREE_VALUE (binfo) : NULL_TREE;