1 /* Breadth-first and depth-first routines for
2 searching multiple-inheritance lattice for GNU C++.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002, 2003 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 (tree, tree);
85 static int is_subobject_of_p (tree, tree, tree);
86 static int is_subobject_of_p_1 (tree, tree, tree);
87 static tree dfs_check_overlap (tree, void *);
88 static tree dfs_no_overlap_yet (tree, void *);
89 static base_kind lookup_base_r (tree, tree, base_access,
90 bool, bool, bool, tree *);
91 static int dynamic_cast_base_recurse (tree, tree, bool, tree *);
92 static tree marked_pushdecls_p (tree, void *);
93 static tree unmarked_pushdecls_p (tree, void *);
94 static tree dfs_debug_unmarkedp (tree, void *);
95 static tree dfs_debug_mark (tree, void *);
96 static tree dfs_get_vbase_types (tree, void *);
97 static tree dfs_push_type_decls (tree, void *);
98 static tree dfs_push_decls (tree, void *);
99 static tree dfs_unuse_fields (tree, void *);
100 static tree add_conversions (tree, void *);
101 static int look_for_overrides_r (tree, tree);
102 static struct search_level *push_search_level (struct stack_level *,
104 static struct search_level *pop_search_level (struct stack_level *);
105 static void grow_bfs_bases (tree **, size_t *, size_t *);
106 static tree bfs_walk (tree, tree (*) (tree, void *),
107 tree (*) (tree, void *), void *);
108 static tree lookup_field_queue_p (tree, void *);
109 static int shared_member_p (tree);
110 static tree lookup_field_r (tree, void *);
111 static tree canonical_binfo (tree);
112 static tree shared_marked_p (tree, void *);
113 static tree shared_unmarked_p (tree, void *);
114 static int dependent_base_p (tree);
115 static tree dfs_accessible_queue_p (tree, void *);
116 static tree dfs_accessible_p (tree, void *);
117 static tree dfs_access_in_type (tree, void *);
118 static access_kind access_in_type (tree, tree);
119 static tree dfs_canonical_queue (tree, void *);
120 static tree dfs_assert_unmarked_p (tree, void *);
121 static void assert_canonical_unmarked (tree);
122 static int protected_accessible_p (tree, tree, tree);
123 static int friend_accessible_p (tree, tree, tree);
124 static void setup_class_bindings (tree, int);
125 static int template_self_reference_p (tree, tree);
126 static tree dfs_find_vbase_instance (tree, void *);
127 static tree dfs_get_pure_virtuals (tree, void *);
128 static tree dfs_build_inheritance_graph_order (tree, void *);
130 /* Allocate a level of searching. */
132 static struct search_level *
133 push_search_level (struct stack_level *stack, struct obstack *obstack)
135 struct search_level tem;
138 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
141 /* Discard a level of search allocation. */
143 static struct search_level *
144 pop_search_level (struct stack_level *obstack)
146 register struct search_level *stack = pop_stack_level (obstack);
151 /* Variables for gathering statistics. */
152 #ifdef GATHER_STATISTICS
153 static int n_fields_searched;
154 static int n_calls_lookup_field, n_calls_lookup_field_1;
155 static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
156 static int n_calls_get_base_type;
157 static int n_outer_fields_searched;
158 static int n_contexts_saved;
159 #endif /* GATHER_STATISTICS */
162 /* Worker for lookup_base. BINFO is the binfo we are searching at,
163 BASE is the RECORD_TYPE we are searching for. ACCESS is the
164 required access checks. WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
165 IS_VIRTUAL indicate how BINFO was reached from the start of the
166 search. WITHIN_CURRENT_SCOPE is true if we met the current scope,
167 or friend thereof (this allows us to determine whether a protected
168 base is accessible or not). IS_NON_PUBLIC indicates whether BINFO
169 is accessible and IS_VIRTUAL indicates if it is morally virtual.
171 If BINFO is of the required type, then *BINFO_PTR is examined to
172 compare with any other instance of BASE we might have already
173 discovered. *BINFO_PTR is initialized and a base_kind return value
174 indicates what kind of base was located.
176 Otherwise BINFO's bases are searched. */
179 lookup_base_r (tree binfo, tree base, base_access access,
180 bool within_current_scope,
181 bool is_non_public, /* inside a non-public part */
182 bool is_virtual, /* inside a virtual part */
187 base_kind found = bk_not_base;
189 if (access == ba_check
190 && !within_current_scope
191 && is_friend (BINFO_TYPE (binfo), current_scope ()))
193 /* Do not clear is_non_public here. If A is a private base of B, A
194 is not allowed to convert a B* to an A*. */
195 within_current_scope = 1;
198 if (same_type_p (BINFO_TYPE (binfo), base))
200 /* We have found a base. Check against what we have found
202 found = bk_same_type;
204 found = bk_via_virtual;
206 found = bk_inaccessible;
210 else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
211 BINFO_OFFSET (*binfo_ptr)))
213 if (access != ba_any)
215 else if (!is_virtual)
216 /* Prefer a non-virtual base. */
224 bases = BINFO_BASETYPES (binfo);
228 for (i = TREE_VEC_LENGTH (bases); i--;)
230 tree base_binfo = TREE_VEC_ELT (bases, i);
231 int this_non_public = is_non_public;
232 int this_virtual = is_virtual;
235 if (access <= ba_ignore)
237 else if (TREE_VIA_PUBLIC (base_binfo))
239 else if (access == ba_not_special)
241 else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
243 else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
248 if (TREE_VIA_VIRTUAL (base_binfo))
251 bk = lookup_base_r (base_binfo, base,
252 access, within_current_scope,
253 this_non_public, this_virtual,
259 if (access != ba_any)
264 case bk_inaccessible:
265 if (found == bk_not_base)
267 my_friendly_assert (found == bk_via_virtual
268 || found == bk_inaccessible, 20010723);
276 my_friendly_assert (found == bk_not_base, 20010723);
281 if (found != bk_ambig)
292 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
293 ACCESS specifies. Return the binfo we discover (which might not be
294 canonical). If KIND_PTR is non-NULL, fill with information about
295 what kind of base we discovered.
297 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
298 not set in ACCESS, then an error is issued and error_mark_node is
299 returned. If the ba_quiet bit is set, then no error is issued and
300 NULL_TREE is returned. */
303 lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
305 tree binfo = NULL; /* The binfo we've found so far. */
309 if (t == error_mark_node || base == error_mark_node)
312 *kind_ptr = bk_not_base;
313 return error_mark_node;
315 my_friendly_assert (TYPE_P (base), 20011127);
323 t_binfo = TYPE_BINFO (t);
325 /* Ensure that the types are instantiated. */
326 t = complete_type (TYPE_MAIN_VARIANT (t));
327 base = complete_type (TYPE_MAIN_VARIANT (base));
329 bk = lookup_base_r (t_binfo, base, access & ~ba_quiet,
334 case bk_inaccessible:
336 if (!(access & ba_quiet))
338 error ("`%T' is an inaccessible base of `%T'", base, t);
339 binfo = error_mark_node;
343 if (access != ba_any)
346 if (!(access & ba_quiet))
348 error ("`%T' is an ambiguous base of `%T'", base, t);
349 binfo = error_mark_node;
362 /* Worker function for get_dynamic_cast_base_type. */
365 dynamic_cast_base_recurse (tree subtype, tree binfo, bool is_via_virtual,
372 if (BINFO_TYPE (binfo) == subtype)
378 *offset_ptr = BINFO_OFFSET (binfo);
383 binfos = BINFO_BASETYPES (binfo);
384 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
385 for (i = 0; i < n_baselinks; i++)
387 tree base_binfo = TREE_VEC_ELT (binfos, i);
390 if (!TREE_VIA_PUBLIC (base_binfo))
392 rval = dynamic_cast_base_recurse
393 (subtype, base_binfo,
394 is_via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
398 worst = worst >= 0 ? -3 : worst;
401 else if (rval == -3 && worst != -1)
407 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
408 started from is related to the required TARGET type, in order to optimize
409 the inheritance graph search. This information is independent of the
410 current context, and ignores private paths, hence get_base_distance is
411 inappropriate. Return a TREE specifying the base offset, BOFF.
412 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
413 and there are no public virtual SUBTYPE bases.
414 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
415 BOFF == -2, SUBTYPE is not a public base.
416 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
419 get_dynamic_cast_base_type (tree subtype, tree target)
421 tree offset = NULL_TREE;
422 int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
427 offset = build_int_2 (boff, -1);
428 TREE_TYPE (offset) = ssizetype;
432 /* Search for a member with name NAME in a multiple inheritance lattice
433 specified by TYPE. If it does not exist, return NULL_TREE.
434 If the member is ambiguously referenced, return `error_mark_node'.
435 Otherwise, return the FIELD_DECL. */
437 /* Do a 1-level search for NAME as a member of TYPE. The caller must
438 figure out whether it can access this field. (Since it is only one
439 level, this is reasonable.) */
442 lookup_field_1 (tree type, tree name)
446 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
447 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
448 || TREE_CODE (type) == TYPENAME_TYPE)
449 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
450 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
451 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
452 the code often worked even when we treated the index as a list
454 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
458 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
459 && DECL_SORTED_FIELDS (TYPE_NAME (type)))
461 tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
462 int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
469 #ifdef GATHER_STATISTICS
471 #endif /* GATHER_STATISTICS */
473 if (DECL_NAME (fields[i]) > name)
475 else if (DECL_NAME (fields[i]) < name)
479 /* We might have a nested class and a field with the
480 same name; we sorted them appropriately via
481 field_decl_cmp, so just look for the last field with
484 && DECL_NAME (fields[i+1]) == name)
492 field = TYPE_FIELDS (type);
494 #ifdef GATHER_STATISTICS
495 n_calls_lookup_field_1++;
496 #endif /* GATHER_STATISTICS */
499 #ifdef GATHER_STATISTICS
501 #endif /* GATHER_STATISTICS */
502 my_friendly_assert (DECL_P (field), 0);
503 if (DECL_NAME (field) == NULL_TREE
504 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
506 tree temp = lookup_field_1 (TREE_TYPE (field), name);
510 if (TREE_CODE (field) == USING_DECL)
511 /* For now, we're just treating member using declarations as
512 old ARM-style access declarations. Thus, there's no reason
513 to return a USING_DECL, and the rest of the compiler can't
514 handle it. Once the class is defined, these are purged
515 from TYPE_FIELDS anyhow; see handle_using_decl. */
517 else if (DECL_NAME (field) == name)
519 field = TREE_CHAIN (field);
522 if (name == vptr_identifier)
524 /* Give the user what s/he thinks s/he wants. */
525 if (TYPE_POLYMORPHIC_P (type))
526 return TYPE_VFIELD (type);
531 /* There are a number of cases we need to be aware of here:
532 current_class_type current_function_decl
539 Those last two make life interesting. If we're in a function which is
540 itself inside a class, we need decls to go into the fn's decls (our
541 second case below). But if we're in a class and the class itself is
542 inside a function, we need decls to go into the decls for the class. To
543 achieve this last goal, we must see if, when both current_class_ptr and
544 current_function_decl are set, the class was declared inside that
545 function. If so, we know to put the decls into the class's scope. */
550 if (current_function_decl == NULL_TREE)
551 return current_class_type;
552 if (current_class_type == NULL_TREE)
553 return current_function_decl;
554 if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
555 && same_type_p (DECL_CONTEXT (current_function_decl),
557 || (DECL_FRIEND_CONTEXT (current_function_decl)
558 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
559 current_class_type)))
560 return current_function_decl;
562 return current_class_type;
565 /* Returns nonzero if we are currently in a function scope. Note
566 that this function returns zero if we are within a local class, but
567 not within a member function body of the local class. */
570 at_function_scope_p ()
572 tree cs = current_scope ();
573 return cs && TREE_CODE (cs) == FUNCTION_DECL;
576 /* Returns true if the innermost active scope is a class scope. */
581 tree cs = current_scope ();
582 return cs && TYPE_P (cs);
585 /* Return the scope of DECL, as appropriate when doing name-lookup. */
588 context_for_name_lookup (tree decl)
592 For the purposes of name lookup, after the anonymous union
593 definition, the members of the anonymous union are considered to
594 have been defined in the scope in which the anonymous union is
596 tree context = DECL_CONTEXT (decl);
598 while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
599 context = TYPE_CONTEXT (context);
601 context = global_namespace;
606 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
610 canonical_binfo (tree binfo)
612 return (TREE_VIA_VIRTUAL (binfo)
613 ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
616 /* A queue function that simply ensures that we walk into the
617 canonical versions of virtual bases. */
620 dfs_canonical_queue (tree binfo, void *data ATTRIBUTE_UNUSED)
622 return canonical_binfo (binfo);
625 /* Called via dfs_walk from assert_canonical_unmarked. */
628 dfs_assert_unmarked_p (tree binfo, void *data ATTRIBUTE_UNUSED)
630 my_friendly_assert (!BINFO_MARKED (binfo), 0);
634 /* Asserts that all the nodes below BINFO (using the canonical
635 versions of virtual bases) are unmarked. */
638 assert_canonical_unmarked (tree binfo)
640 dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
643 /* If BINFO is marked, return a canonical version of BINFO.
644 Otherwise, return NULL_TREE. */
647 shared_marked_p (tree binfo, void *data)
649 binfo = canonical_binfo (binfo);
650 return markedp (binfo, data);
653 /* If BINFO is not marked, return a canonical version of BINFO.
654 Otherwise, return NULL_TREE. */
657 shared_unmarked_p (tree binfo, void *data)
659 binfo = canonical_binfo (binfo);
660 return unmarkedp (binfo, data);
663 /* The accessibility routines use BINFO_ACCESS for scratch space
664 during the computation of the accssibility of some declaration. */
666 #define BINFO_ACCESS(NODE) \
667 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
669 /* Set the access associated with NODE to ACCESS. */
671 #define SET_BINFO_ACCESS(NODE, ACCESS) \
672 ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0), \
673 (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
675 /* Called from access_in_type via dfs_walk. Calculate the access to
676 DATA (which is really a DECL) in BINFO. */
679 dfs_access_in_type (tree binfo, void *data)
681 tree decl = (tree) data;
682 tree type = BINFO_TYPE (binfo);
683 access_kind access = ak_none;
685 if (context_for_name_lookup (decl) == type)
687 /* If we have desceneded to the scope of DECL, just note the
688 appropriate access. */
689 if (TREE_PRIVATE (decl))
691 else if (TREE_PROTECTED (decl))
692 access = ak_protected;
698 /* First, check for an access-declaration that gives us more
699 access to the DECL. The CONST_DECL for an enumeration
700 constant will not have DECL_LANG_SPECIFIC, and thus no
702 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
704 tree decl_access = purpose_member (type, DECL_ACCESS (decl));
706 access = ((access_kind)
707 TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
716 /* Otherwise, scan our baseclasses, and pick the most favorable
718 binfos = BINFO_BASETYPES (binfo);
719 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
720 for (i = 0; i < n_baselinks; ++i)
722 tree base_binfo = TREE_VEC_ELT (binfos, i);
723 access_kind base_access
724 = BINFO_ACCESS (canonical_binfo (base_binfo));
726 if (base_access == ak_none || base_access == ak_private)
727 /* If it was not accessible in the base, or only
728 accessible as a private member, we can't access it
730 base_access = ak_none;
731 else if (TREE_VIA_PROTECTED (base_binfo))
732 /* Public and protected members in the base are
734 base_access = ak_protected;
735 else if (!TREE_VIA_PUBLIC (base_binfo))
736 /* Public and protected members in the base are
738 base_access = ak_private;
740 /* See if the new access, via this base, gives more
741 access than our previous best access. */
742 if (base_access != ak_none
743 && (base_access == ak_public
744 || (base_access == ak_protected
745 && access != ak_public)
746 || (base_access == ak_private
747 && access == ak_none)))
749 access = base_access;
751 /* If the new access is public, we can't do better. */
752 if (access == ak_public)
759 /* Note the access to DECL in TYPE. */
760 SET_BINFO_ACCESS (binfo, access);
762 /* Mark TYPE as visited so that if we reach it again we do not
763 duplicate our efforts here. */
764 SET_BINFO_MARKED (binfo);
769 /* Return the access to DECL in TYPE. */
772 access_in_type (tree type, tree decl)
774 tree binfo = TYPE_BINFO (type);
776 /* We must take into account
780 If a name can be reached by several paths through a multiple
781 inheritance graph, the access is that of the path that gives
784 The algorithm we use is to make a post-order depth-first traversal
785 of the base-class hierarchy. As we come up the tree, we annotate
786 each node with the most lenient access. */
787 dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
788 dfs_walk (binfo, dfs_unmark, shared_marked_p, 0);
789 assert_canonical_unmarked (binfo);
791 return BINFO_ACCESS (binfo);
794 /* Called from dfs_accessible_p via dfs_walk. */
797 dfs_accessible_queue_p (tree binfo, void *data ATTRIBUTE_UNUSED)
799 if (BINFO_MARKED (binfo))
802 /* If this class is inherited via private or protected inheritance,
803 then we can't see it, unless we are a friend of the subclass. */
804 if (!TREE_VIA_PUBLIC (binfo)
805 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
809 return canonical_binfo (binfo);
812 /* Called from dfs_accessible_p via dfs_walk. */
815 dfs_accessible_p (tree binfo, void *data)
817 int protected_ok = data != 0;
820 SET_BINFO_MARKED (binfo);
821 access = BINFO_ACCESS (binfo);
822 if (access == ak_public || (access == ak_protected && protected_ok))
824 else if (access != ak_none
825 && is_friend (BINFO_TYPE (binfo), current_scope ()))
831 /* Returns nonzero if it is OK to access DECL through an object
832 indiated by BINFO in the context of DERIVED. */
835 protected_accessible_p (tree decl, tree derived, tree binfo)
839 /* We're checking this clause from [class.access.base]
841 m as a member of N is protected, and the reference occurs in a
842 member or friend of class N, or in a member or friend of a
843 class P derived from N, where m as a member of P is private or
846 Here DERIVED is a possible P and DECL is m. accessible_p will
847 iterate over various values of N, but the access to m in DERIVED
850 Note that I believe that the passage above is wrong, and should read
851 "...is private or protected or public"; otherwise you get bizarre results
852 whereby a public using-decl can prevent you from accessing a protected
853 member of a base. (jason 2000/02/28) */
855 /* If DERIVED isn't derived from m's class, then it can't be a P. */
856 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
859 access = access_in_type (derived, decl);
861 /* If m is inaccessible in DERIVED, then it's not a P. */
862 if (access == ak_none)
867 When a friend or a member function of a derived class references
868 a protected nonstatic member of a base class, an access check
869 applies in addition to those described earlier in clause
870 _class.access_) Except when forming a pointer to member
871 (_expr.unary.op_), the access must be through a pointer to,
872 reference to, or object of the derived class itself (or any class
873 derived from that class) (_expr.ref_). If the access is to form
874 a pointer to member, the nested-name-specifier shall name the
875 derived class (or any class derived from that class). */
876 if (DECL_NONSTATIC_MEMBER_P (decl))
878 /* We can tell through what the reference is occurring by
879 chasing BINFO up to the root. */
881 while (BINFO_INHERITANCE_CHAIN (t))
882 t = BINFO_INHERITANCE_CHAIN (t);
884 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
891 /* Returns nonzero if SCOPE is a friend of a type which would be able
892 to access DECL through the object indicated by BINFO. */
895 friend_accessible_p (tree scope, tree decl, tree binfo)
897 tree befriending_classes;
903 if (TREE_CODE (scope) == FUNCTION_DECL
904 || DECL_FUNCTION_TEMPLATE_P (scope))
905 befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
906 else if (TYPE_P (scope))
907 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
911 for (t = befriending_classes; t; t = TREE_CHAIN (t))
912 if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
915 /* Nested classes are implicitly friends of their enclosing types, as
916 per core issue 45 (this is a change from the standard). */
918 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
919 if (protected_accessible_p (decl, t, binfo))
922 if (TREE_CODE (scope) == FUNCTION_DECL
923 || DECL_FUNCTION_TEMPLATE_P (scope))
925 /* Perhaps this SCOPE is a member of a class which is a
927 if (DECL_CLASS_SCOPE_P (decl)
928 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
931 /* Or an instantiation of something which is a friend. */
932 if (DECL_TEMPLATE_INFO (scope))
933 return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
935 else if (CLASSTYPE_TEMPLATE_INFO (scope))
936 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
941 /* DECL is a declaration from a base class of TYPE, which was the
942 class used to name DECL. Return nonzero if, in the current
943 context, DECL is accessible. If TYPE is actually a BINFO node,
944 then we can tell in what context the access is occurring by looking
945 at the most derived class along the path indicated by BINFO. */
948 accessible_p (tree type, tree decl)
953 /* Nonzero if it's OK to access DECL if it has protected
954 accessibility in TYPE. */
955 int protected_ok = 0;
957 /* If we're not checking access, everything is accessible. */
958 if (!scope_chain->check_access)
961 /* If this declaration is in a block or namespace scope, there's no
963 if (!TYPE_P (context_for_name_lookup (decl)))
969 type = BINFO_TYPE (type);
972 binfo = TYPE_BINFO (type);
974 /* [class.access.base]
976 A member m is accessible when named in class N if
978 --m as a member of N is public, or
980 --m as a member of N is private, and the reference occurs in a
981 member or friend of class N, or
983 --m as a member of N is protected, and the reference occurs in a
984 member or friend of class N, or in a member or friend of a
985 class P derived from N, where m as a member of P is private or
988 --there exists a base class B of N that is accessible at the point
989 of reference, and m is accessible when named in class B.
991 We walk the base class hierarchy, checking these conditions. */
993 /* Figure out where the reference is occurring. Check to see if
994 DECL is private or protected in this scope, since that will
995 determine whether protected access is allowed. */
996 if (current_class_type)
997 protected_ok = protected_accessible_p (decl, current_class_type, binfo);
999 /* Now, loop through the classes of which we are a friend. */
1001 protected_ok = friend_accessible_p (current_scope (), decl, binfo);
1003 /* Standardize the binfo that access_in_type will use. We don't
1004 need to know what path was chosen from this point onwards. */
1005 binfo = TYPE_BINFO (type);
1007 /* Compute the accessibility of DECL in the class hierarchy
1008 dominated by type. */
1009 access_in_type (type, decl);
1010 /* Walk the hierarchy again, looking for a base class that allows
1012 t = dfs_walk (binfo, dfs_accessible_p,
1013 dfs_accessible_queue_p,
1014 protected_ok ? &protected_ok : 0);
1015 /* Clear any mark bits. Note that we have to walk the whole tree
1016 here, since we have aborted the previous walk from some point
1017 deep in the tree. */
1018 dfs_walk (binfo, dfs_unmark, dfs_canonical_queue, 0);
1019 assert_canonical_unmarked (binfo);
1021 return t != NULL_TREE;
1024 /* Recursive helper funciton for is_subobject_of_p; see that routine
1025 for documentation of the parameters. */
1028 is_subobject_of_p_1 (tree parent, tree binfo, tree most_derived)
1033 if (parent == binfo)
1036 binfos = BINFO_BASETYPES (binfo);
1037 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1039 /* Iterate through the base types. */
1040 for (i = 0; i < n_baselinks; i++)
1042 tree base_binfo = TREE_VEC_ELT (binfos, i);
1045 base_type = TREE_TYPE (base_binfo);
1046 if (!CLASS_TYPE_P (base_type))
1047 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1048 class there's no way to descend into it. */
1051 /* Avoid walking into the same virtual base more than once. */
1052 if (TREE_VIA_VIRTUAL (base_binfo))
1054 if (CLASSTYPE_MARKED4 (base_type))
1056 SET_CLASSTYPE_MARKED4 (base_type);
1057 base_binfo = binfo_for_vbase (base_type, most_derived);
1060 if (is_subobject_of_p_1 (parent, base_binfo, most_derived))
1066 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1067 found as a base class and sub-object of the object denoted by
1068 BINFO. MOST_DERIVED is the most derived type of the hierarchy being
1072 is_subobject_of_p (tree parent, tree binfo, tree most_derived)
1077 result = is_subobject_of_p_1 (parent, binfo, most_derived);
1078 /* Clear the mark bits on virtual bases. */
1079 for (vbase = CLASSTYPE_VBASECLASSES (most_derived);
1081 vbase = TREE_CHAIN (vbase))
1082 CLEAR_CLASSTYPE_MARKED4 (TREE_TYPE (TREE_VALUE (vbase)));
1087 struct lookup_field_info {
1088 /* The type in which we're looking. */
1090 /* The name of the field for which we're looking. */
1092 /* If non-NULL, the current result of the lookup. */
1094 /* The path to RVAL. */
1096 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1099 /* If nonzero, we are looking for types, not data members. */
1101 /* If nonzero, RVAL was found by looking through a dependent base. */
1102 int from_dep_base_p;
1103 /* If something went wrong, a message indicating what. */
1107 /* Returns nonzero if BINFO is not hidden by the value found by the
1108 lookup so far. If BINFO is hidden, then there's no need to look in
1109 it. DATA is really a struct lookup_field_info. Called from
1110 lookup_field via breadth_first_search. */
1113 lookup_field_queue_p (tree binfo, void *data)
1115 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1117 /* Don't look for constructors or destructors in base classes. */
1118 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
1121 /* If this base class is hidden by the best-known value so far, we
1122 don't need to look. */
1123 binfo = CANONICAL_BINFO (binfo, lfi->type);
1124 if (!lfi->from_dep_base_p && lfi->rval_binfo
1125 && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1131 /* Within the scope of a template class, you can refer to the to the
1132 current specialization with the name of the template itself. For
1135 template <typename T> struct S { S* sp; }
1137 Returns nonzero if DECL is such a declaration in a class TYPE. */
1140 template_self_reference_p (tree type, tree decl)
1142 return (CLASSTYPE_USE_TEMPLATE (type)
1143 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
1144 && TREE_CODE (decl) == TYPE_DECL
1145 && DECL_ARTIFICIAL (decl)
1146 && DECL_NAME (decl) == constructor_name (type));
1150 /* Nonzero for a class member means that it is shared between all objects
1153 [class.member.lookup]:If the resulting set of declarations are not all
1154 from sub-objects of the same type, or the set has a nonstatic member
1155 and includes members from distinct sub-objects, there is an ambiguity
1156 and the program is ill-formed.
1158 This function checks that T contains no nonstatic members. */
1161 shared_member_p (tree t)
1163 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
1164 || TREE_CODE (t) == CONST_DECL)
1166 if (is_overloaded_fn (t))
1168 for (; t; t = OVL_NEXT (t))
1170 tree fn = OVL_CURRENT (t);
1171 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1179 /* DATA is really a struct lookup_field_info. Look for a field with
1180 the name indicated there in BINFO. If this function returns a
1181 non-NULL value it is the result of the lookup. Called from
1182 lookup_field via breadth_first_search. */
1185 lookup_field_r (tree binfo, void *data)
1187 struct lookup_field_info *lfi = (struct lookup_field_info *) data;
1188 tree type = BINFO_TYPE (binfo);
1189 tree nval = NULL_TREE;
1190 int from_dep_base_p;
1192 /* First, look for a function. There can't be a function and a data
1193 member with the same name, and if there's a function and a type
1194 with the same name, the type is hidden by the function. */
1195 if (!lfi->want_type)
1197 int idx = lookup_fnfields_1 (type, lfi->name);
1199 nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
1203 /* Look for a data member or type. */
1204 nval = lookup_field_1 (type, lfi->name);
1206 /* If there is no declaration with the indicated name in this type,
1207 then there's nothing to do. */
1211 /* If we're looking up a type (as with an elaborated type specifier)
1212 we ignore all non-types we find. */
1213 if (lfi->want_type && TREE_CODE (nval) != TYPE_DECL
1214 && !DECL_CLASS_TEMPLATE_P (nval))
1216 if (lfi->name == TYPE_IDENTIFIER (type))
1218 /* If the aggregate has no user defined constructors, we allow
1219 it to have fields with the same name as the enclosing type.
1220 If we are looking for that name, find the corresponding
1222 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval))
1223 if (DECL_NAME (nval) == lfi->name
1224 && TREE_CODE (nval) == TYPE_DECL)
1231 nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
1233 nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
1239 /* You must name a template base class with a template-id. */
1240 if (!same_type_p (type, lfi->type)
1241 && template_self_reference_p (type, nval))
1244 from_dep_base_p = dependent_base_p (binfo);
1245 if (lfi->from_dep_base_p && !from_dep_base_p)
1247 /* If the new declaration is not found via a dependent base, and
1248 the old one was, then we must prefer the new one. We weren't
1249 really supposed to be able to find the old one, so we don't
1250 want to be affected by a specialization. Consider:
1252 struct B { typedef int I; };
1253 template <typename T> struct D1 : virtual public B {};
1254 template <typename T> struct D :
1255 public D1, virtual pubic B { I i; };
1257 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1258 D1 is specialized. */
1259 lfi->from_dep_base_p = 0;
1260 lfi->rval = NULL_TREE;
1261 lfi->rval_binfo = NULL_TREE;
1262 lfi->ambiguous = NULL_TREE;
1265 else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
1266 /* Similarly, if the old declaration was not found via a dependent
1267 base, and the new one is, ignore the new one. */
1270 /* If the lookup already found a match, and the new value doesn't
1271 hide the old one, we might have an ambiguity. */
1272 if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
1274 if (nval == lfi->rval && shared_member_p (nval))
1275 /* The two things are really the same. */
1277 else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
1278 /* The previous value hides the new one. */
1282 /* We have a real ambiguity. We keep a chain of all the
1284 if (!lfi->ambiguous && lfi->rval)
1286 /* This is the first time we noticed an ambiguity. Add
1287 what we previously thought was a reasonable candidate
1289 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
1290 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1293 /* Add the new value. */
1294 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
1295 TREE_TYPE (lfi->ambiguous) = error_mark_node;
1296 lfi->errstr = "request for member `%D' is ambiguous";
1301 if (from_dep_base_p && TREE_CODE (nval) == TYPE_DECL
1302 /* We need to return a member template class so we can
1303 define partial specializations. Is there a better
1305 && !DECL_CLASS_TEMPLATE_P (nval))
1306 /* The thing we're looking for isn't a type, so the implicit
1307 typename extension doesn't apply, so we just pretend we
1308 didn't find anything. */
1312 lfi->from_dep_base_p = from_dep_base_p;
1313 lfi->rval_binfo = binfo;
1319 /* Return a "baselink" which BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1320 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1321 FUNCTIONS, and OPTYPE respectively. */
1324 build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
1328 my_friendly_assert (TREE_CODE (functions) == FUNCTION_DECL
1329 || TREE_CODE (functions) == TEMPLATE_DECL
1330 || TREE_CODE (functions) == TEMPLATE_ID_EXPR
1331 || TREE_CODE (functions) == OVERLOAD,
1333 my_friendly_assert (!optype || TYPE_P (optype), 20020730);
1334 my_friendly_assert (TREE_TYPE (functions), 20020805);
1336 baselink = make_node (BASELINK);
1337 TREE_TYPE (baselink) = TREE_TYPE (functions);
1338 BASELINK_BINFO (baselink) = binfo;
1339 BASELINK_ACCESS_BINFO (baselink) = access_binfo;
1340 BASELINK_FUNCTIONS (baselink) = functions;
1341 BASELINK_OPTYPE (baselink) = optype;
1346 /* Look for a member named NAME in an inheritance lattice dominated by
1347 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1348 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1349 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1350 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1351 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1352 TREE_VALUEs are the list of ambiguous candidates.
1354 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1356 If nothing can be found return NULL_TREE and do not issue an error. */
1359 lookup_member (tree xbasetype, tree name, int protect, bool want_type)
1361 tree rval, rval_binfo = NULL_TREE;
1362 tree type = NULL_TREE, basetype_path = NULL_TREE;
1363 struct lookup_field_info lfi;
1365 /* rval_binfo is the binfo associated with the found member, note,
1366 this can be set with useful information, even when rval is not
1367 set, because it must deal with ALL members, not just non-function
1368 members. It is used for ambiguity checking and the hidden
1369 checks. Whereas rval is only set if a proper (not hidden)
1370 non-function member is found. */
1372 const char *errstr = 0;
1374 if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
1375 && IDENTIFIER_CLASS_VALUE (name))
1377 tree field = IDENTIFIER_CLASS_VALUE (name);
1378 if (TREE_CODE (field) != FUNCTION_DECL
1379 && ! (want_type && TREE_CODE (field) != TYPE_DECL))
1380 /* We're in the scope of this class, and the value has already
1381 been looked up. Just return the cached value. */
1385 if (TREE_CODE (xbasetype) == TREE_VEC)
1387 type = BINFO_TYPE (xbasetype);
1388 basetype_path = xbasetype;
1390 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
1393 basetype_path = TYPE_BINFO (type);
1394 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
1400 complete_type (type);
1402 #ifdef GATHER_STATISTICS
1403 n_calls_lookup_field++;
1404 #endif /* GATHER_STATISTICS */
1406 memset ((PTR) &lfi, 0, sizeof (lfi));
1409 lfi.want_type = want_type;
1410 bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
1412 rval_binfo = lfi.rval_binfo;
1414 type = BINFO_TYPE (rval_binfo);
1415 errstr = lfi.errstr;
1417 /* If we are not interested in ambiguities, don't report them;
1418 just return NULL_TREE. */
1419 if (!protect && lfi.ambiguous)
1425 return lfi.ambiguous;
1432 In the case of overloaded function names, access control is
1433 applied to the function selected by overloaded resolution. */
1434 if (rval && protect && !is_overloaded_fn (rval)
1435 && !enforce_access (xbasetype, rval))
1436 return error_mark_node;
1438 if (errstr && protect)
1440 error (errstr, name, type);
1442 print_candidates (lfi.ambiguous);
1443 rval = error_mark_node;
1446 /* If the thing we found was found via the implicit typename
1447 extension, build the typename type. */
1448 if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
1451 if (rval && is_overloaded_fn (rval))
1452 rval = build_baselink (rval_binfo, basetype_path, rval,
1453 (IDENTIFIER_TYPENAME_P (name)
1454 ? TREE_TYPE (name): NULL_TREE));
1458 /* Like lookup_member, except that if we find a function member we
1459 return NULL_TREE. */
1462 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
1464 tree rval = lookup_member (xbasetype, name, protect, want_type);
1466 /* Ignore functions. */
1467 if (rval && BASELINK_P (rval))
1473 /* Like lookup_member, except that if we find a non-function member we
1474 return NULL_TREE. */
1477 lookup_fnfields (tree xbasetype, tree name, int protect)
1479 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false);
1481 /* Ignore non-functions. */
1482 if (rval && !BASELINK_P (rval))
1488 /* TYPE is a class type. Return the index of the fields within
1489 the method vector with name NAME, or -1 is no such field exists. */
1492 lookup_fnfields_1 (tree type, tree name)
1494 tree method_vec = (CLASS_TYPE_P (type)
1495 ? CLASSTYPE_METHOD_VEC (type)
1498 if (method_vec != 0)
1501 register tree *methods = &TREE_VEC_ELT (method_vec, 0);
1502 int len = TREE_VEC_LENGTH (method_vec);
1505 #ifdef GATHER_STATISTICS
1506 n_calls_lookup_fnfields_1++;
1507 #endif /* GATHER_STATISTICS */
1509 /* Constructors are first... */
1510 if (name == ctor_identifier)
1511 return (methods[CLASSTYPE_CONSTRUCTOR_SLOT]
1512 ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
1513 /* and destructors are second. */
1514 if (name == dtor_identifier)
1515 return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
1516 ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
1518 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1519 i < len && methods[i];
1522 #ifdef GATHER_STATISTICS
1523 n_outer_fields_searched++;
1524 #endif /* GATHER_STATISTICS */
1526 tmp = OVL_CURRENT (methods[i]);
1527 if (DECL_NAME (tmp) == name)
1530 /* If the type is complete and we're past the conversion ops,
1531 switch to binary search. */
1532 if (! DECL_CONV_FN_P (tmp)
1533 && COMPLETE_TYPE_P (type))
1535 int lo = i + 1, hi = len;
1541 #ifdef GATHER_STATISTICS
1542 n_outer_fields_searched++;
1543 #endif /* GATHER_STATISTICS */
1545 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
1549 else if (tmp < name)
1558 /* If we didn't find it, it might have been a template
1559 conversion operator to a templated type. If there are any,
1560 such template conversion operators will all be overloaded on
1561 the first conversion slot. (Note that we don't look for this
1562 case above so that we will always find specializations
1564 if (IDENTIFIER_TYPENAME_P (name))
1566 i = CLASSTYPE_FIRST_CONVERSION_SLOT;
1567 if (i < len && methods[i])
1569 tmp = OVL_CURRENT (methods[i]);
1570 if (TREE_CODE (tmp) == TEMPLATE_DECL
1571 && DECL_TEMPLATE_CONV_FN_P (tmp))
1580 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1581 the class or namespace used to qualify the name. CONTEXT_CLASS is
1582 the class corresponding to the object in which DECL will be used.
1583 Return a possibly modified version of DECL that takes into account
1586 In particular, consider an expression like `B::m' in the context of
1587 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1588 then the most derived class indicated by the BASELINK_BINFO will be
1589 `B', not `D'. This function makes that adjustment. */
1592 adjust_result_of_qualified_name_lookup (tree decl,
1593 tree qualifying_scope,
1596 if (context_class && CLASS_TYPE_P (qualifying_scope)
1597 && DERIVED_FROM_P (qualifying_scope, context_class)
1598 && BASELINK_P (decl))
1602 my_friendly_assert (CLASS_TYPE_P (context_class), 20020808);
1604 /* Look for the QUALIFYING_SCOPE as a base of the
1605 CONTEXT_CLASS. If QUALIFYING_SCOPE is ambiguous, we cannot
1606 be sure yet than an error has occurred; perhaps the function
1607 chosen by overload resolution will be static. */
1608 base = lookup_base (context_class, qualifying_scope,
1609 ba_ignore | ba_quiet, NULL);
1612 BASELINK_ACCESS_BINFO (decl) = base;
1613 BASELINK_BINFO (decl)
1614 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
1615 ba_ignore | ba_quiet,
1624 /* Start with enough room for ten concurrent base classes. That
1625 will be enough for most hierarchies. */
1626 #define BFS_WALK_INITIAL_QUEUE_SIZE 10
1628 /* Subroutine of bfs_walk; enlarges the buffer it uses for its
1631 grow_bfs_bases (tree **basep, size_t *sizep, size_t *headp)
1634 size_t size = *sizep;
1635 size_t head = *headp;
1637 /* If the size is BFS_WALK_INITIAL_QUEUE_SIZE, the old array is on
1639 if (size == BFS_WALK_INITIAL_QUEUE_SIZE)
1641 base = xmalloc (size * 2 * sizeof(tree));
1642 memcpy (base, *basep, size * sizeof(tree));
1645 base = xrealloc (*basep, size * 2 * sizeof(tree));
1650 /* Shift all the elements between head and the former end of the
1651 array, opening up a gap between tail and head. If head==0 we
1652 don't need to do anything to achieve this. */
1655 memmove (&base[head + size], &base[head],
1656 (size - head) * sizeof (tree));
1657 *headp = head + size;
1661 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1662 type in the hierarchy, in a breadth-first preorder traversal.
1663 If it ever returns a non-NULL value, that value is immediately
1664 returned and the walk is terminated. At each node, FN is passed a
1665 BINFO indicating the path from the curently visited base-class to
1666 TYPE. Before each base-class is walked QFN is called. If the
1667 value returned is nonzero, the base-class is walked; otherwise it
1668 is not. If QFN is NULL, it is treated as a function which always
1669 returns 1. Both FN and QFN are passed the DATA whenever they are
1672 Implementation notes: Uses a circular queue, which starts off on
1673 the stack but gets moved to the malloc arena if it needs to be
1674 enlarged. The underflow and overflow conditions are
1675 indistinguishable except by context: if head == tail and we just
1676 moved the head pointer, the queue is empty, but if we just moved
1677 the tail pointer, the queue is full. Base class vectors are only
1678 put on the queue if they are nonempty, which is why it's safe to
1679 use do-while for the inner loop. */
1682 bfs_walk (tree binfo, tree (*fn) (tree, void *),
1683 tree (*qfn) (tree, void *), void *data)
1685 tree rval = NULL_TREE;
1687 tree bfs_bases_initial[BFS_WALK_INITIAL_QUEUE_SIZE];
1688 /* A circular queue of the base classes of BINFO. These will be
1689 built up in breadth-first order, except where QFN prunes the
1692 size_t bfs_bases_size = BFS_WALK_INITIAL_QUEUE_SIZE;
1693 tree *bfs_bases = bfs_bases_initial;
1695 /* Is the first one what we're looking for? If so, we're done. */
1696 rval = fn (binfo, data);
1700 /* If it has no base types, we are also done. */
1701 if (BINFO_BASETYPES (binfo) == 0
1702 || TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 0)
1705 /* Otherwise, initialize the queue with its basetypes vector
1709 bfs_bases[tail++] = BINFO_BASETYPES (binfo);
1716 binfos = bfs_bases[head++];
1717 if (head == bfs_bases_size)
1721 n_baselinks = TREE_VEC_LENGTH (binfos);
1724 binfo = TREE_VEC_ELT (binfos, i);
1728 binfo = qfn (binfo, data);
1732 rval = fn (binfo, data);
1736 if (BINFO_BASETYPES (binfo) == 0
1737 || TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 0)
1740 bfs_bases[tail++] = BINFO_BASETYPES (binfo);
1741 if (tail == bfs_bases_size)
1744 grow_bfs_bases (&bfs_bases, &bfs_bases_size, &head);
1746 while (i < n_baselinks);
1748 while (head != tail);
1751 if (bfs_bases != bfs_bases_initial)
1756 /* Exactly like bfs_walk, except that a depth-first traversal is
1757 performed, and PREFN is called in preorder, while POSTFN is called
1761 dfs_walk_real (tree binfo,
1762 tree (*prefn) (tree, void *), tree (*postfn) (tree, void *),
1763 tree (*qfn) (tree, void *), void *data)
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 (tree binfo, tree (*fn) (tree, void *),
1808 tree (*qfn) (tree, void *), void *data)
1810 return dfs_walk_real (binfo, 0, fn, qfn, data);
1813 /* Check that virtual overrider OVERRIDER is acceptable for base function
1814 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1817 check_final_overrider (tree overrider, tree basefn)
1819 tree over_type = TREE_TYPE (overrider);
1820 tree base_type = TREE_TYPE (basefn);
1821 tree over_return = TREE_TYPE (over_type);
1822 tree base_return = TREE_TYPE (base_type);
1823 tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
1824 tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
1827 if (same_type_p (base_return, over_return))
1829 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
1830 || (TREE_CODE (base_return) == TREE_CODE (over_return)
1831 && POINTER_TYPE_P (base_return)))
1833 /* Potentially covariant. */
1834 unsigned base_quals, over_quals;
1836 fail = !POINTER_TYPE_P (base_return);
1839 fail = cp_type_quals (base_return) != cp_type_quals (over_return);
1841 base_return = TREE_TYPE (base_return);
1842 over_return = TREE_TYPE (over_return);
1844 base_quals = cp_type_quals (base_return);
1845 over_quals = cp_type_quals (over_return);
1847 if ((base_quals & over_quals) != over_quals)
1850 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
1852 tree binfo = lookup_base (over_return, base_return,
1853 ba_check | ba_quiet, NULL);
1859 && can_convert (TREE_TYPE (base_type), TREE_TYPE (over_type)))
1860 /* GNU extension, allow trivial pointer conversions such as
1861 converting to void *, or qualification conversion. */
1863 /* can_convert will permit user defined conversion from a
1864 (reference to) class type. We must reject them. */
1865 over_return = TREE_TYPE (over_type);
1866 if (TREE_CODE (over_return) == REFERENCE_TYPE)
1867 over_return = TREE_TYPE (over_return);
1868 if (CLASS_TYPE_P (over_return))
1878 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
1884 cp_error_at ("invalid covariant return type for `%#D'", overrider);
1885 cp_error_at (" overriding `%#D'", basefn);
1889 cp_error_at ("conflicting return type specified for `%#D'",
1891 cp_error_at (" overriding `%#D'", basefn);
1893 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1894 DECL_CONTEXT (overrider));
1898 /* Check throw specifier is at least as strict. */
1899 if (!comp_except_specs (base_throw, over_throw, 0))
1901 if (!IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
1903 cp_error_at ("looser throw specifier for `%#F'", overrider);
1904 cp_error_at (" overriding `%#F'", basefn);
1905 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
1906 DECL_CONTEXT (overrider));
1914 /* Given a class TYPE, and a function decl FNDECL, look for
1915 virtual functions in TYPE's hierarchy which FNDECL overrides.
1916 We do not look in TYPE itself, only its bases.
1918 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1919 find that it overrides anything.
1921 We check that every function which is overridden, is correctly
1925 look_for_overrides (tree type, tree fndecl)
1927 tree binfo = TYPE_BINFO (type);
1928 tree basebinfos = BINFO_BASETYPES (binfo);
1929 int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
1933 for (ix = 0; ix != nbasebinfos; ix++)
1935 tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
1937 if (TYPE_POLYMORPHIC_P (basetype))
1938 found += look_for_overrides_r (basetype, fndecl);
1943 /* Look in TYPE for virtual functions with the same signature as
1947 look_for_overrides_here (tree type, tree fndecl)
1951 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
1952 ix = CLASSTYPE_DESTRUCTOR_SLOT;
1954 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
1957 tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
1959 for (; fns; fns = OVL_NEXT (fns))
1961 tree fn = OVL_CURRENT (fns);
1963 if (!DECL_VIRTUAL_P (fn))
1964 /* Not a virtual. */;
1965 else if (DECL_CONTEXT (fn) != type)
1966 /* Introduced with a using declaration. */;
1967 else if (DECL_STATIC_FUNCTION_P (fndecl))
1969 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
1970 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1971 if (compparms (TREE_CHAIN (btypes), dtypes))
1974 else if (same_signature_p (fndecl, fn))
1981 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1982 TYPE itself and its bases. */
1985 look_for_overrides_r (tree type, tree fndecl)
1987 tree fn = look_for_overrides_here (type, fndecl);
1990 if (DECL_STATIC_FUNCTION_P (fndecl))
1992 /* A static member function cannot match an inherited
1993 virtual member function. */
1994 cp_error_at ("`%#D' cannot be declared", fndecl);
1995 cp_error_at (" since `%#D' declared in base class", fn);
1999 /* It's definitely virtual, even if not explicitly set. */
2000 DECL_VIRTUAL_P (fndecl) = 1;
2001 check_final_overrider (fndecl, fn);
2006 /* We failed to find one declared in this class. Look in its bases. */
2007 return look_for_overrides (type, fndecl);
2010 /* A queue function to use with dfs_walk that only walks into
2011 canonical bases. DATA should be the type of the complete object,
2012 or a TREE_LIST whose TREE_PURPOSE is the type of the complete
2013 object. By using this function as a queue function, you will walk
2014 over exactly those BINFOs that actually exist in the complete
2015 object, including those for virtual base classes. If you
2016 SET_BINFO_MARKED for each binfo you process, you are further
2017 guaranteed that you will walk into each virtual base class exactly
2021 dfs_unmarked_real_bases_queue_p (tree binfo, void *data)
2023 if (TREE_VIA_VIRTUAL (binfo))
2025 tree type = (tree) data;
2027 if (TREE_CODE (type) == TREE_LIST)
2028 type = TREE_PURPOSE (type);
2029 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2031 return unmarkedp (binfo, NULL);
2034 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
2035 that are marked, rather than unmarked. */
2038 dfs_marked_real_bases_queue_p (tree binfo, void *data)
2040 if (TREE_VIA_VIRTUAL (binfo))
2042 tree type = (tree) data;
2044 if (TREE_CODE (type) == TREE_LIST)
2045 type = TREE_PURPOSE (type);
2046 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
2048 return markedp (binfo, NULL);
2051 /* A queue function that skips all virtual bases (and their
2055 dfs_skip_vbases (tree binfo, void *data ATTRIBUTE_UNUSED)
2057 if (TREE_VIA_VIRTUAL (binfo))
2063 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2066 dfs_get_pure_virtuals (tree binfo, void *data)
2068 tree type = (tree) data;
2070 /* We're not interested in primary base classes; the derived class
2071 of which they are a primary base will contain the information we
2073 if (!BINFO_PRIMARY_P (binfo))
2077 for (virtuals = BINFO_VIRTUALS (binfo);
2079 virtuals = TREE_CHAIN (virtuals))
2080 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
2081 CLASSTYPE_PURE_VIRTUALS (type)
2082 = tree_cons (NULL_TREE, BV_FN (virtuals),
2083 CLASSTYPE_PURE_VIRTUALS (type));
2086 SET_BINFO_MARKED (binfo);
2091 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2094 get_pure_virtuals (tree type)
2098 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2099 is going to be overridden. */
2100 CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
2101 /* Now, run through all the bases which are not primary bases, and
2102 collect the pure virtual functions. We look at the vtable in
2103 each class to determine what pure virtual functions are present.
2104 (A primary base is not interesting because the derived class of
2105 which it is a primary base will contain vtable entries for the
2106 pure virtuals in the base class. */
2107 dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals,
2108 dfs_unmarked_real_bases_queue_p, type);
2109 dfs_walk (TYPE_BINFO (type), dfs_unmark,
2110 dfs_marked_real_bases_queue_p, type);
2112 /* Put the pure virtuals in dfs order. */
2113 CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
2115 for (vbases = CLASSTYPE_VBASECLASSES (type);
2117 vbases = TREE_CHAIN (vbases))
2121 for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
2123 virtuals = TREE_CHAIN (virtuals))
2125 tree base_fndecl = BV_FN (virtuals);
2126 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
2127 error ("`%#D' needs a final overrider", base_fndecl);
2132 /* DEPTH-FIRST SEARCH ROUTINES. */
2135 markedp (tree binfo, void *data ATTRIBUTE_UNUSED)
2137 return BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2141 unmarkedp (tree binfo, void *data ATTRIBUTE_UNUSED)
2143 return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
2147 marked_vtable_pathp (tree binfo, void *data ATTRIBUTE_UNUSED)
2149 return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2153 unmarked_vtable_pathp (tree binfo, void *data ATTRIBUTE_UNUSED)
2155 return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE;
2159 marked_pushdecls_p (tree binfo, void *data ATTRIBUTE_UNUSED)
2161 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2162 && !dependent_base_p (binfo)
2163 && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2167 unmarked_pushdecls_p (tree binfo, void *data ATTRIBUTE_UNUSED)
2169 return (CLASS_TYPE_P (BINFO_TYPE (binfo))
2170 && !dependent_base_p (binfo)
2171 && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
2174 /* The worker functions for `dfs_walk'. These do not need to
2175 test anything (vis a vis marking) if they are paired with
2176 a predicate function (above). */
2179 dfs_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
2181 CLEAR_BINFO_MARKED (binfo);
2185 /* get virtual base class types.
2186 This adds type to the vbase_types list in reverse dfs order.
2187 Ordering is very important, so don't change it. */
2190 dfs_get_vbase_types (tree binfo, void *data)
2192 tree type = (tree) data;
2194 if (TREE_VIA_VIRTUAL (binfo))
2195 CLASSTYPE_VBASECLASSES (type)
2196 = tree_cons (BINFO_TYPE (binfo),
2198 CLASSTYPE_VBASECLASSES (type));
2199 SET_BINFO_MARKED (binfo);
2203 /* Called via dfs_walk from mark_primary_bases. Builds the
2204 inheritance graph order list of BINFOs. */
2207 dfs_build_inheritance_graph_order (tree binfo, void *data)
2209 tree *last_binfo = (tree *) data;
2212 TREE_CHAIN (*last_binfo) = binfo;
2213 *last_binfo = binfo;
2214 SET_BINFO_MARKED (binfo);
2218 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
2221 get_vbase_types (tree type)
2225 CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
2226 dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
2227 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2228 reverse it so that we get normal dfs ordering. */
2229 CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
2230 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
2231 /* Thread the BINFOs in inheritance-graph order. */
2233 dfs_walk_real (TYPE_BINFO (type),
2234 dfs_build_inheritance_graph_order,
2238 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
2241 /* Called from find_vbase_instance via dfs_walk. */
2244 dfs_find_vbase_instance (tree binfo, void *data)
2246 tree base = TREE_VALUE ((tree) data);
2248 if (BINFO_PRIMARY_P (binfo)
2249 && same_type_p (BINFO_TYPE (binfo), base))
2255 /* Find the real occurrence of the virtual BASE (a class type) in the
2256 hierarchy dominated by TYPE. */
2259 find_vbase_instance (tree base, tree type)
2263 instance = binfo_for_vbase (base, type);
2264 if (!BINFO_PRIMARY_P (instance))
2267 return dfs_walk (TYPE_BINFO (type),
2268 dfs_find_vbase_instance,
2270 build_tree_list (type, base));
2274 /* Debug info for C++ classes can get very large; try to avoid
2275 emitting it everywhere.
2277 Note that this optimization wins even when the target supports
2278 BINCL (if only slightly), and reduces the amount of work for the
2282 maybe_suppress_debug_info (tree t)
2284 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2285 does not support name references between translation units. It supports
2286 symbolic references between translation units, but only within a single
2287 executable or shared library.
2289 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2290 that the type was never defined, so we only get the members we
2292 if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
2295 /* We might have set this earlier in cp_finish_decl. */
2296 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
2298 /* If we already know how we're handling this class, handle debug info
2300 if (CLASSTYPE_INTERFACE_KNOWN (t))
2302 if (CLASSTYPE_INTERFACE_ONLY (t))
2303 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2304 /* else don't set it. */
2306 /* If the class has a vtable, write out the debug info along with
2308 else if (TYPE_CONTAINS_VPTR_P (t))
2309 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
2311 /* Otherwise, just emit the debug info normally. */
2314 /* Note that we want debugging information for a base class of a class
2315 whose vtable is being emitted. Normally, this would happen because
2316 calling the constructor for a derived class implies calling the
2317 constructors for all bases, which involve initializing the
2318 appropriate vptr with the vtable for the base class; but in the
2319 presence of optimization, this initialization may be optimized
2320 away, so we tell finish_vtable_vardecl that we want the debugging
2321 information anyway. */
2324 dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
2326 tree t = BINFO_TYPE (binfo);
2328 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2333 /* Returns BINFO if we haven't already noted that we want debugging
2334 info for this base class. */
2337 dfs_debug_unmarkedp (tree binfo, void *data ATTRIBUTE_UNUSED)
2339 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo))
2340 ? binfo : NULL_TREE);
2343 /* Write out the debugging information for TYPE, whose vtable is being
2344 emitted. Also walk through our bases and note that we want to
2345 write out information for them. This avoids the problem of not
2346 writing any debug info for intermediate basetypes whose
2347 constructors, and thus the references to their vtables, and thus
2348 the vtables themselves, were optimized away. */
2351 note_debug_info_needed (tree type)
2353 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
2355 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
2356 rest_of_type_compilation (type, toplevel_bindings_p ());
2359 dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
2362 /* Subroutines of push_class_decls (). */
2364 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2365 because it (or one of the intermediate bases) depends on template parms. */
2368 dependent_base_p (tree binfo)
2370 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2372 if (currently_open_class (TREE_TYPE (binfo)))
2374 if (dependent_type_p (TREE_TYPE (binfo)))
2381 setup_class_bindings (tree name, int type_binding_p)
2383 tree type_binding = NULL_TREE;
2386 /* If we've already done the lookup for this declaration, we're
2388 if (IDENTIFIER_CLASS_VALUE (name))
2391 /* First, deal with the type binding. */
2394 type_binding = lookup_member (current_class_type, name,
2395 /*protect=*/2, /*want_type=*/true);
2396 if (TREE_CODE (type_binding) == TREE_LIST
2397 && TREE_TYPE (type_binding) == error_mark_node)
2398 /* NAME is ambiguous. */
2399 push_class_level_binding (name, type_binding);
2401 pushdecl_class_level (type_binding);
2404 /* Now, do the value binding. */
2405 value_binding = lookup_member (current_class_type, name,
2406 /*protect=*/2, /*want_type=*/false);
2409 && (TREE_CODE (value_binding) == TYPE_DECL
2410 || DECL_CLASS_TEMPLATE_P (value_binding)
2411 || (TREE_CODE (value_binding) == TREE_LIST
2412 && TREE_TYPE (value_binding) == error_mark_node
2413 && (TREE_CODE (TREE_VALUE (value_binding))
2415 /* We found a type-binding, even when looking for a non-type
2416 binding. This means that we already processed this binding
2418 else if (value_binding)
2420 if (TREE_CODE (value_binding) == TREE_LIST
2421 && TREE_TYPE (value_binding) == error_mark_node)
2422 /* NAME is ambiguous. */
2423 push_class_level_binding (name, value_binding);
2426 if (BASELINK_P (value_binding))
2427 /* NAME is some overloaded functions. */
2428 value_binding = BASELINK_FUNCTIONS (value_binding);
2429 pushdecl_class_level (value_binding);
2434 /* Push class-level declarations for any names appearing in BINFO that
2438 dfs_push_type_decls (tree binfo, void *data ATTRIBUTE_UNUSED)
2443 type = BINFO_TYPE (binfo);
2444 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2445 if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
2446 && !(!same_type_p (type, current_class_type)
2447 && template_self_reference_p (type, fields)))
2448 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
2450 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2451 DERIVED_FROM_P, which calls get_base_distance. */
2452 SET_BINFO_PUSHDECLS_MARKED (binfo);
2457 /* Push class-level declarations for any names appearing in BINFO that
2458 are not TYPE_DECLS. */
2461 dfs_push_decls (tree binfo, void *data)
2467 type = BINFO_TYPE (binfo);
2468 dep_base_p = (processing_template_decl && type != current_class_type
2469 && dependent_base_p (binfo));
2473 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2474 if (DECL_NAME (fields)
2475 && TREE_CODE (fields) != TYPE_DECL
2476 && TREE_CODE (fields) != USING_DECL
2477 && !DECL_ARTIFICIAL (fields))
2478 setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
2479 else if (TREE_CODE (fields) == FIELD_DECL
2480 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2481 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
2483 method_vec = (CLASS_TYPE_P (type)
2484 ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
2486 if (method_vec && TREE_VEC_LENGTH (method_vec) >= 3)
2491 /* Farm out constructors and destructors. */
2492 end = TREE_VEC_END (method_vec);
2494 for (methods = &TREE_VEC_ELT (method_vec, 2);
2495 methods < end && *methods;
2497 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)),
2498 /*type_binding_p=*/0);
2502 CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
2507 /* When entering the scope of a class, we cache all of the
2508 fields that that class provides within its inheritance
2509 lattice. Where ambiguities result, we mark them
2510 with `error_mark_node' so that if they are encountered
2511 without explicit qualification, we can emit an error
2515 push_class_decls (tree type)
2517 search_stack = push_search_level (search_stack, &search_obstack);
2519 /* Enter type declarations and mark. */
2520 dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
2522 /* Enter non-type declarations and unmark. */
2523 dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
2526 /* Here's a subroutine we need because C lacks lambdas. */
2529 dfs_unuse_fields (tree binfo, void *data ATTRIBUTE_UNUSED)
2531 tree type = TREE_TYPE (binfo);
2534 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2536 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
2539 TREE_USED (fields) = 0;
2540 if (DECL_NAME (fields) == NULL_TREE
2541 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2542 unuse_fields (TREE_TYPE (fields));
2549 unuse_fields (tree type)
2551 dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
2557 /* We haven't pushed a search level when dealing with cached classes,
2558 so we'd better not try to pop it. */
2560 search_stack = pop_search_level (search_stack);
2564 print_search_statistics ()
2566 #ifdef GATHER_STATISTICS
2567 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2568 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
2569 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
2570 n_outer_fields_searched, n_calls_lookup_fnfields);
2571 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
2572 #else /* GATHER_STATISTICS */
2573 fprintf (stderr, "no search statistics\n");
2574 #endif /* GATHER_STATISTICS */
2578 init_search_processing ()
2580 gcc_obstack_init (&search_obstack);
2584 reinit_search_statistics ()
2586 #ifdef GATHER_STATISTICS
2587 n_fields_searched = 0;
2588 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
2589 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
2590 n_calls_get_base_type = 0;
2591 n_outer_fields_searched = 0;
2592 n_contexts_saved = 0;
2593 #endif /* GATHER_STATISTICS */
2597 add_conversions (tree binfo, void *data)
2600 tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
2601 tree *conversions = (tree *) data;
2603 /* Some builtin types have no method vector, not even an empty one. */
2607 for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
2609 tree tmp = TREE_VEC_ELT (method_vec, i);
2612 if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
2615 name = DECL_NAME (OVL_CURRENT (tmp));
2617 /* Make sure we don't already have this conversion. */
2618 if (! IDENTIFIER_MARKED (name))
2620 *conversions = tree_cons (binfo, tmp, *conversions);
2621 IDENTIFIER_MARKED (name) = 1;
2627 /* Return a TREE_LIST containing all the non-hidden user-defined
2628 conversion functions for TYPE (and its base-classes). The
2629 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2630 containing the conversion functions. The TREE_PURPOSE is the BINFO
2631 from which the conversion functions in this node were selected. */
2634 lookup_conversions (tree type)
2637 tree conversions = NULL_TREE;
2639 if (COMPLETE_TYPE_P (type))
2640 bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
2642 for (t = conversions; t; t = TREE_CHAIN (t))
2643 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
2654 /* Check whether the empty class indicated by EMPTY_BINFO is also present
2655 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
2658 dfs_check_overlap (tree empty_binfo, void *data)
2660 struct overlap_info *oi = (struct overlap_info *) data;
2662 for (binfo = TYPE_BINFO (oi->compare_type);
2664 binfo = BINFO_BASETYPE (binfo, 0))
2666 if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
2668 oi->found_overlap = 1;
2671 else if (BINFO_BASETYPES (binfo) == NULL_TREE)
2678 /* Trivial function to stop base traversal when we find something. */
2681 dfs_no_overlap_yet (tree binfo, void *data)
2683 struct overlap_info *oi = (struct overlap_info *) data;
2684 return !oi->found_overlap ? binfo : NULL_TREE;
2687 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2688 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
2691 types_overlap_p (tree empty_type, tree next_type)
2693 struct overlap_info oi;
2695 if (! IS_AGGR_TYPE (next_type))
2697 oi.compare_type = next_type;
2698 oi.found_overlap = 0;
2699 dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
2700 dfs_no_overlap_yet, &oi);
2701 return oi.found_overlap;
2704 /* Given a vtable VAR, determine which of the inherited classes the vtable
2705 inherits (in a loose sense) functions from.
2707 FIXME: This does not work with the new ABI. */
2710 binfo_for_vtable (tree var)
2712 tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
2713 tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
2714 int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
2717 for (i = 0; i < n_baseclasses; i++)
2719 tree base_binfo = TREE_VEC_ELT (binfos, i);
2720 if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
2724 /* If no secondary base classes matched, return the primary base, if
2726 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
2727 return get_primary_binfo (main_binfo);
2732 /* Returns the binfo of the first direct or indirect virtual base derived
2733 from BINFO, or NULL if binfo is not via virtual. */
2736 binfo_from_vbase (tree binfo)
2738 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
2740 if (TREE_VIA_VIRTUAL (binfo))
2746 /* Returns the binfo of the first direct or indirect virtual base derived
2747 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2751 binfo_via_virtual (tree binfo, tree limit)
2753 for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
2754 binfo = BINFO_INHERITANCE_CHAIN (binfo))
2756 if (TREE_VIA_VIRTUAL (binfo))
2762 /* Returns the BINFO (if any) for the virtual baseclass T of the class
2763 C from the CLASSTYPE_VBASECLASSES list. */
2766 binfo_for_vbase (tree basetype, tree classtype)
2770 binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
2771 return binfo ? TREE_VALUE (binfo) : NULL_TREE;