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 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 /* Obstack used for remembering decision points of breadth-first. */
41 static struct obstack search_obstack;
43 /* Methods for pushing and popping objects to and from obstacks. */
46 push_stack_level (obstack, tp, size)
47 struct obstack *obstack;
48 char *tp; /* Sony NewsOS 5.0 compiler doesn't like void * here. */
51 struct stack_level *stack;
52 obstack_grow (obstack, tp, size);
53 stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
54 obstack_finish (obstack);
55 stack->obstack = obstack;
56 stack->first = (tree *) obstack_base (obstack);
57 stack->limit = obstack_room (obstack) / sizeof (tree *);
62 pop_stack_level (stack)
63 struct stack_level *stack;
65 struct stack_level *tem = stack;
66 struct obstack *obstack = tem->obstack;
68 obstack_free (obstack, tem);
72 #define search_level stack_level
73 static struct search_level *search_stack;
77 /* The class dominating the hierarchy. */
79 /* A pointer to a complete object of the indicated TYPE. */
84 static tree lookup_field_1 PARAMS ((tree, tree));
85 static int is_subobject_of_p PARAMS ((tree, tree, tree));
86 static int is_subobject_of_p_1 PARAMS ((tree, tree, tree));
87 static tree dfs_check_overlap PARAMS ((tree, void *));
88 static tree dfs_no_overlap_yet PARAMS ((tree, void *));
89 static base_kind lookup_base_r
90 PARAMS ((tree, tree, base_access, int, int, int, tree *));
91 static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
92 static tree marked_pushdecls_p PARAMS ((tree, void *));
93 static tree unmarked_pushdecls_p PARAMS ((tree, void *));
94 static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
95 static tree dfs_debug_mark PARAMS ((tree, void *));
96 static tree dfs_get_vbase_types PARAMS ((tree, void *));
97 static tree dfs_push_type_decls PARAMS ((tree, void *));
98 static tree dfs_push_decls PARAMS ((tree, void *));
99 static tree dfs_unuse_fields PARAMS ((tree, void *));
100 static tree add_conversions PARAMS ((tree, void *));
101 static int look_for_overrides_r PARAMS ((tree, tree));
102 static struct search_level *push_search_level
103 PARAMS ((struct stack_level *, struct obstack *));
104 static struct search_level *pop_search_level
105 PARAMS ((struct stack_level *));
107 PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
109 static tree lookup_field_queue_p PARAMS ((tree, void *));
110 static int shared_member_p PARAMS ((tree));
111 static tree lookup_field_r PARAMS ((tree, void *));
112 static tree canonical_binfo PARAMS ((tree));
113 static tree shared_marked_p PARAMS ((tree, void *));
114 static tree shared_unmarked_p PARAMS ((tree, void *));
115 static int dependent_base_p PARAMS ((tree));
116 static tree dfs_accessible_queue_p PARAMS ((tree, void *));
117 static tree dfs_accessible_p PARAMS ((tree, void *));
118 static tree dfs_access_in_type PARAMS ((tree, void *));
119 static access_kind access_in_type PARAMS ((tree, tree));
120 static tree dfs_canonical_queue PARAMS ((tree, void *));
121 static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
122 static void assert_canonical_unmarked PARAMS ((tree));
123 static int protected_accessible_p PARAMS ((tree, tree, tree));
124 static int friend_accessible_p PARAMS ((tree, tree, tree));
125 static void setup_class_bindings PARAMS ((tree, int));
126 static int template_self_reference_p PARAMS ((tree, tree));
127 static tree dfs_find_vbase_instance PARAMS ((tree, void *));
128 static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
129 static tree dfs_build_inheritance_graph_order PARAMS ((tree, void *));
131 /* Allocate a level of searching. */
133 static struct search_level *
134 push_search_level (stack, obstack)
135 struct stack_level *stack;
136 struct obstack *obstack;
138 struct search_level tem;
141 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
144 /* Discard a level of search allocation. */
146 static struct search_level *
147 pop_search_level (obstack)
148 struct stack_level *obstack;
150 register struct search_level *stack = pop_stack_level (obstack);
155 /* Variables for gathering statistics. */
156 #ifdef GATHER_STATISTICS
157 static int n_fields_searched;
158 static int n_calls_lookup_field, n_calls_lookup_field_1;
159 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
160 static int n_calls_get_base_type;
161 static int n_outer_fields_searched;
162 static int n_contexts_saved;
163 #endif /* GATHER_STATISTICS */
166 /* Worker for lookup_base. BINFO is the binfo we are searching at,
167 BASE is the RECORD_TYPE we are searching for. ACCESS is the
168 required access checks. WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
169 IS_VIRTUAL indicate how BINFO was reached from the start of the
170 search. WITHIN_CURRENT_SCOPE is true if we met the current scope,
171 or friend thereof (this allows us to determine whether a protected
172 base is accessible or not). IS_NON_PUBLIC indicates whether BINFO
173 is accessible and IS_VIRTUAL indicates if it is morally virtual.
175 If BINFO is of the required type, then *BINFO_PTR is examined to
176 compare with any other instance of BASE we might have already
177 discovered. *BINFO_PTR is initialized and a base_kind return value
178 indicates what kind of base was located.
180 Otherwise BINFO's bases are searched. */
183 lookup_base_r (binfo, base, access, within_current_scope,
184 is_non_public, is_virtual, binfo_ptr)
187 int within_current_scope;
188 int is_non_public; /* inside a non-public part */
189 int is_virtual; /* inside a virtual part */
194 base_kind found = bk_not_base;
196 if (access == ba_check
197 && !within_current_scope
198 && is_friend (BINFO_TYPE (binfo), current_scope ()))
200 /* Do not clear is_non_public here. If A is a private base of B, A
201 is not allowed to convert a B* to an A*. */
202 within_current_scope = 1;
205 if (same_type_p (BINFO_TYPE (binfo), base))
207 /* We have found a base. Check against what we have found
209 found = bk_same_type;
211 found = bk_via_virtual;
213 found = bk_inaccessible;
217 else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
218 BINFO_OFFSET (*binfo_ptr)))
220 if (access != ba_any)
222 else if (!is_virtual)
223 /* Prefer a non-virtual base. */
231 bases = BINFO_BASETYPES (binfo);
235 for (i = TREE_VEC_LENGTH (bases); i--;)
237 tree base_binfo = TREE_VEC_ELT (bases, i);
238 int this_non_public = is_non_public;
239 int this_virtual = is_virtual;
242 if (access <= ba_ignore)
244 else if (TREE_VIA_PUBLIC (base_binfo))
246 else if (access == ba_not_special)
248 else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
250 else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
255 if (TREE_VIA_VIRTUAL (base_binfo))
258 bk = lookup_base_r (base_binfo, base,
259 access, within_current_scope,
260 this_non_public, this_virtual,
266 if (access != ba_any)
271 case bk_inaccessible:
272 if (found == bk_not_base)
274 my_friendly_assert (found == bk_via_virtual
275 || found == bk_inaccessible, 20010723);
283 my_friendly_assert (found == bk_not_base, 20010723);
288 if (found != bk_ambig)
299 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
300 ACCESS specifies. Return the binfo we discover (which might not be
301 canonical). If KIND_PTR is non-NULL, fill with information about
302 what kind of base we discovered.
304 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
305 not set in ACCESS, then an error is issued and error_mark_node is
306 returned. If the ba_quiet bit is set, then no error is issued and
307 NULL_TREE is returned. */
310 lookup_base (t, base, access, kind_ptr)
315 tree binfo = NULL; /* The binfo we've found so far. */
319 if (t == error_mark_node || base == error_mark_node)
322 *kind_ptr = bk_not_base;
323 return error_mark_node;
325 my_friendly_assert (TYPE_P (base), 20011127);
333 t_binfo = TYPE_BINFO (t);
335 /* Ensure that the types are instantiated. */
336 t = complete_type (TYPE_MAIN_VARIANT (t));
337 base = complete_type (TYPE_MAIN_VARIANT (base));
339 bk = lookup_base_r (t_binfo, base, access & ~ba_quiet,
344 case bk_inaccessible:
346 if (!(access & ba_quiet))
348 error ("`%T' is an inaccessible base of `%T'", base, t);
349 binfo = error_mark_node;
353 if (access != ba_any)
356 if (!(access & ba_quiet))
358 error ("`%T' is an ambiguous base of `%T'", base, t);
359 binfo = error_mark_node;
372 /* Worker function for get_dynamic_cast_base_type. */
375 dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
385 if (BINFO_TYPE (binfo) == subtype)
391 *offset_ptr = BINFO_OFFSET (binfo);
396 binfos = BINFO_BASETYPES (binfo);
397 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
398 for (i = 0; i < n_baselinks; i++)
400 tree base_binfo = TREE_VEC_ELT (binfos, i);
403 if (!TREE_VIA_PUBLIC (base_binfo))
405 rval = dynamic_cast_base_recurse
406 (subtype, base_binfo,
407 via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
411 worst = worst >= 0 ? -3 : worst;
414 else if (rval == -3 && worst != -1)
420 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
421 started from is related to the required TARGET type, in order to optimize
422 the inheritance graph search. This information is independent of the
423 current context, and ignores private paths, hence get_base_distance is
424 inappropriate. Return a TREE specifying the base offset, BOFF.
425 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
426 and there are no public virtual SUBTYPE bases.
427 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
428 BOFF == -2, SUBTYPE is not a public base.
429 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
432 get_dynamic_cast_base_type (subtype, target)
436 tree offset = NULL_TREE;
437 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
442 offset = build_int_2 (boff, -1);
443 TREE_TYPE (offset) = ssizetype;
447 /* Search for a member with name NAME in a multiple inheritance lattice
448 specified by TYPE. If it does not exist, return NULL_TREE.
449 If the member is ambiguously referenced, return `error_mark_node'.
450 Otherwise, return the FIELD_DECL. */
452 /* Do a 1-level search for NAME as a member of TYPE. The caller must
453 figure out whether it can access this field. (Since it is only one
454 level, this is reasonable.) */
457 lookup_field_1 (type, name)
462 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
463 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
464 || TREE_CODE (type) == TYPENAME_TYPE)
465 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
466 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
467 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
468 the code often worked even when we treated the index as a list
470 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
474 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
475 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
477 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
478 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
485 #ifdef GATHER_STATISTICS
487 #endif /* GATHER_STATISTICS */
489 if (DECL_NAME (fields[i]) > name)
491 else if (DECL_NAME (fields[i]) < name)
495 /* We might have a nested class and a field with the
496 same name; we sorted them appropriately via
497 field_decl_cmp, so just look for the last field with
500 && DECL_NAME (fields[i+1]) == name)
508 field = TYPE_FIELDS (type);
510 #ifdef GATHER_STATISTICS
511 n_calls_lookup_field_1++;
512 #endif /* GATHER_STATISTICS */
515 #ifdef GATHER_STATISTICS
517 #endif /* GATHER_STATISTICS */
518 my_friendly_assert (DECL_P (field), 0);
519 if (DECL_NAME (field) == NULL_TREE
520 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
522 tree temp = lookup_field_1 (TREE_TYPE (field), name);
526 if (TREE_CODE (field) == USING_DECL)
527 /* For now, we're just treating member using declarations as
528 old ARM-style access declarations. Thus, there's no reason
529 to return a USING_DECL, and the rest of the compiler can't
530 handle it. Once the class is defined, these are purged
531 from TYPE_FIELDS anyhow; see handle_using_decl. */
533 else if (DECL_NAME (field) == name)
535 field = TREE_CHAIN (field);
538 if (name == vptr_identifier)
540 /* Give the user what s/he thinks s/he wants. */
541 if (TYPE_POLYMORPHIC_P (type))
542 return TYPE_VFIELD (type);
547 /* There are a number of cases we need to be aware of here:
548 current_class_type current_function_decl
555 Those last two make life interesting. If we're in a function which is
556 itself inside a class, we need decls to go into the fn's decls (our
557 second case below). But if we're in a class and the class itself is
558 inside a function, we need decls to go into the decls for the class. To
559 achieve this last goal, we must see if, when both current_class_ptr and
560 current_function_decl are set, the class was declared inside that
561 function. If so, we know to put the decls into the class's scope. */
566 if (current_function_decl == NULL_TREE)
567 return current_class_type;
568 if (current_class_type == NULL_TREE)
569 return current_function_decl;
570 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
571 && same_type_p (DECL_CONTEXT (current_function_decl),
573 || (DECL_FRIEND_CONTEXT (current_function_decl)
574 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
575 current_class_type)))
576 return current_function_decl;
578 return current_class_type;
581 /* Returns nonzero if we are currently in a function scope. Note
582 that this function returns zero if we are within a local class, but
583 not within a member function body of the local class. */
586 at_function_scope_p ()
588 tree cs = current_scope ();
589 return cs && TREE_CODE (cs) == FUNCTION_DECL;
592 /* Returns true if the innermost active scope is a class scope. */
597 tree cs = current_scope ();
598 return cs && TYPE_P (cs);
601 /* Return the scope of DECL, as appropriate when doing name-lookup. */
604 context_for_name_lookup (decl)
609 For the purposes of name lookup, after the anonymous union
610 definition, the members of the anonymous union are considered to
611 have been defined in the scope in which the anonymous union is
613 tree context = DECL_CONTEXT (decl);
615 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
616 context = TYPE_CONTEXT (context);
618 context = global_namespace;
623 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
627 canonical_binfo (binfo)
630 return (TREE_VIA_VIRTUAL (binfo)
631 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
634 /* A queue function that simply ensures that we walk into the
635 canonical versions of virtual bases. */
638 dfs_canonical_queue (binfo, data)
640 void *data ATTRIBUTE_UNUSED;
642 return canonical_binfo (binfo);
645 /* Called via dfs_walk from assert_canonical_unmarked. */
648 dfs_assert_unmarked_p (binfo, data)
650 void *data ATTRIBUTE_UNUSED;
652 my_friendly_assert (!BINFO_MARKED (binfo), 0);
656 /* Asserts that all the nodes below BINFO (using the canonical
657 versions of virtual bases) are unmarked. */
660 assert_canonical_unmarked (binfo)
663 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
666 /* If BINFO is marked, return a canonical version of BINFO.
667 Otherwise, return NULL_TREE. */
670 shared_marked_p (binfo, data)
674 binfo = canonical_binfo (binfo);
675 return markedp (binfo, data);
678 /* If BINFO is not marked, return a canonical version of BINFO.
679 Otherwise, return NULL_TREE. */
682 shared_unmarked_p (binfo, data)
686 binfo = canonical_binfo (binfo);
687 return unmarkedp (binfo, data);
690 /* The accessibility routines use BINFO_ACCESS for scratch space
691 during the computation of the accssibility of some declaration. */
693 #define BINFO_ACCESS(NODE) \
694 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
696 /* Set the access associated with NODE to ACCESS. */
698 #define SET_BINFO_ACCESS(NODE, ACCESS) \
699 ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0), \
700 (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
702 /* Called from access_in_type via dfs_walk. Calculate the access to
703 DATA (which is really a DECL) in BINFO. */
706 dfs_access_in_type (binfo, data)
710 tree decl = (tree) data;
711 tree type = BINFO_TYPE (binfo);
712 access_kind access = ak_none;
714 if (context_for_name_lookup (decl) == type)
716 /* If we have desceneded to the scope of DECL, just note the
717 appropriate access. */
718 if (TREE_PRIVATE (decl))
720 else if (TREE_PROTECTED (decl))
721 access = ak_protected;
727 /* First, check for an access-declaration that gives us more
728 access to the DECL. The CONST_DECL for an enumeration
729 constant will not have DECL_LANG_SPECIFIC, and thus no
731 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
733 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
735 access = ((access_kind)
736 TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
745 /* Otherwise, scan our baseclasses, and pick the most favorable
747 binfos = BINFO_BASETYPES (binfo);
748 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
749 for (i = 0; i < n_baselinks; ++i)
751 tree base_binfo = TREE_VEC_ELT (binfos, i);
752 access_kind base_access
753 = BINFO_ACCESS (canonical_binfo (base_binfo));
755 if (base_access == ak_none || base_access == ak_private)
756 /* If it was not accessible in the base, or only
757 accessible as a private member, we can't access it
759 base_access = ak_none;
760 else if (TREE_VIA_PROTECTED (base_binfo))
761 /* Public and protected members in the base are
763 base_access = ak_protected;
764 else if (!TREE_VIA_PUBLIC (base_binfo))
765 /* Public and protected members in the base are
767 base_access = ak_private;
769 /* See if the new access, via this base, gives more
770 access than our previous best access. */
771 if (base_access != ak_none
772 && (base_access == ak_public
773 || (base_access == ak_protected
774 && access != ak_public)
775 || (base_access == ak_private
776 && access == ak_none)))
778 access = base_access;
780 /* If the new access is public, we can't do better. */
781 if (access == ak_public)
788 /* Note the access to DECL in TYPE. */
789 SET_BINFO_ACCESS (binfo, access);
791 /* Mark TYPE as visited so that if we reach it again we do not
792 duplicate our efforts here. */
793 SET_BINFO_MARKED (binfo);
798 /* Return the access to DECL in TYPE. */
801 access_in_type (type, decl)
805 tree binfo = TYPE_BINFO (type);
807 /* We must take into account
811 If a name can be reached by several paths through a multiple
812 inheritance graph, the access is that of the path that gives
815 The algorithm we use is to make a post-order depth-first traversal
816 of the base-class hierarchy. As we come up the tree, we annotate
817 each node with the most lenient access. */
818 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
819 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
820 assert_canonical_unmarked (binfo);
822 return BINFO_ACCESS (binfo);
825 /* Called from dfs_accessible_p via dfs_walk. */
828 dfs_accessible_queue_p (binfo, data)
830 void *data ATTRIBUTE_UNUSED;
832 if (BINFO_MARKED (binfo))
835 /* If this class is inherited via private or protected inheritance,
836 then we can't see it, unless we are a friend of the subclass. */
837 if (!TREE_VIA_PUBLIC (binfo)
838 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
842 return canonical_binfo (binfo);
845 /* Called from dfs_accessible_p via dfs_walk. */
848 dfs_accessible_p (binfo, data)
852 int protected_ok = data != 0;
855 SET_BINFO_MARKED (binfo);
856 access = BINFO_ACCESS (binfo);
857 if (access == ak_public || (access == ak_protected && protected_ok))
859 else if (access != ak_none
860 && is_friend (BINFO_TYPE (binfo), current_scope ()))
866 /* Returns nonzero if it is OK to access DECL through an object
867 indiated by BINFO in the context of DERIVED. */
870 protected_accessible_p (decl, derived, binfo)
877 /* We're checking this clause from [class.access.base]
879 m as a member of N is protected, and the reference occurs in a
880 member or friend of class N, or in a member or friend of a
881 class P derived from N, where m as a member of P is private or
884 Here DERIVED is a possible P and DECL is m. accessible_p will
885 iterate over various values of N, but the access to m in DERIVED
888 Note that I believe that the passage above is wrong, and should read
889 "...is private or protected or public"; otherwise you get bizarre results
890 whereby a public using-decl can prevent you from accessing a protected
891 member of a base. (jason 2000/02/28) */
893 /* If DERIVED isn't derived from m's class, then it can't be a P. */
894 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
897 access = access_in_type (derived, decl);
899 /* If m is inaccessible in DERIVED, then it's not a P. */
900 if (access == ak_none)
905 When a friend or a member function of a derived class references
906 a protected nonstatic member of a base class, an access check
907 applies in addition to those described earlier in clause
908 _class.access_) Except when forming a pointer to member
909 (_expr.unary.op_), the access must be through a pointer to,
910 reference to, or object of the derived class itself (or any class
911 derived from that class) (_expr.ref_). If the access is to form
912 a pointer to member, the nested-name-specifier shall name the
913 derived class (or any class derived from that class). */
914 if (DECL_NONSTATIC_MEMBER_P (decl))
916 /* We can tell through what the reference is occurring by
917 chasing BINFO up to the root. */
919 while (BINFO_INHERITANCE_CHAIN (t))
920 t = BINFO_INHERITANCE_CHAIN (t);
922 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
929 /* Returns nonzero if SCOPE is a friend of a type which would be able
930 to access DECL through the object indicated by BINFO. */
933 friend_accessible_p (scope, decl, binfo)
938 tree befriending_classes;
944 if (TREE_CODE (scope) == FUNCTION_DECL
945 || DECL_FUNCTION_TEMPLATE_P (scope))
946 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
947 else if (TYPE_P (scope))
948 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
952 for (t = befriending_classes; t; t = TREE_CHAIN (t))
953 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
956 /* Nested classes are implicitly friends of their enclosing types, as
957 per core issue 45 (this is a change from the standard). */
959 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
960 if (protected_accessible_p (decl, t, binfo))
963 if (TREE_CODE (scope) == FUNCTION_DECL
964 || DECL_FUNCTION_TEMPLATE_P (scope))
966 /* Perhaps this SCOPE is a member of a class which is a
968 if (DECL_CLASS_SCOPE_P (decl)
969 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
972 /* Or an instantiation of something which is a friend. */
973 if (DECL_TEMPLATE_INFO (scope))
974 return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
976 else if (CLASSTYPE_TEMPLATE_INFO (scope))
977 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
982 /* DECL is a declaration from a base class of TYPE, which was the
983 class used to name DECL. Return nonzero if, in the current
984 context, DECL is accessible. If TYPE is actually a BINFO node,
985 then we can tell in what context the access is occurring by looking
986 at the most derived class along the path indicated by BINFO. */
989 accessible_p (type, decl)
997 /* Nonzero if it's OK to access DECL if it has protected
998 accessibility in TYPE. */
999 int protected_ok = 0;
1001 /* If we're not checking access, everything is accessible. */
1002 if (!scope_chain->check_access)
1005 /* If this declaration is in a block or namespace scope, there's no
1007 if (!TYPE_P (context_for_name_lookup (decl)))
1013 type = BINFO_TYPE (type);
1016 binfo = TYPE_BINFO (type);
1018 /* [class.access.base]
1020 A member m is accessible when named in class N if
1022 --m as a member of N is public, or
1024 --m as a member of N is private, and the reference occurs in a
1025 member or friend of class N, or
1027 --m as a member of N is protected, and the reference occurs in a
1028 member or friend of class N, or in a member or friend of a
1029 class P derived from N, where m as a member of P is private or
1032 --there exists a base class B of N that is accessible at the point
1033 of reference, and m is accessible when named in class B.
1035 We walk the base class hierarchy, checking these conditions. */
1037 /* Figure out where the reference is occurring. Check to see if
1038 DECL is private or protected in this scope, since that will
1039 determine whether protected access is allowed. */
1040 if (current_class_type)
1041 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
1043 /* Now, loop through the classes of which we are a friend. */
1045 protected_ok = friend_accessible_p (current_scope (), decl, binfo);
1047 /* Standardize the binfo that access_in_type will use. We don't
1048 need to know what path was chosen from this point onwards. */
1049 binfo = TYPE_BINFO (type);
1051 /* Compute the accessibility of DECL in the class hierarchy
1052 dominated by type. */
1053 access_in_type (type, decl);
1054 /* Walk the hierarchy again, looking for a base class that allows
1056 t = dfs_walk (binfo, dfs_accessible_p,
1057 dfs_accessible_queue_p,
1058 protected_ok ? &protected_ok : 0);
1059 /* Clear any mark bits. Note that we have to walk the whole tree
1060 here, since we have aborted the previous walk from some point
1061 deep in the tree. */
1062 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1063 assert_canonical_unmarked (binfo);
1065 return t != NULL_TREE;
1068 /* Recursive helper funciton for is_subobject_of_p; see that routine
1069 for documentation of the parameters. */
1072 is_subobject_of_p_1 (parent, binfo, most_derived)
1073 tree parent, binfo, most_derived;
1078 if (parent == binfo)
1081 binfos = BINFO_BASETYPES (binfo);
1082 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1084 /* Iterate through the base types. */
1085 for (i = 0; i < n_baselinks; i++)
1087 tree base_binfo = TREE_VEC_ELT (binfos, i);
1090 base_type = TREE_TYPE (base_binfo);
1091 if (!CLASS_TYPE_P (base_type))
1092 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1093 class there's no way to descend into it. */
1096 /* Avoid walking into the same virtual base more than once. */
1097 if (TREE_VIA_VIRTUAL (base_binfo))
1099 if (CLASSTYPE_MARKED4 (base_type))
1101 SET_CLASSTYPE_MARKED4 (base_type);
1102 base_binfo = binfo_for_vbase (base_type, most_derived);
1105 if (is_subobject_of_p_1 (parent, base_binfo, most_derived))
1111 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1112 found as a base class and sub-object of the object denoted by
1113 BINFO. MOST_DERIVED is the most derived type of the hierarchy being
1117 is_subobject_of_p (tree parent, tree binfo, tree most_derived)
1122 result = is_subobject_of_p_1 (parent, binfo, most_derived);
1123 /* Clear the mark bits on virtual bases. */
1124 for (vbase = CLASSTYPE_VBASECLASSES (most_derived);
1126 vbase = TREE_CHAIN (vbase))
1127 CLEAR_CLASSTYPE_MARKED4 (TREE_TYPE (TREE_VALUE (vbase)));
1132 struct lookup_field_info {
1133 /* The type in which we're looking. */
1135 /* The name of the field for which we're looking. */
1137 /* If non-NULL, the current result of the lookup. */
1139 /* The path to RVAL. */
1141 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1144 /* If nonzero, we are looking for types, not data members. */
1146 /* If nonzero, RVAL was found by looking through a dependent base. */
1147 int from_dep_base_p;
1148 /* If something went wrong, a message indicating what. */
1152 /* Returns nonzero if BINFO is not hidden by the value found by the
1153 lookup so far. If BINFO is hidden, then there's no need to look in
1154 it. DATA is really a struct lookup_field_info. Called from
1155 lookup_field via breadth_first_search. */
1158 lookup_field_queue_p (binfo, data)
1162 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1164 /* Don't look for constructors or destructors in base classes. */
1165 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1168 /* If this base class is hidden by the best-known value so far, we
1169 don't need to look. */
1170 binfo = CANONICAL_BINFO (binfo, lfi->type);
1171 if (!lfi->from_dep_base_p && lfi->rval_binfo
1172 && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1178 /* Within the scope of a template class, you can refer to the to the
1179 current specialization with the name of the template itself. For
1182 template <typename T> struct S { S* sp; }
1184 Returns nonzero if DECL is such a declaration in a class TYPE. */
1187 template_self_reference_p (type, decl)
1191 return (CLASSTYPE_USE_TEMPLATE (type)
1192 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
1193 && TREE_CODE (decl) == TYPE_DECL
1194 && DECL_ARTIFICIAL (decl)
1195 && DECL_NAME (decl) == constructor_name (type));
1199 /* Nonzero for a class member means that it is shared between all objects
1202 [class.member.lookup]:If the resulting set of declarations are not all
1203 from sub-objects of the same type, or the set has a nonstatic member
1204 and includes members from distinct sub-objects, there is an ambiguity
1205 and the program is ill-formed.
1207 This function checks that T contains no nonstatic members. */
1213 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
1214 || TREE_CODE (t) == CONST_DECL)
1216 if (is_overloaded_fn (t))
1218 for (; t; t = OVL_NEXT (t))
1220 tree fn = OVL_CURRENT (t);
1221 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1229 /* DATA is really a struct lookup_field_info. Look for a field with
1230 the name indicated there in BINFO. If this function returns a
1231 non-NULL value it is the result of the lookup. Called from
1232 lookup_field via breadth_first_search. */
1235 lookup_field_r (binfo, data)
1239 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1240 tree type = BINFO_TYPE (binfo);
1241 tree nval = NULL_TREE;
1242 int from_dep_base_p;
1244 /* First, look for a function. There can't be a function and a data
1245 member with the same name, and if there's a function and a type
1246 with the same name, the type is hidden by the function. */
1247 if (!lfi->want_type)
1249 int idx = lookup_fnfields_1 (type, lfi->name);
1251 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1255 /* Look for a data member or type. */
1256 nval = lookup_field_1 (type, lfi->name);
1258 /* If there is no declaration with the indicated name in this type,
1259 then there's nothing to do. */
1263 /* If we're looking up a type (as with an elaborated type specifier)
1264 we ignore all non-types we find. */
1265 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1266 && !DECL_CLASS_TEMPLATE_P (nval))
1268 if (lfi->name == TYPE_IDENTIFIER (type))
1270 /* If the aggregate has no user defined constructors, we allow
1271 it to have fields with the same name as the enclosing type.
1272 If we are looking for that name, find the corresponding
1274 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1275 if (DECL_NAME (nval) == lfi->name
1276 && TREE_CODE (nval) == TYPE_DECL)
1283 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1285 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1291 /* You must name a template base class with a template-id. */
1292 if (!same_type_p (type, lfi->type)
1293 && template_self_reference_p (type, nval))
1296 from_dep_base_p = dependent_base_p (binfo);
1297 if (lfi->from_dep_base_p && !from_dep_base_p)
1299 /* If the new declaration is not found via a dependent base, and
1300 the old one was, then we must prefer the new one. We weren't
1301 really supposed to be able to find the old one, so we don't
1302 want to be affected by a specialization. Consider:
1304 struct B { typedef int I; };
1305 template <typename T> struct D1 : virtual public B {};
1306 template <typename T> struct D :
1307 public D1, virtual pubic B { I i; };
1309 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1310 D1 is specialized. */
1311 lfi->from_dep_base_p = 0;
1312 lfi->rval = NULL_TREE;
1313 lfi->rval_binfo = NULL_TREE;
1314 lfi->ambiguous = NULL_TREE;
1317 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1318 /* Similarly, if the old declaration was not found via a dependent
1319 base, and the new one is, ignore the new one. */
1322 /* If the lookup already found a match, and the new value doesn't
1323 hide the old one, we might have an ambiguity. */
1324 if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
1326 if (nval == lfi->rval && shared_member_p (nval))
1327 /* The two things are really the same. */
1329 else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1330 /* The previous value hides the new one. */
1334 /* We have a real ambiguity. We keep a chain of all the
1336 if (!lfi->ambiguous && lfi->rval)
1338 /* This is the first time we noticed an ambiguity. Add
1339 what we previously thought was a reasonable candidate
1341 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1342 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1345 /* Add the new value. */
1346 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1347 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1348 lfi->errstr = "request for member `%D' is ambiguous";
1353 if (from_dep_base_p && TREE_CODE (nval) == TYPE_DECL
1354 /* We need to return a member template class so we can
1355 define partial specializations. Is there a better
1357 && !DECL_CLASS_TEMPLATE_P (nval))
1358 /* The thing we're looking for isn't a type, so the implicit
1359 typename extension doesn't apply, so we just pretend we
1360 didn't find anything. */
1364 lfi->from_dep_base_p = from_dep_base_p;
1365 lfi->rval_binfo = binfo;
1371 /* Return a "baselink" which BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1372 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1373 FUNCTIONS, and OPTYPE respectively. */
1376 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1380 my_friendly_assert (TREE_CODE (functions) == FUNCTION_DECL
1381 || TREE_CODE (functions) == TEMPLATE_DECL
1382 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1383 || TREE_CODE (functions) == OVERLOAD,
1385 my_friendly_assert (!optype || TYPE_P (optype), 20020730);
1386 my_friendly_assert (TREE_TYPE (functions), 20020805);
1388 baselink = make_node (BASELINK);
1389 TREE_TYPE (baselink) = TREE_TYPE (functions);
1390 BASELINK_BINFO (baselink) = binfo;
1391 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1392 BASELINK_FUNCTIONS (baselink) = functions;
1393 BASELINK_OPTYPE (baselink) = optype;
1398 /* Look for a member named NAME in an inheritance lattice dominated by
1399 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1400 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1401 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1402 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1403 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1404 TREE_VALUEs are the list of ambiguous candidates.
1406 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1408 If nothing can be found return NULL_TREE and do not issue an error. */
1411 lookup_member (xbasetype, name, protect, want_type)
1412 register tree xbasetype, name;
1413 int protect, want_type;
1415 tree rval, rval_binfo = NULL_TREE;
1416 tree type = NULL_TREE, basetype_path = NULL_TREE;
1417 struct lookup_field_info lfi;
1419 /* rval_binfo is the binfo associated with the found member, note,
1420 this can be set with useful information, even when rval is not
1421 set, because it must deal with ALL members, not just non-function
1422 members. It is used for ambiguity checking and the hidden
1423 checks. Whereas rval is only set if a proper (not hidden)
1424 non-function member is found. */
1426 const char *errstr = 0;
1428 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1429 && IDENTIFIER_CLASS_VALUE (name))
1431 tree field = IDENTIFIER_CLASS_VALUE (name);
1432 if (TREE_CODE (field) != FUNCTION_DECL
1433 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1434 /* We're in the scope of this class, and the value has already
1435 been looked up. Just return the cached value. */
1439 if (TREE_CODE (xbasetype) == TREE_VEC)
1441 type = BINFO_TYPE (xbasetype);
1442 basetype_path = xbasetype;
1444 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1447 basetype_path = TYPE_BINFO (type);
1448 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1454 complete_type (type);
1456 #ifdef GATHER_STATISTICS
1457 n_calls_lookup_field++;
1458 #endif /* GATHER_STATISTICS */
1460 memset ((PTR) &lfi, 0, sizeof (lfi));
1463 lfi.want_type = want_type;
1464 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
1466 rval_binfo = lfi.rval_binfo;
1468 type = BINFO_TYPE (rval_binfo);
1469 errstr = lfi.errstr;
1471 /* If we are not interested in ambiguities, don't report them;
1472 just return NULL_TREE. */
1473 if (!protect && lfi.ambiguous)
1479 return lfi.ambiguous;
1486 In the case of overloaded function names, access control is
1487 applied to the function selected by overloaded resolution. */
1488 if (rval && protect && !is_overloaded_fn (rval)
1489 && !enforce_access (xbasetype, rval))
1490 return error_mark_node;
1492 if (errstr && protect)
1494 error (errstr, name, type);
1496 print_candidates (lfi.ambiguous);
1497 rval = error_mark_node;
1500 /* If the thing we found was found via the implicit typename
1501 extension, build the typename type. */
1502 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1505 if (rval && is_overloaded_fn (rval))
1506 rval = build_baselink (rval_binfo, basetype_path, rval,
1507 (IDENTIFIER_TYPENAME_P (name)
1508 ? TREE_TYPE (name): NULL_TREE));
1512 /* Like lookup_member, except that if we find a function member we
1513 return NULL_TREE. */
1516 lookup_field (xbasetype, name, protect, want_type)
1517 register tree xbasetype, name;
1518 int protect, want_type;
1520 tree rval = lookup_member (xbasetype, name, protect, want_type);
1522 /* Ignore functions. */
1523 if (rval && BASELINK_P (rval))
1529 /* Like lookup_member, except that if we find a non-function member we
1530 return NULL_TREE. */
1533 lookup_fnfields (xbasetype, name, protect)
1534 register tree xbasetype, name;
1537 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
1539 /* Ignore non-functions. */
1540 if (rval && !BASELINK_P (rval))
1546 /* TYPE is a class type. Return the index of the fields within
1547 the method vector with name NAME, or -1 is no such field exists. */
1550 lookup_fnfields_1 (type, name)
1553 tree method_vec = (CLASS_TYPE_P (type)
1554 ? CLASSTYPE_METHOD_VEC (type)
1557 if (method_vec != 0)
1560 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1561 int len = TREE_VEC_LENGTH (method_vec);
1564 #ifdef GATHER_STATISTICS
1565 n_calls_lookup_fnfields_1++;
1566 #endif /* GATHER_STATISTICS */
1568 /* Constructors are first... */
1569 if (name == ctor_identifier)
1570 return (methods[CLASSTYPE_CONSTRUCTOR_SLOT]
1571 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
1572 /* and destructors are second. */
1573 if (name == dtor_identifier)
1574 return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1575 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
1577 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1578 i < len && methods[i];
1581 #ifdef GATHER_STATISTICS
1582 n_outer_fields_searched++;
1583 #endif /* GATHER_STATISTICS */
1585 tmp = OVL_CURRENT (methods[i]);
1586 if (DECL_NAME (tmp) == name)
1589 /* If the type is complete and we're past the conversion ops,
1590 switch to binary search. */
1591 if (! DECL_CONV_FN_P (tmp)
1592 && COMPLETE_TYPE_P (type))
1594 int lo = i + 1, hi = len;
1600 #ifdef GATHER_STATISTICS
1601 n_outer_fields_searched++;
1602 #endif /* GATHER_STATISTICS */
1604 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1608 else if (tmp < name)
1617 /* If we didn't find it, it might have been a template
1618 conversion operator to a templated type. If there are any,
1619 such template conversion operators will all be overloaded on
1620 the first conversion slot. (Note that we don't look for this
1621 case above so that we will always find specializations
1623 if (IDENTIFIER_TYPENAME_P (name))
1625 i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1626 if (i < len && methods[i])
1628 tmp = OVL_CURRENT (methods[i]);
1629 if (TREE_CODE (tmp) == TEMPLATE_DECL
1630 && DECL_TEMPLATE_CONV_FN_P (tmp))
1639 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1640 the class or namespace used to qualify the name. CONTEXT_CLASS is
1641 the class corresponding to the object in which DECL will be used.
1642 Return a possibly modified version of DECL that takes into account
1645 In particular, consider an expression like `B::m' in the context of
1646 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1647 then the most derived class indicated by the BASELINK_BINFO will be
1648 `B', not `D'. This function makes that adjustment. */
1651 adjust_result_of_qualified_name_lookup (tree decl,
1652 tree qualifying_scope,
1655 if (context_class && CLASS_TYPE_P (qualifying_scope)
1656 && DERIVED_FROM_P (qualifying_scope, context_class)
1657 && BASELINK_P (decl))
1661 my_friendly_assert (CLASS_TYPE_P (context_class), 20020808);
1663 /* Look for the QUALIFYING_SCOPE as a base of the
1664 CONTEXT_CLASS. If QUALIFYING_SCOPE is ambiguous, we cannot
1665 be sure yet than an error has occurred; perhaps the function
1666 chosen by overload resolution will be static. */
1667 base = lookup_base (context_class, qualifying_scope,
1668 ba_ignore | ba_quiet, NULL);
1671 BASELINK_ACCESS_BINFO (decl) = base;
1672 BASELINK_BINFO (decl)
1673 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1674 ba_ignore | ba_quiet,
1683 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1684 type in the hierarchy, in a breadth-first preorder traversal.
1685 If it ever returns a non-NULL value, that value is immediately
1686 returned and the walk is terminated. At each node, FN is passed a
1687 BINFO indicating the path from the curently visited base-class to
1688 TYPE. Before each base-class is walked QFN is called. If the
1689 value returned is nonzero, the base-class is walked; otherwise it
1690 is not. If QFN is NULL, it is treated as a function which always
1691 returns 1. Both FN and QFN are passed the DATA whenever they are
1695 bfs_walk (binfo, fn, qfn, data)
1697 tree (*fn) PARAMS ((tree, void *));
1698 tree (*qfn) PARAMS ((tree, void *));
1703 tree rval = NULL_TREE;
1704 /* An array of the base classes of BINFO. These will be built up in
1705 breadth-first order, except where QFN prunes the search. */
1706 varray_type bfs_bases;
1708 /* Start with enough room for ten base classes. That will be enough
1709 for most hierarchies. */
1710 VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
1712 /* Put the first type into the stack. */
1713 VARRAY_TREE (bfs_bases, 0) = binfo;
1716 for (head = 0; head < tail; ++head)
1722 /* Pull the next type out of the queue. */
1723 binfo = VARRAY_TREE (bfs_bases, head);
1725 /* If this is the one we're looking for, we're done. */
1726 rval = (*fn) (binfo, data);
1730 /* Queue up the base types. */
1731 binfos = BINFO_BASETYPES (binfo);
1732 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
1733 for (i = 0; i < n_baselinks; i++)
1735 tree base_binfo = TREE_VEC_ELT (binfos, i);
1738 base_binfo = (*qfn) (base_binfo, data);
1742 if (tail == VARRAY_SIZE (bfs_bases))
1743 VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
1744 VARRAY_TREE (bfs_bases, tail) = base_binfo;
1753 /* Exactly like bfs_walk, except that a depth-first traversal is
1754 performed, and PREFN is called in preorder, while POSTFN is called
1758 dfs_walk_real (binfo, prefn, postfn, qfn, data)
1760 tree (*prefn) PARAMS ((tree, void *));
1761 tree (*postfn) PARAMS ((tree, void *));
1762 tree (*qfn) PARAMS ((tree, void *));
1768 tree rval = NULL_TREE;
1770 /* Call the pre-order walking function. */
1773 rval = (*prefn) (binfo, data);
1778 /* Process the basetypes. */
1779 binfos = BINFO_BASETYPES (binfo);
1780 n_baselinks = BINFO_N_BASETYPES (binfo);
1781 for (i = 0; i < n_baselinks; i++)
1783 tree base_binfo = TREE_VEC_ELT (binfos, i);
1786 base_binfo = (*qfn) (base_binfo, data);
1790 rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
1796 /* Call the post-order walking function. */
1798 rval = (*postfn) (binfo, data);
1803 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1807 dfs_walk (binfo, fn, qfn, data)
1809 tree (*fn) PARAMS ((tree, void *));
1810 tree (*qfn) PARAMS ((tree, void *));
1813 return dfs_walk_real (binfo, 0, fn, qfn, data);
1816 /* Check that virtual overrider OVERRIDER is acceptable for base function
1817 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1820 check_final_overrider (overrider, basefn)
1821 tree overrider, basefn;
1823 tree over_type = TREE_TYPE (overrider);
1824 tree base_type = TREE_TYPE (basefn);
1825 tree over_return = TREE_TYPE (over_type);
1826 tree base_return = TREE_TYPE (base_type);
1827 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1828 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1831 if (same_type_p (base_return, over_return))
1833 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1834 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1835 && POINTER_TYPE_P (base_return)))
1837 /* Potentially covariant. */
1838 unsigned base_quals, over_quals;
1840 fail = !POINTER_TYPE_P (base_return);
1843 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1845 base_return = TREE_TYPE (base_return);
1846 over_return = TREE_TYPE (over_return);
1848 base_quals = cp_type_quals (base_return);
1849 over_quals = cp_type_quals (over_return);
1851 if ((base_quals & over_quals) != over_quals)
1854 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1856 tree binfo = lookup_base (over_return, base_return,
1857 ba_check | ba_quiet, NULL);
1863 && can_convert (TREE_TYPE (base_type), TREE_TYPE (over_type)))
1864 /* GNU extension, allow trivial pointer conversions such as
1865 converting to void *, or qualification conversion. */
1867 /* can_convert will permit user defined conversion from a
1868 (reference to) class type. We must reject them. */
1869 over_return = TREE_TYPE (over_type);
1870 if (TREE_CODE (over_return) == REFERENCE_TYPE)
1871 over_return = TREE_TYPE (over_return);
1872 if (CLASS_TYPE_P (over_return))
1882 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
1888 cp_error_at ("invalid covariant return type for `%#D'", overrider);
1889 cp_error_at (" overriding `%#D'", basefn);
1893 cp_error_at ("conflicting return type specified for `%#D'",
1895 cp_error_at (" overriding `%#D'", basefn);
1897 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1898 DECL_CONTEXT (overrider));
1902 /* Check throw specifier is at least as strict. */
1903 if (!comp_except_specs (base_throw, over_throw, 0))
1905 if (!IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
1907 cp_error_at ("looser throw specifier for `%#F'", overrider);
1908 cp_error_at (" overriding `%#F'", basefn);
1909 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1910 DECL_CONTEXT (overrider));
1918 /* Given a class TYPE, and a function decl FNDECL, look for
1919 virtual functions in TYPE's hierarchy which FNDECL overrides.
1920 We do not look in TYPE itself, only its bases.
1922 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1923 find that it overrides anything.
1925 We check that every function which is overridden, is correctly
1929 look_for_overrides (type, fndecl)
1932 tree binfo = TYPE_BINFO (type);
1933 tree basebinfos = BINFO_BASETYPES (binfo);
1934 int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
1938 for (ix = 0; ix != nbasebinfos; ix++)
1940 tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
1942 if (TYPE_POLYMORPHIC_P (basetype))
1943 found += look_for_overrides_r (basetype, fndecl);
1948 /* Look in TYPE for virtual functions with the same signature as
1952 look_for_overrides_here (type, fndecl)
1957 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
1958 ix = CLASSTYPE_DESTRUCTOR_SLOT;
1960 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
1963 tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
1965 for (; fns; fns = OVL_NEXT (fns))
1967 tree fn = OVL_CURRENT (fns);
1969 if (!DECL_VIRTUAL_P (fn))
1970 /* Not a virtual. */;
1971 else if (DECL_CONTEXT (fn) != type)
1972 /* Introduced with a using declaration. */;
1973 else if (DECL_STATIC_FUNCTION_P (fndecl))
1975 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1976 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1977 if (compparms (TREE_CHAIN (btypes), dtypes))
1980 else if (same_signature_p (fndecl, fn))
1987 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1988 TYPE itself and its bases. */
1991 look_for_overrides_r (type, fndecl)
1994 tree fn = look_for_overrides_here (type, fndecl);
1997 if (DECL_STATIC_FUNCTION_P (fndecl))
1999 /* A static member function cannot match an inherited
2000 virtual member function. */
2001 cp_error_at ("`%#D' cannot be declared", fndecl);
2002 cp_error_at (" since `%#D' declared in base class", fn);
2006 /* It's definitely virtual, even if not explicitly set. */
2007 DECL_VIRTUAL_P (fndecl) = 1;
2008 check_final_overrider (fndecl, fn);
2013 /* We failed to find one declared in this class. Look in its bases. */
2014 return look_for_overrides (type, fndecl);
2017 /* A queue function to use with dfs_walk that only walks into
2018 canonical bases. DATA should be the type of the complete object,
2019 or a TREE_LIST whose TREE_PURPOSE is the type of the complete
2020 object. By using this function as a queue function, you will walk
2021 over exactly those BINFOs that actually exist in the complete
2022 object, including those for virtual base classes. If you
2023 SET_BINFO_MARKED for each binfo you process, you are further
2024 guaranteed that you will walk into each virtual base class exactly
2028 dfs_unmarked_real_bases_queue_p (binfo, data)
2032 if (TREE_VIA_VIRTUAL (binfo))
2034 tree type = (tree) data;
2036 if (TREE_CODE (type) == TREE_LIST)
2037 type = TREE_PURPOSE (type);
2038 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2040 return unmarkedp (binfo, NULL);
2043 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2044 that are marked, rather than unmarked. */
2047 dfs_marked_real_bases_queue_p (binfo, data)
2051 if (TREE_VIA_VIRTUAL (binfo))
2053 tree type = (tree) data;
2055 if (TREE_CODE (type) == TREE_LIST)
2056 type = TREE_PURPOSE (type);
2057 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2059 return markedp (binfo, NULL);
2062 /* A queue function that skips all virtual bases (and their
2066 dfs_skip_vbases (binfo, data)
2068 void *data ATTRIBUTE_UNUSED;
2070 if (TREE_VIA_VIRTUAL (binfo))
2076 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2079 dfs_get_pure_virtuals (binfo, data)
2083 tree type = (tree) data;
2085 /* We're not interested in primary base classes; the derived class
2086 of which they are a primary base will contain the information we
2088 if (!BINFO_PRIMARY_P (binfo))
2092 for (virtuals = BINFO_VIRTUALS (binfo);
2094 virtuals = TREE_CHAIN (virtuals))
2095 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2096 CLASSTYPE_PURE_VIRTUALS (type)
2097 = tree_cons (NULL_TREE, BV_FN (virtuals),
2098 CLASSTYPE_PURE_VIRTUALS (type));
2101 SET_BINFO_MARKED (binfo);
2106 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2109 get_pure_virtuals (type)
2114 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2115 is going to be overridden. */
2116 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
2117 /* Now, run through all the bases which are not primary bases, and
2118 collect the pure virtual functions. We look at the vtable in
2119 each class to determine what pure virtual functions are present.
2120 (A primary base is not interesting because the derived class of
2121 which it is a primary base will contain vtable entries for the
2122 pure virtuals in the base class. */
2123 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2124 dfs_unmarked_real_bases_queue_p, type);
2125 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2126 dfs_marked_real_bases_queue_p, type);
2128 /* Put the pure virtuals in dfs order. */
2129 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2131 for (vbases = CLASSTYPE_VBASECLASSES (type);
2133 vbases = TREE_CHAIN (vbases))
2137 for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
2139 virtuals = TREE_CHAIN (virtuals))
2141 tree base_fndecl = BV_FN (virtuals);
2142 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2143 error ("`%#D' needs a final overrider", base_fndecl);
2148 /* DEPTH-FIRST SEARCH ROUTINES. */
2151 markedp (binfo, data)
2153 void *data ATTRIBUTE_UNUSED;
2155 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2159 unmarkedp (binfo, data)
2161 void *data ATTRIBUTE_UNUSED;
2163 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2167 marked_vtable_pathp (binfo, data)
2169 void *data ATTRIBUTE_UNUSED;
2171 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2175 unmarked_vtable_pathp (binfo, data)
2177 void *data ATTRIBUTE_UNUSED;
2179 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2183 marked_pushdecls_p (binfo, data)
2185 void *data ATTRIBUTE_UNUSED;
2187 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2188 && !dependent_base_p (binfo)
2189 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2193 unmarked_pushdecls_p (binfo, data)
2195 void *data ATTRIBUTE_UNUSED;
2197 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2198 && !dependent_base_p (binfo)
2199 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2202 /* The worker functions for `dfs_walk'. These do not need to
2203 test anything (vis a vis marking) if they are paired with
2204 a predicate function (above). */
2207 dfs_unmark (binfo, data)
2209 void *data ATTRIBUTE_UNUSED;
2211 CLEAR_BINFO_MARKED (binfo);
2215 /* get virtual base class types.
2216 This adds type to the vbase_types list in reverse dfs order.
2217 Ordering is very important, so don't change it. */
2220 dfs_get_vbase_types (binfo, data)
2224 tree type = (tree) data;
2226 if (TREE_VIA_VIRTUAL (binfo))
2227 CLASSTYPE_VBASECLASSES (type)
2228 = tree_cons (BINFO_TYPE (binfo),
2230 CLASSTYPE_VBASECLASSES (type));
2231 SET_BINFO_MARKED (binfo);
2235 /* Called via dfs_walk from mark_primary_bases. Builds the
2236 inheritance graph order list of BINFOs. */
2239 dfs_build_inheritance_graph_order (binfo, data)
2243 tree *last_binfo = (tree *) data;
2246 TREE_CHAIN (*last_binfo) = binfo;
2247 *last_binfo = binfo;
2248 SET_BINFO_MARKED (binfo);
2252 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
2255 get_vbase_types (type)
2260 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
2261 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
2262 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2263 reverse it so that we get normal dfs ordering. */
2264 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
2265 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2266 /* Thread the BINFOs in inheritance-graph order. */
2268 dfs_walk_real (TYPE_BINFO (type),
2269 dfs_build_inheritance_graph_order,
2273 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
2276 /* Called from find_vbase_instance via dfs_walk. */
2279 dfs_find_vbase_instance (binfo, data)
2283 tree base = TREE_VALUE ((tree) data);
2285 if (BINFO_PRIMARY_P (binfo)
2286 && same_type_p (BINFO_TYPE (binfo), base))
2292 /* Find the real occurrence of the virtual BASE (a class type) in the
2293 hierarchy dominated by TYPE. */
2296 find_vbase_instance (base, type)
2302 instance = binfo_for_vbase (base, type);
2303 if (!BINFO_PRIMARY_P (instance))
2306 return dfs_walk (TYPE_BINFO (type),
2307 dfs_find_vbase_instance,
2309 build_tree_list (type, base));
2313 /* Debug info for C++ classes can get very large; try to avoid
2314 emitting it everywhere.
2316 Note that this optimization wins even when the target supports
2317 BINCL (if only slightly), and reduces the amount of work for the
2321 maybe_suppress_debug_info (t)
2324 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2325 does not support name references between translation units. It supports
2326 symbolic references between translation units, but only within a single
2327 executable or shared library.
2329 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2330 that the type was never defined, so we only get the members we
2332 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
2335 /* We might have set this earlier in cp_finish_decl. */
2336 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2338 /* If we already know how we're handling this class, handle debug info
2340 if (CLASSTYPE_INTERFACE_KNOWN (t))
2342 if (CLASSTYPE_INTERFACE_ONLY (t))
2343 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2344 /* else don't set it. */
2346 /* If the class has a vtable, write out the debug info along with
2348 else if (TYPE_CONTAINS_VPTR_P (t))
2349 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2351 /* Otherwise, just emit the debug info normally. */
2354 /* Note that we want debugging information for a base class of a class
2355 whose vtable is being emitted. Normally, this would happen because
2356 calling the constructor for a derived class implies calling the
2357 constructors for all bases, which involve initializing the
2358 appropriate vptr with the vtable for the base class; but in the
2359 presence of optimization, this initialization may be optimized
2360 away, so we tell finish_vtable_vardecl that we want the debugging
2361 information anyway. */
2364 dfs_debug_mark (binfo, data)
2366 void *data ATTRIBUTE_UNUSED;
2368 tree t = BINFO_TYPE (binfo);
2370 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2375 /* Returns BINFO if we haven't already noted that we want debugging
2376 info for this base class. */
2379 dfs_debug_unmarkedp (binfo, data)
2381 void *data ATTRIBUTE_UNUSED;
2383 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2384 ? binfo : NULL_TREE);
2387 /* Write out the debugging information for TYPE, whose vtable is being
2388 emitted. Also walk through our bases and note that we want to
2389 write out information for them. This avoids the problem of not
2390 writing any debug info for intermediate basetypes whose
2391 constructors, and thus the references to their vtables, and thus
2392 the vtables themselves, were optimized away. */
2395 note_debug_info_needed (type)
2398 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2400 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2401 rest_of_type_compilation (type, toplevel_bindings_p ());
2404 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
2407 /* Subroutines of push_class_decls (). */
2409 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2410 because it (or one of the intermediate bases) depends on template parms. */
2413 dependent_base_p (binfo)
2416 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2418 if (currently_open_class (TREE_TYPE (binfo)))
2420 if (dependent_type_p (TREE_TYPE (binfo)))
2427 setup_class_bindings (name, type_binding_p)
2431 tree type_binding = NULL_TREE;
2434 /* If we've already done the lookup for this declaration, we're
2436 if (IDENTIFIER_CLASS_VALUE (name))
2439 /* First, deal with the type binding. */
2442 type_binding = lookup_member (current_class_type, name,
2445 if (TREE_CODE (type_binding) == TREE_LIST
2446 && TREE_TYPE (type_binding) == error_mark_node)
2447 /* NAME is ambiguous. */
2448 push_class_level_binding (name, type_binding);
2450 pushdecl_class_level (type_binding);
2453 /* Now, do the value binding. */
2454 value_binding = lookup_member (current_class_type, name,
2459 && (TREE_CODE (value_binding) == TYPE_DECL
2460 || DECL_CLASS_TEMPLATE_P (value_binding)
2461 || (TREE_CODE (value_binding) == TREE_LIST
2462 && TREE_TYPE (value_binding) == error_mark_node
2463 && (TREE_CODE (TREE_VALUE (value_binding))
2465 /* We found a type-binding, even when looking for a non-type
2466 binding. This means that we already processed this binding
2468 else if (value_binding)
2470 if (TREE_CODE (value_binding) == TREE_LIST
2471 && TREE_TYPE (value_binding) == error_mark_node)
2472 /* NAME is ambiguous. */
2473 push_class_level_binding (name, value_binding);
2476 if (BASELINK_P (value_binding))
2477 /* NAME is some overloaded functions. */
2478 value_binding = BASELINK_FUNCTIONS (value_binding);
2479 pushdecl_class_level (value_binding);
2484 /* Push class-level declarations for any names appearing in BINFO that
2488 dfs_push_type_decls (binfo, data)
2490 void *data ATTRIBUTE_UNUSED;
2495 type = BINFO_TYPE (binfo);
2496 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2497 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
2498 && !(!same_type_p (type, current_class_type)
2499 && template_self_reference_p (type, fields)))
2500 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
2502 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2503 DERIVED_FROM_P, which calls get_base_distance. */
2504 SET_BINFO_PUSHDECLS_MARKED (binfo);
2509 /* Push class-level declarations for any names appearing in BINFO that
2510 are not TYPE_DECLS. */
2513 dfs_push_decls (binfo, data)
2521 type = BINFO_TYPE (binfo);
2522 dep_base_p = (processing_template_decl && type != current_class_type
2523 && dependent_base_p (binfo));
2527 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2528 if (DECL_NAME (fields)
2529 && TREE_CODE (fields) != TYPE_DECL
2530 && TREE_CODE (fields) != USING_DECL
2531 && !DECL_ARTIFICIAL (fields))
2532 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
2533 else if (TREE_CODE (fields) == FIELD_DECL
2534 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2535 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
2537 method_vec = (CLASS_TYPE_P (type)
2538 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
2540 if (method_vec && TREE_VEC_LENGTH (method_vec) >= 3)
2545 /* Farm out constructors and destructors. */
2546 end = TREE_VEC_END (method_vec);
2548 for (methods = &TREE_VEC_ELT (method_vec, 2);
2549 methods < end && *methods;
2551 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
2552 /*type_binding_p=*/0);
2556 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
2561 /* When entering the scope of a class, we cache all of the
2562 fields that that class provides within its inheritance
2563 lattice. Where ambiguities result, we mark them
2564 with `error_mark_node' so that if they are encountered
2565 without explicit qualification, we can emit an error
2569 push_class_decls (type)
2572 search_stack = push_search_level (search_stack, &search_obstack);
2574 /* Enter type declarations and mark. */
2575 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
2577 /* Enter non-type declarations and unmark. */
2578 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
2581 /* Here's a subroutine we need because C lacks lambdas. */
2584 dfs_unuse_fields (binfo, data)
2586 void *data ATTRIBUTE_UNUSED;
2588 tree type = TREE_TYPE (binfo);
2591 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2593 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
2596 TREE_USED (fields) = 0;
2597 if (DECL_NAME (fields) == NULL_TREE
2598 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2599 unuse_fields (TREE_TYPE (fields));
2609 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
2615 /* We haven't pushed a search level when dealing with cached classes,
2616 so we'd better not try to pop it. */
2618 search_stack = pop_search_level (search_stack);
2622 print_search_statistics ()
2624 #ifdef GATHER_STATISTICS
2625 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2626 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2627 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2628 n_outer_fields_searched, n_calls_lookup_fnfields);
2629 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2630 #else /* GATHER_STATISTICS */
2631 fprintf (stderr, "no search statistics\n");
2632 #endif /* GATHER_STATISTICS */
2636 init_search_processing ()
2638 gcc_obstack_init (&search_obstack);
2642 reinit_search_statistics ()
2644 #ifdef GATHER_STATISTICS
2645 n_fields_searched = 0;
2646 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2647 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2648 n_calls_get_base_type = 0;
2649 n_outer_fields_searched = 0;
2650 n_contexts_saved = 0;
2651 #endif /* GATHER_STATISTICS */
2655 add_conversions (binfo, data)
2660 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2661 tree *conversions = (tree *) data;
2663 /* Some builtin types have no method vector, not even an empty one. */
2667 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
2669 tree tmp = TREE_VEC_ELT (method_vec, i);
2672 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
2675 name = DECL_NAME (OVL_CURRENT (tmp));
2677 /* Make sure we don't already have this conversion. */
2678 if (! IDENTIFIER_MARKED (name))
2680 *conversions = tree_cons (binfo, tmp, *conversions);
2681 IDENTIFIER_MARKED (name) = 1;
2687 /* Return a TREE_LIST containing all the non-hidden user-defined
2688 conversion functions for TYPE (and its base-classes). The
2689 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2690 containing the conversion functions. The TREE_PURPOSE is the BINFO
2691 from which the conversion functions in this node were selected. */
2694 lookup_conversions (type)
2698 tree conversions = NULL_TREE;
2700 if (COMPLETE_TYPE_P (type))
2701 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
2703 for (t = conversions; t; t = TREE_CHAIN (t))
2704 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
2715 /* Check whether the empty class indicated by EMPTY_BINFO is also present
2716 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
2719 dfs_check_overlap (empty_binfo, data)
2723 struct overlap_info *oi = (struct overlap_info *) data;
2725 for (binfo = TYPE_BINFO (oi->compare_type);
2727 binfo = BINFO_BASETYPE (binfo, 0))
2729 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
2731 oi->found_overlap = 1;
2734 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
2741 /* Trivial function to stop base traversal when we find something. */
2744 dfs_no_overlap_yet (binfo, data)
2748 struct overlap_info *oi = (struct overlap_info *) data;
2749 return !oi->found_overlap ? binfo : NULL_TREE;
2752 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2753 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
2756 types_overlap_p (empty_type, next_type)
2757 tree empty_type, next_type;
2759 struct overlap_info oi;
2761 if (! IS_AGGR_TYPE (next_type))
2763 oi.compare_type = next_type;
2764 oi.found_overlap = 0;
2765 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
2766 dfs_no_overlap_yet, &oi);
2767 return oi.found_overlap;
2770 /* Given a vtable VAR, determine which of the inherited classes the vtable
2771 inherits (in a loose sense) functions from.
2773 FIXME: This does not work with the new ABI. */
2776 binfo_for_vtable (var)
2779 tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
2780 tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
2781 int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
2784 for (i = 0; i < n_baseclasses; i++)
2786 tree base_binfo = TREE_VEC_ELT (binfos, i);
2787 if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
2791 /* If no secondary base classes matched, return the primary base, if
2793 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
2794 return get_primary_binfo (main_binfo);
2799 /* Returns the binfo of the first direct or indirect virtual base derived
2800 from BINFO, or NULL if binfo is not via virtual. */
2803 binfo_from_vbase (binfo)
2806 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2808 if (TREE_VIA_VIRTUAL (binfo))
2814 /* Returns the binfo of the first direct or indirect virtual base derived
2815 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2819 binfo_via_virtual (binfo, limit)
2823 for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
2824 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2826 if (TREE_VIA_VIRTUAL (binfo))
2832 /* Returns the BINFO (if any) for the virtual baseclass T of the class
2833 C from the CLASSTYPE_VBASECLASSES list. */
2836 binfo_for_vbase (basetype, classtype)
2842 binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
2843 return binfo ? TREE_VALUE (binfo) : NULL_TREE;