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 tree
bfs_walk (tree
, tree (*) (tree
, void *),
106 tree (*) (tree
, void *), void *);
107 static tree
lookup_field_queue_p (tree
, void *);
108 static int shared_member_p (tree
);
109 static tree
lookup_field_r (tree
, void *);
110 static tree
canonical_binfo (tree
);
111 static tree
shared_marked_p (tree
, void *);
112 static tree
shared_unmarked_p (tree
, void *);
113 static int dependent_base_p (tree
);
114 static tree
dfs_accessible_queue_p (tree
, void *);
115 static tree
dfs_accessible_p (tree
, void *);
116 static tree
dfs_access_in_type (tree
, void *);
117 static access_kind
access_in_type (tree
, tree
);
118 static tree
dfs_canonical_queue (tree
, void *);
119 static tree
dfs_assert_unmarked_p (tree
, void *);
120 static void assert_canonical_unmarked (tree
);
121 static int protected_accessible_p (tree
, tree
, tree
);
122 static int friend_accessible_p (tree
, tree
, tree
);
123 static void setup_class_bindings (tree
, int);
124 static int template_self_reference_p (tree
, tree
);
125 static tree
dfs_find_vbase_instance (tree
, void *);
126 static tree
dfs_get_pure_virtuals (tree
, void *);
127 static tree
dfs_build_inheritance_graph_order (tree
, void *);
129 /* Allocate a level of searching. */
131 static struct search_level
*
132 push_search_level (struct stack_level
*stack
, struct obstack
*obstack
)
134 struct search_level tem
;
137 return push_stack_level (obstack
, (char *)&tem
, sizeof (tem
));
140 /* Discard a level of search allocation. */
142 static struct search_level
*
143 pop_search_level (struct stack_level
*obstack
)
145 register struct search_level
*stack
= pop_stack_level (obstack
);
150 /* Variables for gathering statistics. */
151 #ifdef GATHER_STATISTICS
152 static int n_fields_searched
;
153 static int n_calls_lookup_field
, n_calls_lookup_field_1
;
154 static int n_calls_lookup_fnfields
, n_calls_lookup_fnfields_1
;
155 static int n_calls_get_base_type
;
156 static int n_outer_fields_searched
;
157 static int n_contexts_saved
;
158 #endif /* GATHER_STATISTICS */
161 /* Worker for lookup_base. BINFO is the binfo we are searching at,
162 BASE is the RECORD_TYPE we are searching for. ACCESS is the
163 required access checks. WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
164 IS_VIRTUAL indicate how BINFO was reached from the start of the
165 search. WITHIN_CURRENT_SCOPE is true if we met the current scope,
166 or friend thereof (this allows us to determine whether a protected
167 base is accessible or not). IS_NON_PUBLIC indicates whether BINFO
168 is accessible and IS_VIRTUAL indicates if it is morally virtual.
170 If BINFO is of the required type, then *BINFO_PTR is examined to
171 compare with any other instance of BASE we might have already
172 discovered. *BINFO_PTR is initialized and a base_kind return value
173 indicates what kind of base was located.
175 Otherwise BINFO's bases are searched. */
178 lookup_base_r (tree binfo
, tree base
, base_access access
,
179 bool within_current_scope
,
180 bool is_non_public
, /* inside a non-public part */
181 bool is_virtual
, /* inside a virtual part */
186 base_kind found
= bk_not_base
;
188 if (access
== ba_check
189 && !within_current_scope
190 && is_friend (BINFO_TYPE (binfo
), current_scope ()))
192 /* Do not clear is_non_public here. If A is a private base of B, A
193 is not allowed to convert a B* to an A*. */
194 within_current_scope
= 1;
197 if (same_type_p (BINFO_TYPE (binfo
), base
))
199 /* We have found a base. Check against what we have found
201 found
= bk_same_type
;
203 found
= bk_via_virtual
;
205 found
= bk_inaccessible
;
209 else if (!is_virtual
|| !tree_int_cst_equal (BINFO_OFFSET (binfo
),
210 BINFO_OFFSET (*binfo_ptr
)))
212 if (access
!= ba_any
)
214 else if (!is_virtual
)
215 /* Prefer a non-virtual base. */
223 bases
= BINFO_BASETYPES (binfo
);
227 for (i
= TREE_VEC_LENGTH (bases
); i
--;)
229 tree base_binfo
= TREE_VEC_ELT (bases
, i
);
230 int this_non_public
= is_non_public
;
231 int this_virtual
= is_virtual
;
234 if (access
<= ba_ignore
)
236 else if (TREE_VIA_PUBLIC (base_binfo
))
238 else if (access
== ba_not_special
)
240 else if (TREE_VIA_PROTECTED (base_binfo
) && within_current_scope
)
242 else if (is_friend (BINFO_TYPE (binfo
), current_scope ()))
247 if (TREE_VIA_VIRTUAL (base_binfo
))
250 bk
= lookup_base_r (base_binfo
, base
,
251 access
, within_current_scope
,
252 this_non_public
, this_virtual
,
258 if (access
!= ba_any
)
263 case bk_inaccessible
:
264 if (found
== bk_not_base
)
266 my_friendly_assert (found
== bk_via_virtual
267 || found
== bk_inaccessible
, 20010723);
275 my_friendly_assert (found
== bk_not_base
, 20010723);
280 if (found
!= bk_ambig
)
291 /* Lookup BASE in the hierarchy dominated by T. Do access checking as
292 ACCESS specifies. Return the binfo we discover (which might not be
293 canonical). If KIND_PTR is non-NULL, fill with information about
294 what kind of base we discovered.
296 If the base is inaccessible, or ambiguous, and the ba_quiet bit is
297 not set in ACCESS, then an error is issued and error_mark_node is
298 returned. If the ba_quiet bit is set, then no error is issued and
299 NULL_TREE is returned. */
302 lookup_base (tree t
, tree base
, base_access access
, base_kind
*kind_ptr
)
304 tree binfo
= NULL
; /* The binfo we've found so far. */
308 if (t
== error_mark_node
|| base
== error_mark_node
)
311 *kind_ptr
= bk_not_base
;
312 return error_mark_node
;
314 my_friendly_assert (TYPE_P (base
), 20011127);
322 t_binfo
= TYPE_BINFO (t
);
324 /* Ensure that the types are instantiated. */
325 t
= complete_type (TYPE_MAIN_VARIANT (t
));
326 base
= complete_type (TYPE_MAIN_VARIANT (base
));
328 bk
= lookup_base_r (t_binfo
, base
, access
& ~ba_quiet
,
333 case bk_inaccessible
:
335 if (!(access
& ba_quiet
))
337 error ("`%T' is an inaccessible base of `%T'", base
, t
);
338 binfo
= error_mark_node
;
342 if (access
!= ba_any
)
345 if (!(access
& ba_quiet
))
347 error ("`%T' is an ambiguous base of `%T'", base
, t
);
348 binfo
= error_mark_node
;
361 /* Worker function for get_dynamic_cast_base_type. */
364 dynamic_cast_base_recurse (tree subtype
, tree binfo
, bool is_via_virtual
,
371 if (BINFO_TYPE (binfo
) == subtype
)
377 *offset_ptr
= BINFO_OFFSET (binfo
);
382 binfos
= BINFO_BASETYPES (binfo
);
383 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
384 for (i
= 0; i
< n_baselinks
; i
++)
386 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
389 if (!TREE_VIA_PUBLIC (base_binfo
))
391 rval
= dynamic_cast_base_recurse
392 (subtype
, base_binfo
,
393 is_via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
), offset_ptr
);
397 worst
= worst
>= 0 ? -3 : worst
;
400 else if (rval
== -3 && worst
!= -1)
406 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
407 started from is related to the required TARGET type, in order to optimize
408 the inheritance graph search. This information is independent of the
409 current context, and ignores private paths, hence get_base_distance is
410 inappropriate. Return a TREE specifying the base offset, BOFF.
411 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
412 and there are no public virtual SUBTYPE bases.
413 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
414 BOFF == -2, SUBTYPE is not a public base.
415 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */
418 get_dynamic_cast_base_type (tree subtype
, tree target
)
420 tree offset
= NULL_TREE
;
421 int boff
= dynamic_cast_base_recurse (subtype
, TYPE_BINFO (target
),
426 offset
= build_int_2 (boff
, -1);
427 TREE_TYPE (offset
) = ssizetype
;
431 /* Search for a member with name NAME in a multiple inheritance lattice
432 specified by TYPE. If it does not exist, return NULL_TREE.
433 If the member is ambiguously referenced, return `error_mark_node'.
434 Otherwise, return the FIELD_DECL. */
436 /* Do a 1-level search for NAME as a member of TYPE. The caller must
437 figure out whether it can access this field. (Since it is only one
438 level, this is reasonable.) */
441 lookup_field_1 (tree type
, tree name
)
445 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
446 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
447 || TREE_CODE (type
) == TYPENAME_TYPE
)
448 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
449 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
450 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously,
451 the code often worked even when we treated the index as a list
453 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */
457 && DECL_LANG_SPECIFIC (TYPE_NAME (type
))
458 && DECL_SORTED_FIELDS (TYPE_NAME (type
)))
460 tree
*fields
= &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type
)), 0);
461 int lo
= 0, hi
= TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type
)));
468 #ifdef GATHER_STATISTICS
470 #endif /* GATHER_STATISTICS */
472 if (DECL_NAME (fields
[i
]) > name
)
474 else if (DECL_NAME (fields
[i
]) < name
)
478 /* We might have a nested class and a field with the
479 same name; we sorted them appropriately via
480 field_decl_cmp, so just look for the last field with
483 && DECL_NAME (fields
[i
+1]) == name
)
491 field
= TYPE_FIELDS (type
);
493 #ifdef GATHER_STATISTICS
494 n_calls_lookup_field_1
++;
495 #endif /* GATHER_STATISTICS */
498 #ifdef GATHER_STATISTICS
500 #endif /* GATHER_STATISTICS */
501 my_friendly_assert (DECL_P (field
), 0);
502 if (DECL_NAME (field
) == NULL_TREE
503 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
505 tree temp
= lookup_field_1 (TREE_TYPE (field
), name
);
509 if (TREE_CODE (field
) == USING_DECL
)
510 /* For now, we're just treating member using declarations as
511 old ARM-style access declarations. Thus, there's no reason
512 to return a USING_DECL, and the rest of the compiler can't
513 handle it. Once the class is defined, these are purged
514 from TYPE_FIELDS anyhow; see handle_using_decl. */
516 else if (DECL_NAME (field
) == name
)
518 field
= TREE_CHAIN (field
);
521 if (name
== vptr_identifier
)
523 /* Give the user what s/he thinks s/he wants. */
524 if (TYPE_POLYMORPHIC_P (type
))
525 return TYPE_VFIELD (type
);
530 /* There are a number of cases we need to be aware of here:
531 current_class_type current_function_decl
538 Those last two make life interesting. If we're in a function which is
539 itself inside a class, we need decls to go into the fn's decls (our
540 second case below). But if we're in a class and the class itself is
541 inside a function, we need decls to go into the decls for the class. To
542 achieve this last goal, we must see if, when both current_class_ptr and
543 current_function_decl are set, the class was declared inside that
544 function. If so, we know to put the decls into the class's scope. */
549 if (current_function_decl
== NULL_TREE
)
550 return current_class_type
;
551 if (current_class_type
== NULL_TREE
)
552 return current_function_decl
;
553 if ((DECL_FUNCTION_MEMBER_P (current_function_decl
)
554 && same_type_p (DECL_CONTEXT (current_function_decl
),
556 || (DECL_FRIEND_CONTEXT (current_function_decl
)
557 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl
),
558 current_class_type
)))
559 return current_function_decl
;
561 return current_class_type
;
564 /* Returns nonzero if we are currently in a function scope. Note
565 that this function returns zero if we are within a local class, but
566 not within a member function body of the local class. */
569 at_function_scope_p ()
571 tree cs
= current_scope ();
572 return cs
&& TREE_CODE (cs
) == FUNCTION_DECL
;
575 /* Returns true if the innermost active scope is a class scope. */
580 tree cs
= current_scope ();
581 return cs
&& TYPE_P (cs
);
584 /* Return the scope of DECL, as appropriate when doing name-lookup. */
587 context_for_name_lookup (tree decl
)
591 For the purposes of name lookup, after the anonymous union
592 definition, the members of the anonymous union are considered to
593 have been defined in the scope in which the anonymous union is
595 tree context
= DECL_CONTEXT (decl
);
597 while (context
&& TYPE_P (context
) && ANON_AGGR_TYPE_P (context
))
598 context
= TYPE_CONTEXT (context
);
600 context
= global_namespace
;
605 /* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
609 canonical_binfo (tree binfo
)
611 return (TREE_VIA_VIRTUAL (binfo
)
612 ? TYPE_BINFO (BINFO_TYPE (binfo
)) : binfo
);
615 /* A queue function that simply ensures that we walk into the
616 canonical versions of virtual bases. */
619 dfs_canonical_queue (tree binfo
, void *data ATTRIBUTE_UNUSED
)
621 return canonical_binfo (binfo
);
624 /* Called via dfs_walk from assert_canonical_unmarked. */
627 dfs_assert_unmarked_p (tree binfo
, void *data ATTRIBUTE_UNUSED
)
629 my_friendly_assert (!BINFO_MARKED (binfo
), 0);
633 /* Asserts that all the nodes below BINFO (using the canonical
634 versions of virtual bases) are unmarked. */
637 assert_canonical_unmarked (tree binfo
)
639 dfs_walk (binfo
, dfs_assert_unmarked_p
, dfs_canonical_queue
, 0);
642 /* If BINFO is marked, return a canonical version of BINFO.
643 Otherwise, return NULL_TREE. */
646 shared_marked_p (tree binfo
, void *data
)
648 binfo
= canonical_binfo (binfo
);
649 return markedp (binfo
, data
);
652 /* If BINFO is not marked, return a canonical version of BINFO.
653 Otherwise, return NULL_TREE. */
656 shared_unmarked_p (tree binfo
, void *data
)
658 binfo
= canonical_binfo (binfo
);
659 return unmarkedp (binfo
, data
);
662 /* The accessibility routines use BINFO_ACCESS for scratch space
663 during the computation of the accssibility of some declaration. */
665 #define BINFO_ACCESS(NODE) \
666 ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
668 /* Set the access associated with NODE to ACCESS. */
670 #define SET_BINFO_ACCESS(NODE, ACCESS) \
671 ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0), \
672 (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
674 /* Called from access_in_type via dfs_walk. Calculate the access to
675 DATA (which is really a DECL) in BINFO. */
678 dfs_access_in_type (tree binfo
, void *data
)
680 tree decl
= (tree
) data
;
681 tree type
= BINFO_TYPE (binfo
);
682 access_kind access
= ak_none
;
684 if (context_for_name_lookup (decl
) == type
)
686 /* If we have desceneded to the scope of DECL, just note the
687 appropriate access. */
688 if (TREE_PRIVATE (decl
))
690 else if (TREE_PROTECTED (decl
))
691 access
= ak_protected
;
697 /* First, check for an access-declaration that gives us more
698 access to the DECL. The CONST_DECL for an enumeration
699 constant will not have DECL_LANG_SPECIFIC, and thus no
701 if (DECL_LANG_SPECIFIC (decl
) && !DECL_DISCRIMINATOR_P (decl
))
703 tree decl_access
= purpose_member (type
, DECL_ACCESS (decl
));
705 access
= ((access_kind
)
706 TREE_INT_CST_LOW (TREE_VALUE (decl_access
)));
715 /* Otherwise, scan our baseclasses, and pick the most favorable
717 binfos
= BINFO_BASETYPES (binfo
);
718 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
719 for (i
= 0; i
< n_baselinks
; ++i
)
721 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
722 access_kind base_access
723 = BINFO_ACCESS (canonical_binfo (base_binfo
));
725 if (base_access
== ak_none
|| base_access
== ak_private
)
726 /* If it was not accessible in the base, or only
727 accessible as a private member, we can't access it
729 base_access
= ak_none
;
730 else if (TREE_VIA_PROTECTED (base_binfo
))
731 /* Public and protected members in the base are
733 base_access
= ak_protected
;
734 else if (!TREE_VIA_PUBLIC (base_binfo
))
735 /* Public and protected members in the base are
737 base_access
= ak_private
;
739 /* See if the new access, via this base, gives more
740 access than our previous best access. */
741 if (base_access
!= ak_none
742 && (base_access
== ak_public
743 || (base_access
== ak_protected
744 && access
!= ak_public
)
745 || (base_access
== ak_private
746 && access
== ak_none
)))
748 access
= base_access
;
750 /* If the new access is public, we can't do better. */
751 if (access
== ak_public
)
758 /* Note the access to DECL in TYPE. */
759 SET_BINFO_ACCESS (binfo
, access
);
761 /* Mark TYPE as visited so that if we reach it again we do not
762 duplicate our efforts here. */
763 SET_BINFO_MARKED (binfo
);
768 /* Return the access to DECL in TYPE. */
771 access_in_type (tree type
, tree decl
)
773 tree binfo
= TYPE_BINFO (type
);
775 /* We must take into account
779 If a name can be reached by several paths through a multiple
780 inheritance graph, the access is that of the path that gives
783 The algorithm we use is to make a post-order depth-first traversal
784 of the base-class hierarchy. As we come up the tree, we annotate
785 each node with the most lenient access. */
786 dfs_walk_real (binfo
, 0, dfs_access_in_type
, shared_unmarked_p
, decl
);
787 dfs_walk (binfo
, dfs_unmark
, shared_marked_p
, 0);
788 assert_canonical_unmarked (binfo
);
790 return BINFO_ACCESS (binfo
);
793 /* Called from dfs_accessible_p via dfs_walk. */
796 dfs_accessible_queue_p (tree binfo
, void *data ATTRIBUTE_UNUSED
)
798 if (BINFO_MARKED (binfo
))
801 /* If this class is inherited via private or protected inheritance,
802 then we can't see it, unless we are a friend of the subclass. */
803 if (!TREE_VIA_PUBLIC (binfo
)
804 && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo
)),
808 return canonical_binfo (binfo
);
811 /* Called from dfs_accessible_p via dfs_walk. */
814 dfs_accessible_p (tree binfo
, void *data
)
816 int protected_ok
= data
!= 0;
819 SET_BINFO_MARKED (binfo
);
820 access
= BINFO_ACCESS (binfo
);
821 if (access
== ak_public
|| (access
== ak_protected
&& protected_ok
))
823 else if (access
!= ak_none
824 && is_friend (BINFO_TYPE (binfo
), current_scope ()))
830 /* Returns nonzero if it is OK to access DECL through an object
831 indiated by BINFO in the context of DERIVED. */
834 protected_accessible_p (tree decl
, tree derived
, tree binfo
)
838 /* We're checking this clause from [class.access.base]
840 m as a member of N is protected, and the reference occurs in a
841 member or friend of class N, or in a member or friend of a
842 class P derived from N, where m as a member of P is private or
845 Here DERIVED is a possible P and DECL is m. accessible_p will
846 iterate over various values of N, but the access to m in DERIVED
849 Note that I believe that the passage above is wrong, and should read
850 "...is private or protected or public"; otherwise you get bizarre results
851 whereby a public using-decl can prevent you from accessing a protected
852 member of a base. (jason 2000/02/28) */
854 /* If DERIVED isn't derived from m's class, then it can't be a P. */
855 if (!DERIVED_FROM_P (context_for_name_lookup (decl
), derived
))
858 access
= access_in_type (derived
, decl
);
860 /* If m is inaccessible in DERIVED, then it's not a P. */
861 if (access
== ak_none
)
866 When a friend or a member function of a derived class references
867 a protected nonstatic member of a base class, an access check
868 applies in addition to those described earlier in clause
869 _class.access_) Except when forming a pointer to member
870 (_expr.unary.op_), the access must be through a pointer to,
871 reference to, or object of the derived class itself (or any class
872 derived from that class) (_expr.ref_). If the access is to form
873 a pointer to member, the nested-name-specifier shall name the
874 derived class (or any class derived from that class). */
875 if (DECL_NONSTATIC_MEMBER_P (decl
))
877 /* We can tell through what the reference is occurring by
878 chasing BINFO up to the root. */
880 while (BINFO_INHERITANCE_CHAIN (t
))
881 t
= BINFO_INHERITANCE_CHAIN (t
);
883 if (!DERIVED_FROM_P (derived
, BINFO_TYPE (t
)))
890 /* Returns nonzero if SCOPE is a friend of a type which would be able
891 to access DECL through the object indicated by BINFO. */
894 friend_accessible_p (tree scope
, tree decl
, tree binfo
)
896 tree befriending_classes
;
902 if (TREE_CODE (scope
) == FUNCTION_DECL
903 || DECL_FUNCTION_TEMPLATE_P (scope
))
904 befriending_classes
= DECL_BEFRIENDING_CLASSES (scope
);
905 else if (TYPE_P (scope
))
906 befriending_classes
= CLASSTYPE_BEFRIENDING_CLASSES (scope
);
910 for (t
= befriending_classes
; t
; t
= TREE_CHAIN (t
))
911 if (protected_accessible_p (decl
, TREE_VALUE (t
), binfo
))
914 /* Nested classes are implicitly friends of their enclosing types, as
915 per core issue 45 (this is a change from the standard). */
917 for (t
= TYPE_CONTEXT (scope
); t
&& TYPE_P (t
); t
= TYPE_CONTEXT (t
))
918 if (protected_accessible_p (decl
, t
, binfo
))
921 if (TREE_CODE (scope
) == FUNCTION_DECL
922 || DECL_FUNCTION_TEMPLATE_P (scope
))
924 /* Perhaps this SCOPE is a member of a class which is a
926 if (DECL_CLASS_SCOPE_P (decl
)
927 && friend_accessible_p (DECL_CONTEXT (scope
), decl
, binfo
))
930 /* Or an instantiation of something which is a friend. */
931 if (DECL_TEMPLATE_INFO (scope
))
932 return friend_accessible_p (DECL_TI_TEMPLATE (scope
), decl
, binfo
);
934 else if (CLASSTYPE_TEMPLATE_INFO (scope
))
935 return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope
), decl
, binfo
);
940 /* DECL is a declaration from a base class of TYPE, which was the
941 class used to name DECL. Return nonzero if, in the current
942 context, DECL is accessible. If TYPE is actually a BINFO node,
943 then we can tell in what context the access is occurring by looking
944 at the most derived class along the path indicated by BINFO. */
947 accessible_p (tree type
, tree decl
)
952 /* Nonzero if it's OK to access DECL if it has protected
953 accessibility in TYPE. */
954 int protected_ok
= 0;
956 /* If we're not checking access, everything is accessible. */
957 if (!scope_chain
->check_access
)
960 /* If this declaration is in a block or namespace scope, there's no
962 if (!TYPE_P (context_for_name_lookup (decl
)))
968 type
= BINFO_TYPE (type
);
971 binfo
= TYPE_BINFO (type
);
973 /* [class.access.base]
975 A member m is accessible when named in class N if
977 --m as a member of N is public, or
979 --m as a member of N is private, and the reference occurs in a
980 member or friend of class N, or
982 --m as a member of N is protected, and the reference occurs in a
983 member or friend of class N, or in a member or friend of a
984 class P derived from N, where m as a member of P is private or
987 --there exists a base class B of N that is accessible at the point
988 of reference, and m is accessible when named in class B.
990 We walk the base class hierarchy, checking these conditions. */
992 /* Figure out where the reference is occurring. Check to see if
993 DECL is private or protected in this scope, since that will
994 determine whether protected access is allowed. */
995 if (current_class_type
)
996 protected_ok
= protected_accessible_p (decl
, current_class_type
, binfo
);
998 /* Now, loop through the classes of which we are a friend. */
1000 protected_ok
= friend_accessible_p (current_scope (), decl
, binfo
);
1002 /* Standardize the binfo that access_in_type will use. We don't
1003 need to know what path was chosen from this point onwards. */
1004 binfo
= TYPE_BINFO (type
);
1006 /* Compute the accessibility of DECL in the class hierarchy
1007 dominated by type. */
1008 access_in_type (type
, decl
);
1009 /* Walk the hierarchy again, looking for a base class that allows
1011 t
= dfs_walk (binfo
, dfs_accessible_p
,
1012 dfs_accessible_queue_p
,
1013 protected_ok
? &protected_ok
: 0);
1014 /* Clear any mark bits. Note that we have to walk the whole tree
1015 here, since we have aborted the previous walk from some point
1016 deep in the tree. */
1017 dfs_walk (binfo
, dfs_unmark
, dfs_canonical_queue
, 0);
1018 assert_canonical_unmarked (binfo
);
1020 return t
!= NULL_TREE
;
1023 /* Recursive helper funciton for is_subobject_of_p; see that routine
1024 for documentation of the parameters. */
1027 is_subobject_of_p_1 (tree parent
, tree binfo
, tree most_derived
)
1032 if (parent
== binfo
)
1035 binfos
= BINFO_BASETYPES (binfo
);
1036 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1038 /* Iterate through the base types. */
1039 for (i
= 0; i
< n_baselinks
; i
++)
1041 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1044 base_type
= TREE_TYPE (base_binfo
);
1045 if (!CLASS_TYPE_P (base_type
))
1046 /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
1047 class there's no way to descend into it. */
1050 /* Avoid walking into the same virtual base more than once. */
1051 if (TREE_VIA_VIRTUAL (base_binfo
))
1053 if (CLASSTYPE_MARKED4 (base_type
))
1055 SET_CLASSTYPE_MARKED4 (base_type
);
1056 base_binfo
= binfo_for_vbase (base_type
, most_derived
);
1059 if (is_subobject_of_p_1 (parent
, base_binfo
, most_derived
))
1065 /* Routine to see if the sub-object denoted by the binfo PARENT can be
1066 found as a base class and sub-object of the object denoted by
1067 BINFO. MOST_DERIVED is the most derived type of the hierarchy being
1071 is_subobject_of_p (tree parent
, tree binfo
, tree most_derived
)
1076 result
= is_subobject_of_p_1 (parent
, binfo
, most_derived
);
1077 /* Clear the mark bits on virtual bases. */
1078 for (vbase
= CLASSTYPE_VBASECLASSES (most_derived
);
1080 vbase
= TREE_CHAIN (vbase
))
1081 CLEAR_CLASSTYPE_MARKED4 (TREE_TYPE (TREE_VALUE (vbase
)));
1086 struct lookup_field_info
{
1087 /* The type in which we're looking. */
1089 /* The name of the field for which we're looking. */
1091 /* If non-NULL, the current result of the lookup. */
1093 /* The path to RVAL. */
1095 /* If non-NULL, the lookup was ambiguous, and this is a list of the
1098 /* If nonzero, we are looking for types, not data members. */
1100 /* If nonzero, RVAL was found by looking through a dependent base. */
1101 int from_dep_base_p
;
1102 /* If something went wrong, a message indicating what. */
1106 /* Returns nonzero if BINFO is not hidden by the value found by the
1107 lookup so far. If BINFO is hidden, then there's no need to look in
1108 it. DATA is really a struct lookup_field_info. Called from
1109 lookup_field via breadth_first_search. */
1112 lookup_field_queue_p (tree binfo
, void *data
)
1114 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1116 /* Don't look for constructors or destructors in base classes. */
1117 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi
->name
))
1120 /* If this base class is hidden by the best-known value so far, we
1121 don't need to look. */
1122 binfo
= CANONICAL_BINFO (binfo
, lfi
->type
);
1123 if (!lfi
->from_dep_base_p
&& lfi
->rval_binfo
1124 && is_subobject_of_p (binfo
, lfi
->rval_binfo
, lfi
->type
))
1130 /* Within the scope of a template class, you can refer to the to the
1131 current specialization with the name of the template itself. For
1134 template <typename T> struct S { S* sp; }
1136 Returns nonzero if DECL is such a declaration in a class TYPE. */
1139 template_self_reference_p (tree type
, tree decl
)
1141 return (CLASSTYPE_USE_TEMPLATE (type
)
1142 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
1143 && TREE_CODE (decl
) == TYPE_DECL
1144 && DECL_ARTIFICIAL (decl
)
1145 && DECL_NAME (decl
) == constructor_name (type
));
1149 /* Nonzero for a class member means that it is shared between all objects
1152 [class.member.lookup]:If the resulting set of declarations are not all
1153 from sub-objects of the same type, or the set has a nonstatic member
1154 and includes members from distinct sub-objects, there is an ambiguity
1155 and the program is ill-formed.
1157 This function checks that T contains no nonstatic members. */
1160 shared_member_p (tree t
)
1162 if (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == TYPE_DECL \
1163 || TREE_CODE (t
) == CONST_DECL
)
1165 if (is_overloaded_fn (t
))
1167 for (; t
; t
= OVL_NEXT (t
))
1169 tree fn
= OVL_CURRENT (t
);
1170 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1178 /* DATA is really a struct lookup_field_info. Look for a field with
1179 the name indicated there in BINFO. If this function returns a
1180 non-NULL value it is the result of the lookup. Called from
1181 lookup_field via breadth_first_search. */
1184 lookup_field_r (tree binfo
, void *data
)
1186 struct lookup_field_info
*lfi
= (struct lookup_field_info
*) data
;
1187 tree type
= BINFO_TYPE (binfo
);
1188 tree nval
= NULL_TREE
;
1189 int from_dep_base_p
;
1191 /* First, look for a function. There can't be a function and a data
1192 member with the same name, and if there's a function and a type
1193 with the same name, the type is hidden by the function. */
1194 if (!lfi
->want_type
)
1196 int idx
= lookup_fnfields_1 (type
, lfi
->name
);
1198 nval
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), idx
);
1202 /* Look for a data member or type. */
1203 nval
= lookup_field_1 (type
, lfi
->name
);
1205 /* If there is no declaration with the indicated name in this type,
1206 then there's nothing to do. */
1210 /* If we're looking up a type (as with an elaborated type specifier)
1211 we ignore all non-types we find. */
1212 if (lfi
->want_type
&& TREE_CODE (nval
) != TYPE_DECL
1213 && !DECL_CLASS_TEMPLATE_P (nval
))
1215 if (lfi
->name
== TYPE_IDENTIFIER (type
))
1217 /* If the aggregate has no user defined constructors, we allow
1218 it to have fields with the same name as the enclosing type.
1219 If we are looking for that name, find the corresponding
1221 for (nval
= TREE_CHAIN (nval
); nval
; nval
= TREE_CHAIN (nval
))
1222 if (DECL_NAME (nval
) == lfi
->name
1223 && TREE_CODE (nval
) == TYPE_DECL
)
1230 nval
= purpose_member (lfi
->name
, CLASSTYPE_TAGS (type
));
1232 nval
= TYPE_MAIN_DECL (TREE_VALUE (nval
));
1238 /* You must name a template base class with a template-id. */
1239 if (!same_type_p (type
, lfi
->type
)
1240 && template_self_reference_p (type
, nval
))
1243 from_dep_base_p
= dependent_base_p (binfo
);
1244 if (lfi
->from_dep_base_p
&& !from_dep_base_p
)
1246 /* If the new declaration is not found via a dependent base, and
1247 the old one was, then we must prefer the new one. We weren't
1248 really supposed to be able to find the old one, so we don't
1249 want to be affected by a specialization. Consider:
1251 struct B { typedef int I; };
1252 template <typename T> struct D1 : virtual public B {};
1253 template <typename T> struct D :
1254 public D1, virtual pubic B { I i; };
1256 The `I' in `D<T>' is unambigousuly `B::I', regardless of how
1257 D1 is specialized. */
1258 lfi
->from_dep_base_p
= 0;
1259 lfi
->rval
= NULL_TREE
;
1260 lfi
->rval_binfo
= NULL_TREE
;
1261 lfi
->ambiguous
= NULL_TREE
;
1264 else if (lfi
->rval_binfo
&& !lfi
->from_dep_base_p
&& from_dep_base_p
)
1265 /* Similarly, if the old declaration was not found via a dependent
1266 base, and the new one is, ignore the new one. */
1269 /* If the lookup already found a match, and the new value doesn't
1270 hide the old one, we might have an ambiguity. */
1271 if (lfi
->rval_binfo
&& !is_subobject_of_p (lfi
->rval_binfo
, binfo
, lfi
->type
))
1273 if (nval
== lfi
->rval
&& shared_member_p (nval
))
1274 /* The two things are really the same. */
1276 else if (is_subobject_of_p (binfo
, lfi
->rval_binfo
, lfi
->type
))
1277 /* The previous value hides the new one. */
1281 /* We have a real ambiguity. We keep a chain of all the
1283 if (!lfi
->ambiguous
&& lfi
->rval
)
1285 /* This is the first time we noticed an ambiguity. Add
1286 what we previously thought was a reasonable candidate
1288 lfi
->ambiguous
= tree_cons (NULL_TREE
, lfi
->rval
, NULL_TREE
);
1289 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1292 /* Add the new value. */
1293 lfi
->ambiguous
= tree_cons (NULL_TREE
, nval
, lfi
->ambiguous
);
1294 TREE_TYPE (lfi
->ambiguous
) = error_mark_node
;
1295 lfi
->errstr
= "request for member `%D' is ambiguous";
1300 if (from_dep_base_p
&& TREE_CODE (nval
) == TYPE_DECL
1301 /* We need to return a member template class so we can
1302 define partial specializations. Is there a better
1304 && !DECL_CLASS_TEMPLATE_P (nval
))
1305 /* The thing we're looking for isn't a type, so the implicit
1306 typename extension doesn't apply, so we just pretend we
1307 didn't find anything. */
1311 lfi
->from_dep_base_p
= from_dep_base_p
;
1312 lfi
->rval_binfo
= binfo
;
1318 /* Return a "baselink" which BASELINK_BINFO, BASELINK_ACCESS_BINFO,
1319 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
1320 FUNCTIONS, and OPTYPE respectively. */
1323 build_baselink (tree binfo
, tree access_binfo
, tree functions
, tree optype
)
1327 my_friendly_assert (TREE_CODE (functions
) == FUNCTION_DECL
1328 || TREE_CODE (functions
) == TEMPLATE_DECL
1329 || TREE_CODE (functions
) == TEMPLATE_ID_EXPR
1330 || TREE_CODE (functions
) == OVERLOAD
,
1332 my_friendly_assert (!optype
|| TYPE_P (optype
), 20020730);
1333 my_friendly_assert (TREE_TYPE (functions
), 20020805);
1335 baselink
= make_node (BASELINK
);
1336 TREE_TYPE (baselink
) = TREE_TYPE (functions
);
1337 BASELINK_BINFO (baselink
) = binfo
;
1338 BASELINK_ACCESS_BINFO (baselink
) = access_binfo
;
1339 BASELINK_FUNCTIONS (baselink
) = functions
;
1340 BASELINK_OPTYPE (baselink
) = optype
;
1345 /* Look for a member named NAME in an inheritance lattice dominated by
1346 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it
1347 is 1, we enforce accessibility. If PROTECT is zero, then, for an
1348 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error
1349 messages about inaccessible or ambiguous lookup. If PROTECT is 2,
1350 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
1351 TREE_VALUEs are the list of ambiguous candidates.
1353 WANT_TYPE is 1 when we should only return TYPE_DECLs.
1355 If nothing can be found return NULL_TREE and do not issue an error. */
1358 lookup_member (tree xbasetype
, tree name
, int protect
, bool want_type
)
1360 tree rval
, rval_binfo
= NULL_TREE
;
1361 tree type
= NULL_TREE
, basetype_path
= NULL_TREE
;
1362 struct lookup_field_info lfi
;
1364 /* rval_binfo is the binfo associated with the found member, note,
1365 this can be set with useful information, even when rval is not
1366 set, because it must deal with ALL members, not just non-function
1367 members. It is used for ambiguity checking and the hidden
1368 checks. Whereas rval is only set if a proper (not hidden)
1369 non-function member is found. */
1371 const char *errstr
= 0;
1373 if (xbasetype
== current_class_type
&& TYPE_BEING_DEFINED (xbasetype
)
1374 && IDENTIFIER_CLASS_VALUE (name
))
1376 tree field
= IDENTIFIER_CLASS_VALUE (name
);
1377 if (TREE_CODE (field
) != FUNCTION_DECL
1378 && ! (want_type
&& TREE_CODE (field
) != TYPE_DECL
))
1379 /* We're in the scope of this class, and the value has already
1380 been looked up. Just return the cached value. */
1384 if (TREE_CODE (xbasetype
) == TREE_VEC
)
1386 type
= BINFO_TYPE (xbasetype
);
1387 basetype_path
= xbasetype
;
1389 else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype
)))
1392 basetype_path
= TYPE_BINFO (type
);
1393 my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path
) == NULL_TREE
,
1399 complete_type (type
);
1401 #ifdef GATHER_STATISTICS
1402 n_calls_lookup_field
++;
1403 #endif /* GATHER_STATISTICS */
1405 memset ((PTR
) &lfi
, 0, sizeof (lfi
));
1408 lfi
.want_type
= want_type
;
1409 bfs_walk (basetype_path
, &lookup_field_r
, &lookup_field_queue_p
, &lfi
);
1411 rval_binfo
= lfi
.rval_binfo
;
1413 type
= BINFO_TYPE (rval_binfo
);
1414 errstr
= lfi
.errstr
;
1416 /* If we are not interested in ambiguities, don't report them;
1417 just return NULL_TREE. */
1418 if (!protect
&& lfi
.ambiguous
)
1424 return lfi
.ambiguous
;
1431 In the case of overloaded function names, access control is
1432 applied to the function selected by overloaded resolution. */
1433 if (rval
&& protect
&& !is_overloaded_fn (rval
)
1434 && !enforce_access (xbasetype
, rval
))
1435 return error_mark_node
;
1437 if (errstr
&& protect
)
1439 error (errstr
, name
, type
);
1441 print_candidates (lfi
.ambiguous
);
1442 rval
= error_mark_node
;
1445 /* If the thing we found was found via the implicit typename
1446 extension, build the typename type. */
1447 if (rval
&& lfi
.from_dep_base_p
&& !DECL_CLASS_TEMPLATE_P (rval
))
1450 if (rval
&& is_overloaded_fn (rval
))
1451 rval
= build_baselink (rval_binfo
, basetype_path
, rval
,
1452 (IDENTIFIER_TYPENAME_P (name
)
1453 ? TREE_TYPE (name
): NULL_TREE
));
1457 /* Like lookup_member, except that if we find a function member we
1458 return NULL_TREE. */
1461 lookup_field (tree xbasetype
, tree name
, int protect
, bool want_type
)
1463 tree rval
= lookup_member (xbasetype
, name
, protect
, want_type
);
1465 /* Ignore functions. */
1466 if (rval
&& BASELINK_P (rval
))
1472 /* Like lookup_member, except that if we find a non-function member we
1473 return NULL_TREE. */
1476 lookup_fnfields (tree xbasetype
, tree name
, int protect
)
1478 tree rval
= lookup_member (xbasetype
, name
, protect
, /*want_type=*/false);
1480 /* Ignore non-functions. */
1481 if (rval
&& !BASELINK_P (rval
))
1487 /* TYPE is a class type. Return the index of the fields within
1488 the method vector with name NAME, or -1 is no such field exists. */
1491 lookup_fnfields_1 (tree type
, tree name
)
1493 tree method_vec
= (CLASS_TYPE_P (type
)
1494 ? CLASSTYPE_METHOD_VEC (type
)
1497 if (method_vec
!= 0)
1500 register tree
*methods
= &TREE_VEC_ELT (method_vec
, 0);
1501 int len
= TREE_VEC_LENGTH (method_vec
);
1504 #ifdef GATHER_STATISTICS
1505 n_calls_lookup_fnfields_1
++;
1506 #endif /* GATHER_STATISTICS */
1508 /* Constructors are first... */
1509 if (name
== ctor_identifier
)
1510 return (methods
[CLASSTYPE_CONSTRUCTOR_SLOT
]
1511 ? CLASSTYPE_CONSTRUCTOR_SLOT
: -1);
1512 /* and destructors are second. */
1513 if (name
== dtor_identifier
)
1514 return (methods
[CLASSTYPE_DESTRUCTOR_SLOT
]
1515 ? CLASSTYPE_DESTRUCTOR_SLOT
: -1);
1517 for (i
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1518 i
< len
&& methods
[i
];
1521 #ifdef GATHER_STATISTICS
1522 n_outer_fields_searched
++;
1523 #endif /* GATHER_STATISTICS */
1525 tmp
= OVL_CURRENT (methods
[i
]);
1526 if (DECL_NAME (tmp
) == name
)
1529 /* If the type is complete and we're past the conversion ops,
1530 switch to binary search. */
1531 if (! DECL_CONV_FN_P (tmp
)
1532 && COMPLETE_TYPE_P (type
))
1534 int lo
= i
+ 1, hi
= len
;
1540 #ifdef GATHER_STATISTICS
1541 n_outer_fields_searched
++;
1542 #endif /* GATHER_STATISTICS */
1544 tmp
= DECL_NAME (OVL_CURRENT (methods
[i
]));
1548 else if (tmp
< name
)
1557 /* If we didn't find it, it might have been a template
1558 conversion operator to a templated type. If there are any,
1559 such template conversion operators will all be overloaded on
1560 the first conversion slot. (Note that we don't look for this
1561 case above so that we will always find specializations
1563 if (IDENTIFIER_TYPENAME_P (name
))
1565 i
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1566 if (i
< len
&& methods
[i
])
1568 tmp
= OVL_CURRENT (methods
[i
]);
1569 if (TREE_CODE (tmp
) == TEMPLATE_DECL
1570 && DECL_TEMPLATE_CONV_FN_P (tmp
))
1579 /* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is
1580 the class or namespace used to qualify the name. CONTEXT_CLASS is
1581 the class corresponding to the object in which DECL will be used.
1582 Return a possibly modified version of DECL that takes into account
1585 In particular, consider an expression like `B::m' in the context of
1586 a derived class `D'. If `B::m' has been resolved to a BASELINK,
1587 then the most derived class indicated by the BASELINK_BINFO will be
1588 `B', not `D'. This function makes that adjustment. */
1591 adjust_result_of_qualified_name_lookup (tree decl
,
1592 tree qualifying_scope
,
1595 if (context_class
&& CLASS_TYPE_P (qualifying_scope
)
1596 && DERIVED_FROM_P (qualifying_scope
, context_class
)
1597 && BASELINK_P (decl
))
1601 my_friendly_assert (CLASS_TYPE_P (context_class
), 20020808);
1603 /* Look for the QUALIFYING_SCOPE as a base of the
1604 CONTEXT_CLASS. If QUALIFYING_SCOPE is ambiguous, we cannot
1605 be sure yet than an error has occurred; perhaps the function
1606 chosen by overload resolution will be static. */
1607 base
= lookup_base (context_class
, qualifying_scope
,
1608 ba_ignore
| ba_quiet
, NULL
);
1611 BASELINK_ACCESS_BINFO (decl
) = base
;
1612 BASELINK_BINFO (decl
)
1613 = lookup_base (base
, BINFO_TYPE (BASELINK_BINFO (decl
)),
1614 ba_ignore
| ba_quiet
,
1623 /* Walk the class hierarchy dominated by TYPE. FN is called for each
1624 type in the hierarchy, in a breadth-first preorder traversal.
1625 If it ever returns a non-NULL value, that value is immediately
1626 returned and the walk is terminated. At each node, FN is passed a
1627 BINFO indicating the path from the curently visited base-class to
1628 TYPE. Before each base-class is walked QFN is called. If the
1629 value returned is nonzero, the base-class is walked; otherwise it
1630 is not. If QFN is NULL, it is treated as a function which always
1631 returns 1. Both FN and QFN are passed the DATA whenever they are
1635 bfs_walk (tree binfo
, tree (*fn
) (tree
, void *),
1636 tree (*qfn
) (tree
, void *), void *data
)
1640 tree rval
= NULL_TREE
;
1641 /* An array of the base classes of BINFO. These will be built up in
1642 breadth-first order, except where QFN prunes the search. */
1643 varray_type bfs_bases
;
1645 /* Start with enough room for ten base classes. That will be enough
1646 for most hierarchies. */
1647 VARRAY_TREE_INIT (bfs_bases
, 10, "search_stack");
1649 /* Put the first type into the stack. */
1650 VARRAY_TREE (bfs_bases
, 0) = binfo
;
1653 for (head
= 0; head
< tail
; ++head
)
1659 /* Pull the next type out of the queue. */
1660 binfo
= VARRAY_TREE (bfs_bases
, head
);
1662 /* If this is the one we're looking for, we're done. */
1663 rval
= (*fn
) (binfo
, data
);
1667 /* Queue up the base types. */
1668 binfos
= BINFO_BASETYPES (binfo
);
1669 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
): 0;
1670 for (i
= 0; i
< n_baselinks
; i
++)
1672 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1675 base_binfo
= (*qfn
) (base_binfo
, data
);
1679 if (tail
== VARRAY_SIZE (bfs_bases
))
1680 VARRAY_GROW (bfs_bases
, 2 * VARRAY_SIZE (bfs_bases
));
1681 VARRAY_TREE (bfs_bases
, tail
) = base_binfo
;
1690 /* Exactly like bfs_walk, except that a depth-first traversal is
1691 performed, and PREFN is called in preorder, while POSTFN is called
1695 dfs_walk_real (tree binfo
,
1696 tree (*prefn
) (tree
, void *), tree (*postfn
) (tree
, void *),
1697 tree (*qfn
) (tree
, void *), void *data
)
1702 tree rval
= NULL_TREE
;
1704 /* Call the pre-order walking function. */
1707 rval
= (*prefn
) (binfo
, data
);
1712 /* Process the basetypes. */
1713 binfos
= BINFO_BASETYPES (binfo
);
1714 n_baselinks
= BINFO_N_BASETYPES (binfo
);
1715 for (i
= 0; i
< n_baselinks
; i
++)
1717 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1720 base_binfo
= (*qfn
) (base_binfo
, data
);
1724 rval
= dfs_walk_real (base_binfo
, prefn
, postfn
, qfn
, data
);
1730 /* Call the post-order walking function. */
1732 rval
= (*postfn
) (binfo
, data
);
1737 /* Exactly like bfs_walk, except that a depth-first post-order traversal is
1741 dfs_walk (tree binfo
, tree (*fn
) (tree
, void *),
1742 tree (*qfn
) (tree
, void *), void *data
)
1744 return dfs_walk_real (binfo
, 0, fn
, qfn
, data
);
1747 /* Check that virtual overrider OVERRIDER is acceptable for base function
1748 BASEFN. Issue diagnostic, and return zero, if unacceptable. */
1751 check_final_overrider (tree overrider
, tree basefn
)
1753 tree over_type
= TREE_TYPE (overrider
);
1754 tree base_type
= TREE_TYPE (basefn
);
1755 tree over_return
= TREE_TYPE (over_type
);
1756 tree base_return
= TREE_TYPE (base_type
);
1757 tree over_throw
= TYPE_RAISES_EXCEPTIONS (over_type
);
1758 tree base_throw
= TYPE_RAISES_EXCEPTIONS (base_type
);
1761 if (same_type_p (base_return
, over_return
))
1763 else if ((CLASS_TYPE_P (over_return
) && CLASS_TYPE_P (base_return
))
1764 || (TREE_CODE (base_return
) == TREE_CODE (over_return
)
1765 && POINTER_TYPE_P (base_return
)))
1767 /* Potentially covariant. */
1768 unsigned base_quals
, over_quals
;
1770 fail
= !POINTER_TYPE_P (base_return
);
1773 fail
= cp_type_quals (base_return
) != cp_type_quals (over_return
);
1775 base_return
= TREE_TYPE (base_return
);
1776 over_return
= TREE_TYPE (over_return
);
1778 base_quals
= cp_type_quals (base_return
);
1779 over_quals
= cp_type_quals (over_return
);
1781 if ((base_quals
& over_quals
) != over_quals
)
1784 if (CLASS_TYPE_P (base_return
) && CLASS_TYPE_P (over_return
))
1786 tree binfo
= lookup_base (over_return
, base_return
,
1787 ba_check
| ba_quiet
, NULL
);
1793 && can_convert (TREE_TYPE (base_type
), TREE_TYPE (over_type
)))
1794 /* GNU extension, allow trivial pointer conversions such as
1795 converting to void *, or qualification conversion. */
1797 /* can_convert will permit user defined conversion from a
1798 (reference to) class type. We must reject them. */
1799 over_return
= TREE_TYPE (over_type
);
1800 if (TREE_CODE (over_return
) == REFERENCE_TYPE
)
1801 over_return
= TREE_TYPE (over_return
);
1802 if (CLASS_TYPE_P (over_return
))
1812 else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
)))
1818 cp_error_at ("invalid covariant return type for `%#D'", overrider
);
1819 cp_error_at (" overriding `%#D'", basefn
);
1823 cp_error_at ("conflicting return type specified for `%#D'",
1825 cp_error_at (" overriding `%#D'", basefn
);
1827 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
),
1828 DECL_CONTEXT (overrider
));
1832 /* Check throw specifier is at least as strict. */
1833 if (!comp_except_specs (base_throw
, over_throw
, 0))
1835 if (!IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
)))
1837 cp_error_at ("looser throw specifier for `%#F'", overrider
);
1838 cp_error_at (" overriding `%#F'", basefn
);
1839 SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider
),
1840 DECL_CONTEXT (overrider
));
1848 /* Given a class TYPE, and a function decl FNDECL, look for
1849 virtual functions in TYPE's hierarchy which FNDECL overrides.
1850 We do not look in TYPE itself, only its bases.
1852 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
1853 find that it overrides anything.
1855 We check that every function which is overridden, is correctly
1859 look_for_overrides (tree type
, tree fndecl
)
1861 tree binfo
= TYPE_BINFO (type
);
1862 tree basebinfos
= BINFO_BASETYPES (binfo
);
1863 int nbasebinfos
= basebinfos
? TREE_VEC_LENGTH (basebinfos
) : 0;
1867 for (ix
= 0; ix
!= nbasebinfos
; ix
++)
1869 tree basetype
= BINFO_TYPE (TREE_VEC_ELT (basebinfos
, ix
));
1871 if (TYPE_POLYMORPHIC_P (basetype
))
1872 found
+= look_for_overrides_r (basetype
, fndecl
);
1877 /* Look in TYPE for virtual functions with the same signature as
1881 look_for_overrides_here (tree type
, tree fndecl
)
1885 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl
))
1886 ix
= CLASSTYPE_DESTRUCTOR_SLOT
;
1888 ix
= lookup_fnfields_1 (type
, DECL_NAME (fndecl
));
1891 tree fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type
), ix
);
1893 for (; fns
; fns
= OVL_NEXT (fns
))
1895 tree fn
= OVL_CURRENT (fns
);
1897 if (!DECL_VIRTUAL_P (fn
))
1898 /* Not a virtual. */;
1899 else if (DECL_CONTEXT (fn
) != type
)
1900 /* Introduced with a using declaration. */;
1901 else if (DECL_STATIC_FUNCTION_P (fndecl
))
1903 tree btypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1904 tree dtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1905 if (compparms (TREE_CHAIN (btypes
), dtypes
))
1908 else if (same_signature_p (fndecl
, fn
))
1915 /* Look in TYPE for virtual functions overridden by FNDECL. Check both
1916 TYPE itself and its bases. */
1919 look_for_overrides_r (tree type
, tree fndecl
)
1921 tree fn
= look_for_overrides_here (type
, fndecl
);
1924 if (DECL_STATIC_FUNCTION_P (fndecl
))
1926 /* A static member function cannot match an inherited
1927 virtual member function. */
1928 cp_error_at ("`%#D' cannot be declared", fndecl
);
1929 cp_error_at (" since `%#D' declared in base class", fn
);
1933 /* It's definitely virtual, even if not explicitly set. */
1934 DECL_VIRTUAL_P (fndecl
) = 1;
1935 check_final_overrider (fndecl
, fn
);
1940 /* We failed to find one declared in this class. Look in its bases. */
1941 return look_for_overrides (type
, fndecl
);
1944 /* A queue function to use with dfs_walk that only walks into
1945 canonical bases. DATA should be the type of the complete object,
1946 or a TREE_LIST whose TREE_PURPOSE is the type of the complete
1947 object. By using this function as a queue function, you will walk
1948 over exactly those BINFOs that actually exist in the complete
1949 object, including those for virtual base classes. If you
1950 SET_BINFO_MARKED for each binfo you process, you are further
1951 guaranteed that you will walk into each virtual base class exactly
1955 dfs_unmarked_real_bases_queue_p (tree binfo
, void *data
)
1957 if (TREE_VIA_VIRTUAL (binfo
))
1959 tree type
= (tree
) data
;
1961 if (TREE_CODE (type
) == TREE_LIST
)
1962 type
= TREE_PURPOSE (type
);
1963 binfo
= binfo_for_vbase (BINFO_TYPE (binfo
), type
);
1965 return unmarkedp (binfo
, NULL
);
1968 /* Like dfs_unmarked_real_bases_queue_p but walks only into things
1969 that are marked, rather than unmarked. */
1972 dfs_marked_real_bases_queue_p (tree binfo
, void *data
)
1974 if (TREE_VIA_VIRTUAL (binfo
))
1976 tree type
= (tree
) data
;
1978 if (TREE_CODE (type
) == TREE_LIST
)
1979 type
= TREE_PURPOSE (type
);
1980 binfo
= binfo_for_vbase (BINFO_TYPE (binfo
), type
);
1982 return markedp (binfo
, NULL
);
1985 /* A queue function that skips all virtual bases (and their
1989 dfs_skip_vbases (tree binfo
, void *data ATTRIBUTE_UNUSED
)
1991 if (TREE_VIA_VIRTUAL (binfo
))
1997 /* Called via dfs_walk from dfs_get_pure_virtuals. */
2000 dfs_get_pure_virtuals (tree binfo
, void *data
)
2002 tree type
= (tree
) data
;
2004 /* We're not interested in primary base classes; the derived class
2005 of which they are a primary base will contain the information we
2007 if (!BINFO_PRIMARY_P (binfo
))
2011 for (virtuals
= BINFO_VIRTUALS (binfo
);
2013 virtuals
= TREE_CHAIN (virtuals
))
2014 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals
)))
2015 CLASSTYPE_PURE_VIRTUALS (type
)
2016 = tree_cons (NULL_TREE
, BV_FN (virtuals
),
2017 CLASSTYPE_PURE_VIRTUALS (type
));
2020 SET_BINFO_MARKED (binfo
);
2025 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */
2028 get_pure_virtuals (tree type
)
2032 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
2033 is going to be overridden. */
2034 CLASSTYPE_PURE_VIRTUALS (type
) = NULL_TREE
;
2035 /* Now, run through all the bases which are not primary bases, and
2036 collect the pure virtual functions. We look at the vtable in
2037 each class to determine what pure virtual functions are present.
2038 (A primary base is not interesting because the derived class of
2039 which it is a primary base will contain vtable entries for the
2040 pure virtuals in the base class. */
2041 dfs_walk (TYPE_BINFO (type
), dfs_get_pure_virtuals
,
2042 dfs_unmarked_real_bases_queue_p
, type
);
2043 dfs_walk (TYPE_BINFO (type
), dfs_unmark
,
2044 dfs_marked_real_bases_queue_p
, type
);
2046 /* Put the pure virtuals in dfs order. */
2047 CLASSTYPE_PURE_VIRTUALS (type
) = nreverse (CLASSTYPE_PURE_VIRTUALS (type
));
2049 for (vbases
= CLASSTYPE_VBASECLASSES (type
);
2051 vbases
= TREE_CHAIN (vbases
))
2055 for (virtuals
= BINFO_VIRTUALS (TREE_VALUE (vbases
));
2057 virtuals
= TREE_CHAIN (virtuals
))
2059 tree base_fndecl
= BV_FN (virtuals
);
2060 if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl
))
2061 error ("`%#D' needs a final overrider", base_fndecl
);
2066 /* DEPTH-FIRST SEARCH ROUTINES. */
2069 markedp (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2071 return BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2075 unmarkedp (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2077 return !BINFO_MARKED (binfo
) ? binfo
: NULL_TREE
;
2081 marked_vtable_pathp (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2083 return BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2087 unmarked_vtable_pathp (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2089 return !BINFO_VTABLE_PATH_MARKED (binfo
) ? binfo
: NULL_TREE
;
2093 marked_pushdecls_p (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2095 return (CLASS_TYPE_P (BINFO_TYPE (binfo
))
2096 && !dependent_base_p (binfo
)
2097 && BINFO_PUSHDECLS_MARKED (binfo
)) ? binfo
: NULL_TREE
;
2101 unmarked_pushdecls_p (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2103 return (CLASS_TYPE_P (BINFO_TYPE (binfo
))
2104 && !dependent_base_p (binfo
)
2105 && !BINFO_PUSHDECLS_MARKED (binfo
)) ? binfo
: NULL_TREE
;
2108 /* The worker functions for `dfs_walk'. These do not need to
2109 test anything (vis a vis marking) if they are paired with
2110 a predicate function (above). */
2113 dfs_unmark (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2115 CLEAR_BINFO_MARKED (binfo
);
2119 /* get virtual base class types.
2120 This adds type to the vbase_types list in reverse dfs order.
2121 Ordering is very important, so don't change it. */
2124 dfs_get_vbase_types (tree binfo
, void *data
)
2126 tree type
= (tree
) data
;
2128 if (TREE_VIA_VIRTUAL (binfo
))
2129 CLASSTYPE_VBASECLASSES (type
)
2130 = tree_cons (BINFO_TYPE (binfo
),
2132 CLASSTYPE_VBASECLASSES (type
));
2133 SET_BINFO_MARKED (binfo
);
2137 /* Called via dfs_walk from mark_primary_bases. Builds the
2138 inheritance graph order list of BINFOs. */
2141 dfs_build_inheritance_graph_order (tree binfo
, void *data
)
2143 tree
*last_binfo
= (tree
*) data
;
2146 TREE_CHAIN (*last_binfo
) = binfo
;
2147 *last_binfo
= binfo
;
2148 SET_BINFO_MARKED (binfo
);
2152 /* Set CLASSTYPE_VBASECLASSES for TYPE. */
2155 get_vbase_types (tree type
)
2159 CLASSTYPE_VBASECLASSES (type
) = NULL_TREE
;
2160 dfs_walk (TYPE_BINFO (type
), dfs_get_vbase_types
, unmarkedp
, type
);
2161 /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
2162 reverse it so that we get normal dfs ordering. */
2163 CLASSTYPE_VBASECLASSES (type
) = nreverse (CLASSTYPE_VBASECLASSES (type
));
2164 dfs_walk (TYPE_BINFO (type
), dfs_unmark
, markedp
, 0);
2165 /* Thread the BINFOs in inheritance-graph order. */
2167 dfs_walk_real (TYPE_BINFO (type
),
2168 dfs_build_inheritance_graph_order
,
2172 dfs_walk (TYPE_BINFO (type
), dfs_unmark
, markedp
, NULL
);
2175 /* Called from find_vbase_instance via dfs_walk. */
2178 dfs_find_vbase_instance (tree binfo
, void *data
)
2180 tree base
= TREE_VALUE ((tree
) data
);
2182 if (BINFO_PRIMARY_P (binfo
)
2183 && same_type_p (BINFO_TYPE (binfo
), base
))
2189 /* Find the real occurrence of the virtual BASE (a class type) in the
2190 hierarchy dominated by TYPE. */
2193 find_vbase_instance (tree base
, tree type
)
2197 instance
= binfo_for_vbase (base
, type
);
2198 if (!BINFO_PRIMARY_P (instance
))
2201 return dfs_walk (TYPE_BINFO (type
),
2202 dfs_find_vbase_instance
,
2204 build_tree_list (type
, base
));
2208 /* Debug info for C++ classes can get very large; try to avoid
2209 emitting it everywhere.
2211 Note that this optimization wins even when the target supports
2212 BINCL (if only slightly), and reduces the amount of work for the
2216 maybe_suppress_debug_info (tree t
)
2218 /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
2219 does not support name references between translation units. It supports
2220 symbolic references between translation units, but only within a single
2221 executable or shared library.
2223 For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
2224 that the type was never defined, so we only get the members we
2226 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== NO_DEBUG
)
2229 /* We might have set this earlier in cp_finish_decl. */
2230 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 0;
2232 /* If we already know how we're handling this class, handle debug info
2234 if (CLASSTYPE_INTERFACE_KNOWN (t
))
2236 if (CLASSTYPE_INTERFACE_ONLY (t
))
2237 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
2238 /* else don't set it. */
2240 /* If the class has a vtable, write out the debug info along with
2242 else if (TYPE_CONTAINS_VPTR_P (t
))
2243 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t
)) = 1;
2245 /* Otherwise, just emit the debug info normally. */
2248 /* Note that we want debugging information for a base class of a class
2249 whose vtable is being emitted. Normally, this would happen because
2250 calling the constructor for a derived class implies calling the
2251 constructors for all bases, which involve initializing the
2252 appropriate vptr with the vtable for the base class; but in the
2253 presence of optimization, this initialization may be optimized
2254 away, so we tell finish_vtable_vardecl that we want the debugging
2255 information anyway. */
2258 dfs_debug_mark (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2260 tree t
= BINFO_TYPE (binfo
);
2262 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2267 /* Returns BINFO if we haven't already noted that we want debugging
2268 info for this base class. */
2271 dfs_debug_unmarkedp (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2273 return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo
))
2274 ? binfo
: NULL_TREE
);
2277 /* Write out the debugging information for TYPE, whose vtable is being
2278 emitted. Also walk through our bases and note that we want to
2279 write out information for them. This avoids the problem of not
2280 writing any debug info for intermediate basetypes whose
2281 constructors, and thus the references to their vtables, and thus
2282 the vtables themselves, were optimized away. */
2285 note_debug_info_needed (tree type
)
2287 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type
)))
2289 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type
)) = 0;
2290 rest_of_type_compilation (type
, toplevel_bindings_p ());
2293 dfs_walk (TYPE_BINFO (type
), dfs_debug_mark
, dfs_debug_unmarkedp
, 0);
2296 /* Subroutines of push_class_decls (). */
2298 /* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
2299 because it (or one of the intermediate bases) depends on template parms. */
2302 dependent_base_p (tree binfo
)
2304 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2306 if (currently_open_class (TREE_TYPE (binfo
)))
2308 if (dependent_type_p (TREE_TYPE (binfo
)))
2315 setup_class_bindings (tree name
, int type_binding_p
)
2317 tree type_binding
= NULL_TREE
;
2320 /* If we've already done the lookup for this declaration, we're
2322 if (IDENTIFIER_CLASS_VALUE (name
))
2325 /* First, deal with the type binding. */
2328 type_binding
= lookup_member (current_class_type
, name
,
2329 /*protect=*/2, /*want_type=*/true);
2330 if (TREE_CODE (type_binding
) == TREE_LIST
2331 && TREE_TYPE (type_binding
) == error_mark_node
)
2332 /* NAME is ambiguous. */
2333 push_class_level_binding (name
, type_binding
);
2335 pushdecl_class_level (type_binding
);
2338 /* Now, do the value binding. */
2339 value_binding
= lookup_member (current_class_type
, name
,
2340 /*protect=*/2, /*want_type=*/false);
2343 && (TREE_CODE (value_binding
) == TYPE_DECL
2344 || DECL_CLASS_TEMPLATE_P (value_binding
)
2345 || (TREE_CODE (value_binding
) == TREE_LIST
2346 && TREE_TYPE (value_binding
) == error_mark_node
2347 && (TREE_CODE (TREE_VALUE (value_binding
))
2349 /* We found a type-binding, even when looking for a non-type
2350 binding. This means that we already processed this binding
2352 else if (value_binding
)
2354 if (TREE_CODE (value_binding
) == TREE_LIST
2355 && TREE_TYPE (value_binding
) == error_mark_node
)
2356 /* NAME is ambiguous. */
2357 push_class_level_binding (name
, value_binding
);
2360 if (BASELINK_P (value_binding
))
2361 /* NAME is some overloaded functions. */
2362 value_binding
= BASELINK_FUNCTIONS (value_binding
);
2363 pushdecl_class_level (value_binding
);
2368 /* Push class-level declarations for any names appearing in BINFO that
2372 dfs_push_type_decls (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2377 type
= BINFO_TYPE (binfo
);
2378 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
2379 if (DECL_NAME (fields
) && TREE_CODE (fields
) == TYPE_DECL
2380 && !(!same_type_p (type
, current_class_type
)
2381 && template_self_reference_p (type
, fields
)))
2382 setup_class_bindings (DECL_NAME (fields
), /*type_binding_p=*/1);
2384 /* We can't just use BINFO_MARKED because envelope_add_decl uses
2385 DERIVED_FROM_P, which calls get_base_distance. */
2386 SET_BINFO_PUSHDECLS_MARKED (binfo
);
2391 /* Push class-level declarations for any names appearing in BINFO that
2392 are not TYPE_DECLS. */
2395 dfs_push_decls (tree binfo
, void *data
)
2401 type
= BINFO_TYPE (binfo
);
2402 dep_base_p
= (processing_template_decl
&& type
!= current_class_type
2403 && dependent_base_p (binfo
));
2407 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
2408 if (DECL_NAME (fields
)
2409 && TREE_CODE (fields
) != TYPE_DECL
2410 && TREE_CODE (fields
) != USING_DECL
2411 && !DECL_ARTIFICIAL (fields
))
2412 setup_class_bindings (DECL_NAME (fields
), /*type_binding_p=*/0);
2413 else if (TREE_CODE (fields
) == FIELD_DECL
2414 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
2415 dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields
)), data
);
2417 method_vec
= (CLASS_TYPE_P (type
)
2418 ? CLASSTYPE_METHOD_VEC (type
) : NULL_TREE
);
2420 if (method_vec
&& TREE_VEC_LENGTH (method_vec
) >= 3)
2425 /* Farm out constructors and destructors. */
2426 end
= TREE_VEC_END (method_vec
);
2428 for (methods
= &TREE_VEC_ELT (method_vec
, 2);
2429 methods
< end
&& *methods
;
2431 setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods
)),
2432 /*type_binding_p=*/0);
2436 CLEAR_BINFO_PUSHDECLS_MARKED (binfo
);
2441 /* When entering the scope of a class, we cache all of the
2442 fields that that class provides within its inheritance
2443 lattice. Where ambiguities result, we mark them
2444 with `error_mark_node' so that if they are encountered
2445 without explicit qualification, we can emit an error
2449 push_class_decls (tree type
)
2451 search_stack
= push_search_level (search_stack
, &search_obstack
);
2453 /* Enter type declarations and mark. */
2454 dfs_walk (TYPE_BINFO (type
), dfs_push_type_decls
, unmarked_pushdecls_p
, 0);
2456 /* Enter non-type declarations and unmark. */
2457 dfs_walk (TYPE_BINFO (type
), dfs_push_decls
, marked_pushdecls_p
, 0);
2460 /* Here's a subroutine we need because C lacks lambdas. */
2463 dfs_unuse_fields (tree binfo
, void *data ATTRIBUTE_UNUSED
)
2465 tree type
= TREE_TYPE (binfo
);
2468 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
2470 if (TREE_CODE (fields
) != FIELD_DECL
|| DECL_ARTIFICIAL (fields
))
2473 TREE_USED (fields
) = 0;
2474 if (DECL_NAME (fields
) == NULL_TREE
2475 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
2476 unuse_fields (TREE_TYPE (fields
));
2483 unuse_fields (tree type
)
2485 dfs_walk (TYPE_BINFO (type
), dfs_unuse_fields
, unmarkedp
, 0);
2491 /* We haven't pushed a search level when dealing with cached classes,
2492 so we'd better not try to pop it. */
2494 search_stack
= pop_search_level (search_stack
);
2498 print_search_statistics ()
2500 #ifdef GATHER_STATISTICS
2501 fprintf (stderr
, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
2502 n_fields_searched
, n_calls_lookup_field
, n_calls_lookup_field_1
);
2503 fprintf (stderr
, "%d fnfields searched in %d calls to lookup_fnfields\n",
2504 n_outer_fields_searched
, n_calls_lookup_fnfields
);
2505 fprintf (stderr
, "%d calls to get_base_type\n", n_calls_get_base_type
);
2506 #else /* GATHER_STATISTICS */
2507 fprintf (stderr
, "no search statistics\n");
2508 #endif /* GATHER_STATISTICS */
2512 init_search_processing ()
2514 gcc_obstack_init (&search_obstack
);
2518 reinit_search_statistics ()
2520 #ifdef GATHER_STATISTICS
2521 n_fields_searched
= 0;
2522 n_calls_lookup_field
= 0, n_calls_lookup_field_1
= 0;
2523 n_calls_lookup_fnfields
= 0, n_calls_lookup_fnfields_1
= 0;
2524 n_calls_get_base_type
= 0;
2525 n_outer_fields_searched
= 0;
2526 n_contexts_saved
= 0;
2527 #endif /* GATHER_STATISTICS */
2531 add_conversions (tree binfo
, void *data
)
2534 tree method_vec
= CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo
));
2535 tree
*conversions
= (tree
*) data
;
2537 /* Some builtin types have no method vector, not even an empty one. */
2541 for (i
= 2; i
< TREE_VEC_LENGTH (method_vec
); ++i
)
2543 tree tmp
= TREE_VEC_ELT (method_vec
, i
);
2546 if (!tmp
|| ! DECL_CONV_FN_P (OVL_CURRENT (tmp
)))
2549 name
= DECL_NAME (OVL_CURRENT (tmp
));
2551 /* Make sure we don't already have this conversion. */
2552 if (! IDENTIFIER_MARKED (name
))
2554 *conversions
= tree_cons (binfo
, tmp
, *conversions
);
2555 IDENTIFIER_MARKED (name
) = 1;
2561 /* Return a TREE_LIST containing all the non-hidden user-defined
2562 conversion functions for TYPE (and its base-classes). The
2563 TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
2564 containing the conversion functions. The TREE_PURPOSE is the BINFO
2565 from which the conversion functions in this node were selected. */
2568 lookup_conversions (tree type
)
2571 tree conversions
= NULL_TREE
;
2573 if (COMPLETE_TYPE_P (type
))
2574 bfs_walk (TYPE_BINFO (type
), add_conversions
, 0, &conversions
);
2576 for (t
= conversions
; t
; t
= TREE_CHAIN (t
))
2577 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t
)))) = 0;
2588 /* Check whether the empty class indicated by EMPTY_BINFO is also present
2589 at offset 0 in COMPARE_TYPE, and set found_overlap if so. */
2592 dfs_check_overlap (tree empty_binfo
, void *data
)
2594 struct overlap_info
*oi
= (struct overlap_info
*) data
;
2596 for (binfo
= TYPE_BINFO (oi
->compare_type
);
2598 binfo
= BINFO_BASETYPE (binfo
, 0))
2600 if (BINFO_TYPE (binfo
) == BINFO_TYPE (empty_binfo
))
2602 oi
->found_overlap
= 1;
2605 else if (BINFO_BASETYPES (binfo
) == NULL_TREE
)
2612 /* Trivial function to stop base traversal when we find something. */
2615 dfs_no_overlap_yet (tree binfo
, void *data
)
2617 struct overlap_info
*oi
= (struct overlap_info
*) data
;
2618 return !oi
->found_overlap
? binfo
: NULL_TREE
;
2621 /* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
2622 offset 0 in NEXT_TYPE. Used in laying out empty base class subobjects. */
2625 types_overlap_p (tree empty_type
, tree next_type
)
2627 struct overlap_info oi
;
2629 if (! IS_AGGR_TYPE (next_type
))
2631 oi
.compare_type
= next_type
;
2632 oi
.found_overlap
= 0;
2633 dfs_walk (TYPE_BINFO (empty_type
), dfs_check_overlap
,
2634 dfs_no_overlap_yet
, &oi
);
2635 return oi
.found_overlap
;
2638 /* Given a vtable VAR, determine which of the inherited classes the vtable
2639 inherits (in a loose sense) functions from.
2641 FIXME: This does not work with the new ABI. */
2644 binfo_for_vtable (tree var
)
2646 tree main_binfo
= TYPE_BINFO (DECL_CONTEXT (var
));
2647 tree binfos
= TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo
));
2648 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo
));
2651 for (i
= 0; i
< n_baseclasses
; i
++)
2653 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2654 if (base_binfo
!= NULL_TREE
&& BINFO_VTABLE (base_binfo
) == var
)
2658 /* If no secondary base classes matched, return the primary base, if
2660 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo
)))
2661 return get_primary_binfo (main_binfo
);
2666 /* Returns the binfo of the first direct or indirect virtual base derived
2667 from BINFO, or NULL if binfo is not via virtual. */
2670 binfo_from_vbase (tree binfo
)
2672 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2674 if (TREE_VIA_VIRTUAL (binfo
))
2680 /* Returns the binfo of the first direct or indirect virtual base derived
2681 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
2685 binfo_via_virtual (tree binfo
, tree limit
)
2687 for (; binfo
&& (!limit
|| !same_type_p (BINFO_TYPE (binfo
), limit
));
2688 binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
2690 if (TREE_VIA_VIRTUAL (binfo
))
2696 /* Returns the BINFO (if any) for the virtual baseclass T of the class
2697 C from the CLASSTYPE_VBASECLASSES list. */
2700 binfo_for_vbase (tree basetype
, tree classtype
)
2704 binfo
= purpose_member (basetype
, CLASSTYPE_VBASECLASSES (classtype
));
2705 return binfo
? TREE_VALUE (binfo
) : NULL_TREE
;